casper.interface.gisic package#

Submodules#

casper.interface.gisic.norm_functions module#

casper.interface.gisic.norm_functions.in_molecular_band(wl: float, tol: float = 10) bool[source]#

Check if a given wavelength falls within a known molecular absorption band.

This function evaluates whether the input wavelength wl is within the range of any of the predefined molecular bands (e.g., G-band, C2 regions).

Parameters:
  • wl (float) – The wavelength value to check (in Angstroms).

  • tol (float, optional) – Tolerance in Angstroms (currently unused), default is 10.

Returns:

True if the wavelength falls within one of the defined bands, otherwise False.

Return type:

bool

casper.interface.gisic.normalize module#

casper.interface.gisic.normalize.normalize(wavelength: ndarray, flux: ndarray, sigma: int = 30, k: int = 3, s: int = 12, cahk: bool = False, band_check: bool = True, flux_min: float = 70, boost: bool = True, return_points: bool = False) Tuple[ndarray, ndarray, ndarray] | Tuple[ndarray, ndarray, ndarray, Dict[str, Any]][source]#

Normalize a spectrum using spline fitting based on inflection segment detection.

Parameters:
  • wavelength (np.ndarray) – Array of wavelength values.

  • flux (np.ndarray) – Array of flux values corresponding to the input wavelengths.

  • sigma (int, optional) – Gaussian smoothing kernel width for segment generation.

  • k (int, optional) – Degree of the spline for continuum fitting.

  • s (int, optional) – Spline smoothing factor.

  • cahk (bool, optional) – Whether to apply special treatment to the Ca H&K region.

  • band_check (bool, optional) – Whether to exclude strong band regions during continuum estimation.

  • flux_min (float, optional) – Minimum threshold for flux filtering.

  • boost (bool, optional) – Whether to boost the flux points during continuum point definition.

  • return_points (bool, optional) – If True, also return the continuum anchor points used for spline fitting.

Returns:

If return_points is False:
  • wavelength : np.ndarray

  • normalized_flux : np.ndarray

  • continuum : np.ndarray

If return_points is True:
  • wavelength : np.ndarray

  • normalized_flux : np.ndarray

  • continuum : np.ndarray

  • pointsdict

    Dictionary with keys “wavelength” and “flux” for the continuum anchor points.

Return type:

tuple

casper.interface.gisic.segment module#

class casper.interface.gisic.segment.Segment(wl: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = None, flux: _Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] = None)[source]#

Bases: object

Initialize a Segment object with wavelength and flux values.

Parameters:
  • wl (array_like) – Wavelength values that define the x-axis of the segment.

  • flux (array_like) – Flux values corresponding to each wavelength in wl, defining the y-axis of the segment.

midpoint#

The median value of the wavelength array, used to represent the segment’s center.

Type:

float

define_cont_point(mad_min: float, mad_range: float, boost: bool = True) None[source]#

Define a continuum point based on the flux variability within the segment.

Parameters:
  • mad_min (float) – Minimum normalized MAD value used as a baseline.

  • mad_range (float) – Range used to scale the influence of flux variation on the continuum point.

  • boost (bool, optional) – If True, adjust the continuum point toward the segment’s maximum flux based on relative variation. If False, use the median flux as the continuum point.

  • Sets

  • ----

  • self.mad_relative (float) – Relative MAD used to determine how strongly to bias the continuum point.

  • self.continuum_point (float) – Estimated continuum level for this segment.

get_statistics(flux_min: float = 70) None[source]#

Compute robust statistical measures of flux variability for the segment.

Parameters:
  • flux_min (float, optional) – The lower percentile threshold used for clipping the flux distribution. Default is 70.

  • Sets

  • ----

  • self.mad (float) – Median Absolute Deviation (MAD) of the flux values.

  • self.mad_normal (float) – Normalized MAD, calculated as MAD divided by the median flux.

  • self.flux_med (float) – Median flux value computed from the central clipped range [flux_min percentile, 98th percentile].

  • self.flux_min (float) – Flux value at the flux_min percentile.

  • self.flux_max (float) – Flux value at the 98th percentile.

is_edge(which: str) None[source]#

Set the midpoint of the segment to either the first or last wavelength value, depending on whether the segment is at the start or end of the spectrum.

Parameters:

which (str) – Indicates which edge the segment belongs to. Must be one of: - “left”: sets midpoint to the first wavelength. - “right”: sets midpoint to the last wavelength.

Raises:

Prints an error message if which is not "left" or "right".

casper.interface.gisic.spectrum module#

class casper.interface.gisic.spectrum.Spectrum(wavelength: ndarray, flux: ndarray)[source]#

Bases: object

Initialize a Spectrum object with wavelength and flux data.

Parameters:
  • wavelength (np.ndarray) – The wavelength values of the spectrum.

  • flux (np.ndarray) – The flux values corresponding to each wavelength.

  • Sets

  • ----

  • self.wavelength (np.ndarray) – Input wavelength array.

  • self.flux (np.ndarray) – Input flux array.

  • self.segments (Optional[List]) – Will store segmentation data for continuum fitting (initialized to None).

  • self.mad_global (Optional[float]) – Global MAD (Median Absolute Deviation) for the entire spectrum (initialized to None).

add_continuum_point(point: Tuple[float, float]) None[source]#

Add a new continuum anchor point and sort existing points by wavelength.

Parameters:

point (tuple of float) – A tuple containing (wavelength, flux) to be added to the continuum points.

Notes

After adding the new point, both self.midpoints and self.fluxpoints are sorted based on the wavelength values in self.midpoints.

assess_segment_variation() None[source]#

Compute variation metrics for each segment based on normalized MAD (Median Absolute Deviation).

This method evaluates the variability of flux within each segment by collecting normalized MAD values. It then calculates global statistics across all segments and derives a scaled relative MAD array used for weighting or further processing.

Sets#

self.mad_arraynp.ndarray

Normalized MAD values for all segments.

self.mad_globalfloat

Global median of the segment MAD values.

self.mad_minfloat

Minimum normalized MAD value across segments.

self.mad_maxfloat

Maximum normalized MAD value across segments.

self.mad_rangefloat

Difference between mad_max and mad_min.

self.mad_relative_arraynp.ndarray

Normalized MAD values scaled between 0 and 1.

define_cont_points(boost: float) None[source]#

Define continuum points for all segments using a boosted MAD-normalized strategy.

This method loops over all Segment objects in self.segments and calls their define_cont_point() method. The boost value scales the clipped median flux based on the MAD distribution across segments.

Parameters:

boost (float) – A scaling factor applied to adjust the continuum point above the median flux, based on signal variation.

Return type:

None

Notes

self.assess_segment_variation() must be called beforehand to ensure self.mad_min and self.mad_range are initialized.

generate_inflection_segments(sigma: int = 30, width: int = 30, cahk: bool = False, cahkwidth: int = 2, band_check: bool = False, flux_min: float = 70) None[source]#

Identify inflection segments in the spectrum based on second derivative zero crossings.

Parameters:
  • sigma (int, optional) – Gaussian smoothing kernel width for the flux array. Default is 30.

  • width (int, optional) – Width of each segment around identified inflection points. Default is 30.

  • cahk (bool, optional) – If True, forces inflection segments at Ca H (3916Å) and Ca K (3991Å) lines. Default is False.

  • cahkwidth (int, optional) – Width around the Ca H&K lines if cahk is True. Default is 2.

  • band_check (bool, optional) – Whether to skip segments inside known molecular bands. Default is False.

  • flux_min (float, optional) – Lower percentile threshold used during segment statistics estimation. Default is 70.

  • Sets

  • ----

  • self.segments (List[Segment]) – List of spectral segments created around inflection points.

  • self.frame (pd.DataFrame) – Intermediate DataFrame containing smoothed flux and its derivatives.

  • self.ZEROS (List[float]) – List of wavelength values where the second derivative crosses zero.

generate_segments(bins: int = 25, lower: int = 70) None[source]#

Divide the spectrum into segments and compute statistics for each.

Parameters:
  • bins (int, optional) – Number of equal-sized segments to divide the spectrum into. Default is 25.

  • lower (int, optional) – Lower percentile threshold for flux clipping during statistics calculation. Default is 70.

  • Sets

  • ----

  • self.segments (List[Segment]) – A list of Segment objects representing subdivided portions of the spectrum. The first and last segments are marked as edges.

  • (e.g. (Each segment will also compute its own local statistics)

  • MAD

  • flux). (median)

get_continuum_points() None[source]#

Print all continuum anchor points.

Prints the index, midpoint wavelength, and corresponding flux value for each continuum anchor point stored in self.midpoints and self.fluxpoints.

Return type:

None

normalize() None[source]#

Normalize the flux by dividing it by the fitted continuum.

This method computes the normalized flux (flux_norm) by dividing the raw flux values by the continuum. Extreme values below 0 or above 2 are clipped to 1.0 for stability.

Return type:

None

remove_point(index: List[int]) None[source]#

Remove continuum anchor points at the specified indices.

Parameters:

index (list of int) – A list of indices indicating which points to remove from midpoints and fluxpoints.

Notes

Indices are assumed to be sorted in increasing order. To avoid indexing errors due to shifting during deletion, removal is done in reverse order.

set_fluxpoints(flux_values: list[float]) None[source]#

Set the list of flux values at continuum midpoints.

Parameters:

flux_values (list of float) – List of flux values corresponding to each continuum midpoint.

Return type:

None

set_segment_continuum() ndarray[source]#

Collect and store the continuum flux point of each segment.

This method gathers the estimated continuum flux points from each spectral segment and stores them in self.fluxpoints.

Returns:

Array of continuum flux values for all segments.

Return type:

np.ndarray

set_segment_midpoints() ndarray[source]#

Collect and store the midpoint wavelength of each segment.

This method iterates over all spectral segments and extracts their midpoint values, storing them in self.midpoints.

Returns:

Array of midpoint wavelength values for all segments.

Return type:

np.ndarray

set_wavelength(wavelength: list[float]) None[source]#

Set the list of continuum midpoint wavelengths.

Parameters:

wavelength (list of float) – List of wavelength values representing the continuum midpoints.

Return type:

None

spline_continuum(k: int = 3, s: float = 5.0) None[source]#

Fit a spline to the continuum points and evaluate it across the full wavelength range.

Parameters:
  • k (int, optional) – Degree of the smoothing spline. Default is 3 (cubic spline).

  • s (float, optional) – Smoothing factor. Larger values result in smoother fits. Default is 5.0.

Return type:

None

Module contents#