Esempio n. 1
0
def mock_scheme(tmp_path: Path) -> Scheme:

    model_yml_str = """
    megacomplex:
        m1:
            type: decay
            k_matrix: []
    dataset:
        dataset1:
            megacomplex: [m1]
    """
    model_path = tmp_path / "model.yml"
    model_path.write_text(model_yml_str)

    parameter_path = tmp_path / "parameters.yml"
    parameter_path.write_text("[1.0, 67.0]")

    dataset_path = tmp_path / "dataset.nc"
    xr.DataArray([[1, 2, 3]], coords=[
        ("spectral", [1]), ("time", [1, 2, 3])
    ]).to_dataset(name="data").to_netcdf(dataset_path)

    scheme_yml_str = f"""
    model: {model_path}
    parameters: {parameter_path}
    maximum_number_function_evaluations: 42
    data:
        dataset1: {dataset_path}
    """
    scheme_path = tmp_path / "scheme.yml"
    scheme_path.write_text(scheme_yml_str)

    return load_scheme(scheme_path)
Esempio n. 2
0
def test_scheme(tmpdir):

    model_path = tmpdir.join("model.yml")
    with open(model_path, "w") as f:
        model = "type: kinetic-spectrum\ndataset:\n  dataset1:\n    megacomplex: []"
        f.write(model)

    parameter_path = tmpdir.join("parameters.yml")
    with open(parameter_path, "w") as f:
        parameter = "[1.0, 67.0]"
        f.write(parameter)

    dataset_path = tmpdir.join("dataset.nc")
    xr.DataArray([[1, 2, 3]], coords=[
        ("e", [1]), ("c", [1, 2, 3])
    ]).to_dataset(name="data").to_netcdf(dataset_path)

    scheme = f"""
    model: {model_path}
    parameters: {parameter_path}
    non-negative-least-squares: True
    maximum-number-function-evaluations: 42
    data:
      dataset1: {dataset_path}

    saving:
        level: minimal
        data_filter: [a, b, c]
        data_format: csv
        parameter_format: yaml
        report: false
    """
    scheme_path = tmpdir.join("scheme.yml")
    with open(scheme_path, "w") as f:
        f.write(scheme)

    scheme = load_scheme(scheme_path)
    assert scheme.model is not None
    assert scheme.model.model_type == "kinetic-spectrum"

    assert scheme.parameters is not None
    assert scheme.parameters.get("1") == 1.0
    assert scheme.parameters.get("2") == 67.0

    assert scheme.non_negative_least_squares
    assert scheme.maximum_number_function_evaluations == 42

    assert "dataset1" in scheme.data
    assert scheme.data["dataset1"].data.shape == (1, 3)

    assert scheme.saving.level == "minimal"
    assert scheme.saving.data_filter == ["a", "b", "c"]
    assert scheme.saving.data_format == "csv"
    assert scheme.saving.parameter_format == "yaml"
    assert not scheme.saving.report
Esempio n. 3
0
    def from_yaml_file(filename: str) -> Scheme:
        """Create :class:`Scheme` from specs in yaml file.

        Warning
        -------
        Deprecated use ``glotaran.io.load_scheme(filename)`` instead.

        Parameters
        ----------
        filename : str
            Path to the spec file.

        Returns
        -------
        Scheme
            Analysis schmeme
        """
        return load_scheme(filename)
Esempio n. 4
0
def test_save_scheme(tmp_path: Path):
    save_model(MODEL, tmp_path / "m.yml")
    save_parameters(PARAMETERS, tmp_path / "p.csv")
    save_dataset(DATASET, tmp_path / "d.nc")
    scheme = Scheme(
        MODEL,
        PARAMETERS,
        {"dataset_1": DATASET},
    )
    scheme_path = tmp_path / "testscheme.yml"
    save_scheme(file_name=scheme_path, format_name="yml", scheme=scheme)

    assert scheme_path.is_file()
    assert scheme_path.read_text() == want
    loaded = load_scheme(scheme_path)
    print(loaded.model.validate(loaded.parameters))
    assert loaded.model.valid(loaded.parameters)
    assert isinstance(scheme.data["dataset_1"], xr.Dataset)
Esempio n. 5
0
def load_scheme_file(filename, verbose=False):
    return _load_file(filename,
                      lambda file: load_scheme(file, format_name="yml"),
                      "scheme", verbose)