-
Notifications
You must be signed in to change notification settings - Fork 47
Add get_stats endpoint. #123
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
143d612
Add get_stats endpoint.
tjann 6a6a4a7
Restore urlopen to test, need for mocking.
tjann cf73f11
Update get_stats test with get stats response, not get_places_in resp…
tjann 196364b
Update GetStatsTest test docstrings.
tjann 795f3ff
REST API uses place instead of dcids for place key var name.
tjann 927eba8
Update tests to match default REST/JSON output.
tjann d37ba9f
Update tests to match default REST/JSON output.
tjann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 file contains hidden or 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 file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,15 +49,15 @@ def get_places_in(dcids, place_type): | |
|
|
||
| >>> get_places_in(["geoId/06"], "County") | ||
| { | ||
| 'geoId/06': [ | ||
| 'geoId/06041', | ||
| 'geoId/06089', | ||
| 'geoId/06015', | ||
| 'geoId/06023', | ||
| 'geoId/06067', | ||
| ... | ||
| # and 53 more | ||
| ] | ||
| 'geoId/06': [ | ||
| 'geoId/06041', | ||
| 'geoId/06089', | ||
| 'geoId/06015', | ||
| 'geoId/06023', | ||
| 'geoId/06067', | ||
| ... | ||
| # and 53 more | ||
| ] | ||
| } | ||
| """ | ||
| dcids = filter(lambda v: v==v, dcids) # Filter out NaN values | ||
|
|
@@ -71,7 +71,72 @@ def get_places_in(dcids, place_type): | |
| # Create the results and format it appropriately | ||
| result = utils._format_expand_payload(payload, 'place', must_exist=dcids) | ||
| return result | ||
|
|
||
| def get_stats(dcids, stats_var): | ||
| """ Returns :obj:`TimeSeries` for :code:`dcids` \ | ||
| based on the :code:`stats_var`. | ||
|
|
||
| Args: | ||
| dcids (:obj:`iterable` of :obj:`str`): Dcids of places to query for. | ||
| stats_var (:obj:`str`): The dcid of the :obj:StatisticalVariable. | ||
| Returns: | ||
| A :obj:`dict` mapping the :obj:`Place` identified by the given :code:`dcid` | ||
| to its place name and the :obj:`TimeSeries` associated with the | ||
| :obj:`StatisticalVariable` identified by the given :code:`stats_var`. | ||
| See example below for more detail about how the returned :obj:`dict` is | ||
| structured. | ||
|
|
||
| Raises: | ||
| ValueError: If the payload returned by the Data Commons REST API is | ||
| malformed. | ||
|
|
||
| Examples: | ||
| We would like to get the :obj:`TimeSeries` of the number of males | ||
| at least 25 years old that attended 12th grade but did not receive | ||
| a high school diploma | ||
| (`dc/0hyp6tkn18vcb <https://browser.datacommons.org/kg?dcid=dc/0hyp6tkn18vcb>`_) | ||
| in `Arkansas <https://browser.datacommons.org/kg?dcid=geoId/05>`_ | ||
| and `California <https://browser.datacommons.org/kg?dcid=geoId/06>`_. | ||
|
|
||
| >>> get_stats(["geoId/05", "geoId/06"], "dc/0hyp6tkn18vcb") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mahesh says the rollout is complete, could you use a human-curated DCID in this example? (Adding a TODO in this PR is fine, if it needs more work.) |
||
| { | ||
| 'geoId/05': { | ||
| 'place_name': 'Arkansas' | ||
| 'data': { | ||
| '2011':18136, | ||
| '2012':17279, | ||
| '2013':17459, | ||
| '2014':16966, | ||
| '2015':17173, | ||
| '2016':17041, | ||
| '2017':17783, | ||
| '2018':18003 | ||
| }, | ||
| }, | ||
| 'geoId/05': { | ||
| 'place_name': 'California' | ||
| 'data': { | ||
| '2011':316667, | ||
| '2012':324116, | ||
| '2013':331853, | ||
| '2014':342818, | ||
| '2015':348979, | ||
| '2016':354806, | ||
| '2017':360645, | ||
| '2018':366331 | ||
| }, | ||
| }, | ||
| } | ||
| """ | ||
| dcids = filter(lambda v: v==v, dcids) # Filter out NaN values | ||
| dcids = list(dcids) | ||
| url = utils._API_ROOT + utils._API_ENDPOINTS['get_stats'] | ||
| payload = utils._send_request(url, req_json={ | ||
| 'place': dcids, | ||
| 'stats_var': stats_var, | ||
| }) | ||
|
|
||
| return payload | ||
|
|
||
| def get_related_places(dcids, population_type, measured_property, | ||
| measurement_method, stat_type, constraining_properties={}, | ||
|
|
@@ -112,12 +177,12 @@ def get_related_places(dcids, population_type, measured_property, | |
| "gender": "Female" | ||
| }, "count", "CenusACS5yrSurvey", "measuredValue") | ||
| { | ||
| 'geoId/06085': [ | ||
| 'geoId/06041', | ||
| 'geoId/06089', | ||
| 'geoId/06015', | ||
| 'geoId/06023', | ||
| ] | ||
| 'geoId/06085': [ | ||
| 'geoId/06041', | ||
| 'geoId/06089', | ||
| 'geoId/06015', | ||
| 'geoId/06023', | ||
| ] | ||
| } | ||
| """ | ||
| dcids = filter(lambda v: v==v, dcids) # Filter out NaN values | ||
|
|
||
This file contains hidden or 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 file contains hidden or 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
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.
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.
why is this commented?
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.
It was broken on current master, I've added a TODO.