11# Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
22
33"""
4- Unit test for automatic recomputation of 1-to-0 analysis operations after ROI changes .
4+ Unit test for automatic recomputation of 1-to-0 analysis operations.
55
66This test verifies that analysis results (like centroid) are automatically updated
7- when ROI is modified through various methods:
8- - Programmatically setting a new ROI (simulating edit_roi_graphically)
9- - Adding multiple ROIs at once
10- - Deleting a single ROI using delete_single_roi
11- - Deleting all ROIs using delete_regions_of_interest
7+ when data changes through various methods:
8+ - ROI modifications (adding, deleting ROIs)
9+ - Data transformations via recompute_1_to_1 (modifying processing parameters)
1210
13- The test creates a Gaussian image, computes its centroid, then verifies that:
11+ The tests create a Gaussian image, compute its centroid, then verify that:
14121. The centroid changes when a ROI is added to restrict the calculation region
15132. Two centroid rows are generated when two ROIs are added
16143. One centroid row remains after deleting the first ROI
17154. The centroid returns to the original value when all ROIs are deleted
16+ 5. The centroid is recomputed when processing parameters are modified
1817"""
1918
2019# guitest: show
2322
2423import numpy as np
2524from sigima .objects import Gauss2DParam , create_image_from_param , create_image_roi
25+ from sigima .params import RotateParam
2626
2727from datalab .adapters_metadata import GeometryAdapter
2828from datalab .config import Conf
@@ -45,7 +45,7 @@ def get_centroid_coords(obj) -> tuple[float, float] | None:
4545 return None
4646
4747
48- def test_roi_auto_recompute ():
48+ def test_analysis_recompute_after_roi_change ():
4949 """Test automatic recomputation of analysis results when ROI changes."""
5050 with datalab_test_app_context (console = False ) as win :
5151 panel = win .imagepanel
@@ -147,5 +147,94 @@ def test_roi_auto_recompute():
147147 print ("\n ✓ All ROI auto-recompute tests passed!" )
148148
149149
150+ def test_analysis_recompute_after_recompute_1_to_1 ():
151+ """Test automatic recomputation of analysis after processing parameter changes."""
152+ with datalab_test_app_context (console = False ) as win :
153+ panel = win .imagepanel
154+
155+ # Create a Gaussian image offset from center
156+ SIZE = 200
157+ # In Gauss2DParam, x0 and y0 are the center coordinates with Xmin=-10.0,
158+ # Ymin=-10.0, Xmax=10.0, Ymax=10.0 by default.
159+ # The centroid position should be at (49.75, 99.5).
160+ # After a 45° rotation, it should move closer to (64.32, 134.68).
161+ # Or after a 90° rotation, it should move to (99.5, 149.2).
162+ param = Gauss2DParam .create (height = SIZE , width = SIZE , x0 = - 5.0 )
163+ img = create_image_from_param (param )
164+ panel .add_object (img )
165+
166+ # Apply a rotation transformation with 45° angle
167+ rot_param = RotateParam .create (angle = 45.0 )
168+ with Conf .proc .show_result_dialog .temp (False ):
169+ panel .processor .run_feature ("rotate" , rot_param )
170+
171+ # Get the rotated image
172+ img_rotated = panel .objview .get_sel_objects ()[0 ]
173+ print (f"\n Rotated image title: { img_rotated .title } " )
174+
175+ # Compute centroid on the rotated image
176+ with Conf .proc .show_result_dialog .temp (False ):
177+ panel .processor .run_feature ("centroid" )
178+
179+ # Get initial centroid (after 45° rotation)
180+ centroid = get_centroid_coords (img_rotated )
181+ assert centroid is not None , "Centroid should be computed"
182+ x0 , y0 = centroid
183+ print (f"Initial centroid (45° rotation): ({ x0 :.1f} , { y0 :.1f} )" )
184+ expected_x0 = 64.32
185+ expected_y0 = 134.68
186+ assert np .isclose (x0 , expected_x0 , atol = 0.2 ), (
187+ f"X centroid should be near { expected_x0 :.1f} , got { x0 :.1f} "
188+ )
189+ assert np .isclose (y0 , expected_y0 , atol = 0.2 ), (
190+ f"Y centroid should be near { expected_y0 :.1f} , got { y0 :.1f} "
191+ )
192+
193+ # Now modify the rotation angle to 90° via the Processing tab
194+ # The rotated object is already selected, just ensure we're accessing it
195+ panel .objview .select_objects ([img_rotated ])
196+
197+ # Get the processing parameter editor and change the angle
198+ assert panel .objprop .processing_param_editor is not None
199+ editor = panel .objprop .processing_param_editor
200+ editor .dataset .angle = 90.0 # Change from 45° to 90°
201+
202+ # Apply the modified parameters (this triggers recompute_1_to_1)
203+ report = panel .objprop .apply_processing_parameters (interactive = False )
204+
205+ assert report .success , f"Recompute failed: { report .message } "
206+ print ("Processing parameters recomputed with new angle (90°)" )
207+
208+ # Verify centroid was automatically recomputed
209+ centroid = get_centroid_coords (img_rotated )
210+ assert centroid is not None , "Centroid should still exist after recompute"
211+ x1 , y1 = centroid
212+ print (f"Centroid after recompute (90° rotation): ({ x1 :.1f} , { y1 :.1f} )" )
213+
214+ # After 90° rotation: the Gaussian blob that was at upper-right (after 45°)
215+ # moves toward the center-right area
216+ # Empirical measurements show approximately (99.5, 149.2)
217+ expected_x1 = 99.5
218+ expected_y1 = 149.2
219+ assert np .isclose (x1 , expected_x1 , atol = 0.2 ), (
220+ f"X centroid should be near { expected_x1 :.1f} , got { x1 :.1f} "
221+ )
222+ assert np .isclose (y1 , expected_y1 , atol = 0.2 ), (
223+ f"Y centroid should be near { expected_y1 :.1f} , got { y1 :.1f} "
224+ )
225+
226+ # Most importantly, verify the centroid changed from 90° rotation
227+ centroid_changed = not (
228+ np .isclose (x1 , x0 , atol = 5.0 ) and np .isclose (y1 , y0 , atol = 5.0 )
229+ )
230+ assert centroid_changed , (
231+ f"Centroid should have changed from ({ x0 :.1f} , { y0 :.1f} ) "
232+ f"to ({ x1 :.1f} , { y1 :.1f} ) after changing rotation angle"
233+ )
234+
235+ print ("\n ✓ Recompute_1_to_1 auto-analysis test passed!" )
236+
237+
150238if __name__ == "__main__" :
151- test_roi_auto_recompute ()
239+ test_analysis_recompute_after_roi_change ()
240+ test_analysis_recompute_after_recompute_1_to_1 ()
0 commit comments