Skip to content
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

Avoid setting corner pixels for empty layers #5423

Merged
merged 1 commit into from
Dec 20, 2022

Conversation

andy-sweet
Copy link
Member

@andy-sweet andy-sweet commented Dec 19, 2022

Description

This avoids trying to set Layer.corner_pixels (which should be integer indices in the data space of the layer) when a layer is empty. For all non-image layers, this causes a warning to be raised from numpy (see #5420) because Layer._data_extent is a (2, Layer.ndim) array of all nans. For image layers, if Layer.data is empty (which seems much less likely) we get all kinds of other errors before even reaching this warning.

Type of change

  • Bug-fix (non-breaking change which fixes an issue)

References

Closes #5420

How has this been tested?

  • all existing tests pass with my change

data_bbox_clipped = np.clip(
data_bbox_int, displayed_extent[0], displayed_extent[1]
)
corners[:, displayed_axes] = data_bbox_clipped
self.corner_pixels = corners
Copy link
Member Author

Choose a reason for hiding this comment

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

Alternatively, we could set Layer.corner_pixels to be an empty array in this case, but I'm not sure which dimension would be empty in that case.

@codecov
Copy link

codecov bot commented Dec 19, 2022

Codecov Report

Merging #5423 (c04f372) into main (b9e613a) will increase coverage by 1.56%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main    #5423      +/-   ##
==========================================
+ Coverage   87.54%   89.10%   +1.56%     
==========================================
  Files         597      597              
  Lines       50610    50612       +2     
==========================================
+ Hits        44304    45096     +792     
+ Misses       6306     5516     -790     
Impacted Files Coverage Δ
napari/layers/base/base.py 91.58% <100.00%> (+0.01%) ⬆️
napari/_qt/qt_main_window.py 76.21% <0.00%> (+0.18%) ⬆️
napari/__main__.py 45.18% <0.00%> (+0.23%) ⬆️
napari/_qt/qt_viewer.py 78.68% <0.00%> (+0.40%) ⬆️
napari/settings/_base.py 91.98% <0.00%> (+0.47%) ⬆️
napari/_qt/utils.py 80.19% <0.00%> (+0.49%) ⬆️
napari/_qt/widgets/qt_viewer_dock_widget.py 96.02% <0.00%> (+0.56%) ⬆️
napari/utils/theme.py 92.85% <0.00%> (+0.59%) ⬆️
napari/_vispy/layers/image.py 96.68% <0.00%> (+0.66%) ⬆️
napari/components/viewer_model.py 96.55% <0.00%> (+0.68%) ⬆️
... and 42 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

# contains nans, in which case the integer valued corner pixels
# cannot be meaningfully set.
displayed_extent = self.extent.data[:, displayed_axes]
if not np.all(np.isnan(displayed_extent)):
Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally, I'd want to have something like if self.is_empty() here instead. The existing code effectively serves as a proxy for that and is deliberately distinct from checking if there any nans in displayed_extent (which will also cause the same warning to be issued).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if not np.all(np.isnan(displayed_extent)):
if not np.any(np.isnan(displayed_extent)):

I think that the existence nan on nay position should prevent this.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did consider this as a more general fix (i.e. to prevent the warning), but my concern is that it might hide other bugs.

Like I mentioned above, I really want something like Layer.is_empty() in which case layer extent and corner pixels are not well defined. If we have one nan in the layer extent (i.e. instead of all nans), something probably went very wrong. Though in that case, I'd probably expect worse things to happen, so I'd also be fine with this suggestion.

@aganders3
Copy link
Contributor

aganders3 commented Dec 20, 2022

I agree it would be helpful to have some indicator the layer is empty. For what it's worth Points and some other layers use len(layer.data) > 0: to guard the same issue in _update_thumbnail, so at least I think your fix is fairly consistent with other parts of the code.

Maybe Layer.is_empty() could just be implemented for now as a check for all (or any, as suggested) nan in the extent data?

Copy link
Contributor

@brisvag brisvag left a comment

Choose a reason for hiding this comment

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

+1 for Layer.is_empty(). I can imagine this being a useful on every layer.

@andy-sweet
Copy link
Member Author

I can pick this up again in a few hours, but if someone wants to get main passing again, feel free to approve/merge this. I'd also be down to accept @Czaki's suggestion if that means this is approved/merged soon.

I think we can follow up with extra things (like Layer.is_empty) later.

@andy-sweet
Copy link
Member Author

Thanks for the approval! I'm going to make an exception to the typical process of waiting at least 24 hours here, in order to get main and PR tests passing again. I will also write up an issue to capture some of the other ideas/fixes.

@imagesc-bot
Copy link

This pull request has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/adding-empty-shapes-layer-after-adding-rgb-image-causes-runtimewarning/75676/4

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.

[test-bot] Comprehensive tests failing
6 participants