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

Communicate napari layer deletions appropriately to the plugin #49

Merged
merged 15 commits into from
Aug 13, 2024

Conversation

saarah815
Copy link
Contributor

@saarah815 saarah815 commented Aug 7, 2024

Before submitting a pull request (PR), please read the contributing guide.

Please fill out as much of this template as you can, but if you have any problems or questions, just leave a comment and we will help out :)

Description

What is this PR

  • Bug fix
  • Addition of a new feature
  • Other

Why is this PR needed?

"Currently, if a user deletes the atlas or moving image layer the plugin goes into undefined behaviour and will probably throw index out of bounds errors.

Need to make sure that layer deletions are communicated appropriately and the plugin can handle restarting the entire workflow without having to restart the plugin."

[#44]

What does this PR do?

Bug Fixes:

  • Deleting moving image sets self._moving_image and self._moving_image_data_backup to None.
  • Deleting moving image removes it from the sample image dropdown menu and the next item in dropdown menu (equivalent to list of layers) is automatically selected as moving image.
  • Deleting either one of the atlas layers (Reference or Annotations) automatically deletes the other layer, and clears any class attributes related to the atlas, sets self._atlas back to None, and sets the index for the atlas selection combobox back to 0.

Additional Features:

  • If user deletes atlas and tries to re-run registration with atlas selection combobox index == 0, message box appears:

image

  • If user deletes moving image and the new moving image is automatically set to the atlas layer (i.e. user forgets to change this / add a new moving image layer), message box appears:

image

References

Issue #44
Issue #53

How has this PR been tested?

  • Tested by deleting the moving layer only --> works as expected.
  • Tested by deleting the atlas layers only --> works as expected.
  • Tested by deleting both layers --> works as expected.

Essentially user is able to delete layers as required and restart entire workflow without restarting plugin.

Is this a breaking change?

No.

Does this PR require an update to the documentation?

N/A

Checklist:

  • The code has been tested locally
  • Tests have been added to cover all new functionality (unit & integration)
  • The documentation has been updated to reflect any changes
  • The code has been formatted with pre-commit

@IgorTatarnikov IgorTatarnikov self-requested a review August 8, 2024 09:30
Copy link
Member

@IgorTatarnikov IgorTatarnikov left a comment

Choose a reason for hiding this comment

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

This is great, thanks! I've added a few comments throughout the code.

Also, I get an error if I remove the last layer using the napari layer manager. That's being caused by _on_sample_dropdown_index_changed firing due to _update_dropdowns causing the moving_image_index_change signal to fire. This can be fixed by adding a check for an invalid layer index (-1) before running line 311-312.

I wonder if we can take advantage of this interaction with signals and reduce some code duplication.

Perhaps, _delete_atlas_layers can be called inside of the _on_atlas_dropdown_index_changed method. And the _handle_layer_deletion method just calls the reset_atlas_combobox method.

So a layer is deleted, if the layer is the atlas or annotation layer then the reset_atlas_combobox method is called. This causes the _on_atlas_dropdown_changed signal to fire with 0 calling _on_atlas_dropdown_index_changed, and there inside of the if index == 0: block we call _delete_atlas_layers.

brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
def _delete_atlas_layers(self):
# Delete atlas reference layer if it exists
if (
hasattr(self, "_atlas_data_layer")
Copy link
Member

Choose a reason for hiding this comment

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

As above you can check for None instead of hasattr (you can go through and fix this wherever it occurs)

brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>
@saarah815
Copy link
Contributor Author

This is great, thanks! I've added a few comments throughout the code.

Also, I get an error if I remove the last layer using the napari layer manager. That's being caused by _on_sample_dropdown_index_changed firing due to _update_dropdowns causing the moving_image_index_change signal to fire. This can be fixed by adding a check for an invalid layer index (-1) before running line 311-312.

I wonder if we can take advantage of this interaction with signals and reduce some code duplication.

Perhaps, _delete_atlas_layers can be called inside of the _on_atlas_dropdown_index_changed method. And the _handle_layer_deletion method just calls the reset_atlas_combobox method.

So a layer is deleted, if the layer is the atlas or annotation layer then the reset_atlas_combobox method is called. This causes the _on_atlas_dropdown_changed signal to fire with 0 calling _on_atlas_dropdown_index_changed, and there inside of the if index == 0: block we call _delete_atlas_layers.

Sounds good, will do - thank you!

@saarah815
Copy link
Contributor Author

Pushed all new changes. Just wanted to mention a few things:

  • In response to getting an error when all napari layers are deleted, adding a check for an invalid layer index in _on_sample_dropdown_index_changed doesn't work as expected. Took me a while to figure out why but I think it's to do with the fact that each time the list of layers is changed in any way, update_sample_image_names briefly causes the sample image dropdown menu to clear (line 101 in widgets/select_images_view.py: self.available_sample_dropdown.clear()). So checking for an invalid layer index and printing a warning when layer index = -1 basically ended up causing this warning to occur every time the list of layers is changed at all, rather than only when the list is empty. Tried working around this by adding a remove_item method in class SampleImageComboBox(QComboBox) in widgets/select_images_view.py, which worked to remove items without clearing the whole list, but the warning was still appearing when unexpected. Ended up settling on adding a check in update_sample_image_names to see if len(sample_image_names) == 0 and printing the warning then - this ended up being a much simpler fix and the warning now only appears if all layers have been deleted, as expected.

  • I kept the if statements as successive ifs rather than if and elif blocks - I think because of all the repeated actions when deleting and adding layers, the code ends up short-circuiting and skipping actions as a result, which I noticed when combining some of the logic into if and elif blocks. Works as expected when I left it as successive ifs though, so I just kept it like that. But happy to change this if this was a misunderstanding on my end!

Other than those two points, all other changes have been made :)

Copy link
Member

@IgorTatarnikov IgorTatarnikov left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the comments! Just a few small things left. I just noticed that the tests seem to have broken, they enter a loop at the top of test_registration_widget. We'll have to take a look at this before merging.

brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
brainglobe_registration/widgets/select_images_view.py Outdated Show resolved Hide resolved
brainglobe_registration/registration_widget.py Outdated Show resolved Hide resolved
Copy link

codecov bot commented Aug 9, 2024

Codecov Report

Attention: Patch coverage is 88.23529% with 6 lines in your changes missing coverage. Please review.

Project coverage is 82.93%. Comparing base (f690dea) to head (c4e0cd5).

Files Patch % Lines
brainglobe_registration/registration_widget.py 86.95% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev      #49      +/-   ##
==========================================
+ Coverage   80.96%   82.93%   +1.96%     
==========================================
  Files           8        8              
  Lines         541      580      +39     
==========================================
+ Hits          438      481      +43     
+ Misses        103       99       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@IgorTatarnikov IgorTatarnikov left a comment

Choose a reason for hiding this comment

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

Looks great, thank you!

@IgorTatarnikov IgorTatarnikov merged commit 17435b1 into brainglobe:dev Aug 13, 2024
13 checks passed
@IgorTatarnikov IgorTatarnikov mentioned this pull request Aug 16, 2024
7 tasks
alessandrofelder added a commit that referenced this pull request Aug 16, 2024
* Moving from tabs to a dropdown style widget (#25)

* Fixed contents margins

* Added brainglobe-utils as dependency

* Added min dependency version for brainglobe-utils

* Add option to scale sample image (#26)

* Fixed contents margins

* Scaling of sample image added to adjust moving image dropdown

* Added brainglobe-utils as dependency

* Added error message when no images selected

* Added tests for scaling

* Added tests to check multiple scale_factors

* Cache the moving image data to allow scaling image multiple times

* Fixed tests by removing (0,0) case add guard in _on_scale_image in registration_widget.py

* Fixed tests

* Add docstring to _on_scale_moving_image

* Replaced all curr_ variable names with current_

* Added units to pixel size selection, set max range to be 100

* Allow free rotation of the atlas (#36)

* Fixed contents margins

* Scaling of sample image added to adjust moving image dropdown

* Added brainglobe-utils as dependency

* Added error message when no images selected

* Atlas rotation working using scipy

* Pitch, yaw, roll implemented using one affine transform

* Dask loading for both the reference atlas and the rotation

* Added a 'Reset Atlas' button, blocked while dask computes the rotation to avoid race conditions

* Rotate atlas button also greyed out while daks processes the rotation

* Use the update upload_pypi action (#29)

See neuroinformatics-unit/movement#108

* Added elastix Logs directory to .gitignore

* Fixed tests

* Moved run_registration to be imported just as run button is clicked to avoid long boot times

* Atlas rotation works again (might relate to napari 0.4.19

* Added test for atlas_rotation_signal from AdjustMovingImage view

* [pre-commit.ci] pre-commit autoupdate (#30)

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.1.9 → v0.2.0](astral-sh/ruff-pre-commit@v0.1.9...v0.2.0)
- [github.com/psf/black: 23.12.1 → 24.1.1](psf/black@23.12.1...24.1.1)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* [pre-commit.ci] pre-commit autoupdate (#31)

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.2.0 → v0.3.5](astral-sh/ruff-pre-commit@v0.2.0...v0.3.5)
- [github.com/psf/black: 24.1.1 → 24.3.0](psf/black@24.1.1...24.3.0)
- [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](pre-commit/mirrors-mypy@v1.8.0...v1.9.0)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Import header from brainglobe-utils (#33)

* import header from brainglobe-utils

* split package name over two lines

* remove brainglobe png from manifest

* add brainglobe-utils dependency

* Update requirements (#34)

* import header from brainglobe-utils

* split package name over two lines

* remove brainglobe png from manifest

* add brainglobe-utils dependency

* Switched from bg-atlasapi to brainglobe-atlasapi

* Added lxml_html_clean explicitly to requirements

* Pinned itk to 5.4rc2 for now as 5.4rc3 cased a seg fault

* Undo itk pin

---------

Co-authored-by: Kimberly Meechan <24316371+K-Meech@users.noreply.github.com>

* add codecov token (#35)

* Added tests for calculate_rotated_bounding_box

* Fixed docstrings for functions in utils.py

* Keep track of napari layers for the atlas and atlas annotations in the napari widget class

* Added tests for atlas rotation

* Fixed pre-commit

* Add test for reset_atlas in adjust_moving_image_view

* Add caching

* Update the function calls in test_adjust_moving_image_view

* Apply suggestions from code review

Co-authored-by: Alessandro Felder <alessandrofelder@users.noreply.github.com>

* Applied suggestions from code review

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Applied suggestions from code review

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Kimberly Meechan <24316371+K-Meech@users.noreply.github.com>
Co-authored-by: Alessandro Felder <alessandrofelder@users.noreply.github.com>

* Cleaned up a few things

* Mark 'Advanced Settings' as optional (#48)

* Info message when running BrainGlobe Registration plugin with no atlas installed (#46)

* added error when no atlas found

* added error when no atlas found

* Update brainglobe_registration/registration_widget.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/registration_widget.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* changes to info message

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/registration_widget.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* parent_widget

* parent_widget

---------

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Clean up widget (#51)

* Cleaned up rogue widget duplication

* Added parent to parameters_tab

* Fix slow plugin loading (#54)

* Filter Images (#52)

* work_in_progress

* filter_images

* filter_images

* filter_images

* filter_images

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/utils/utils.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

* Communicate napari layer deletions appropriately to the plugin (#49)

* layer_deletion

* Update brainglobe_registration/registration_widget.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* layer_deletion_edits

* Made pytest happy

* Update brainglobe_registration/registration_widget.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/registration_widget.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* Update brainglobe_registration/registration_widget.py

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* updated_layer

* fixed manual vs automatic deletion

* fixed manual vs automatic deletion

* Refactored some old code to avoid duplication

* add comments

---------

Co-authored-by: Igor Tatarnikov <61896994+IgorTatarnikov@users.noreply.github.com>
Co-authored-by: IgorTatarnikov <igor.tatarnikov@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Kimberly Meechan <24316371+K-Meech@users.noreply.github.com>
Co-authored-by: Alessandro Felder <alessandrofelder@users.noreply.github.com>
Co-authored-by: saarah815 <63860766+saarah815@users.noreply.github.com>
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.

2 participants