diff --git a/spectrafit/test/test_models.py b/spectrafit/test/test_models.py index a88c7df76..52147e69f 100644 --- a/spectrafit/test/test_models.py +++ b/spectrafit/test/test_models.py @@ -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.""" @@ -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), } ) @@ -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, @@ -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, @@ -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, diff --git a/spectrafit/test/test_report.py b/spectrafit/test/test_report.py index af0f697e1..6e4cfb866 100644 --- a/spectrafit/test/test_report.py +++ b/spectrafit/test/test_report.py @@ -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), } ) ) diff --git a/spectrafit/test/test_tools.py b/spectrafit/test/test_tools.py index b4ccd6e72..e00a57f3c 100644 --- a/spectrafit/test/test_tools.py +++ b/spectrafit/test/test_tools.py @@ -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), } ) @@ -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), } ) diff --git a/tools/spectra_generator.py b/tools/spectra_generator.py index cea3f8550..ed8897867 100644 --- a/tools/spectra_generator.py +++ b/tools/spectra_generator.py @@ -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)