Model Grids

Contents

Model Grids#

Model Grid#

Core grid class that wraps an xarray.Dataset and exposes wavelength, resolution, parameter coordinates, and interpolation helpers.

class ForMoSA.grid.model_grid.ModelGrid(dataset, model_path=None, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER)[source]#

Bases: object

Representation of a model grid with metadata. Handles loading from file and exposing basic properties.

Parameters:
  • dataset (Dataset) – Dataset containing the model grid

  • model_path (str | PathLike | None) – Path to the model grid file

  • logger (Logger | None) – Logger instance for logging

  • log_level (str) – Level of the logger

  • display_unit (WavelengthUnit) – Unit of the wavelength to display

Notes

Authors: Allan Denis

property suffix: str#

Suffix used for saving (Overriden in subgrid).

property GridType: str#

OGrid type (overriden in subgrid).

property grid_as_dataarray: DataArray#

Grid as xr.DataArray (more tailored to manipulations).

property model_path: Path | str#

Path to the model.

property grid_name: str#

Name of the grid (Overriden in subgrid).

property logger: Logger#

Logger.

property native_unit: PrefixUnit#

Native unit of the wavelength.

property unit: PrefixUnit#

Unit of the wavelength to display.

property wave: ndarray#

Wavelength of the grid.

property res: ndarray#

Resolution of the grid.

property grid: Dataset#

Grid as xr.Dataset.

property attrs: dict#

Dictionary of attributes of the grid.

property keys: list#

Keys of the grid parameters.

property titles: list#

Names of the grid parameters.

property key_values: dict#

Values taken by the grid parameters.

property lims_params_grid#

Limits of grid parameters.

property nyquist: ndarray#

Nyquist sampling.

property effective_resolution: ndarray#

Effective resolution beeing the minimum between Nyquist sampling and resolution.

property n_grids: int#

Number of grids.

property size: int#

Size of the grid.

property dims: list[str]#

List of names for each dimension of the grid.

property dimensions: list[int]#

List of number of points for each dimension.

classmethod from_file(path, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER)[source]#

Generate grid from file.

Parameters:
  • path (str | PathLike) – Path to the grid

  • logger (Logger | None) – Logger instance for logging

  • log_level (str) – Level of the logger

  • display_unit (WavelengthUnit) – Unit of the wavelength to display

Returns:

ModelGrid – Instance of :class:~ModelGrid

Examples

>>> grid = ModelGrid._from_file(path)

Notes

Authors: Allan Denis

save_grid(store_path)[source]#

Save the grid to a specified directory.

Parameters:

store_path (str | PathLike) – Path where to store the grid

Return type:

None

Notes

Authors: Simon Petrus, Paulina Palma-Bifani, Matthieu Ravet and Allan Denis

Grid Loader#

Responsible for loading and validating NetCDF grid files.

class ForMoSA.grid.grid_loader.GridLoader[source]#

Bases: object

Class responsible for Grid loading.

Notes

Authors: Allan Denis

SubGrid Base#

Abstract base class for spectroscopic and photometric subgrids.

Inheritance diagram of ForMoSA.grid.subgrid_spectroscopy.SubGridSpectroscopy, ForMoSA.grid.subgrid_photometry.SubGridPhotometry
class ForMoSA.grid.subgrid_base.SubGrid(grid, parent_grid, logger=None, log_level='INFO', name='Unknown', display_unit=WavelengthUnit.MICROMETER)[source]#

Bases: ModelGrid, ABC

Base class for any subgrid (spectroscopic or photometric). Inherits from the ModelGrid class.

Parameters:
  • grid (Dataset) – Grid

  • parent_grid (ModelGrid) – Parent model grid

  • target_resolution (str | float) – Target resolution to reach for the model (‘obs’, ‘mod’ or float)

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the Logger

  • name (str) – Name of the subgrid

  • display_unit (WavelengthUnit) – Unit of the wavelength

Notes

Authors: Allan Denis

abstract property GridType: <property object at 0x7d7380ea7150>#

Grid type (spectroscopic or photometric).

abstract property relevant_parameter_kinds: list[ParameterKind]#

List of relevant parameter kinds the subgrid applies to.

property parent_grid: ModelGrid#

Parent grid.

property suffix: str#

Suffix used for saving.

property name: str#

Name of the subgrid.

property grid_name: str#

Name of the grid.

property wavelength_range: tuple#

Wavelength range of the subgrid.

property unit: PrefixUnit#

Unit of the wavelength to display.

property is_spectroscopic: bool#

Whether subgrid is spectroscopic.

property is_photometric: bool#

Whether subgrid is photometric.

classmethod from_dataset(ds, parent_grid, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER)[source]#

Generate SubGrid from dataset.

Parameters:
  • ds (Dataset) – Dataset containing the parameters of the subgrid

  • parent_grid (ModelGrid) – Parent model grid

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the Logger

  • display_unit (WavelengthUnit)

Return type:

SubGrid

Returns:

‘SubGrid’ – An instance of class SubGrid

Notes

Authors: Allan Denis

classmethod from_file(path, parent_grid, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER)[source]#

Generate SubGrid from file.

Parameters:
Return type:

SubGrid

Returns:

‘SubGrid’ – An instance of class SubGrid

Notes

Authors: Allan Denis

adapt_grid(backend='loky', n_jobs=-1)[source]#

Adapt the entire grid to the observation.

Parameters:
  • backend (str) – Joblib parallel backend. Built-in options: ‘loky’ (default), ‘multiprocessing’, ‘threading’, ‘sequential’. Third-party: ‘dask’, ‘ray’.

  • n_jobs (int) – Number of parallel jobs. -1 uses all available CPUs. Passed to joblib.Parallel.

Return type:

None

Notes

Authors: Arthur Vigan and Allan Denis

evaluate_at_gridpoints(grid_params, interp_method='linear')[source]#

Evaluate model given a list of parameters and their associated values.

Parameters:
Return type:

ObservedModel

Returns:

observed_model (ObservedModel) – Instance of class ObservedModel

Notes

Authors: Allan Denis

SubGrid Set#

Container for a collection of adapted subgrids.

class ForMoSA.grid.subgrid_set.SubGridSet(parent_grid, logger=None, log_level='INFO')[source]#

Bases: object

Container for a set of subgrids (spectroscopic or photometric).

Can be initialized empty or directly from an ObservationSet.

Parameters:
  • parent_grid (ModelGrid) – Parent model grid

  • logger (Logger | None) – Logger

  • log_level (str) – Logger level

Notes

Authors: Allan Denis

property logger: Logger#

Logger.

property parent_grid: ModelGrid#

Parent grid.

property subgrids: list[SubGrid]#

List of adapted subgrids.

property n_subgrids: int#

Number of subgrids.

property is_empty: bool#

Whether the subgrid set is empty.

property has_spectroscopy: bool#

Whether the subgrid set contains spectroscopic subgrids.

property has_photometry: bool#

Whether the subgrid set contains photometric subgrids.

property spectroscopic_subgrids: list[SubGridSpectroscopy]#

List of spectroscopic subgrids.

property photometric_subgrids: list[SubGridPhotometry]#

List of photometric subgrids.

property wavelength_range: tuple#

Global wavelength range.

property subgrid_names: list[str]#

List of grid names.

classmethod from_path(path, parent_grid, logger=None, log_level='INFO')[source]#

Generate an instance of SubGridSet from the folder containing all the subgrids.

Parameters:
  • path (str | PathLike) – Path containing the subgrids

  • parent_grid (ModelGrid) – Instance of ModelGrid corresponding to the parent grid

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the Logger

Return type:

SubGridSet

Returns:

“SubGridSet” – Instance of SubGridSet

Notes

Authors: Allan Denis

add_subgrid(*args, **kwargs)[source]#

Add a subgrid to the set based on the type of data provided.

Parameters:

args

  • If a SubGrid object is provided, directly add it

  • If a .nc file is provided, provide a single argument (str | Path)

  • If an xr.Dataset is provided, provide a single argument

Return type:

None

Notes

Authors: Allan Denis

interpolate_all(method)[source]#

Interpolate missing values of all the subgrids.

Parameters:

method (str) – Interpolation method

Return type:

None

Notes

Authors: Allan Denis

save_all(path)[source]#

Save all the grids of the set of subgrids to the directort store_path.

Parameters:
Return type:

None

Notes

Authors: Allan Denis

Spectroscopic SubGrid#

class ForMoSA.grid.subgrid_spectroscopy.SubGridSpectroscopy(grid, parent_grid=None, remove_continuum=False, wave_cont=None, res_cont=None, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER, name='Unknown')[source]#

Bases: SubGrid

Spectral subgrid class, which implements adaptation to a specific wavelength and resolution.

Parameters:
  • grid (Dataset) – Grid

  • parent_grid (ModelGrid | None) – Parent model grid

  • remove_continuum (bool) – Whether to remove the continuum

  • wave_cont (str | None) – Wavelengths for continuum removal (‘window1 / window2 / windw3 / …’ where window{i} = ‘wave{i}, wave{i+1}’)

  • res_cont (float | None) – Resolutions for continuum removal

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the logger

  • name (str) – Name of the subgrid

  • display_unit (WavelengthUnit)

Notes

Authors: Allan Denis

property GridType: ObservationType#

Observation type.

property wave_cont: str | ndarray | None#

Wavelengths for continuum removal.

property res_cont: float | None#

Resolutions for continuum removal.

property remove_continuum: bool#

Whether to remove continuum.

property relevant_parameter_kinds: list[ParameterKind]#

List of relevant parameter kinds the observation applies to.

classmethod from_parent(parent_grid, target_wavelength, target_resolution, remove_continuum=False, wave_cont=None, res_cont=None, name='unknown', logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER, backend='loky', n_jobs=-1)[source]#

Build spectroscopic subgrid from the parent grid, target_wavelength and target resolution to reach.

Parameters:
  • parent_grid (ModelGrid) – Instance of ModelGrid

  • target_wavelength (ndarray) – Target wavelength to reach for the subgrid

  • target_resolution (ndarray) – Target resolution to reach for the subgrid

  • remove_continuum (bool) – Whether to remove the continuum

  • wave_cont (ndarray | None) – Wavelengths used for the continuum

  • res_cont (float | None) – Resolution used for the continuum

  • name (str) – Name of the subgrid

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the Logger

  • display_unit (WavelengthUnit) – Unit to display for the wavelength

  • backend (str)

  • n_jobs (int)

Return type:

SubGridSpectroscopy

Returns:

SubGridSPectroscopy – Instance of SubGridSpectroscopy

Examples

>>> subgrid = SubGridSpectroscopy.from_parent(parent_grid, target_wavelength, target_resolution, remove_continuum, wave_cont, res_cont, name, logger, log_level, display_unit)

Notes

Authors: Allan Denis

classmethod from_grid(ds, parent_grid, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER)[source]#

Retrieve spectorscopic subgrid from grid.

Parameters:
  • dx (xr.Dataset) – Grid data

  • parent_grid (ModelGrid) – Instance of ModelGrid

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the Logger

  • display_unit (WavelengthUnit) – Unit to display for the wavelength

  • ds (Dataset)

Return type:

SubGridSpectroscopy

Returns:

SubGridSpectroscopy – Instance of SubGridSpectroscopy

Examples

>>> subgrid = SubGridSpectroscopy.from_grid(grid, parent_grid, logger, log_level, display_unit)

Notes

Authors: Allan Denis

adapt(backend='loky', n_jobs=-1)[source]#

Adapt the native grid to the target wavelength and resolution. Optionally remove the continuum.

Return type:

None

Parameters:

Photometric SubGrid#

class ForMoSA.grid.subgrid_photometry.SubGridPhotometry(grid, parent_grid, Filter, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER, name='Unknown')[source]#

Bases: SubGrid

Photometric subgrid class, which implements adaptation to a specific filter.

Parameters:
  • grid (Dataset) – Dataset containing the subgrid

  • parent_grid (ModelGrid) – Parent model grid

  • Filter (ndarray[PhotometryFilter]) – Instance of :class:~PhotometryFilter corresponding to the photometric filter

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the logger

  • display_unit (WavelengthUnit) – Unit of the wavelength to display

  • name (str) – Name of the subgrid

Notes

Authors: Allan Denis

property GridType: ObservationType#

Observation type.

property wave_cont: ndarray | None#

Wavelengths for continuum removal.

property res_cont: float | None#

Resolutions for continuum removal.

property remove_continuum: bool#

Whether to remove continuum.

property Filter: ndarray[PhotometryFilter]#

Filter.

property relevant_parameter_kinds: list[ParameterKind]#

List of relevant parameter kinds the subgrid applies to.

classmethod from_parent(parent_grid, Filter, name='unknown', logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER, backend='loky', n_jobs=-1)[source]#

Build Photometric subgrid from the parent grid, target_wavelength.

Parameters:
  • parent_grid (ModelGrid) – Instance of ModelGrid

  • Filter (ndarray[PhotometryFilter]) – Arrays containing instances of class PhotometryFilter corresponding to the photometric filter

  • name (str) – Name of the subgrid

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the Logger

  • display_unit (WavelengthUnit) – Unit to display for the wavelength

  • backend (str)

  • n_jobs (int)

Return type:

SubGridPhotometry

Returns:

SubGridPhotometryy – Instance of SubGridPhotometry

Examples

>>> subgrid = SubGridPhotometry.from_parent(parent_grid, Filter, name, logger, log_level, display_unit)

Notes

Authors: Allan Denis

classmethod from_grid(ds, parent_grid, logger=None, log_level='INFO', display_unit=WavelengthUnit.MICROMETER)[source]#

Retrieve photometric subgrid from grid.

Parameters:
  • dx (xr.Dataset) – Grid data

  • parent_grid (ModelGrid) – Instance of ModelGrid

  • logger (Logger | None) – Logger

  • log_level (str) – Level of the Logger

  • display_unit (WavelengthUnit) – Unit to display for the wavelength

  • ds (Dataset)

Return type:

SubGridPhotometry

Returns:

SubGridPhotometry – Instance of SubGridPhotometry

Examples

>>> subgrid = SubGridPhotometry.from_grid(ds, parent_grid, logger, log_level, display_unit)

Notes

Authors: Allan Denis

adapt(backend='loky', n_jobs=-1)[source]#

Adapt the native grid to the target wavelength and resolution.

Notes

Authors: Allan Denis

Return type:

None

Parameters:
integrate_filter_curve(model_to_adapt, print_logger=False)[source]#

Method to integrate the filter curve on a spectrum

Parameters:
  • model_to_adapt (DataArray) – Model to integrate

  • print_logger (bool)

Return type:

float

Returns:

xr.DataArray – Integrated value under the filter curve

Notes

Authors: Allan Denis