def sort_volumes_per_voxel(input_volumes, sort_matrix):
"""Sort the given volumes per voxel using the sort index in the given matrix.
What this essentially does is to look per voxel from which map we should take the first value. Then we place that
value in the first volume and we repeat for the next value and finally for the next voxel.
If the length of the 4th dimension is > 1 we shift the 4th dimension to the 5th dimension and sort
the array as if the 4th dimension values where a single value. This is useful for sorting (eigen)vector matrices.
Args:
input_volumes (:class:`list`): list of 4d ndarray
sort_matrix (ndarray): 4d ndarray with for every voxel the sort index
Returns:
:class:`list`: the same input volumes but then with every voxel sorted according to the given sort index.
"""
def load_maps(map_list):
tmp = []
for data in map_list:
if isinstance(data, string_types):
data = load_nifti(data).get_data()
if len(data.shape) < 4:
data = data[..., None]
tmp.append(data)
return tmp
input_volumes = load_maps(input_volumes)
if input_volumes[0].shape[3] > 1:
volume = np.concatenate([np.reshape(m, m.shape[0:3] + (1,) + (m.shape[3],)) for m in input_volumes], axis=3)
grid = np.ogrid[[slice(x) for x in volume.shape]]
sorted_volume = volume[list(grid[:-2]) + [np.reshape(sort_matrix, sort_matrix.shape + (1,))] + list(grid[-1])]
return [sorted_volume[..., ind, :] for ind in range(len(input_volumes))]
else:
volume = np.concatenate([m for m in input_volumes], axis=3)
sorted_volume = volume[list(np.ogrid[[slice(x) for x in volume.shape]][:-1])+[sort_matrix]]
return [np.reshape(sorted_volume[..., ind], sorted_volume.shape[0:3] + (1,))
for ind in range(len(input_volumes))]
评论列表
文章目录