tracts.demography.base_parametrized_demography

Classes

BaseFounderEvent(found_time, ...[, end_time])

BaseMigrationEvent(rate_parameter, ...)

BaseParametrizedDemography([name, min_time, ...])

FixedParametersHandler(logger)

FounderEvent(found_time, source_populations, ...)

class tracts.demography.base_parametrized_demography.BaseFounderEvent(found_time, source_populations, remainder_population, end_time=None)

Bases: ABC

__init__(found_time, source_populations, remainder_population, end_time=None)
abstractmethod execute(parametrized_demography, params)
class tracts.demography.base_parametrized_demography.BaseMigrationEvent(rate_parameter, source_population)

Bases: ABC

__init__(rate_parameter, source_population)
abstractmethod execute(parametrized_demography, migration_matrix, params)
class tracts.demography.base_parametrized_demography.BaseParametrizedDemography(name='', min_time=2, max_time=inf)

Bases: ABC

__init__(name='', min_time=2, max_time=inf)
abstractmethod add_continuous_migration(source_population, rate_param, start_param, end_param)
add_dependent_parameter(param_name, expression, param_type=ParamType.UNTYPED, bounds=None)

Dependent parameters cannot be optimized directly. This is used in sex-biased migration to define a sex-specific migration rate computed from an overall migration rate and sex

add_parameter(param_name, param_type=ParamType.UNTYPED, bounds=None)

Adds the given parameter name to the free parameters of the model. Free parameters include all parameters that can be optimized directly. They may become fixed during optimization

add_population(population_name)

Adds the given population name to the populations of the model.

abstractmethod add_pulse_migration(source_population, rate_param, time_param)
check_bounds(params)

Checks the bounds on parameters. Bounds should be absolute restrictions on possible parameter values, whereas constraints should be restrictions on parameter values relative to each other.

check_constraints(params)

Constraints take the form of a dict {'param_subset':Tuple[String], 'expression': lambda (param_subset)}. The violation score is the largest negative value from all the constraints.

execute_migration_events(migration_matrix, params)
finalize()
get_index(time_param_name, population_name, params)

Returns the matrix index as a tuple from the position and time. Reduces repetitive code.

abstractmethod get_migration_matrices(params)
Return type:

dict[str, ndarray]

get_param_value(param_name, params)

Gets the correct value from the name of the parameter and the list of passed params. If param_name is a number instead, uses the number directly.

abstractmethod get_random_parameters()
get_violation_score(params, verbose=False)

Takes in a list of params equal to the length of model_base_params and returns a negative violation score if the resulting matrix would be or is invalid.

property has_been_fixed
insert_params(params, params_from_proportions)

Used for merging the parameters solved by the primary optimizer with the parameters found from the known ancestry proportions into a single list of parameters in the correct order for the model.

is_time_param()
list_parameters()
logger = <Logger tracts.demography.base_parametrized_demography (WARNING)>
property parameter_bounds
property params_fixed_by_ancestry
property params_fixed_by_value
static parse_proportions(ancestor_names, proportions)

Parses the ancestry proportions used in a founding event into a dict of parametrized source populations and a remainder population.

Return type:

tuple[dict[slice(<class ‘str’>, <class ‘str’>, None)], str]

proportions_from_matrices(migration_matrices)
proportions_from_matrices_return_keys()

This method returns the expected keys from self.proportions_from_matrices(). It is used by FixedParametersHandler to validate that the fixed parameter will be solvable from the given data.

static proportions_from_matrix(migration_matrix)
set_up_fixed_parameters(params_to_fix_by_ancestry, proportions, params_to_fix_by_value={})
class tracts.demography.base_parametrized_demography.FixedParametersHandler(logger)

Bases: object

__init__(logger)
add_fixed_parameters(new_fixed_params)
check_for_improper_constraint(demography)

Checks that the choice of parameters to fix does not underconstrain or overconstrain any of the matrices.

check_for_unsolvable_proportions(demography)

Checks that the demography has an assignment of (full) parameters that results in the chosen proportions.

compute_params_fixed_by_ancestry(params, known_ancestry_proportions=None, units='phys')

Input in physical units “phys” by default, else “opt” for optimizer units

convert_to_optimizer_params(physical_params)

converts parameters from optimization units to physical units, for example inverse log-scaling rates. scaling_functions is a dictionary mapping param types to functions that convert physical to optimization units.

convert_to_physical_params(optimizer_params)

converts optimizer parameters from optimization units to physical units, for example log-scaling rates. scaling_functions is a dictionary mapping param types to functions that convert physical to optimization units.

extend_parameters(free_parameters, units='phys')

takes in the free parameters (those seen by the optimizer) and extends them to include the full parameter list by computing the parameters fixed by ancestry after adding parameters fixed by value.

full_params_objective_func(parameters, units='phys')

returns the difference between computed and model ancestry proportions, as an array.

get_sorted_indices(param_list)

Gives the list of indices as they appear in model_base_params

property has_been_fixed
indices_to_labels(indices)

Takes in a list of indices and returns the corresponding parameter names.

insert_fixed_params(model_base_params, params_to_optimize, fixed_params)

Used for merging the parameters fixed parameters. This could be refactored with insert solved parameters with the parameters found from the known ancestry proportions

non_computed_parameters include both parameters fixed by value and parameters to be optimized over. params_to_optimize: the list of values for the parameters fixed by neither values nor ancestry proportions fixed_params: the values for the fixed parameters.

the list of values are presumed to be in the same order as in non-computed parameters

Output: a list of values for all the non-computed parameters. This will then be used to compute the computed paramters.

insert_solved_params(full_params, param_values_from_proportions)

param_values_from_proportions includes only values for those parameters fixed by ancestry proportions. Used for merging the parameters solved by the primary optimizer with the parameters found from the known ancestry proportions into a single list of parameters in the correct order for the model.

known_ancestry_proportions: dict[str, list[float]]
order_fixed_param_dict(fixed_params_dict)
reduce_parameters(full_parameters)

Takes in the full set of parameters for the demography and reduces them to only the free parameters.

release_fixed_parameters(freed_params)
set_up_fixed_parameters(demography, params_to_fix_by_ancestry, proportions, user_params_to_fix_by_value={})

Tells the model to calculate certain rate parameters based on the known ancestry proportions of the sample populations. Proportions are given as a dict with keys corresponding to the sample populations.

We can fix parameters by value or to match global ancestry proportion.

class tracts.demography.base_parametrized_demography.FounderEvent(found_time, source_populations, remainder_population, end_time=None)

Bases: BaseFounderEvent

__init__(found_time, source_populations, remainder_population, end_time=None)
execute(parametrized_demography, params)