Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ repos:
- id: blacken-docs
additional_dependencies: [black==22.6.0]
- repo: https://github.com/PyCQA/isort.git
rev: 5.10.1
rev: v5.11.3
hooks:
- id: isort
additional_dependencies: [toml==0.10.2]
Expand Down
82 changes: 39 additions & 43 deletions spectrafit/plugins/test/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json

from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any

import pytest
Expand Down Expand Up @@ -49,55 +48,52 @@ def test_raise_no_guilty_ouput() -> None:
assert "The output file format 'illegal' is not supported." in str(excinfo.value)


def test_json_conversion() -> None:
def test_json_conversion(tmp_path: Path) -> None:
"""Test json to yaml conversion."""
with TemporaryDirectory() as tmpdir:
infile = Path(tmpdir) / "input_1.json"

with open(infile, "w", encoding="utf8") as f:
json.dump({"a": 1, "b": 2}, f)
args = {
"infile": infile,
"format": "yaml",
}
convert(args)
with open(infile.with_suffix(".yaml"), encoding="utf8") as f:
data = yaml.safe_load(f)
infile = tmp_path / "input_1.json"

assert data == {"a": 1, "b": 2}
with open(infile, "w", encoding="utf8") as f:
json.dump({"a": 1, "b": 2}, f)
args = {
"infile": infile,
"format": "yaml",
}
convert(args)
with open(infile.with_suffix(".yaml"), encoding="utf8") as f:
data = yaml.safe_load(f)

assert data == {"a": 1, "b": 2}

def test_yaml_conversion() -> None:

def test_yaml_conversion(tmp_path: Path) -> None:
"""Test yaml to json conversion."""
with TemporaryDirectory() as tmpdir:
infile = Path(tmpdir) / "input_1.yaml"

with open(infile, "w", encoding="utf8") as f:
yaml.dump({"a": 1, "b": 2}, f)
args = {
"infile": infile,
"format": "toml",
}
convert(args)
with open(infile.with_suffix(".toml"), "rb") as f:
data = tomli.load(f)
infile = tmp_path / "input_1.yaml"

assert data == {"a": 1, "b": 2}
with open(infile, "w", encoding="utf8") as f:
yaml.dump({"a": 1, "b": 2}, f)
args = {
"infile": infile,
"format": "toml",
}
convert(args)
with open(infile.with_suffix(".toml"), "rb") as f:
data = tomli.load(f)

assert data == {"a": 1, "b": 2}


def test_toml_conversion() -> None:
def test_toml_conversion(tmp_path: Path) -> None:
"""Test toml to json conversion."""
with TemporaryDirectory() as tmpdir:
infile = Path(tmpdir) / "input_1.toml"

with open(infile, "wb+") as f:
tomli_w.dump({"a": 1, "b": 2}, f)
args = {
"infile": infile,
"format": "json",
}
convert(args)
with open(infile.with_suffix(".json"), encoding="utf8") as f:
data = json.load(f)
infile = tmp_path / "input_1.toml"

with open(infile, "wb+") as f:
tomli_w.dump({"a": 1, "b": 2}, f)
args = {
"infile": infile,
"format": "json",
}
convert(args)
with open(infile.with_suffix(".json"), encoding="utf8") as f:
data = json.load(f)

assert data == {"a": 1, "b": 2}
assert data == {"a": 1, "b": 2}
91 changes: 44 additions & 47 deletions spectrafit/plugins/test/test_notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import sys

from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Any
from typing import Dict
from typing import List
Expand Down Expand Up @@ -136,38 +135,38 @@ def export_report_fixture(

@pytest.fixture(name="class_spectrafit")
def class_spectrafit_fixture(
dataframe_2: pd.DataFrame, initial_model: List[Dict[str, Dict[str, Dict[str, Any]]]]
dataframe_2: pd.DataFrame,
initial_model: List[Dict[str, Dict[str, Dict[str, Any]]]],
tmp_path: Path,
) -> Dict[Any, Any]:
"""Create a SpectraFitNotebook object."""
with TemporaryDirectory() as tmpdir:
_df = pd.DataFrame(data={"x": [1, 2, 3], "y": [1, 2, 3]})
sp = SpectraFitNotebook(
df=_df,
x_column="x",
y_column="y",
folder=str(tmpdir),
fname="test",
)
sp.df_fit = dataframe_2
sp.initial_model = initial_model
sp.df_pre = _df
sp.df_metric = _df
return {"sp": sp, "tmpdir": str(tmpdir)}
_df = pd.DataFrame(data={"x": [1, 2, 3], "y": [1, 2, 3]})
sp = SpectraFitNotebook(
df=_df,
x_column="x",
y_column="y",
folder=str(tmp_path),
fname="test",
)
sp.df_fit = dataframe_2
sp.initial_model = initial_model
sp.df_pre = _df
sp.df_metric = _df
return {"sp": sp, "tmpdir": tmp_path}


@pytest.fixture(name="class_spectrafit_fit")
def class_spectrafit_fixture_fit(
dataframe: pd.DataFrame, x_column: str, y_column: str
dataframe: pd.DataFrame, x_column: str, y_column: str, tmp_path: Path
) -> SpectraFitNotebook:
"""Create a SpectraFitNotebook object."""
with TemporaryDirectory() as tmpdir:
return SpectraFitNotebook(
df=dataframe,
x_column=x_column,
y_column=y_column,
fname="test",
folder=str(tmpdir),
)
return SpectraFitNotebook(
df=dataframe,
x_column=x_column,
y_column=y_column,
fname="test",
folder=str(tmp_path),
)


def test_dataframe_display(dataframe: pd.DataFrame) -> None:
Expand Down Expand Up @@ -245,31 +244,29 @@ def test_dataframe_display_all(dataframe: pd.DataFrame) -> None:
class TestExportResults:
"""Test of the Export Results class."""

def test_export_df(self, dataframe: pd.DataFrame) -> None:
def test_export_df(self, dataframe: pd.DataFrame, tmp_path: Path) -> None:
"""Test the export function."""
with TemporaryDirectory() as tmpdir:
fname = "test"
prefix = "test"
suffix = "csv"
folder = str(tmpdir)
ExportResults().export_df(
df=dataframe,
args=FnameAPI(fname=fname, prefix=prefix, suffix=suffix, folder=folder),
)
assert Path(folder).joinpath(f"{prefix}_{fname}.{suffix}").exists()
fname = "test"
prefix = "test"
suffix = "csv"
folder = str(tmp_path)
ExportResults().export_df(
df=dataframe,
args=FnameAPI(fname=fname, prefix=prefix, suffix=suffix, folder=folder),
)
assert Path(folder).joinpath(f"{prefix}_{fname}.{suffix}").exists()

def test_export_report(self, dataframe: pd.DataFrame) -> None:
def test_export_report(self, dataframe: pd.DataFrame, tmp_path: Path) -> None:
"""Test the export function."""
with TemporaryDirectory() as tmpdir:
fname = "test"
prefix = "test"
suffix = "lock"
folder = str(tmpdir)
ExportResults().export_report(
report=dataframe.to_dict(orient="list"),
args=FnameAPI(fname=fname, prefix=prefix, suffix=suffix, folder=folder),
)
assert Path(folder).joinpath(f"{prefix}_{fname}.{suffix}").exists()
fname = "test"
prefix = "test"
suffix = "lock"
folder = str(tmp_path)
ExportResults().export_report(
report=dataframe.to_dict(orient="list"),
args=FnameAPI(fname=fname, prefix=prefix, suffix=suffix, folder=folder),
)
assert Path(folder).joinpath(f"{prefix}_{fname}.{suffix}").exists()

def test_static_fname(self) -> None:
"""Test the static function of generating PathPosixs."""
Expand Down
2 changes: 1 addition & 1 deletion spectrafit/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ def print_confidence_interval(self) -> None:
self.minimizer, self.result, **self.args["conf_interval"]
)
)
except (MinimizerException, ValueError) as exc:
except (MinimizerException, ValueError, KeyError) as exc:
print(f"Error: {exc} -> No confidence interval could be calculated!")
self.args["confidence_interval"] = {}

Expand Down
Loading