style: reformat assertion blocks in test files

This commit is contained in:
2026-09-29 13:06:22 +03:00
parent be3e288e0d
commit 687e05cbe2
2 changed files with 20 additions and 22 deletions
+16 -19
View File
@@ -82,9 +82,9 @@ def _test_positive_values(component, W, component_name="Component"):
f"W range: [{W_arr.min():.3f}, {W_arr.max():.3f}], " f"W range: [{W_arr.min():.3f}, {W_arr.max():.3f}], "
f"result range: [{np.nanmin(result):.6f}, {np.nanmax(result):.6f}]" f"result range: [{np.nanmin(result):.6f}, {np.nanmax(result):.6f}]"
) )
assert not np.any( assert not np.any(np.isinf(result)), (
np.isinf(result) f"{component_name}: Inf values found in output."
), f"{component_name}: Inf values found in output." )
assert np.all(result > 0), ( assert np.all(result > 0), (
f"{component_name}: non-positive values found. " f"{component_name}: non-positive values found. "
f"Min = {np.min(result):.10f} at W = {W_arr[np.argmin(result)][0]:.4f}" f"Min = {np.min(result):.10f} at W = {W_arr[np.argmin(result)][0]:.4f}"
@@ -108,21 +108,20 @@ def _test_non_negative_values(
W_arr = np.asarray(W, dtype=float) W_arr = np.asarray(W, dtype=float)
result = component(W_arr) result = component(W_arr)
assert not np.any( assert not np.any(np.isnan(result)), (
np.isnan(result) f"{component_name}: NaN values found in output."
), f"{component_name}: NaN values found in output." )
assert not np.any( assert not np.any(np.isinf(result)), (
np.isinf(result) f"{component_name}: Inf values found in output."
), f"{component_name}: Inf values found in output." )
if allow_zero: if allow_zero:
assert np.all(result >= 0), ( assert np.all(result >= 0), (
f"{component_name}: negative values found. " f"Min = {np.min(result):.10f}" f"{component_name}: negative values found. Min = {np.min(result):.10f}"
) )
else: else:
assert np.all(result > 0), ( assert np.all(result > 0), (
f"{component_name}: non-positive values found. " f"{component_name}: non-positive values found. Min = {np.min(result):.10f}"
f"Min = {np.min(result):.10f}"
) )
@@ -137,12 +136,11 @@ def _test_output_shape_preserved(component, W, component_name="Component"):
W_arr = np.asarray(W, dtype=float) W_arr = np.asarray(W, dtype=float)
result = component(W_arr) result = component(W_arr)
assert isinstance( assert isinstance(result, np.ndarray), (
result, np.ndarray f"{component_name}: output is {type(result)}, expected np.ndarray"
), f"{component_name}: output is {type(result)}, expected np.ndarray" )
assert result.shape == W_arr.shape, ( assert result.shape == W_arr.shape, (
f"{component_name}: shape mismatch. " f"{component_name}: shape mismatch. Input {W_arr.shape} → Output {result.shape}"
f"Input {W_arr.shape} → Output {result.shape}"
) )
@@ -217,8 +215,7 @@ def _test_output_is_ndarray(component, W, component_name="Component"):
result = component(W_arr) result = component(W_arr)
assert isinstance(result, np.ndarray), ( assert isinstance(result, np.ndarray), (
f"{component_name}: output type is {type(result).__name__}, " f"{component_name}: output type is {type(result).__name__}, expected np.ndarray"
f"expected np.ndarray"
) )
+4 -3
View File
@@ -179,6 +179,7 @@ class TestDeclarativeDetectorResponse:
detector = BetaSpectrum.create_detector_from_config(config) detector = BetaSpectrum.create_detector_from_config(config)
from beta_spectrum.components.detector_response import DetectorResponse from beta_spectrum.components.detector_response import DetectorResponse
assert isinstance(detector, DetectorResponse) assert isinstance(detector, DetectorResponse)
assert detector.n_channels == 512 assert detector.n_channels == 512
assert detector._mode == "analytical" assert detector._mode == "analytical"
@@ -278,9 +279,9 @@ class TestDeclarativeDetectorResponse:
assert convolved.shape == (256,) assert convolved.shape == (256,)
# Normalized spectrum should integrate to ~1 over channel energies # Normalized spectrum should integrate to ~1 over channel energies
integral = np.trapezoid(convolved, detector.channel_energies) integral = np.trapezoid(convolved, detector.channel_energies)
assert np.isclose( assert np.isclose(integral, 1.0, rtol=0.05), (
integral, 1.0, rtol=0.05 f"Integral should be ~1.0, got {integral}"
), f"Integral should be ~1.0, got {integral}" )
@pytest.mark.skip(reason="DetectorResponse not yet implemented") @pytest.mark.skip(reason="DetectorResponse not yet implemented")
def test_detector_range_extends_to_endpoint(self): def test_detector_range_extends_to_endpoint(self):