-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
Better management of Trace attribute mutability #147
Open
bmorris3
wants to merge
4
commits into
astropy:main
Choose a base branch
from
bmorris3:fix-issue-126
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
62cbf11
Implementing BaseTemplate to manage Trace attr mutability
bmorris3 511b1cd
Making Trace the base, better organization for Trace attrs
bmorris3 9a7cee8
Adding mutability test, plus FlatTrace.shift overload
bmorris3 46c33a6
Removing duplicate __all__ str
bmorris3 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 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 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 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 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
# Licensed under a 3-clause BSD style license - see LICENSE.rst | ||
|
||
from copy import deepcopy | ||
from dataclasses import dataclass | ||
from dataclasses import dataclass, field | ||
import warnings | ||
|
||
from astropy.modeling import fitting, models | ||
|
@@ -13,7 +13,7 @@ | |
__all__ = ['Trace', 'FlatTrace', 'ArrayTrace', 'KosmosTrace'] | ||
|
||
|
||
@dataclass | ||
@dataclass(init=False, frozen=True) | ||
class Trace: | ||
""" | ||
Basic tracing class that by default traces the middle of the image. | ||
|
@@ -22,44 +22,28 @@ class Trace: | |
---------- | ||
image : `~astropy.nddata.CCDData` | ||
Image to be traced | ||
|
||
Properties | ||
---------- | ||
shape : tuple | ||
Shape of the array describing the trace | ||
""" | ||
image: CCDData | ||
_trace: (np.ndarray, None) = field(repr=False) | ||
|
||
def __init__(self, image, _trace=None): | ||
object.__setattr__(self, '_trace', self._default_trace(image)) | ||
object.__setattr__(self, 'image', image) | ||
|
||
def __post_init__(self): | ||
self.trace_pos = self.image.shape[0] / 2 | ||
self.trace = np.ones_like(self.image[0]) * self.trace_pos | ||
# this class only exists to catch __post_init__ calls in its | ||
# subclasses, so that super().__post_init__ calls work correctly. | ||
pass | ||
|
||
def __getitem__(self, i): | ||
return self.trace[i] | ||
|
||
@property | ||
def shape(self): | ||
return self.trace.shape | ||
|
||
def shift(self, delta): | ||
""" | ||
Shift the trace by delta pixels perpendicular to the axis being traced | ||
|
||
Parameters | ||
---------- | ||
delta : float | ||
Shift to be applied to the trace | ||
""" | ||
# act on self.trace.data to ignore the mask and then re-mask when calling _bound_trace | ||
self.trace = np.asarray(self.trace.data) + delta | ||
self._bound_trace() | ||
|
||
def _bound_trace(self): | ||
""" | ||
Mask trace positions that are outside the upper/lower bounds of the image. | ||
""" | ||
ny = self.image.shape[0] | ||
self.trace = np.ma.masked_outside(self.trace, 0, ny-1) | ||
object.__setattr__(self, '_trace', np.ma.masked_outside(self._trace, 0, ny - 1)) | ||
|
||
def __add__(self, delta): | ||
""" | ||
|
@@ -77,8 +61,41 @@ def __sub__(self, delta): | |
""" | ||
return self.__add__(-delta) | ||
|
||
def shift(self, delta): | ||
""" | ||
Shift the trace by delta pixels perpendicular to the axis being traced | ||
|
||
Parameters | ||
---------- | ||
delta : float | ||
Shift to be applied to the trace | ||
""" | ||
# act on self._trace.data to ignore the mask and then re-mask when calling _bound_trace | ||
object.__setattr__(self, '_trace', np.asarray(self._trace.data) + delta) | ||
self._bound_trace() | ||
|
||
@dataclass | ||
@property | ||
def shape(self): | ||
return self._trace.shape | ||
|
||
@property | ||
def trace(self): | ||
return self._trace | ||
|
||
@staticmethod | ||
def _default_trace(image, trace_pos=None): | ||
""" | ||
Compute a default trace position and trace array using only | ||
the image dimensions. | ||
""" | ||
if trace_pos is None: | ||
trace_pos = image.shape[0] / 2 | ||
|
||
trace = np.ones_like(image[0]) * trace_pos | ||
return trace | ||
|
||
|
||
@dataclass(init=False, frozen=True) | ||
class FlatTrace(Trace): | ||
""" | ||
Trace that is constant along the axis being traced | ||
|
@@ -92,10 +109,12 @@ class FlatTrace(Trace): | |
trace_pos : float | ||
Position of the trace | ||
""" | ||
trace_pos: float | ||
_trace_pos: (float, np.ndarray) = field(repr=False) | ||
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. Can you help me understand when/why this |
||
|
||
def __post_init__(self): | ||
self.set_position(self.trace_pos) | ||
def __init__(self, image, trace_pos): | ||
object.__setattr__(self, '_trace_pos', trace_pos) | ||
super().__init__(image, _trace=self._default_trace(image, trace_pos)) | ||
self.set_position(trace_pos) | ||
|
||
def set_position(self, trace_pos): | ||
""" | ||
|
@@ -106,12 +125,29 @@ def set_position(self, trace_pos): | |
trace_pos : float | ||
Position of the trace | ||
""" | ||
self.trace_pos = trace_pos | ||
self.trace = np.ones_like(self.image[0]) * self.trace_pos | ||
object.__setattr__(self, '_trace_pos', trace_pos) | ||
object.__setattr__(self, '_trace', np.ones_like(self.image[0]) * trace_pos) | ||
self._bound_trace() | ||
|
||
@property | ||
def trace_pos(self): | ||
return self._trace_pos | ||
|
||
@dataclass | ||
def shift(self, delta): | ||
""" | ||
Shift the trace by delta pixels perpendicular to the axis being traced | ||
|
||
Parameters | ||
---------- | ||
delta : float | ||
Shift to be applied to the trace | ||
""" | ||
# act on self._trace.data to ignore the mask and then re-mask when calling _bound_trace | ||
object.__setattr__(self, '_trace_pos', self._trace_pos + delta) | ||
super().shift(delta) | ||
|
||
|
||
@dataclass(init=False, frozen=True) | ||
class ArrayTrace(Trace): | ||
""" | ||
Define a trace given an array of trace positions | ||
|
@@ -121,24 +157,26 @@ class ArrayTrace(Trace): | |
trace : `numpy.ndarray` | ||
Array containing trace positions | ||
""" | ||
trace: np.ndarray | ||
|
||
def __post_init__(self): | ||
def __init__(self, image, trace): | ||
super().__init__(image, _trace=trace) | ||
nx = self.image.shape[1] | ||
nt = len(self.trace) | ||
nt = len(trace) | ||
if nt != nx: | ||
if nt > nx: | ||
# truncate trace to fit image | ||
self.trace = self.trace[0:nx] | ||
trace = trace[0:nx] | ||
else: | ||
# assume trace starts at beginning of image and pad out trace to fit. | ||
# padding will be the last value of the trace, but will be masked out. | ||
padding = np.ma.MaskedArray(np.ones(nx - nt) * self.trace[-1], mask=True) | ||
self.trace = np.ma.hstack([self.trace, padding]) | ||
padding = np.ma.MaskedArray(np.ones(nx - nt) * trace[-1], mask=True) | ||
trace = np.ma.hstack([trace, padding]) | ||
object.__setattr__(self, '_trace', trace) | ||
|
||
self._bound_trace() | ||
|
||
|
||
@dataclass | ||
@dataclass(init=False, frozen=True) | ||
class KosmosTrace(Trace): | ||
""" | ||
Trace the spectrum aperture in an image. | ||
|
@@ -192,14 +230,24 @@ class KosmosTrace(Trace): | |
4) add other interpolation modes besides spline, maybe via | ||
specutils.manipulation methods? | ||
""" | ||
bins: int = 20 | ||
guess: float = None | ||
window: int = None | ||
peak_method: str = 'gaussian' | ||
bins: int | ||
guess: float | ||
window: int | ||
peak_method: str | ||
_crossdisp_axis = 0 | ||
_disp_axis = 1 | ||
|
||
def __post_init__(self): | ||
def _process_init_kwargs(self, **kwargs): | ||
for attr, value in kwargs.items(): | ||
object.__setattr__(self, attr, value) | ||
|
||
def __init__(self, image, bins=20, guess=None, window=None, peak_method='gaussian'): | ||
# This method will assign the user supplied value (or default) to the attrs: | ||
self._process_init_kwargs( | ||
bins=bins, guess=guess, window=window, peak_method=peak_method | ||
) | ||
super().__init__(image, _trace=self._default_trace(image)) | ||
|
||
# handle multiple image types and mask uncaught invalid values | ||
if isinstance(self.image, NDData): | ||
img = np.ma.masked_invalid(np.ma.masked_array(self.image.data, | ||
|
@@ -223,7 +271,7 @@ def __post_init__(self): | |
|
||
if not isinstance(self.bins, int): | ||
warnings.warn('TRACE: Converting bins to int') | ||
self.bins = int(self.bins) | ||
object.__setattr__(self, 'bins', int(self.bins)) | ||
|
||
if self.bins < 4: | ||
raise ValueError('bins must be >= 4') | ||
|
@@ -240,7 +288,7 @@ def __post_init__(self): | |
"length of the image's spatial direction") | ||
elif self.window is not None and not isinstance(self.window, int): | ||
warnings.warn('TRACE: Converting window to int') | ||
self.window = int(self.window) | ||
object.__setattr__(self, 'window', int(self.window)) | ||
|
||
# set max peak location by user choice or wavelength with max avg flux | ||
ztot = img.sum(axis=self._disp_axis) / img.shape[self._disp_axis] | ||
|
@@ -343,4 +391,4 @@ def __post_init__(self): | |
warnings.warn("TRACE ERROR: No valid points found in trace") | ||
trace_y = np.tile(np.nan, len(x_bins)) | ||
|
||
self.trace = np.ma.masked_invalid(trace_y) | ||
object.__setattr__(self, '_trace', np.ma.masked_invalid(trace_y)) |
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.
Is this just for alphabetization purposes?