astrocook.recipes.absorbers module#
- astrocook.recipes.absorbers.MIN_B_ROUNDS = 2#
Rounds of “remove the Ly-a narrower than min_b, refit the rest” in lya_auto (#517, F21, agreed with Guido): the refit can narrow other lines below min_b, so the filter runs again after it, with one more refit; what is still below min_b after the last refit is removed without a refit. Before, the filter ran once, before the last refit, and 39 Ly-a with b < 10 km/s were left on J0103-1305
- astrocook.recipes.absorbers.merge_region_ids(old_ids, old_ident, new_ids, new_ident)[source]#
Merge the regions of a new identification with those of earlier steps of a pipeline.
The old regions are kept; the new ones, with their IDs shifted past the largest old ID, fill the pixels that no old region covers. The identifications follow the same IDs: the old ones are kept, the new ones get the shifted IDs, and only the regions left in the merged column are kept (F90).
- Returns:
The merged region IDs (array) and identifications (dict).
- Return type:
tuple
- astrocook.recipes.absorbers.FIT_ERRORS = (<class 'ValueError'>, <class 'ArithmeticError'>)#
The errors of a fit that does not converge or cannot be evaluated (ValueError covers np.linalg.LinAlgError and a start outside the bounds)
- astrocook.recipes.absorbers.AT_BOUND_RTOL = 0.001#
A fitted value within this fraction of its bound is at the bound
- astrocook.recipes.absorbers.NLIVE_PER_PARAMETER = 10#
The live points of nested sampling needed per free parameter (with fewer dynesty fails or does not end; phase 6-quater, PR 10-bis, Guido)
- astrocook.recipes.absorbers.NOT_FITTED = 'not_fitted'#
The flag of the components whose group refit_all could not fit (phase 6-bis, PR 4b): they keep their values from before the fit
- class astrocook.recipes.absorbers.RecipeAbsorbers(session_v2: Session)[source]#
Bases:
objectRecipes for identification, fitting, and management of absorption lines.
These methods are accessed via the
session.absorbersattribute and delegate logic toSystemList,Spectrum, and the fitting engine.- components_auto(multiplets: str = 'CIV,SiIV,MgII,Ly_ab', score_threshold: float = 0.5, merge_dv: float = 10.0, min_pix_region: int = 3, mask_col: str = 'abs_mask', z_window_kms: float = 20.0, region_limit: str = 'None', max_b: float = 50.0, max_logN: float = 19.0, mask: str = 'new', z_em_margin_kms: float = 3000.0) Session[source]#
Auto-detect and fit components.
The Master Pipeline: Identifies candidates (doublets or singles), populates them as systems, and refits the spectrum. Supports masking by region. Delegates to
identify_lines(),populate_from_identification(), andrefit_all().- Parameters:
multiplets (str, optional) – Comma-separated list of multiplets to search for. Defaults to
"CIV,SiIV,MgII,Ly_ab".score_threshold (float, optional) – Min R^2 score (0-1) to accept a candidate. Defaults to
0.5.merge_dv (float, optional) – Max velocity (km/s) to merge adjacent regions. Defaults to
10.0.min_pix_region (int, optional) – Minimum width in pixels to count as a region. Defaults to
3.mask_col (str, optional) – Name of the mask column (True=Absorbed). Defaults to
"abs_mask".z_window_kms (float, optional) – Velocity window for fitting (km/s). Defaults to
20.0.region_limit (str, optional) – Limit search to
'red_side','lya_forest', or'None'. Defaults to"None".max_b (float, optional) – Upper bound on b (km/s) in the fits. It keeps the components from broadening into their neighbours, which makes the fits of the forest slow and gives spurious broad lines; components that end at the bound may be broader and can be refitted with
refit_all()orfit_component(). Defaults to50.0.max_logN (float, optional) – Upper bound on logN in the fits. In a saturated trough that the candidates do not fill, a component can otherwise fill it with its damping wings and run away to logN 20-23, blacking out the model around it (findings register F62). With 19, the Lyman-limit systems stay within reach; damped systems are to be fitted by hand. Components that end at the bound can be refitted with
refit_all(). Defaults to19.0.mask (str, optional) – The mask of the absorbed pixels used to find the regions:
'new'(made again with the defaults offind_absorbed()) or'session'(theabs_maskalready in the session, e.g. fromestimate_autowith other parameters). Defaults to'new'(F50, phase 6-bis, PR 6b).z_em_margin_kms (float, optional) – How far beyond
z_ema candidate system can lie (km/s). Defaults to3000.0(F55, phase 6-bis, PR 6b).
- Returns:
A new
Sessionwith detected systems.- Return type:
- doublets_auto(multiplets: str = 'CIV,SiIV,MgII', score_threshold: float = 0.5, z_window_kms: float = 20.0, max_b: float = 50.0, max_logN: float = 19.0, mask: str = 'new', z_em_margin_kms: float = 3000.0) Session[source]#
Auto-fit metal doublets (Red Side).
Wrapper for
components_auto()targeted at the red side (z < z_em). Finds CIV, SiIV, MgII.- Parameters:
multiplets (str, optional) – Multiplets to search for. Defaults to
"CIV,SiIV,MgII".score_threshold (float, optional) – Min R^2 score (0-1). Defaults to
0.5.z_window_kms (float, optional) – Fit window (km/s). Defaults to
20.0.max_b (float, optional) – Upper bound on b (km/s) in the fits, see
components_auto(). Defaults to50.0.max_logN (float, optional) – Upper bound on logN in the fits, see
components_auto(). Defaults to19.0.mask (str, optional) – The mask of the absorbed pixels used to find the regions:
'new'(made again with the defaults offind_absorbed()) or'session'(theabs_maskalready in the session, e.g. fromestimate_autowith other parameters). Defaults to'new'(F50, phase 6-bis, PR 6b).z_em_margin_kms (float, optional) – How far beyond
z_ema candidate system can lie (km/s). Defaults to3000.0(F55, phase 6-bis, PR 6b).
- Returns:
A new
Sessionwith detected doublets.- Return type:
- lya_auto(score_threshold: float = 0.1, min_b: float = 10.0, z_window_kms: float = 100.0, max_b: float = 50.0, max_logN: float = 19.0, mask: str = 'new', z_em_margin_kms: float = 3000.0) Session[source]#
Auto-fit Ly-alpha forest.
Wrapper for
components_auto()targeted at the forest. Includes filtering of interlopers based on the b-parameter. Delegates toastrocook.core.system_list.SystemList.filter_by_criteria().- Parameters:
score_threshold (float, optional) – Lower threshold for single lines (R^2). Defaults to
0.1.min_b (float, optional) – Minimum b parameter (km/s) to keep. Defaults to
10.0.z_window_kms (float, optional) – Fit window (km/s). Defaults to
100.0.max_b (float, optional) – Upper bound on b (km/s) in the fits, see
components_auto(). Defaults to50.0.max_logN (float, optional) – Upper bound on logN in the fits, see
components_auto(). Defaults to19.0.mask (str, optional) – The mask of the absorbed pixels used to find the regions:
'new'(made again with the defaults offind_absorbed()) or'session'(theabs_maskalready in the session, e.g. fromestimate_autowith other parameters). Defaults to'new'(F50, phase 6-bis, PR 6b).z_em_margin_kms (float, optional) – How far beyond
z_ema candidate system can lie (km/s). Defaults to3000.0(F55, phase 6-bis, PR 6b).
- Returns:
A new
Sessionwith the forest model.- Return type:
- forest_auto(score_threshold: float = 0.1, min_b: float = 10.0, z_window_kms: float = 20.0, max_b: float = 50.0, max_logN: float = 19.0, z_em_margin_kms: float = 3000.0) Session[source]#
Auto-fit full Lyman series.
Pipeline that models the entire Lyman series (Ly-alpha to Ly-limit) simultaneously. Uses ‘Ly_a’ detection but maps it to ‘Lyman’ series.
- Parameters:
score_threshold (float, optional) – Threshold for detection. Defaults to
0.1.min_b (float, optional) – Minimum b parameter (km/s). Defaults to
10.0.z_window_kms (float, optional) – Fit window (km/s). Defaults to
20.0.max_b (float, optional) – Upper bound on b (km/s) in the fits, see
components_auto(). Defaults to50.0.max_logN (float, optional) – Upper bound on logN in the fits, see
components_auto(). Defaults to19.0.z_em_margin_kms (float, optional) – How far beyond
z_ema candidate system can lie (km/s). Defaults to3000.0(F55, phase 6-bis, PR 6b).
- Returns:
A new
Sessionwith Lyman series systems.- Return type:
- metals_auto(ions: str = 'CIV,SiIV,NV,OVI,CII,MgII', aic_penalty: float = 5.0, z_window_kms: float = 20.0, max_b: float = 50.0, max_logN: float = 19.0) Session[source]#
Check for associated metals.
Scans existing systems and attempts to add other common metal ions (e.g., NV, SiIV) at the same redshift, keeping them if fit quality holds. Delegates to
astrocook.core.system_list.SystemList.add_component().- Parameters:
ions (str, optional) – Comma-separated list of ions to check. Defaults to
"CIV,SiIV,NV,OVI,CII,MgII".aic_penalty (float, optional) – Decrease of the AIC needed to keep a new ion, against the same model without it on the pixels of its fit. An ion is also kept only if its fit gives logN >= 12 and 2 <= b <= 50 km/s, with neither b nor logN at the bounds of the fit (F121). Each ion tried is reported (
triedin the extra of the result), with the reason when it is not kept. Defaults to5.0.z_window_kms (float, optional) – Fit window (km/s) for local island fitting. Defaults to
20.0.max_b (float, optional) – Upper bound on b (km/s) in the fits, see
components_auto(). Defaults to50.0.max_logN (float, optional) – Upper bound on logN in the fits, see
components_auto(). Defaults to19.0.
- Returns:
A new
Sessionwith added metals.- Return type:
- identify_lines(multiplets: str = 'CIV,SiIV,MgII,Ly_ab', mask_col: str = 'abs_mask', min_pix_region: int = 3, merge_dv: float = 10.0, score_threshold: float = 0.5, bypass_scoring: bool = False, debug_rating: bool = False, z_em_margin_kms: float = 3000.0) Session[source]#
Identify likely absorption systems.
Finds absorption regions, computes correlation signals for multiplets, and identifies the most likely candidates using a scoring system. Delegates to
astrocook.core.spectrum.Spectrum.identify_lines().- Parameters:
multiplets (str, optional) – Multiplets to search for (comma-separated). Defaults to
"CIV,SiIV,MgII,Ly_ab".mask_col (str, optional) – Column of the mask (True for the absorbed pixels, used to find the absorbed regions). Defaults to
"abs_mask".min_pix_region (int, optional) – Minimum width in pixels to count as a region. Defaults to
3.merge_dv (float, optional) – Max velocity (km/s) to merge adjacent regions. Defaults to
10.0.score_threshold (float, optional) – Min R^2 score (0 to 1) to accept a doublet candidate. Defaults to
0.5.bypass_scoring (bool, optional) – If
True, accept all kinematic candidates ignoring score. Defaults toFalse.debug_rating (bool, optional) – If
True, show debug plots. Defaults toFalse.z_em_margin_kms (float, optional) – How far beyond
z_ema candidate system can lie (km/s). Defaults to3000.0(F55, phase 6-bis, PR 6b).
- Returns:
A new
Sessionwith identification maps in metadata.- Return type:
- populate_from_identification(region_id_col: str = 'abs_ids', series_map_json: str = '', z_map_json: str = '') Session[source]#
Populate components from identification.
Populates the system list using the regions and candidates found by
identify_lines(). Delegates toastrocook.core.system_list.SystemList.add_component()andastrocook.core.spectrum.Spectrum.update_model().- Parameters:
- Returns:
A new
Sessionwith populated systems and updated model.- Return type:
- add_component(series: str = 'Ly_a', z: float | None = None, logN: float = 13.5, b: float = 10.0, btur: float = 0.0, z_window_kms: float = 20.0) Session[source]#
Add a single component.
Adds a new manual Voigt component at a specific redshift and auto-fits it. Delegates to
astrocook.core.system_list.SystemList.add_component().- Parameters:
series (str, optional) – Transition or multiplet (e.g.
'CIV'), or a multiplet of its own written as its transitions ('FeII_2586,FeII_2600'). Defaults to"Ly_a". An unknown one is refused.z (float) – Redshift (required; the default 0.0 of the registry was removed in phase 6-quater, PR 10).
logN (float, optional) – Column Density (log cm^-2). Defaults to
13.5.b (float, optional) – Doppler parameter (km/s). Defaults to
10.0.btur (float, optional) – Turbulent broadening (km/s). Defaults to
0.0.z_window_kms (float, optional) – Max shift for initial fit (km/s). Defaults to
20.0.
- Returns:
A new
Sessionwith the added component.- Return type:
- update_component(uuid: str, z: float | None = None, logN: float | None = None, b: float | None = None, btur: float | None = None, series: str | None = None, resol: float | None = None) Session[source]#
Update component parameters.
Modifies the physical parameters of an existing component. Delegates to
astrocook.core.system_list.SystemList.update_component().- Parameters:
uuid (str) – Component UUID.
z (float or None, optional) – Redshift. Defaults to None (unchanged).
logN (float or None, optional) – Column Density (log). Defaults to None (unchanged).
b (float or None, optional) – Doppler parameter (km/s). Defaults to None (unchanged).
btur (float or None, optional) – Turbulent broadening (km/s). Defaults to None (unchanged).
series (str or None, optional) – Transition or multiplet. Defaults to None (unchanged).
resol (float or None, optional) – Resolving power R of the component, instead of that of the spectrum; 0 removes it. Defaults to None (unchanged).
- Returns:
A new
Sessionwith updated component.- Return type:
- update_components(uuids: list, z: float | None = None, logN: float | None = None, b: float | None = None, btur: float | None = None, series: str | None = None, resol: float | None = None) Session[source]#
Batch update component parameters.
Modifies the physical parameters of multiple existing components simultaneously. Delegates to
astrocook.core.system_list.SystemList.update_components().- Parameters:
uuids (list) – List of component UUIDs to update.
z (float or None, optional) – Redshift. Defaults to None (unchanged).
logN (float or None, optional) – Column Density (log). Defaults to None (unchanged).
b (float or None, optional) – Doppler parameter (km/s). Defaults to None (unchanged).
btur (float or None, optional) – Turbulent broadening (km/s). Defaults to None (unchanged).
series (str or None, optional) – Transition or multiplet. Defaults to None (unchanged; the string
'None'of the old logs too).resol (float or None, optional) – Resolving power R of the components, instead of that of the spectrum; 0 removes it. Defaults to None (unchanged).
- Returns:
A new
Sessionwith updated components.- Return type:
- delete_component(uuid: str | None = None, uuids: list | None = None) Session[source]#
Delete component(s).
Removes specified components and updates the model. Without a continuum the model cannot be computed: the components are deleted anyway and the
modelcolumn, if any, is removed. Delegates toastrocook.core.system_list.SystemList.delete_component()andastrocook.core.spectrum.Spectrum.update_model().
- detect_anchor(sibling_name: str, trans_self: str, trans_sibling: str) float[source]#
Detect doublet anchor redshift.
Finds the redshift of maximum overlap between this session and a sibling session (useful for doublet identification across orders/exposures). Delegates to
astrocook.core.spectrum.Spectrum.detect_doublet_z().- Parameters:
sibling_name (str) – Name of the sibling session.
trans_self (str) – Transition in this session (e.g.,
'OVI_1031').trans_sibling (str) – Transition in sibling session (e.g.,
'OVI_1037').
- Returns:
The detected redshift.
- Return type:
float
- fit_component(uuid: str, max_nfev: int = 2000, z_window_kms: float = 20.0, group_depth: int = 2) Session[source]#
Fit component.
Fits the selected component (and its connected neighbors) using the Voigt engine. Delegates to
astrocook.core.system_list.SystemList.fitting_context()andastrocook.fitting.voigt_fitter.VoigtFitter.- Parameters:
- Returns:
A new
Sessionwith updated parameters and model.- Return type:
- update_constraint(uuid: str, param: str, is_free: bool | None = None, expression: str | None = None, target_uuid: str | None = None) Session[source]#
Update constraint.
Freeze, unfreeze, or link component parameters. Delegates to
astrocook.core.system_list.SystemList.update_constraint().- Parameters:
uuid (str) – Component UUID.
param (str) – Parameter name (
z,logN,b).is_free (bool, optional) – True: free (and not linked); False: frozen; None: as it is, frozen if an expression is given.
expression (str, optional) – Math expression for linking (e.g.,
"p['uuid'].z").target_uuid (str, optional) – UUID of the component it is linked to, which the fits put in its group: one named by the expression; None: the first it names.
- Returns:
A new
Sessionwith updated constraints.- Return type:
- update_constraints_column(param: str, is_free: bool) Session[source]#
Bulk update constraints.
Updates the constraint status (free/frozen) for a specific parameter across ALL components in the session.
- add_linked_system(series_list: str, z: float, logN: float = 13.5, b: float = 10.0, z_window_kms: float = 20.0) Session[source]#
Add linked system.
Add multiple components (e.g. CIV, SiIV) and link their redshifts to the first component in the list.
- Parameters:
- Returns:
A new
Sessionwith linked components fitted.- Return type:
- optimize_system(uuid: str, max_components: int = 5, threshold_sigma: float = 2.5, aic_penalty: float = 5.0, z_window_kms: float = 100.0, min_dv: float = 10.0, group_depth: int = 2, patience: int = 2, max_nfev: int = 1000) Session[source]#
Optimize system (Iterative Fit).
Iteratively adds components to a system to minimize residuals using the Akaike Information Criterion (AIC). Delegates to
astrocook.core.system_list.SystemList.optimize_hierarchy().- Parameters:
uuid (str) – Target Component UUID (seed).
max_components (int, optional) – Max candidate components to try: each counts, also one rejected by the AIC or too close to another (
min_dv). Defaults to5.threshold_sigma (float, optional) – Residual threshold (sigma) to trigger new component. Defaults to
2.5.aic_penalty (float, optional) – Min AIC improvement to accept new component (the AIC already counts 2 per free parameter; 5 asks for substantial support). Defaults to
5.0.z_window_kms (float, optional) – Half-width (km/s) of the window around the first line of the system where the residuals are searched and the AIC is computed; it is also how far the redshifts can move in the fits (
z_window_kmsoffit_component()). Defaults to100.0.min_dv (float, optional) – Min separation (km/s). Defaults to
10.0.group_depth (int, optional) – Grouping depth. Defaults to
2.patience (int, optional) – Trials allowed without AIC improvement. Defaults to
2.max_nfev (int, optional) – Max evaluations of the model in each fit (the starting one and one per candidate). A fit that reaches it counts as failed: its candidate is rejected, and if the starting fit fails no component is added (F149). Defaults to
1000, the value that was fixed before phase 6-quater, PR 10-bis.
- Returns:
A new
Sessionwith optimized systems.- Return type:
- fit_bayesian(uuids: list[str], engine: str = 'mcmc', nsteps: int = 500, nlive: int = 250, group_depth: int = 2, seed: int | None = None) Session[source]#
Run Bayesian sampling (MCMC or Nested) on a group of components.
The prior is flat: z within ±20 km/s of its current value, logN and b within the bounds of the constraint model (10-23 and 1-300 km/s). The components get the medians of the posterior, and its standard deviations as errors.
- Parameters:
uuids (list of str) – The UUIDs of the components to fit (their group, by
group_depth, is sampled).engine ({'mcmc', 'nested'}, optional) – The sampling engine to use (default: ‘mcmc’).
nsteps (int, optional) – Number of MCMC steps of each walker (default: 500). The walkers are 32, or twice the free parameters when these are more than 16; the first
min(200, nsteps // 2)steps are discarded (burn-in).nlive (int, optional) – Number of live points for Nested Sampling (default: 250); at least 10 per free parameter (with fewer the sampler fails or does not end).
group_depth (int, optional) – Depth of connected group search (FoF) to define the fitting group (default: 2).
seed (int, optional) – Seed of the random numbers, for a sampling that can be repeated (default: None, not repeatable).
- Raises:
ImportError – If the sampler of
engineis not installed (optional dependencies:pip install "astrocook[bayes]").
- refit_all(max_nfev: int = 200, z_window_kms: float = 100.0, group_depth: int = 2, max_group_size: int = 20, region_limit: str = 'None', selected_uuids: list | None = None, max_b: float = 300.0, max_logN: float = 23.0) Session[source]#
Refit all systems.
Iteratively fits all disjoint groups of components (islands) in the spectrum. Uses chunking for large groups. Delegates to
astrocook.core.system_list.SystemList.get_connected_group()andastrocook.fitting.voigt_fitter.VoigtFitter.- Parameters:
max_nfev (int, optional) – Max iterations per group. Defaults to
200.z_window_kms (float, optional) – Fit window around lines (km/s). Defaults to
100.0.group_depth (int, optional) – Grouping depth (0=Iterative Single, 1=Neighbors, 2=FoF). Defaults to
2, as the GUI dialog (#573, F11).max_group_size (int, optional) – Max components per fit group (chunking). Defaults to
20.region_limit (str, optional) – Limit fitting to
'red_side'or'lya_forest'. Defaults to"None".max_b (float, optional) – Upper bound on b (km/s). Defaults to
300.0, the physical limit of the constraint model; the automatic pipelines pass"50.0"(seecomponents_auto()).max_logN (float, optional) – Upper bound on logN. Defaults to
23.0, the physical limit of the constraint model; the automatic pipelines pass"19.0"(seecomponents_auto()).
- Returns:
A new
Sessionwith all systems refitted.- Return type:
- clean_negligible(min_logN: float = 11.5, max_b: float = 80.0, min_b: float = 3.0, combined_check: bool = True) Session[source]#
Clean negligible components.
Removes unphysical or negligible components from the system list. This is useful for cleaning up results after automated fitting procedures, which may produce artifacts such as extremely broad, shallow lines (ghosts) or narrow noise spikes.
The cleaning criteria are: 1. Narrow Spikes: Components with
b < min_b. 2. Weak Lines: Components withlogN < min_logN. 3. Broad/Ghost Lines: Components withb > max_b.If
combined_checkisTrue, broad lines are only removed if they are also weak (logN < 13.0). This protects strong, broad features like DLA wings or OVI absorbers.- Parameters:
min_logN (float, optional) – Minimum column density (log cm-2) to keep. Defaults to
11.5.max_b (float, optional) – Maximum Doppler parameter (km/s). Components broader than this may be removed. Defaults to
80.0.min_b (float, optional) – Minimum Doppler parameter (km/s). Components narrower than this are always removed. Defaults to
3.0.combined_check (bool, optional) – If
True, broad components (b > max_b) are only removed if they are also weak (logN < 13.0). IfFalse, all broad components are removed. Defaults toTrue.
- Returns:
A new
Sessionwith the flagged components removed.- Return type: