feat: experimental data calibration pipeline with ROOT I/O
- Fix chi2 calculation bug in calibration.py (remove incorrect coeffs[::-1] reversal in np.polyval) - Fix broken beta_spectrum/cli.py (remove orphaned detector response add_argument calls and unused imports) - Add ExpSpectrum.apply_calibration() and EnergyCalibrator.from_am241_spectrum() - Add LivetimeDetermination class for pulser-based livetime - Add SpectrumPipeline orchestration (read→subtract→calibrate→livetime→validate) - Add ROOT I/O with PyROOT primary + uproot fallback - Add bs_exp CLI with process, livetime, export subcommands - Add exp_calibration notebook with pre-calibration visualization - Add comprehensive tests (73 passing) - Add bs_exp console script and [root] extra to pyproject.toml - Remove obsolete detector_response module and unused notebooks
This commit is contained in:
+42
-1
@@ -8,7 +8,10 @@ and calibration information.
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
from typing import Optional
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from exp_data.calibration import CalibrationResult
|
||||
|
||||
import numpy as np
|
||||
|
||||
@@ -140,6 +143,44 @@ class ExpSpectrum:
|
||||
date=self.date,
|
||||
)
|
||||
|
||||
def apply_calibration(self, calibration: "CalibrationResult") -> ExpSpectrum:
|
||||
"""Return a new spectrum with energies recalibrated via the given CalibrationResult.
|
||||
|
||||
The calibration polynomial is applied to the current energy axis:
|
||||
calibrated_energies = calibration(channel_indices).
|
||||
|
||||
Parameters
|
||||
----------
|
||||
calibration : CalibrationResult
|
||||
A CalibrationResult from EnergyCalibrator (e.g., from Am-241 peak detection).
|
||||
|
||||
Returns
|
||||
-------
|
||||
ExpSpectrum
|
||||
New ExpSpectrum with recalibrated energies, all other fields preserved.
|
||||
"""
|
||||
|
||||
calibrated_energies = calibration(self.energies)
|
||||
return ExpSpectrum(
|
||||
energies=calibrated_energies,
|
||||
counts=self.counts.copy(),
|
||||
errors=self.errors.copy(),
|
||||
metadata={
|
||||
**self.metadata,
|
||||
"calibration": {
|
||||
"type": "linear" if calibration.order == 1 else "quadratic",
|
||||
"coefficients": calibration.coefficients.tolist(),
|
||||
"chi2_per_dof": calibration.chi2_per_dof,
|
||||
"n_points": calibration.n_points,
|
||||
},
|
||||
},
|
||||
dead_time=self.dead_time,
|
||||
live_time=self.live_time,
|
||||
source=self.source,
|
||||
run_id=self.run_id,
|
||||
date=self.date,
|
||||
)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Serialize to dictionary."""
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user