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

Issue 170 align spectrum along trace #235

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fixed codestyle issues.
  • Loading branch information
hpparvi committed Dec 10, 2024
commit 98cc0ad20e809cf45fd1303210a7bec4c51ace30
20 changes: 10 additions & 10 deletions specreduce/tests/test_align_along_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ def mk_test_image():
def test_align_spectrum_along_trace_bad_input():
image, trace = mk_test_image()
with pytest.raises(ValueError, match='Unre'):
im = align_spectrum_along_trace(image, None)
im = align_spectrum_along_trace(image, None) # noqa

with pytest.raises(ValueError, match='method must be'):
im = align_spectrum_along_trace(image, trace, method='int')
im = align_spectrum_along_trace(image, trace, method='int') # noqa

with pytest.raises(ValueError, match='Spectral axis length'):
im = align_spectrum_along_trace(image.T, trace, method='interpolate', disp_axis=0)
im = align_spectrum_along_trace(image.T, trace, method='interpolate', disp_axis=0) # noqa

with pytest.raises(ValueError, match='Displacement axis must be'):
im = align_spectrum_along_trace(image, trace, disp_axis=2)
im = align_spectrum_along_trace(image, trace, disp_axis=2) # noqa

with pytest.raises(ValueError, match='The number of image dimensions must be'):
im = align_spectrum_along_trace(np.zeros((3,6,9)), trace)
im = align_spectrum_along_trace(np.zeros((3, 6, 9)), trace) # noqa


@pytest.mark.parametrize("method, truth_data, truth_mask, truth_ucty",
Expand All @@ -39,18 +39,18 @@ def test_align_spectrum_along_trace_bad_input():
[0, 0, 0, 0.25, 0.5, 0.25, 0, 0, 0]]).T,
np.array([[0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0]]).astype(bool).T,
np.array([[1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. , 1. ],
np.array([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]]).T),
('shift',
np.array([[0. , 0. , 0. , 0. , 1. , 0. , 0. , 0. , 0. ],
[0. , 0. , 0. , 0.5, 0.5, 0. , 0. , 0. , 0. ]]).T,
np.array([[0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0.5, 0.5, 0., 0., 0., 0.]]).T,
np.array([[0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0]]).astype(bool).T,
np.ones((9,2)))],
np.ones((9, 2)))],
ids=('method=interpolate', 'method=shift'))
def test_align_spectrum_along_trace(method, truth_data, truth_mask, truth_ucty):
image, trace = mk_test_image()
im = align_spectrum_along_trace(image, trace, method=method)
im = align_spectrum_along_trace(image, trace, method=method)
assert im.shape == image.shape
assert im.unit == u.DN
assert im.uncertainty.uncertainty_type == 'var'
Expand Down
3 changes: 2 additions & 1 deletion specreduce/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
from specreduce.tracing import Trace, FlatTrace
from specreduce.extract import _ap_weight_image, _align_along_trace

__all__ = ['measure_cross_dispersion_profile', '_align_along_trace', 'align_2d_spectrum_along_trace']
__all__ = ['measure_cross_dispersion_profile', '_align_along_trace',
'align_2d_spectrum_along_trace']


def _get_image_ndim(image):
if isinstance(image, np.ndarray):
return image.ndim
elif isinstance(image, NDData):
return image.data.ndim

Check warning on line 20 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L17-L20

Added lines #L17 - L20 were not covered by tests
else:
raise ValueError('Unrecognized image data format.')

Check warning on line 22 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L22

Added line #L22 was not covered by tests


def align_2d_spectrum_along_trace(image: NDData | np.ndarray,
Expand Down Expand Up @@ -61,74 +62,74 @@
require additional pre-processing (e.g., geometric rectification) before using
this function.
"""
if _get_image_ndim(image) > 2:
raise ValueError('The number of image dimensions must be 2.')
if not (0 <= disp_axis <= 1):
raise ValueError('Displacement axis must be either 0 or 1.')

Check warning on line 68 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L65-L68

Added lines #L65 - L68 were not covered by tests

if isinstance(trace, Trace):
trace = trace.trace.data
elif isinstance(trace, (np.ndarray, Number)):
pass

Check warning on line 73 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L70-L73

Added lines #L70 - L73 were not covered by tests
else:
raise ValueError('Unrecognized trace format.')

Check warning on line 75 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L75

Added line #L75 was not covered by tests

image = _ImageParser()._parse_image(image, disp_axis=disp_axis)
data = image.data
mask = image.mask | ~np.isfinite(data)
ucty = image.uncertainty.represent_as(VarianceUncertainty).array

Check warning on line 80 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L77-L80

Added lines #L77 - L80 were not covered by tests

if disp_axis == 0:
data = data.T
mask = mask.T
ucty = ucty.T

Check warning on line 85 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L82-L85

Added lines #L82 - L85 were not covered by tests

n_rows = data.shape[0]
n_cols = data.shape[1]

Check warning on line 88 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L87-L88

Added lines #L87 - L88 were not covered by tests

rows = np.broadcast_to(np.arange(n_rows)[:, None], data.shape)
cols = np.broadcast_to(np.arange(n_cols), data.shape)

Check warning on line 91 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L90-L91

Added lines #L90 - L91 were not covered by tests

if method == 'interpolate':

Check warning on line 93 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L93

Added line #L93 was not covered by tests
# Calculate the real and integer shifts
# and the interpolation weights.
shifts = trace - n_rows / 2.0
k = np.floor(shifts).astype(int)
a = shifts - k

Check warning on line 98 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L96-L98

Added lines #L96 - L98 were not covered by tests

# Calculate the shifted indices and mask the
# edge pixels without information.
ix1 = rows + k
ix2 = ix1 + 1
shift_mask = (ix1 < 0) | (ix1 > n_rows - 2)
ix1 = np.clip(ix1, 0, n_rows - 1)
ix2 = np.clip(ix2, 0, n_rows - 1)

Check warning on line 106 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L102-L106

Added lines #L102 - L106 were not covered by tests

# Shift the data, uncertainties, and the mask using linear
# interpolation.
data_r = (1.0-a)*data[ix1, cols] + a*data[ix2, cols]
ucty_r = (1.0-a)**2*ucty[ix1, cols] + a**2*ucty[ix2, cols]
mask_r = mask[ix1, cols] | mask[ix2, cols] | shift_mask

Check warning on line 112 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L110-L112

Added lines #L110 - L112 were not covered by tests

elif method == 'shift':
shifts = trace.astype(int) - n_rows // 2
ix = rows + shifts
shift_mask = (ix < 0) | (ix > n_rows - 1)
ix = np.clip(ix, 0, n_rows - 1)

Check warning on line 118 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L114-L118

Added lines #L114 - L118 were not covered by tests

data_r = data[ix, cols]
ucty_r = ucty[ix, cols]
mask_r = mask[ix, cols] | shift_mask

Check warning on line 122 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L120-L122

Added lines #L120 - L122 were not covered by tests

else:
raise ValueError("method must be either 'interpolate' or 'shift'.")

Check warning on line 125 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L125

Added line #L125 was not covered by tests

if disp_axis == 0:
data_r = data_r.T
mask_r = mask_r.T
ucty_r = ucty_r.T

Check warning on line 130 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L127-L130

Added lines #L127 - L130 were not covered by tests

return Spectrum1D(data_r * image.unit, mask=mask_r, meta=image.meta,

Check warning on line 132 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L132

Added line #L132 was not covered by tests
uncertainty=VarianceUncertainty(ucty_r).represent_as(image.uncertainty))


Expand Down Expand Up @@ -307,7 +308,7 @@

# now that we have figured out the mask for the window in cross-disp. axis,
# select only the pixel(s) we want to include in measuring the avg. profile
pixel_mask = np.ones(image.shape)

Check warning on line 311 in specreduce/utils/utils.py

View check run for this annotation

Codecov / codecov/patch

specreduce/utils/utils.py#L311

Added line #L311 was not covered by tests
pixel_mask[:, pixels] = 0

# combine these masks to isolate the rows and cols used to measure profile
Expand Down
Loading