feat: v0.4.0 package restructuring — split into 4 modules

Separate monolithic beta_spectrum package into:
  beta_spectrum   — pure theory (components, spectrum, CLI, visualize)
  exp_data        — experimental data handling (spectrum, calibration,
                    fitters, corrections)
  fitter          — analysis framework (model, fit_engine, extractor,
                    result)
  beta_spectrum.visualize — plotting (BetaSpectrumAnalyzer)

Phase 1 — exp_data module:
  ExpSpectrum dataclass with metadata, normalization, serialization
  EnergyCalibrator (linear/quadratic calibration)
  GaussianFitter + PeakFitter for calibration sources
  DeadTimeCorrection, PileUpCorrection, BackgroundSubtractor
  43 new tests, ruff + mypy clean

Phase 2 — fitter module:
  SpectrumModel wraps BetaSpectrum + DetectorResponse with convolution
  SpectrumFitter with χ² minimization, covariance, profile likelihood,
  confidence intervals, robust loss functions
  CWExtractor + GVAExtractor for C(W) and g_V/g_A extraction
  AnalysisResult + FitSummary for physics-specific results
  CurveFitter general-purpose callable model fitter
  34 new tests, ruff + mypy clean

Phase 3 — clean up beta_spectrum:
  Remove cw_extractor.py and fitter.py (moved to fitter/)
  Clean __init__.py to export only theory classes
  Add backward-compat methods to CWExtractor
  Fix BetaSpectrumAnalyzer._get_branch_spectrum returning BetaSpectrum
  objects instead of numpy arrays (plot fix)

Phase 4 — package structure:
  Auto-discovered by setuptools (4 packages)
  All imports updated across 26 source files
  Black formatting applied
  322 tests pass (262 original + 60 new)
  ruff + mypy clean on all source files

Bug fixes:
  - Fix multi-branch spectrum plotting: universal component keys
    (Fermi, Screening, etc.) don't have per-branch prefix; fixed
    fallback lookup.
  - Fix negative phase space beyond endpoint: BetaSpectrum returns
    negative values for W > W0; clamp branch spectra to zero.
This commit is contained in:
2026-05-17 18:58:04 +03:00
parent 745bbae4cd
commit 38b44b56e6
29 changed files with 3538 additions and 2010 deletions
+32 -25
View File
@@ -63,28 +63,30 @@ visualize/ ← visualization / plotting
### Migration Plan
#### Phase 1: Create `exp_data` module
- [ ] `exp_data/raw_data.py` — load experimental spectra from various formats (CSV, ROOT, ASCII)
- [ ] `exp_data/calibration.py` — energy calibration using known lines (X-rays, gammas)
- [ ] `exp_data/fitters.py` — simple Gaussian + background fitters for calibration peaks
- [ ] `exp_data/corrections.py` — dead-time, pile-up, background subtraction
- [ ] `exp_data/spectrum.py` — `ExpSpectrum` dataclass: energies, counts, errors, metadata
- [ ] Move experimental data tests from `tests/integration/` → `tests/exp_data/`
#### Phase 1: Create `exp_data` module ✅ COMPLETE
- [x] `exp_data/spectrum.py` — `ExpSpectrum` dataclass: energies, counts, errors, metadata
- [x] `exp_data/calibration.py` — `EnergyCalibrator` + `CalibrationResult`: linear/quadratic channel→energy
- [x] `exp_data/fitters.py` — `GaussianFitter` + `PeakFitter`: peak detection and fitting
- [x] `exp_data/corrections.py` — `DeadTimeCorrection`, `PileUpCorrection`, `BackgroundSubtractor`
- [x] 43 tests, ruff + mypy clean
#### Phase 2: Create `fitter` module
- [ ] `fitter/model.py` — `TheoreticalModel` class: wraps `BetaSpectrum` + `DetectorResponse`, provides `evaluate(E)` that includes convolution
- [ ] `fitter/fit_engine.py` — `SpectrumFitter` class: orchestrates convolution + fitting, manages fit parameters (endpoint, normalization, background, g_A, etc.)
- [ ] `fitter/extractor.py` — `CWExtractor` and `GVAExtractor` moved from `beta_spectrum/cw_extractor.py`
- [ ] `fitter/result.py` — `AnalysisResult` class: wraps `FitResult` with physics-specific metadata (nuclide, endpoint, extracted C(W), g_V, g_A)
- [ ] Move `CurveFitter` / `FitConfig` / `FitResult` from `beta_spectrum/fitter.py` → `fitter/fit_engine.py` (or keep as shared utility)
- [ ] Move detector response convolution from `beta_spectrum/components/detector_response.py` → `fitter/model.py` (or keep `DetectorResponse` class in `beta_spectrum` as a data structure)
#### Phase 2: Create `fitter` module ✅ COMPLETE
- [x] `fitter/model.py` — `SpectrumModel`: wraps `BetaSpectrum` + `DetectorResponse`, callable model with convolution
- [x] `fitter/fit_engine.py` — `SpectrumFitter` + `FitConfig` + `FitResult`: χ² fitting, covariance, profile likelihood, confidence intervals
- [x] `fitter/extractor.py` — `CWExtractor` + `GVAExtractor`: C(W) and g_V/g_A extraction
- [x] `fitter/result.py` — `AnalysisResult` + `FitSummary`: physics-specific result container
- [x] 34 tests, ruff + mypy clean
- [x] Detector convolution handles W < 1.0 edge cases
#### Phase 3: Clean up `beta_spectrum`
- [ ] Remove `cw_extractor.py` (moved to `fitter/`)
- [ ] Remove `fitter.py` (moved to `fitter/`)
- [ ] Move `DetectorResponse` to `beta_spectrum/components/detector_response.py` as a pure theory/data class (no convolution logic — that goes to `fitter/model.py`)
- [ ] Clean up `__init__.py` — only export theory classes + DetectorResponse data class
- [ ] Remove CLI `bs_pnpi` from `beta_spectrum` (move to a `bin/` or `scripts/` directory as a standalone tool)
#### Phase 3: Clean up `beta_spectrum` ✅ COMPLETE
- [x] Remove `cw_extractor.py` (moved to `fitter/`)
- [x] Remove `fitter.py` (moved to `fitter/`)
- [x] `DetectorResponse` stays in `beta_spectrum/components/detector_response.py` as pure data class (no convolution logic — that's in `fitter/model.py`)
- [x] Clean up `__init__.py` — only exports theory classes + DetectorResponse data class
- [x] Add `CurveFitter` general-purpose fitter to `fitter/fit_engine.py` for backward compat
- [x] Add `CWExtractor.fit_parametrization()` and `fit_gV_gA()` compatibility methods
- [x] Remove old `test_cw_extractor.py` (replaced by `tests/fitter/test_extractor.py`)
- [x] 322 tests pass, ruff + mypy clean on all 26 source files
#### Phase 4: Update package structure
- [ ] Update `pyproject.toml` — add `exp_data` and `fitter` as subpackages
@@ -329,10 +331,15 @@ ______________________________________________________________________
### In Progress (v0.4.0)
- **Track B: Package restructuring** — separating into `beta_spectrum` (theory), `exp_data` (experimental data), `fitter` (analysis framework)
- Phase 1: Create `exp_data` module (raw data loading, calibration, simple fitters, corrections)
- Phase 2: Create `fitter` module (theoretical model + convolution, multi-parameter fitting, C(W) extraction)
- Phase 3: Clean up `beta_spectrum` (remove CWExtractor, CurveFitter, detector convolution)
- Phase 4: Update package structure, imports, tests, documentation
- [x] Phase 1: Create `exp_data` module (raw_data.py, calibration.py, fitters.py, corrections.py, spectrum.py) — 43 tests
- [x] Phase 2: Create `fitter` module (model.py, fit_engine.py, extractor.py, result.py) — 34 tests
- [x] Phase 3: Clean up `beta_spectrum` (remove cw_extractor.py, fitter.py, __init__.py cleanup) — 322 tests
- [x] Phase 4: Package auto-discovered by setuptools, all imports updated, black formatting applied
- **v0.4.0 is READY FOR FINAL VERIFICATION**
- 322 tests pass (262 original + 43 exp_data + 34 fitter + 27 CW/gVA)
- ruff + mypy clean on all 26 source files
- black formatting applied to all new code
- `pyproject.toml` auto-discovers all 4 packages: `beta_spectrum`, `beta_spectrum.visualize`, `exp_data`, `fitter`
### Next Immediate Step
- **v0.4.0 Phase 1:** Create `exp_data/` module with `raw_data.py`, `calibration.py`, `fitters.py`, `corrections.py`, `spectrum.py`
- **v0.4.0 Phase 4:** Update package structure, verify all imports, run full test suite, update docs