Skip to content

Commit

Permalink
Reducing number of phase parameter variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
astronomerritt committed Mar 7, 2024
1 parent 3d8620f commit 76b19ba
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 67 deletions.
75 changes: 25 additions & 50 deletions src/adler/dataclasses/AdlerData.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ class AdlerData:
List of filters under investigation.
model_lists : list
List of lists of models per-filter. Length = len(filter_list). For example, if the filters are ['u', 'g', 'i'],
and HG has been calculated for all three and HG1G2 for 'u' only, model_lists = [ ['HG', 'HG1G2'], ['HG'], ['HG'] ].
This list of lists is used to track the parameters in the H and phase_parameter attributes, which will take an identical shape.
List of lists of models per-filter. Length = len(filter_list) such that [[filter1_model1, filter1_model2], [filter2_model1]]. Used to index values
in the H_adler, phase_parameter_1, phase_parameter_1_err, phase_parameter_2 and phase_parameter_2_err list structures.
phaseAngle_min_adler : array_like
Minimum phase angle of observations used in fitting model (degrees). Size = len(filter_list).
Expand All @@ -35,17 +34,12 @@ class AdlerData:
H_err_adler : list
Error in absolute magnitude. List of lists arranged identically to model_lists, with per-filter and per-model values.
phase_parameter_1 : list
First phase parameter of the model in question. List of lists arranged identically to model_lists, with per-filter and per-model values.
phase_parameters : list
Phase parameters of the model in question. List of lists arranged as model_lists, with per-filter and per-model values: however, phase_parameters[filter_index][model_index] is itself a
list of either one or two values, depending on the number of phase parameters.
phase_parameter_1_err : list
Error in the first phase parameter. List of lists arranged identically to model_lists, with per-filter and per-model values.
phase_parameter_2 : list
Second phase parameter of the model in question. List of lists arranged identically to model_lists, with per-filter and per-model values.
phase_parameter_2_err : list
Error in the second phase parameter. List of lists arranged identically to model_lists, with per-filter and per-model values.
phase_parameter_err : list
Error in the first phase parameter. List of lists arranged identically to phase_parameters.
"""

Expand All @@ -61,10 +55,8 @@ class AdlerData:
# They can be cast to Numpy arrays later if needed.
H_adler: list = field(default_factory=list, init=False)
H_err_adler: list = field(default_factory=list, init=False)
phase_parameter_1: list = field(default_factory=list, init=False)
phase_parameter_2: list = field(default_factory=list, init=False)
phase_parameter_1_err: list = field(default_factory=list, init=False)
phase_parameter_2_err: list = field(default_factory=list, init=False)
phase_parameters: list = field(default_factory=list, init=False)
phase_parameters_err: list = field(default_factory=list, init=False)

def __post_init__(self):
"""This runs post-initialisation and creates all of the class attributes where one dimension is "filters" to ensure the arrays
Expand All @@ -81,10 +73,8 @@ def __post_init__(self):

self.H_adler = [[] for a in range(0, filter_length)]
self.H_err_adler = [[] for a in range(0, filter_length)]
self.phase_parameter_1 = [[] for a in range(0, filter_length)]
self.phase_parameter_2 = [[] for a in range(0, filter_length)]
self.phase_parameter_1_err = [[] for a in range(0, filter_length)]
self.phase_parameter_2_err = [[] for a in range(0, filter_length)]
self.phase_parameters = [[] for a in range(0, filter_length)]
self.phase_parameters_err = [[] for a in range(0, filter_length)]

def populate_phase_parameters(
self,
Expand All @@ -96,10 +86,8 @@ def populate_phase_parameters(
arc,
H,
H_err,
parameter_1,
parameter_1_err,
parameter_2=None,
parameter_2_err=None,
parameters,
parameters_err,
):
"""Convenience method to correctly populate phase curve arrays/lists.
Expand Down Expand Up @@ -130,54 +118,41 @@ def populate_phase_parameters(
H_err : float
Error on the absolute magnitude.
parameter_1 : float
First phase parameter of the model.
parameter_1_err : float
Error of the first phase parameter.
parameters : list
Phase parameters of the model.
parameter_2 : float, optional
Second phase parameter of the model. Default is None.
parameter_2_err : float, optional
Error of the second phase parameter. Default is None.
parameters_err : list
Phase parameter errors.
"""

# Raise an exception if only one of parameter_2 and parameter_2_err is given.
if (parameter_2 is None) != (parameter_2_err is None):
raise Exception(
"If using a model with 2 phase parameters, both parameter_2 and parameter_2_err must be supplied."
)

# Make sure the supplied filter is in the filter list.
try:
filter_index = self.filter_list.index(filter_name)
except ValueError:
raise Exception("Filter {} is not in supplied filter list.".format(filter_name))

# If parameters and/or parameters_err are not lists, error out.
if not isinstance(parameters, list) or not isinstance(parameters_err, list):
raise TypeError("Both parameters and parameters_err arguments must be lists.")

self.phaseAngle_min_adler[filter_index] = phaseAngle_min
self.phaseAngle_range_adler[filter_index] = phaseAngle_range
self.nobs_adler[filter_index] = nobs
self.arc_adler[filter_index] = arc

# Check and see if the model has already been calculated for this filter.
if model_name not in self.model_lists[filter_index]:
self.model_lists[filter_index].append(model_name)

self.H_adler[filter_index].append(H)
self.H_err_adler[filter_index].append(H_err)
self.phase_parameter_1[filter_index].append(parameter_1)
self.phase_parameter_1_err[filter_index].append(parameter_1_err)
self.phase_parameter_2[filter_index].append(parameter_2)
self.phase_parameter_2_err[filter_index].append(parameter_2_err)
self.phase_parameters[filter_index].append(parameters)
self.phase_parameters_err[filter_index].append(parameters_err)

else:
model_index = self.model_lists[filter_index].index(model_name)

self.H_adler[filter_index][model_index] = H
self.H_err_adler[filter_index][model_index] = H_err
self.phase_parameter_1[filter_index][model_index] = parameter_1
self.phase_parameter_1_err[filter_index][model_index] = parameter_1_err
self.phase_parameter_2[filter_index][model_index] = parameter_2
self.phase_parameter_2_err[filter_index][model_index] = parameter_2_err
self.phase_parameters[filter_index][model_index] = parameters
self.phase_parameters_err[filter_index][model_index] = parameters_err
27 changes: 10 additions & 17 deletions tests/adler/dataclasses/test_AdlerData.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ def test_populate_phase_parameters():

test_object = AdlerData(["u", "g", "r"])

test_object.populate_phase_parameters("u", "model_1", 11.0, 12.0, 13, 14.0, 15.0, 16.0, 17.0, 18.0)
test_object.populate_phase_parameters("u", "model_1", 11.0, 12.0, 13, 14.0, 15.0, 16.0, [17.0], [18.0])
test_object.populate_phase_parameters(
"u", "model_2", 21.0, 22.0, 23, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0
"u", "model_2", 21.0, 22.0, 23, 24.0, 25.0, 26.0, [27.0, 27.5], [28.0, 28.5]
)
test_object.populate_phase_parameters("g", "model_1", 31.0, 32.0, 33, 34.0, 35.0, 36.0, 37.0, 38.0)
test_object.populate_phase_parameters("g", "model_1", 31.0, 32.0, 33, 34.0, 35.0, 36.0, [37.0], [38.0])
test_object.populate_phase_parameters(
"r", "model_2", 41.0, 42.0, 43, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0, 50.0
"r", "model_2", 41.0, 42.0, 43, 44.0, 45.0, 46.0, [47.0, 47.5], [48.0, 48.5]
)

assert test_object.filter_list == ["u", "g", "r"]
Expand All @@ -27,21 +27,14 @@ def test_populate_phase_parameters():
# test to make sure the object is correctly populated
assert test_object.H_adler == [[15.0, 25.0], [35.0], [45.0]]
assert test_object.H_err_adler == [[16.0, 26.0], [36.0], [46.0]]
assert test_object.phase_parameter_1 == [[17.0, 27.0], [37.0], [47.0]]
assert test_object.phase_parameter_1_err == [[18.0, 28.0], [38.0], [48.0]]
assert test_object.phase_parameter_2 == [[None, 29.0], [None], [49.0]]
assert test_object.phase_parameter_2_err == [[None, 30.0], [None], [50.0]]
assert test_object.phase_parameters == [[[17.0], [27.0, 27.5]], [[37.0]], [[47.0, 47.5]]]
assert test_object.phase_parameters_err == [[[18.0], [28.0, 28.5]], [[38.0]], [[48.0, 48.5]]]

# testing to make sure the correct error messages trigger
with pytest.raises(Exception) as error_info_1:
test_object.populate_phase_parameters(
"u", "model_1", 11.0, 12.0, 13, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0
)

assert (
error_info_1.value.args[0]
== "If using a model with 2 phase parameters, both parameter_2 and parameter_2_err must be supplied."
)
with pytest.raises(TypeError) as error_info_1:
test_object.populate_phase_parameters("u", "model_1", 11.0, 12.0, 13, 14.0, 15.0, 16.0, 17.0, 18.0)

assert error_info_1.value.args[0] == "Both parameters and parameters_err arguments must be lists."

with pytest.raises(Exception) as error_info_2:
test_object.populate_phase_parameters("y", "model_1", 11.0, 12.0, 13, 14.0, 15.0, 16.0, 17.0, 18.0)
Expand Down

0 comments on commit 76b19ba

Please sign in to comment.