dustapprox.tools package

Contents

dustapprox.tools package#

Submodules#

dustapprox.tools.downloader module#

A simple tool to download files from URLs

download_file(link, file_name, overwrite=False)[source]#

Download a file on disk from url

Parameters:
  • link (str) – url of the file

  • file_name (str) – path and filename of the download location

  • overwrite (bool) – set to re-download (default False)

Return type:

Returns the filename of the data

dustapprox.tools.generate_model module#

Tools to generate pre-computed models from atmosphere grids

This module provides functions to generate photometric grids from atmosphere models, train polynomial models on these grids, and export the trained models to ECSV files.

class GridParameters[source]#

Bases: object

Parameters for generating a photometric grid .. attribute:: model_pattern

The pattern to identify the atmosphere models.

type:

str

pbset#

The list of passbands to compute.

Type:

List[str] | List[Filter]

atmosphere_name#

The name of the atmosphere set.

Type:

Optional[str]

extinction_curve#

The extinction curve to use (from dust_extinction).

Type:

Union[str, BaseExtRvModel]

A0#

Array of A0 values to use.

Type:

Optional[npt.NDArray]

R0#

Array of R0 values to use.

Type:

Optional[npt.NDArray]

apfields#

Additional atmosphere parameters to include in the output.

Type:

Optional[List[str]]

n_jobs#

Number of parallel jobs to use (-1 uses all available cores).

Type:

int

A0: ndarray[tuple[Any, ...], dtype[_ScalarT]] | None = None#
R0: ndarray[tuple[Any, ...], dtype[_ScalarT]] | None = None#
__init__(model_pattern, pbset, atmosphere_name=None, atmosphere_shortname=None, extinction_curve='F99', A0=None, R0=None, apfields=None, n_jobs=-1)#
Parameters:
  • model_pattern (str)

  • pbset (List[str] | List[Filter])

  • atmosphere_name (str | None)

  • atmosphere_shortname (str | None)

  • extinction_curve (str | BaseExtRvModel)

  • A0 (ndarray[tuple[Any, ...], dtype[_ScalarT]] | None)

  • R0 (ndarray[tuple[Any, ...], dtype[_ScalarT]] | None)

  • apfields (List[str] | None)

  • n_jobs (int)

Return type:

None

apfields: List[str] | None = None#
atmosphere_name: str | None = None#
atmosphere_shortname: str | None = None#
copy()[source]#

Create a copy of the current GridParameters instance

Returns:

A new instance of GridParameters with the same attributes.

Return type:

GridParameters

extinction_curve: str | BaseExtRvModel = 'F99'#
generate_grid(output_path)[source]#

Shortcut to generate the photometric grid based on the parameters

seealso:: generate_grid()

Returns:

DataFrame with the computed grid

Return type:

pd.DataFrame

Parameters:

output_path (str | Path)

model_pattern: str = <dataclasses._MISSING_TYPE object>#
n_jobs: int = -1#
pbset: List[str] | List[Filter] = <dataclasses._MISSING_TYPE object>#
class ModelParameters[source]#

Bases: object

Parameters for training a model

output_fname#

The output filename for the trained models.

Type:

Union[str, pathlib.Path]

features#

The list of features to use for training.

Type:

Union[List[str], List[LiteralString]]

model_kwargs#

additional parameters to the model

Type:

dict

__init__(output_fname, features, kwargs=None)#
Parameters:
  • output_fname (str | Path)

  • features (List[str] | List[LiteralString])

  • kwargs (Dict | None)

Return type:

None

features: List[str] | List[LiteralString] = <dataclasses._MISSING_TYPE object>#
kwargs: Dict | None = None#
output_fname: str | Path = <dataclasses._MISSING_TYPE object>#
export_trained_model_to_ecsv(fname, models, **kwargs)[source]#

Export a collection of models :param fname: The name of the file to write. :type fname: str :param models: The list of models to export. :type models: Sequence[_BaseModel] :param **kwargs: Additional metadata to attach to the resulting ECSV file. :type **kwargs: keyword arguments

Return type:

None

Parameters:
  • fname (str | Path)

  • models (Sequence[BaseModel])

generate_grid(model_pattern, grid_fname, pbset, atmosphere_name=None, extinction_curve='F99', A0=None, R0=None, apfields=None, n_jobs=-1)[source]#

Generate the photometric grid from models :param model_pattern: Pattern to identify the atmosphere models :type model_pattern: str :param grid_fname: Output filename for the grid :type grid_fname: Union[str, pathlib.Path] :param pbset: List of passbands to compute :type pbset: List[str] :param atmosphere_name: Name of the atmosphere set :type atmosphere_name: Optional[str] :param extinction_curve: Extinction curve to use (from dust_extinction) :type extinction_curve: Union[str, BaseExtRvModel] :param A0: Array of A0 values to use :type A0: Optional[npt.NDArray] :param R0: Array of R0 values to use :type R0: Optional[npt.NDArray] :param apfields: Additional atmosphere parameters to include in the output :type apfields: Optional[Sequence[str]] :param n_jobs: Number of parallel jobs to use (-1 uses all available cores) :type n_jobs: int

Returns:

DataFrame with the computed grid

Return type:

pd.DataFrame

Parameters:
  • model_pattern (str)

  • grid_fname (str | Path)

  • pbset (List[str] | List[Filter])

  • atmosphere_name (str | None)

  • extinction_curve (str | BaseExtRvModel)

  • A0 (ndarray[tuple[Any, ...], dtype[_ScalarT]] | None)

  • R0 (ndarray[tuple[Any, ...], dtype[_ScalarT]] | None)

  • apfields (Sequence[str] | None)

  • n_jobs (int)

main_example()[source]#

Generate a pre-computed polynomial model library end-to-end

Return type:

PrecomputedModel

train_polynomial_model(r, output_fname, features, *, degree=3)[source]#

Train polynomial models for the given features from the photometric grid :param r: The photometric grid DataFrame. :type r: pd.DataFrame :param output_fname: The output filename for the trained models. :type output_fname: Union[str, pathlib.Path] :param features: The list of features to use for training. :type features: Union[List[str], List[LiteralString]] :param degree: The degree of the polynomial to fit. :type degree: int

Returns:

The list of trained polynomial models.

Return type:

List[polynomial.PolynomialModel]

Parameters:
  • r (DataFrame)

  • output_fname (str | Path)

  • features (List[str] | List[LiteralString])

  • degree (int)

dustapprox.tools.grid module#

Generate a grid of models with extinction from an atmosphere library

Example of script that produces a grid of dust attenuated stellar models from an atmosphere library.

This example can run in parallel on multiple processes or cores.

compute_photometric_grid(sources='models/Kurucz2003all/*.fl.dat.txt', which_filters=None, extinction_curve='F99', A0=None, R0=None, apfields=None, n_jobs=1, verbose=0, atmosphere_name=None, **kwargs)[source]#

Run the computations of the photometric grid in parallel

Parameters:
  • sources (str) – pattern of atmospehric models to process (using glob syntax)

  • which_filters (Sequence[Union[str, UnitFilter]], optional) – list of filter names or UnitFilter instances to compute photometry for. If None, use a default set of filters.

  • extinction_curve (Union[str, BaseExtRvModel], optional, default="F99") – Name of the extinction curve to use from dust_extinction or an instance of BaseExtRvModel.

  • A0 (npt.NDArray, optional) – array of A0 values to compute (default: from 0.01 to 20.0 in steps of 0.1)

  • R0 (npt.NDArray, optional) – array of R0 values to compute (default: [2.3, 2.6, 3.1, 3.6, 4.1, 4.6, 5.1])

  • apfields (Sequence[str], optional) – list of atmospheric parameter field names to extract from the atmosphere files. If None, use (‘teff’, ‘logg’, ‘feh’, ‘alpha’)

  • n_jobs (int) – number of parallel processes to run (default: 1, -1 for as many as CPUs)

  • verbose (int) – verbosity level (default: 0)

  • atmosphere_name (str, optional) – Name of the atmosphere model set used. If None, use the sources pattern.

  • **kwargs (keyword arguments) – Additional metadata to attach to the resulting DataFrame

Returns:

Dataframe with the photometric values for each passband

Return type:

pd.DataFrame

dustapprox.tools.model_characteristics module#

Generates some characteristic model plots.

class ModelCharacteristicsRunner[source]#

Bases: object

Class to run model characteristics plots for a set of precomputed models.

__init__(library_path, library_selection, testdata_path, _what=<factory>, _vega_zpt=<factory>, vega_spec_approx=None, _data=None)#
Parameters:
  • library_path (str)

  • library_selection (dict[str, Any])

  • testdata_path (str)

  • _what (list[ModelInfo])

  • _vega_zpt (dict[str, float])

  • vega_spec_approx (SVOSpectrum | None)

  • _data (DataFrame | None)

Return type:

None

property data: DataFrame#

Data grid with photometric values and stellar parameters to test on Load the test data grid if not already done.

library_path: str = <dataclasses._MISSING_TYPE object>#

which precomputed model library path to use

library_selection: dict[str, Any] = <dataclasses._MISSING_TYPE object>#

Selection criteria to select the precomputed model library

models()[source]#

Return a generator on the selected models.

Return type:

Generator[BaseModel, None, None]

passbands()[source]#

Return a generator on the passbands of the selected models.

Return type:

Generator[str, None, None]

plot_1d_residuals(close_figure=True, **kwargs)[source]#

Plot 1D residuals for each selected model.

Parameters:

close_figure (bool, optional) – Whether to close the figure after saving (default: True)

Return type:

None

:param : :type : see also: plot_quick_diagnostic_1d

plot_2d_residuals(close_figure=True, **kwargs)[source]#

Plot 2D residuals for each selected model.

Parameters:

close_figure (bool, optional) – Whether to close the figure after saving (default: True)

Return type:

None

:param : :type : see also: imshow_binned_statistic_2d

set_vega_zeropoints(vega_spec)[source]#

Compute Vega zeropoints for each passband in the selected models.

Parameters:

vega_spec (svo.SVOSpectrum) – An approximate Vega spectrum for zeropoint calculations

Return type:

None

testdata_path: str = <dataclasses._MISSING_TYPE object>#

Path to the test data photometric grid

vega_spec_approx: SVOSpectrum | None = None#

An approximate Vega spectrum for zeropoint calculations

property what: Sequence[ModelInfo]#

Which models to run.

run_model_characteristics(*, passbands='Gaia', extinction_curve='F99', plot_1d=True, plot_2d=True, close_figure=True)[source]#

Run the model characteristics plots for a given set of passbands and extinction curve.

This function generates a small photometric test grid if not already existing, and then produces 1D and/or 2D residual plots for the selected passbands.

Parameters:
  • passbands (str) – Name of the passband set to use (e.g., “Gaia”, “Generic”), this corresponds to dustapprox.models.PrecomputedModel.find() passband argument.

  • extinction_curve (str) – Name of the extinction curve to use (e.g., “F99”, “G23”), this corresponds to dustapprox.models.PrecomputedModel.find() extinction argument and dustapprox.tools.generate_model.GridParameters extinction_curve field.

  • plot_1d (bool) – Whether to generate 1D residual plots (default: True).

  • plot_2d (bool) – Whether to generate 2D residual plots (default: True).

  • close_figure (bool) – Whether to close figures after saving (default: True).

Module contents#

Convenient tools to generate models and plots.