|
| 1 | +""" |
| 2 | +CIFTI (Greyordinate-wise) Data Conversion |
| 3 | +========================================= |
| 4 | +
|
| 5 | +For imaging data in CIFTI format, use the ``modelarrayio cifti-to-h5`` command to convert |
| 6 | +the CIFTI files to the HDF5 format (``.h5``) used by **ModelArray**, |
| 7 | +and ``modelarrayio h5-to-cifti`` to export results back to CIFTI. |
| 8 | +The CIFTI workflow is very similar to the MIF workflow |
| 9 | +(:ref:`sphx_glr_auto_examples_plot_mif_workflow.py`). |
| 10 | +""" |
| 11 | + |
| 12 | +# %% |
| 13 | +# Prepare data |
| 14 | +# ------------ |
| 15 | +# |
| 16 | +# To convert a list of CIFTI files to ``.h5`` format, you need: |
| 17 | +# |
| 18 | +# 1. **A cohort CSV** describing every CIFTI file to include (one CSV per scalar recommended). |
| 19 | +# |
| 20 | +# Cohort CSV columns (names are fixed, not user-defined): |
| 21 | +# |
| 22 | +# * ``scalar_name`` — which metric is being analysed (e.g., ``FA``) |
| 23 | +# * ``source_file`` — path to the subject's CIFTI file |
| 24 | + |
| 25 | +# %% |
| 26 | +# Example folder structure |
| 27 | +# ------------------------ |
| 28 | +# |
| 29 | +# .. code-block:: text |
| 30 | +# |
| 31 | +# /home/username/myProject/data |
| 32 | +# | |
| 33 | +# ├── cohort_FA.csv |
| 34 | +# │ |
| 35 | +# ├── FA |
| 36 | +# │ ├── sub-01_FA.dscalar.nii |
| 37 | +# │ ├── sub-02_FA.dscalar.nii |
| 38 | +# │ ├── sub-03_FA.dscalar.nii |
| 39 | +# │ └── ... |
| 40 | +# │ |
| 41 | +# └── ... |
| 42 | +# |
| 43 | +# Corresponding ``cohort_FA.csv`` for scalar FA: |
| 44 | +# |
| 45 | +# .. list-table:: |
| 46 | +# :header-rows: 1 |
| 47 | +# :widths: auto |
| 48 | +# |
| 49 | +# * - **scalar_name** *(required)* |
| 50 | +# - **source_file** *(required)* |
| 51 | +# - subject_id |
| 52 | +# - age |
| 53 | +# - sex |
| 54 | +# * - FA |
| 55 | +# - /home/username/myProject/data/FA/sub-01_FA.dscalar.nii |
| 56 | +# - sub-01 |
| 57 | +# - 10 |
| 58 | +# - F |
| 59 | +# * - FA |
| 60 | +# - /home/username/myProject/data/FA/sub-02_FA.dscalar.nii |
| 61 | +# - sub-02 |
| 62 | +# - 20 |
| 63 | +# - M |
| 64 | +# * - FA |
| 65 | +# - /home/username/myProject/data/FA/sub-03_FA.dscalar.nii |
| 66 | +# - sub-03 |
| 67 | +# - 15 |
| 68 | +# - F |
| 69 | +# * - ... |
| 70 | +# - ... |
| 71 | +# - ... |
| 72 | +# - ... |
| 73 | +# - ... |
| 74 | +# |
| 75 | +# Notes: |
| 76 | +# |
| 77 | +# * Column order does not matter. |
| 78 | +# * Values are case-sensitive — folder names, file names, and scalar names must match exactly |
| 79 | +# between the CSV and disk. |
| 80 | + |
| 81 | +# %% |
| 82 | +# Convert CIFTI files to HDF5 |
| 83 | +# --------------------------- |
| 84 | +# |
| 85 | +# Using the FA dataset from the example above: |
| 86 | +# |
| 87 | +# .. code-block:: console |
| 88 | +# |
| 89 | +# # activate your conda environment first |
| 90 | +# conda activate <env_name> |
| 91 | +# |
| 92 | +# modelarrayio cifti-to-h5 \ |
| 93 | +# --cohort-file /home/username/myProject/data/cohort_FA.csv \ |
| 94 | +# --output /home/username/myProject/data/FA.h5 |
| 95 | +# |
| 96 | +# This produces ``FA.h5`` in ``/home/username/myProject/data``. You can then use |
| 97 | +# `ModelArray <https://pennlinc.github.io/ModelArray/>`_ to run statistical analyses on it. |
| 98 | + |
| 99 | +# %% |
| 100 | +# Convert result .h5 back to CIFTI |
| 101 | +# -------------------------------- |
| 102 | +# |
| 103 | +# After running **ModelArray** and obtaining statistical results inside ``FA.h5`` (suppose the |
| 104 | +# analysis name is ``"mylm"``), use ``modelarrayio h5-to-cifti`` to export them as CIFTI files. |
| 105 | +# |
| 106 | +# You must also provide an example CIFTI file to use as a template for the output. |
| 107 | +# |
| 108 | +# .. code-block:: console |
| 109 | +# |
| 110 | +# modelarrayio h5-to-cifti \ |
| 111 | +# --cohort-file /home/username/myProject/data/cohort_FA.csv \ |
| 112 | +# --analysis-name mylm \ |
| 113 | +# --input-hdf5 /home/username/myProject/data/FA.h5 \ |
| 114 | +# --output-dir /home/username/myProject/data/FA_stats \ |
| 115 | +# --example-cifti /home/username/myProject/data/FA/sub-01_FA.dscalar.nii |
| 116 | +# |
| 117 | +# All converted volume data are saved as ``float32``. Results in ``FA_stats`` can be viewed |
| 118 | +# with any CIFTI image viewer. |
| 119 | +# |
| 120 | +# .. warning:: |
| 121 | +# |
| 122 | +# If ``--output-dir`` already exists, ``modelarrayio h5-to-cifti`` will not delete it — you will |
| 123 | +# see ``WARNING: Output directory exists``. Existing files that are **not** part of the |
| 124 | +# current output list are left unchanged. Existing files that **are** part of the current |
| 125 | +# output list will be overwritten. To avoid confusion, consider manually deleting the output |
| 126 | +# directory before re-running ``modelarrayio h5-to-cifti``. |
| 127 | + |
| 128 | +# %% |
| 129 | +# Number-of-observations image |
| 130 | +# ---------------------------- |
| 131 | +# |
| 132 | +# If you requested ``nobs`` during model fitting in ModelArray, after conversion you will find |
| 133 | +# an image called ``*_model.nobs.dscalar.nii*``. With subject-specific masks, this image may be |
| 134 | +# inhomogeneous across voxels. |
| 135 | +# |
| 136 | +# Voxels that did not have sufficient subjects (due to subject-specific masking) are stored as |
| 137 | +# ``NaN`` in the HDF5 file. How different viewers display these greyordinates: |
| 138 | +# |
| 139 | +# .. list-table:: |
| 140 | +# :header-rows: 1 |
| 141 | +# :widths: auto |
| 142 | +# |
| 143 | +# * - Viewer |
| 144 | +# - Regular voxel |
| 145 | +# - Voxel without sufficient subjects |
| 146 | +# * - nibabel (Python) |
| 147 | +# - e.g. ``nobs: 209.0``, ``p.value: 0.01`` |
| 148 | +# - ``NaN`` |
| 149 | +# * - MRtrix mrview |
| 150 | +# - e.g. ``nobs: 209``, ``p.value: 0.01`` |
| 151 | +# - ``?`` |
| 152 | +# * - ITK-SNAP |
| 153 | +# - e.g. ``nobs: 209``, ``p.value: 0.01`` |
| 154 | +# - ``0`` (displayed, but excluded when thresholding) |
| 155 | + |
| 156 | +# %% |
| 157 | +# Additional help |
| 158 | +# --------------- |
| 159 | +# |
| 160 | +# Full argument documentation is available from the command line: |
| 161 | +# |
| 162 | +# .. code-block:: console |
| 163 | +# |
| 164 | +# modelarrayio cifti-to-h5 --help |
| 165 | +# modelarrayio h5-to-cifti --help |
| 166 | +# |
| 167 | +# or in the :doc:`/usage` page of this documentation. |
0 commit comments