Skip to content

Commit

Permalink
WIP ♻️ Rename activation to activations and use dict instead of a list
Browse files Browse the repository at this point in the history
Co-authored-by: s-weigand <[email protected]>
  • Loading branch information
jsnel and s-weigand committed Oct 13, 2024
1 parent 4598f79 commit a02b1e5
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 40 deletions.
10 changes: 5 additions & 5 deletions glotaran/builtin/elements/coherent_artifact/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ def calculate_matrix( # type:ignore[override]
if not 1 <= self.order <= 3:
raise GlotaranModelError("Coherent artifact order must be between in [1,3]")

activations = [
a
for a in model.activation
activations = {
key: a
for key,a in model.activations.items()
if isinstance(a, MultiGaussianActivation) and self.label in a.compartments
]
}

if not len(activations):
raise GlotaranModelError(
Expand All @@ -50,7 +50,7 @@ def calculate_matrix( # type:ignore[override]
raise GlotaranModelError(
f'Coherent artifact "{self.label}" must be associated with exactly one activation.'
)
activation = activations[0]
activation = next(iter(activations.values()))

parameters = activation.parameters(global_axis)

Expand Down
2 changes: 1 addition & 1 deletion glotaran/builtin/elements/damped_oscillation/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def calculate_matrix( # type:ignore[override]

clp_labels = []
matrices = []
for activation in model.activation:
for _, activation in model.activations.items():
oscillation_labels = [
label for label in self.oscillations if label in activation.compartments
]
Expand Down
4 changes: 2 additions & 2 deletions glotaran/builtin/elements/kinetic/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def calculate_matrix( # type:ignore[override]
) -> tuple[list[str], ArrayLike]:
compartments = self.compartments
matrices = []
for activation in model.activation:
for _,activation in model.activations.items():
initial_concentrations = np.array(
[float(activation.compartments.get(label, 0)) for label in compartments]
)
Expand Down Expand Up @@ -185,7 +185,7 @@ def create_result(
initial_concentrations = []
a_matrices = []
kinetic_amplitudes = []
for activation in model.activation:
for _,activation in model.activations.items():
initial_concentration = np.array(
[float(activation.compartments.get(label, 0)) for label in self.compartments]
)
Expand Down
10 changes: 5 additions & 5 deletions glotaran/builtin/items/activation/data_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def to_string(self) -> str:


def validate_activations(
value: list[Activation],
value: dict[str,Activation],
activation: Activation,
parameters: Parameters | None,
) -> list[ItemIssue]:
Expand All @@ -41,7 +41,7 @@ def validate_activations(


class ActivationDataModel(DataModel):
activation: list[Activation.get_annotated_type()] = Attribute( # type:ignore[valid-type]
activations: dict[str,Activation.get_annotated_type()] = Attribute( # type:ignore[valid-type]
validator=validate_activations,
description="The activation(s) of the dataset.",
)
Expand All @@ -54,9 +54,9 @@ def create_result(
amplitudes: xr.DataArray,
concentrations: xr.DataArray,
) -> dict[str, xr.DataArray]:
gaussian_activations = [
a for a in model.activation if isinstance(a, MultiGaussianActivation)
]
gaussian_activations = {
key:a for a in model.activations.items() if isinstance(a, MultiGaussianActivation)
}
if not len(gaussian_activations):
return {}

Expand Down
4 changes: 2 additions & 2 deletions glotaran/testing/simulated_data/parallel_spectral_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
KineticSpectrumDataModel(
elements=["parallel"],
global_elements=["spectral"],
activation=[GaussianActivation.model_validate(ACTIVATION)], # type:ignore[call-arg]
activations={"irf":GaussianActivation.model_validate(ACTIVATION)}, # type:ignore[call-arg]
),
ModelLibrary.from_dict(LIBRARY),
SIMULATION_PARAMETERS,
Expand All @@ -35,7 +35,7 @@
"datasets": {
"parallel-decay": {
"elements": ["parallel"],
"activation": [ACTIVATION],
"activations": {"irf":ACTIVATION},
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions glotaran/testing/simulated_data/sequential_spectral_decay.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
KineticSpectrumDataModel(
elements=["sequential"],
global_elements=["spectral"],
activation=[GaussianActivation.model_validate(ACTIVATION)], # type:ignore[call-arg]
activations={"irf":GaussianActivation.model_validate(ACTIVATION)}, # type:ignore[call-arg]
),
ModelLibrary.from_dict(LIBRARY),
SIMULATION_PARAMETERS,
Expand All @@ -35,7 +35,7 @@
"datasets": {
"sequential-decay": {
"elements": ["sequential"],
"activation": [ACTIVATION],
"activations": {"irf":ACTIVATION},
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
),
)
def test_coherent_artifact(activation: Activation):
data_model = ActivationDataModel(elements=["coherent-artifact"], activation=[activation])
data_model = ActivationDataModel(elements=["coherent-artifact"], activations={"irf":activation})
data_model.data = simulate(
data_model, test_library, test_parameters_simulation, test_axies, clp=test_clp
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
),
)
def test_coherent_artifact(activation: Activation):
data_model = ActivationDataModel(elements=["doas"], activation=[activation])
data_model = ActivationDataModel(elements=["does"], activations={"irf": activation})
data_model.data = simulate(
data_model, test_library, test_parameters_simulation, test_axies, clp=test_clp
)
Expand Down
2 changes: 1 addition & 1 deletion tests/builtin/elements/kinetic/test_kinetic_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_decay(decay_method: str, activation: Activation):
activation.compartments = {"s1": 1, "s2": 1}
else:
activation.compartments = {"s1": 1}
data_model = ActivationDataModel(elements=[decay_method], activation=[activation])
data_model = ActivationDataModel(elements=[decay_method], activations={"irf":activation})
data_model.data = simulate(
data_model, test_library, test_parameters_simulation, test_axies, clp=test_clp
)
Expand Down
10 changes: 5 additions & 5 deletions tests/builtin/elements/test_spectral_decay_full_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@
test_global_axis = np.arange(0, 50)
test_model_axis = np.arange(-10, 1500, 1)
test_axies = {"spectral": test_global_axis, "time": test_model_axis}
test_activation = [InstantActivation(type="instant", compartments={"s1": 1})]
test_activation = {"no_irf":InstantActivation(type="instant", compartments={"s1": 1})}
test_data_model_cls = DataModel.create_class_for_elements((KineticElement, SpectralElement))
test_data = simulate(
test_data_model_cls(
elements=["decay"],
global_elements=["spectral"],
activation=test_activation,
activations=test_activation,
),
test_library,
test_parameters_simulation,
Expand All @@ -102,7 +102,7 @@
ExperimentModel(
datasets={
"decay": ActivationDataModel(
elements=["decay"], data=test_data, activation=test_activation
elements=["decay"], data=test_data, activations=test_activation
),
},
),
Expand All @@ -117,7 +117,7 @@
elements=["decay"],
global_elements=["spectral"],
data=test_data,
activation=test_activation,
activations=test_activation,
),
},
),
Expand All @@ -127,7 +127,7 @@
elements=["spectral"],
global_elements=["decay"],
data=test_data,
activation=test_activation,
activations=test_activation,
),
},
),
Expand Down
16 changes: 8 additions & 8 deletions tests/builtin/elements/test_spectral_decay_linked_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,29 @@
(KineticElement, SpectralElement)
)

test_activation_1 = [
test_activation_1 = {"irf":
GaussianActivation(
type="gaussian",
compartments={"s1": 1, "s2": 0.75},
center="activation.center",
width="activation.width",
),
]
}

test_activation_2 = [
test_activation_2 = {"irf":
GaussianActivation(
type="gaussian",
compartments={"s1": 1, "s2": 0.1},
center="activation.center",
width="activation.width",
),
]
}

test_data_1 = simulate(
test_data_model_simulation_cls(
elements=["decay"],
global_elements=["spectral"],
activation=test_activation_1,
activations=test_activation_1,
),
test_library,
test_parameters_simulation,
Expand All @@ -110,7 +110,7 @@
test_data_model_simulation_cls(
elements=["decay"],
global_elements=["spectral"],
activation=test_activation_2,
activations=test_activation_2,
),
test_library,
test_parameters_simulation,
Expand All @@ -126,12 +126,12 @@
"decay_1": ActivationDataModel(
elements=["decay"],
data=test_data_1,
activation=test_activation_1,
activations=test_activation_1,
),
"decay_2": ActivationDataModel(
elements=["decay"],
data=test_data_2,
activation=test_activation_2,
activations=test_activation_2,
),
},
),
Expand Down
9 changes: 5 additions & 4 deletions tests/builtin/io/yml/test_load_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
datasets:
kinetic_parallel:
elements: [parallel]
activation:
- type: instant
compartments:
"s1": 1
activations:
irf:
type: instant
compartments:
"s1": 1
"""


Expand Down
4 changes: 2 additions & 2 deletions tests/project/test_scheme.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"datasets": {
"kinetic_parallel": {
"elements": ["parallel"],
"activation": [
"activations": {"irf":
{"type": "instant", "compartments": {"s1": 1}},
],
},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/test_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_create_model_scheme_json_schema(tmp_path: Path):
blank_schema = create_model_scheme_json_schema()

assert "properties" not in blank_schema["$defs"]["Parameter"]
assert "activation" in blank_schema["$defs"]["GlotaranDataModel"]["properties"]
assert "activations" in blank_schema["$defs"]["GlotaranDataModel"]["properties"]
assert create_model_scheme_json_schema() == create_model_scheme_json_schema()

params = Parameters.from_dict({"foo": {"bar": [1]}})
Expand Down

0 comments on commit a02b1e5

Please sign in to comment.