From 2a0af074aea17e6a8c329aeeb7f7aba09e0d3e4f Mon Sep 17 00:00:00 2001 From: Josh Meyers Date: Thu, 6 Jun 2024 09:38:06 -0700 Subject: [PATCH] Be more careful with np copies --- batoid/coordSys.py | 2 +- batoid/coordTransform.py | 4 ++-- batoid/rayVector.py | 6 ++++-- tests/test_RayVector.py | 13 ++++++++++++- 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/batoid/coordSys.py b/batoid/coordSys.py index cd8fe8fb..d8229f45 100644 --- a/batoid/coordSys.py +++ b/batoid/coordSys.py @@ -119,7 +119,7 @@ def toLocal(self, v): vv : ndarray of float, shape (n, 3) Vector in local coordinates. """ - v = np.array(v, dtype=float) + v = np.array(v, dtype=float, copy=True) v -= self.origin return (self.rot.T@v.T).T diff --git a/batoid/coordTransform.py b/batoid/coordTransform.py index 0c80da38..3f4a08a6 100644 --- a/batoid/coordTransform.py +++ b/batoid/coordTransform.py @@ -85,7 +85,7 @@ def applyForwardArray(self, x, y, z): Unlike applyForward, this method does not transform in-place, but returns a newly created ndarray. """ - r = np.array([x, y, z], dtype=float).T + r = np.array([x, y, z], dtype=float, copy=True).T r -= self.dr return self.drot.T@r.T @@ -107,7 +107,7 @@ def applyReverseArray(self, x, y, z): Unlike applyReverse, this method does not transform in-place, but returns a newly created ndarray. """ - r = np.array([x, y, z], dtype=float) + r = np.array([x, y, z], dtype=float, copy=True) r = (self.drot@r).T r += self.dr return r.T diff --git a/batoid/rayVector.py b/batoid/rayVector.py index 2ac88c3e..c639ee4b 100644 --- a/batoid/rayVector.py +++ b/batoid/rayVector.py @@ -16,7 +16,7 @@ def _reshape_arrays(arrays, shape, dtype=float): array = arrays[i] if not hasattr(array, 'shape') or array.shape != shape: arrays[i] = np.array(np.broadcast_to(array, shape)) - arrays[i] = np.ascontiguousarray(arrays[i], dtype=dtype) + arrays[i] = np.array(arrays[i], dtype=dtype, copy=True, order='C') return arrays @@ -50,6 +50,8 @@ def __init__( shape = np.broadcast( x, y, z, vx, vy, vz, t, wavelength, flux, vignetted, failed ).shape + if shape == (): + shape = (1,) x, y, z, vx, vy, vz, t, wavelength, flux = _reshape_arrays( [x, y, z, vx, vy, vz, t, wavelength, flux], shape @@ -819,7 +821,7 @@ def _finish( if isinstance(flux, Real): flux = np.full(len(x), float(flux)) if source is None: - vv = np.array(dirCos, dtype=float) + vv = np.array(dirCos, dtype=float, copy=True) vv /= n*np.sqrt(np.dot(vv, vv)) zhat = -n*vv xhat = np.cross(np.array([1.0, 0.0, 0.0]), zhat) diff --git a/tests/test_RayVector.py b/tests/test_RayVector.py index 9c31d060..bd78a8a9 100644 --- a/tests/test_RayVector.py +++ b/tests/test_RayVector.py @@ -25,6 +25,17 @@ def test_properties(): ) rv = batoid.RayVector(x, y, z, vx, vy, vz, t, w, fx, vig, fa, cs) + assert x is not rv.x + assert y is not rv.y + assert z is not rv.z + assert vx is not rv.vx + assert vy is not rv.vy + assert vz is not rv.vz + assert t is not rv.t + assert w is not rv.wavelength + assert fx is not rv.flux + assert vig is not rv.vignetted + assert fa is not rv.failed np.testing.assert_array_equal(rv.x, x) np.testing.assert_array_equal(rv.y, y) @@ -1101,4 +1112,4 @@ def test_fromFieldAngles(): test_factory_optic() test_getitem() test_fromStop() - test_fromFieldAngles() \ No newline at end of file + test_fromFieldAngles()