Skip to content

Operations: Fix Crop Coordinates ROI not updating when switching between stacks#3145

Open
RasmiaKulan wants to merge 4 commits into
mainfrom
3115-Fix_rescale_and_autocrop_in_crop_coordinates_operation
Open

Operations: Fix Crop Coordinates ROI not updating when switching between stacks#3145
RasmiaKulan wants to merge 4 commits into
mainfrom
3115-Fix_rescale_and_autocrop_in_crop_coordinates_operation

Conversation

@RasmiaKulan
Copy link
Copy Markdown
Collaborator

Issue Closes #3115

Description

  • Added validation within set_stacks to refresh the ROI field using the current stack if the currently selected filter is Crop Coordinates, and the ROI field exists.
  • Added a test for roi_field_called_with_auto_bounding_box_on_different_stacks

Developer Testing

  • I have verified unit tests pass locally: python -m pytest -vs
  • I have verified the test roi_field_called_with_auto_bounding_box_on_different_stacks passes locally. Tested using the debugger as well to check if the variables were returning expected results.

Acceptance Criteria and Reviewer Testing

  • Unit tests pass locally: python -m pytest -vs

ROI within FoV steps:

  • Load two datasets, one with a larger (x, y) axis than the other.
  • Open the Crop coordinates operation and select the larger stack
  • Resize the ROI to be larger than the smaller stack and click okay, but don't apply the change
  • Using the stack selector, select the smaller sample
  • Open up the ROI editor view
  • The ROI should be within the FoV of the imagestack

Auto-crop is re-activated when switching between stacks when multiple datasets are loaded:

  • Load two datasets, one with a larger (x, y) axis than the other.
  • Open the Crop coordinates operation and open ROI editor - autocrop will work as expected
  • Using the stack selector, select the smaller sample
  • Open up the ROI editor view and confirm that the autocrop updates correctly

Documentation and Additional Notes

@JackEAllen JackEAllen requested review from JackEAllen May 11, 2026 09:23
@JackEAllen JackEAllen self-assigned this May 11, 2026
@RasmiaKulan RasmiaKulan force-pushed the 3115-Fix_rescale_and_autocrop_in_crop_coordinates_operation branch from a1d7609 to 8f13664 Compare May 11, 2026 14:27
@coveralls
Copy link
Copy Markdown

coveralls commented May 11, 2026

Coverage Status

coverage: 68.032% (+0.03%) from 67.999% — 3115-Fix_rescale_and_autocrop_in_crop_coordinates_operation into main

Copy link
Copy Markdown
Collaborator

@JackEAllen JackEAllen left a comment

Choose a reason for hiding this comment

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

Thank you for adding a fix. Functionally this works well and resolves the issue 🥳
I do have a small question about the test I'd like to get your opinion on, but otherwise happy with the change.

We can explore improving the extendability of updating fields on stack change for specific operations such is the case with this fix separately as part of #3152

Comment on lines +469 to +473
first_stack = mock.Mock()
first_stack.data = np.ones((2, 201, 201))

second_stack = mock.Mock()
second_stack.data = np.ones((2, 143, 140))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Are the stacks used in this test if we are mocking get_bounding_box?
I think that when the init_roi_field runs, it does not inspect stack.data, it just uses the mocked return values via the side_effect so you may be abel to remove them so long as you keep the distinct stack assignment and init calls by having two:

self.presenter.stack = mock.Mock()
self.presenter.init_roi_field(mock_roi_field)

where assigning one stack object to the presenter.stack and calling init_roi_field performed twice may be enough to emulate a stack change.

@JackEAllen JackEAllen marked this pull request as draft May 12, 2026 15:37
@RasmiaKulan RasmiaKulan marked this pull request as ready for review May 12, 2026 20:37
Comment on lines +472 to +480
stack = mock.Mock(
name="test_stack",
shape=(2, 201, 201),
num_images=2,
num_sinograms=201,
)
with mock.patch("mantidimaging.gui.windows.operations.presenter.BlockQtSignals"):
self.presenter.set_stack(stack)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The main assertion here looks good, but do we need the mock attributes here?
They are set, but don't seem to be asserted anywhere after being passed to the mock unless i've missed something.
As do_update_previews is not mocked, I wonder if this could silently succeed or fail for unrelated reasons. i'd be tempted to add it ( @mock.patch('mantidimaging.gui.windows.operations.presenter.FiltersWindowPresenter.do_update_previews')) as an additional patch and we could then simplify to:

        stack = mock.Mock(name="test_stack")
        self.presenter.set_stack(stack)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@JackEAllen I still needed to keep:

with mock.patch("mantidimaging.gui.windows.operations.presenter.BlockQtSignals")

because self.view in the test is a MagicMock rather than a real Qt QObject. Without the patch, BlockQtSignals runs its real assertion:

assert isinstance(obj, QObject)

which causes the test to fail before reaching the behaviour under test.

I also still needed to specify num_images on the stack mock because set_stack() accesses self.max_preview_image_idx, which evaluates:

self.stack.num_images - 1

If num_images is left as the default auto-generated Mock, this results in:

TypeError: unsupported operand type(s) for -: 'Mock' and 'int'

Since do_update_previews is now mocked, the additional attributes like shape and num_sinograms are no longer required, so the test can still be simplified to only the minimal attributes actually used by set_stack().

@JackEAllen JackEAllen marked this pull request as draft May 19, 2026 22:08
@RasmiaKulan RasmiaKulan marked this pull request as ready for review May 20, 2026 13:13
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.

Operations: Crop Coordinates ROI does Not Rescale Nor is AutoCrop Updated When Switching between Stacks

3 participants