galapy.Handlers

Handlers objects allow to manage the free-parameters and their priors in a sampling run.

Classes

GXYParameters(gxy_model, sample_params[, rng_kw])

A class to handle model parameters specifically for galapy.galaxie.GXY() type objects.

ModelParameters(*args[, sample_params, rng_kw])

A class to handle model parameters for GalaPy objects of type Model.

NoiseParameters(noise_model, sample_params)

A class to handle model parameters specifically for galapy.Noise.Noise() type objects.

class galapy.Handlers.ModelParameters(*args, sample_params={}, rng_kw={})

A class to handle model parameters for GalaPy objects of type Model.

This class provides methods for setting up and handling model parameters, both fixed and free, for galaxy and noise models.

Parameters:
  • *args (galapy.internal.abc.Model) – The galapy.Galaxy.GXY and/or galapy.Noise.Noise models to extract parameters from.

  • sample_params (dict, optional) – A dictionary containing fixed and free parameter values for sampling. The keys are parameter names, and the values are either fixed values or tuples containing (prior_distribution, log_prior), where prior_distribution is a 2-elements iterables defining the limits of the uniform prior and log_prior is a boolean defining whether the prior has to be considered logarithmic (base 10) or not.

  • rng_kw (dict, optional) – Keyword arguments to be passed to numpy.random.default_rng().

parameters

A dictionary containing all model parameters.

Type:

dict

par_free

An array containing names of free parameters.

Type:

numpy.ndarray

par_log

An array containing booleans defining which of the free parameters are treated logarithmically.

Type:

numpy.ndarray

par_prior

An array containing prior distributions for free parameters.

Type:

numpy.ndarray

Examples

>>> from galapy.Galaxy import GXY
>>> from galapy.Handlers import ModelParameters

Instantiate a galaxy model

>>> gxy = GXY()

Define sample parameters

>>> sample_params = {'galaxy.age': 1.e+8, 'galaxy.sfh.psi_max': ([0., 5.], True)}

Create ModelParameters instance

>>> params = ModelParameters(gxy, sample_params)

Return nested dictionary of parameters

>>> nested = params.return_nested()
>>> gxy.set_parameters( **nested['galaxy'] )

Dump instance to a dictionary

>>> dumped_params = handle.dump()

Build instance from dumped dictionary

>>> handle2 = ModelParameters.load(dumped_params)

the 2 handlers are now equivalent:

>>> handle.return_nested() == handle2.return_nested()
True

but they are not the same object

>>> handle is handle2
False
classmethod load(dictionary)

Build a ModelParameters instance loading from a dictionary.

Parameters:

dictionary (dict) – A formatted dictionary containing stored info from which to define the new ModelParameters instance.

Returns:

A ModelParameters instance with loaded parameters.

Return type:

ModelParameters

Raises:

KeyError – If an element of the input dictionary is not an attribute of the class.

dump()

Dump a ModelParameters instance to a dictionary.

Returns:

A dictionary containing dumped attributes of the current instance.

Return type:

dict

return_nested(par=None)

From a list of parameters returns a nested dictionary in the proper format. Here ‘proper’ means that such dictionary has the hierarchy necessary to pass it as keyword arguments dictionary to an object of type galapy.internal.abc.Model()

Parameters:

par (array-like or iterable) – a list of values to be assigned to the parameters flagged as free

Returns:

A nested dictionary with the proper format to be passed as keyword arguments to the constructor or to function set_parameters of objects of type galapy.internal.abc.Model().

Return type:

dict

Examples

Given a galaxy model of type GXY:

>>> from galapy.Galaxy import GXY
>>> gxy = GXY()

and a dictionary with the fixed and free parameters as follows

>>> p = { 'age' : 1.e+8, 'sfh.psi_max' : ( [0., 5.], True ) }

we can build a parameter handler:

>>> from galapy.GalaxyParameters import GXYParameters
>>> par = GXYParameters( gxy, p )
>>> par.par_free
numpy.array( [ 'sfh.psi_max' ] )

Now, assuming we want to set a new value to the ‘sfh.psi_max’ parameter,

>>> nested = par.return_nested( [ 123. ] )
>>> nested
{ 'sfh' : { 'psi_max' : 123. } }

This dictionary can be used to set the parameters of the galaxy model

>>> gxy.set_parameters( **nested )
class galapy.Handlers.GXYParameters(gxy_model, sample_params, rng_kw={})

A class to handle model parameters specifically for galapy.galaxie.GXY() type objects.

This class inherits from ModelParameters and provides methods tailored for galaxy models.

Parameters:
  • gxy_model (galapy.Galaxy.GXY) – The galaxy model to extract parameters from.

  • sample_params (dict) – A dictionary containing fixed and free parameter values for sampling. The keys are parameter names, and the values are either fixed values or tuples containing (prior_distribution, log_prior).

  • rng_kw (dict, optional) – Keyword arguments to be passed to numpy.random.default_rng().

return_nested(par)

Return model parameters as a nested dictionary specifically for galaxies.

Parameters:

par (array-like or iterable) – A list of values to be assigned to free parameters.

Returns:

A nested dictionary with galaxy model parameters.

Return type:

dict

class galapy.Handlers.NoiseParameters(noise_model, sample_params, rng_kw={})

A class to handle model parameters specifically for galapy.Noise.Noise() type objects.

This class inherits from ModelParameters and provides methods tailored for noise models.

Parameters:
  • noise_model (galapy.Noise.Noise) – The noise model to extract parameters from.

  • sample_params (dict) – A dictionary containing fixed and free parameter values for sampling. The keys are parameter names, and the values are either fixed values or tuples containing (prior_distribution, log_prior).

  • rng_kw (dict, optional) – Keyword arguments to be passed to numpy.random.default_rng().

return_nested(par)

Return model parameters as a nested dictionary specifically for noise.

Parameters:

par (array-like or iterable) – A list of values to be assigned to free parameters.

Returns:

A nested dictionary with noise model parameters.

Return type:

dict