Skip to content

Commit 4387c1a

Browse files
authored
Add observation_date as argument to get_place_obs (#97)
* Add observation_date as argument to get_place_obs * Update doc string * Update test
1 parent fe71648 commit 4387c1a

2 files changed

Lines changed: 19 additions & 65 deletions

File tree

datacommons/populations.py

Lines changed: 16 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -352,36 +352,34 @@ def get_pop_obs(dcid):
352352
url = utils._API_ROOT + utils._API_ENDPOINTS['get_pop_obs'] + '?dcid={}'.format(dcid)
353353
return utils._send_request(url, compress=True, post=False)
354354

355-
def get_place_obs(place_type, population_type, constraining_properties={}):
356-
""" Returns all :obj:`StatisticalPopulation`'s and :obj:`Observation`'s for \
357-
all places of the given :code:`place_type`.
355+
def get_place_obs(place_type, observation_date, population_type, constraining_properties={}):
356+
""" Returns all :obj:`Observation`'s for all places given the place type,
357+
observation date and the :obj:`StatisticalPopulation` constraints.
358358
359359
Args:
360360
place_type (:obj:`str`): The type of places to query
361361
:obj:`StatisticalPopulation`'s and :obj:`Observation`'s for.
362+
observation_date (:obj:`str`): The observation date in ISO-8601 format.
362363
population_type (:obj:`str`): The population type of the
363364
:obj:`StatisticalPopulation`
364365
constraining_properties (:obj:`map` from :obj:`str` to :obj:`str`, optional):
365366
A map from constraining property to the value that the
366367
:obj:`StatisticalPopulation` should be constrained by.
367368
368369
Returns:
369-
Given a :code:`Place` type (i.e. :obj:`State`, :obj:`County`, :obj:`City`),
370-
a :code:`population_type` (i.e. :obj:`Person`), and optionally a set of
371-
constraining properties defining the `obj`:`StatisticalPopulation`, this
372-
function returns *all* :obj:`StatisticalPopulation`'s and
373-
:obj:`Observation`'s for all places of the given type. See examples for more
374-
details on how the format of the return value is structured.
370+
A list of dictionaries, with each dictionary containng *all*
371+
:obj:`Observation`'s of a place that conform to the :obj:`StatisticalPopulation`
372+
constraints. See examples for more details on how the format of the
373+
return value is structured.
375374
376375
Raises:
377-
ValueError: If the payload returned by the Data Commons REST API is
378-
malformed.
376+
ValueError: If the payload is malformed.
379377
380378
Examples:
381379
We would like to get all :obj:`StatisticalPopulation` and
382-
:obj:`Observations` for all places of type :obj:`City` where the populations
383-
have a population type of :obj:`Person` is specified by the following
384-
constraining properties.
380+
:obj:`Observations` for all places of type :obj:`City` in year 2017 where
381+
the populations have a population type of :obj:`Person` is specified by the
382+
following constraining properties.
385383
386384
- Persons should have `age <https://browser.datacommons.org/kg?dcid=age>`_
387385
with value `Years5To17 <https://browser.datacommons.org/kg?dcid=Years5To17>`_
@@ -392,7 +390,7 @@ def get_place_obs(place_type, population_type, constraining_properties={}):
392390
... 'age': 'Years5To17',
393391
... 'placeOfBirth': 'BornInOtherStateInTheUnitedStates'
394392
... }
395-
>>> get_place_obs('City', 'Person', constraining_properties=props)
393+
>>> get_place_obs('City', '2017', Person', constraining_properties=props)
396394
[
397395
{
398396
'name': 'Marcus Hook borough',
@@ -401,68 +399,33 @@ def get_place_obs(place_type, population_type, constraining_properties={}):
401399
'dc/p/pq6frs32sfvk': {
402400
'observations': [
403401
{
404-
'id': 'dc/o/0005qml1el8qh',
405402
'marginOfError': 39,
406403
'measuredProp': 'count',
407404
'measuredValue': 67,
408-
'measurementMethod': 'CensusACS5yrSurvey',
409-
'observationDate': '2014',
410-
'provenanceId': 'dc/3j71hj1',
411-
'type': 'Observation'
412-
},
413-
{
414-
'id': 'dc/o/wvskpk5vyjkhb',
415-
'marginOfError': 33,
416-
'measuredProp': 'count',
417-
'measuredValue': 58,
418-
'measurementMethod': 'CensusACS5yrSurvey',
419-
'observationDate': '2015',
420-
'provenanceId': 'dc/3j71hj1',
421-
'type': 'Observation'
422-
},
423-
{
424-
'id': 'dc/o/3h44trf3vyrm3',
425-
'marginOfError': 36,
426-
'measuredProp': 'count',
427-
'measuredValue': 42,
428-
'measurementMethod': 'CensusACS5yrSurvey',
429-
'observationDate': '2011',
430-
'provenanceId': 'dc/3j71hj1',
431405
'type': 'Observation'
432406
},
433407
# More observations...
434408
],
435-
'provenanceId': 'dc/3j71hj1'
436409
}
437410
}
438411
},
439412
# Entries for more cities...
440413
]
441414
442415
The value returned by :code:`get_place_obs` is a :obj:`list` of
443-
:obj:`dict`'s. Each dictionary corresponds to :obj:`StatisticalPopulation`'s
416+
:obj:`dict`'s. Each dictionary corresponds to a :obj:`StatisticalPopulation`
444417
matching the given :code:`population_type` and
445418
:code:`constraining_properties` for a single place of the given
446419
:code:`place_type`. The dictionary contains the following keys.
447420
448421
- :code:`name`: The name of the place being described.
449422
- :code:`place`: The dcid associated with the place being described.
450423
- :code:`populations`: A :obj:`dict` mapping :code:`StatisticalPopulation`
451-
dcids to a a :obj:`dict` with a list of :code:`observations` and a the
452-
:code:`provenanceId` identifying the source that defined the
453-
:code:`StatisticalPopulation`.
424+
dcids to a a :obj:`dict` with a list of :code:`observations`.
454425
455426
Each :obj:`Observation` is represented by a :obj:`dict` with the following
456427
keys.
457-
458-
- :code:`id`: The :code:`dcid` identifying the :obj:`Observation`.
459-
- :code:`provenanceId`: The dcid identifying the source that defined this
460-
:obj:`Observation`.
461-
- :code:`type`: The type associated with the :obj:`Observation`.
462428
- :code:`measuredProp`: The property measured by the :obj:`Observation`.
463-
- :code:`observationDate`: The date when the :obj:`Observation` was made.
464-
- :code:`observationPeriod` (optional): The period over which the
465-
:obj:`Observation` was made.
466429
- :code:`measurementMethod` (optional): A field identifying how the
467430
:obj:`Observation` was made
468431
- Additional fields that denote values measured by the :obj:`Observation`.
@@ -475,6 +438,7 @@ def get_place_obs(place_type, population_type, constraining_properties={}):
475438
url = utils._API_ROOT + utils._API_ENDPOINTS['get_place_obs']
476439
payload = utils._send_request(url, req_json={
477440
'place_type': place_type,
441+
'observation_date': observation_date,
478442
'population_type': population_type,
479443
'pvs': pv,
480444
}, compress=True)

datacommons/test/populations_test.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def json(self):
137137
# Mock responses for post requests to get_place_obs
138138
if args[0] == utils._API_ROOT + utils._API_ENDPOINTS['get_place_obs']\
139139
and req['place_type'] == 'City'\
140+
and req['observation_date'] == '2017'\
140141
and req['population_type'] == 'Person'\
141142
and req['pvs'] == constrained_props:
142143
res_json = json.dumps({
@@ -148,17 +149,11 @@ def json(self):
148149
'dc/p/pq6frs32sfvk': {
149150
'observations': [
150151
{
151-
'id': 'dc/o/0005qml1el8qh',
152152
'marginOfError': 39,
153153
'measuredProp': 'count',
154154
'measuredValue': 67,
155-
'measurementMethod': 'CensusACS5yrSurvey',
156-
'observationDate': '2014',
157-
'provenanceId': 'dc/3j71hj1',
158-
'type': 'Observation'
159155
}
160156
],
161-
'provenanceId': 'dc/3j71hj1'
162157
}
163158
}
164159
}
@@ -512,7 +507,8 @@ def test_valid(self, post_mock):
512507
'placeOfBirth': 'BornInOtherStateInTheUnitedStates',
513508
'age': 'Years5To17'
514509
}
515-
place_obs = dc.get_place_obs('City', 'Person', constraining_properties=pvs)
510+
place_obs = dc.get_place_obs(
511+
'City', '2017', 'Person', constraining_properties=pvs)
516512
self.assertListEqual(place_obs, [
517513
{
518514
'name': 'Marcus Hook borough',
@@ -521,17 +517,11 @@ def test_valid(self, post_mock):
521517
'dc/p/pq6frs32sfvk': {
522518
'observations': [
523519
{
524-
'id': 'dc/o/0005qml1el8qh',
525520
'marginOfError': 39,
526521
'measuredProp': 'count',
527522
'measuredValue': 67,
528-
'measurementMethod': 'CensusACS5yrSurvey',
529-
'observationDate': '2014',
530-
'provenanceId': 'dc/3j71hj1',
531-
'type': 'Observation'
532523
}
533524
],
534-
'provenanceId': 'dc/3j71hj1'
535525
}
536526
}
537527
}

0 commit comments

Comments
 (0)