Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 78 additions & 15 deletions datacommons/examples/stat_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,27 +130,90 @@ def call_str(pvs):
try:
dc.get_stat_all(
dc.get_places_in(['country/USA'], 'County')['country/USA'], [
'Count_Person_Unemployed', 'Median_Age_Person', 'Count_Death',
'Count_Death_CertainInfectiousParasiticDiseases',
'Count_UnemploymentInsuranceClaim_StateUnemploymentInsurance',
'Count_Worker_NAICSAccommodationFoodServices',
'Count_Household_With0AvailableVehicles',
'Count_Person_WhiteAlone', 'Count_Person_WorkedFullTime',
'Count_Person_Employed', 'Count_Person_EnrolledInSchool',
'Count_Person', 'LandAreaSqMeter',
'PopulationDensityPerSqMeter',
'Count_Person_BlackOrAfricanAmericanAlone',
'PercentBlackOrAfricanAmericanAlone', 'Count_Person_Female',
'Count_Person_Male',
'Count_Person_AmericanIndianAndAlaskaNativeAlone',
'Count_Person_AmericanIndianAndAlaskaNativeAloneOrInCombinationWithOneOrMoreOtherRaces',
'Count_Person_AmericanIndianOrAlaskaNativeAlone',
'Count_Person_AsianAlone',
'Count_Person_AsianAloneOrInCombinationWithOneOrMoreOtherRaces',
'Count_Person_BlackOrAfricanAmericanAloneOrInCombinationWithOneOrMoreOtherRaces',
'Count_Person_HispanicOrLatino',
'Count_Person_NativeHawaiianAndOtherPacificIslanderAlone',
'Count_Person_NativeHawaiianAndOtherPacificIslanderAloneOrInCombinationWithOneOrMoreOtherRaces',
'Count_Person_NativeHawaiianOrOtherPacificIslanderAlone',
'Count_Person_SomeOtherRaceAlone',
'Count_Person_SomeOtherRaceAloneOrInCombinationWithOneOrMoreOtherRaces',
'Count_Person_TwoOrMoreRaces', 'Count_Person_WhiteAlone',
'Count_Person_WhiteAloneNotHispanicOrLatino',
'Count_Person_WhiteAloneOrInCombinationWithOneOrMoreOtherRaces',
'Count_Person_Upto5Years', 'Count_Person_Upto18Years',
'Count_Person_65OrMoreYears', 'Count_Person_75OrMoreYears',
'Count_Person_ForeignBorn',
'Count_Person_USCitizenByNaturalization',
'Count_Person_NotAUSCitizen', 'Count_Person_Nonveteran',
'Count_Person_Veteran', 'Count_Person_NotWorkedFullTime',
'Count_Person_WorkedFullTime', 'Count_Person_Employed',
'Count_Person_Unemployed', 'Count_Person_InLaborForce',
'Count_Person_IncomeOf10000To14999USDollar',
'Count_Person_IncomeOf15000To24999USDollar',
'Count_Person_IncomeOf25000To34999USDollar',
'Count_Person_IncomeOf35000To49999USDollar',
'Count_Person_IncomeOf50000To64999USDollar',
'Count_Person_IncomeOf65000To74999USDollar',
'Count_Person_IncomeOf75000OrMoreUSDollar',
'Count_Person_IncomeOfUpto9999USDollar',
'Count_Person_EnrolledInSchool',
'Count_Person_NotEnrolledInSchool',
'Count_Person_EnrolledInCollegeUndergraduateYears',
'Count_Person_EnrolledInGrade1ToGrade4',
'Count_Person_EnrolledInGrade5ToGrade8',
'Count_Person_EnrolledInGrade9ToGrade12',
'Count_Person_ResidesInGroupQuarters',
'Count_Person_NowMarried',
'RetailDrugDistribution_DrugDistribution_DMethamphetamine',
'Count_Household_With2Person',
'Count_Person_EnrolledInKindergarten',
'Count_Person_EnrolledInNurserySchoolPreschool',
'Count_Person_GraduateOrProfessionalSchool',
'Count_Person_EducationalAttainment10ThGrade',
'Count_Person_EducationalAttainment11ThGrade',
'Count_Person_EducationalAttainment12ThGradeNoDiploma',
'Count_Person_EducationalAttainment1StGrade',
'Count_Person_EducationalAttainment2NdGrade',
'Count_Person_EducationalAttainment3RdGrade',
'Count_Person_EducationalAttainment4ThGrade',
'Count_Person_EducationalAttainment5ThGrade',
'Count_Person_EducationalAttainment6ThGrade',
'Count_Person_EducationalAttainment7ThGrade',
'Count_Person_EducationalAttainment8ThGrade',
'Count_Person_EducationalAttainment9ThGrade',
'Count_Person_EducationalAttainmentAssociatesDegree',
'Count_Person_EducationalAttainmentBachelorsDegree',
'Count_Person_EducationalAttainmentBachelorsDegreeOrHigher',
'Count_Person_EducationalAttainmentDoctorateDegree',
'Count_Person_EducationalAttainmentGedOrAlternativeCredential',
'Count_Person_EducationalAttainmentKindergarten',
'Count_Person_EducationalAttainmentMastersDegree',
'Count_Person_EducationalAttainmentNoSchoolingCompleted',
'Count_Person_EducationalAttainmentNurserySchool',
'Count_Person_EducationalAttainmentPrimarySchool',
'Count_Person_EducationalAttainmentProfessionalSchoolDegree',
'Count_Person_EducationalAttainmentRegularHighSchoolDiploma',
'Count_Person_EducationalAttainmentSomeCollege1OrMoreYearsNoDegree',
'Count_Person_EducationalAttainmentSomeCollegeLessThan1Year',
'Count_Person_Divorced', 'Count_Person_MarriedAndNotSeparated',
'Count_Person_NeverMarried', 'Count_Person_Separated',
'Count_Person_Widowed', 'Count_Person_NowMarried',
'Count_Person_AbovePovertyLevelInThePast12Months',
'Count_Person_BelowPovertyLevelInThePast12Months',
'Percent_Person_20OrMoreYears_WithDiabetes',
'Percent_Person_20OrMoreYears_Obesity',
'Percent_Person_20OrMoreYears_PhysicalInactivity',
'Percent_Person_Upto64Years_NoHealthInsurance',
'Median_Age_Person', 'Median_Income_Person', 'Count_Death',
'Count_Death_CertainInfectiousParasiticDiseases',
'Count_Death_DiseasesOfBloodAndBloodFormingOrgansAndImmuneDisorders',
'Count_Household_NoComputer',
'Median_Income_Household_HouseholderRaceHispanicOrLatino,',
'Count_HousingUnit_RenterOccupied',
'Count_Worker_NAICSInformation'
'Count_Death_DiseasesOfTheRespiratorySystem'
])
except ValueError:
print('Stress test for get_stat_all FAILED!')
Expand Down
19 changes: 13 additions & 6 deletions datacommons/stat_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import datacommons.utils as utils

# stat_var specific batch size.
_STAT_BATCH_SIZE = 2000


def get_stat_value(place,
stat_var,
Expand Down Expand Up @@ -203,20 +206,24 @@ def get_stat_all(places, stat_vars):
}
"""
url = utils._API_ROOT + utils._API_ENDPOINTS['get_stat_all']
# Cast iterable-like to list.
places = list(places)
stat_vars = list(stat_vars)

# Aiming for _STAT_BATCH_SIZE entries total.
# _STAT_BATCH_SIZE = num places x num stat_vars, so aim for
# _STAT_BATCH_SIZE/len(stat_vars) places per batch.
places_per_batch = _STAT_BATCH_SIZE // len(stat_vars)
# Get number of batches via an arithmetic ceiling trick:
# 11//10 rounds down to 1.
# -11//10 rounds down to -2.
# We can divide with, then remove the negative to get the ceiling.
batches = -(-len(places) // utils._QUERY_BATCH_SIZE)
batches = -(-len(places) // places_per_batch)
res = {}
for i in range(batches):
req_json = {
'stat_vars':
stat_vars,
'places':
places[i * utils._QUERY_BATCH_SIZE:(i + 1) *
utils._QUERY_BATCH_SIZE]
'stat_vars': stat_vars,
'places': places[i * places_per_batch:(i + 1) * places_per_batch]
}
# Send the request
res_json = utils._send_request(url,
Expand Down