From b6bcfd5c11b7a40835a8283e878c2d17e14e05aa Mon Sep 17 00:00:00 2001 From: Peter Ercius Date: Tue, 9 Dec 2025 14:47:55 -0800 Subject: [PATCH 1/5] use radius instead of square mask region in v0 of com_sparse --- python/stempy/image/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/stempy/image/__init__.py b/python/stempy/image/__init__.py index dee8d95d..3f5b6622 100644 --- a/python/stempy/image/__init__.py +++ b/python/stempy/image/__init__.py @@ -552,8 +552,8 @@ def _com_sparse_v0(array, crop_to=None, init_center=None, replace_nans=True): if crop_to is not None: # Crop around the initial center - keep = (x > comx0 - crop_to[0]) & (x <= comx0 + crop_to[0]) & (y > comy0 - crop_to[1]) & ( - y <= comy0 + crop_to[1]) + r = np.sqrt((x - init_center[0])**2 + (y - init_center[1])**2) + keep = (r < crop_to) x = x[keep] y = y[keep] mm = len(x) From f5f29092ad8397266903f1650c4c750e52377504 Mon Sep 17 00:00:00 2001 From: Peter Ercius Date: Tue, 9 Dec 2025 14:53:01 -0800 Subject: [PATCH 2/5] use radius in com_v1_kernel --- python/stempy/image/__init__.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/python/stempy/image/__init__.py b/python/stempy/image/__init__.py index 3f5b6622..925894cd 100644 --- a/python/stempy/image/__init__.py +++ b/python/stempy/image/__init__.py @@ -404,7 +404,7 @@ def com_v1_kernel( position_indices: np.ndarray, scan_shape: Tuple[int, int], frame_shape: Tuple[int, int], - crop_to: Union[Tuple[int, int], None] = None, + crop_to: Union[int, None] = None, init_center: Union[Tuple[int, int], None] = None, replace_nans: bool = True, ) -> np.ndarray: @@ -432,12 +432,8 @@ def com_v1_kernel( # Cropping if crop_to is not None: if init_center is not None: - xmin = init_center[0] - crop_to[0] - xmax = init_center[0] + crop_to[0] - ymin = init_center[1] - crop_to[1] - ymax = init_center[1] + crop_to[1] - - mask = (x > xmin) & (x <= xmax) & (y > ymin) & (y <= ymax) + r = np.sqrt((x - init_center[0])**2 + (y - init_center[1])**2) + mask = (r < crop_to) position_indices = position_indices[mask] x = x[mask] y = y[mask] @@ -594,9 +590,9 @@ def com_sparse( :param array: A SparseArray of the electron counted data :type array: stempy.io.SparseArray - :param crop_to: optional; The size of the region to crop around initial full frame COM for improved COM near + :param crop_to: optional; The radius from the center to crop around initial full frame COM for improved COM near the zero beam - :type crop_to: tuple of ints of length 2 + :type crop_to: int :param init_center: optional; The initial center to use before cropping. If this is not set then cropping will be applied around the center of mass of the each full frame. :type init_center: tuple of ints of length 2 From e03737d48e0fd52cf651663fbc0f19f0856d2ee7 Mon Sep 17 00:00:00 2001 From: Peter Ercius Date: Tue, 9 Dec 2025 15:05:38 -0800 Subject: [PATCH 3/5] fix code where init_center is not supplied --- python/stempy/image/__init__.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/python/stempy/image/__init__.py b/python/stempy/image/__init__.py index 925894cd..85d5b964 100644 --- a/python/stempy/image/__init__.py +++ b/python/stempy/image/__init__.py @@ -450,13 +450,8 @@ def com_v1_kernel( event_center_x = centers_x[position_indices] event_center_y = centers_y[position_indices] - - mask = ( - (x > event_center_x - crop_to[0]) - & (x <= event_center_x + crop_to[0]) - & (y > event_center_y - crop_to[1]) - & (y <= event_center_y + crop_to[1]) - ) + r = np.sqrt((x - event_center_x[0])**2 + (y - event_center_y[1])**2) + mask = (r < crop_to) position_indices = position_indices[mask] x = x[mask] y = y[mask] @@ -548,7 +543,7 @@ def _com_sparse_v0(array, crop_to=None, init_center=None, replace_nans=True): if crop_to is not None: # Crop around the initial center - r = np.sqrt((x - init_center[0])**2 + (y - init_center[1])**2) + r = np.sqrt((x - comx0[0])**2 + (y - comy0[1])**2) keep = (r < crop_to) x = x[keep] y = y[keep] From 6bb18c535303b749c1f573ecb1daa530b1dc8b35 Mon Sep 17 00:00:00 2001 From: Peter Ercius Date: Tue, 9 Dec 2025 15:18:23 -0800 Subject: [PATCH 4/5] change tests to use int --- tests/test_image.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_image.py b/tests/test_image.py index a964c41c..731836a4 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -58,12 +58,12 @@ def test_com_sparse_parameters(simulate_sparse_array, version): assert round(com0[0,].mean()) == 30 # Test crop_to input. Initial COM should be full frame COM - com1 = com_sparse(sp, crop_to=(10, 10), version=version) + com1 = com_sparse(sp, crop_to=10, version=version) assert round(com1[0,].mean()) == 30 # Test crop_to and init_center input. # No counts will be in the center so all positions will be np.nan - com2 = com_sparse(sp, crop_to=(10, 10), init_center=(1, 1), version=version) + com2 = com_sparse(sp, crop_to=10, init_center=(1, 1), version=version) assert np.isnan(com2[0,0,0]) @@ -75,17 +75,17 @@ def test_com_sparse_version_comparison(sparse_array_small): {"crop_to": None, "init_center": None, "replace_nans": True}, {"crop_to": None, "init_center": None, "replace_nans": False}, { - "crop_to": (frame_x // 2, frame_y // 2), + "crop_to": frame_x // 2, "init_center": None, "replace_nans": True, }, { - "crop_to": (frame_x // 2, frame_y // 2), + "crop_to": frame_x // 2, "init_center": (frame_x // 2, frame_y // 2), "replace_nans": True, }, { - "crop_to": (frame_x // 2, frame_y // 2), + "crop_to": frame_x // 2, "init_center": (frame_x // 2, frame_y // 2), "replace_nans": False, }, From 6f9714d8abfe418c0bae8be21d581306474672b9 Mon Sep 17 00:00:00 2001 From: Peter Ercius Date: Tue, 9 Dec 2025 16:41:38 -0800 Subject: [PATCH 5/5] int aren't tuples --- python/stempy/image/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/stempy/image/__init__.py b/python/stempy/image/__init__.py index 85d5b964..94db98a0 100644 --- a/python/stempy/image/__init__.py +++ b/python/stempy/image/__init__.py @@ -450,7 +450,7 @@ def com_v1_kernel( event_center_x = centers_x[position_indices] event_center_y = centers_y[position_indices] - r = np.sqrt((x - event_center_x[0])**2 + (y - event_center_y[1])**2) + r = np.sqrt((x - event_center_x)**2 + (y - event_center_y)**2) mask = (r < crop_to) position_indices = position_indices[mask] x = x[mask] @@ -543,7 +543,7 @@ def _com_sparse_v0(array, crop_to=None, init_center=None, replace_nans=True): if crop_to is not None: # Crop around the initial center - r = np.sqrt((x - comx0[0])**2 + (y - comy0[1])**2) + r = np.sqrt((x - comx0)**2 + (y - comy0)**2) keep = (r < crop_to) x = x[keep] y = y[keep]