Skip to content

Commit

Permalink
Simplify taxon root and rank filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
synrg committed Jul 9, 2024
1 parent e1297d9 commit 82f259d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
29 changes: 13 additions & 16 deletions dronefly/core/formatters/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,29 +136,26 @@ def taxa_per_rank(
root_taxon_id: int = None,
):
"""Generate taxa matching ranks to count in treewise order."""
options = {}
subtree = (
lambda t: True
if root_taxon_id is None
else root_taxon_id in [t.id] + [a.id for a in t.ancestors]
)
include_leaves = None
include_ranks = None
if isinstance(ranks_to_count, list):
options["include_ranks"] = ranks_to_count
include = lambda t: subtree(t) # noqa: E731
include_ranks = ranks_to_count
else:
if ranks_to_count == "leaf":
include = (
lambda t: subtree(t) and t.count == t.descendant_obs_count
) # noqa: E731
include_leaves = lambda t: t.count == t.descendant_obs_count # noqa: E731
include_ranks = None
else:
# single rank case:
options["root_id"] = root_taxon_id
include_ranks = [ranks_to_count]
include = lambda t: subtree(t) and t.rank in include_ranks # noqa: E731
tree = make_tree(life_list.data, **options)
hide_root = tree.id == ROOT_TAXON_ID
tree = make_tree(life_list.data, include_ranks=include_ranks, root_id=root_taxon_id)
hide_root = (
tree.id == ROOT_TAXON_ID
or include_ranks
and len(include_ranks) == 1
and tree.rank != include_ranks[0]
)
for taxon_count in tree.flatten(hide_root=hide_root):
if include(taxon_count):
if include_leaves is None or include_leaves(taxon_count):
yield taxon_count


Expand Down
8 changes: 4 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ packages = [
[tool.poetry.dependencies]
python = ">=3.8.1,<3.13"
# pyinaturalist = "=0.20.0.dev0"
pyinaturalist = { git = "https://github.com/pyinat/pyinaturalist", rev = "192437bfe112bd017d2a2b3344a72ad498602af6" }
pyinaturalist = { git = "https://github.com/pyinat/pyinaturalist" }
dateparser = "^1.1.1"
filelock = "^3.13.3"
inflect = "^5.3.0"
Expand Down

0 comments on commit 82f259d

Please sign in to comment.