@@ -249,18 +249,6 @@ class PARRECError(Exception):
249
249
GEN_RE = re .compile (r".\s+(.*?)\s*:\s*(.*)" )
250
250
251
251
252
- def _unique_rows (a ):
253
- """
254
- find unique rows of a 2D array. based on discussion at:
255
- http://stackoverflow.com/questions/16970982/find-unique-rows-in-numpy-array
256
- """
257
- if a .ndim != 2 :
258
- raise ValueError ("expected 2D input" )
259
- b = np .ascontiguousarray (a ).view (
260
- np .dtype ((np .void , a .dtype .itemsize * a .shape [1 ])))
261
- return np .unique (b ).view (a .dtype ).reshape (- 1 , a .shape [1 ])
262
-
263
-
264
252
def _split_header (fobj ):
265
253
""" Split header into `version`, `gen_dict`, `image_lines` """
266
254
version = None
@@ -1126,48 +1114,34 @@ def get_sorted_slice_indices(self):
1126
1114
n_used = np .prod (self .get_data_shape ()[2 :])
1127
1115
return sort_order [:n_used ]
1128
1116
1129
- def get_volume_labels (self , collapse_slices = True ):
1117
+ def get_volume_labels (self ):
1130
1118
""" Dynamic labels corresponding to the final data dimension(s).
1131
1119
1132
1120
This is useful for custom data sorting. A subset of the info in
1133
1121
``self.image_defs`` is returned in an order that matches the final
1134
1122
data dimension(s). Only labels that have more than one unique value
1135
1123
across the dataset will be returned.
1136
1124
1137
- Parameters
1138
- ----------
1139
- collapse_slices : bool, optional
1140
- If True, only return volume indices (corresponding to the first
1141
- slice). If False, return indices corresponding to individual
1142
- slices as well (with shape matching self.shape[2:]).
1143
-
1144
1125
Returns
1145
1126
-------
1146
1127
sort_info : dict
1147
- Each key corresponds to a dynamically varying sequence dimension.
1148
- The ordering of each value corresponds to that returned by
1149
- ``self.get_sorted_slice_indices``.
1128
+ Each key corresponds to volume labels for a dynamically varying
1129
+ sequence dimension. The ordering of the labels matches the volume
1130
+ ordering determined via ``self.get_sorted_slice_indices``.
1150
1131
"""
1151
1132
sorted_indices = self .get_sorted_slice_indices ()
1152
1133
image_defs = self .image_defs
1153
1134
1154
- # define which keys to store sorting info for
1155
- dynamic_keys = ['slice number' ,
1156
- 'cardiac phase number' ,
1135
+ # define which keys which might vary across image volumes
1136
+ dynamic_keys = ['cardiac phase number' ,
1157
1137
'echo number' ,
1158
1138
'label type' ,
1159
1139
'image_type_mr' ,
1160
1140
'dynamic scan number' ,
1161
- 'slice orientation' ,
1162
- 'image_display_orientation' , # ????
1163
- 'image angulation' ,
1164
1141
'scanning sequence' ,
1165
1142
'gradient orientation number' ,
1166
1143
'diffusion b value number' ]
1167
1144
1168
- if collapse_slices :
1169
- dynamic_keys .remove ('slice number' )
1170
-
1171
1145
# remove dynamic keys that may not be present in older .PAR versions
1172
1146
dynamic_keys = [d for d in dynamic_keys if d in
1173
1147
image_defs .dtype .fields ]
@@ -1177,31 +1151,18 @@ def get_volume_labels(self, collapse_slices=True):
1177
1151
ndim = image_defs [key ].ndim
1178
1152
if ndim == 1 :
1179
1153
num_unique = len (np .unique (image_defs [key ]))
1180
- elif ndim == 2 :
1181
- # for 2D cases, e.g. 'image angulation'
1182
- num_unique = len (_unique_rows (image_defs [key ]))
1183
1154
else :
1184
- raise ValueError ("unexpected image_defs shape > 2D " )
1155
+ raise ValueError ("unexpected image_defs shape > 1D " )
1185
1156
if num_unique > 1 :
1186
1157
non_unique_keys .append (key )
1187
1158
1188
- if collapse_slices :
1189
- for key in ('slice_orientation' , 'image_angulation' ,
1190
- 'image_display_orientation' ):
1191
- if key in non_unique_keys :
1192
- raise ValueError (
1193
- 'for values of {0} that differ overslices, use '
1194
- '"collapse_slice=False"' .format (key ))
1195
- sl1_indices = image_defs ['slice number' ][sorted_indices ] == 1
1159
+ # each key in dynamic keys will be identical across slices, so use
1160
+ # the value at slice 1.
1161
+ sl1_indices = image_defs ['slice number' ][sorted_indices ] == 1
1196
1162
1197
1163
sort_info = {}
1198
1164
for key in non_unique_keys :
1199
- if collapse_slices :
1200
- sort_info [key ] = image_defs [key ][sorted_indices ][sl1_indices ]
1201
- else :
1202
- value = image_defs [key ][sorted_indices ]
1203
- sort_info [key ] = value .reshape (self .get_data_shape ()[2 :],
1204
- order = 'F' )
1165
+ sort_info [key ] = image_defs [key ][sorted_indices ][sl1_indices ]
1205
1166
return sort_info
1206
1167
1207
1168
0 commit comments