Skip to content

Commit

Permalink
Merge pull request #576 from pyinat/taxonomy
Browse files Browse the repository at this point in the history
Add Observation.taxon.ancestors based on identification data
  • Loading branch information
JWCook authored Jul 23, 2024
2 parents 83f6b2f + 67041c7 commit 7a3a43e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ questions, and other contributions that have helped to improve pyinaturalist:

* [AE Hamrick](https://github.com/AEHamrick)
* [abubelinha](https://github.com/abubelinha)
* [Arky](https://github.com/arky)
* [Ben Armstrong](https://github.com/synrg)
* [Dr. Tom August](https://github.com/AugustT)
* [Eduardo Ramírez](https://github.com/eduramirezh)
* [Felipe S Barros](https://github.com/FelipeSBarros)
* [jumear](https://github.com/jumear)
* [Ken-ichi Ueda](https://github.com/kueda)
* [Matt Muir](https://forum.inaturalist.org/u/muir)
* [Nicolas Noé](https://github.com/niconoe)
* [Patrick Leary](https://github.com/pleary)
* [Peter Desmet](https://github.com/peterdesmet)
* [pisum](https://forum.inaturalist.org/u/pisum)
* [Sam R](https://github.com/samtreesandbushes)
* [SDJ Brown](https://github.com/sdjbrown)
* [Stijn Van Hoey](https://github.com/stijnvanhoey)
Expand Down
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Add `root_id` filter to `taxon.make_tree()` to explicitly set the root taxon instead of determining it automatically
* Fix `taxon.make_tree()` rank filtering to allow skipping any number of rank levels
* `taxon.make_tree()` now returns copies of original taxon objects instead of modifying them in-place
* Update `Observation.taxon.ancestors` based on identification data, if available

### Rate limits, timeouts, and error handling
* Increase default request timeout from 10 to 20 seconds
Expand Down
11 changes: 11 additions & 0 deletions pyinaturalist/models/observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ def __init__(self, **kwargs):
# Set identifications_count if missing
if kwargs.get('identifications') and not kwargs.get('identifications_count'):
kwargs['identifications_count'] = len(kwargs['identifications'])

# Add ancestor info to Observation.taxon based on identification data, if available
if kwargs.get('taxon') and (idents := kwargs.get('identifications')):
taxa_by_id = {
t['id']: t
for t in chain.from_iterable(
[i.get('taxon', {}).get('ancestors', []) for i in idents]
)
}
ancestors = [taxa_by_id.get(i) for i in kwargs['taxon'].get('ancestor_ids')]
kwargs['taxon']['ancestors'] = list(filter(None, ancestors))
self.__attrs_init__(**kwargs) # type: ignore

@property
Expand Down
18 changes: 18 additions & 0 deletions test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,24 @@ def test_observation__cumulative_ids__most_agree():
assert obs.cumulative_ids == (2, 3)


def test_observation__add_ancestors():
obs = Observation.from_json(j_observation_2)
assert [f'{t.rank} {t.name}' for t in obs.taxon.ancestors] == [
'kingdom Animalia',
'phylum Arthropoda',
'subphylum Hexapoda',
'class Insecta',
'subclass Pterygota',
'order Lepidoptera',
'superfamily Papilionoidea',
'family Nymphalidae',
'subfamily Danainae',
'tribe Danaini',
'subtribe Danaina',
'genus Danaus',
]


def test_observations():
obs_list = Observations.from_json_list(
[j_observation_1, j_observation_1, j_observation_2, j_observation_3_ofvs]
Expand Down

0 comments on commit 7a3a43e

Please sign in to comment.