test: adapt existing suites to unconditional core, drop dead detector tests
This commit is contained in:
@@ -9,7 +9,7 @@ from fitter.model import SpectrumModel
|
||||
class TestFitConfig:
|
||||
"""Test FitConfig defaults."""
|
||||
|
||||
def test_defaults(self):
|
||||
def test_defaults(self) -> None:
|
||||
config = FitConfig()
|
||||
assert config.method == "trf"
|
||||
assert config.max_nfev == 1000
|
||||
@@ -19,11 +19,13 @@ class TestFitConfig:
|
||||
class TestSpectrumFitter:
|
||||
"""Test SpectrumFitter."""
|
||||
|
||||
def _make_fitter(self):
|
||||
def _make_fitter(self) -> "SpectrumFitter":
|
||||
"""Create a simple fitter with synthetic data."""
|
||||
|
||||
# Create a simple linear model
|
||||
def linear_model(energies_keV, norm, bg):
|
||||
def linear_model(
|
||||
energies_keV: np.ndarray, norm: float, bg: float
|
||||
) -> np.ndarray:
|
||||
return norm * (1.0 - energies_keV / 100.0) + bg
|
||||
|
||||
# Synthetic data
|
||||
@@ -33,7 +35,9 @@ class TestSpectrumFitter:
|
||||
|
||||
# Mock model
|
||||
class MockModel:
|
||||
def evaluate(self, energies_keV, norm, bg):
|
||||
def evaluate(
|
||||
self, energies_keV: np.ndarray, norm: float, bg: float
|
||||
) -> np.ndarray:
|
||||
return linear_model(energies_keV, norm, bg)
|
||||
|
||||
model = MockModel()
|
||||
@@ -42,7 +46,7 @@ class TestSpectrumFitter:
|
||||
)
|
||||
return fitter
|
||||
|
||||
def test_fit_basic(self):
|
||||
def test_fit_basic(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0], param_names=["norm", "bg"])
|
||||
|
||||
@@ -53,7 +57,7 @@ class TestSpectrumFitter:
|
||||
assert result.chi2_per_dof > 0
|
||||
assert len(result.parameters) == 2
|
||||
|
||||
def test_fit_with_bounds(self):
|
||||
def test_fit_with_bounds(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(
|
||||
x0=[10.0, 2.0],
|
||||
@@ -61,36 +65,36 @@ class TestSpectrumFitter:
|
||||
)
|
||||
assert result.success
|
||||
|
||||
def test_fit_returns_residuals(self):
|
||||
def test_fit_returns_residuals(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0])
|
||||
|
||||
assert len(result.residuals) == 200
|
||||
assert len(result.model_values) == 200
|
||||
|
||||
def test_parameters_property(self):
|
||||
def test_parameters_property(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
fitter.fit(x0=[10.0, 2.0])
|
||||
params = fitter.parameters
|
||||
assert len(params) == 2
|
||||
|
||||
def test_covariance_property(self):
|
||||
def test_covariance_property(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
fitter.fit(x0=[10.0, 2.0])
|
||||
cov = fitter.covariance
|
||||
assert cov.shape == (2, 2)
|
||||
|
||||
def test_chi2_per_dof(self):
|
||||
def test_chi2_per_dof(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0])
|
||||
assert result.chi2_per_dof == result.chi2 / result.n_free
|
||||
|
||||
def test_p_value(self):
|
||||
def test_p_value(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0])
|
||||
assert 0.0 <= result.p_value <= 1.0
|
||||
|
||||
def test_parameters_with_errors(self):
|
||||
def test_parameters_with_errors(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0])
|
||||
params = result.parameters_with_errors
|
||||
@@ -100,7 +104,7 @@ class TestSpectrumFitter:
|
||||
assert isinstance(err, float)
|
||||
assert err >= 0
|
||||
|
||||
def test_correlation_matrix(self):
|
||||
def test_correlation_matrix(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0])
|
||||
corr = result.correlation_matrix
|
||||
@@ -108,7 +112,7 @@ class TestSpectrumFitter:
|
||||
# Diagonal should be 1
|
||||
np.testing.assert_allclose(np.diag(corr), 1.0)
|
||||
|
||||
def test_summary(self):
|
||||
def test_summary(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0], param_names=["norm", "bg"])
|
||||
summary = result.summary(param_names=["norm", "bg"])
|
||||
@@ -116,7 +120,7 @@ class TestSpectrumFitter:
|
||||
assert "bg" in summary
|
||||
assert "Fit Results" in summary
|
||||
|
||||
def test_profile_likelihood(self):
|
||||
def test_profile_likelihood(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0])
|
||||
param_vals, chi2_vals, norm_vals = result.profile_likelihood(0, n_points=10)
|
||||
@@ -125,7 +129,7 @@ class TestSpectrumFitter:
|
||||
assert len(norm_vals) == 10
|
||||
assert np.min(norm_vals) >= 0 # Normalized χ² should be >= 0
|
||||
|
||||
def test_confidence_interval(self):
|
||||
def test_confidence_interval(self) -> None:
|
||||
fitter = self._make_fitter()
|
||||
result = fitter.fit(x0=[10.0, 2.0])
|
||||
ci = result.confidence_interval(0, confidence=0.95)
|
||||
@@ -136,7 +140,7 @@ class TestSpectrumFitter:
|
||||
class TestSpectrumFitterWithRealModel:
|
||||
"""Test SpectrumFitter with a real SpectrumModel."""
|
||||
|
||||
def test_fit_with_real_model(self):
|
||||
def test_fit_with_real_model(self) -> None:
|
||||
from beta_spectrum import BetaSpectrum, SpectrumConfig
|
||||
|
||||
config = SpectrumConfig(
|
||||
@@ -144,16 +148,12 @@ class TestSpectrumFitterWithRealModel:
|
||||
Z_daughter=44,
|
||||
A_number=99,
|
||||
endpoint_MeV=0.294,
|
||||
detector_model="gaussian",
|
||||
detector_sigma_a_keV=0.5,
|
||||
detector_n_channels=200,
|
||||
)
|
||||
spectrum = BetaSpectrum.from_config(config)
|
||||
detector = BetaSpectrum.create_detector_from_config(config)
|
||||
model = SpectrumModel(spectrum, detector)
|
||||
model = SpectrumModel(spectrum)
|
||||
|
||||
# Generate synthetic data from model
|
||||
energies_keV = detector.channel_energies
|
||||
# Generate synthetic data from the unconvolved model on a keV grid
|
||||
energies_keV = np.linspace(1.0, 293.0, 200)
|
||||
true_norm = 5.0
|
||||
true_bg = 0.1
|
||||
counts = model.evaluate(energies_keV, true_norm, true_bg)
|
||||
|
||||
Reference in New Issue
Block a user