Skip to content

Commit

Permalink
tests: add test for base peak
Browse files Browse the repository at this point in the history
  • Loading branch information
David Wallace authored and MyPyDavid committed Mar 10, 2024
1 parent 8400ef4 commit 554760d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 34 deletions.
8 changes: 8 additions & 0 deletions src/raman_fitting/config/base_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from .default_models import load_config_from_toml_files
from .path_settings import create_default_package_dir_or_ask, InternalPathSettings
from types import MappingProxyType


def get_default_models_and_peaks_from_definitions():
Expand All @@ -36,5 +37,12 @@ class Settings(BaseSettings):
init_var=False,
validate_default=False,
)
default_definitions: MappingProxyType | None = Field(
default_factory=load_config_from_toml_files,
alias="my_default_definitions",
init_var=False,
validate_default=False,
)

destination_dir: Path = Field(default_factory=create_default_package_dir_or_ask)
internal_paths: InternalPathSettings = Field(default_factory=InternalPathSettings)
34 changes: 0 additions & 34 deletions src/raman_fitting/models/deconvolution/base_peak.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,37 +217,3 @@ def get_peaks_from_peak_definitions(
for peak_name, peak_def in peak_type_defs.items():
peak_models[peak_name] = BasePeak(**peak_def)
return peak_models


def _main():
model_definitions = load_config_from_toml_files()
print(model_definitions["first_order"]["models"])
peaks = {}
peak_items = {
**model_definitions["first_order"]["peaks"],
**model_definitions["second_order"]["peaks"],
}.items()
for k, v in peak_items:
peaks.update({k: BasePeak(**v)})

peak_d = BasePeak(**model_definitions["first_order"]["peaks"]["D"])
print(peak_d)
model_items = {
**model_definitions["first_order"]["models"],
**model_definitions["second_order"]["models"],
}.items()
models = {}
for model_name, model_comp in model_items:
print(k, v)
comps = model_comp.split("+")
peak_comps = [peaks[i] for i in comps]
lmfit_comp_model = sum(
map(lambda x: x.lmfit_model, peak_comps), peak_comps.pop().lmfit_model
)
models[model_name] = lmfit_comp_model
print(lmfit_comp_model)
# breakpoint()


if __name__ == "__main__":
_main()
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def example_files(internal_paths):
return example_files


@pytest.fixture(autouse=True)
def default_definitions(internal_paths):
return settings.default_definitions


@pytest.fixture(autouse=True)
def default_models(internal_paths):
return settings.default_models
Expand Down
34 changes: 34 additions & 0 deletions tests/models/test_base_peak.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from raman_fitting.models.deconvolution.base_peak import BasePeak


def test_initialize_base_peaks(
default_definitions, default_models_first_order, default_models_second_order
):
peaks = {}

peak_items = {
**default_definitions["first_order"]["peaks"],
**default_definitions["second_order"]["peaks"],
}.items()
for k, v in peak_items:
peaks.update({k: BasePeak(**v)})

peak_d = BasePeak(**default_definitions["first_order"]["peaks"]["D"])
assert (
peak_d.peak_name
== default_definitions["first_order"]["peaks"]["D"]["peak_name"]
)
assert (
peak_d.peak_type
== default_definitions["first_order"]["peaks"]["D"]["peak_type"]
)
assert (
peak_d.lmfit_model.components[0].prefix
== default_definitions["first_order"]["peaks"]["D"]["peak_name"] + "_"
)
assert (
peak_d.param_hints["center"].value
== default_definitions["first_order"]["peaks"]["D"]["param_hints"]["center"][
"value"
]
)

0 comments on commit 554760d

Please sign in to comment.