Skip to content

Commit

Permalink
test: ✅ Replace random.rand to random.default_rng
Browse files Browse the repository at this point in the history
  • Loading branch information
Anselmoo committed Jan 21, 2024
1 parent d061673 commit 0eb1694
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 47 deletions.
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 0eb1694

Please sign in to comment.