-
Notifications
You must be signed in to change notification settings - Fork 21
1020 bug get spacecraft spin phase always returns nans #1021
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
Merged
subagonsouth
merged 4 commits into
IMAP-Science-Operations-Center:dev
from
subagonsouth:1020-bug-get_spacecraft_spin_phase-always-returns-nans
Oct 16, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| # This is a fake csv file for the sole purpose of testing the get_spacecraft_spin_phase function | ||
| spin_number,spin_start_sec,spin_start_subsec,spin_period_sec,spin_period_valid,spin_phase_valid,spin_period_source,thruster_firing | ||
| # Start with thruster firing | ||
| 0,0,0,15.0,1,1,0,1 | ||
| # Turn thruster firing off then 3 valid spins | ||
| 1,15,0,15.0,1,1,0,0 | ||
| 2,30,0,15.0,1,1,0,0 | ||
| 3,45,0,15.0,1,1,0,0 | ||
| # Missing spin then 2 good spins | ||
| 5,75,0,15.0,1,1,0,0 | ||
| 6,90,0,15.0,1,1,0,0 | ||
| # invalid spin period | ||
| 7,105,0,15.0,0,1,0,0 | ||
| # invalid spin phase | ||
| 8,120,0,15.0,1,0,0,0 | ||
| # 1 good spin | ||
| 9,135,0,15.0,1,1,0,0 | ||
| # Thruster firing on | ||
| 10,150,0,15.0,1,1,0,1 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This approach was much simpler than what I had and you were able to test for different scenario. Nice! |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,51 +41,60 @@ def test_imap_state_ecliptic(use_test_metakernel): | |
| assert state.shape == (6,) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("_set_spin_data_filepath") | ||
| @pytest.fixture() | ||
| def fake_spin_data(monkeypatch, spice_test_data_path): | ||
| """Generate fake spin dataframe for testing""" | ||
| fake_spin_path = spice_test_data_path / "fake_spin_data.csv" | ||
| monkeypatch.setenv("SPIN_DATA_FILEPATH", str(fake_spin_path)) | ||
| return fake_spin_path | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "query_met_times, expected_type, expected_length", | ||
| "query_met_times, expected", | ||
| [ | ||
| (453051323.0, float, None), # Scalar test | ||
| (np.array([453051323.0, 453051324.0]), float, 2), # Array test | ||
| (np.array([]), None, 0), # Empty array test | ||
| (np.array([453051323.0]), float, 1), # Single element array test | ||
| # 452995203.0 is a midnight time which should have invalid spin | ||
| # phase and period flags on in the spin data file. The spin phase | ||
| # should be invalid. | ||
| (452995203.0, np.nan, None), | ||
| # Test that five minutes after midnight is also invalid since | ||
| # first 10 minutes after midnight are invalid. | ||
| (np.arange(452995203.0, 452995203.0 + 300), np.nan, 300), | ||
| (15, 0.0), # Scalar test | ||
| (np.array([15.1, 30.1]), np.array([0.1 / 15, 0.1 / 15])), # Array test | ||
| (np.array([]), None), # Empty array test | ||
| (np.array([50]), np.array([5 / 15])), # Single element array test | ||
| # The first spin has thruster firing set, but should return valid value | ||
| (5.0, 5 / 15), | ||
| # Test invalid spin period flag causes nan | ||
| (106.0, np.nan), | ||
| # Test invalid spin phase flag causes nans | ||
| (np.array([121, 122, 123]), np.full(3, np.nan)), | ||
| # Test that invalid spin period causes nans | ||
| (np.array([110, 111]), np.full(2, np.nan)), | ||
| # Test for time in missing spin | ||
| (65, np.nan), | ||
| (np.array([65.1, 66]), np.full(2, np.nan)), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. much better tests! |
||
| # Combined test | ||
| ( | ||
| [453011323.0], | ||
| np.nan, | ||
| 1, | ||
| ), # Test for spin phase that's outside of spin phase range | ||
| ( | ||
| 453011323.0, | ||
| np.nan, | ||
| None, | ||
| ), # Test for spin phase that's outside of spin phase range | ||
| np.array([7.5, 30, 61, 75, 106, 121, 136]), | ||
| np.array([0.5, 0, np.nan, 0, np.nan, np.nan, 1 / 15]), | ||
| ), | ||
| ], | ||
| ) | ||
| def test_get_spacecraft_spin_phase(query_met_times, expected_type, expected_length): | ||
| def test_get_spacecraft_spin_phase(query_met_times, expected, fake_spin_data): | ||
| """Test get_spacecraft_spin_phase() with generated spin data.""" | ||
| # Call the function | ||
| spin_phases = get_spacecraft_spin_phase(query_met_times=query_met_times) | ||
|
|
||
| # Check the type of the result | ||
| if expected_type is np.nan: | ||
| assert np.isnan(spin_phases).all(), "Spin phase must be NaN." | ||
| elif isinstance(expected_type, float): | ||
| assert isinstance(spin_phases, float), "Spin phase must be a float." | ||
|
|
||
| # If the expected length is None, it means we're testing a scalar | ||
| if expected_length is None: | ||
| # Test the returned type | ||
| if isinstance(expected, float): | ||
| assert isinstance(spin_phases, float), "Spin phase must be a float." | ||
| elif expected is None: | ||
| assert len(spin_phases) == 0, "Spin phase must be empty." | ||
| else: | ||
| assert ( | ||
| len(spin_phases) == expected_length | ||
| ), f"Spin phase must have length {expected_length} for array input." | ||
| assert spin_phases.shape == expected.shape | ||
| # Test the value | ||
| np.testing.assert_array_almost_equal(spin_phases, expected) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("query_met_times", [-1, 165]) | ||
| def test_get_spacecraft_spin_phase_value_error(query_met_times, fake_spin_data): | ||
| """Test get_spacecraft_spin_phase() for raising ValueError.""" | ||
| with pytest.raises(ValueError, match="Query times"): | ||
| _ = get_spacecraft_spin_phase(query_met_times) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("_set_spin_data_filepath") | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for fixing those! I tried to be careful but still missed those two.