Beispiel #1
0
    def test_opt_args(self, urlopen):
        """Calling get_stat_value with optional args returns specific data."""
        # Call get_stat_value for specific obs
        self.assertEqual(dc.get_stat_value('geoId/06', 'Count_Person', '2010'),
                         133)

        # Call get_stat_value with all optional args
        stat = dc.get_stat_value('geoId/06', 'Count_Person', '2010',
                                 'CensusPEPSurvey', 'P1Y', 'RealPeople', 100)
        self.assertEqual(stat, 103)
    def __buildJsonDataFromSchema(self, dcid_to_city_name=None, category_name_to_field_name=None, outfile="dataset.json", verbose=False):
        if dcid_to_city_name is None:
            if self.dcid_to_city_name == {}:
                self.__loadSchema()
            dcids = self.dcid_to_city_name
        else:
            dcids = dcid_to_city_name

        if category_name_to_field_name is None:
            if self.category_name_to_field_name == {}:
                self.__loadSchema()
            category_names = self.category_name_to_field_name
        else:
            category_names = category_name_to_field_name

        for dcid, city_name in dcids.items():
            self.dataset[dcid] = {}
            self.dataset[dcid]["city_name"] = city_name
            if verbose:
                print("##############   ", city_name,
                      "   ##############", "\n\n")
            for cat_name, stat_name in category_names.items():
                # if stat_name in self.handler.not_supported:
                ns = self.handler.not_supported(dcid, stat_name)
                if ns[0]:
                    f = ns[1]
                    x = f(dcid)
                else:
                    x = dc.get_stat_value(dcid, stat_name)
                if verbose:
                    print(cat_name, ":", x)
                if math.isnan(x):
                    print("***Got NaN for", city_name, ",", cat_name)
                self.dataset[dcid][cat_name] = x

            print("Finished data collection for", city_name)
        self.writeDatasetToJsonFile(outfile)
        return
Beispiel #3
0
    def test_basic(self, urlopen):
        """Calling get_stat_value with minimal and proper args."""
        # Call get_stat_value

        self.assertEqual(dc.get_stat_value('geoId/06', 'Count_Person'), 123)
Beispiel #4
0
def main():
    param_sets = [
        {
            'place': 'geoId/06085',
            'stat_var': 'Count_Person',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'Count_Person',
            'date': '2018',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'Count_Person',
            'date': '2018',
            'measurement_method': 'CensusACS5yrSurvey',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'UnemploymentRate_Person',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'UnemploymentRate_Person',
            'observation_period': 'P1Y',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'UnemploymentRate_Person',
            'observation_period': 'P1Y',
            'measurement_method': 'BLSSeasonallyUnadjusted',
        },
        {
            'place': 'nuts/HU22',
            'stat_var':
            'Amount_EconomicActivity_GrossDomesticProduction_Nominal',
        },
        {
            'place': 'nuts/HU22',
            'stat_var':
            'Amount_EconomicActivity_GrossDomesticProduction_Nominal',
            'observation_period': 'P1Y',
            'unit': 'PurchasingPowerStandard'
        },
    ]

    def call_str(pvs):
        """Helper function to print the minimal call string."""
        s = "'{}', '{}'".format(pvs.get('place'), pvs.get('stat_var'))
        if pvs.get('measurement_method'):
            s += ", measurement_method='{}'".format(
                pvs.get('measurement_method'))
        if pvs.get('observation_period'):
            s += ", observation_period='{}'".format(
                pvs.get('observation_period'))
        if pvs.get('unit'):
            s += ", unit='{}'".format(pvs.get('unit'))
        if pvs.get('scaling_factor'):
            s += ", scaling_factor={}".format(pvs.get('scaling_factor'))
        return s

    for pvs in param_sets:
        print('\nget_stat_value({})'.format(call_str(pvs)))
        print(
            '>>> ',
            dc.get_stat_value(pvs.get('place'),
                              pvs.get('stat_var'),
                              date=pvs.get('date'),
                              measurement_method=pvs.get('measurement_method'),
                              observation_period=pvs.get('observation_period'),
                              unit=pvs.get('unit'),
                              scaling_factor=pvs.get('scaling_factor')))
    for pvs in param_sets:
        pvs.pop('date', None)
        print('\nget_stat_series({})'.format(call_str(pvs)))
        print(
            '>>> ',
            dc.get_stat_series(
                pvs.get('place'),
                pvs.get('stat_var'),
                measurement_method=pvs.get('measurement_method'),
                observation_period=pvs.get('observation_period'),
                unit=pvs.get('unit'),
                scaling_factor=pvs.get('scaling_factor')))

    pp = pprint.PrettyPrinter(indent=4)
    print(
        "\nget_stat_all(['geoId/06085', 'country/FRA'], ['Median_Age_Person', 'Count_Person'])"
    )
    print('>>> ')
    pp.pprint(
        dc.get_stat_all(['geoId/06085', 'country/FRA'],
                        ['Median_Age_Person', 'Count_Person']))

    print(
        "\nget_stat_all(['badPlaceId', 'country/FRA'], ['Median_Age_Person', 'Count_Person'])"
    )
    print('>>> ')
    pp.pprint(
        dc.get_stat_all(['badPlaceId', 'country/FRA'],
                        ['Median_Age_Person', 'Count_Person']))
def handle55To64(dcid):
    x1 = dc.get_stat_value(dcid, "Count_Person_55To59Years")
    x2 = dc.get_stat_value(dcid, "Count_Person_60To61Years")
    x3 = dc.get_stat_value(dcid, "Count_Person_62To64Years")
    return x1+x2+x3
Beispiel #6
0
def main():
    param_sets = [
        {
            'place': 'geoId/06085',
            'stat_var': "Count_Person",
        },
        {
            'place': 'geoId/06085',
            'stat_var': "Count_Person",
            'date': '2018',
        },
        {
            'place': 'geoId/06085',
            'stat_var': "Count_Person",
            'date': '2018',
            'measurement_method': 'CensusACS5yrSurvey',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'UnemploymentRate_Person',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'UnemploymentRate_Person',
            'observation_period': 'P1Y',
        },
        {
            'place': 'geoId/06085',
            'stat_var': 'UnemploymentRate_Person',
            'observation_period': 'P1Y',
            'measurement_method': 'BLSSeasonallyUnadjusted',
        },
        {
            'place': 'nuts/HU22',
            'stat_var':
            'Amount_EconomicActivity_GrossDomesticProduction_Nominal',
        },
        {
            'place': 'nuts/HU22',
            'stat_var':
            'Amount_EconomicActivity_GrossDomesticProduction_Nominal',
            'observation_period': 'P1Y',
            'unit': 'PurchasingPowerStandard'
        },
    ]

    def call_str(pvs):
        """Helper function to print the minimal call string."""
        s = "'{}', '{}'".format(pvs.get('place'), pvs.get('stat_var'))
        if pvs.get('measurement_method'):
            s += ", measurement_method='{}'".format(
                pvs.get('measurement_method'))
        if pvs.get('observation_period'):
            s += ", observation_period='{}'".format(
                pvs.get('observation_period'))
        if pvs.get('unit'):
            s += ", unit='{}'".format(pvs.get('unit'))
        if pvs.get('scaling_factor'):
            s += ", scaling_factor={}".format(pvs.get('scaling_factor'))
        return s

    for pvs in param_sets:
        print('\nget_stat_value({})'.format(call_str(pvs)))
        print(
            '>>> ',
            dc.get_stat_value(pvs.get('place'),
                              pvs.get('stat_var'),
                              date=pvs.get('date'),
                              measurement_method=pvs.get('measurement_method'),
                              observation_period=pvs.get('observation_period'),
                              unit=pvs.get('unit'),
                              scaling_factor=pvs.get('scaling_factor')))
    for pvs in param_sets:
        pvs.pop('date', None)
        print('\nget_stat_series({})'.format(call_str(pvs)))
        print(
            '>>> ',
            dc.get_stat_series(
                pvs.get('place'),
                pvs.get('stat_var'),
                measurement_method=pvs.get('measurement_method'),
                observation_period=pvs.get('observation_period'),
                unit=pvs.get('unit'),
                scaling_factor=pvs.get('scaling_factor')))

    pp = pprint.PrettyPrinter(indent=4)
    print(
        '\nget_stat_all(["geoId/06085", "country/FRA"], ["Median_Age_Person", "Count_Person"])'
    )
    print('>>> ')
    pp.pprint(
        dc.get_stat_all(["geoId/06085", "country/FRA"],
                        ["Median_Age_Person", "Count_Person"]))

    print(
        '\nget_stat_all(["badPlaceId", "country/FRA"], ["Median_Age_Person", "Count_Person"])'
    )
    print('>>> ')
    pp.pprint(
        dc.get_stat_all(["badPlaceId", "country/FRA"],
                        ["Median_Age_Person", "Count_Person"]))

    print('\nSTRESS TEST FOR GET_STAT_ALL')
    try:
        dc.get_stat_all(
            dc.get_places_in(['country/USA'], 'County')['country/USA'], [
                '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_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_Death_DiseasesOfTheRespiratorySystem'
            ])
    except ValueError:
        print('Stress test for get_stat_all FAILED!')
    else:
        print('Stress test for get_stat_all succeeded.')
Beispiel #7
0
import datacommons as dc
res = dc.get_stat_value("presinctId/271730145", "Count_Person", date="2018")
print(res)