tracts.demography.base_parametrized_demography.FixedParametersHandler#
- class FixedParametersHandler(logger)#
Bases:
objectA class that handles the fixing of parameters based on ancestry proportions. This class is used by
BaseParametrizedDemographyto fix parameters based on known ancestry proportions.- logger#
The logger.
- Type:
logging.Logger
- params_not_fixed_by_ancestry#
A list of the names of the parameters that are not fixed by ancestry proportions.
- Type:
list[str]
- params_fixed_by_ancestry#
A dict of the names of the parameters that are fixed by ancestry proportions and their corresponding values.
- Type:
dict[str, str]
- known_ancestry_proportions#
A dict mapping sample population names to their corresponding ancestry proportions.
- Type:
dict[str, list[float]] | None
- reduced_constraints#
A list of constraints that have been reduced from the original constraints after fixing parameters by ancestry proportions. These constraints are of the same form as the original constraints, but they only involve the free parameters that are being optimized after fixing parameters by ancestry proportions.
- Type:
list[dict]
- user_params_fixed_by_value#
A dict of the names of the parameters that are fixed by user-defined values and their corresponding values.
- Type:
dict[str, float]
- demography#
The demography object that this FixedParametersHandler is associated with.
- to_physical_params_functions#
A dict mapping parameter types to functions that convert parameters from optimization units to physical units.
- Type:
dict[str, Callable]
- to_optimizer_params_functions#
A dict mapping parameter types to functions that convert parameters from physical units to optimization units.
- Type:
dict[str, Callable]
- add_fixed_parameters(new_fixed_params)#
Adds new fixed parameters by value to the current set of fixed parameters, and updates the indices of the free parameters accordingly. Checks that the new fixed parameters do not conflict with any existing fixed parameters.
- Parameters:
new_fixed_params (
dict[str,float]) – A dict mapping parameter names to their corresponding values, which are used to fix parameters by user-defined values. These parameters cannot be optimized and are not computed from ancestry proportions.
- check_for_improper_constraint(demography)#
Checks that the choice of parameters to fix does not underconstrain or overconstrain any of the matrices.
- Parameters:
demography (
BaseParametrizedDemography) – The demography object that thisFixedParametersHandleris associated with.
- check_for_unsolvable_proportions(demography)#
Checks that the demography has an assignment of (full) parameters that results in the chosen proportions.
- Parameters:
demography (
BaseParametrizedDemography) – The demography object that thisFixedParametersHandleris associated with.
- compute_params_fixed_by_ancestry(params, known_ancestry_proportions=None, units=None, show_ancestry_warning=False, counter=0, verbose_warning_screen=0, verbose_warning_log=0)#
Compute the parameters fixed by ancestry proportions.
- Parameters:
params (
list[float]) – The input parameters for the demography model.known_ancestry_proportions (
dict[str,ndarray] |None) – A dictionary mapping ancestor population names to their known ancestry proportions.units (
str|None) – The units of the input parameters. If “phys”, the input parameters are in physical units. If “opt”, the input parameters are in optimization units. If None, defaults to “phys”. The units of the output parameters are the same as the input parameters.show_ancestry_warning (
bool) – Whether to print a warning message when the ancestry proportions cannot be perfectly matched by any values of the parameters fixed by ancestry. Used internally to display a warning at the end of optimization if the problem is not resolved for optimal parameters.counter (
int) – The current iteration number of the optimization process, used for logging and printing purposes.verbose_warning_screen (
int) – The verbosity level for printing to the screen. If 0, no information is printed.verbose_warning_log (
int) – The verbosity level for logging. If 0, no information is logged.
- Returns:
The computed parameters for the fixed ancestry proportion model.
- Return type:
list[float]
- convert_to_optimizer_params(physical_params)#
Converts parameters from optimization units to physical units.
- Parameters:
physical_params (
list[float]) – A list of parameter values for the free parameters of the model in physical units. The order of the values corresponds to the order of the parameters inmodel_base_params.- Returns:
An array of parameter values for the free parameters of the model in optimization units, in the same order as in
physical_params.- Return type:
np.ndarray
- convert_to_physical_params(optimizer_params)#
Converts optimizer parameters from optimization units to physical units.
- Parameters:
optimizer_params (
list[float]) – A list of parameter values for the free parameters of the model in optimization units. The order of the values corresponds to the order of the parameters inmodel_base_params.- Returns:
An array of parameter values for the free parameters of the model in physical units, in the same order as in
optimizer_params.- Return type:
np.ndarray
- extend_parameters(free_parameters, units=None, show_ancestry_warning=False, counter=0, verbose_warning_screen=0, verbose_warning_log=0)#
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.
- Parameters:
free_parameters (
ndarray) – An array of parameter values for the free parameters of the model that are being optimized by the primary optimizer. The order of the values corresponds to the order of the parameters inmodel_base_paramsthat are not fixed by ancestry proportions or by user-defined values.units (
str|None) – The units of the input parameters. If “phys”, the input parameters are in physical units. If “opt”, the input parameters are in optimization units. If None, defaults to “phys”. The units of the output parameters are the same as the input parameters.show_ancestry_warning (
bool) – Whether to show a warning if the parameters fixed by ancestry proportions are out of bounds or violate any constraints after being computed. For internal use only, to display the warning only at the end of optimization.counter (
int) – The current iteration number of the optimization process, used for logging and printing purposes.verbose_warning_screen (
int) – The verbosity level for printing to the screen. If 0, no information is printed.verbose_warning_log (
int) – The verbosity level for logging. If 0, no information is logged.
- Returns:
An array of parameter values for all the free parameters of the model, including those fixed by ancestry proportions and by user-defined values, in the same order as in
model_base_params.- Return type:
np.ndarray
- full_params_objective_func(parameters, units=None)#
Returns the difference between computed and model ancestry proportions, as an array.
- Parameters:
parameters (
list[float]) – A list of parameter values for all the free parameters of the model, including those fixed by ancestry proportions and by user-defined values, in the same order as inmodel_base_params.units (
str|None) – The units of the input parameters. Only implemented for physical parameters so the function will raise an error ifunitsis not"phys".
- Returns:
An array of the differences between the computed ancestry proportions from the given parameters and the known ancestry proportions, for each sample population and each ancestor population with a fixed proportion, in the same order as in
known_ancestry_proportions.- Return type:
np.ndarray
- get_sorted_indices(param_list)#
Computes the list of indices as they appear in
model_base_params.- Parameters:
param_list (
list[str]) – A list of parameter names for which to get the indices.- Returns:
An array of indices corresponding to the positions of the parameters in
param_listas they appear inmodel_base_params.- Return type:
np.ndarray
- property has_been_fixed#
Whether any parameters have been fixed by ancestry proportions.
- Returns:
True if any parameters have been fixed by ancestry proportions, False otherwise.
- Return type:
bool
- indices_to_labels(indices)#
Takes in a list of indices and returns the corresponding parameter names.
- Parameters:
indices (
list[int]) – A list of indices corresponding to the positions of parameters inmodel_base_params.- Returns:
A list of parameter names corresponding to the given indices.
- Return type:
list[str]
- insert_fixed_params(model_base_params, params_to_optimize, fixed_params)#
Inserts the fixed parameter values into the list of parameters to be optimized.
- Parameters:
model_base_params (
dict[str,Parameter]) – A dictionary of all base parameters for the model.params_to_optimize (
list[float]) – A list of values for the parameters fixed by neither values nor ancestry proportions.fixed_params (
list[float]) – A list of parameter values for the parameters fixed by value.
- Returns:
A list of parameter values for all the non-computed parameters.
- Return type:
list[float]
- insert_solved_params(full_params, param_values_from_proportions)#
Merges 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.
- Parameters:
full_params (
list[float]) – A list of parameter values for all the free parameters of the model, including those fixed by ancestry proportions and by user-defined values, in the same order as inmodel_base_params.param_values_from_proportions (
list[float]) – A list of parameter values for the parameters fixed by ancestry proportions, in the same order as inmodel_base_paramsfor the parameters fixed by ancestry proportions.
- Returns:
An array of parameter values for all the free parameters of the model, including those fixed by ancestry proportions and by user-defined values, in the same order as in
model_base_params.- Return type:
np.ndarray
- order_fixed_param_dict(fixed_params_dict)#
Orders the given dict of fixed parameters by the order of the parameters in
model_base_params.- Parameters:
fixed_params_dict (
dict[str,float]) – A dictionary mapping parameter names to their fixed values.- Returns:
A dictionary mapping parameter names to their fixed values, ordered by the order of the parameters in
model_base_params.- Return type:
dict[str, float]
- reduce_parameters(full_parameters)#
Takes in the full set of parameters for the demography and reduces them to only the free parameters.
- Parameters:
full_parameters (
list[float]) – A list of parameter values for all the free parameters of the model, including those fixed by ancestry proportions and by user-defined values, in the same order as inmodel_base_params.- Returns:
An array of parameter values for the free parameters of the model that are being optimized by the primary optimizer, in the same order as in
model_base_paramsthat are not fixed by ancestry proportions or by user-defined values.- Return type:
np.ndarray
- release_fixed_parameters(freed_params)#
Removes parameters from the current set of fixed parameters, and updates the indices of the free parameters accordingly.
- Parameters:
freed_params (
list[str]) – A list of parameter names corresponding to parameters that are currently fixed by value that should be released and made free to be optimized by the primary optimizer.
- set_up_fixed_parameters(demography, params_to_fix_by_ancestry, proportions, user_params_to_fix_by_value=None)#
Tells the model to calculate certain rate parameters based on the known ancestry proportions of the sample populations, or to fix them by value.
- Parameters:
demography (
BaseParametrizedDemography) – The demography object that this FixedParametersHandler is associated with.params_to_fix_by_ancestry (
list[str]) – A list of the names of the parameters to be fixed by ancestry proportions.proportions (
dict[str,list[float]]) – A dict mapping sample population names to their corresponding ancestry proportions, which are used to fix the parameters inparams_to_fix_by_ancestry.user_params_to_fix_by_value (
dict[str,float] |None) – A dict mapping parameter names to their corresponding values, which are used to fix parameters by user-defined values. These parameters cannot be optimized and are not computed from ancestry proportions.