Skip to content

Commit

Permalink
Convert required_parameters to attribute; tidy up redshift's apply
Browse files Browse the repository at this point in the history
  • Loading branch information
OliviaLynn committed Jul 10, 2024
1 parent ea8ef0a commit 67413bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
23 changes: 9 additions & 14 deletions src/tdastro/base_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,7 @@ def add_effect(self, effect, **kwargs):
Raises a ``AttributeError`` if the PhysicalModel does not have all of the
required attributes.
"""
required: list = effect.required_parameters()
for parameter in required:
for parameter in effect.required_parameters:
# Raise an AttributeError if the parameter is missing or set to None.
if getattr(self, parameter) is None:
raise AttributeError(f"Parameter {parameter} unset for model {type(self).__name__}")
Expand Down Expand Up @@ -274,26 +273,22 @@ def sample_parameters(self, include_effects=True, **kwargs):


class EffectModel(ParameterizedModel):
"""A physical or systematic effect to apply to an observation."""
"""A physical or systematic effect to apply to an observation.
Attributes
----------
required_parameters : `list` of `str`
A list of the parameters of a PhysicalModel that this effect needs to access.
"""

def __init__(self, **kwargs):
super().__init__(**kwargs)
self.required_parameters = []

def __str__(self):
"""Return the string representation of the model."""
return "EffectModel"

def required_parameters(self):
"""Returns a list of the parameters of a PhysicalModel
that this effect needs to access.
Returns
-------
parameters : `list` of `str`
A list of every required parameter the effect needs.
"""
return []

def apply(self, flux_density, wavelengths=None, physical_model=None, **kwargs):
"""Apply the effect to observations (flux_density values)
Expand Down
26 changes: 9 additions & 17 deletions src/tdastro/effects/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ class Redshift(EffectModel):
"""A redshift effect model.
This contains a "pre-effect" method, which is used to calculate the emitted wavelengths/times
needed to give us the observed wavelengths and times given the redshift.
Attributes
----------
redshift : `float`
The redshift.
needed to give us the observed wavelengths and times given the redshift. Times are calculated
with respect to the t0 of the given model.
Notes
-----
Expand All @@ -28,25 +24,23 @@ def __init__(self, redshift=None, t0=None, **kwargs):
redshift : `float`
The redshift.
t0 : `float`
The epoch of the peak or the zero phase, date. # TODO WORDING (1/?)
The epoch of the peak or the zero phase, date.
# TODO WORDING (1/3) -> Both how this is written, and to check, are we picking t0 or epoch?
**kwargs : `dict`, optional
Any additional keyword arguments.
"""
super().__init__(**kwargs)
self.required_parameters["redshift", "t0"]
self.add_parameter("redshift", redshift, required=True, **kwargs)
self.add_parameter("t0", t0, required=True, **kwargs)

def required_parameters(self): # TODO - can this just be an attribute?
"""Return the required parameters for the Redshift effect model."""
return ["redshift", "t0"]

def __str__(self) -> str:
"""Return a string representation of the Redshift effect model."""
return f"RedshiftEffect(redshift={self.redshift})"

def pre_effect(
self, observed_times, observed_wavelengths, **kwargs
): # TODO WORDING (2/?) -> should I change "emitted" to "rest"?
): # TODO WORDING (2/3) -> Should I change "emitted" to "rest"? What is standard here?
"""Calculate the emitted times and wavelengths needed to give us the observed times and wavelengths
given the redshift.
Expand All @@ -64,15 +58,15 @@ def pre_effect(
tuple of (numpy.ndarray, numpy.ndarray)
The emission-frame times and wavelengths needed to generate the emission-frame flux densities,
which will then be redshifted to observation-frame flux densities at the observation-frame
times and wavelengths.
times and wavelengths. # TODO WORDING (3/3)
"""
observed_times_rel_to_t0 = observed_times - self.t0
emitted_times_rel_to_t0 = observed_times_rel_to_t0 / (1 + self.redshift)
emitted_times = emitted_times_rel_to_t0 + self.t0
emitted_wavelengths = observed_wavelengths / (1 + self.redshift)
return (emitted_times, emitted_wavelengths)

def apply(self, flux_density, wavelengths=None, physical_model=None, **kwargs):
def apply(self, flux_density, wavelengths, physical_model=None, **kwargs):
"""Apply the effect to observations (flux_density values).
Parameters
Expand All @@ -90,8 +84,6 @@ def apply(self, flux_density, wavelengths=None, physical_model=None, **kwargs):
Returns
-------
flux_density : `numpy.ndarray`
The results.
The redshifted results.
"""
if physical_model is None:
raise ValueError("No physical model provided to Redshift effect.")
return flux_density / (1 + self.redshift)

0 comments on commit 67413bc

Please sign in to comment.