Histogram Fitting#
Use massphoto.distfit.fit() for raw samples or precomputed histograms.
It returns massphoto.distfit.FitResult. Select the fitting engine with
method="hybrid" (default) or method="full".
- massphoto.distfit.fit(data: DataFrame | ndarray | tuple[ndarray, ndarray], bins: int | ndarray | None = None, *, initial_peaks: ArrayLike | None = None, initial_sigma: float | ArrayLike | None = None, initial_amps: float | ArrayLike | None = None, binwidth: float | None = None, method: Literal['hybrid'] = 'hybrid', min_prominence: float | None = None, max_peaks: int | None = None, include_background: bool = True, bounds_mu: list[tuple[float, float]] | None = None, bounds_sigma: list[tuple[float, float]] | None = None, verbose: int = 0) FitResult[source]#
- massphoto.distfit.fit(data: DataFrame | ndarray | tuple[ndarray, ndarray], bins: int | ndarray | None = None, *, initial_peaks: ArrayLike | None = None, initial_sigma: float | ArrayLike | None = None, initial_amps: float | ArrayLike | None = None, binwidth: float | None = None, method: Literal['full'], min_prominence: float | None = None, max_peaks: int | None = None, include_background: bool = True, weights: ndarray | None = None, bounds: tuple[ndarray, ndarray] | None = None, jacobian: bool = True, verbose: int = 0) FitResult
Convenience wrapper to call either hybrid or full fitting method.
If initial_peaks is not provided, automatically detects peaks and sets bounds.
Accepts various input formats:
data=samples: unbinned data with auto-binning
data=samples, bins=N: unbinned data with N bins
data=samples, bins=edges: unbinned data with custom edges
data=samples, binwidth=w: unbinned data with bin width w
data=(counts, edges): pre-computed histogram
- Parameters:
bins (number of bins or bin edges array)
initial_peaks (list of peak positions (mu values) or None for auto-detection)
initial_sigma (single sigma for all peaks, or list of sigmas (None for auto-estimation))
initial_amps (single amplitude for all peaks, or list of amplitudes (None for auto-estimation))
binwidth (bin width for automatic binning)
method ('hybrid' or 'full')
min_prominence (minimum peak prominence for auto-detection (None for auto-estimation))
max_peaks (maximum number of peaks to detect (None for unlimited))
include_background (whether to fit constant background)
**kwargs (passed to the selected fitting engine. The hybrid method accepts) –
bounds_mu,bounds_sigma, andverbose; the full method acceptsweights,bounds,jacobian, andverbose.
- Return type:
Notes
If initial_peaks is not provided, peaks will be automatically detected using find_peak_guesses. Use min_prominence and max_peaks to control detection sensitivity.
If initial_sigma is not provided, it will be estimated from the data using estimate_bandwidth_from_fwhm based on the highest peak’s FWHM. You can also provide a single sigma for all peaks or individual sigmas.
In most cases, the ‘hybrid’ method is recommended for better stability with overlapping peaks, while ‘full’ fits amplitudes as free parameters and allows to constrain them with bounds if needed.
Examples
>>> # Automatic peak detection with custom binning >>> result = fit(masses, binwidth=2.5, include_background=True)
>>> # Manual peak specification with automatic sigma/amplitude >>> result = fit(masses, initial_peaks=[100, 200, 300], bins=50)
>>> # Manual peak specification with shared sigma >>> result = fit(masses, initial_peaks=[100, 200], initial_sigma=10.0)
>>> # Full manual specification >>> result = fit(masses, initial_peaks=[100, 200], ... initial_sigma=[8.0, 12.0], initial_amps=[500, 300])
Typical output#
FitResult( peaks=DataFrame with columns: ['A', 'mu', 'sigma', 'A_err', 'mu_err', 'sigma_err'], background=..., # Fitted background value background_err=..., # Uncertainty of background counts=array([...]), # Histogram bin counts bin_edges=array([...]), # Histogram bin edges model_counts=array([...]), # Model-predicted counts per bin residuals=array([...]), # Difference: counts - model_counts cov=array([...]), # Covariance matrix (if available) metrics={...}, # Goodness-of-fit metrics warnings=[...], # List of warnings (if any) # Convenience attributes for plotting bin_centers=array([...]), # Bin centers for plotting bin_widths=..., # Bin width for plotting model_x=array([...]), # High-resolution x values for model plotting model_y=array([...]), # Model values at high-resolution x for plotting )
- class massphoto.distfit.FitResult(peaks, background, background_err, counts, bin_edges, model_counts, residuals, cov, metrics, warnings, bin_centers=None, bin_widths=None, model_x=None, model_y=None)[source]#
Bases:
objectComplete fitting result.
This dataclass stores all fit outputs (peaks, background, model, residuals, covariance, and summary metrics) as attributes.