Esempio n. 1
0
def get_child_households_profile(geo_code, geo_level, session):
    # head of household
    # gender
    head_gender_dist, total_households = get_stat_data(
            ['gender of head of household'], geo_level, geo_code, session,
            order_by='gender of head of household',
            table_name='genderofheadofhouseholdunder18')
    female_heads = head_gender_dist['Female']['numerators']['this']

    # annual household income
    income_dist_data, _ = get_stat_data(
            ['annual household income'], geo_level, geo_code, session,
            exclude=['Unspecified'],
            recode=HOUSEHOLD_INCOME_RECODE,
            key_order=HOUSEHOLD_INCOME_RECODE.values(),
            table_name='annualhouseholdincomeunder18')

    # median income
    median = calculate_median_stat(income_dist_data)
    median_income = HOUSEHOLD_INCOME_ESTIMATE[median]

    # type of dwelling
    type_of_dwelling_dist, _ = get_stat_data(
            ['type of main dwelling'], geo_level, geo_code, session,
            recode=TYPE_OF_DWELLING_RECODE,
            order_by='-total')
    informal = type_of_dwelling_dist['Shack']['numerators']['this']

    # size of household
    household_size_dist, _ = get_stat_data(
        ['household size', 'age of household head'],
        geo_level, geo_code, session
    )

    return {
        'total_households': {
            'name': 'Households with heads under 18 years old',
            'values': {'this': total_households},
        },
        'type_of_dwelling_distribution': type_of_dwelling_dist,
        'informal': {
            'name': 'Child-headed households that are informal dwellings (shacks)',
            'values': {'this': percent(informal, total_households)},
            'numerators': {'this': informal},
        },
        'annual_income_distribution': income_dist_data,
        'median_annual_income': {
            'name': 'Average annual child-headed household income',
            'values': {'this': median_income},
        },
        'household_size_distribution': household_size_dist,
        'head_of_household': {
            'gender_distribution': head_gender_dist,
            'female': {
                'name': 'Child-headed households with women as their head',
                'values': {'this': percent(female_heads, total_households)},
                'numerators': {'this': female_heads},
                },
        },
    }
Esempio n. 2
0
def get_child_households_profile(geo, session):
    # head of household
    # gender
    head_gender_dist, total_households = get_stat_data(
            ['gender of head of household'], geo, session,
            order_by='gender of head of household',
            table_name='genderofheadofhouseholdunder18')
    female_heads = head_gender_dist['Female']['numerators']['this']

    # annual household income
    if geo.version == '2011':
        HOUSEHOLD_INCOME_RECODE = HOUSEHOLD_INCOME_RECODE_2011
    else:
        HOUSEHOLD_INCOME_RECODE = COLLAPSED_ANNUAL_INCOME_CATEGORIES
    income_dist_data, _ = get_stat_data(
            ['annual household income'], geo, session,
            exclude=['Unspecified'],
            recode=HOUSEHOLD_INCOME_RECODE,
            key_order=HOUSEHOLD_INCOME_RECODE.values(),
            table_name='annualhouseholdincomeunder18')

    # median income
    median = calculate_median_stat(income_dist_data)
    median_income = HOUSEHOLD_INCOME_ESTIMATE[median]

    # type of dwelling
    type_of_dwelling_dist, _ = get_stat_data(
            ['type of main dwelling'], geo, session,
            recode=TYPE_OF_DWELLING_RECODE,
            order_by='-total')
    informal = type_of_dwelling_dist['Shack']['numerators']['this']

    return {
        'total_households': {
            'name': 'Households with heads under 18 years old',
            'values': {'this': total_households},
        },
        'type_of_dwelling_distribution': type_of_dwelling_dist,
        'informal': {
            'name': 'Child-headed households that are informal dwellings (shacks)',
            'values': {'this': percent(informal, total_households)},
            'numerators': {'this': informal},
        },
        'annual_income_distribution': income_dist_data,
        'median_annual_income': {
            'name': 'Average annual child-headed household income',
            'values': {'this': median_income},
        },
        'head_of_household': {
            'gender_distribution': head_gender_dist,
            'female': {
                'name': 'Child-headed households with women as their head',
                'values': {'this': percent(female_heads, total_households)},
                'numerators': {'this': female_heads},
                },
        },
    }
Esempio n. 3
0
def get_economics_profile(geo_code, geo_level, session):
    # income
    income_dist_data, total_workers = get_stat_data(
            ['employed individual monthly income'], geo_level, geo_code, session,
            exclude=['Not applicable'],
            recode=COLLAPSED_INCOME_CATEGORIES,
            key_order=COLLAPSED_INCOME_CATEGORIES.values())

    # median income
    median = calculate_median_stat(income_dist_data)
    median_income = ESTIMATED_INCOME_CATEGORIES[median]

    # employment status
    employ_status, total_workers = get_stat_data(
            ['official employment status'], geo_level, geo_code, session,
            exclude=['Age less than 15 years', 'Not applicable'],
            order_by='official employment status',
            table_name='officialemploymentstatus')

    # sector
    sector_dist_data, _ = get_stat_data(
            ['type of sector'], geo_level, geo_code, session,
            exclude=['Not applicable'],
            order_by='type of sector')

    # access to internet
    internet_access_dist, total_with_access = get_stat_data(
            ['access to internet'], geo_level, geo_code, session, exclude=['No access to internet'],
            order_by='access to internet')
    _, total_without_access = get_stat_data(
            ['access to internet'], geo_level, geo_code, session, only=['No access to internet'])
    total_households = total_with_access + total_without_access

    return {'individual_income_distribution': income_dist_data,
            'median_individual_income': {
                'name': 'Average monthly income',
                'values': {'this': median_income},
                },
            'employment_status': employ_status,
            'sector_type_distribution': sector_dist_data,
            'internet_access_distribution': internet_access_dist,
            'internet_access': {
                'name': 'Households with internet access',
                'values': {'this': percent(total_with_access, total_households)},
                'numerators': {'this': total_with_access},
                }
            }
Esempio n. 4
0
def get_children_profile(geo, session):
    profile = {}
    # age
    child_adult_dist, _ = get_stat_data(
        ['age in completed years'], geo, session,
        table_name='ageincompletedyearssimplified',
        recode={'< 18': 'Children (< 18)',
                '18 to 64': 'Adults (>= 18)',
                '>= 65': 'Adults (>= 18)'})

    # parental survival
    survival, total = get_stat_data(
        ['mother alive', 'father alive'], geo, session)

    parental_survival_dist = OrderedDict()
    parental_survival_dist['metadata'] = survival['metadata']

    parental_survival_dist['Both parents'] = survival['Yes']['Yes']
    parental_survival_dist['Both parents']['name'] = 'Both parents'

    parental_survival_dist['Neither parent'] = survival['No']['No']
    parental_survival_dist['Neither parent']['name'] = 'Neither parent'

    parental_survival_dist['One parent'] = survival['Yes']['No']
    parental_survival_dist['One parent']['numerators']['this'] += survival['No']['Yes']['numerators']['this']

    rest = (total - parental_survival_dist['Both parents']['values']['this']
            - parental_survival_dist['Neither parent']['values']['this']
            - parental_survival_dist['One parent']['values']['this'])

    parental_survival_dist['Uncertain'] = {
        'name': 'Uncertain',
        'numerators': {'this': rest},
    }

    # calculate percentage
    for data in parental_survival_dist.itervalues():
        if 'numerators' in data:
            data['values'] = {'this': percent(data['numerators']['this'], total)}

    # gender
    gender_dist, _ = get_stat_data(
        ['gender'], geo, session,
        table_name='genderunder18')

    # school

    # NOTE: this data is incompatible with some views (check out
    # https://github.com/censusreporter/censusreporter/issues/78)
    #
    # school_attendance_dist, total_school_aged = get_stat_data(
    #     ['present school attendance', 'age in completed years'],
    #     geo, session,
    # )
    # school_attendance_dist['Yes']['metadata'] = \
    #         school_attendance_dist['metadata']
    # school_attendance_dist = school_attendance_dist['Yes']
    # total_attendance = sum(d['numerators']['this'] for d in
    #                        school_attendance_dist.values()
    #                        if 'numerators' in d)

    # school attendance
    school_attendance_dist, total_school_aged = get_stat_data(
        ['present school attendance'],
        geo, session,
        recode=COLLAPSED_ATTENDANCE_CATEGORIES,
    )
    total_attendance = school_attendance_dist['Yes']['numerators']['this']

    # education level
    education17_dist, _ = get_stat_data(
        ['highest educational level'],
        geo, session,
        recode=COLLAPSED_EDUCATION_CATEGORIES,
        table_name='highesteducationallevel17',
        key_order=EDUCATION_KEY_ORDER,
    )

    # employment
    employment_dist, total_15to17 = get_stat_data(
        ['official employment status'],
        geo, session,
        table_name='officialemploymentstatus15to17',
        exclude=['Not applicable']
    )
    total_in_labour_force = float(sum(v["numerators"]["this"] for k, v
                                      in employment_dist.iteritems()
                                      if COLLAPSED_EMPLOYMENT_CATEGORIES.get(k, None)
                                      == 'In labour force'))

    employment_indicators = {
        'percent_in_labour_force': {
            "name": "Of children between 15 and 17 are in the labour force",
            "numerators": {"this": total_in_labour_force},
            "values": {"this": percent(total_in_labour_force, total_15to17)}
        },
        'employment_distribution': employment_dist,
    }
    # median income
    if geo.version == '2011':
        recode = COLLAPSED_MONTHLY_INCOME_CATEGORIES
        fields = ['individual monthly income']
        table_name = 'individualmonthlyincome15to17'
        income_dist_data, total_workers = get_stat_data(
            fields, geo, session,
            exclude=['Not applicable'],
            recode=recode,
            key_order=recode.values(),
            table_name=table_name
        )
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_MONTHLY_INCOME_CATEGORIES[median]
        employment_indicators.update({
            'median_income': {
                'name': 'Average monthly income of employed children between 15 and 17',
                'values': {'this': median_income},
            }
        })
    else:
        recode = COLLAPSED_ANNUAL_INCOME_CATEGORIES
        fields = ['individual annual income']
        table_name = 'individualannualincome15to17'
        income_dist_data, total_workers = get_stat_data(
            fields, geo, session,
            exclude=['Not applicable'],
            recode=recode,
            key_order=recode.values(),
            table_name=table_name
        )
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_ANNUAL_INCOME_CATEGORIES[median]
        employment_indicators.update({
            'median_annual_income': {
                'name': 'Average annual income of employed children between 15 and 17',
                'values': {'this': median_income},
            }
        })

    profile.update({
        'demographics': {
            'child_adult_distribution': child_adult_dist,
            'total_children': {
                "name": "Children",
                "values": {"this": child_adult_dist['Children (< 18)']['numerators']['this']}
            },
            'gender_distribution': gender_dist,
            'parental_survival_distribution': parental_survival_dist,
            'percent_no_parent': {
                "name": "Of children 14 and under have no living biological parents",
                "values": parental_survival_dist["Neither parent"]['values'],
                "numerators": parental_survival_dist["Neither parent"]['numerators'],
            },
        },
        'school': {
            'school_attendance_distribution': school_attendance_dist,
            'percent_school_attendance': {
                "name": "School-aged children (5 to 17 years old) are in school",
                "numerators": {"this": total_school_aged},
                "values": {"this": percent(float(total_attendance),
                                           float(total_school_aged))}
            },
            'education17_distribution': education17_dist,
        },
        'employment': employment_indicators
    })
    return profile
Esempio n. 5
0
def get_economics_profile(geo, session):
    profile = {}
    # income
    if geo.version == '2011':
        # distribution
        recode = COLLAPSED_MONTHLY_INCOME_CATEGORIES
        fields = ['employed individual monthly income']
        income_dist_data, total_workers = get_stat_data(
            fields, geo, session,
            exclude=['Not applicable'],
            recode=recode,
            key_order=recode.values())

        # median income
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_MONTHLY_INCOME_CATEGORIES[median]
        profile.update({
            'individual_income_distribution': income_dist_data,
            'median_individual_income': {
                'name': 'Average monthly income',
                'values': {'this': median_income},
            }
        })
    else:
        # distribution
        recode = COLLAPSED_ANNUAL_INCOME_CATEGORIES
        fields = ['employed individual annual income']
        income_dist_data, total_workers = get_stat_data(
            fields, geo, session,
            exclude=['Not applicable'],
            recode=recode,
            key_order=recode.values())

        # median income
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_ANNUAL_INCOME_CATEGORIES[median]
        profile.update({
            'individual_annual_income_distribution': income_dist_data,
            'median_annual_individual_income': {
                'name': 'Average annual income',
                'values': {'this': median_income},
            }
        })

    # employment status
    employ_status, total_workers = get_stat_data(
            ['official employment status'], geo, session,
            exclude=['Age less than 15 years', 'Not applicable'],
            order_by='official employment status',
            table_name='officialemploymentstatus')

    # sector
    sector_dist_data, _ = get_stat_data(
            ['type of sector'], geo, session,
            exclude=['Not applicable'],
            order_by='type of sector')

    # access to internet
    internet_access_dist, total_with_access = get_stat_data(
            ['access to internet'], geo, session, exclude=['No access to internet'],
            order_by='access to internet')
    _, total_without_access = get_stat_data(
            ['access to internet'], geo, session, only=['No access to internet'])
    total_households = total_with_access + total_without_access

    profile.update({
        'employment_status': employ_status,
        'sector_type_distribution': sector_dist_data,
        'internet_access_distribution': internet_access_dist,
        'internet_access': {
            'name': 'Households with internet access',
            'values': {'this': percent(total_with_access, total_households)},
            'numerators': {'this': total_with_access},
        }
    })

    return profile
Esempio n. 6
0
def get_households_profile(geo, session):
    # head of household
    # gender
    head_gender_dist, total_households = get_stat_data(
            ['gender of household head'], geo, session,
            order_by='gender of household head')
    female_heads = head_gender_dist['Female']['numerators']['this']

    # age
    db_model_u18 = get_model_from_fields(
        ['gender of head of household'], geo.geo_level,
        table_name='genderofheadofhouseholdunder18'
    )
    objects = get_objects_by_geo(db_model_u18, geo, session)
    total_under_18 = float(sum(o[0] for o in objects))

    # tenure
    tenure_data, _ = get_stat_data(
            ['tenure status'], geo, session,
            recode=HOUSEHOLD_OWNERSHIP_RECODE,
            order_by='tenure status')
    owned = 0
    for key, data in tenure_data.iteritems():
        if key.startswith('Owned'):
            owned += data['numerators']['this']

    # annual household income
    if geo.version == '2011':
        HOUSEHOLD_INCOME_RECODE = HOUSEHOLD_INCOME_RECODE_2011
    else:
        HOUSEHOLD_INCOME_RECODE = COLLAPSED_ANNUAL_INCOME_CATEGORIES
    income_dist_data, _ = get_stat_data(
            ['annual household income'], geo, session,
            exclude=['Unspecified', 'Not applicable'],
            recode=HOUSEHOLD_INCOME_RECODE,
            key_order=HOUSEHOLD_INCOME_RECODE.values(),
            table_name='annualhouseholdincome_genderofhouseholdhead')

    # median income
    median = calculate_median_stat(income_dist_data)
    median_income = HOUSEHOLD_INCOME_ESTIMATE[median]

    # type of dwelling
    type_of_dwelling_dist, _ = get_stat_data(
            ['type of dwelling'], geo, session,
            recode=TYPE_OF_DWELLING_RECODE,
            order_by='-total')
    informal = type_of_dwelling_dist['Shack']['numerators']['this']

    # household goods
    household_goods, _ = get_stat_data(
            ['household goods'], geo, session,
            recode=HOUSEHOLD_GOODS_RECODE,
            key_order=sorted(HOUSEHOLD_GOODS_RECODE.values()))

    return {'total_households': {
                'name': 'Households',
                'values': {'this': total_households},
                },
            'owned': {
                'name': 'Households fully owned or being paid off',
                'values': {'this': percent(owned, total_households)},
                'numerators': {'this': owned},
                },
            'type_of_dwelling_distribution': type_of_dwelling_dist,
            'informal': {
                'name': 'Households that are informal dwellings (shacks)',
                'values': {'this': percent(informal, total_households)},
                'numerators': {'this': informal},
                },
            'tenure_distribution': tenure_data,
            'household_goods': household_goods,
            'annual_income_distribution': income_dist_data,
            'median_annual_income': {
                'name': 'Average annual household income',
                'values': {'this': median_income},
                },
            'head_of_household': {
                'gender_distribution': head_gender_dist,
                'female': {
                    'name': 'Households with women as their head',
                    'values': {'this': percent(female_heads, total_households)},
                    'numerators': {'this': female_heads},
                    },
                'under_18': {
                    'name': 'Households with heads under 18 years old',
                    'values': {'this': total_under_18},
                    }
                },
           }
Esempio n. 7
0
def get_children_profile(geo_code, geo_level, session):
    # age
    child_adult_dist, _ = get_stat_data(
        ['age in completed years'], geo_level, geo_code, session,
        table_name='ageincompletedyearssimplified',
        recode={'< 18': 'Children (< 18)',
                '18 to 64': 'Adults (>= 18)',
                '>= 65': 'Adults (>= 18)'})

    # parental survival
    survival, total = get_stat_data(
        ['mother alive', 'father alive'], geo_level, geo_code, session)

    parental_survival_dist = OrderedDict()
    parental_survival_dist['metadata'] = survival['metadata']

    parental_survival_dist['Both parents'] = survival['Yes']['Yes']
    parental_survival_dist['Both parents']['name'] = 'Both parents'

    parental_survival_dist['Neither parent'] = survival['No']['No']
    parental_survival_dist['Neither parent']['name'] = 'Neither parent'

    parental_survival_dist['One parent'] = survival['Yes']['No']
    parental_survival_dist['One parent']['numerators']['this'] += survival['No']['Yes']['numerators']['this']

    rest = (total - parental_survival_dist['Both parents']['values']['this']
            - parental_survival_dist['Neither parent']['values']['this']
            - parental_survival_dist['One parent']['values']['this'])

    parental_survival_dist['Uncertain'] = {
        'name': 'Uncertain',
        'numerators': {'this': rest},
    }

    # calculate percentage
    for data in parental_survival_dist.itervalues():
        if 'numerators' in data:
            data['values'] = {'this': percent(data['numerators']['this'], total)}

    # gender
    gender_dist, _ = get_stat_data(
        ['gender'], geo_level, geo_code, session,
        table_name='genderunder18')

    # school

    # NOTE: this data is incompatible with some views (check out
    # https://github.com/censusreporter/censusreporter/issues/78)
    #
    # school_attendance_dist, total_school_aged = get_stat_data(
    #     ['present school attendance', 'age in completed years'],
    #     geo_level, geo_code, session,
    # )
    # school_attendance_dist['Yes']['metadata'] = \
    #         school_attendance_dist['metadata']
    # school_attendance_dist = school_attendance_dist['Yes']
    # total_attendance = sum(d['numerators']['this'] for d in
    #                        school_attendance_dist.values()
    #                        if 'numerators' in d)

    # school attendance
    school_attendance_dist, total_school_aged = get_stat_data(
        ['present school attendance'],
        geo_level, geo_code, session,
    )
    total_attendance = school_attendance_dist['Yes']['numerators']['this']

    # education level
    education17_dist, _ = get_stat_data(
        ['highest educational level'],
        geo_level, geo_code, session,
        recode=COLLAPSED_EDUCATION_CATEGORIES,
        table_name='highesteducationallevel17',
        key_order=EDUCATION_KEY_ORDER,
    )

    # employment
    employment_dist, total_15to17 = get_stat_data(
        ['official employment status'],
        geo_level, geo_code, session,
        table_name='officialemploymentstatus15to17',
        exclude=['Not applicable']
    )
    total_in_labour_force = float(sum(v["numerators"]["this"] for k, v
                                      in employment_dist.iteritems()
                                      if COLLAPSED_EMPLOYMENT_CATEGORIES.get(k, None)
                                      == 'In labour force'))

    # median income
    income_dist_data, total_workers = get_stat_data(
        ['individual monthly income'], geo_level, geo_code, session,
        exclude=['Not applicable'],
        recode=COLLAPSED_INCOME_CATEGORIES,
        key_order=COLLAPSED_INCOME_CATEGORIES.values(),
        table_name='individualmonthlyincome15to17'
    )
    median = calculate_median_stat(income_dist_data)
    median_income = ESTIMATED_INCOME_CATEGORIES[median]

    return {
        'demographics': {
            'child_adult_distribution': child_adult_dist,
            'total_children': {
                "name": "Children",
                "values": {"this": child_adult_dist['Children (< 18)']['numerators']['this']}
            },
            'gender_distribution': gender_dist,
            'parental_survival_distribution': parental_survival_dist,
            'percent_no_parent': {
                "name": "Of children 14 and under have no living biological parents",
                "values": parental_survival_dist["Neither parent"]['values'],
                "numerators": parental_survival_dist["Neither parent"]['numerators'],
            },
        },
        'school': {
            'school_attendance_distribution': school_attendance_dist,
            'percent_school_attendance': {
                "name": "School-aged children (5 to 17 years old) are in school",
                "numerators": {"this": total_school_aged},
                "values": {"this": percent(float(total_attendance),
                                           float(total_school_aged))}
            },
            'education17_distribution': education17_dist,
        },
        'employment': {
            'percent_in_labour_force': {
                "name": "Of children between 15 and 17 are in the labour force",
                "numerators": {"this": total_in_labour_force},
                "values": {"this": percent(total_in_labour_force, total_15to17)}
            },
            'employment_distribution': employment_dist,
            'median_income': {
                'name': 'Average monthly income of employed children between 15 and 17',
                'values': {'this': median_income},
            },
        }
    }
Esempio n. 8
0
def get_households_profile(geo_code, geo_level, session):
    # head of household
    # gender
    head_gender_dist, total_households = get_stat_data(
            ['gender of household head'], geo_level, geo_code, session,
            order_by='gender of household head')
    female_heads = head_gender_dist['Female']['numerators']['this']

    # age
    db_model_u18 = get_model_from_fields(
        ['gender of head of household'], geo_level,
        table_name='genderofheadofhouseholdunder18'
    )
    objects = get_objects_by_geo(db_model_u18, geo_code, geo_level, session)
    total_under_18 = float(sum(o[0] for o in objects))

    # tenure
    tenure_data, _ = get_stat_data(
            ['tenure status'], geo_level, geo_code, session,
            order_by='tenure status')
    owned = 0
    for key, data in tenure_data.iteritems():
        if key.startswith('Owned'):
            owned += data['numerators']['this']

    # annual household income
    income_dist_data, _ = get_stat_data(
            ['annual household income'], geo_level, geo_code, session,
            exclude=['Unspecified'],
            recode=HOUSEHOLD_INCOME_RECODE,
            key_order=HOUSEHOLD_INCOME_RECODE.values(),
            table_name='annualhouseholdincome_genderofhouseholdhead')

    # median income
    median = calculate_median_stat(income_dist_data)
    median_income = HOUSEHOLD_INCOME_ESTIMATE[median]

    # type of dwelling
    type_of_dwelling_dist, _ = get_stat_data(
            ['type of dwelling'], geo_level, geo_code, session,
            recode=TYPE_OF_DWELLING_RECODE,
            order_by='-total')
    informal = type_of_dwelling_dist['Shack']['numerators']['this']

    # household goods
    household_goods, _ = get_stat_data(
            ['household goods'], geo_level, geo_code, session,
            total=total_households,
            recode=HOUSEHOLD_GOODS_RECODE,
            exclude=['total households'],
            key_order=sorted(HOUSEHOLD_GOODS_RECODE.values()))

    return {'total_households': {
                'name': 'Households',
                'values': {'this': total_households},
                },
            'owned': {
                'name': 'Households fully owned or being paid off',
                'values': {'this': percent(owned, total_households)},
                'numerators': {'this': owned},
                },
            'type_of_dwelling_distribution': type_of_dwelling_dist,
            'informal': {
                'name': 'Households that are informal dwellings (shacks)',
                'values': {'this': percent(informal, total_households)},
                'numerators': {'this': informal},
                },
            'tenure_distribution': tenure_data,
            'household_goods': household_goods,
            'annual_income_distribution': income_dist_data,
            'median_annual_income': {
                'name': 'Average annual household income',
                'values': {'this': median_income},
                },
            'head_of_household': {
                'gender_distribution': head_gender_dist,
                'female': {
                    'name': 'Households with women as their head',
                    'values': {'this': percent(female_heads, total_households)},
                    'numerators': {'this': female_heads},
                    },
                'under_18': {
                    'name': 'Households with heads under 18 years old',
                    'values': {'this': total_under_18},
                    }
                },
           }
Esempio n. 9
0
def get_child_households_profile(geo, session):
    # head of household
    # gender
    head_gender_dist, total_households = get_stat_data(
        ["gender of head of household"],
        geo,
        session,
        table_universe="Households headed by children under 18",
        order_by="gender of head of household",
    )
    female_heads = head_gender_dist["Female"]["numerators"]["this"]

    # annual household income
    if geo.version == "2011":
        HOUSEHOLD_INCOME_RECODE = HOUSEHOLD_INCOME_RECODE_2011
    else:
        HOUSEHOLD_INCOME_RECODE = COLLAPSED_ANNUAL_INCOME_CATEGORIES
    income_dist_data, _ = get_stat_data(
        ["annual household income"],
        geo,
        session,
        exclude=["Unspecified"],
        recode=HOUSEHOLD_INCOME_RECODE,
        key_order=HOUSEHOLD_INCOME_RECODE.values(),
        table_name="annualhouseholdincomeunder18",
    )

    # median income
    median = calculate_median_stat(income_dist_data)
    median_income = HOUSEHOLD_INCOME_ESTIMATE[median]

    # type of dwelling
    type_of_dwelling_dist, _ = get_stat_data(
        ["type of main dwelling"],
        geo,
        session,
        recode=TYPE_OF_DWELLING_RECODE,
        order_by="-total",
    )
    informal = type_of_dwelling_dist["Shack"]["numerators"]["this"]

    return {
        "total_households": {
            "name": "Households with heads under 18 years old",
            "values": {"this": total_households},
        },
        "type_of_dwelling_distribution": type_of_dwelling_dist,
        "informal": {
            "name": "Child-headed households that are informal dwellings (shacks)",
            "values": {"this": percent(informal, total_households)},
            "numerators": {"this": informal},
        },
        "annual_income_distribution": income_dist_data,
        "median_annual_income": {
            "name": "Average annual child-headed household income",
            "values": {"this": median_income},
        },
        "head_of_household": {
            "gender_distribution": head_gender_dist,
            "female": {
                "name": "Child-headed households with women as their head",
                "values": {"this": percent(female_heads, total_households)},
                "numerators": {"this": female_heads},
            },
        },
    }
Esempio n. 10
0
def get_children_profile(geo, session):
    profile = {}
    # age
    child_adult_dist, _ = get_stat_data(
        ["age in completed years"],
        geo,
        session,
        table_name="ageincompletedyearssimplified",
        recode={
            "< 18": "Children (< 18)",
            "18 to 64": "Adults (>= 18)",
            ">= 65": "Adults (>= 18)",
        },
        key_order=["Children (< 18)", "Adults (>= 18)"],
    )

    # parental survival
    survival, total = get_stat_data(["mother alive", "father alive"], geo, session)

    parental_survival_dist = OrderedDict()
    parental_survival_dist["metadata"] = survival["metadata"]

    parental_survival_dist["Both parents"] = survival["Yes"]["Yes"]
    parental_survival_dist["Both parents"]["name"] = "Both parents"

    parental_survival_dist["Neither parent"] = survival["No"]["No"]
    parental_survival_dist["Neither parent"]["name"] = "Neither parent"

    parental_survival_dist["One parent"] = survival["Yes"]["No"]
    parental_survival_dist["One parent"]["numerators"]["this"] += survival["No"]["Yes"][
        "numerators"
    ]["this"]

    rest = (
        total
        - parental_survival_dist["Both parents"]["numerators"]["this"]
        - parental_survival_dist["Neither parent"]["numerators"]["this"]
        - parental_survival_dist["One parent"]["numerators"]["this"]
    )

    parental_survival_dist["Uncertain"] = {
        "name": "Uncertain",
        "numerators": {"this": rest},
        "values": {"this": percent(rest, total)},
    }

    # gender
    gender_dist, _ = get_stat_data(
        ["gender"], geo, session, table_universe="Children under 18"
    )

    # school

    # NOTE: this data is incompatible with some views (check out
    # https://github.com/censusreporter/censusreporter/issues/78)
    #
    # school_attendance_dist, total_school_aged = get_stat_data(
    #     ['present school attendance', 'age in completed years'],
    #     geo, session,
    # )
    # school_attendance_dist['Yes']['metadata'] = \
    #         school_attendance_dist['metadata']
    # school_attendance_dist = school_attendance_dist['Yes']
    # total_attendance = sum(d['numerators']['this'] for d in
    #                        school_attendance_dist.values()
    #                        if 'numerators' in d)

    # school attendance
    school_attendance_dist, total_school_aged = get_stat_data(
        ["present school attendance"],
        geo,
        session,
        recode=COLLAPSED_ATTENDANCE_CATEGORIES,
    )
    total_attendance = school_attendance_dist["Yes"]["numerators"]["this"]

    # education level
    education17_dist, _ = get_stat_data(
        ["highest educational level"],
        geo,
        session,
        table_universe="17-year-old children",
        recode=COLLAPSED_EDUCATION_CATEGORIES,
        key_order=EDUCATION_KEY_ORDER,
    )

    # employment
    employment_dist, total_15to17 = get_stat_data(
        ["official employment status"],
        geo,
        session,
        table_universe="Children 15 to 17",
        exclude=["Not applicable"],
    )
    total_in_labour_force = float(
        sum(
            v["numerators"]["this"]
            for k, v in employment_dist.iteritems()
            if COLLAPSED_EMPLOYMENT_CATEGORIES.get(k, None) == "In labour force"
        )
    )

    employment_indicators = {
        "percent_in_labour_force": {
            "name": "Of children between 15 and 17 are in the labour force",
            "numerators": {"this": total_in_labour_force},
            "values": {"this": percent(total_in_labour_force, total_15to17)},
        },
        "employment_distribution": employment_dist,
    }
    # median income
    # monthly or annual
    if geo.version == "2011":
        income_dist_data, total_workers = get_stat_data(
            ["individual monthly income"],
            geo,
            session,
            table_universe="Children 15 to 17 who are employed",
            exclude=["Not applicable"],
            recode=COLLAPSED_MONTHLY_INCOME_CATEGORIES,
            key_order=COLLAPSED_MONTHLY_INCOME_CATEGORIES.values(),
        )
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_MONTHLY_INCOME_CATEGORIES[median]
        employment_indicators.update(
            {
                "median_income": {
                    "name": "Average monthly income of employed children between 15 and 17",
                    "values": {"this": median_income},
                }
            }
        )
    else:
        income_dist_data, total_workers = get_stat_data(
            ["individual annual income"],
            geo,
            session,
            table_universe="Children 15 to 17 who are employed",
            exclude=["Not applicable"],
            recode=COLLAPSED_ANNUAL_INCOME_CATEGORIES,
            key_order=COLLAPSED_ANNUAL_INCOME_CATEGORIES.values(),
        )
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_ANNUAL_INCOME_CATEGORIES[median]
        employment_indicators.update(
            {
                "median_annual_income": {
                    "name": "Average annual income of employed children between 15 and 17",
                    "values": {"this": median_income},
                }
            }
        )

    profile.update(
        {
            "demographics": {
                "child_adult_distribution": child_adult_dist,
                "total_children": {
                    "name": "Children",
                    "values": {
                        "this": child_adult_dist["Children (< 18)"]["numerators"][
                            "this"
                        ]
                    },
                },
                "gender_distribution": gender_dist,
                "parental_survival_distribution": parental_survival_dist,
                "percent_no_parent": {
                    "name": "Of children 14 and under have no living biological parents",
                    "values": parental_survival_dist["Neither parent"]["values"],
                    "numerators": parental_survival_dist["Neither parent"][
                        "numerators"
                    ],
                },
            },
            "school": {
                "school_attendance_distribution": school_attendance_dist,
                "percent_school_attendance": {
                    "name": "School-aged children (5 to 17 years old) are in school",
                    "numerators": {"this": total_school_aged},
                    "values": {
                        "this": percent(
                            float(total_attendance), float(total_school_aged)
                        )
                    },
                },
                "education17_distribution": education17_dist,
            },
            "employment": employment_indicators,
        }
    )
    return profile
Esempio n. 11
0
def get_economics_profile(geo, session):
    profile = {}
    # income
    if geo.version == "2011":
        # distribution
        recode = COLLAPSED_MONTHLY_INCOME_CATEGORIES
        fields = ["employed individual monthly income"]
        income_dist_data, total_workers = get_stat_data(
            fields,
            geo,
            session,
            exclude=["Not applicable"],
            recode=recode,
            key_order=recode.values(),
        )

        # median income
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_MONTHLY_INCOME_CATEGORIES[median]
        profile.update(
            {
                "individual_income_distribution": income_dist_data,
                "median_individual_income": {
                    "name": "Average monthly income",
                    "values": {"this": median_income},
                },
            }
        )
    else:
        # distribution
        recode = COLLAPSED_ANNUAL_INCOME_CATEGORIES
        fields = ["employed individual annual income"]
        income_dist_data, total_workers = get_stat_data(
            fields,
            geo,
            session,
            exclude=["Not applicable"],
            recode=recode,
            key_order=recode.values(),
        )

        # median income
        median = calculate_median_stat(income_dist_data)
        median_income = ESTIMATED_ANNUAL_INCOME_CATEGORIES[median]
        profile.update(
            {
                "individual_annual_income_distribution": income_dist_data,
                "median_annual_individual_income": {
                    "name": "Average annual income",
                    "values": {"this": median_income},
                },
            }
        )

    # employment status
    employ_status, total_workers = get_stat_data(
        ["official employment status"],
        geo,
        session,
        exclude=["Age less than 15 years", "Not applicable"],
        order_by="official employment status",
        table_name="officialemploymentstatus",
    )

    # sector
    sector_dist_data, _ = get_stat_data(
        ["type of sector"],
        geo,
        session,
        exclude=["Not applicable"],
        order_by="type of sector",
    )

    profile.update(
        {
            "employment_status": employ_status,
            "sector_type_distribution": sector_dist_data,
        }
    )

    # access to internet
    if current_context().get("year") == "latest":
        internet_access_dist, total_households = get_stat_data(
            ["access to internet"],
            geo,
            session,
            recode=INTERNET_ACCESS_RECODE,
            table_name="accesstointernet_2016",
        )

        profile.update({"internet_access_distribution": internet_access_dist})

    else:
        internet_access_dist, total_with_access = get_stat_data(
            ["access to internet"],
            geo,
            session,
            exclude=["No access to internet"],
            order_by="access to internet",
        )
        _, total_without_access = get_stat_data(
            ["access to internet"], geo, session, only=["No access to internet"]
        )
        total_households = total_with_access + total_without_access

        profile.update(
            {
                "internet_access_distribution": internet_access_dist,
                "internet_access": {
                    "name": "Households with internet access",
                    "values": {"this": percent(total_with_access, total_households)},
                    "numerators": {"this": total_with_access},
                },
            }
        )

    return profile
Esempio n. 12
0
def get_households_profile(geo, session):
    # head of household
    # gender
    head_gender_dist, total_households = get_stat_data(
        ["gender of household head"],
        geo,
        session,
        table_universe="Households",
        order_by="gender of household head",
    )
    female_heads = head_gender_dist["Female"]["numerators"]["this"]

    # age
    u18_table = get_datatable("genderofheadofhouseholdunder18")
    objects = u18_table.get_rows_for_geo(geo, session)

    total_under_18 = float(sum(o[0] for o in objects))

    # tenure
    tenure_data, _ = get_stat_data(
        ["tenure status"],
        geo,
        session,
        table_universe="Households",
        recode=HOUSEHOLD_OWNERSHIP_RECODE,
        order_by="-total",
    )
    owned = 0
    for key, data in tenure_data.iteritems():
        if key.startswith("Owned"):
            owned += data["numerators"]["this"]

    # annual household income
    if geo.version == "2011":
        HOUSEHOLD_INCOME_RECODE = HOUSEHOLD_INCOME_RECODE_2011
    else:
        HOUSEHOLD_INCOME_RECODE = COLLAPSED_ANNUAL_INCOME_CATEGORIES
    income_dist_data, _ = get_stat_data(
        ["annual household income"],
        geo,
        session,
        table_universe="Households",
        exclude=["Unspecified", "Not applicable"],
        recode=HOUSEHOLD_INCOME_RECODE,
        key_order=HOUSEHOLD_INCOME_RECODE.values(),
    )

    # median income
    median = calculate_median_stat(income_dist_data)
    median_income = HOUSEHOLD_INCOME_ESTIMATE[median]

    # type of dwelling
    type_of_dwelling_dist, _ = get_stat_data(
        ["type of dwelling"],
        geo,
        session,
        table_universe="Households",
        recode=TYPE_OF_DWELLING_RECODE,
        order_by="-total",
    )
    informal = type_of_dwelling_dist["Shack"]["numerators"]["this"]

    # household goods
    household_goods, _ = get_stat_data(
        ["household goods"],
        geo,
        session,
        table_universe="Households",
        recode=HOUSEHOLD_GOODS_RECODE,
        key_order=sorted(HOUSEHOLD_GOODS_RECODE.values()),
    )

    return {
        "total_households": {
            "name": "Households",
            "values": {"this": total_households},
        },
        "owned": {
            "name": "Households fully owned or being paid off",
            "values": {"this": percent(owned, total_households)},
            "numerators": {"this": owned},
        },
        "type_of_dwelling_distribution": type_of_dwelling_dist,
        "informal": {
            "name": "Households that are informal dwellings (shacks)",
            "values": {"this": percent(informal, total_households)},
            "numerators": {"this": informal},
        },
        "tenure_distribution": tenure_data,
        "household_goods": household_goods,
        "annual_income_distribution": income_dist_data,
        "median_annual_income": {
            "name": "Average annual household income",
            "values": {"this": median_income},
        },
        "head_of_household": {
            "gender_distribution": head_gender_dist,
            "female": {
                "name": "Households with women as their head",
                "values": {"this": percent(female_heads, total_households)},
                "numerators": {"this": female_heads},
            },
            "under_18": {
                "name": "Households with heads under 18 years old",
                "values": {"this": total_under_18},
            },
        },
    }