Population class extension: gridcode module

Module containing the gridcode generation functions for the binarycpython package.

This class object is an extension to the population grid object

class binarycpython.utils.population_extensions.gridcode.gridcode(**kwargs)[source]

Bases: object

Extension to the population grid object that contains functionality to handle the metadata that will be put in the ensemble

add_grid_variable(name, parameter_name, longname, valuerange, samplerfunc, probdist, dphasevol=- 1, gridtype='centred', branchpoint=0, branchcode=None, precode=None, postcode=None, topcode=None, bottomcode=None, condition=None, index=None, dry_parallel=False)[source]

Function to add grid variables to the grid_options.

The execution of the grid generation will be through a nested for loop. Each of the grid variables will get create a deeper for loop.

The real function that generates the numbers will get written to a new file in the TMP_DIR, and then loaded imported and evaluated. beware that if you insert some destructive piece of code, it will be executed anyway. Use at own risk.

Parameters
  • name (str) –

    name of parameter used in the grid Python code. This is evaluated as a parameter and you can use it throughout the rest of the function

    Examples:

    name = 'lnM_1'
    

  • parameter_name (str) –

    name of the parameter in binary_c

    This name must correspond to a Python variable of the same name, which is automatic if parameter_name == name.

    Note: if parameter_name != name, you must set a

    variable in “precode” or “postcode” to define a Python variable called parameter_name

  • longname (str) –

    Long name of parameter

    Examples:

    longname = 'Primary mass'
    

  • range

    Range of values to take. Does not get used really, the samplerfunc is used to get the values from

    Examples:

    range = [math.log(m_min), math.log(m_max)]
    

  • samplerfunc (str) –

    Function returning a list or numpy array of samples spaced appropriately. You can either use a real function, or a string representation of a function call.

    Examples:

    samplerfunc = "self.const_linear(math.log(m_min), math.log(m_max), {})".format(resolution['M_1'])
    

  • precode (Optional[str]) –

    Extra room for some code. This code will be evaluated within the loop of the sampling function (i.e. a value for lnM_1 is chosen already)

    Examples:

    precode = 'M_1=math.exp(lnM_1);'
    

  • postcode (Optional[str]) – Code executed after the probability is calculated.

  • probdist (str) –

    Function determining the probability that gets assigned to the sampled parameter

    Examples:

    probdist = 'self.Kroupa2001(M_1)*M_1'
    

  • dphasevol (Union[str, int]) –

    part of the parameter space that the total probability is calculated with. Put to -1 if you want to ignore any dphasevol calculations and set the value to 1

    Examples:

    dphasevol = 'dlnM_1'
    

  • condition (Optional[str]) –

    condition that has to be met in order for the grid generation to continue

    Examples:

    condition = "self.grid_options['binary']==1"
    

  • gridtype (str) – Method on how the value range is sampled. Can be either ‘edge’ (steps starting at the lower edge of the value range) or ‘centred’ (steps starting at lower edge + 0.5 * stepsize).

  • dry_parallel (Optional[bool]) – If True, try to parallelize this variable in dry runs.

  • topcode (Optional[str]) – Code added at the very top of the block.

  • bottomcode (Optional[str]) – Code added at the very bottom of the block.

Return type

None

delete_grid_variable(name)[source]

Function to delete a grid variable with the given name.

Parameters

name (str) – name of the grid variable to be deleted.

Return type

None

rename_grid_variable(oldname, newname)[source]

Function to rename a grid variable.

note: this does NOT alter the order of the self.grid_options[“_grid_variables”] dictionary.

The order in which the grid variables are loaded into the grid is based on their grid_variable_number property

Parameters
  • oldname (str) – old name of the grid variable

  • newname (str) – new name of the grid variable

Return type

None

update_grid_variable(name, **kwargs)[source]

Function to update the values of a grid variable.

Parameters
  • name (str) – name of the grid variable to be changed.

  • **kwargs – key-value pairs to override the existing grid variable data. See add_grid_variable for these names.

Return type

None