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

Converting between negative data and wcs axes. #91

Merged
merged 3 commits into from
Mar 14, 2018
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: 2 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ Bug Fixes
scalar ``.data``. [#79]
- Fixed ``NDCube.world_axis_physical_types`` in cases where there is a
``missing`` WCS axis. [#80]
- Fixed bugs in converting between negative data and WCS axis
numbers. [#91]

24 changes: 22 additions & 2 deletions ndcube/tests/test_utils_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,42 @@
((0, missing_axis_none), 2),
((1, missing_axis_none), 1),
((0, missing_axis_0_2), 1),
((1, missing_axis_1), 0)])
((1, missing_axis_1), 0),
((-1, missing_axis_0_2), 1),
((-2, missing_axis_1), 2),
((-1, missing_axis_none), 0)])
def test_data_axis_to_wcs_axis(test_input, expected):
assert utils.cube.data_axis_to_wcs_axis(*test_input) == expected


@pytest.mark.parametrize("test_input", [(-2, missing_axis_0_2), (1, missing_axis_0_2)])
def test_data_axis_to_wcs_axis_error(test_input):
with pytest.raises(IndexError):
utils.cube.data_axis_to_wcs_axis(*test_input)


@pytest.mark.parametrize(
"test_input,expected",
[((None, missing_axis_none), None),
((0, missing_axis_none), 2),
((1, missing_axis_none), 1),
((1, missing_axis_0_2), 0),
((0, missing_axis_1), 1)])
((0, missing_axis_1), 1),
((-1, missing_axis_0_2), None),
((-2, missing_axis_0_2), 0),
((-2, missing_axis_1), None),
((-3, missing_axis_1), 1),
((-1, missing_axis_none), 0)])
def test_wcs_axis_to_data_axis(test_input, expected):
assert utils.cube.wcs_axis_to_data_axis(*test_input) == expected


@pytest.mark.parametrize("test_input", [(-10, missing_axis_0_2), (10, missing_axis_0_2)])
def test_wcs_axis_to_data_axis_error(test_input):
with pytest.raises(IndexError):
utils.cube.data_axis_to_wcs_axis(*test_input)


def test_select_order():
lists = [['TIME', 'WAVE', 'HPLT-TAN',
'HPLN-TAN'], ['WAVE', 'HPLT-TAN', 'UTC',
Expand Down
10 changes: 10 additions & 0 deletions ndcube/utils/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def data_axis_to_wcs_axis(data_axis, missing_axis):
if data_axis is None:
result = None
else:
if data_axis < 0:
data_axis = np.invert(missing_axis).sum() + data_axis
if data_axis > np.invert(missing_axis).sum()-1 or data_axis < 0:
raise IndexError("Data axis out of range. Number data axes = {0}".format(
np.invert(missing_axis).sum()))
result = len(missing_axis)-np.where(np.cumsum(
[b is False for b in missing_axis][::-1]) == data_axis+1)[0][0]-1
return result
Expand All @@ -25,6 +30,11 @@ def wcs_axis_to_data_axis(wcs_axis, missing_axis):
if wcs_axis is None:
result = None
else:
if wcs_axis < 0:
wcs_axis = len(missing_axis) + wcs_axis
if wcs_axis > len(missing_axis)-1 or wcs_axis < 0:
raise IndexError("WCS axis out of range. Number WCS axes = {0}".format(
len(missing_axis)))
if missing_axis[wcs_axis]:
result = None
else:
Expand Down