Skip to content

Raise warning and downsample if data given to _image.resample is too large#19368

Merged
QuLogic merged 1 commit into
matplotlib:mainfrom
dstansby:resample-error
Jan 27, 2022
Merged

Raise warning and downsample if data given to _image.resample is too large#19368
QuLogic merged 1 commit into
matplotlib:mainfrom
dstansby:resample-error

Conversation

@dstansby

@dstansby dstansby commented Jan 26, 2021

Copy link
Copy Markdown
Member

Fixes #19276. This still needs:

  • Documentation
  • Tests

@dstansby dstansby marked this pull request as draft January 26, 2021 16:53

@jklymak jklymak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can accurately downsample so I don't think we should error here. An aliased downsampled image is still zoomable, at which point the aliasing will go away. If the image is above 2**23 we should just subsample by a factor of 2 and warn.

@dstansby

dstansby commented Jan 26, 2021

Copy link
Copy Markdown
Member Author

We can accurately downsample so I don't think we should error here. An aliased downsampled image is still zoomable, at which point the aliasing will go away. If the image is above 2**23 we should just subsample by a factor of 2 and warn.

👍 - for subsampling, would you recommend just doing arr[::2]?

@jklymak

jklymak commented Jan 26, 2021

Copy link
Copy Markdown
Member

I think we want arr[::int(np.ceil(Ny/2**23)), :][:,::int(np.ceil(Nx/2**23))]?

@dstansby

Copy link
Copy Markdown
Member Author

Yep, my comment was modulo finding the right stride, but just wanted to double check that striding was the correct approach.

@jklymak

jklymak commented Jan 26, 2021

Copy link
Copy Markdown
Member

The right approach is open to debate, in that you could anti-alias the data before hitting it with a stride. But for such a massive subsample, I think a simple stride is better than an unspecified overflow/wrap!

@dstansby dstansby changed the title Raise errors if data given to _image.resample is too large Raise warning and downsample if data given to _image.resample is too large Apr 3, 2021
@dstansby dstansby force-pushed the resample-error branch 3 times, most recently from 5f1773c to 1fd6269 Compare April 4, 2021 09:39
@dstansby dstansby force-pushed the resample-error branch 2 times, most recently from 40b5664 to a81575c Compare September 7, 2021 19:01
@timhoffm

timhoffm commented Sep 8, 2021

Copy link
Copy Markdown
Member

Why do we bother to downsample at all? We cannot display more than N pixels and should refuse to accept more (In the face of ambiguity refuse the temptation to guess). If a user has more data, it's their responsibility and competence to reduce them.

@dstansby

dstansby commented Sep 8, 2021

Copy link
Copy Markdown
Member Author

I'm happy to go either way - perhaps worth discussing on a dev call and coming back with a decision?

@jklymak

jklymak commented Sep 8, 2021

Copy link
Copy Markdown
Member

We almost always resample so I don't see the problem with reducing the data before trying the final resample step. This will be aliased, but a max-sized image will also be aliased by the final downsample, so I doubt it would be detectable.

@tacaswell

Copy link
Copy Markdown
Member

I am 👍🏻 in favor of this.

@jklymak

jklymak commented Sep 10, 2021

Copy link
Copy Markdown
Member

As discussed on the call, we should go ahead and subsample and warn, which I think is what this PR does.

@dstansby

Copy link
Copy Markdown
Member Author

👍 - I still need to add a test for resampling the columns that's similar to the existing one for rows

@dstansby dstansby force-pushed the resample-error branch 2 times, most recently from 60a3373 to cecd991 Compare December 28, 2021 10:44
@dstansby dstansby marked this pull request as ready for review December 28, 2021 11:30
@dstansby

Copy link
Copy Markdown
Member Author

This should be good for review now. Tests and an API note added.

@jklymak jklymak left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to let this languish. I think this looks good.

@jklymak jklymak added this to the v3.6.0 milestone Jan 5, 2022
Comment thread lib/matplotlib/image.py Outdated
Comment thread lib/matplotlib/image.py
Comment on lines +176 to +185
if data.shape[1] > 2**23:
warnings.warn(msg.format(n='2**23 rows'))
step = int(np.ceil(data.shape[1] / 2**23))
data = data[:, ::step]
transform = Affine2D().scale(step, 1) + transform
if data.shape[0] > 2**24:
warnings.warn(msg.format(n='2**24 columns'))
step = int(np.ceil(data.shape[0] / 2**24))
data = data[::step, :]
transform = Affine2D().scale(1, step) + transform

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is the rows, 1 is the columns; you have the message swapped. I don't know if the limits should be swapped as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - I checked the limits and they are correct, just the message that needed changing.

@QuLogic

QuLogic commented Jan 11, 2022

Copy link
Copy Markdown
Member

Needs rebase for docs to work.

@tacaswell

Copy link
Copy Markdown
Member

power-cycled to pick up the CI config from main.

@QuLogic QuLogic merged commit d100c42 into matplotlib:main Jan 27, 2022
@dstansby dstansby deleted the resample-error branch February 7, 2022 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

imshow with very large arrays not working as expected

5 participants