From 82f259db9792099ac53a22a6b3aa48894b9bde41 Mon Sep 17 00:00:00 2001 From: Ben Armstrong Date: Tue, 9 Jul 2024 17:04:40 -0300 Subject: [PATCH] Simplify taxon root and rank filters. --- dronefly/core/formatters/generic.py | 29 +++++++++++++---------------- poetry.lock | 8 ++++---- pyproject.toml | 2 +- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/dronefly/core/formatters/generic.py b/dronefly/core/formatters/generic.py index 4eb3459..ca4642c 100644 --- a/dronefly/core/formatters/generic.py +++ b/dronefly/core/formatters/generic.py @@ -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 diff --git a/poetry.lock b/poetry.lock index f62c4ec..0165e96 100644 --- a/poetry.lock +++ b/poetry.lock @@ -903,13 +903,13 @@ rich = ">=10.9" [package.extras] all = ["filelock (>2.0)", "ujson (>5.0)"] -docs = ["furo (>=2023.7,<2024.0)", "ipython (>=8.12,<9.0)", "linkify-it-py (>=2.0,<3.0)", "matplotlib (>=3.7)", "myst-parser (>=1.0)", "nbsphinx (>=0.9,<0.10)", "pillow (>=10.1)", "sphinx (>=6.2,<7.0)", "sphinx-autodoc-typehints (==1.20)", "sphinx-automodapi (>=0.15,<0.16)", "sphinx-copybutton (>=0.5)", "sphinx-design (>=0.5)", "sphinxcontrib-apidoc (>=0.3,<0.4)", "sphinxext-opengraph (>=0.9)"] +docs = ["furo (>=2024.4,<2025.0)", "ipython (>=8.12,<9.0)", "linkify-it-py (>=2.0,<3.0)", "matplotlib (>=3.7)", "myst-parser (>=1.0)", "nbsphinx (>=0.9,<0.10)", "pillow (>=10.1)", "sphinx (>=6.2,<7.0)", "sphinx-autodoc-typehints (==1.20)", "sphinx-automodapi (>=0.15,<0.16)", "sphinx-copybutton (>=0.5)", "sphinx-design (>=0.5)", "sphinxcontrib-apidoc (>=0.3,<0.4)", "sphinxext-opengraph (>=0.9)"] [package.source] type = "git" url = "https://github.com/pyinat/pyinaturalist" -reference = "192437bfe112bd017d2a2b3344a72ad498602af6" -resolved_reference = "192437bfe112bd017d2a2b3344a72ad498602af6" +reference = "HEAD" +resolved_reference = "12c04689c921a4d2e5cd4352c8e7c525e693b7f0" [[package]] name = "pylint" @@ -1466,4 +1466,4 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", [metadata] lock-version = "2.0" python-versions = ">=3.8.1,<3.13" -content-hash = "f606ae230213ad4cc3b82564435ed71cfdd68719aba15c81d88182f1b00fd5ee" +content-hash = "6da26c504bc6d6c19b84d56d76ed9ae8b7fd486de95c20dd7003e59e1135a859" diff --git a/pyproject.toml b/pyproject.toml index 5572e34..ca66f81 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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"