@@ -8,8 +8,7 @@ import numpy as np
8
8
import numpy .linalg as npl
9
9
import sys
10
10
import os
11
- import json
12
- from copy import deepcopy
11
+ import csv
13
12
import nibabel
14
13
import nibabel .parrec as pr
15
14
from nibabel .parrec import one_line
@@ -69,16 +68,17 @@ def get_opt_parser():
69
68
for --dwell-time. The field strength must be supplied
70
69
because it is not encoded in the PAR/REC format.""" )))
71
70
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 " ,
73
72
default = False ,
74
73
help = one_line (
75
- """Export .PAR dimension labels corresponding to the fourth
74
+ """Export .PAR volume labels corresponding to the fourth
76
75
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
+ """ )))
82
82
p .add_option (
83
83
Option ("--origin" , action = "store" , dest = "origin" , default = "scanner" ,
84
84
help = one_line (
@@ -146,16 +146,6 @@ def error(msg, exit_code):
146
146
sys .exit (exit_code )
147
147
148
148
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
-
159
149
def proc_file (infile , opts ):
160
150
# figure out the output filename, and see if it exists
161
151
basefilename = splitext_addext (os .path .basename (infile ))[0 ]
@@ -285,13 +275,16 @@ def proc_file(infile, opts):
285
275
fid .write ('%s ' % val )
286
276
fid .write ('\n ' )
287
277
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 ( )
291
281
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 )
295
288
296
289
# write out dwell time if requested
297
290
if opts .dwell_time :
0 commit comments