Configuration class#
Bundles the spectra, parametrization, and settings that define an inference run, with serialisation to and from YAML.
Classes:
Configuration class to be used for |
- class Config(polydfe_spectra_config: str = None, polydfe_init_file: str = None, polydfe_init_file_id: int = 1, sfs_neut: Spectra | Spectrum = None, sfs_sel: Spectra | Spectrum = None, intervals_del: Tuple[float, float, int] = (-100000000.0, -1e-05, 1000), intervals_ben: Tuple[float, float, int] = (1e-05, 10000.0, 1000), intervals_h: Tuple[float, float, int] = (0.0, 1.0, 21), h_callback: Callable[[float, ndarray], ndarray] = None, integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, model: Parametrization | str = 'GammaExpParametrization', seed: int = 0, x0: Dict[str, Dict[str, float]] = {}, bounds: Dict[str, Tuple[float, float]] = {}, scales: Dict[str, Literal['lin', 'log', 'symlog']] = {}, loss_type: Literal['likelihood', 'L2'] = 'likelihood', opts_mle: dict = {}, method_mle: str = 'L-BFGS-B', n_runs: int = 10, fixed_params: Dict[str, Dict[str, float]] = None, shared_params: List[SharedParams] = [], covariates: List[Covariate] = [], do_bootstrap: bool = True, n_bootstraps: int = 100, n_bootstrap_retries: int = 2, parallelize: bool = True, include_divergence: bool = True, n_sites_div_neut: Dict[str, float] = None, n_sites_div_sel: Dict[str, float] = None, folded: bool = None, **kwargs)[source]#
Bases:
objectConfiguration class to be used for
BaseInferenceandJointInference.Methods:
Create config object.
Update config with given data.
Parse polyDFE init file.
Create an init file for polyDFE.
Parse frequency spectra and mutational target site from polyDFE configuration file.
Create a sfs config file for polyDFE.
Represent config as dictionary.
Create JSON representation of object.
Create YAML representation of object.
Save object to file.
Load config from dictionary, as returned by
to_dict()or decoded from JSON or YAML.Load config from JSON str.
Load config from YAML str.
Load object from file.
Get the model name in polyDFE that corresponds to the configured DFE parametrization.
- __init__(polydfe_spectra_config: str = None, polydfe_init_file: str = None, polydfe_init_file_id: int = 1, sfs_neut: Spectra | Spectrum = None, sfs_sel: Spectra | Spectrum = None, intervals_del: Tuple[float, float, int] = (-100000000.0, -1e-05, 1000), intervals_ben: Tuple[float, float, int] = (1e-05, 10000.0, 1000), intervals_h: Tuple[float, float, int] = (0.0, 1.0, 21), h_callback: Callable[[float, ndarray], ndarray] = None, integration_mode: Literal['midpoint', 'quad'] = 'midpoint', linearized: bool = True, model: Parametrization | str = 'GammaExpParametrization', seed: int = 0, x0: Dict[str, Dict[str, float]] = {}, bounds: Dict[str, Tuple[float, float]] = {}, scales: Dict[str, Literal['lin', 'log', 'symlog']] = {}, loss_type: Literal['likelihood', 'L2'] = 'likelihood', opts_mle: dict = {}, method_mle: str = 'L-BFGS-B', n_runs: int = 10, fixed_params: Dict[str, Dict[str, float]] = None, shared_params: List[SharedParams] = [], covariates: List[Covariate] = [], do_bootstrap: bool = True, n_bootstraps: int = 100, n_bootstrap_retries: int = 2, parallelize: bool = True, include_divergence: bool = True, n_sites_div_neut: Dict[str, float] = None, n_sites_div_sel: Dict[str, float] = None, folded: bool = None, **kwargs)[source]#
Create config object.
- Parameters:
polydfe_spectra_config¶ (
str) – Path to polyDFE SFS config file.sfs_neut¶ (
Spectra|Spectrum) – Neutral SFS. Note that we require monomorphic counts to be specified in order to infer the mutation rate.sfs_sel¶ (
Spectra|Spectrum) – Selected SFS. Note that we require monomorphic counts to be specified in order to infer the mutation rate.intervals_del¶ (
Tuple[float,float,int]) –(start, stop, n_interval)for deleterious population-scaled selection coefficients. The intervals will be log10-spaced. Decreasing the number of intervals to100provides nearly identical results while increasing speed, especially when precomputing across dominance coefficients.intervals_ben¶ (
Tuple[float,float,int]) – Same asintervals_delbut for positive selection coefficients. Decreasing the number of intervals to100provides nearly identical results while increasing speed, especially when precomputing across dominance coefficients.intervals_h¶ (
Tuple[float,float,int]) –(start, stop, n_points)for the linearly spaced grid of dominance coefficients. This is only used when inferring dominance coefficients. Values ofhbetween the grid points will be interpolated linearly.h_callback¶ (
Callable[[float,ndarray],ndarray]) – A function mapping the scalar parameterhand the array of selection coefficientsSto dominance coefficients of the same shape, allowing models wherehdepends onS. The default islambda h, S: np.full_like(S, h), keepinghconstant. Expected allele counts for a given dominance value are obtained by linear interpolation between precomputed values inintervals_h. The inferred parameter is still namedh, even if transformed byh_callback, and its bounds, scales, and initial values can be set viabounds,scales, andx0. The fitness of heterozygotes and mutation homozygotes is defined as1 + 2hsand1 + 2s, respectively.integration_mode¶ (
Literal['midpoint','quad']) – Integration mode when computing expected SFS under semidominance.quadis not recommended.linearized¶ (
bool) – Whether to discretize and cache the linearized integral mapping DFE to SFS or usescipy.integrate.quad()in each call.Falseis slow, intended for validation only and deprecated.model¶ (
Parametrization|str) – Parametrization of the DFE.seed¶ (
int) – Seed for the random number generator. UseNonefor no seed.x0¶ (
Dict[str,Dict[str,float]]) – Dictionary of initial values in the form{type: {param: value}}bounds¶ (
Dict[str,Tuple[float,float]]) – Bounds for the optimization in the form {param: (lower, upper)}scales¶ (
Dict[str,Literal['lin','log','symlog']]) – Scales for the optimization in the form {param: scale}loss_type¶ (
Literal['likelihood','L2']) – Loss function to use.method_mle¶ (
str) – Method to use for optimization, which needs to support bounds. Seescipy.optimize.minimize()for available methods.n_runs¶ (
int) – Number of independent optimization runs out of which the best one is chosen. The first run will use the initial values if specified. Consider increasing this number if the optimization does not produce good results.fixed_params¶ (
Dict[str,Dict[str,float]]) – Fixed parameters for the optimization.shared_params¶ (
List[SharedParams]) – Shared parameters for the optimization.covariates¶ (
List[Covariate]) – Covariates for the optimization.do_bootstrap¶ (
bool) – Whether to do bootstrapping automatically.n_bootstrap_retries¶ (
int) – Number of optimization runs for each bootstrap sample. This parameter previously defined the number of retries per bootstrap sample when subsequent runs failed, but now it defines the total number of runs per bootstrap sample, taking the most likely one.parallelize¶ (
bool) – Whether to parallelize the optimization.include_divergence¶ (
bool) – Whether to include divergence counts in the likelihood. This only takes effect when the divergence target sizes are given vian_sites_div_neutandn_sites_div_sel; otherwise inference is based on polymorphism alone. Defaults toTrue.n_sites_div_neut¶ (
Dict[str,float]) – Number of mutational target sites over which neutral divergence was counted, keyed by type. Specifying it (withn_sites_div_sel) opts neutral divergence into the likelihood. Also used to round-trip divergence target sizes through serialization.n_sites_div_sel¶ (
Dict[str,float]) – Number of mutational target sites over which selected divergence was counted, keyed by type. Specifying it (withn_sites_div_neut) opts selected divergence into the likelihood.folded¶ (
bool) – Whether the SFS are folded. If not specified, the SFS will be folded if both of the given SFS appear to be folded.kwargs¶ – Additional keyword arguments which are ignored.
- update(**kwargs) Config[source]#
Update config with given data.
- Parameters:
kwargs¶ – Data to update.
- Returns:
Updated config.
- parse_polydfe_init_file(file: str, id: int = 1, type='all')[source]#
Parse polyDFE init file. This will define the initial parameters and which ones will be held fixed during the optimization.
- create_polydfe_init_file(file: str, n: int, type: str = 'all')[source]#
Create an init file for polyDFE.
- parse_polydfe_sfs_config(file: str)[source]#
Parse frequency spectra and mutational target site from polyDFE configuration file.
- to_dict() dict[source]#
Represent config as dictionary.
- Returns:
Dictionary representation of config.
- static from_dict(data: dict) Config[source]#
Load config from dictionary, as returned by
to_dict()or decoded from JSON or YAML.