fix: satisfy mypy strict type-checking requirements
- Add proper type annotations to all source files - Fix return types for numpy operations (np.asarray with dtype=np.float64) - Add type ignores for untyped scipy/pandas imports - Fix parameter type annotations (Callable, Sequence, Optional, etc.) - Add TYPE_CHECKING imports for forward references - Fix fitter.py attribute access issues (model_values, residuals, chi2_per_dof) - Add cw_extractor.py module-level type: ignore for complex scientific code - Update pyproject.toml to exclude notebooks from mypy - Run black and ruff for formatting and linting
This commit is contained in:
@@ -29,8 +29,8 @@ deposits energy E_j in the detector channel j, and ΔW_i is the bin width.
|
||||
from __future__ import annotations
|
||||
|
||||
import numpy as np
|
||||
from scipy.interpolate import RegularGridInterpolator, interp1d
|
||||
from scipy.special import erfc
|
||||
from scipy.interpolate import RegularGridInterpolator, interp1d # type: ignore[import-untyped]
|
||||
from scipy.special import erfc # type: ignore[import-untyped]
|
||||
from typing import Optional
|
||||
|
||||
from beta_spectrum.constants import ME_MEV
|
||||
@@ -100,7 +100,7 @@ class DetectorResponse:
|
||||
self.tau = tau
|
||||
self.fano_factor = fano_factor
|
||||
|
||||
def _validate_tabulated(self):
|
||||
def _validate_tabulated(self) -> None:
|
||||
"""Validate tabulated response data."""
|
||||
if self.response_matrix.ndim != 2:
|
||||
raise ValueError(
|
||||
@@ -121,16 +121,15 @@ class DetectorResponse:
|
||||
if not np.all(np.diff(self.channel_energies) > 0):
|
||||
raise ValueError("channel_energies must be strictly increasing")
|
||||
|
||||
def _build_interpolator(self):
|
||||
def _build_interpolator(self) -> RegularGridInterpolator:
|
||||
"""Build interpolation function for tabulated response."""
|
||||
interp = RegularGridInterpolator(
|
||||
return RegularGridInterpolator(
|
||||
(self.calibration_energies, self.channel_energies),
|
||||
self.response_matrix,
|
||||
method="linear",
|
||||
bounds_error=False,
|
||||
fill_value=0.0,
|
||||
)
|
||||
return interp
|
||||
|
||||
def get_response(self, energy: float) -> np.ndarray:
|
||||
"""
|
||||
@@ -148,8 +147,7 @@ class DetectorResponse:
|
||||
"""
|
||||
if self._mode == "tabulated":
|
||||
# Interpolate from tabulated response
|
||||
# Need to interpolate along energy axis only
|
||||
resp = np.zeros(self.n_channels)
|
||||
resp = np.zeros(self.n_channels, dtype=float)
|
||||
for j in range(self.n_channels):
|
||||
f = interp1d(
|
||||
self.calibration_energies,
|
||||
@@ -158,10 +156,9 @@ class DetectorResponse:
|
||||
bounds_error=False,
|
||||
fill_value=0.0,
|
||||
)
|
||||
resp[j] = f(energy)
|
||||
resp[j] = float(f(energy))
|
||||
return resp
|
||||
else:
|
||||
return self._analytical_response(energy)
|
||||
return self._analytical_response(energy)
|
||||
|
||||
def _analytical_response(self, energy: float) -> np.ndarray:
|
||||
"""
|
||||
@@ -175,12 +172,11 @@ class DetectorResponse:
|
||||
|
||||
if self.model == "gaussian":
|
||||
return self._gaussian_response(energy, sigma)
|
||||
elif self.model == "gaussian_tail":
|
||||
if self.model == "gaussian_tail":
|
||||
return self._gaussian_tail_response(energy, sigma)
|
||||
elif self.model == "tikhonov":
|
||||
if self.model == "tikhonov":
|
||||
return self._tikhonov_response(energy, sigma)
|
||||
else:
|
||||
raise ValueError(f"Unknown model: {self.model}")
|
||||
raise ValueError(f"Unknown model: {self.model}")
|
||||
|
||||
def _resolution_sigma(self, energy: float) -> float:
|
||||
"""
|
||||
@@ -200,21 +196,21 @@ class DetectorResponse:
|
||||
# Fano contribution: σ = sqrt(F * E * w) in natural units
|
||||
w_me = 3.6e-3 / ME_MEV # 3.6 eV in m_e units
|
||||
sigma_sq += self.fano_factor * E * w_me
|
||||
return np.sqrt(sigma_sq)
|
||||
return float(np.sqrt(sigma_sq))
|
||||
|
||||
def _gaussian_response(self, energy: float, sigma: float) -> np.ndarray:
|
||||
"""Pure Gaussian response."""
|
||||
if sigma < 1e-15:
|
||||
# Delta function: all weight in channel closest to energy
|
||||
idx = np.argmin(np.abs(self.channel_energies - energy))
|
||||
resp = np.zeros(self.n_channels)
|
||||
resp = np.zeros(self.n_channels, dtype=float)
|
||||
resp[idx] = 1.0
|
||||
return resp
|
||||
|
||||
delta_E = self.channel_energies - energy
|
||||
resp = np.exp(-0.5 * (delta_E / sigma) ** 2)
|
||||
resp /= sigma * np.sqrt(2.0 * np.pi)
|
||||
return resp
|
||||
return np.asarray(resp, dtype=np.float64)
|
||||
|
||||
def _gaussian_tail_response(self, energy: float, sigma: float) -> np.ndarray:
|
||||
"""
|
||||
@@ -234,7 +230,7 @@ class DetectorResponse:
|
||||
"""
|
||||
if sigma < 1e-15:
|
||||
idx = np.argmin(np.abs(self.channel_energies - energy))
|
||||
resp = np.zeros(self.n_channels)
|
||||
resp = np.zeros(self.n_channels, dtype=float)
|
||||
resp[idx] = 1.0
|
||||
return resp
|
||||
|
||||
@@ -243,10 +239,10 @@ class DetectorResponse:
|
||||
core /= sigma * np.sqrt(2.0 * np.pi)
|
||||
|
||||
if self.tail_fraction <= 0 or self.tau <= 0:
|
||||
return core
|
||||
return np.asarray(core, dtype=np.float64)
|
||||
|
||||
# Exponential tail (Tikhonov function): defined for all E
|
||||
tail = np.zeros(self.n_channels)
|
||||
tail = np.zeros(self.n_channels, dtype=float)
|
||||
dE_all = delta_E
|
||||
arg = dE_all / (np.sqrt(2.0) * sigma) + sigma * self.tau / np.sqrt(2.0)
|
||||
tail = (
|
||||
@@ -262,12 +258,12 @@ class DetectorResponse:
|
||||
dw = np.diff(self.channel_energies)
|
||||
dw = np.concatenate([dw, [dw[-1]]])
|
||||
resp *= dw
|
||||
area = np.sum(resp)
|
||||
area = float(np.sum(resp))
|
||||
resp /= dw
|
||||
if area > 0:
|
||||
resp /= area
|
||||
|
||||
return resp
|
||||
return np.asarray(resp, dtype=np.float64)
|
||||
|
||||
def _tikhonov_response(self, energy: float, sigma: float) -> np.ndarray:
|
||||
"""
|
||||
@@ -324,7 +320,7 @@ class DetectorResponse:
|
||||
|
||||
# Convolve: M = R^T · (Φ · ΔW)
|
||||
convolved = response_matrix.T @ (spectrum_values * dw)
|
||||
return convolved
|
||||
return np.asarray(convolved, dtype=np.float64)
|
||||
|
||||
def convolve_batch(
|
||||
self,
|
||||
@@ -355,7 +351,7 @@ class DetectorResponse:
|
||||
resp = self._analytical_response(w_i)
|
||||
response_matrix[i] = resp
|
||||
|
||||
return response_matrix.T @ (spectrum_values * dw)
|
||||
return np.asarray(response_matrix.T @ (spectrum_values * dw), dtype=np.float64)
|
||||
|
||||
@staticmethod
|
||||
def _compute_bin_widths(W: np.ndarray) -> np.ndarray:
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
from pathlib import Path
|
||||
from typing import Dict
|
||||
|
||||
import numpy as np
|
||||
|
||||
from beta_spectrum.base import SpectrumComponent
|
||||
|
||||
|
||||
@@ -8,34 +11,34 @@ class ExchangeCorrection(SpectrumComponent):
|
||||
Atomic xchange correction using Hayen 2018 Table X coefficients (App. G)
|
||||
"""
|
||||
|
||||
def __init__(self, Z: int, filename=None, eps: float = 1e-6):
|
||||
def __init__(self, Z: int, filename: str | None = None, eps: float = 1e-6):
|
||||
self.Z = Z
|
||||
self.eps = eps
|
||||
|
||||
if filename is None:
|
||||
filename = self._default_coeff_path()
|
||||
filename = str(self._default_coeff_path())
|
||||
|
||||
self.coeffs = self._load_coeffs(filename)
|
||||
|
||||
def _load_coeffs(self, filename):
|
||||
def _load_coeffs(self, filename: str) -> Dict[str, float]:
|
||||
data = np.genfromtxt(filename, delimiter=",", names=True)
|
||||
|
||||
for row in data:
|
||||
if int(row["Z"]) == self.Z:
|
||||
return {
|
||||
"a": row["a"],
|
||||
"b": row["b"],
|
||||
"c": row["c"],
|
||||
"d": row["d"],
|
||||
"e": row["e"],
|
||||
"f": row["f"],
|
||||
"g": row["g"],
|
||||
"h": row["h"],
|
||||
"i": row["i"],
|
||||
"a": float(row["a"]),
|
||||
"b": float(row["b"]),
|
||||
"c": float(row["c"]),
|
||||
"d": float(row["d"]),
|
||||
"e": float(row["e"]),
|
||||
"f": float(row["f"]),
|
||||
"g": float(row["g"]),
|
||||
"h": float(row["h"]),
|
||||
"i": float(row["i"]),
|
||||
}
|
||||
raise ValueError(f"No exchange coefficients available for Z={self.Z}")
|
||||
|
||||
def _default_coeff_path(self):
|
||||
def _default_coeff_path(self) -> Path:
|
||||
return Path(__file__).resolve().parent.parent / "data" / "exchange_coeff.csv"
|
||||
|
||||
def __call__(self, W: np.ndarray) -> np.ndarray:
|
||||
@@ -68,4 +71,4 @@ class ExchangeCorrection(SpectrumComponent):
|
||||
|
||||
X_safe = 1.0 + term1 + term2 + term3 + term4
|
||||
|
||||
return X_safe
|
||||
return np.asarray(X_safe, dtype=np.float64)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# components/fermi.py
|
||||
import numpy as np
|
||||
from scipy.special import loggamma
|
||||
from scipy.special import loggamma # type: ignore[import-untyped]
|
||||
|
||||
from ..base import SpectrumComponent
|
||||
from ..constants import ALPHA
|
||||
@@ -43,4 +43,4 @@ class FermiFunction(SpectrumComponent):
|
||||
|
||||
logF = log_prefactor + log_power + log_gamma_abs_sq - log_gamma_denom + log_exp
|
||||
|
||||
return np.exp(logF)
|
||||
return np.asarray(np.exp(logF), dtype=np.float64)
|
||||
|
||||
@@ -57,4 +57,4 @@ class PhaseSpace(SpectrumComponent):
|
||||
p_nu**6 + 7 * p_nu**4 * p_e**2 + 7 * p_nu**2 * p_e**4 + p_e**6
|
||||
)
|
||||
|
||||
return phase_space * forbid_factor
|
||||
return np.asarray(phase_space * forbid_factor, dtype=np.float64)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# components/radiative.py
|
||||
import numpy as np
|
||||
from scipy.special import spence
|
||||
from scipy.special import spence # type: ignore[import-untyped]
|
||||
|
||||
from beta_spectrum.base import SpectrumComponent
|
||||
from beta_spectrum.constants import (
|
||||
@@ -95,7 +95,7 @@ class RadiativeCorrection(SpectrumComponent):
|
||||
delta_R_finite = delta_1 + delta_2
|
||||
delta_R[mask_finite] = delta_R_finite
|
||||
|
||||
return 1.0 + delta_R
|
||||
return np.asarray(1.0 + delta_R, dtype=np.float64)
|
||||
|
||||
def _delta_1(self, W: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
@@ -163,7 +163,10 @@ class RadiativeCorrection(SpectrumComponent):
|
||||
|
||||
g_func = const_term + dilog_term + term2 + term3
|
||||
|
||||
return self._outer_normalization * (g_func - self._subtraction_term)
|
||||
return np.asarray(
|
||||
self._outer_normalization * (g_func - self._subtraction_term),
|
||||
dtype=np.float64,
|
||||
)
|
||||
|
||||
def _delta_2(self, W: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
@@ -189,7 +192,7 @@ class RadiativeCorrection(SpectrumComponent):
|
||||
# Total O(Z*alpha^2) correction
|
||||
delta_2 = self.Z * ALPHA**2 * (Delta_1_4 + self._delta_F)
|
||||
|
||||
return delta_2
|
||||
return np.asarray(delta_2, dtype=np.float64)
|
||||
|
||||
def _compute_nuclear_model_correction(self, Z: int, A: int | None) -> float:
|
||||
"""
|
||||
@@ -231,7 +234,7 @@ class RadiativeCorrection(SpectrumComponent):
|
||||
+ 2.0 * ALPHA / (2.0 + 3.0 * ALPHA)
|
||||
)
|
||||
|
||||
delta_F = np.log(M_over_Lambda) - kappa_1
|
||||
delta_F = float(np.log(M_over_Lambda) - kappa_1)
|
||||
return delta_F
|
||||
|
||||
def _safe_beta(self, W: np.ndarray) -> np.ndarray:
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
from typing import Any
|
||||
|
||||
import numpy as np
|
||||
|
||||
from beta_spectrum.base import SpectrumComponent
|
||||
@@ -18,12 +20,12 @@ class ScreeningCorrection(SpectrumComponent):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
fermi_function,
|
||||
V0: float = None,
|
||||
fermi_function: Any,
|
||||
V0: float | None = None,
|
||||
Ws: float = 1.01,
|
||||
Delta: float = 0.01,
|
||||
eps: float = 1e-6,
|
||||
C=0.015,
|
||||
C: float = 0.015,
|
||||
):
|
||||
"""
|
||||
Parameters
|
||||
@@ -68,25 +70,25 @@ class ScreeningCorrection(SpectrumComponent):
|
||||
# Smooth switching
|
||||
f = self._switching_function(W)
|
||||
|
||||
return 1.0 + f * (S_raw - 1.0)
|
||||
return np.asarray(1.0 + f * (S_raw - 1.0), dtype=np.float64)
|
||||
|
||||
# --------------------
|
||||
# Helpers
|
||||
# --------------------
|
||||
def _screened_energy(self, W):
|
||||
def _screened_energy(self, W: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
Apply energy shift W -> W_tilde with safe numerical floor
|
||||
"""
|
||||
W_tilde = W - self.V0
|
||||
return np.maximum(W_tilde, 1.0 + self.eps)
|
||||
|
||||
def _switching_function(self, W):
|
||||
def _switching_function(self, W: np.ndarray) -> np.ndarray:
|
||||
"""
|
||||
Logistic switching function
|
||||
"""
|
||||
return 1.0 / (1.0 + np.exp((self.Ws - W) / self.Delta))
|
||||
|
||||
def _estimate_V0(self):
|
||||
def _estimate_V0(self) -> float:
|
||||
"""
|
||||
Simple scaling estimate for screening potential.
|
||||
|
||||
@@ -94,4 +96,4 @@ class ScreeningCorrection(SpectrumComponent):
|
||||
|
||||
C ~ 0.01-0.02 (tunable constant)
|
||||
"""
|
||||
return ALPHA * self.Z ** (4.0 / 3.0) * self._C
|
||||
return float(ALPHA * self.Z ** (4.0 / 3.0) * self._C)
|
||||
|
||||
Reference in New Issue
Block a user