From 0214c9c803a38d6c88796cb43c81ed81cfa34b0d Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Wed, 24 Jul 2024 17:27:06 -0500 Subject: [PATCH 1/6] TST: Fix deprecation warning --- tests/ptycho/test_position.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ptycho/test_position.py b/tests/ptycho/test_position.py index b495ec78..d32c7abc 100644 --- a/tests/ptycho/test_position.py +++ b/tests/ptycho/test_position.py @@ -6,6 +6,7 @@ import os.path import unittest +import tike.precision from tike.ptycho.exitwave import ExitWaveOptions from tike.ptycho.object import ObjectOptions from tike.ptycho.position import PositionOptions @@ -338,7 +339,7 @@ def test_consistent_rpie_unmeasured_detector_regions(self): measured_pixels = np.logical_not(unmeasured_pixels) # Zero out these regions on the diffraction measurement data - self.data = self.data.astype(np.floating) + self.data = self.data.astype(tike.precision.floating) self.data[:, unmeasured_pixels] = np.nan params = tike.ptycho.PtychoParameters( @@ -380,7 +381,7 @@ def test_consistent_lstsq_grad_unmeasured_detector_regions(self): measured_pixels = np.logical_not(unmeasured_pixels) # Zero out these regions on the diffraction measurement data - self.data = self.data.astype(np.floating) + self.data = self.data.astype(tike.precision.floating) self.data[:, unmeasured_pixels] = np.nan params = tike.ptycho.PtychoParameters( From 03c971aebcc7e11f81afd9916cfb17c3f55c09a7 Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Wed, 24 Jul 2024 17:51:19 -0500 Subject: [PATCH 2/6] BUG: Avoid casting complex to real values --- src/tike/ptycho/object.py | 8 ++++++-- src/tike/ptycho/probe.py | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/tike/ptycho/object.py b/src/tike/ptycho/object.py index 6677b0f3..c2449407 100644 --- a/src/tike/ptycho/object.py +++ b/src/tike/ptycho/object.py @@ -89,12 +89,16 @@ def copy_to_device(self) -> ObjectOptions: if self.v is not None: options.v = cp.asarray( self.v, - dtype=tike.precision.floating, + dtype=tike.precision.cfloating + if np.iscomplexobj(self.v) + else tike.precision.floating, ) if self.m is not None: options.m = cp.asarray( self.m, - dtype=tike.precision.floating, + dtype=tike.precision.cfloating + if np.iscomplexobj(self.m) + else tike.precision.floating, ) if self.preconditioner is not None: options.preconditioner = cp.asarray( diff --git a/src/tike/ptycho/probe.py b/src/tike/ptycho/probe.py index de194000..4a365eaf 100644 --- a/src/tike/ptycho/probe.py +++ b/src/tike/ptycho/probe.py @@ -181,12 +181,16 @@ def copy_to_device(self) -> ProbeOptions: if self.v is not None: options.v = cp.asarray( self.v, - dtype=tike.precision.floating, + dtype=tike.precision.cfloating + if np.iscomplexobj(self.v) + else tike.precision.floating, ) if self.m is not None: options.m = cp.asarray( self.m, - dtype=tike.precision.floating, + dtype=tike.precision.cfloating + if np.iscomplexobj(self.m) + else tike.precision.floating, ) if self.preconditioner is not None: options.preconditioner = cp.asarray( From 11bb3c8b39b84eeb676c879ac8b7151d53e07a63 Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Thu, 25 Jul 2024 08:43:22 -0500 Subject: [PATCH 3/6] BUG: Prevent casting from complex to real --- src/tike/ptycho/object.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/tike/ptycho/object.py b/src/tike/ptycho/object.py index c2449407..952b9d2c 100644 --- a/src/tike/ptycho/object.py +++ b/src/tike/ptycho/object.py @@ -258,6 +258,7 @@ def get_padded_object(scan, probe, extra: int = 0): return np.full_like( probe, shape=span.astype(tike.precision.integer), + dtype=tike.precision.cfloating, fill_value=tike.precision.cfloating(0.5 + 0j), ), scan + 1 - min_corner + extra From 8729c7478c92ebd9e401efa4420bf7995c3f60e2 Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Thu, 25 Jul 2024 10:34:26 -0500 Subject: [PATCH 4/6] BUG: Fix NEP50 errors from Numpy 2.0 --- src/tike/ptycho/solvers/lstsq.py | 3 ++- src/tike/ptycho/solvers/rpie.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/tike/ptycho/solvers/lstsq.py b/src/tike/ptycho/solvers/lstsq.py index 40b94571..042445ef 100644 --- a/src/tike/ptycho/solvers/lstsq.py +++ b/src/tike/ptycho/solvers/lstsq.py @@ -503,7 +503,8 @@ def keep_some_args_constant( step_length = cp.full( shape=(farplane.shape[0], 1, farplane.shape[2], 1, 1), - fill_value=exitwave_options.step_length_start, + fill_value=tike.precision.floating(exitwave_options.step_length_start), + dtype=tike.precision.floating, ) if exitwave_options.step_length_usemodes == 'dominant_mode': diff --git a/src/tike/ptycho/solvers/rpie.py b/src/tike/ptycho/solvers/rpie.py index 1f8837ee..8608f7d7 100644 --- a/src/tike/ptycho/solvers/rpie.py +++ b/src/tike/ptycho/solvers/rpie.py @@ -382,7 +382,8 @@ def keep_some_args_constant( step_length = cp.full( shape=(farplane.shape[0], 1, farplane.shape[2], 1, 1), - fill_value=exitwave_options.step_length_start, + fill_value=tike.precision.floating(exitwave_options.step_length_start), + dtype=tike.precision.floating, ) if exitwave_options.step_length_usemodes == 'dominant_mode': From 93c30f9d21824934f47e56590cc6c10199c6a78c Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Thu, 25 Jul 2024 14:50:42 -0500 Subject: [PATCH 5/6] BUG: Using typing.Union instead of | operator --- src/tike/ptycho/position.py | 5 ++--- src/tike/ptycho/solvers/rpie.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/tike/ptycho/position.py b/src/tike/ptycho/position.py index d9766712..22cb4953 100644 --- a/src/tike/ptycho/position.py +++ b/src/tike/ptycho/position.py @@ -119,7 +119,6 @@ import dataclasses import logging import typing -import copy import cupy as cp import cupyx.scipy.ndimage @@ -458,9 +457,9 @@ def insert(self, other, indices): @staticmethod def join( - x: typing.Iterable[PositionOptions | None], + x: typing.Iterable[typing.Union[PositionOptions, None]], reorder: npt.NDArray[np.intc], - ) -> PositionOptions | None: + ) -> typing.Union[PositionOptions, None]: if None in x: return None new = PositionOptions( diff --git a/src/tike/ptycho/solvers/rpie.py b/src/tike/ptycho/solvers/rpie.py index 8608f7d7..a025d8a4 100644 --- a/src/tike/ptycho/solvers/rpie.py +++ b/src/tike/ptycho/solvers/rpie.py @@ -330,7 +330,7 @@ def _get_nearplane_gradients( position_options: typing.Union[None, PositionOptions], exitwave_options: ExitWaveOptions, ) -> typing.Tuple[ - float, npt.NDArray, npt.NDArray, npt.NDArray, npt.NDArray, npt.NDArray | None + float, npt.NDArray, npt.NDArray, npt.NDArray, npt.NDArray, typing.Union[npt.NDArray, None] ]: cost: float = 0.0 count: float = 1.0 / len(batches[n]) From d92c6368966fd57041b2eb1130a41ef3fac8ea6f Mon Sep 17 00:00:00 2001 From: Daniel Ching Date: Fri, 26 Jul 2024 14:54:53 -0500 Subject: [PATCH 6/6] TST: Remove tests that rely on undocumented behavior in numpy This smoke test for the clustering methods relies on argpartition returning the same result every time. However, the behavior of argparition changed with NumPy 2.0. Fortunately(?) the behavior change is not a bug because argparition never promised a stable paritioning, it only promises that elements will correctly be divided before and after the pivot. However, these test must be removed because they are recall tests which assume that every argparition implementation returns the exact same result. --- tests/test_random.py | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/tests/test_random.py b/tests/test_random.py index d5ddf50a..35376057 100644 --- a/tests/test_random.py +++ b/tests/test_random.py @@ -78,16 +78,6 @@ class TestWobblyCenter(ClusterTests, unittest.TestCase): cluster_method = staticmethod(tike.cluster.wobbly_center) - def test_simple_cluster(self): - references = [ - np.array([2, 3, 4, 9]), - np.array([0, 5, 8]), - np.array([1, 6, 7]), - ] - result = tike.cluster.wobbly_center(np.arange(10)[:, None], 3) - for a, b in zip(references, result): - np.testing.assert_array_equal(a, b) - def test_same_mean(self): """Test that wobbly center generates better samples of the population. @@ -118,16 +108,6 @@ class TestWobblyCenterRandomBootstrap(ClusterTests, unittest.TestCase): cluster_method = staticmethod(tike.cluster.wobbly_center_random_bootstrap) - def test_simple_cluster(self): - references = [ - np.array([2, 3, 4, 9]), - np.array([0, 5, 8]), - np.array([1, 6, 7]), - ] - result = tike.cluster.wobbly_center(np.arange(10)[:, None], 3) - for a, b in zip(references, result): - np.testing.assert_array_equal(a, b) - def test_same_mean(self): """Test that wobbly center generates better samples of the population.