Exemplo n.º 1
0
    def save(self, path: str) -> list[str]:
        """Saves the result to given folder.

        Warning
        -------
        Deprecated use ``save_result(result_path=result_path, result=result,
        format_name="legacy", allow_overwrite=True)`` instead.


        Returns a list with paths of all saved items.
        The following files are saved:

        * `result.md`: The result with the model formatted as markdown text.

        * `optimized_parameters.csv`: The optimized parameter as csv file.

        * `{dataset_label}.nc`: The result data for each dataset as NetCDF file.

        Parameters
        ----------
        path :
            The path to the folder in which to save the result.
        """
        save_result(result_path=path,
                    result=self,
                    format_name="legacy",
                    allow_overwrite=True)
Exemplo n.º 2
0
def test_save_result_folder(
    tmp_path: Path,
    dummy_result: Result,
    format_name: Literal["folder", "legacy"],
):
    """Check all files exist."""

    result_dir = tmp_path / "testresult"
    assert not result_dir.exists()
    with pytest.warns(UserWarning) as record:
        save_paths = save_result(result_path=str(result_dir),
                                 format_name=format_name,
                                 result=dummy_result)

    assert len(record) == 1
    if format_name == "legacy":
        assert record[0].category == GlotaranApiDeprecationWarning
    else:
        assert record[0].category == UserWarning

    wanted_files = [
        "result.md",
        "initial_parameters.csv",
        "optimized_parameters.csv",
        "parameter_history.csv",
        "dataset_1.nc",
    ]
    if format_name == "legacy":
        wanted_files += ["scheme.yml", "model.yml", "result.yml"]
    for wanted in wanted_files:
        assert (result_dir / wanted).exists()
        assert (result_dir / wanted).as_posix() in save_paths
Exemplo n.º 3
0
    def save(
            self,
            path: StrOrPath,
            saving_options: SavingOptions = SAVING_OPTIONS_DEFAULT
    ) -> list[str]:
        """Save the result to given folder.

        Parameters
        ----------
        path : StrOrPath
            The path to the folder in which to save the result.
        saving_options : SavingOptions
            Options for the saved result.

        Returns
        -------
        list[str]
            Paths to all the saved files.
        """
        return cast(
            List[str],
            save_result(
                result_path=path,
                result=self,
                format_name="yaml",
                allow_overwrite=True,
                saving_options=saving_options,
            ),
        )
Exemplo n.º 4
0
def test_save_result_folder_error_path_is_file(
    tmpdir: TmpDir,
    dummy_result: Result,  # noqa: F811
    format_name: Literal["folder", "legacy"],
):
    """Raise error if result_path is a file without extension and overwrite is true."""

    result_dir = Path(tmpdir / "testresult")
    result_dir.touch()

    with pytest.raises(ValueError, match="The path '.+?' is not a directory."):
        save_result(
            result_path=str(result_dir),
            format_name=format_name,
            result=dummy_result,
            allow_overwrite=True,
        )
Exemplo n.º 5
0
def test_save_result_folder(
    tmpdir: TmpDir,
    dummy_result: Result,  # noqa: F811
    format_name: Literal["folder", "legacy"],
):
    """Check all files exist."""

    result_dir = Path(tmpdir / "testresult")
    save_result(result_path=str(result_dir),
                format_name=format_name,
                result=dummy_result)

    assert (result_dir / "result.md").exists()
    assert (result_dir / "optimized_parameters.csv").exists()
    assert (result_dir / "dataset1.nc").exists()
    assert (result_dir / "dataset2.nc").exists()
    assert (result_dir / "dataset3.nc").exists()
Exemplo n.º 6
0
    def save_result(
        self,
        result: Result,
        result_path: str,
        saving_options: SavingOptions = SAVING_OPTIONS_DEFAULT,
    ) -> list[str]:
        """Write a :class:`Result` instance to a spec file and data files.

        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.
        * ``result.yml``: Yaml spec file of the result
        * ``model.yml``: Model spec file.
        * ``scheme.yml``: Scheme spec file.
        * ``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.

        Parameters
        ----------
        result : Result
            :class:`Result` instance to write.
        result_path : str | PathLike[str]
            Path to write the result data to.
        saving_options : SavingOptions
            Options for saving the the result.

        Returns
        -------
        list[str]
            List of file paths which were created.
        """
        result_folder = Path(result_path).parent
        paths = save_result(
            result,
            result_folder,
            format_name="folder",
            saving_options=saving_options,
            allow_overwrite=True,
            used_inside_of_plugin=True,
        )

        model_path = result_folder / "model.yml"
        save_model(result.scheme.model, model_path, allow_overwrite=True)
        paths.append(model_path.as_posix())

        scheme_path = result_folder / "scheme.yml"
        save_scheme(result.scheme, scheme_path, allow_overwrite=True)
        paths.append(scheme_path.as_posix())

        result_dict = asdict(result, folder=result_folder)
        write_dict(result_dict, file_name=result_path)
        paths.append(result_path)

        return paths
Exemplo n.º 7
0
def test_save_result_yml(
        tmpdir: TmpDir,
        dummy_result: Result,  # noqa: F811
):
    """Check all files exist."""

    result_dir = Path(tmpdir / "testresult")
    save_result(result_path=str(result_dir),
                format_name="yml",
                result=dummy_result)

    assert (result_dir / "result.md").exists()
    assert (result_dir / "scheme.yml").exists()
    assert (result_dir / "result.yml").exists()
    assert (result_dir / "initial_parameters.csv").exists()
    assert (result_dir / "optimized_parameters.csv").exists()
    assert (result_dir / "dataset1.nc").exists()
    assert (result_dir / "dataset2.nc").exists()
    assert (result_dir / "dataset3.nc").exists()
Exemplo n.º 8
0
def test_save_result_folder_error_path_is_file(
    tmp_path: Path,
    dummy_result: Result,
    format_name: Literal["folder", "legacy"],
):
    """Raise error if result_path is a file without extension and overwrite is true."""

    result_dir = tmp_path / "testresulterror"
    result_dir.touch()

    with pytest.warns(UserWarning):
        with pytest.raises(ValueError,
                           match="The path '.+?' is not a directory."):
            save_result(
                result_path=str(result_dir),
                format_name=format_name,
                result=dummy_result,
                allow_overwrite=True,
            )
Exemplo n.º 9
0
    def save_result(
        self,
        result: Result,
        result_path: str,
        *,
        saving_options: SavingOptions = SAVING_OPTIONS_DEFAULT,
    ) -> list[str]:
        """Save the result to a given folder.

        Warning
        -------
        Deprecated use ``glotaran.io.save_result(result, Path(result_path) / 'result.yml')``
        instead.

        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.
        * ``result.yml``: Yaml spec file of the result
        * ``model.yml``: Model spec file.
        * ``scheme.yml``: Scheme spec file.
        * ``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.

        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.

        Returns
        -------
        list[str]
            List of file paths which were created.
        """
        warn_deprecated(
            deprecated_qual_name_usage=(
                "glotaran.io.save_result(result, result_path, format_name='legacy')"
            ),
            new_qual_name_usage=(
                "glotaran.io.save_result(result, Path(result_path) / 'result.yml')"
            ),
            to_be_removed_in_version="0.8.0",
        )

        return save_result(
            result=result,
            result_path=Path(result_path) / "result.yml",
            saving_options=saving_options,
            allow_overwrite=True,
        )
Exemplo n.º 10
0
def test_save_result_yml(
    tmp_path: Path,
    dummy_result: Result,
):
    """Check all files exist."""
    expected = dedent(
        f"""\
        number_of_function_evaluations: 1
        success: true
        termination_reason: The maximum number of function evaluations is exceeded.
        glotaran_version: {__version__}
        free_parameter_labels:
          - rates.species_1
          - rates.species_2
          - rates.species_3
          - irf.center
          - irf.width
        scheme: scheme.yml
        initial_parameters: initial_parameters.csv
        optimized_parameters: optimized_parameters.csv
        parameter_history: parameter_history.csv
        data:
          dataset_1: dataset_1.nc
        """
    )

    result_dir = tmp_path / "testresult"
    save_result(result_path=result_dir / "result.yml", result=dummy_result)

    assert (result_dir / "result.md").exists()
    assert (result_dir / "scheme.yml").exists()
    assert (result_dir / "result.yml").exists()
    assert (result_dir / "initial_parameters.csv").exists()
    assert (result_dir / "optimized_parameters.csv").exists()
    assert (result_dir / "dataset_1.nc").exists()

    # We can't check equality due to numerical fluctuations
    got = (result_dir / "result.yml").read_text()
    print(got)
    assert expected in got
Exemplo n.º 11
0
    optimization_method="TrustRegionReflection",
)

# %% Optimize the analysis scheme (and estimate parameters)
result = optimize(scheme)
print(result.markdown(True))
result2 = optimize(result.get_scheme())
print(result2.markdown(True))

# %% Basic print of results
print(result.markdown(True))

# %% Save the results
try:
    save_result(result_path=str(output_folder),
                result=result,
                format_name="folder",
                allow_overwrite=True)
except (ValueError, FileExistsError) as error:
    print(f"catching error: {error}")
    try:
        save_result(result_path=str(output_folder),
                    result=result,
                    format_name="yml",
                    allow_overwrite=True)
    except FileExistsError as error:
        print(f"catching error: {error}")
        timestamp = datetime.today().strftime("%y%m%d_%H%M")
        save_result(
            result_path=str(output_folder.joinpath(timestamp)),
            result=result,
            format_name="yml",
Exemplo n.º 12
0
def optimize_cmd(
    dataformat: str,
    data: typing.List[str],
    out: str,
    nfev: int,
    nnls: bool,
    yes: bool,
    parameters_file: str,
    model_file: str,
    scheme_file: str,
):
    """Optimizes a model.
    e.g.:
    glotaran optimize --

    """
    if scheme_file is not None:
        scheme = util.load_scheme_file(scheme_file, verbose=True)
        if nfev is not None:
            scheme.nfev = nfev
    else:
        if model_file is None:
            click.echo("Error: Neither scheme nor model specified", err=True)
            sys.exit(1)
        model = util.load_model_file(model_file, verbose=True)

        if parameters_file is None:
            click.echo("Error: Neither scheme nor parameter specified",
                       err=True)
            sys.exit(1)
        parameters = util.load_parameter_file(parameters_file, verbose=True)

        if len(data) == 0:
            click.echo("Error: Neither scheme nor data specified", err=True)
            sys.exit(1)
        dataset_files = {arg[0]: arg[1] for arg in data}
        datasets = {}
        for label in model.dataset:
            if label not in dataset_files:
                click.echo(f"Missing dataset for '{label}'", err=True)
                sys.exit(1)
            path = dataset_files[label]
            datasets[label] = util.load_dataset_file(path,
                                                     fmt=dataformat,
                                                     verbose=True)

        scheme = Scheme(
            model=model,
            parameters=parameters,
            data=datasets,
            non_negative_least_squares=nnls,
            maximum_number_function_evaluations=nfev,
        )

    click.echo(scheme.validate())
    click.echo(f"Use NNLS: {scheme.non_negative_least_squares}")
    click.echo(
        f"Max Nr Function Evaluations: {scheme.maximum_number_function_evaluations}"
    )
    click.echo(f"Saving directory: is '{out if out is not None else 'None'}'")

    if yes or click.confirm(
            "Do you want to start optimization?", abort=True, default=True):
        #  try:
        #      click.echo('Preparing optimization...', nl=False)
        #      optimizer = gta.analysis.optimizer.Optimizer(scheme)
        #      click.echo(' Success')
        #  except Exception as e:
        #      click.echo(" Error")
        #      click.echo(e, err=True)
        #      sys.exit(1)
        try:
            click.echo("Optimizing...")
            result = optimize(scheme)
            click.echo("Optimization done.")
            click.echo(result.markdown(with_model=False))
            click.echo("Optimized Parameter:")
            click.echo(result.optimized_parameters.markdown())
        except Exception as e:
            click.echo(f"An error occurred during optimization: \n\n{e}",
                       err=True)
            sys.exit(1)

        if out is not None:
            try:
                click.echo(f"Saving directory is '{out}'")
                if yes or click.confirm("Do you want to save the data?",
                                        default=True):
                    save_result(result_path=out,
                                format_name="yml",
                                result=result)
                    click.echo("File saving successful.")
            except Exception as e:
                click.echo(f"An error occurred during saving: \n\n{e}",
                           err=True)
                sys.exit(1)

        click.echo("All done, have a nice day!")
Exemplo n.º 13
0
    result2 = optimize(result.get_scheme())
    return result, result2


if __name__ == "__main__":
    start_pt = time.process_time()
    start_wt = time.time()
    model, parameters, dataset1, dataset2 = setup()
    print(
        f"   Setup: pt={time.process_time() - start_pt}s wt={time.time() - start_wt}s"
    )
    result, result2 = optimize_benchmark(model, parameters, dataset1, dataset2)
    print(
        f"Optimize: pt={time.process_time() - start_pt}s wt={time.time() - start_wt}s"
    )
    save_result(
        result=result,
        result_path=str(output_folder.joinpath("result1")),
        format_name="yml",
        allow_overwrite=True,
    )
    save_result(
        result=result2,
        result_path=str(output_folder.joinpath("result2")),
        format_name="yml",
        allow_overwrite=True,
    )
    print(
        f"    Save: pt={time.process_time() - start_pt}s wt={time.time() - start_wt}s"
    )