kinextract.make_fit_state
- kinextract.make_fit_state(cfg: FitConfig, gal_file: str | None = None, gal_errors=None)[source]
Load a spectrum and assemble the fully-populated FitState for one fit.
This is the top-level orchestration entry point that turns a single galaxy/IFU-bin spectrum file into everything the LOSVD-recovery optimizer needs. In order, it:
Loads the spectrum and trims it to the fit window via load_spectrum_for_fit.
Flags bad/non-finite pixels and out-of-range pixels (~kinextract.io.setbadreg) by setting their error to
BIG.Applies emission-line pre-masking: known emission-line wavelengths via ~kinextract.masking.build_emission_line_mask, then segment-wise rolling-median detection of unlisted emission features via ~kinextract.masking._segment_emission_mask, then optionally grows/merges the combined emission mask by
cfg.mask_emission_grow_A.Reads and interpolates the stellar template library onto the fit-region wavelength grid (~kinextract.templates.build_template_matrix_fortran), masking pixels outside template coverage and estimating the pooled fractional template flux error used later for error propagation.
Builds the non-parametric LOSVD velocity grid (
st.xl), either directly fromcfg.losvd_vmin/cfg.losvd_vmaxor spanning±4.5 * cfg.sigl.Constructs the FitState object with all derived quantities (rebinning factors, continuum-polynomial coordinates, etc.).
Precomputes the LOSVD interpolation tables and instrument-profile map, and sets a unit continuum placeholder (
st.continuum_mult); whencfg.fit_continuumis True,kinextract.jointfits and overwrites this with the real co-fit continuum.
- Parameters:
cfg (FitConfig) – Fit configuration controlling every stage above (redshift, wavelength window, continuum-cofit/pre-normalized mode selection, template list, LOSVD grid parameters, masking thresholds, etc.).
gal_file (str, optional) – Path to the galaxy spectrum file, forwarded to load_spectrum_for_fit.
gal_errors (float or ndarray, optional) – Optional flux-error override, forwarded to load_spectrum_for_fit.
- Returns:
st (FitState) – Fully populated fit state, ready to be passed to the optimizer (LOSVD bins, template matrix, masks, continuum initialization, and precomputed interpolation tables all set).
tpl_files (list of str) – The template file paths used to build the template matrix, in the same order as the columns of
st.t. Whencfg.template_npz_fileis set, this is instead the packed template names (seekinextract.templates.load_packed_templates()), since there are no individual per-template file paths in that case.
- Raises:
ValueError – If fewer than 10 pixels remain in the fit region after loading and trimming.