コード例 #1
0
    def save_parameters(
        self,
        parameters: ParameterGroup,
        file_name: str,
        *,
        as_optimized: bool = True,
        replace_infinfinity: bool = True,
    ) -> None:
        """Save a :class:`ParameterGroup` to a TSV file.

        Parameters
        ----------
        parameters : ParameterGroup
            Parameters to be saved to file.
        file_name : str
            File to write the parameters to.
        as_optimized : bool
            Whether to include properties which are the result of optimization.
        replace_infinfinity : bool
            Weather to replace infinity values with empty strings.
        """
        save_parameters(
            parameters,
            file_name,
            format_name="csv",
            sep="\t",
            as_optimized=as_optimized,
            replace_infinfinity=replace_infinfinity,
        )
コード例 #2
0
def test_parameter_to_csv(tmpdir):
    csv_path = tmpdir.join("parameters.csv")
    params = load_parameters(
        """
    b:
        - ["1", 0.25, {vary: false, min: 0, max: 8}]
        - ["2", 0.75, {expr: '1 - $b.1', non-negative: true}]
    rates:
        - ["total", 2]
        - ["branch1", {expr: '$rates.total * $b.1'}]
        - ["branch2", {expr: '$rates.total * $b.2'}]
    """,
        format_name="yml_str",
    )

    save_parameters(csv_path, params, "csv")

    with open(csv_path) as f:
        print(f.read())
    params_from_csv = load_parameters(csv_path)

    for label, p in params.all():
        assert params_from_csv.has(label)
        p_from_csv = params_from_csv.get(label)
        assert p.label == p_from_csv.label
        assert p.value == p_from_csv.value
        assert p.minimum == p_from_csv.minimum
        assert p.maximum == p_from_csv.maximum
        assert p.vary == p_from_csv.vary
        assert p.non_negative == p_from_csv.non_negative
        assert p.expression == p_from_csv.expression
コード例 #3
0
def test_roundtrips(yaml_reference: ParameterGroup, tmp_path: Path,
                    format_name: str, reference_path: Path):
    """Roundtrip via save and load have the same data."""
    format_reference = load_parameters(reference_path)
    parameter_path = tmp_path / f"test_parameters.{format_name}"
    save_parameters(file_name=parameter_path,
                    format_name=format_name,
                    parameters=yaml_reference)
    parameters_roundtrip = load_parameters(parameter_path)

    assert parameters_roundtrip == yaml_reference
    assert parameters_roundtrip == format_reference

    if format_name in {"csv", "tsv"}:
        assert parameter_path.read_text() == reference_path.read_text()

        first_data_line = parameter_path.read_text().splitlines()[1]
        sep = "," if format_name == "csv" else "\t"

        assert f"{sep}-inf" not in first_data_line
        assert f"{sep}inf" not in first_data_line
    else:
        assert_frame_equal(
            pd.read_excel(parameter_path, na_values=["None", "none"]),
            pd.read_excel(reference_path, na_values=["None", "none"]),
        )
コード例 #4
0
def test_parameter_group_to_csv(tmpdir):
    csv_path = tmpdir.join("parameters.csv")
    params = load_parameters(
        """
    b:
        - ["1", 0.25, {vary: false, min: 0, max: 8}]
        - ["2", 0.75, {expr: '1 - $b.1', non-negative: true}]
    rates:
        - ["total", 2]
        - ["branch1", {expr: '$rates.total * $b.1'}]
        - ["branch2", {expr: '$rates.total * $b.2'}]
    """,
        format_name="yml_str",
    )
    for _, p in params.all():
        p.standard_error = 42

    save_parameters(params, csv_path, "csv")
    wanted = dedent(
        """\
        label,value,expression,minimum,maximum,non-negative,vary,standard-error
        b.1,0.25,None,0.0,8.0,False,False,42
        b.2,0.75,1 - $b.1,,,False,False,42
        rates.total,2.0,None,,,False,True,42
        rates.branch1,0.5,$rates.total * $b.1,,,False,False,42
        rates.branch2,1.5,$rates.total * $b.2,,,False,False,42
        """
    )

    with open(csv_path) as f:
        got = f.read()
        print(got)
        assert got == wanted
    params_from_csv = load_parameters(csv_path)

    for label, p in params.all():
        assert params_from_csv.has(label)
        p_from_csv = params_from_csv.get(label)
        assert p.label == p_from_csv.label
        assert p.value == p_from_csv.value
        assert p.minimum == p_from_csv.minimum
        assert p.maximum == p_from_csv.maximum
        assert p.vary == p_from_csv.vary
        assert p.non_negative == p_from_csv.non_negative
        assert p.expression == p_from_csv.expression
コード例 #5
0
def test_as_optimized_false(yaml_reference: ParameterGroup, tmp_path: Path,
                            format_name: str):
    """Column 'standard-error' is missing if as_optimized==False"""
    parameter_path = tmp_path / f"test_parameters.{format_name}"
    save_parameters(
        file_name=parameter_path,
        format_name=format_name,
        parameters=yaml_reference,
        as_optimized=False,
    )

    if format_name in {"csv", "tsv"}:
        assert "standard-error" not in parameter_path.read_text().splitlines(
        )[0]
    else:
        assert ("standard-error"
                not in pd.read_excel(parameter_path,
                                     na_values=["None", "none"]).columns)
コード例 #6
0
ファイル: test_save_scheme.py プロジェクト: jsnel/pyglotaran
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)
コード例 #7
0
def test_replace_infinfinity(yaml_reference: ParameterGroup, tmp_path: Path,
                             format_name: str, sep: str):
    """Column 'standard-error' is missing if as_optimized==False"""
    parameter_path = tmp_path / f"test_parameters.{format_name}"
    save_parameters(
        file_name=parameter_path,
        format_name=format_name,
        parameters=yaml_reference,
        replace_infinfinity=False,
    )
    df = pd.read_csv(parameter_path, sep=sep)
    assert all(df["minimum"] == -np.inf)
    assert all(df["maximum"] == np.inf)

    first_data_line = parameter_path.read_text().splitlines()[1]
    assert f"{sep}-inf" in first_data_line
    assert f"{sep}inf" in first_data_line

    assert load_parameters(parameter_path) == yaml_reference
コード例 #8
0
ファイル: parameter_group.py プロジェクト: jsnel/pyglotaran
    def to_csv(self, filename: str, delimiter: str = ",") -> None:
        """Save a :class:`ParameterGroup` to a CSV file.

        Warning
        -------
        Deprecated use
        ``glotaran.io.save_parameters(parameters, file_name=<parameters.csv>, format_name="csv")``
        instead.

        Parameters
        ----------
        filename : str
            File to write the parameter specs to.
        delimiter : str
            Character to separate columns., by default ","
        """
        save_parameters(
            self,
            file_name=filename,
            allow_overwrite=True,
            sep=delimiter,
            replace_infinfinity=False,
        )
コード例 #9
0
    def save_result(
        self,
        result: Result,
        result_path: str,
        *,
        saving_options: SavingOptions = SAVING_OPTIONS_DEFAULT,
        used_inside_of_plugin: bool = False,
    ) -> list[str]:
        """Save the result to a given folder.

        Returns a list with paths of all saved items.
        The following files are saved if not configured otherwise:
        * ``result.md``: The result with the model formatted as markdown text.
        * ``initial_parameters.csv``: Initially used parameters.
        * ``optimized_parameters.csv``: The optimized parameter as csv file.
        * ``parameter_history.csv``: Parameter changes over the optimization
        * ``{dataset_label}.nc``: The result data for each dataset as NetCDF file.

        Note
        ----
        As a side effect it populates the file path properties of ``result`` which can be
        used in other plugins (e.g. the ``yml`` save_result).

        Parameters
        ----------
        result : Result
            Result instance to be saved.
        result_path : str
            The path to the folder in which to save the result.
        saving_options : SavingOptions
            Options for saving the the result.
        used_inside_of_plugin: bool
            Denote that this plugin is used from inside another plugin,
            if false a user warning will be thrown. , by default False

        Returns
        -------
        list[str]
            List of file paths which were created.

        Raises
        ------
        ValueError
            If ``result_path`` is a file.
        """
        if used_inside_of_plugin is not True:
            warn(
                UserWarning(
                    "The folder plugin is only intended for internal use by other plugins "
                    "as quick way to save most of the files. The saved result will be incomplete, "
                    "thus it is not recommended to be used directly."
                )
            )

        result_folder = Path(result_path)
        if result_folder.is_file():
            raise ValueError(f"The path '{result_folder}' is not a directory.")
        result_folder.mkdir(parents=True, exist_ok=True)

        paths = []
        if saving_options.report:
            report_path = result_folder / "result.md"
            report_path.write_text(str(result.markdown()))
            paths.append(report_path.as_posix())

        initial_parameters_path = f"initial_parameters.{saving_options.parameter_format}"
        save_parameters(
            result.scheme.parameters,
            result_folder / initial_parameters_path,
            format_name=saving_options.parameter_format,
            allow_overwrite=True,
        )
        paths.append((result_folder / initial_parameters_path).as_posix())

        optimized_parameters_path = f"optimized_parameters.{saving_options.parameter_format}"
        save_parameters(
            result.optimized_parameters,
            result_folder / optimized_parameters_path,
            format_name=saving_options.parameter_format,
            allow_overwrite=True,
        )
        paths.append((result_folder / optimized_parameters_path).as_posix())

        parameter_history_path = result_folder / "parameter_history.csv"
        result.parameter_history.to_csv(parameter_history_path)
        paths.append(parameter_history_path.as_posix())

        for label, dataset in result.data.items():
            data_path = result_folder / f"{label}.{saving_options.data_format}"
            if saving_options.data_filter is not None:
                dataset = dataset[saving_options.data_filter]
            save_dataset(
                dataset,
                data_path,
                format_name=saving_options.data_format,
                allow_overwrite=True,
            )
            paths.append(data_path.as_posix())

        return paths