Skip to content

Commit

Permalink
Add another optimized impl for prune extinct lineages asexual
Browse files Browse the repository at this point in the history
  • Loading branch information
mmore500 committed Jan 5, 2025
1 parent 681b71b commit f9a7a05
Showing 1 changed file with 30 additions and 5 deletions.
35 changes: 30 additions & 5 deletions hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pandas as pd

from ._alifestd_has_contiguous_ids import alifestd_has_contiguous_ids
from ._alifestd_is_topologically_sorted import alifestd_is_topologically_sorted

Check warning on line 7 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L6-L7

Added lines #L6 - L7 were not covered by tests
from ._alifestd_try_add_ancestor_id_col import alifestd_try_add_ancestor_id_col
from ._alifestd_unfurl_lineage_asexual import alifestd_unfurl_lineage_asexual
from ._jit import jit
Expand Down Expand Up @@ -54,6 +55,20 @@ def _create_has_extant_descendant_contiguous(
return has_extant_descendant

Check warning on line 55 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L55

Added line #L55 was not covered by tests


@jit(nopython=True)
def _create_has_extant_descendant_contiguous_sorted(

Check warning on line 59 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L58-L59

Added lines #L58 - L59 were not covered by tests
ancestor_ids: np.ndarray,
extant_mask: np.ndarray,
) -> np.ndarray:
"""Implementation detail for alifestd_prune_extinct_lineages_asexual."""

has_extant_descendant = extant_mask.copy()
for id_ in range(len(ancestor_ids) - 1, -1, -1):
has_extant_descendant[ancestor_ids[id_]] |= has_extant_descendant[id_]

Check warning on line 67 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L65-L67

Added lines #L65 - L67 were not covered by tests

return has_extant_descendant

Check warning on line 69 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L69

Added line #L69 was not covered by tests


def alifestd_prune_extinct_lineages_asexual(
phylogeny_df: pd.DataFrame,
mutate: bool = False,
Expand Down Expand Up @@ -92,7 +107,10 @@ def alifestd_prune_extinct_lineages_asexual(
phylogeny_df = phylogeny_df.copy()

phylogeny_df = alifestd_try_add_ancestor_id_col(phylogeny_df, mutate=True)
phylogeny_df.set_index("id", drop=False, inplace=True)
if alifestd_has_contiguous_ids(phylogeny_df):
phylogeny_df.reset_index(drop=True, inplace=True)

Check warning on line 111 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L110-L111

Added lines #L110 - L111 were not covered by tests
else:
phylogeny_df.index = phylogeny_df["id"]

Check warning on line 113 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L113

Added line #L113 was not covered by tests

extant_mask = None
if "extant" in phylogeny_df:
Expand All @@ -105,15 +123,22 @@ def alifestd_prune_extinct_lineages_asexual(
else:
raise ValueError('Need "extant" or "destruction_time" column.')

if alifestd_has_contiguous_ids(phylogeny_df):
if not alifestd_has_contiguous_ids(phylogeny_df):
has_extant_descendant = _create_has_extant_descendant_noncontiguous(

Check warning on line 127 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L126-L127

Added lines #L126 - L127 were not covered by tests
phylogeny_df,
extant_mask,
)
elif not alifestd_is_topologically_sorted(phylogeny_df):
has_extant_descendant = _create_has_extant_descendant_contiguous(

Check warning on line 132 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L131-L132

Added lines #L131 - L132 were not covered by tests
phylogeny_df["ancestor_id"].to_numpy(dtype=np.uint64),
extant_mask.to_numpy(dtype=bool),
)
else:
has_extant_descendant = _create_has_extant_descendant_noncontiguous(
phylogeny_df,
extant_mask,
has_extant_descendant = (

Check warning on line 137 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L137

Added line #L137 was not covered by tests
_create_has_extant_descendant_contiguous_sorted(
phylogeny_df["ancestor_id"].to_numpy(dtype=np.uint64),
extant_mask.to_numpy(dtype=bool),
)
)

phylogeny_df = phylogeny_df[has_extant_descendant].reset_index(drop=True)

Check warning on line 144 in hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py

View check run for this annotation

Codecov / codecov/patch

hstrat/_auxiliary_lib/_alifestd_prune_extinct_lineages_asexual.py#L144

Added line #L144 was not covered by tests
Expand Down

0 comments on commit f9a7a05

Please sign in to comment.