pycea.tl.parsimony

Contents

pycea.tl.parsimony#

pycea.tl.parsimony(tdata, key, method='fitch_hartigan', missing_state=None, costs=None, reconstruct=True, tree=None, key_added='parsimony', copy=True)#

Computes the small-parsimony score of a tree.

The parsimony score is the number of edges along which the state of a categorical attribute changes. Ancestral states can be reconstructed on the fly (default) or read from states already present on the tree.

Parameters:
  • tdata (TreeData) – TreeData object.

  • key (str) – An obs.keys() corresponding to a categorical variable.

  • method (str | Callable (default: 'fitch_hartigan')) – Method used to reconstruct ancestral states when reconstruct=True. Passed to pycea.tl.ancestral_states(). Typically 'fitch_hartigan' or 'sankoff'.

  • missing_state (str | None (default: None)) – The state to consider as missing data. Edges touching a missing state are not counted.

  • costs (DataFrame | None (default: None)) – A DataFrame with the costs of changing states (from rows to columns). Only used if method='sankoff'.

  • reconstruct (bool (default: True)) – If True, reconstruct ancestral states with pycea.tl.ancestral_states() before scoring. If False, use states already stored under key on the tree.

  • tree (str | Sequence[str] | None (default: None)) – The obst key or keys of the trees to use. If None, all trees are used.

  • key_added (str (default: 'parsimony')) – Key in tdata.uns where the parsimony score(s) are stored.

  • copy (Literal[True, False] (default: True)) – If True, returns the parsimony score(s).

Return type:

int | Series | None

Returns:

Returns None if copy=False. Otherwise returns an int for a single tree, or a Series of scores indexed by tree for multiple trees.

Sets the following fields:

  • tdata.uns[key_added]int | Series
    • Parsimony score for a single tree, or a Series of scores for multiple trees.

Examples

Compute the parsimony score of a categorical character:

>>> py.tl.parsimony(tdata, key="clone")