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

Make vectorization consistent across all time conversion functions #1324

Conversation

subagonsouth
Copy link
Contributor

Fix bug causing iteration over a 0-d array when numpy.vectorize returns a 0-d array rather than a scalar

Change Summary

This makes the vectorization of spiceypy time functions consistent.

It also fixes a bug that was caused by calling et_to_utc on a 0-d vector. The bug was caused by a 0-d vector being Iterable but not able to be used in list comprehension. This is ultimately due to np.vectorize undesirable behavior of returning a 0-d array instead of a scalar for scalar inputs. I find the fix to be a bit hacky, so if someone has an alternative, cleaner approach, let me know.

Updated Files

  • imap_processing/spice/time.py
    • Add the _vectorize method which wraps np.vectorize and returns that result with [()] which converts 0-d vectors to scalars and leaves everything else as is.
    • Use the _vectorize for all functions that were manually vectorizing calls to spiceypy functions.
  • imap_processing/tests/spice/test_time.py
    • Add checks that scalar input -> scalar output for vectorized functions.

Closes: #1305

Fix bug causing iteration over a 0-d array when numpy.vectorize returns a 0-d array rather than a scalar
@subagonsouth subagonsouth added the SPICE Related to SPICE label Jan 31, 2025
@subagonsouth subagonsouth self-assigned this Jan 31, 2025
Copy link
Collaborator

@greglucas greglucas left a comment

Choose a reason for hiding this comment

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

I think the simpler way would be if these functions took arrays as inputs directly, but that would require adding that upstream in spiceypy.

…en a scalar is input

Modify met_to_datetime64 to return a scalar when a scalar is input
@subagonsouth subagonsouth merged commit 444b9f9 into IMAP-Science-Operations-Center:dev Feb 3, 2025
17 checks passed
@subagonsouth subagonsouth deleted the 1305-spice---use-npvectorize-throughout branch February 3, 2025 18:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SPICE Related to SPICE
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

SPICE - use np.vectorize throughout
2 participants