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

Fix trajectory_predict_skypos mutating input array #790

Merged
merged 2 commits into from
Jan 24, 2025
Merged

Conversation

wilsonbb
Copy link
Collaborator

@wilsonbb wilsonbb commented Jan 24, 2025

trajectory_predict_skypos is meant to predict the positions of a KBMOD trajectory along a list or array of observation times, however it mutates the observation times if they are provided as a numpy array rather than a list.

It calls dt = np.asarray(times) which creates a new numpy array if times is a list or is a no-op if it is an array. We then subtract out the starting timestamp so all times are offset from 0, and we use the in-place operation of dt -= dt[0] to preserve memory. However if times is an array, the original object passed to the array has been mutated to now be offset by 0.

As quickly illustrated in a python repl:

>>> import numpy as np
>>> def modify_me(n):
...     new_n = np.asarray(n)
...     new_n -= new_n[0]
... 
>>> a = list(range(10, 20))
>>> modify_me(a)
>>> a
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> b = np.asarray(a)
>>> modify_me(b)
>>> b
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 

This PR fixes this by choosing not to do the in-place subtraction.

To add better testing for this, we a) use tests with non-zero observation times to actually test the subtraction and b) test both lists and numpy arrays as inputs.

Note that this was showing up as an issue with the KnownObjMatcher having its numpy array of observation times mutated by the call to trajectory_predict_skypos. Then when it tries to compare the observation times against times from an ephemeris, a match cannot be made due to the temporal subtraction.

@wilsonbb wilsonbb marked this pull request as ready for review January 24, 2025 00:33
@wilsonbb wilsonbb requested a review from jeremykubica January 24, 2025 00:33
Copy link
Contributor

@jeremykubica jeremykubica left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I added some optional comments (one stylistic and one extending the test).

src/kbmod/trajectory_utils.py Outdated Show resolved Hide resolved
tests/test_known_object_filters.py Show resolved Hide resolved
tests/test_trajectory_utils.py Show resolved Hide resolved
@wilsonbb wilsonbb merged commit 4c1272f into main Jan 24, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants