-
-
Notifications
You must be signed in to change notification settings - Fork 56
Added unit tests that mimics a dynamic spectra #931
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
Open
nabobalis
wants to merge
6
commits into
sunpy:main
Choose a base branch
from
nabobalis:tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1fe08da
First pass
nabobalis 32aeae9
Remove example
nabobalis a3320bf
Update tests to be better
nabobalis c2aef68
MORE FIXES
nabobalis ef02a10
Add ignore for runtimewarning for astropy and angles in wcs
nabobalis 7bbce5b
Move figure test env forward
nabobalis 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,135 @@ | ||
| """ | ||
| Tests to simulate dynamic spectrum WCSes (frequency x time). | ||
| """ | ||
| import pytest | ||
| from numpy.testing import assert_allclose | ||
|
|
||
| import astropy.units as u | ||
|
|
||
| from ndcube.wcs.wrappers import ResampledLowLevelWCS | ||
|
|
||
|
|
||
| def _world_at(cube, time_pixel, freq_pixel): | ||
| return cube.wcs.low_level_wcs.pixel_to_world_values(time_pixel, freq_pixel) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("ndc", [ | ||
| "ndcube_gwcs_2d_t_f_linear", | ||
| "ndcube_gwcs_2d_t_f_log", | ||
| ], indirect=True) | ||
| def test_dynspec_array_axis_physical_types(ndc): | ||
| types = ndc.array_axis_physical_types | ||
| assert "em.freq" in types[0] | ||
| assert "time" in types[1] | ||
|
|
||
|
|
||
| def test_linear_dynspec_pixel_to_world(ndcube_gwcs_2d_t_f_linear): | ||
| time, freq = ndcube_gwcs_2d_t_f_linear.wcs.low_level_wcs.pixel_to_world_values(3, 2) | ||
| assert_allclose(time, 42.0) | ||
| assert_allclose(freq, 2e6) | ||
|
|
||
|
|
||
| def test_linear_dynspec_world_to_pixel(ndcube_gwcs_2d_t_f_linear): | ||
| pix_t, pix_f = ndcube_gwcs_2d_t_f_linear.wcs.low_level_wcs.world_to_pixel_values(28.0, 4e6) | ||
| assert_allclose(pix_t, 2.0) | ||
| assert_allclose(pix_f, 4.0) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("bin_shape", "expected_shape", "expected_time", "expected_freq"), [ | ||
| ((2, 1), (8, 10), 0.0, 0.5e6), | ||
| ((1, 2), (16, 5), 7.0, 0.0), | ||
| ]) | ||
| def test_linear_dynspec_rebin_wcs(ndcube_gwcs_2d_t_f_linear, bin_shape, | ||
| expected_shape, expected_time, expected_freq): | ||
| rebinned = ndcube_gwcs_2d_t_f_linear.rebin(bin_shape) | ||
| time0, freq0 = rebinned.wcs.low_level_wcs.pixel_to_world_values(0, 0) | ||
|
|
||
| assert rebinned.shape == expected_shape | ||
| assert isinstance(rebinned.wcs.low_level_wcs, ResampledLowLevelWCS) | ||
| assert_allclose(time0, expected_time) | ||
| assert_allclose(freq0, expected_freq) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("lower_corner", "upper_corner", "expected_shape"), [ | ||
| ([None, 3e6 * u.Hz], [None, 7e6 * u.Hz], (5, 10)), | ||
| ([14 * u.s, None], [56 * u.s, None], (16, 4)), | ||
| ]) | ||
| def test_linear_dynspec_crop_by_values_shape(ndcube_gwcs_2d_t_f_linear, | ||
| lower_corner, upper_corner, | ||
| expected_shape): | ||
| cropped = ndcube_gwcs_2d_t_f_linear.crop_by_values(lower_corner, upper_corner) | ||
| assert cropped.shape == expected_shape | ||
|
|
||
|
|
||
| def test_log_dynspec_world_axis_units(ndcube_gwcs_2d_t_f_log): | ||
| assert ndcube_gwcs_2d_t_f_log.wcs.world_axis_units == ("s", "Hz") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("time_pixel", "freq_pixel", "expected_time", "expected_freq"), [ | ||
| (0, 0, 0.0, 3.992e6), | ||
| (9, 15, 122.5, 978.572e6), | ||
| ]) | ||
| def test_log_dynspec_pixel_to_world_endpoints(ndcube_gwcs_2d_t_f_log, | ||
| time_pixel, freq_pixel, | ||
| expected_time, expected_freq): | ||
| time, freq = ndcube_gwcs_2d_t_f_log.wcs.low_level_wcs.pixel_to_world_values( | ||
| time_pixel, freq_pixel) | ||
| assert_allclose(time, expected_time) | ||
| assert_allclose(freq, expected_freq, rtol=1e-6) | ||
|
|
||
|
|
||
| def test_log_dynspec_world_to_pixel_roundtrip(ndcube_gwcs_2d_t_f_log): | ||
| time, freq = _world_at(ndcube_gwcs_2d_t_f_log, 3, 7) | ||
| pix_t, pix_f = ndcube_gwcs_2d_t_f_log.wcs.low_level_wcs.world_to_pixel_values( | ||
| time, freq) | ||
| assert_allclose(pix_t, 3.0, atol=1e-10) | ||
| assert_allclose(pix_f, 7.0, atol=1e-10) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("bin_shape", "expected_shape", "axis"), [ | ||
| ((2, 1), (8, 10), "freq"), | ||
| ((1, 2), (16, 5), "time"), | ||
| ]) | ||
| def test_log_dynspec_rebin_wcs_midpoint(ndcube_gwcs_2d_t_f_log, bin_shape, | ||
| expected_shape, axis): | ||
| rebinned = ndcube_gwcs_2d_t_f_log.rebin(bin_shape) | ||
| time0, freq0 = rebinned.wcs.low_level_wcs.pixel_to_world_values(0, 0) | ||
|
|
||
| assert rebinned.shape == expected_shape | ||
| assert isinstance(rebinned.wcs.low_level_wcs, ResampledLowLevelWCS) | ||
| if axis == "freq": | ||
| _, freq_left = _world_at(ndcube_gwcs_2d_t_f_log, 0, 0) | ||
| _, freq_right = _world_at(ndcube_gwcs_2d_t_f_log, 0, 1) | ||
| assert_allclose(freq0, (freq_left + freq_right) / 2, rtol=1e-6) | ||
| else: | ||
| time_left, _ = _world_at(ndcube_gwcs_2d_t_f_log, 0, 0) | ||
| time_right, _ = _world_at(ndcube_gwcs_2d_t_f_log, 1, 0) | ||
| assert_allclose(time0, (time_left + time_right) / 2, rtol=1e-6) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize(("lower_corner", "upper_corner", "expected_shape", | ||
| "axis", "bounds"), [ | ||
| ([None, 10e6 * u.Hz], [None, 100e6 * u.Hz], (8, 10), "freq", (10e6, 100e6)), | ||
| ([20 * u.s, None], [80 * u.s, None], (16, 6), "time", (20.0, 80.0)), | ||
| ]) | ||
| def test_log_dynspec_crop_by_values_single_axis(ndcube_gwcs_2d_t_f_log, | ||
| lower_corner, upper_corner, | ||
| expected_shape, axis, bounds): | ||
| cropped = ndcube_gwcs_2d_t_f_log.crop_by_values(lower_corner, upper_corner) | ||
| assert cropped.shape == expected_shape | ||
|
|
||
| if axis == "freq": | ||
| values = [cropped.wcs.low_level_wcs.pixel_to_world_values(0, i)[1] | ||
| for i in range(cropped.shape[0])] | ||
| else: | ||
| values = [cropped.wcs.low_level_wcs.pixel_to_world_values(i, 0)[0] | ||
| for i in range(cropped.shape[1])] | ||
|
|
||
| assert values[0] <= bounds[0] | ||
| assert values[-1] >= bounds[1] | ||
|
|
||
|
|
||
| def test_log_dynspec_crop_by_freq_and_time(ndcube_gwcs_2d_t_f_log): | ||
| cropped = ndcube_gwcs_2d_t_f_log.crop_by_values( | ||
| [20 * u.s, 10e6 * u.Hz], [80 * u.s, 100e6 * u.Hz]) | ||
| assert cropped.shape == (8, 6) |
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
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.
I am not sure if this is quite true but it did break a unit test.
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.
This is a real regression in the oldest deps, gwcs looks like it used to handle this internally?!