@@ -9,6 +9,7 @@ import numpy.linalg as npl
9
9
import sys
10
10
import os
11
11
import json
12
+ from copy import deepcopy
12
13
import nibabel
13
14
import nibabel .parrec as pr
14
15
from nibabel .parrec import one_line
@@ -134,6 +135,24 @@ def error(msg, exit_code):
134
135
sys .exit (exit_code )
135
136
136
137
138
+ def sort_info_to_lists (sort_info ):
139
+ sort_info = deepcopy (sort_info )
140
+ # convert from elements from numpy array to lists for easy JSON export
141
+ for key in sort_info :
142
+ ndim = sort_info [key ].ndim
143
+ if ndim == 1 :
144
+ sort_info [key ] = list (sort_info [key ])
145
+ elif ndim == 2 :
146
+ k = sort_info [key ]
147
+ k_list = []
148
+ for row in range (k .shape [0 ]):
149
+ k_list .append (list (k [row ]))
150
+ sort_info [key ] = k_list
151
+ else :
152
+ raise ValueError ("only 1D and 2D arrays supported" )
153
+ return sort_info
154
+
155
+
137
156
def proc_file (infile , opts ):
138
157
# figure out the output filename, and see if it exists
139
158
basefilename = splitext_addext (os .path .basename (infile ))[0 ]
@@ -265,15 +284,15 @@ def proc_file(infile, opts):
265
284
266
285
opts .dim_info = True # TODO: remove hard-coded value
267
286
if opts .dim_info and pr_img .dataobj ._dim_4_labels is not None :
268
- if len (list (pr_img .dataobj ._dim_4_labels .keys ())) > 0 :
269
- with open (basefilename + '.ordering.json' , 'w' ) as fid :
270
- json .dump (pr_img .dataobj ._dim_4_labels , fid , sort_keys = True ,
271
- indent = 4 )
287
+ labels = pr_img .dataobj ._dim_4_labels
272
288
elif opts .dim_info and pr_img .dataobj ._dim_3_4_labels is not None :
273
- if len (list (pr_img .dataobj ._dim_3_4_labels .keys ())) > 0 :
274
- with open (basefilename + '.ordering.json' , 'w' ) as fid :
275
- json .dump (pr_img .dataobj ._dim_3_4_labels , fid , sort_keys = True ,
276
- indent = 4 )
289
+ labels = pr_img .dataobj ._dim_3_4_labels
290
+ else :
291
+ labels = None
292
+ if labels is not None and len (labels ) > 0 :
293
+ sort_info = sort_info_to_lists (labels )
294
+ with open (basefilename + '.ordering.json' , 'w' ) as fid :
295
+ json .dump (sort_info , fid , sort_keys = True , indent = 4 )
277
296
278
297
# write out dwell time if requested
279
298
if opts .dwell_time :
0 commit comments