-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
234 lines (200 loc) · 7.38 KB
/
utils.py
File metadata and controls
234 lines (200 loc) · 7.38 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""Shared CLI helpers for logging, outputs, and result metadata."""
from __future__ import annotations
import logging
import os
from collections.abc import Mapping, Sequence
from pathlib import Path
import h5py
import numpy as np
import pandas as pd
from modelarrayio.storage import h5_storage, tiledb_storage
def configure_logging(log_level: str) -> None:
"""Configure package logging once for CLI entry points."""
logging.basicConfig(
level=getattr(logging, str(log_level).upper(), logging.INFO),
format='[%(levelname)s] %(name)s: %(message)s',
)
def prepare_output_directory(output_dir: str | Path, logger: logging.Logger) -> Path:
"""Create an output directory and warn when reusing an existing path."""
output_path = Path(output_dir)
if output_path.exists():
logger.warning('Output directory exists: %s', output_path)
output_path.mkdir(parents=True, exist_ok=True)
return output_path
def prepare_output_parent(output_file: str | Path) -> Path:
"""Ensure the parent directory for an output file exists."""
output_path = Path(output_file)
output_path.parent.mkdir(parents=True, exist_ok=True)
return output_path
def write_table_dataset(
h5_file: h5py.File,
dataset_name: str,
table: pd.DataFrame,
*,
extra_attrs: Mapping[str, Sequence[str]] | None = None,
) -> h5py.Dataset:
"""Write a dataframe as a transposed HDF5 dataset with column metadata."""
dataset = h5_file.create_dataset(name=dataset_name, data=table.to_numpy().T)
dataset.attrs['column_names'] = list(table.columns)
for key, value in (extra_attrs or {}).items():
dataset.attrs[key] = list(value)
return dataset
def write_hdf5_scalar_matrices(
h5_file: h5py.File,
scalars: Mapping[str, Sequence[np.ndarray]],
sources_by_scalar: Mapping[str, Sequence[str]],
*,
storage_dtype: str,
compression: str,
compression_level: int,
shuffle: bool,
chunk_voxels: int,
target_chunk_mb: float,
) -> None:
"""Write per-scalar matrices into an open HDF5 file."""
for scalar_name, rows in scalars.items():
num_subjects = len(rows)
if num_subjects == 0:
continue
num_items = rows[0].shape[0]
dataset = h5_storage.create_empty_scalar_matrix_dataset(
h5_file,
f'scalars/{scalar_name}/values',
num_subjects,
num_items,
storage_dtype=storage_dtype,
compression=compression,
compression_level=compression_level,
shuffle=shuffle,
chunk_voxels=chunk_voxels,
target_chunk_mb=target_chunk_mb,
sources_list=sources_by_scalar[scalar_name],
)
h5_storage.write_rows_in_column_stripes(dataset, rows)
def write_tiledb_scalar_matrices(
output_dir: str | Path,
scalars: Mapping[str, Sequence[np.ndarray]],
sources_by_scalar: Mapping[str, Sequence[str]],
*,
storage_dtype: str,
compression: str,
compression_level: int,
shuffle: bool,
chunk_voxels: int,
target_chunk_mb: float,
write_column_name_arrays: bool = False,
) -> None:
"""Write per-scalar matrices into a TileDB directory."""
output_path = Path(output_dir)
output_path.mkdir(parents=True, exist_ok=True)
for scalar_name, rows in scalars.items():
num_subjects = len(rows)
if num_subjects == 0:
continue
num_items = rows[0].shape[0]
dataset_path = f'scalars/{scalar_name}/values'
tiledb_storage.create_empty_scalar_matrix_array(
str(output_path),
dataset_path,
num_subjects,
num_items,
storage_dtype=storage_dtype,
compression=compression,
compression_level=compression_level,
shuffle=shuffle,
tile_voxels=chunk_voxels,
target_tile_mb=target_chunk_mb,
sources_list=sources_by_scalar[scalar_name],
)
if write_column_name_arrays:
tiledb_storage.write_column_names(
str(output_path), scalar_name, sources_by_scalar[scalar_name]
)
tiledb_storage.write_rows_in_column_stripes(str(output_path / dataset_path), rows)
def write_hdf5_parcel_arrays(
h5_file: h5py.File,
parcel_arrays: Mapping[str, np.ndarray],
) -> None:
"""Write parcellated CIFTI parcel name arrays as HDF5 string datasets.
Creates one dataset per entry under the ``parcels/`` group.
Parameters
----------
h5_file : h5py.File
Open, writable HDF5 file.
parcel_arrays : mapping
Keys are dataset names (e.g. ``'parcel_id'``, ``'parcel_id_from'``,
``'parcel_id_to'``); values are arrays of parcel name strings.
"""
for name, values in parcel_arrays.items():
h5_file.create_dataset(
f'parcels/{name}',
data=np.array(values, dtype=object),
dtype=h5py.string_dtype(),
)
def write_tiledb_parcel_arrays(
base_uri: str | Path,
parcel_arrays: Mapping[str, np.ndarray],
) -> None:
"""Write parcellated CIFTI parcel name arrays as TileDB string arrays.
Creates one TileDB array per entry under the ``parcels/`` sub-path.
Parameters
----------
base_uri : str or Path
Root directory of the TileDB store.
parcel_arrays : mapping
Keys are array names (e.g. ``'parcel_id'``); values are arrays of
parcel name strings.
"""
for name, values in parcel_arrays.items():
tiledb_storage.write_parcel_names(
str(base_uri),
os.path.join('parcels', name),
[str(v) for v in values],
)
def sanitize_result_name(result_name: str) -> str:
"""Normalize an analysis result name for use in filenames."""
return str(result_name).replace(' ', '_').replace('/', '_')
def read_result_names(
h5_file: h5py.File,
analysis_name: str,
results_matrix: h5py.Dataset,
*,
logger: logging.Logger,
) -> list[str]:
"""Read result names from HDF5 metadata, with compatibility fallbacks."""
names_attr = results_matrix.attrs.get('colnames')
if names_attr is not None:
decoded = _decode_names(names_attr)
if decoded:
return decoded
candidate_paths = (
f'results/{analysis_name}/column_names',
f'results/{analysis_name}/results_matrix/column_names',
)
for path in candidate_paths:
if path not in h5_file:
continue
try:
decoded = _decode_names(h5_file[path][()])
except (KeyError, OSError, RuntimeError, TypeError, ValueError):
logger.debug('Could not read column names from %s', path, exc_info=True)
continue
if decoded:
return decoded
logger.warning("Unable to read column names, using 'componentNNN' instead")
return [f'component{n + 1:03d}' for n in range(results_matrix.shape[0])]
def _decode_names(values: object) -> list[str]:
if isinstance(values, np.ndarray):
sequence = values.tolist()
elif isinstance(values, (list, tuple)):
sequence = list(values)
else:
sequence = [values]
decoded: list[str] = []
for value in sequence:
if isinstance(value, (bytes, bytearray, np.bytes_)):
text = value.decode('utf-8', errors='ignore')
else:
text = str(value)
decoded.append(text.rstrip('\x00').strip())
return [name for name in decoded if name]