Skip to content

Commit

Permalink
refactor: 💡 Add sonartypes recommendations (#1138)
Browse files Browse the repository at this point in the history
* refactor: ♻️ Update regression metrics in PPTXLayoutAPI

* refactor: ♻️ Update model_dump() method calls

* test: ✅ Replace `random.rand` to `random.default_rng`
  • Loading branch information
Anselmoo authored Jan 21, 2024
1 parent 83ab4d1 commit 56eb268
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 53 deletions.
4 changes: 2 additions & 2 deletions spectrafit/api/pptx_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ def __init__(self, format: str, data: PPTXDataAPI) -> None:
self.tmp_fname = self.tmp_plot(pd.DataFrame(data.output.df_fit))
self.title = data.input.description.project_name
self.df_gof = pd.DataFrame({k: [v] for k, v in data.solver.goodness_of_fit})
self.df_regression = pd.DataFrame(**data.solver.regression_metrics.dict())
self.df_regression = pd.DataFrame(**data.solver.regression_metrics.model_dump())
self.df_variables = pd.DataFrame.from_dict(
data.solver.variables, orient="index", columns=None
data.solver.variables, orient="index"
)

def tmp_plot(self, df_fit: pd.DataFrame) -> Path:
Expand Down
2 changes: 1 addition & 1 deletion spectrafit/api/test/test_cmd_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_global_fit() -> None:
"""Test for global fitting settings of CMD Model."""
result = CMDModelAPI(infile="")
result.global_ = 1
assert result.dict()["global_"] == 1
assert result.model_dump()["global_"] == 1


def test_sha256() -> None:
Expand Down
8 changes: 5 additions & 3 deletions spectrafit/plugins/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ def update_layout_axes(
fig.update_layout(
title=args_plot.title,
legend_title=args_plot.legend_title,
legend=args_plot.legend.dict(),
font=args_plot.font.dict(),
legend=args_plot.legend.model_dump(),
font=args_plot.font.model_dump(),
showlegend=args_plot.show_legend,
width=args_plot.size[0],
height=height,
Expand Down Expand Up @@ -939,7 +939,9 @@ class refers to the `Pydantic`-Definition of the `SpectraFit` class.
@property
def pre_process(self) -> None:
"""Pre-processing class."""
self.df, _pre_statistic = PreProcessing(df=self.df, args=self.args_pre.dict())()
self.df, _pre_statistic = PreProcessing(
df=self.df, args=self.args_pre.model_dump()
)()
self.pre_statistic = _pre_statistic["data_statistic"]
self.df_pre = self.df.copy()

Expand Down
64 changes: 29 additions & 35 deletions spectrafit/test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ def assert_solver_models(mp: Tuple[Minimizer, Any]) -> None:
assert isinstance(mp, tuple)


@pytest.fixture
def random_df() -> pd.DataFrame:
"""Fixture for random dataframe."""
return pd.DataFrame(
{
"Energy": np.arange(100).astype(np.float64),
"Intensity_1": np.random.default_rng(101).random(100),
"Intensity_2": np.random.default_rng(102).random(100),
"Intensity_3": np.random.default_rng(103).random(100),
"Intensity_4": np.random.default_rng(104).random(100),
}
)


class TestConstants:
"""Test constants."""

Expand Down Expand Up @@ -250,16 +264,16 @@ class TestModelParametersSolver:
df = pd.DataFrame(
{
"Energy": np.arange(10).astype(np.float64),
"Intensity": np.random.rand(10),
"Intensity": np.random.default_rng(10).random(10),
}
)
df_global = pd.DataFrame(
{
"Energy": np.arange(10).astype(np.float64),
"Intensity_1": np.random.rand(10),
"Intensity_2": np.random.rand(10),
"Intensity_3": np.random.rand(10),
"Intensity_4": np.random.rand(10),
"Intensity_1": np.random.default_rng(1).random(10),
"Intensity_2": np.random.default_rng(2).random(10),
"Intensity_3": np.random.default_rng(3).random(10),
"Intensity_4": np.random.default_rng(4).random(10),
}
)

Expand Down Expand Up @@ -584,17 +598,11 @@ def args_setting(self) -> Dict[str, Any]:
},
}

def test_all_model_local(self, args_setting: Dict[str, Any]) -> None:
def test_all_model_local(
self, random_df: pd.DataFrame, args_setting: Dict[str, Any]
) -> None:
"""Test of the AllModel class for local fitting."""
df = pd.DataFrame(
{
"Energy": np.arange(100).astype(np.float64),
"Intensity_1": np.random.rand(100),
"Intensity_2": np.random.rand(100),
"Intensity_3": np.random.rand(100),
"Intensity_4": np.random.rand(100),
}
)
df = random_df
args = {
"autopeak": False,
"global_": 0,
Expand All @@ -604,17 +612,11 @@ def test_all_model_local(self, args_setting: Dict[str, Any]) -> None:
mp = SolverModels(df=df, args=args)()
assert_solver_models(mp)

def test_all_model_global(self, args_setting: Dict[str, Any]) -> None:
def test_all_model_global(
self, random_df: pd.DataFrame, args_setting: Dict[str, Any]
) -> None:
"""Test of the AllModel class for global fitting."""
df = pd.DataFrame(
{
"Energy": np.arange(100).astype(np.float64),
"Intensity_1": np.random.rand(100),
"Intensity_2": np.random.rand(100),
"Intensity_3": np.random.rand(100),
"Intensity_4": np.random.rand(100),
}
)
df = random_df
args = {
"autopeak": False,
"global_": 1,
Expand All @@ -625,17 +627,9 @@ def test_all_model_global(self, args_setting: Dict[str, Any]) -> None:
mp = SolverModels(df=df, args=args)()
assert_solver_models(mp)

def test_all_model_global_fail(self) -> None:
def test_all_model_global_fail(self, random_df: pd.DataFrame) -> None:
"""Test of the AllModel class for global fitting."""
df = pd.DataFrame(
{
"Energy": np.arange(100).astype(np.float64),
"Intensity_1": np.random.rand(100),
"Intensity_2": np.random.rand(100),
"Intensity_3": np.random.rand(100),
"Intensity_4": np.random.rand(100),
}
)
df = random_df
args = {
"autopeak": True,
"global_": 1,
Expand Down
6 changes: 3 additions & 3 deletions spectrafit/test/test_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def test_raise_error(self) -> None:
_ = RegressionMetrics(
pd.DataFrame(
{
"intensity_0": np.random.rand(10),
"intensity_1": np.random.rand(10),
"fit_0": np.random.rand(10),
"intensity_0": np.random.default_rng(0).normal(size=10),
"intensity_1": np.random.default_rng(1).normal(size=10),
"fit_0": np.random.default_rng(2).normal(size=10),
}
)
)
Expand Down
10 changes: 5 additions & 5 deletions spectrafit/test/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def df() -> pd.DataFrame:
return pd.DataFrame(
{
"energy": np.linspace(0, 10, 100),
"intensity": np.random.rand(100),
"intensity": np.random.default_rng(0).normal(size=100),
}
)

Expand All @@ -40,10 +40,10 @@ def df_large() -> pd.DataFrame:
return pd.DataFrame(
{
"Energy": np.arange(10).astype(np.float64),
"Intensity_1": np.random.rand(10),
"Intensity_2": np.random.rand(10),
"Intensity_3": np.random.rand(10),
"Intensity_4": np.random.rand(10),
"Intensity_1": np.random.default_rng(5).normal(size=10),
"Intensity_2": np.random.default_rng(6).normal(size=10),
"Intensity_3": np.random.default_rng(7).normal(size=10),
"Intensity_4": np.random.default_rng(8).normal(size=10),
}
)

Expand Down
8 changes: 4 additions & 4 deletions tools/spectra_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def double_peak(fname: str) -> None:
"energy": x,
"y_1": stats.norm.pdf(x, 1, sigma) + 0.75 * stats.norm.pdf(x, 5, sigma),
"y_2": stats.norm.pdf(x, 1, sigma)
+ np.random.rand(100) / 10.0
+ np.random.default_rng(11).normal(size=100) / 10.0
+ 0.75 * stats.norm.pdf(x, 5, sigma)
+ np.random.rand(100) / 10.0,
+ np.random.default_rng(22).normal(size=100) / 10.0,
"y_3": stats.norm.pdf(x, 1, sigma)
+ np.random.rand(100) / 100.0
+ np.random.default_rng(33).normal(size=100) / 100.0
+ 0.75 * stats.norm.pdf(x, 5, sigma)
+ np.random.rand(100) / 100.0,
+ np.random.default_rng(44).normal(size=100) / 100.0,
}
).to_csv(Path(fname), index=False)

Expand Down

0 comments on commit 56eb268

Please sign in to comment.