Skip to content

Commit d2cb8df

Browse files
authored
Convert CLIs to subcommands in modelarrayio CLI (#27)
1 parent ce9064a commit d2cb8df

14 files changed

Lines changed: 376 additions & 334 deletions

docs/examples/plot_fixel_workflow.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
# # activate your conda environment first
9999
# conda activate <env_name>
100100
#
101-
# confixel \
101+
# modelarrayio mif-to-h5 \
102102
# --index-file /home/username/myProject/data/FD/index.mif \
103103
# --directions-file /home/username/myProject/data/FD/directions.mif \
104104
# --cohort-file /home/username/myProject/data/cohort_FD.csv \
@@ -118,7 +118,7 @@
118118
#
119119
# .. code-block:: console
120120
#
121-
# fixelstats_write \
121+
# modelarrayio h5-to-mif \
122122
# --index-file /home/username/myProject/data/FD/index.mif \
123123
# --directions-file /home/username/myProject/data/FD/directions.mif \
124124
# --cohort-file /home/username/myProject/data/cohort_FD.csv \
@@ -144,7 +144,7 @@
144144
#
145145
# .. code-block:: console
146146
#
147-
# confixel --help
148-
# fixelstats_write --help
147+
# modelarrayio mif-to-h5 --help
148+
# modelarrayio h5-to-mif --help
149149
#
150150
# or in the :doc:`/usage` page of this documentation.

docs/examples/plot_voxel_workflow.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104
# # activate your conda environment first
105105
# conda activate <env_name>
106106
#
107-
# convoxel \
107+
# modelarrayio nifti-to-h5 \
108108
# --group-mask-file /home/username/myProject/data/group_mask.nii.gz \
109109
# --cohort-file /home/username/myProject/data/cohort_FA.csv \
110110
# --output-hdf5 /home/username/myProject/data/FA.h5
@@ -121,7 +121,7 @@
121121
#
122122
# .. code-block:: console
123123
#
124-
# volumestats_write \
124+
# modelarrayio h5-to-nifti \
125125
# --group-mask-file /home/username/myProject/data/group_mask.nii.gz \
126126
# --cohort-file /home/username/myProject/data/cohort_FA.csv \
127127
# --analysis-name mylm \
@@ -177,7 +177,7 @@
177177
#
178178
# .. code-block:: console
179179
#
180-
# convoxel --help
181-
# volumestats_write --help
180+
# modelarrayio nifti-to-h5 --help
181+
# modelarrayio h5-to-nifti --help
182182
#
183183
# or in the :doc:`/usage` page of this documentation.

docs/usage.rst

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,60 @@ Usage
33
#####
44

55

6-
********
7-
confixel
8-
********
6+
**********
7+
mif-to-h5
8+
**********
99

1010
.. argparse::
11-
:ref: modelarrayio.cli.mif_to_h5.get_parser
12-
:prog: confixel
13-
:func: get_parser
11+
:ref: modelarrayio.cli.mif_to_h5._parse_mif_to_h5
12+
:prog: modelarrayio mif-to-h5
13+
:func: _parse_mif_to_h5
1414

1515

16-
********
17-
convoxel
18-
********
16+
**********
17+
nifti-to-h5
18+
**********
1919

2020
.. argparse::
21-
:ref: modelarrayio.cli.nifti_to_h5.get_parser
22-
:prog: convoxel
23-
:func: get_parser
21+
:ref: modelarrayio.cli.nifti_to_h5._parse_nifti_to_h5
22+
:prog: modelarrayio nifti-to-h5
23+
:func: _parse_nifti_to_h5
2424

2525

26-
********
27-
concifti
28-
********
26+
**********
27+
cifti-to-h5
28+
**********
2929

3030
.. argparse::
31-
:ref: modelarrayio.cli.cifti_to_h5.get_parser
32-
:prog: concifti
33-
:func: get_parser
31+
:ref: modelarrayio.cli.cifti_to_h5._parse_cifti_to_h5
32+
:prog: modelarrayio cifti-to-h5
33+
:func: _parse_cifti_to_h5
3434

3535

36-
****************
37-
fixelstats_write
38-
****************
36+
**********
37+
h5-to-mif
38+
**********
3939

4040
.. argparse::
41-
:ref: modelarrayio.cli.h5_to_mif.get_parser
42-
:prog: fixelstats_write
43-
:func: get_parser
41+
:ref: modelarrayio.cli.h5_to_mif._parse_h5_to_mif
42+
:prog: modelarrayio h5-to-mif
43+
:func: _parse_h5_to_mif
4444

45-
*****************
46-
volumestats_write
47-
*****************
45+
***********
46+
h5-to-nifti
47+
***********
4848

4949
.. argparse::
50-
:ref: modelarrayio.cli.h5_to_nifti.get_parser
51-
:prog: volumestats_write
52-
:func: get_parser
50+
:ref: modelarrayio.cli.h5_to_nifti._parse_h5_to_nifti
51+
:prog: modelarrayio h5-to-nifti
52+
:func: _parse_h5_to_nifti
5353

5454

55-
****************
56-
ciftistats_write
57-
****************
55+
***********
56+
h5-to-cifti
57+
***********
5858

5959
.. argparse::
60-
:ref: modelarrayio.cli.h5_to_cifti.get_parser
61-
:prog: ciftistats_write
62-
:func: get_parser
60+
:ref: modelarrayio.cli.h5_to_cifti._parse_h5_to_cifti
61+
:prog: modelarrayio h5-to-cifti
62+
:func: _parse_h5_to_cifti

pyproject.toml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,7 @@ Repository = "https://github.com/PennLINC/ModelArrayIO"
6767
Documentation = "https://modelarrayio.readthedocs.io"
6868

6969
[project.scripts]
70-
confixel = "modelarrayio.cli.mif_to_h5:main"
71-
convoxel = "modelarrayio.cli.nifti_to_h5:main"
72-
concifti = "modelarrayio.cli.cifti_to_h5:main"
73-
fixelstats_write = "modelarrayio.cli.h5_to_mif:main"
74-
volumestats_write = "modelarrayio.cli.h5_to_nifti:main"
75-
ciftistats_write = "modelarrayio.cli.h5_to_cifti:main"
70+
modelarrayio = "modelarrayio.cli.main:main"
7671

7772
#
7873
# Hatch

src/modelarrayio/cli/cifti_to_h5.py

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,55 @@ def _process_scalar_job(scalar_name, source_files):
204204
return 0
205205

206206

207-
def get_parser():
207+
def cifti_to_h5_main(
208+
cohort_file,
209+
backend='hdf5',
210+
output_hdf5='fixelarray.h5',
211+
output_tiledb='arraydb.tdb',
212+
storage_dtype='float32',
213+
compression='gzip',
214+
compression_level=4,
215+
shuffle=True,
216+
chunk_voxels=0,
217+
target_chunk_mb=2.0,
218+
tdb_compression='zstd',
219+
tdb_compression_level=5,
220+
tdb_shuffle=True,
221+
tdb_tile_voxels=0,
222+
tdb_target_tile_mb=2.0,
223+
tdb_workers=None,
224+
scalar_columns=None,
225+
s3_workers=1,
226+
log_level='INFO',
227+
):
228+
"""Entry point for the ``modelarrayio cifti-to-h5`` command."""
229+
logging.basicConfig(
230+
level=getattr(logging, str(log_level).upper(), logging.INFO),
231+
format='[%(levelname)s] %(name)s: %(message)s',
232+
)
233+
return cifti_to_h5(
234+
cohort_file=cohort_file,
235+
backend=backend,
236+
output_hdf5=output_hdf5,
237+
output_tiledb=output_tiledb,
238+
storage_dtype=storage_dtype,
239+
compression=compression,
240+
compression_level=compression_level,
241+
shuffle=shuffle,
242+
chunk_voxels=chunk_voxels,
243+
target_chunk_mb=target_chunk_mb,
244+
tdb_compression=tdb_compression,
245+
tdb_compression_level=tdb_compression_level,
246+
tdb_shuffle=tdb_shuffle,
247+
tdb_tile_voxels=tdb_tile_voxels,
248+
tdb_target_tile_mb=tdb_target_tile_mb,
249+
tdb_workers=tdb_workers,
250+
scalar_columns=scalar_columns,
251+
s3_workers=s3_workers,
252+
)
253+
254+
255+
def _parse_cifti_to_h5():
208256
parser = argparse.ArgumentParser(
209257
description='Create a hdf5 file of CIDTI2 dscalar data',
210258
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
@@ -228,15 +276,3 @@ def get_parser():
228276
)
229277
add_s3_workers_arg(parser)
230278
return parser
231-
232-
233-
def main():
234-
parser = get_parser()
235-
args = parser.parse_args()
236-
kwargs = vars(args)
237-
log_level = kwargs.pop('log_level')
238-
logging.basicConfig(
239-
level=getattr(logging, str(log_level).upper(), logging.INFO),
240-
format='[%(levelname)s] %(name)s: %(message)s',
241-
)
242-
return cifti_to_h5(**kwargs)

src/modelarrayio/cli/h5_to_cifti.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,34 @@ def h5_to_cifti(example_cifti, in_file, analysis_name, output_dir):
9090
temp_nifti2_1mpvalue.to_filename(out_cifti_1mpvalue)
9191

9292

93-
def main():
94-
"""Write the contents of an hdf5 file to a cifti directory."""
95-
parser = get_parser()
96-
args = parser.parse_args()
97-
98-
if os.path.exists(args.output_dir):
93+
def h5_to_cifti_main(
94+
analysis_name,
95+
in_file,
96+
output_dir,
97+
cohort_file=None,
98+
example_cifti=None,
99+
):
100+
"""Entry point for the ``modelarrayio h5-to-cifti`` command."""
101+
if os.path.exists(output_dir):
99102
print('WARNING: Output directory exists')
100-
os.makedirs(args.output_dir, exist_ok=True)
103+
os.makedirs(output_dir, exist_ok=True)
101104

102-
# Get an example cifti
103-
example_cifti = args.example_cifti
104105
if example_cifti is None:
105106
logger.warning(
106107
'No example cifti file provided, using the first cifti file from the cohort file'
107108
)
108-
cohort_df = pd.read_csv(args.cohort_file)
109+
cohort_df = pd.read_csv(cohort_file)
109110
example_cifti = cohort_df['source_file'][0]
110111

111112
h5_to_cifti(
112113
example_cifti=example_cifti,
113-
in_file=args.in_file,
114-
analysis_name=args.analysis_name,
115-
output_dir=args.output_dir,
114+
in_file=in_file,
115+
analysis_name=analysis_name,
116+
output_dir=output_dir,
116117
)
117118

118119

119-
def get_parser():
120+
def _parse_h5_to_cifti():
120121
parser = argparse.ArgumentParser(
121122
description='Create a directory with cifti results from an hdf5 file',
122123
formatter_class=argparse.ArgumentDefaultsHelpFormatter,

src/modelarrayio/cli/h5_to_mif.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,32 @@ def h5_to_mif(example_mif, in_file, analysis_name, output_dir):
9292
nifti2_to_mif(temp_nifti2_1mpvalue, out_mif_1mpvalue)
9393

9494

95-
def main():
96-
parser = get_parser()
97-
args = parser.parse_args()
98-
99-
if os.path.exists(args.output_dir):
95+
def h5_to_mif_main(index_file, directions_file, cohort_file, analysis_name, in_file, output_dir):
96+
"""Entry point for the ``modelarrayio h5-to-mif`` command."""
97+
if os.path.exists(output_dir):
10098
print('WARNING: Output directory exists')
101-
os.makedirs(args.output_dir, exist_ok=True)
99+
os.makedirs(output_dir, exist_ok=True)
102100

103-
# Copy in the index and directions
104101
shutil.copyfile(
105-
args.directions_file,
106-
os.path.join(args.output_dir, os.path.basename(args.directions_file)),
102+
directions_file,
103+
os.path.join(output_dir, os.path.basename(directions_file)),
107104
)
108105
shutil.copyfile(
109-
args.index_file,
110-
os.path.join(args.output_dir, os.path.basename(args.index_file)),
106+
index_file,
107+
os.path.join(output_dir, os.path.basename(index_file)),
111108
)
112109

113-
# Get an example mif file
114-
cohort_df = pd.read_csv(args.cohort_file)
110+
cohort_df = pd.read_csv(cohort_file)
115111
example_mif = cohort_df['source_file'][0]
116112
h5_to_mif(
117113
example_mif=example_mif,
118-
in_file=args.in_file,
119-
analysis_name=args.analysis_name,
120-
output_dir=args.output_dir,
114+
in_file=in_file,
115+
analysis_name=analysis_name,
116+
output_dir=output_dir,
121117
)
122118

123119

124-
def get_parser():
120+
def _parse_h5_to_mif():
125121
parser = argparse.ArgumentParser(
126122
description='Create a fixel directory from an hdf5 file',
127123
formatter_class=argparse.ArgumentDefaultsHelpFormatter,

src/modelarrayio/cli/h5_to_nifti.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,28 @@ def _decode_names(arr):
126126
output_img_1mpvalue.to_filename(out_file_1mpvalue)
127127

128128

129-
def main():
130-
parser = get_parser()
131-
args = parser.parse_args()
132-
133-
if os.path.exists(args.output_dir):
129+
def h5_to_nifti_main(
130+
group_mask_file,
131+
analysis_name,
132+
in_file,
133+
output_dir,
134+
output_extension='.nii.gz',
135+
):
136+
"""Entry point for the ``modelarrayio h5-to-nifti`` command."""
137+
if os.path.exists(output_dir):
134138
print('WARNING: Output directory exists')
135-
os.makedirs(args.output_dir, exist_ok=True)
136-
137-
h5_to_nifti(*vars(args))
139+
os.makedirs(output_dir, exist_ok=True)
140+
141+
h5_to_nifti(
142+
in_file=in_file,
143+
analysis_name=analysis_name,
144+
group_mask_file=group_mask_file,
145+
output_extension=output_extension,
146+
output_dir=output_dir,
147+
)
138148

139149

140-
def get_parser():
150+
def _parse_h5_to_nifti():
141151
parser = argparse.ArgumentParser(
142152
description='Convert statistical results from an hdf5 file to a volume data (NIfTI file)',
143153
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
@@ -150,13 +160,6 @@ def get_parser():
150160
required=True,
151161
type=IsFile,
152162
)
153-
parser.add_argument(
154-
'--cohort-file',
155-
'--cohort_file',
156-
help='Path to a csv with demographic info and paths to data.',
157-
required=True,
158-
type=IsFile,
159-
)
160163
parser.add_argument(
161164
'--analysis-name',
162165
'--analysis_name',
@@ -180,6 +183,7 @@ def get_parser():
180183
parser.add_argument(
181184
'--output-ext',
182185
'--output_ext',
186+
dest='output_extension',
183187
help=(
184188
'The extension for output volume data. '
185189
'Options are .nii.gz (default) and .nii. Please provide the prefix dot.'

0 commit comments

Comments
 (0)