Skip to content

Commit

Permalink
Rename center_dispersion to irf_center_location in result
Browse files Browse the repository at this point in the history
The name irf_center_location in the results describes more clearly what the variable holds.
Deprecated irf model item center_dispersion  for center_dispersion_coefficients
Deprecated irf model item width_dispersion  for width_dispersion_coefficients
  • Loading branch information
jsnel committed Aug 26, 2021
1 parent 73d16d8 commit 8bc9c31
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 58 deletions.
4 changes: 2 additions & 2 deletions glotaran/builtin/io/yml/test/test_model_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def test_irf(model):
assert irf.width == want
want = [3] if i == 1 else [5, 6]
if i == 2:
assert irf.center_dispersion == want
assert irf.center_dispersion_coefficients == want
want = [7, 8]
assert irf.width_dispersion == want
assert irf.width_dispersion_coefficients == want
want = [9]
assert irf.scale == want
assert irf.normalize == (i == 1)
Expand Down
35 changes: 17 additions & 18 deletions glotaran/builtin/io/yml/test/test_model_spec.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
default-megacomplex: decay


dataset:
dataset1:
megacomplex: [cmplx1]
Expand All @@ -23,36 +22,36 @@ irf:
irf2:
type: spectral-gaussian
center: [1, 2]
width: [3,4]
width: [3, 4]
scale: [9]
normalize: false
backsweep: true
backsweep_period: 55
dispersion_center: 55
center_dispersion: [5,6]
width_dispersion: [7,8]
center_dispersion_coefficients: [5, 6]
width_dispersion_coefficients: [7, 8]
model_dispersion_with_wavenumber: true

initial_concentration:
inputD1:
compartments: [s1,s2,s3]
parameters: [1,2,3]
compartments: [s1, s2, s3]
parameters: [1, 2, 3]
inputD2:
compartments: [s1,s2,s3]
parameters: [1,2,3]
compartments: [s1, s2, s3]
parameters: [1, 2, 3]

# Convention matrix notation column = source, row = target compartment
# (2,1) means from 1 to 2
k_matrix:
km1:
matrix:
(s1, s1): '1'
(s2, s1): '2'
(s1, s2): '3'
(s3, s1): '4'
(s1, s3): '5'
(s4, s1): '6'
(s1, s4): '7'
(s1, s1): "1"
(s2, s1): "2"
(s1, s2): "3"
(s3, s1): "4"
(s1, s3): "5"
(s4, s1): "6"
(s1, s4): "7"

shape:
shape1:
Expand All @@ -63,9 +62,9 @@ shape:

megacomplex:
cmplx1:
k_matrix: [km1] # A megacomplex has one or more k-matrices
k_matrix: [km1] # A megacomplex has one or more k-matrices
cmplx2:
k_matrix: [km2]
k_matrix: [km2]
cmplx3:
type: "spectral"
shape:
Expand Down Expand Up @@ -97,7 +96,7 @@ relations:
- source: s1
target: s2
parameter: 8
interval: [[1,100], [2,200]]
interval: [[1, 100], [2, 200]]

weights:
- datasets: [d1, d2]
Expand Down
28 changes: 14 additions & 14 deletions glotaran/builtin/megacomplexes/decay/irf.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class IrfMultiGaussian:
one or more center of the irf as parameter indices
width:
one or more widths of the gaussian as parameter index
center_dispersion:
center_dispersion_coefficients:
polynomial coefficients for the dispersion of the
center as list of parameter indices. None for no dispersion.
width_dispersion:
width_dispersion_coefficients:
polynomial coefficients for the dispersion of the
width as parameter indices. None for no dispersion.
Expand Down Expand Up @@ -124,8 +124,8 @@ class IrfGaussian(IrfMultiGaussian):
@model_item(
properties={
"dispersion_center": {"type": Parameter, "allow_none": True},
"center_dispersion": {"type": List[Parameter], "default": []},
"width_dispersion": {"type": List[Parameter], "default": []},
"center_dispersion_coefficients": {"type": List[Parameter], "default": []},
"width_dispersion_coefficients": {"type": List[Parameter], "default": []},
"model_dispersion_with_wavenumber": {"type": bool, "default": False},
},
has_type=True,
Expand All @@ -149,12 +149,12 @@ class IrfSpectralMultiGaussian(IrfMultiGaussian):
one or more center of the irf as parameter indices
width:
one or more widths of the gaussian as parameter index
center_dispersion:
polynomial coefficients for the dispersion of the
center as list of parameter indices. None for no dispersion.
width_dispersion:
polynomial coefficients for the dispersion of the
width as parameter indices. None for no dispersion.
center_dispersion_coefficients:
list of parameters with polynomial coefficients describing
the dispersion of the irf center location. None for no dispersion.
width_dispersion_coefficients:
list of parameters with polynomial coefficients describing
the dispersion of the width of the irf. None for no dispersion.
"""

Expand All @@ -173,16 +173,16 @@ def parameter(self, global_index: int, global_axis: np.ndarray):
else (index - self.dispersion_center) / 100
)

if len(self.center_dispersion) != 0:
if len(self.center_dispersion_coefficients) != 0:
if self.dispersion_center is None:
raise ModelError(f"No dispersion center defined for irf '{self.label}'")
for i, disp in enumerate(self.center_dispersion):
for i, disp in enumerate(self.center_dispersion_coefficients):
centers += disp * np.power(dist, i + 1)

if len(self.width_dispersion) != 0:
if len(self.width_dispersion_coefficients) != 0:
if self.dispersion_center is None:
raise ModelError(f"No dispersion center defined for irf '{self.label}'")
for i, disp in enumerate(self.width_dispersion):
for i, disp in enumerate(self.width_dispersion_coefficients):
widths = widths + disp * np.power(dist, i + 1)

return centers, widths, scale, shift, backsweep, backsweep_period
Expand Down
44 changes: 24 additions & 20 deletions glotaran/builtin/megacomplexes/decay/test/test_spectral_irf.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
center: irf.center
width: irf.width
dispersion_center: irf.dispersion_center
center_dispersion: [irf.center_dispersion]
center_dispersion_coefficients: [irf.cdc1]
"""
MODEL_MULTI_IRF_DISPERSION = f"""\
{MODEL_BASE}
Expand All @@ -63,8 +63,8 @@
center: [irf.center]
width: [irf.width]
dispersion_center: irf.dispersion_center
center_dispersion: [irf.center_dispersion1, irf.center_dispersion2]
width_dispersion: [irf.width_dispersion]
center_dispersion_coefficients: [irf.cdc1, irf.cdc2]
width_dispersion_coefficients: [irf.wdc1]
"""

MODEL_MULTIPULSE_IRF_DISPERSION = f"""\
Expand All @@ -75,7 +75,7 @@
center: [irf.center1, irf.center2]
width: [irf.width]
dispersion_center: irf.dispersion_center
center_dispersion: [irf.center_dispersion1, irf.center_dispersion2, irf.center_dispersion3]
center_dispersion_coefficients: [irf.cdc1, irf.cdc2, irf.cdc3]
"""

PARAMETERS_BASE = """\
Expand All @@ -98,7 +98,7 @@
- ['center', 0.3]
- ['width', 0.1]
- ['dispersion_center', 400, {{'vary': False}}]
- ['center_dispersion', 0.5]
- ['cdc1', 0.5]
"""

# What is this?
Expand All @@ -108,9 +108,9 @@
- ["center", 0.3]
- ["width", 0.1]
- ["dispersion_center", 400, {{"vary": False}}]
- ["center_dispersion1", 0.1]
- ["center_dispersion2", 0.01]
- ["width_dispersion", 0.025]
- ["cdc1", 0.1]
- ["cdc2", 0.01]
- ["wdc1", 0.025]
"""

PARAMETERS_MULTIPULSE_IRF_DISPERSION = f"""\
Expand All @@ -120,9 +120,9 @@
- ["center2", 0.4]
- ['width', 0.1]
- ['dispersion_center', 400, {{'vary': False}}]
- ["center_dispersion1", 0.5]
- ["center_dispersion2", 0.1]
- ["center_dispersion3", -0.01]
- ["cdc1", 0.5]
- ["cdc2", 0.1]
- ["cdc3", -0.01]
"""


Expand All @@ -137,13 +137,15 @@ def _spectral_axis():
return np.linspace(300, 500, 3)


def _calculate_irf_position(index, center, dispersion_center=None, center_dispersion=None):
if center_dispersion is None:
center_dispersion = []
def _calculate_irf_position(
index, center, dispersion_center=None, center_dispersion_coefficients=None
):
if center_dispersion_coefficients is None:
center_dispersion_coefficients = []
if dispersion_center is not None:
distance = (index - dispersion_center) / 100
if dispersion_center is not None:
for i, coefficient in enumerate(center_dispersion):
for i, coefficient in enumerate(center_dispersion_coefficients):
center += coefficient * np.power(distance, i + 1)
return center

Expand Down Expand Up @@ -184,10 +186,10 @@ class MultiCenterIrfDispersion:
def test_spectral_irf(suite):

model = suite.model
assert model.valid()
assert model.valid(), model.validate()

parameters = suite.parameters
assert model.valid(parameters)
assert model.valid(parameters), model.validate(parameters)

sim_model = deepcopy(model)
sim_model.dataset["dataset1"].global_megacomplex = ["mc2"]
Expand Down Expand Up @@ -243,12 +245,14 @@ def test_spectral_irf(suite):
# calculated irf location
model_irf_center = suite.model.irf["irf1"].center
model_dispersion_center = suite.model.irf["irf1"].dispersion_center
model_center_dispersion = suite.model.irf["irf1"].center_dispersion
model_center_dispersion_coefficients = suite.model.irf[
"irf1"
].center_dispersion_coefficients
calc_irf_location_at_x = _calculate_irf_position(
x, model_irf_center, model_dispersion_center, model_center_dispersion
x, model_irf_center, model_dispersion_center, model_center_dispersion_coefficients
)
# fitted irf location
fitted_irf_loc_at_x = resultdata["center_dispersion"].sel(spectral=x)
fitted_irf_loc_at_x = resultdata["irf_center_location"].sel(spectral=x)
assert np.allclose(calc_irf_location_at_x, fitted_irf_loc_at_x)

assert "species_associated_spectra" in resultdata
Expand Down
7 changes: 3 additions & 4 deletions glotaran/builtin/megacomplexes/decay/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,9 @@ def retrieve_irf(dataset_model: DatasetModel, dataset: xr.Dataset, global_dimens
dataset["irf_center"] = ("irf_nr", center) if len(center) > 1 else center[0]
dataset["irf_width"] = ("irf_nr", width) if len(width) > 1 else width[0]
if isinstance(irf, IrfSpectralMultiGaussian) and irf.dispersion_center:
# TODO: rename center_dispersion to irf_center_location
dataset["center_dispersion"] = (
dataset["irf_center_location"] = (
("irf_nr", global_dimension),
irf.calculate_dispersion(dataset.coords["spectral"].values),
)
# TODO: deprecate and remove old _n naming for 'center_dispersion'
dataset["center_dispersion_1"] = dataset["center_dispersion"].sel(irf_nr=0)
# center_dispersion_1 for backwards compatibility (0.3-0.4.1)
dataset["center_dispersion_1"] = dataset["irf_center_location"].sel(irf_nr=0)
21 changes: 21 additions & 0 deletions glotaran/deprecation/modules/builtin_io_yml.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,24 @@ def model_spec_deprecations(spec: MutableMapping[Any, Any]) -> None:
swap_keys=("equal_area_penalties", "clp_area_penalties"),
stacklevel=load_model_stack_level,
)

if "irf" in spec:
for _, irf in spec["irf"].items():
deprecate_dict_entry(
dict_to_check=irf,
deprecated_usage="center_dispersion",
new_usage="center_dispersion_coefficients",
to_be_removed_in_version="0.7.0",
swap_keys=("center_dispersion", "center_dispersion_coefficients"),
stacklevel=load_model_stack_level,
)

for _, irf in spec["irf"].items():
deprecate_dict_entry(
dict_to_check=irf,
deprecated_usage="width_dispersion",
new_usage="width_dispersion_coefficients",
to_be_removed_in_version="0.7.0",
swap_keys=("width_dispersion", "width_dispersion_coefficients"),
stacklevel=load_model_stack_level,
)
28 changes: 28 additions & 0 deletions glotaran/deprecation/modules/test/test_builtin_io_yml.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,41 @@
"clp_area_penalties",
[{"type": "equal_area"}],
),
(
dedent(
"""
irf:
irf1:
center_dispersion: [cdc1]
"""
),
1,
"irf",
{"irf1": {"center_dispersion_coefficients": ["cdc1"]}},
),
(
dedent(
"""
irf:
irf1:
"width_dispersion": [wdc1]
"""
),
1,
"irf",
{"irf1": {"width_dispersion_coefficients": ["wdc1"]}},
),
),
ids=(
"type: kinetic-spectrum",
"type: spectrum",
"spectral_relations",
"spectral_constraints",
"equal_area_penalties",
"center_dispersion",
"width_dispersion",
),
)
def test_model_spec_deprecations(
Expand Down

0 comments on commit 8bc9c31

Please sign in to comment.