Repository navigation
n-dimensional rescale, resize, and pyramid transforms - #1522
Conversation
|
Thank you all for the quick reviews on this pull request as well as the previous one! |
|
In addition to the changes suggested above, I explicitly set the multichannel argument in a few other locations in the codebase where |
|
It has been awhile, but I would like to revisit this PR if there is still interest a made a small, separate PR for the default edge mode changes in #2484 It would probably make sense to rebase this PR on top of that one |
|
Hello @grlee77! Thanks for updating the PR.
Comment last updated on April 20, 2017 at 19:03 Hours UTC |
|
I rebased this after the pytest merge. I dropped a single commit related to older numpy compatibility (cbf62401a696f735956912c5538b3b2ca318f2ec) as it is no longer necessary with the new numpy 1.11 minimum requirement. The final commit is a bunch of random PEP8 fixes of issues brought up by pep8speaks. The individual commit history is definitely not ideal, as I was still new to learning git workflows at the time. If it is desired to go forward with this PR I can rebase into a handful of more logical commits that may make it a bit easier to review? |
jni
left a comment
There was a problem hiding this comment.
@grlee77 thanks very much for revisiting this! I've suggested a few changes.
We had a few discussions in other issues about how to deal with multichannel in some of these cases where there is an alternative way of specifying it: namely, by specifying the final dimension, ie output_shape=(r//2, c//2, ch). (Or, for Gaussian filters, sigma=(2, 2, 0), for example.) My gut feeling is that that is the more elegant way of dealing with multichannel issues, and it avoids quite a bit of complexity going forward. What do you think? @soupault I think you had thoughts on this also?
| return multichannel | ||
| else: | ||
| warn('The default multichannel argument (None) is deprecated. ' | ||
| 'Specifiy True or False explicitly.') |
There was a problem hiding this comment.
I suspect we want multichannel=False in the future but the current behavior is True for 3D images and False for 2D or 4D+ images? I think somehow this has to come across in the deprecation message. Otherwise multichannel would be a required argument after the deprecation, which doesn't sound good. =)
Oh and btw, specify in which version the deprecation will take place.
There was a problem hiding this comment.
The current behavior in 0.13 treats as multichannel when the input is 3D and output_shape[2] == image.shape[2]. Otherwise it is not treated as multichannel. I think multichannel_default should be updated to match that behavior backwards compatibility.
I will update the deprecation message appropriately assuming the future default will be False. Assuming this goes into release 0.14, deprecation would be 0.16?
There was a problem hiding this comment.
actually _multichannel_default is fine as is (aside from the warnings text). It is only used by rescale and the various pyramid functions which all previously assumed 2D+channels if the input was 3D and would error on inputs > 3D.
| ndim_out = len(output_shape) | ||
| input_shape = image.shape | ||
| if ndim_out > image.ndim: | ||
| # append dimensions to input_shape |
There was a problem hiding this comment.
How did you decide to append instead of prepend? This is the opposite of numpy broadcasting rules, so I would want a justification noted in a comment here...
There was a problem hiding this comment.
Frankly speaking, I'm not in favour of this check. I think we should either we expose this as a parameter to the user (append/prepend) or to discard.
@jni Let me cite your old message : #2276 (comment) . I assume, due to the lack of nD algorithms, we do not yet have a strict policy on dimensions ordering.
There was a problem hiding this comment.
I pref disallowing output_shape with a different number of dimensions than the input. The problem with this is that in 0.13 it is already allowed to give a 2D input with 3D output shape. The additional dimension gets appended to the end:
scikit-image/skimage/transform/_warps.py
Lines 98 to 99 in d020c22
|
|
||
| else: # 2-dimensional interpolation | ||
|
|
||
| output_shape = np.asarray(output_shape) |
There was a problem hiding this comment.
Why this conversion? It complicates downstream syntax, forcing a call to np.concatenate. I suggest making it a tuple.
There was a problem hiding this comment.
I second @grlee77 solution. output_shape : tuple or ndarray, means that we should handle both options in a single way. And the simplest wat to do so is to convert to ndarray.
There was a problem hiding this comment.
@grlee77 I think we need to update the docstring as well:
Size of the generated output image `(rows, cols[, dim])`... -> (rows, cols[, ...][, dim])
There was a problem hiding this comment.
I can change to output_shape = tuple(output_shape). That should also convert a 1D array to a tuple and allow the simpler tuple concatenation.
| image = np.reshape(image, input_shape) | ||
| elif ndim_out == image.ndim - 1: | ||
| # multichannel case: append shape of last axis | ||
| output_shape = np.concatenate((output_shape, [image.shape[-1], ])) |
There was a problem hiding this comment.
If output_shape is a tuple, this becomes output_shape += (image.shape[-1],)
|
|
||
| else: # n-dimensional interpolation | ||
| for i in range(ndim_out - image.ndim): | ||
| image = image[..., np.newaxis] |
There was a problem hiding this comment.
Didn't we already reshape the image above? And if so, is this not redundant?
There was a problem hiding this comment.
True. I moved it above and then never removed it from here :)
| image is converted according to the conventions of `img_as_float`. | ||
| multichannel : bool, optional | ||
| If True, last axis will not be rescaled. The default is True for 3D | ||
| (2D+color) inputs, False otherwise. |
There was a problem hiding this comment.
... but will be False everywhere in version 0.16? I think overall we've decided to get rid of all multichannel guessing throughout the library on the way to 1.0.
There was a problem hiding this comment.
👍 I agree: we should add deprecation message if multichannel is None saying that it will default to False in 0.16.
There was a problem hiding this comment.
I'd rephrase it like this: By default, is set to True for 3D (2D+color) inputs, and False for others..
| multichannel = _multichannel_default(multichannel, image.ndim) | ||
| scale = np.atleast_1d(scale) | ||
| if len(scale) > 1 and len(scale) != image.ndim: | ||
| raise ValueError("must supply a single scale or one value per axis.") |
There was a problem hiding this comment.
Wouldn't there be a third case — for multichannel images, len(scale) == image.ndim[:-1], right?
| for dim in range(image.shape[2]): | ||
| # apply Gaussian filter to all channels independently | ||
| if multichannel: | ||
| for dim in range(image.shape[-1]): |
There was a problem hiding this comment.
This seems unnecessarily complex — isn't it equivalent to run a single gaussian filter with the corresponding sigma = 0?
|
|
||
| # build downsampled images until max_layer is reached or downscale process | ||
| # does not change image size | ||
| while layer != max_layer: |
There was a problem hiding this comment.
I know it's not on you but this should clearly be a for loop. =)
|
|
||
| shift = lambda xy: xy + 1 | ||
| def shift(xy): | ||
| return xy + 1 |
|
Ah, here is the discussion, which was sort of cut off by @noahstier but I think is worth hashing out in a longer discussion. =) |
| ndim_out = len(output_shape) | ||
| input_shape = image.shape | ||
| if ndim_out > image.ndim: | ||
| # append dimensions to input_shape |
There was a problem hiding this comment.
Frankly speaking, I'm not in favour of this check. I think we should either we expose this as a parameter to the user (append/prepend) or to discard.
@jni Let me cite your old message : #2276 (comment) . I assume, due to the lack of nD algorithms, we do not yet have a strict policy on dimensions ordering.
|
|
||
| else: # 2-dimensional interpolation | ||
|
|
||
| output_shape = np.asarray(output_shape) |
There was a problem hiding this comment.
I second @grlee77 solution. output_shape : tuple or ndarray, means that we should handle both options in a single way. And the simplest wat to do so is to convert to ndarray.
|
|
||
| else: # 2-dimensional interpolation | ||
|
|
||
| output_shape = np.asarray(output_shape) |
There was a problem hiding this comment.
@grlee77 I think we need to update the docstring as well:
Size of the generated output image `(rows, cols[, dim])`... -> (rows, cols[, ...][, dim])
| image is converted according to the conventions of `img_as_float`. | ||
| multichannel : bool, optional | ||
| If True, last axis will not be rescaled. The default is True for 3D | ||
| (2D+color) inputs, False otherwise. |
There was a problem hiding this comment.
👍 I agree: we should add deprecation message if multichannel is None saying that it will default to False in 0.16.
| image is converted according to the conventions of `img_as_float`. | ||
| multichannel : bool, optional | ||
| If True, last axis will not be rescaled. The default is True for 3D | ||
| (2D+color) inputs, False otherwise. |
There was a problem hiding this comment.
I'd rephrase it like this: By default, is set to True for 3D (2D+color) inputs, and False for others..
|
|
||
|
|
||
| def resize(image, output_shape, order=1, mode=None, cval=0, clip=True, | ||
| preserve_range=False): |
There was a problem hiding this comment.
We should add multichannel also to resize.
There was a problem hiding this comment.
What would it be used for? I think the two shapes specify everything that needs to be known? I guess if multichannel was specified an image.shape[-1] != output_shape[-1] an error could be raised.
There was a problem hiding this comment.
Well, both resize and rescale are exposed to the user. They are used for the same purpose, therefore, I think, they should have the similar API. There is nothing more than that :).
In my opinion, the thinking should be the following: if a function performs any logic on "multichanneling", the argument has to be explicitly provided.
There was a problem hiding this comment.
@soupault but the API is necessarily different — rescale can take float input, while resize always needs a tuple. This means that the multichannel nature can be inferred from the output and input shapes. At any rate, I consider this something that can be fixed post merge if necessary.
There was a problem hiding this comment.
This means that the multichannel nature can be inferred from the output and input shapes.
A-a-a-and we are back to it :).
@jni I understand your point. My feeling is that the API should be as homogeneous as possible.
Maybe we could just have another input from @scikit-image/core ?
There was a problem hiding this comment.
@soupault yes I'm happy to discuss it. Notice that I consider this a different discussion to what we've had before: when the argument can be inferred unambiguously, vs when we are just guessing.
At any rate, I'm happy to have the discussion, but I don't think it should hold this PR up. Perhaps you can raise an issue afterwards? And it can cover existing cases of guessing.
There was a problem hiding this comment.
I have not added it so far as I am not sure what the logic would be within the function. As @jni stated, whether or not the last axis would be reshaped is unambiguous from image.shape and output_shape. I suppose if multichannel=True we could raise an error if image.shape[-1] != output_shape[-1], but I'm not sure what else it could do?
There was a problem hiding this comment.
Alright. As far as both of you are strongly concerned not to change the API for resize, let's move the discussion to a separate thread and try to work out the best solution there.
|
|
||
| def pyramid_reduce(image, downscale=2, sigma=None, order=1, | ||
| mode='reflect', cval=0): | ||
| mode='reflect', cval=0, multichannel=None): |
There was a problem hiding this comment.
Please, add deprecation message here as well.
There was a problem hiding this comment.
For all of these pyramid functions, the message is already there within the call to the _multichannel_default routine.
|
|
||
| def pyramid_expand(image, upscale=2, sigma=None, order=1, | ||
| mode='reflect', cval=0): | ||
| mode='reflect', cval=0, multichannel=None): |
|
|
||
| def pyramid_laplacian(image, max_layer=-1, downscale=2, sigma=None, order=1, | ||
| mode='reflect', cval=0): | ||
| mode='reflect', cval=0, multichannel=None): |
|
Most of the comments have been addressed. I think the ones still outstanding are:
|
this requires predetermining max_layer. New tests added to verify the expected behvaiour
| ) | ||
|
|
||
|
|
||
| def _multichannel_default(multichannel, ndim): |
There was a problem hiding this comment.
Please, add a reminder what should be changed/removed by 0.16 to https://github.com/scikit-image/scikit-image/blob/master/TODO.txt.
|
|
||
|
|
||
| def resize(image, output_shape, order=1, mode=None, cval=0, clip=True, | ||
| preserve_range=False): |
There was a problem hiding this comment.
Well, both resize and rescale are exposed to the user. They are used for the same purpose, therefore, I think, they should have the similar API. There is nothing more than that :).
In my opinion, the thinking should be the following: if a function performs any logic on "multichanneling", the argument has to be explicitly provided.
| output_shape = tuple(output_shape) | ||
| ndim_out = len(output_shape) | ||
| input_shape = image.shape | ||
| if ndim_out > image.ndim: |
There was a problem hiding this comment.
@grlee77 I assume, we both agree to get rid of this?
Does anyone object?
There was a problem hiding this comment.
@soupault no, this is necessary for backwards compatibility — see above
There was a problem hiding this comment.
(above meaning the code that has been removed)
There was a problem hiding this comment.
Ah, yes, I've missed this point.
| Whether to keep the original range of values. Otherwise, the input | ||
| image is converted according to the conventions of `img_as_float`. | ||
| multichannel : bool, optional | ||
| By default, is set to True for 3D (2D+color) inputs, and False for |
There was a problem hiding this comment.
Sorry, I meant to add this reformulated sentence in addition to the description. It's not clear what this argument is for as it is.
| return out | ||
|
|
||
|
|
||
| def rescale(image, scale, order=1, mode=None, cval=0, clip=True, |
There was a problem hiding this comment.
Please, update this docstring as well:
(row_scale, col_scale) -> nD notation.
| multichannel : bool, optional | ||
| By default, is set to True for 3D (2D+color) inputs, and False for | ||
| others. Starting in release 0.16, this will always default to False. | ||
| hether the last axis of the image is to be interpreted as multiple |
| By default, is set to True for 3D (2D+color) inputs, and False for | ||
| others. Starting in release 0.16, this will always default to False. | ||
| hether the last axis of the image is to be interpreted as multiple | ||
| channels or another spatial dimension. By default, is set to True for |
There was a problem hiding this comment.
two spaces instead of one after .
| others. Starting in release 0.16, this will always default to False. | ||
| hether the last axis of the image is to be interpreted as multiple | ||
| channels or another spatial dimension. By default, is set to True for | ||
| 3D (2D+color) inputs, and False for others. Starting in release 0.16, |
There was a problem hiding this comment.
two spaces instead of one after .
| others. Starting in release 0.16, this will always default to False. | ||
| Whether the last axis of the image is to be interpreted as multiple | ||
| channels or another spatial dimension. By default, is set to True for | ||
| 3D (2D+color) inputs, and False for others. Starting in release 0.16, |
There was a problem hiding this comment.
two spaces instead of one after .
There was a problem hiding this comment.
okay. fixing these now
| By default, is set to True for 3D (2D+color) inputs, and False for | ||
| others. Starting in release 0.16, this will always default to False. | ||
| Whether the last axis of the image is to be interpreted as multiple | ||
| channels or another spatial dimension. By default, is set to True for |
There was a problem hiding this comment.
two spaces instead of one after .
| multichannel : bool, optional | ||
| By default, is set to True for 3D (2D+color) inputs, and False for | ||
| others. Starting in release 0.16, this will always default to False. | ||
| hether the last axis of the image is to be interpreted as multiple |
| multichannel : bool, optional | ||
| By default, is set to True for 3D (2D+color) inputs, and False for | ||
| others. Starting in release 0.16, this will always default to False. | ||
| hether the last axis of the image is to be interpreted as multiple |
| multichannel : bool, optional | ||
| By default, is set to True for 3D (2D+color) inputs, and False for | ||
| others. Starting in release 0.16, this will always default to False. | ||
| hether the last axis of the image is to be interpreted as multiple |
Codecov Report
@@ Coverage Diff @@
## master #1522 +/- ##
==========================================
- Coverage 85.46% 85.33% -0.14%
==========================================
Files 328 331 +3
Lines 26069 26436 +367
==========================================
+ Hits 22281 22558 +277
- Misses 3788 3878 +90
Continue to review full report at Codecov.
|
|
@soupault |
|
Thank you @grlee77 ! As usual, a very high quality PR! |
This is a pretty simple pull request extending n-dimensional support to the following transforms:
skimage.transform.rescaleskimage.transform.resizeskimage.transform.pyramid_*This is useful for me as I work primarily in medical imaging where data is often either 3D volumetric or 3D+time.
As was previously the case, resize still calls the two-dimensional interpolations implemented in
_shared/interpolation.pyxfor 2D or 2D+color images, while all other cases usescipy.ndimage.map_coordinates. Previouslymap_coordinateswas set up explicitly for 3D only.The changes to
resizeare substantially smaller than the diff would indicate at first glance, because I switched the order of the if / else statement calling the 2D or n-D interpolation code. (The logic for when to call 2D seemed slightly simpler to express)A multichannel argument was added to
rescaleto indicate that the last axis should not be reshaped. This defaults to True for 3D inputs to replicate the previous behaviour for 2D+color images. To do a true 3D rescale, the user must explicitly specifymultichannel=False.Similarly in
pyramids.py:The
_smoothfunction previously always looped 2D smoothing operations over the last axis if the input was 3D. Now there is a newmultichannelargument that explicitly controls this behavior of looping over the last axis, regardless of image dimension. As forrescaleabove,multichanneldefaults to None which will result in a call to a small function to replicate the old behaviour of True for 3D inputs and False otherwise.