Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be more careful with np copies #113

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion batoid/coordSys.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
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)

Check warning on line 122 in batoid/coordSys.py

View check run for this annotation

Codecov / codecov/patch

batoid/coordSys.py#L122

Added line #L122 was not covered by tests
v -= self.origin
return (self.rot.T@v.T).T

Expand Down
4 changes: 2 additions & 2 deletions batoid/coordTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
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

Check warning on line 88 in batoid/coordTransform.py

View check run for this annotation

Codecov / codecov/patch

batoid/coordTransform.py#L88

Added line #L88 was not covered by tests
r -= self.dr
return self.drot.T@r.T

Expand All @@ -107,7 +107,7 @@
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)

Check warning on line 110 in batoid/coordTransform.py

View check run for this annotation

Codecov / codecov/patch

batoid/coordTransform.py#L110

Added line #L110 was not covered by tests
r = (self.drot@r).T
r += self.dr
return r.T
Expand Down
6 changes: 4 additions & 2 deletions batoid/rayVector.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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')

Check warning on line 19 in batoid/rayVector.py

View check run for this annotation

Codecov / codecov/patch

batoid/rayVector.py#L19

Added line #L19 was not covered by tests
return arrays


Expand Down Expand Up @@ -50,6 +50,8 @@
shape = np.broadcast(
x, y, z, vx, vy, vz, t, wavelength, flux, vignetted, failed
).shape
if shape == ():
shape = (1,)

Check warning on line 54 in batoid/rayVector.py

View check run for this annotation

Codecov / codecov/patch

batoid/rayVector.py#L53-L54

Added lines #L53 - L54 were not covered by tests
x, y, z, vx, vy, vz, t, wavelength, flux = _reshape_arrays(
[x, y, z, vx, vy, vz, t, wavelength, flux],
shape
Expand Down Expand Up @@ -819,7 +821,7 @@
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)

Check warning on line 824 in batoid/rayVector.py

View check run for this annotation

Codecov / codecov/patch

batoid/rayVector.py#L824

Added line #L824 was not covered by tests
vv /= n*np.sqrt(np.dot(vv, vv))
zhat = -n*vv
xhat = np.cross(np.array([1.0, 0.0, 0.0]), zhat)
Expand Down
13 changes: 12 additions & 1 deletion tests/test_RayVector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1101,4 +1112,4 @@ def test_fromFieldAngles():
test_factory_optic()
test_getitem()
test_fromStop()
test_fromFieldAngles()
test_fromFieldAngles()
Loading