-
Notifications
You must be signed in to change notification settings - Fork 265
Add an 'axis' parameter to concat_images, plus two tests. #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
c6c6a1d
2626884
c39caac
afaa5fe
3331a51
41732f4
47fc8f0
f8ad078
84640b3
c836dce
49b353a
99f1168
0567ac3
e9298cf
84d990d
188c7ea
69aa16f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,8 +88,8 @@ def squeeze_image(img): | |
img.extra) | ||
|
||
|
||
def concat_images(images, check_affines=True): | ||
''' Concatenate images in list to single image, along last dimension | ||
def concat_images(images, check_affines=True, axis=None): | ||
''' Concatenate images in list to single image, along specified dimension | ||
|
||
Parameters | ||
---------- | ||
|
@@ -98,7 +98,9 @@ def concat_images(images, check_affines=True): | |
check_affines : {True, False}, optional | ||
If True, then check that all the affines for `images` are nearly | ||
the same, raising a ``ValueError`` otherwise. Default is True | ||
|
||
axis : int, optional | ||
If None, concatenates on the last dimension. | ||
If not None, concatenates on the specified dimension. | ||
Returns | ||
------- | ||
concat_img : ``SpatialImage`` | ||
|
@@ -122,8 +124,13 @@ def concat_images(images, check_affines=True): | |
if check_affines: | ||
if not np.all(img.affine == affine): | ||
raise ValueError('Affines do not match') | ||
out_data[i] = img.get_data() | ||
out_data = np.rollaxis(out_data, 0, len(i0shape)+1) | ||
out_data[i] = img.get_data().copy() | ||
if axis is not None: | ||
out_data = np.concatenate(out_data, axis=axis) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First - I think this makes another copy of the array, which may well be large by this point. Second - isn't this the wrong 'axis'? For example, the most common use here would be to concatenate a (i, j, k, N1) and an (i, j, k, N2) image. That would be axis=-1 or axis=3. I think the code above would first - fail if N1 != N2 in the I think the solution is to use concatenate without There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @matthew-brett For some reason, I had it in my head that what we actually had was a list. I'm shocked that the few tests I did worked at all. I'll start over on this. |
||
elif np.all([d.shape[-1] == 1 for d in out_data]): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer not to have a special case for ones on the last axis - the user can always do (when fixed) axis=-1 for that case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fair enough. :) |
||
out_data = np.concatenate(out_data, axis=d.ndim-1) | ||
else: | ||
out_data = np.rollaxis(out_data, 0, len(i0shape)+1) | ||
klass = img0.__class__ | ||
return klass(out_data, affine, header) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be: