-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add parents/ancestry methods #231
Merged
jm-rivera
merged 9 commits into
datacommonsorg:master
from
ONEcampaign:add-fetch-ancestry
Apr 7, 2025
Merged
Add parents/ancestry methods #231
jm-rivera
merged 9 commits into
datacommonsorg:master
from
ONEcampaign:add-fetch-ancestry
Apr 7, 2025
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Mar 31, 2025
dwnoble
reviewed
Mar 31, 2025
Add ancestry tools
45316c5
to
8fccc33
Compare
@dwnoble ready for a new review. Thank you!! |
dwnoble
approved these changes
Apr 7, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @jm-rivera!
FYI @kmoscoe in case it's helpful for the docs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR is part of a group of PRs which will bring some key features from the Data Commons website API to the client library (e.g #229, #230)
TL;DR
Fetch entity parents: A new Node method (
fetch_entity_parents
) which usescontainedInPlace
to get the immediate parents for a given entity or list of entities.Fetch entity ancestry: A new Node method
fetch_entity_ancestry
performs a parallel breadth-first traversal to construct the full ancestry chain for an entity — all the way up to Earth — and returns it in either flat or nested form.With this PR, I aimed to replicate the
/api/place/parent
of the website API. Under the hood, this endpoint relies on legacy calls to the REST V1 bulk info endpoint.Being able to get the full ancestry for an entity (or list of entities) is a very helpful feature. However, doing this through the Node endpoint requires recursively resolving each parent until you reach the root of the place graph (in this case always "Earth"). This PR introduces logic to make that process as efficient and easy to use as possible for users (and being careful not to make any repeated or unnecessary calls to the API).
build_ancestry_map
) to ensure we resolve ancestry level by level, starting from the root entity. This avoids recursion and makes parallel execution easier to manage and reason about.containedInPlace
, I usedThreadPoolExecutor
to fetch parent nodes in parallel. This drastically reduces total resolution time for large (e.g many entities) or deep ancestry chains (e.g. "Kampala" -> "Uganda" -> "Africa" -> "Earth").The data can be returned as two different types:
/api/place/parent
— for parity with the website version or cases when only the list of upstream places is needed.Example usage:
For entity parents
By itself, the above doesn't match the website endpoint, or provide a complete ancestry.
Full ancestry
Which can also be returned as a nested structure: