mutators
module#
Classes for defining mutation effect generators \(\mathcal{p}(x\mid x')\), with
arbitrary bdms.TreeNode
attribute dependence. An abstract base class and
several concrete child classes are included.
These classes are used to define mutation effects for simulations with
bdms.TreeNode.evolve
.
Example
>>> import bdms
Define a discrete mutation model.
>>> mutator = bdms.mutators.DiscreteMutator(
... state_space=["a", "b", "c"],
... transition_matrix=[[0.0, 0.5, 0.5],
... [0.5, 0.0, 0.5],
... [0.5, 0.5, 0.0]],
... )
Mutate a node.
>>> node = bdms.TreeNode(state="a")
>>> mutator.mutate(node, seed=0)
>>> node.state
'c'
- class bdms.mutators.Mutator(attr='state', *args, **kwargs)#
Bases:
ABC
Abstract base class for mutators that mutate a specified
ete3.TreeNode
node attribute.- Parameters:
attr (
str
) – Node attribute to mutate.
- abstract mutate(node, seed=None)#
Mutate a
bdms.TreeNode
object in place.- Parameters:
node (
TreeNode
) – Abdms.TreeNode
to mutate.seed (
int
|Generator
|None
) – A seed to initialize the random number generation. IfNone
, then fresh, unpredictable entropy will be pulled from the OS. If anint
, then it will be used to derive the initial state. If anumpy.random.Generator
, then it will be used directly.
- Return type:
- abstract prob(attr1, attr2, log=False)#
Probability of mutating from
attr1
toattr2
.
- class bdms.mutators.GaussianMutator(shift=0.0, scale=1.0, attr='state')#
Bases:
Mutator
Gaussian mutation effects on a specified attribute.
- Parameters:
- mutate(node, seed=None)#
Mutate a
bdms.TreeNode
object in place.- Parameters:
node (
TreeNode
) – Abdms.TreeNode
to mutate.seed (
int
|Generator
|None
) – A seed to initialize the random number generation. IfNone
, then fresh, unpredictable entropy will be pulled from the OS. If anint
, then it will be used to derive the initial state. If anumpy.random.Generator
, then it will be used directly.
- Return type:
- class bdms.mutators.KdeMutator(data, attr='state', bw_method=None, weights=None)#
Bases:
Mutator
Gaussian kernel density estimator (KDE) for mutation effect on a specified attribute.
- Parameters:
data (
ndarray
[tuple
[int
,...
],dtype
[float64
]]) – Data to fit the KDE to.attr (
str
) – Node attribute to mutate.bw_method (
str
|float
|Callable
[...
,float
] |None
) – KDE bandwidth (seescipy.stats.gaussian_kde
).weights (
ndarray
[tuple
[int
,...
],dtype
[float64
]] |None
) – Weights of data points (seescipy.stats.gaussian_kde
).
- mutate(node, seed=None)#
Mutate a
bdms.TreeNode
object in place.- Parameters:
node (
TreeNode
) – Abdms.TreeNode
to mutate.seed (
int
|Generator
|None
) – A seed to initialize the random number generation. IfNone
, then fresh, unpredictable entropy will be pulled from the OS. If anint
, then it will be used to derive the initial state. If anumpy.random.Generator
, then it will be used directly.
- Return type:
- class bdms.mutators.DiscreteMutator(state_space, transition_matrix, attr='state')#
Bases:
Mutator
Mutations on a discrete space with a stochastic matrix.
- Parameters:
-
state_space_idxs:
dict
[Any
,int
]# Mapping from state values to their indices in the transition matrix.
- mutate(node, seed=None)#
Mutate a
bdms.TreeNode
object in place.- Parameters:
node (
TreeNode
) – Abdms.TreeNode
to mutate.seed (
int
|Generator
|None
) – A seed to initialize the random number generation. IfNone
, then fresh, unpredictable entropy will be pulled from the OS. If anint
, then it will be used to derive the initial state. If anumpy.random.Generator
, then it will be used directly.
- Return type: