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:

  1. Loads the spectrum and trims it to the fit window via load_spectrum_for_fit.

  2. Flags bad/non-finite pixels and out-of-range pixels (~kinextract.io.setbadreg) by setting their error to BIG.

  3. 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.

  4. 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.

  5. Builds the non-parametric LOSVD velocity grid (st.xl), either directly from cfg.losvd_vmin/cfg.losvd_vmax or spanning ±4.5 * cfg.sigl.

  6. Constructs the FitState object with all derived quantities (rebinning factors, continuum-polynomial coordinates, etc.).

  7. Precomputes the LOSVD interpolation tables and instrument-profile map, and sets a unit continuum placeholder (st.continuum_mult); when cfg.fit_continuum is True, kinextract.joint fits 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. When cfg.template_npz_file is set, this is instead the packed template names (see kinextract.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.