Grids

A “Grid” in spexxy is anything that provides any kind of data in a regularly spaced parameter space. The base class for all Grids is spexxy.grid.Grid, which also defines some convenience methods.

The usual way of getting data from a grid is by calling it with the requested parameters:

grid = Grid()
data = grid((3.14, 42.))

A class inheriting from Grid must call Grid’s constructor with a list of GridAxis objects that describe the axes of the grid, i.e. their names and possbile values. Furthermore it must overwrite all necessary methods, in particular __call__(), __contains__(), and all(). See the implementation of ValuesGrid for a simple example.

spexxy comes with two pre-defined grids:

  • ValuesGrid defines a simple grid, for which the values are defined in its constructor. This grid is mainly used for unit tests.
  • FilesGrid is a grid, where each “value” is a spectrum in its own file. A CSV file must be provided containing filenames and all parameters.

Grid

class spexxy.grid.Grid(axes: List[spexxy.grid.grid.GridAxis], *args, **kwargs)

Base class for all grids in spexxy.

__call__(params: Tuple) → Any

Fetches the value for the given parameter set

Parameters:params – Parameter set to catch value for.
Returns:Grid value at given position.
__contains__(params: Tuple) → bool

Checks, whether the grid contains a given parameter set.

Parameters:params – Parameter set to check.
Returns:Whether or not the given parameter set exists in the grid.
__init__(axes: List[spexxy.grid.grid.GridAxis], *args, **kwargs)

Initialize a new Grid.

Parameters:axes – List of axes for this grid.
all() → List[Tuple]

Return all possible parameter combinations.

Returns:All possible parameter combinations.
axes() → List[spexxy.grid.grid.GridAxis]

Returns information about the axes.

Returns:List of GridAxis objects describing the grid’s axes.
axis_name(axis: int) → str

Returns name of given axis.

Parameters:axis – Index of axis to return values for.
Returns:Name of given axis.
axis_names() → List[str]

Returns names of all axis.

Returns:Names of all axis.
axis_values(axis: int) → List[float]

Returns all possible values for the given axis.

Parameters:axis – Index of axis to return values for.
Returns:All possible values for the given axis.
create_array() → Any

In case the values provided by this grid are of an array type, this method creates an empty array that can be filled.

Returns:Empty array element of same type as values in the grid.
static load(filename: str) → spexxy.grid.grid.Grid

Tries to identify type of grid and loads it.

Parameters:filename – Name of file to load.
nearest(params, scales=None) → Tuple

Finds the nearest point within the grid.

Calculates distance between given params and all points p in the grid as the sum over all elements in the parameter tuple of ((params - grid_point) * scales)**2.

Parameters:
  • params – Parameters to find nearest point to.
  • scales – If given, scales dimensions.
Returns:

Parameters for nearest point in grid.

neighbour(params: Tuple, axis: int, distance: int = 1, must_exist: bool = False) → Tuple

Finds a neighbour on the given axis for the given value in the given distance.

Parameters:
  • params – Parameter tuple to search neighbour from.
  • axis – Axis to search for
  • distance – Distance in which to find neighbour. >0: Find larger neighbours, i.e. 0 next larger value, 1 the one after that, etc <=0: Find smaller neighbours, i.e. 0 next smaller value (or value itself), -1 the before that, etc
  • must_exist – Grid point with new parameter set must actually exist.
Returns:

New parameter tuple with neighbour on the given axis.

num_axes() → int

Returns number of axes.

Returns:Number of axes in grid.

GridAxis

class spexxy.grid.GridAxis(name: str, values: List[T] = None, min: float = None, max: float = None, initial: float = None)

Description of a single axis in a grid.

__init__(name: str, values: List[T] = None, min: float = None, max: float = None, initial: float = None)

Initialize a new grid axis.

Parameters:
  • name – Name of new axis.
  • values – List of all possible values for this axis.
  • min – Minimum value for this axis.
  • max – Maximum value for this axis.
  • initial – Initial guess for this axis.
__weakref__

list of weak references to the object (if defined)

neighbour(value: float, distance: int = 1) → float

Finds a neighbour in this axis for the given value in the given distance.

Parameters:
  • value – Value to find neighbour for.
  • distance – Distance in which to find neighbour >0: Find larger neighbours, i.e. 0 next larger value, 1 the one after that, etc <=0: Find smaller neighbours, i.e. 0 next smaller value (or value itself), -1 the before that, etc
Returns:

Value on grid in the given distance to the given value. If given value is on grid, distance is counted from that value.

Raises:

KeyError – If no neighbour has been found.

ValuesGrid

class spexxy.grid.ValuesGrid(axes: List[spexxy.grid.grid.GridAxis], values: Dict[Tuple, Any], *args, **kwargs)

Basic Grid that gets its values from the constructor. Mainly used for testing.

__call__(params: Tuple) → Any

Fetches the value for the given parameter set

Parameters:params – Parameter set to catch value for
Returns:Grid value at given position
__contains__(params: Tuple) → bool

Checks, whether the grid contains a given parameter set.

Parameters:params – Parameter set to check.
Returns:Whether or not the given parameter set exists in the grid.
__init__(axes: List[spexxy.grid.grid.GridAxis], values: Dict[Tuple, Any], *args, **kwargs)

Constructs a new Grid.

Parameters:
  • axes – A list of axes to build the grid from.
  • values – A dictionary where the keys are the parameters and the values the values at that position in the grid.
all() → List[Tuple]

Return all possible parameter combinations.

Returns:All possible parameter combinations.

FilesGrid

class spexxy.grid.FilesGrid(filename: str, norm_to_mean: bool = False, *args, **kwargs)

Grid working on files with a CSV based database.

__call__(params: Tuple) → Any

Fetches the value for the given parameter set.

Parameters:params – Parameter set to catch value for.
Returns:Grid value at given position.
__contains__(params: Tuple) → bool

Checks, whether the grid contains a given parameter set.

Parameters:params – Parameter set to check.
Returns:Whether or not the given parameter set exists in the grid.
__init__(filename: str, norm_to_mean: bool = False, *args, **kwargs)

Constructs a new Grid.

Parameters:
  • filename – Filename of CSV file.
  • norm_to_mean – Normalize spectra to their mean.
all() → List[Tuple]

Return all possible parameter combinations.

Returns:All possible parameter combinations.
filename(params: Tuple, absolute: bool = True) → str

Returns filename for given parameter set.

Parameters:
  • params – Parameter set to catch value for.
  • absolute – If True, return full absolute path, otherwise relative path within grid.
Returns:

Filename.

SynspecGrid

class spexxy.grid.SynspecGrid(synspec: str, models: spexxy.grid.grid.Grid, linelist: str, mollist: str, datadir: str, range: Tuple[float, float], vturb: Union[str, float] = 2.0, elements: List[str] = None, input: Union[str, spexxy.grid.grid.Grid] = None, imode: int = 10, idstd: int = 0, iprin: int = 0, inmod: int = 0, intrpl: int = 0, ichang: int = 0, ichemc: int = 1, iophli: int = 0, nunalp: int = 0, nunbet: int = 0, nungam: int = 0, nunbal: int = 0, ifreq: int = 1, inlte: int = 0, icontl: int = 0, inlist: int = 0, ifhe2: int = 0, ihydpr: int = 1, ihe1pr: int = 0, ihe2pr: int = 0, cutof0: int = 40, cutofs: int = 0, relop: float = 1e-05, space: float = 0.03, normalize: bool = False, nstfile: str = 'nstf', nd: int = None, ifmol: int = 1, tmolim: float = None, ippick: int = None, ibfac: int = None, tempdir: str = None, solar_abund: Dict[str, float] = None, *args, **kwargs)

Synthesizes a new spectrum with Synspec at given grid positions.

__call__(params: Tuple) → Any

Fetches the value for the given parameter set.

Parameters:params – Parameter set to catch value for.
Returns:Grid value at given position.
__contains__(params: Tuple) → bool

Checks, whether the grid contains a given parameter set.

Parameters:params – Parameter set to check.
Returns:Whether or not the given parameter set exists in the grid.
__init__(synspec: str, models: spexxy.grid.grid.Grid, linelist: str, mollist: str, datadir: str, range: Tuple[float, float], vturb: Union[str, float] = 2.0, elements: List[str] = None, input: Union[str, spexxy.grid.grid.Grid] = None, imode: int = 10, idstd: int = 0, iprin: int = 0, inmod: int = 0, intrpl: int = 0, ichang: int = 0, ichemc: int = 1, iophli: int = 0, nunalp: int = 0, nunbet: int = 0, nungam: int = 0, nunbal: int = 0, ifreq: int = 1, inlte: int = 0, icontl: int = 0, inlist: int = 0, ifhe2: int = 0, ihydpr: int = 1, ihe1pr: int = 0, ihe2pr: int = 0, cutof0: int = 40, cutofs: int = 0, relop: float = 1e-05, space: float = 0.03, normalize: bool = False, nstfile: str = 'nstf', nd: int = None, ifmol: int = 1, tmolim: float = None, ippick: int = None, ibfac: int = None, tempdir: str = None, solar_abund: Dict[str, float] = None, *args, **kwargs)

Constructs a new Grid.

Parameters:
  • synspec – Full path to synspec exectuble
  • models – Grid with model atmospheres
  • linelist – File with line list
  • mollist – File with molecular list
  • datadir – Name of data directory
  • range – Tuple of start/end wavelenghts
  • vturb – Either the microturbulence or a CSV file containing a table
  • elements – List of elements to add as new axis
  • input – Either the name of a fort.5 file or a Grid or None (in which case an automatic fort.5 will be used)
  • parameters – Use this fort.55 file instead of the automatically generated one
  • imode
  • idstd
  • iprin
  • inmod
  • intrpl
  • ichang
  • ichemc
  • iophli
  • nunalp
  • nunbet
  • nungam
  • nunbal
  • ifreq
  • inlte
  • icontl
  • inlist
  • ifhe2
  • ihydpr
  • ihe1pr
  • ihe2pr
  • cutof0
  • cutofs
  • relop
  • space
  • normalize – Normalize spectra
  • nstfile – Name of file with non-standard flags
  • nd
  • ifmol
  • tmolim
  • ippick
  • ibfac
  • tempdir – Temporary directory. Won’t be delete if given.
  • solar_abund – Dictionary with solar abundances to use.
all() → List[Tuple]

Return all possible parameter combinations.

Returns:All possible parameter combinations.
filename(params: Tuple) → str

Returns filename for given parameter set.

Parameters:params – Parameter set to catch value for.
Returns:Filename.