Skip to content

Commit 2ec22e3

Browse files
committed
MAINT: change parrec2nii volume label output from JSON to CSV
1 parent 18125f3 commit 2ec22e3

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

bin/parrec2nii

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import numpy as np
88
import numpy.linalg as npl
99
import sys
1010
import os
11-
import json
12-
from copy import deepcopy
11+
import csv
1312
import nibabel
1413
import nibabel.parrec as pr
1514
from nibabel.parrec import one_line
@@ -69,16 +68,17 @@ def get_opt_parser():
6968
for --dwell-time. The field strength must be supplied
7069
because it is not encoded in the PAR/REC format.""")))
7170
p.add_option(
72-
Option("-i", "--dimension-info", action="store_true", dest="dim_info",
71+
Option("-i", "--volume-info", action="store_true", dest="vol_info",
7372
default=False,
7473
help=one_line(
75-
"""Export .PAR dimension labels corresponding to the fourth
74+
"""Export .PAR volume labels corresponding to the fourth
7675
dimension of the data. The dimension info will be stored in
77-
JSON format with a filename matching the input .PAR, but
78-
with the extension .ordering.json. Only labels that vary
79-
along the 4th dimension are exported. (e.g. for a single
80-
volume structural scan there are no dynamic labels and no
81-
output file will be created)""")))
76+
CSV format with the first row containing dimension labels
77+
and the subsequent rows (one per volume), the corresponding
78+
indices. Only labels that vary along the 4th dimension are
79+
exported (e.g. for a single volume structural scan there
80+
are no dynamic labels and no output file will be created).
81+
""")))
8282
p.add_option(
8383
Option("--origin", action="store", dest="origin", default="scanner",
8484
help=one_line(
@@ -146,16 +146,6 @@ def error(msg, exit_code):
146146
sys.exit(exit_code)
147147

148148

149-
def convert_arrays_to_lists(input_dict):
150-
# convert dictionary of numpy arrays to a dictionary of lists
151-
output_dict = deepcopy(input_dict)
152-
for key, value in output_dict.items():
153-
if not isinstance(value, np.ndarray):
154-
raise ValueError("all dictionary entries must be numpy arrays")
155-
output_dict[key] = value.tolist()
156-
return output_dict
157-
158-
159149
def proc_file(infile, opts):
160150
# figure out the output filename, and see if it exists
161151
basefilename = splitext_addext(os.path.basename(infile))[0]
@@ -285,13 +275,16 @@ def proc_file(infile, opts):
285275
fid.write('%s ' % val)
286276
fid.write('\n')
287277

288-
# export data labels varying along the 4th dimensions
289-
if opts.dim_info:
290-
labels = pr_img.header.get_dimension_labels(collapse_slices=True)
278+
# export data labels varying along the 4th dimensions if requested
279+
if opts.vol_info:
280+
labels = pr_img.header.get_volume_labels()
291281
if len(labels) > 0:
292-
labels = convert_arrays_to_lists(labels)
293-
with open(basefilename + '.ordering.json', 'w') as fid:
294-
json.dump(labels, fid, sort_keys=True, indent=4)
282+
vol_keys = list(labels.keys())
283+
with open(basefilename + '.ordering.csv', 'w') as csvfile:
284+
csvwriter = csv.writer(csvfile, delimiter=',')
285+
csvwriter.writerow(vol_keys)
286+
for vals in zip(*[labels[k] for k in vol_keys]):
287+
csvwriter.writerow(vals)
295288

296289
# write out dwell time if requested
297290
if opts.dwell_time:

0 commit comments

Comments
 (0)