-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmif_to_h5.py
More file actions
158 lines (139 loc) · 4.82 KB
/
mif_to_h5.py
File metadata and controls
158 lines (139 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
"""Convert MIF data to an HDF5 file."""
from __future__ import annotations
import argparse
import logging
from collections import defaultdict
from functools import partial
from pathlib import Path
import h5py
import pandas as pd
from tqdm import tqdm
from modelarrayio.cli import utils as cli_utils
from modelarrayio.cli.parser_utils import _is_file, add_to_modelarray_args
from modelarrayio.utils.fixels import gather_fixels, mif_to_image
logger = logging.getLogger(__name__)
def mif_to_h5(
index_file,
directions_file,
cohort_file,
backend='hdf5',
output=Path('fixelarray.h5'),
storage_dtype='float32',
compression='gzip',
compression_level=4,
shuffle=True,
chunk_voxels=0,
target_chunk_mb=2.0,
workers=None,
s3_workers=1,
):
"""Load all fixeldb data and write to an HDF5 or TileDB file.
Parameters
----------
index_file : :obj:`pathlib.Path`
Path to a Nifti2 index file
directions_file : :obj:`pathlib.Path`
Path to a Nifti2 directions file
cohort_file : :obj:`pathlib.Path`
Path to a csv with demographic info and paths to data
backend : :obj:`str`
Backend to use for storage (``'hdf5'`` or ``'tiledb'``)
output : :obj:`pathlib.Path`
Output path. For the hdf5 backend, path to an .h5 file;
for the tiledb backend, path to a .tdb directory.
storage_dtype : :obj:`str`
Floating type to store values
compression : :obj:`str`
Compression filter. ``gzip`` works for both backends;
``lzf`` is HDF5-only; ``zstd`` is TileDB-only.
compression_level : :obj:`int`
Compression level (codec-dependent)
shuffle : :obj:`bool`
Enable shuffle filter
chunk_voxels : :obj:`int`
Chunk/tile size along the fixel axis (0 = auto)
target_chunk_mb : :obj:`float`
Target chunk/tile size in MiB when auto-computing the spatial axis length
workers : :obj:`int`
Maximum number of parallel TileDB write workers. Default 0 (auto).
Has no effect when ``backend='hdf5'``.
s3_workers : :obj:`int`
Number of parallel workers for S3 downloads. Default 1.
Returns
-------
status : :obj:`int`
0 if successful, 1 if failed.
"""
# gather fixel data
fixel_table, voxel_table = gather_fixels(index_file, directions_file)
output_path = Path(output)
# gather cohort data
cohort_df = pd.read_csv(cohort_file)
# upload each cohort's data
scalars = defaultdict(list)
sources_lists = defaultdict(list)
logger.info('Extracting .mif data...')
for row in tqdm(cohort_df.itertuples(index=False), total=cohort_df.shape[0]):
scalar_file = row.source_file
_scalar_img, scalar_data = mif_to_image(scalar_file)
scalars[row.scalar_name].append(scalar_data)
sources_lists[row.scalar_name].append(row.source_file)
# Write the output
if backend == 'hdf5':
output_path = cli_utils.prepare_output_parent(output_path)
with h5py.File(output_path, 'w') as h5_file:
cli_utils.write_table_dataset(h5_file, 'fixels', fixel_table)
cli_utils.write_table_dataset(h5_file, 'voxels', voxel_table)
cli_utils.write_hdf5_scalar_matrices(
h5_file,
scalars,
sources_lists,
storage_dtype=storage_dtype,
compression=compression,
compression_level=compression_level,
shuffle=shuffle,
chunk_voxels=chunk_voxels,
target_chunk_mb=target_chunk_mb,
)
return int(not output_path.exists())
cli_utils.write_tiledb_scalar_matrices(
output_path,
scalars,
sources_lists,
storage_dtype=storage_dtype,
compression=compression,
compression_level=compression_level,
shuffle=shuffle,
chunk_voxels=chunk_voxels,
target_chunk_mb=target_chunk_mb,
)
return 0
def mif_to_h5_main(**kwargs):
"""Entry point for the ``modelarrayio mif-to-h5`` command."""
log_level = kwargs.pop('log_level', 'INFO')
cli_utils.configure_logging(log_level)
return mif_to_h5(**kwargs)
def _parse_mif_to_h5():
parser = argparse.ArgumentParser(
description='Create a hdf5 file of fixel data',
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
IsFile = partial(_is_file, parser=parser)
# MIF-specific arguments
parser.add_argument(
'--index-file',
'--index_file',
help='Index File',
required=True,
type=IsFile,
)
parser.add_argument(
'--directions-file',
'--directions_file',
help='Directions File',
required=True,
type=IsFile,
)
# Common arguments
add_to_modelarray_args(parser, default_output='fixelarray.h5')
return parser