@@ -257,7 +257,7 @@ def _unique_rows(a):
257
257
if a .ndim != 2 :
258
258
raise ValueError ("expected 2D input" )
259
259
b = np .ascontiguousarray (a ).view (
260
- np .dtype ((np .void , a .dtype .itemsize * a .shape [1 ])))
260
+ np .dtype ((np .void , a .dtype .itemsize * a .shape [1 ])))
261
261
return np .unique (b ).view (a .dtype ).reshape (- 1 , a .shape [1 ])
262
262
263
263
@@ -581,21 +581,6 @@ def __init__(self, file_like, header, mmap=True, scaling='dv'):
581
581
self ._slice_scaling = header .get_data_scaling (scaling )
582
582
self ._rec_shape = header .get_rec_shape ()
583
583
584
- try :
585
- # store additional slice & volume label information
586
- self ._dim_4_labels = header .sorted_labels (
587
- sorted_indices = self ._slice_indices ,
588
- collapse_slices = True )
589
- self ._dim_3_4_labels = None
590
- except :
591
- # if non-unique slice orientations or image angulations, need to
592
- # also keep labels for the 3rd (slice) dimension
593
- self ._dim_4_labels = None
594
- self ._dim_3_4_labels = header .sorted_labels (
595
- sorted_indices = self ._slice_indices ,
596
- collapse_slices = False )
597
-
598
-
599
584
@property
600
585
def shape (self ):
601
586
return self ._shape
@@ -1141,25 +1126,27 @@ def get_sorted_slice_indices(self):
1141
1126
n_used = np .prod (self .get_data_shape ()[2 :])
1142
1127
return sort_order [:n_used ]
1143
1128
1144
- def sorted_labels (self , sorted_indices = None , collapse_slices = True ):
1145
- """ return a dictionary of lists of the varying values in
1146
- ``self.image_defs``. This is useful for custom data sorting. only
1147
- keys that have more than 1 unique value across the dataset will exist
1148
- in the returned `sort_info` dictionary.
1129
+ def get_dimension_labels (self , collapse_slices = True ):
1130
+ """ Dynamic labels corresponding to the final data dimension(s).
1131
+
1132
+ This is useful for custom data sorting. A subset of the info in
1133
+ ``self.image_defs`` is returned in an order that matches the final
1134
+ data dimension(s). Only labels that have more than one unique value
1135
+ across the dataset will be returned.
1149
1136
1150
1137
Parameters
1151
1138
----------
1152
- sorted_indices : array, optional
1153
- sorted slice indices as returned by
1154
- ``self.get_sorted_slice_indices``.
1155
1139
collapse_slices : bool, optional
1156
- if True, only return indices corresponding to the first slice
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:]).
1157
1143
1158
1144
Returns
1159
1145
-------
1160
1146
sort_info : dict
1161
1147
Each key corresponds to a dynamically varying sequence dimension.
1162
- The ordering of values corresponds to that in sorted_indices.
1148
+ The ordering of each value corresponds to that returned by
1149
+ ``self.get_sorted_slice_indices``.
1163
1150
"""
1164
1151
1165
1152
# define which keys to store sorting info for
@@ -1176,21 +1163,25 @@ def sorted_labels(self, sorted_indices=None, collapse_slices=True):
1176
1163
'gradient orientation number' ,
1177
1164
'diffusion b value number' ]
1178
1165
1179
- if sorted_indices is None :
1180
- sorted_indices = self .get_sorted_slice_indices ()
1166
+ sorted_indices = self . get_sorted_slice_indices ()
1167
+ image_defs = self .image_defs
1181
1168
1182
1169
if collapse_slices :
1183
1170
dynamic_keys .remove ('slice number' )
1184
1171
1185
- non_unique_keys = []
1172
+ # remove dynamic keys that may not be present in older .PAR versions
1173
+ for key in dynamic_keys :
1174
+ if key not in image_defs .dtype .fields :
1175
+ dynamic_keys .remove (key )
1186
1176
1177
+ non_unique_keys = []
1187
1178
for key in dynamic_keys :
1188
- ndim = self . image_defs [key ].ndim
1179
+ ndim = image_defs [key ].ndim
1189
1180
if ndim == 1 :
1190
- num_unique = len (np .unique (self . image_defs [key ]))
1181
+ num_unique = len (np .unique (image_defs [key ]))
1191
1182
elif ndim == 2 :
1192
1183
# for 2D cases, e.g. 'image angulation'
1193
- num_unique = len (_unique_rows (self . image_defs [key ]))
1184
+ num_unique = len (_unique_rows (image_defs [key ]))
1194
1185
else :
1195
1186
raise ValueError ("unexpected image_defs shape > 2D" )
1196
1187
if num_unique > 1 :
@@ -1203,18 +1194,19 @@ def sorted_labels(self, sorted_indices=None, collapse_slices=True):
1203
1194
if 'image angulation' in non_unique_keys :
1204
1195
raise ValueError ("for non-unique image angulation, need "
1205
1196
"collapse_slice=False" )
1206
- if 'image image_display_orientation' in non_unique_keys : # ???
1197
+ if 'image_display_orientation' in non_unique_keys : # ???
1207
1198
raise ValueError ("for non-unique display orientation, need "
1208
1199
"collapse_slice=False" )
1209
- sl1_indices = self . image_defs ['slice number' ][sorted_indices ] == 1
1200
+ sl1_indices = image_defs ['slice number' ][sorted_indices ] == 1
1210
1201
1211
1202
sort_info = {}
1212
1203
for key in non_unique_keys :
1213
-
1214
1204
if collapse_slices :
1215
- sort_info [key ] = self . image_defs [key ][sorted_indices ][sl1_indices ]
1205
+ sort_info [key ] = image_defs [key ][sorted_indices ][sl1_indices ]
1216
1206
else :
1217
- sort_info [key ] = self .image_defs [key ][sorted_indices ]
1207
+ value = image_defs [key ][sorted_indices ]
1208
+ sort_info [key ] = value .reshape (self .get_data_shape ()[2 :],
1209
+ order = 'F' )
1218
1210
return sort_info
1219
1211
1220
1212
0 commit comments