Skip to content

Processing API

The functions behind DataConfig.from_tiles and FitDataBuilder.build: clipping to wavelength and sky ranges, setting bad data to NaN, and normalisation. Use them directly to inspect intermediate steps, e.g. filter_inspector to see how much data each filter removes.

Pipeline functions

These apply a DataConfig to tiles. FitDataBuilder.build runs process_tile_data, then flatten_tile_coord, then get_normalisation_functions.

lvm_tools.fit_data.processing.process_tile_data(tiles, config)

Clip and then filter tile data with a config.

NumPy RuntimeWarnings (e.g. from medians of all-NaN spaxels) are silenced.

Parameters:

Name Type Description Default
tiles LVMTileLike

The data.

required
config DataConfig

The config.

required

Returns:

Type Description
Dataset

The processed data, with the same dimensions as tiles.data and excluded values set to NaN.

Raises:

Type Description
ValueError

If no data are left after clipping, or after filtering.

lvm_tools.fit_data.processing.clip_data(tile_data, config)

Clip tile data to the config's wavelength, RA and Dec ranges (see clip_dataset).

Parameters:

Name Type Description Default
tile_data Dataset

Tile data, as in LVMTile.data.

required
config DataConfig

The config.

required

Returns:

Type Description
Dataset

The clipped data.

lvm_tools.fit_data.processing.filter_tile_data(tile_data, config)

Set the data the config excludes to NaN (see filter_dataset).

Parameters:

Name Type Description Default
tile_data Dataset

Tile data, as in LVMTile.data.

required
config DataConfig

The config.

required

Returns:

Type Description
Dataset

The filtered data.

lvm_tools.fit_data.processing.get_αδ_ranges(tiles)

RA and Dec ranges of a square around all the spaxels (see bounding_square).

The square is in degrees of RA and Dec, without a cos(Dec) factor.

Parameters:

Name Type Description Default
tiles LVMTileLike

The data.

required

Returns:

Type Description
tuple[tuple[float, float], tuple[float, float]]

The pair (α_range, δ_range), in degrees.

lvm_tools.fit_data.processing.get_normalisations(ds, config)

Normalisation offsets and scales of processed data, using the config's strategies.

The flux uses config.normalise_F_strategy; RA and Dec each use config.normalise_αδ_strategy, separately (DataConfig.from_tiles then gives both the larger scale).

Parameters:

Name Type Description Default
ds Dataset

Processed data, e.g. from process_tile_data.

required
config DataConfig

The config.

required

Returns:

Type Description
tuple[tuple[float, float], tuple[float, float], tuple[float, float]]

((F_offset, F_scale), (α_offset, α_scale), (δ_offset, δ_scale)).

lvm_tools.fit_data.processing.get_normalisation_functions(config)

The normalisation functions given by a config's offsets and scales.

The inverse variance is normalised consistently with the flux: it has offset 0 and scale normalise_F_scale**-2, so it is multiplied by normalise_F_scale**2.

Parameters:

Name Type Description Default
config DataConfig

The config.

required

Returns:

Type Description
tuple[Callable, ...]

Eight functions, in the order FitData takes them: normalise and denormalise the flux, the inverse variance, RA and Dec.

lvm_tools.fit_data.processing.flatten_tile_coord(ds)

Flatten the tile and spaxel dimensions into a single flat_spaxel dimension.

flat_spaxel runs over the spaxels of the first tile, then the second, and so on. It is placed last, so the cubes have dimensions (wavelength, flat_spaxel). The tile and spaxel values of each flat spaxel are kept as coordinates along it, and per-tile coordinates such as mjd are repeated for each spaxel.

Parameters:

Name Type Description Default
ds Dataset

Data with tile and spaxel dimensions.

required

Returns:

Type Description
Dataset

The flattened data.

Clipping functions

lvm_tools.fit_data.clipping.clip_dataset(data, λ_range, α_range, δ_range)

Clip tile data to wavelength range(s) and an RA/Dec box.

The wavelengths are clipped with clip_wavelengths. Values at spaxels outside the RA/Dec box (bounds included) are set to NaN, and spaxels that are outside it in every tile are dropped.

Parameters:

Name Type Description Default
data Dataset

Tile data, as in LVMTile.data.

required
λ_range Range | Ranges

Wavelength range(s) to keep, in Angstrom.

required
α_range Range

Right ascension range (min, max) to keep, in degrees.

required
δ_range Range

Declination range (min, max) to keep, in degrees.

required

Returns:

Type Description
Dataset

The clipped data.

Raises:

Type Description
TypeError

If a range is malformed (see verify_range).

ValueError

If a range has min >= max, or the wavelength ranges overlap or are out of order.

lvm_tools.fit_data.clipping.clip_wavelengths(data, λ_ranges)

Keep only the wavelengths in one or more ranges.

Each range is selected with its bounds included, and the selections are concatenated along the wavelength dimension, so several separate windows (e.g. around different emission lines) can be kept.

Parameters:

Name Type Description Default
data Dataset

Tile data, with a wavelength coordinate in Angstrom.

required
λ_ranges Range | Ranges

A range (min, max) or several ranges, in increasing order and not overlapping.

required

Returns:

Type Description
Dataset

The data at the selected wavelengths.

Raises:

Type Description
TypeError

If the ranges are malformed (see verify_ranges).

ValueError

If a range has min >= max, or the ranges overlap or are out of order.

lvm_tools.fit_data.clipping.bounding_square(x_min, x_max, y_min, y_max)

The square around a rectangle, with a little room to spare.

The square has the rectangle's centre, and sides 1.01 times the longer side of the rectangle (0.5% of padding on each side). It gives RA and Dec ranges of equal width.

Parameters:

Name Type Description Default
x_min float

Lower x bound of the rectangle.

required
x_max float

Upper x bound of the rectangle.

required
y_min float

Lower y bound of the rectangle.

required
y_max float

Upper y bound of the rectangle.

required

Returns:

Type Description
tuple[tuple[float, float], tuple[float, float]]

The square's (x_min, x_max) and (y_min, y_max) ranges.

lvm_tools.fit_data.clipping.ensure_ranges(ranges)

Turn a single range into a tuple of one range; leave several ranges as they are.

Parameters:

Name Type Description Default
ranges Range | Ranges

A range (min, max), or several ranges.

required

Returns:

Type Description
Ranges

A tuple of ranges.

lvm_tools.fit_data.clipping.slice_mask(arr, x_min, x_max)

Where the values of an array lie in [x_min, x_max] (bounds included).

Parameters:

Name Type Description Default
arr DataArray

The values.

required
x_min float

Lower bound.

required
x_max float

Upper bound.

required

Returns:

Type Description
DataArray

A boolean array, True inside the range.

lvm_tools.fit_data.clipping.verify_range(r)

Check that a range is a tuple (or list) of floats with min < max.

Parameters:

Name Type Description Default
r Range

The range.

required

Raises:

Type Description
TypeError

If it is not a tuple or list, or an entry is neither a float nor an int.

ValueError

If min >= max.

Warns:

Type Description
UserWarning

If an entry is an int rather than a float.

lvm_tools.fit_data.clipping.verify_ranges(ranges)

Check several ranges: each must be valid, and they must be in increasing order and not overlap.

Parameters:

Name Type Description Default
ranges Ranges

The ranges.

required

Raises:

Type Description
TypeError

If a range is invalid (see verify_range) or ranges is not a tuple.

ValueError

If a range has min >= max, or the ranges overlap or are out of order.

Filtering functions

lvm_tools.fit_data.filtering.filter_dataset(data, nans_strategy, F_bad_strategy, F_bad_range, fibre_status_include, apply_mask)

Set bad data to NaN.

A value is excluded if any of these flag it: the NaN strategy, the bad flux strategy, the fibre status, and (if apply_mask) the DRP mask. All data variables (flux, inverse variance, LSF and mask) are set to NaN where excluded; spaxel-level flags exclude every wavelength of the spaxel.

Parameters:

Name Type Description Default
data Dataset

Tile data, as in LVMTile.data.

required
nans_strategy ExcludeStrategy

"pixel" (keep other pixels of a spaxel with NaN fluxes) or "spaxel" (exclude spaxels with any NaN flux).

required
F_bad_strategy ExcludeStrategy

"pixel", "spaxel" or "spaxel_max"; see DataConfig.F_bad_strategy.

required
F_bad_range tuple[float, float]

The acceptable flux range (min, max).

required
fibre_status_include tuple[FibreStatus]

Fibre status values to keep.

required
apply_mask bool

Whether to exclude the pixels flagged in the DRP mask.

required

Returns:

Type Description
Dataset

The data, with excluded values set to NaN.

Raises:

Type Description
ValueError

If nans_strategy or F_bad_strategy is not one of the values above.

lvm_tools.fit_data.filtering.filter_inspector(data, F_bad_range, fibre_status_include)

Count how many pixels and spaxels each filter would exclude, to help choose a config.

Each entry of the result is a pair (n_pixels, n_spaxels), with None where a count doesn't apply:

  • "nans": NaN fluxes, and spaxels with any NaN flux.
  • "bad flux": pixels flagged by get_where_bad (the "pixel" strategy).
  • "bad flux median": spaxels flagged by get_where_bad_median (the "spaxel" strategy).
  • "fibre status": spaxels whose fibre status is not in fibre_status_include.
  • "mask": pixels flagged in the DRP mask, and spaxels with any flagged pixel.
  • "any bad": pixels flagged by any of NaN, bad flux, fibre status or mask, and spaxels with any pixel flagged by NaN, bad median flux, fibre status or mask.

Parameters:

Name Type Description Default
data Dataset

Tile data, as in LVMTile.data.

required
F_bad_range tuple[float, float]

The acceptable flux range (min, max).

required
fibre_status_include tuple[FibreStatus]

Fibre status values to keep.

required

Returns:

Type Description
dict

The counts, keyed by filter name.

Example
stats = filter_inspector(tile.data, (-1e-14, np.inf), (0,))
print(f"NaN pixels: {stats['nans'][0]}")
print(f"Bad fibre spaxels: {stats['fibre status'][1]}")

lvm_tools.fit_data.filtering.get_where_nan(arr)

Where an array is NaN.

Parameters:

Name Type Description Default
arr DataArray

The values.

required

Returns:

Type Description
DataArray

A boolean array, True where the value is NaN.

lvm_tools.fit_data.filtering.get_where_bad(arr, bad_range)

Where pixels are outside the acceptable range.

A pixel is flagged when its value is <= bad_range[0] or > bad_range[1]. NaN values are not flagged (nans_strategy handles them).

Parameters:

Name Type Description Default
arr DataArray

The values, e.g. the flux.

required
bad_range tuple[float, float]

The acceptable range (min, max).

required

Returns:

Type Description
DataArray

A boolean array of the same shape, True where the pixel is bad.

lvm_tools.fit_data.filtering.get_where_bad_median(arr, bad_range)

Which spaxels have a median outside the acceptable range, or are entirely NaN.

Parameters:

Name Type Description Default
arr DataArray

The values, with a wavelength dimension, e.g. the flux.

required
bad_range tuple[float, float]

The acceptable range (min, max) of the median over wavelength.

required

Returns:

Type Description
DataArray

A boolean array without the wavelength dimension, True where the spaxel is bad.

lvm_tools.fit_data.filtering.get_where_bad_max(arr, bad_range)

Which spaxels have a maximum outside the acceptable range, or are entirely NaN.

Parameters:

Name Type Description Default
arr DataArray

The values, with a wavelength dimension, e.g. the flux.

required
bad_range tuple[float, float]

The acceptable range (min, max) of the maximum over wavelength.

required

Returns:

Type Description
DataArray

A boolean array without the wavelength dimension, True where the spaxel is bad.

lvm_tools.fit_data.filtering.get_where_badfib(fib_stat_arr, fibre_status_incl)

Which spaxels have a fibre status that is not included.

Parameters:

Name Type Description Default
fib_stat_arr DataArray

The fibre status of each spaxel.

required
fibre_status_incl tuple[FibreStatus]

The status values to keep.

required

Returns:

Type Description
DataArray

A boolean array, True where the status is not in fibre_status_incl.

lvm_tools.fit_data.filtering.get_where_mask(arr_mask)

Where the DRP mask flags a pixel (mask value 1).

Parameters:

Name Type Description Default
arr_mask DataArray

The DRP mask.

required

Returns:

Type Description
DataArray

A boolean array, True where the pixel is flagged.

lvm_tools.fit_data.filtering.combine_wheres(list_where)

Combine boolean arrays with a logical OR, broadcasting them against each other.

Parameters:

Name Type Description Default
list_where list[DataArray]

The boolean arrays (at least one).

required

Returns:

Type Description
DataArray

True wherever any of them is True.

Normalisation functions

lvm_tools.fit_data.normalisation.calc_normalisation(data, strategy)

The offset and scale that normalise data as (data - offset) / scale.

NaN values are ignored. The strategies give:

Strategy Offset Scale
None 0 1
"max only" 0 maximum
"98 only" 0 98th percentile
"extrema" minimum maximum - minimum
"1σ" mean 2 × standard deviation
"2σ" mean 4 × standard deviation
"3σ" mean 6 × standard deviation
"padded" minimum - 0.15 × range 1.3 × range

So "extrema" maps the data onto [0, 1], "Nσ" maps mean ± Nσ onto an interval of width 1 centred on 0, and "padded" maps the data onto [0.15/1.3, 1.15/1.3], leaving NORM_PADDING of the range free on each side.

Parameters:

Name Type Description Default
data ArrayLike

The values.

required
strategy NormaliseStrategy

The normalisation strategy.

required

Returns:

Type Description
tuple[float, float]

The pair (offset, scale).

Raises:

Type Description
ValueError

If the strategy is unknown.

lvm_tools.fit_data.normalisation.normalise(data, offset, scale)

Normalise data: (data - offset) / scale.

Parameters:

Name Type Description Default
data ArrayLike

The values.

required
offset float

The offset.

required
scale float

The scale.

required

Returns:

Type Description
ArrayLike

The normalised values.

lvm_tools.fit_data.normalisation.denormalise(data, offset, scale)

Undo normalise: data * scale + offset.

Parameters:

Name Type Description Default
data ArrayLike

The normalised values.

required
offset float

The offset.

required
scale float

The scale.

required

Returns:

Type Description
ArrayLike

The values on the original scale.

lvm_tools.fit_data.normalisation.get_norm_funcs(offset, scale)

normalise and denormalise with a fixed offset and scale.

Parameters:

Name Type Description Default
offset float

The offset.

required
scale float

The scale.

required

Returns:

Type Description
tuple[Callable, Callable]

The two functions, normalise then denormalise, each taking only the data, e.g. norm, denorm = get_norm_funcs(0.0, 2.0).

Constants

lvm_tools.fit_data.filtering.BAD_FLUX_THRESHOLD = -1e-14 module-attribute

Default lower bound of acceptable flux (DataConfig.F_range), -1e-14 erg cm⁻² s⁻¹ Å⁻¹.

lvm_tools.fit_data.normalisation.NORM_PADDING = 0.15 module-attribute

Padding of the "padded" normalisation, as a fraction of the data range on each side.