def get_demographics_profile(geo_code, geo_level, session): try: sex_dist_data, total_pop = get_stat_data('sex', geo_level, geo_code, session, table_fields=['sex']) urban_dist_data, _ = get_stat_data('rural or urban', geo_level, geo_code, session, table_fields=['rural or urban']) except LocationNotFound: sex_dist_data, total_pop = LOCATIONNOTFOUND, 0 urban_dist_data = LOCATIONNOTFOUND total_urbanised = 0 for data, value in urban_dist_data.get('Urban', {}).iteritems(): if data == 'numerators': total_urbanised += value['this'] final_data = { 'sex_ratio': sex_dist_data, 'urban_distribution': urban_dist_data, 'urbanised': { 'name': 'In urban areas', 'numerators': {'this': total_urbanised}, }, 'total_population': { "name": "People", "values": {"this": total_pop} }} try: final_data['urbanised']['values'] = { 'this': round(total_urbanised / total_pop * 100, 2)} except ZeroDivisionError: final_data['urbanised']['values'] = {'this': 0} return final_data
def get_demographics_profile(geo, session): year = current_context().get('year') with dataset_context(year=year): # gender gender_dist_data, total_pop = get_stat_data( 'gender', geo, session, table_fields=['gender', 'age group']) # age group age_group_dist_data, _ = get_stat_data( 'age group', geo, session, table_fields=['gender', 'age group']) total_under_15 = age_group_dist_data['0-14 Years']['numerators']['this'] # rural or urban rural_dist_data, _ = get_stat_data( ['rural or urban','gender'], geo, session, table_fields=['gender', 'rural or urban']) final_data = { 'gender_ratio': gender_dist_data, 'age_group_distribution': age_group_dist_data, 'under_15': { 'name': 'Under 15 years', 'values': {'this': total_under_15} }, 'rural_distribution': rural_dist_data, 'total_population': { "name": "People", "values": {"this": total_pop} }} return final_data
def get_disabilities_profile(geocode, geo_level, session): try: disabled_or_not, total_ = get_stat_data('disabled or not', geo_level, geocode, session, table_fields=['disabled or not']) disability, _ = get_stat_data('disability', geo_level, geocode, session, table_fields=['disability']) except LocationNotFound: disabled_or_not, total_ = LOCATIONNOTFOUND, 0 disability = LOCATIONNOTFOUND total_disabled = 0 for data, value in disabled_or_not.get('With disability', {}).iteritems(): if data == 'numerators': total_disabled += value['this'] final_data = { 'disabled_or_not_distribution': disabled_or_not, 'disability': disability, 'total_disabled': { 'name': 'Disabled', 'numerators': {'this': total_disabled}, }, 'total_': { "name": "Population", "values": {"this": total_} } } try: final_data['total_disabled']['values'] = { 'this': round(total_disabled / total_ * 100, 2)} except ZeroDivisionError: final_data['total_disabled']['values'] = {'this': 0} return final_data
def get_teachers_profile(geo,session,request): table = 'teachers_type_2005' if request.GET.get('release') is not None: tablereq = 'teachers_type_' + request.GET.get('release') if tablereq in ('teachers_type_2005','teachers_type_2006','teachers_type_2007','teachers_type_2008', 'teachers_type_2009','teachers_type_2010','teachers_type_2011','teachers_type_2012','teachers_type_2013', 'teachers_type_2015','teachers_type_default'): table = tablereq else: table = 'teachers_type_default' teachers_by_type,t_lit = get_stat_data( ['type'],geo,session, table_fields=['teachers','type','year'], table_name = table ) teachers_by_school_type,_ = get_stat_data( ['teachers','type'],geo,session, table_fields=['teachers','type','year'], percent_grouping=['type'], table_name = table, ) final_data = { 'teachers_by_type_distribution': teachers_by_type, 'teachers_by_schools_type_distribution':teachers_by_school_type, 'total_teachers': { "name": "Total teachers", "values": {"this":t_lit} } } return final_data
def get_contraceptive_use_profile(geo_code, geo_level, session): # contraceptive_use stats contraceptive_use_dist_data, _ = get_stat_data( 'contraceptive_use', geo_level, geo_code, session, key_order=['Modern', 'Traditional', 'Not using']) modern = contraceptive_use_dist_data['Modern']['numerators']['this'] traditional = contraceptive_use_dist_data['Traditional']['numerators']['this'] cpr = modern + traditional contraceptive_modern_method_dist_data, _ = get_stat_data( 'contraceptive_modern_method', geo_level, geo_code, session) contraceptive_traditional_method_dist_data, _ = get_stat_data( 'contraceptive_traditional_method', geo_level, geo_code, session) return { 'contraceptive_use_distribution': contraceptive_use_dist_data, 'modern_methods_distribution': contraceptive_modern_method_dist_data, 'traditional_methods_distribution': contraceptive_traditional_method_dist_data, 'cpr': { 'name': 'Contraceptive prevalence rate', 'numerators': {'this': cpr}, 'values': {'this': cpr}, }, }
def get_crime_report_profile(geo, session): stats_dist, s_ = get_stat_data("crimereport", geo, session) crimes_dist, c_ = get_stat_data("typesofcrime", geo, session) crimes = stats_dist['Crimes']['numerators']['this'] crimeindex = stats_dist['Crimesindex']['numerators']['this'] return { 'crimes': { 'name': 'Number of crimes', 'numerators': { 'this': crimes }, 'values': { 'this': crimes }, }, 'crimesindex': { 'name': 'Crime index per 100,000 people', 'numerators': { 'this': crimeindex }, 'values': { 'this': crimeindex }, }, 'crimes_dist': crimes_dist, 'metadata': crimes_dist['metadata'] }
def get_demographics_profile(geo_code, geo_level, session): pop_dist_data, pop_total = get_stat_data( ['population group'], geo_level, geo_code, session) youth_pop_dist_data, youth_pop_total = get_stat_data(['age in completed years'], geo_level, geo_code, session, table_name='youth_gender_age_in_completed_years') youth_gender_data, _ = get_stat_data(['gender'], geo_level, geo_code, session, table_name='youth_gender_population_group') youth_pop_group_data, _ = get_stat_data(['population group'], geo_level, geo_code, session, table_name='youth_gender_population_group') final_data = { 'total_population': { "name": "People", "values": {"this": pop_total} }, 'youth_population_total': { "name": "Youth aged 15-24", "values": {"this": youth_pop_total} }, 'youth_population_perc': { "name": "Of population are youth aged 15-24", "values": {"this": percent(youth_pop_total, pop_total)}, }, 'youth_population_by_year': youth_pop_dist_data, 'youth_population_by_gender': youth_gender_data, 'youth_population_by_pop_group': youth_pop_group_data } geo = geo_data.get_geography(geo_code, geo_level) if geo.square_kms: final_data['population_density'] = { 'name': "people per square kilometre", 'values': {"this": pop_total / geo.square_kms} } return final_data
def get_itn_profile(geo, session): # household possession and use of ITN possession_dist, _ = get_stat_data( ['household possession of itn'], geo, session) households_with_at_least_one_itn = \ possession_dist['Households with at least one ITN']['numerators']['this'] households_with_at_least_one_itn = get_dictionary( 'Possess at least one ITN', 'No ITN', households_with_at_least_one_itn, possession_dist) percentage_households_with_ITN_for_every_two_people = \ possession_dist['Percentage households with ITN for every 2 people in household']['numerators']['this'] percentage_households_with_ITN_for_every_two_people = get_dictionary('1:2', 'less than 1:2', percentage_households_with_ITN_for_every_two_people, possession_dist) average_itn_per_household = possession_dist['Average ITN per household']['numerators']['this'] use_dist, _ = get_stat_data( ['household', 'users', 'itn_use'], geo, session) households_with_at_least_one_itn_use_dist = use_dist['With at least one ITN'] all_households_use_dist = use_dist['All'] return { 'households_with_at_least_one_itn': households_with_at_least_one_itn, 'percentage_households_with_ITN_for_every_two_people': percentage_households_with_ITN_for_every_two_people, 'average_itn_per_household': { 'name': 'Average number of insecticide-treated nets (ITNs) per household', 'numerators': {'this': average_itn_per_household}, 'values': {'this': average_itn_per_household} }, 'households_with_at_least_one_itn_use_dist': households_with_at_least_one_itn_use_dist, 'all_households_use_dist': all_households_use_dist }
def get_contraceptive_use_profile(geo, session): # contraceptive_use stats contraceptive_use_dist_data, _ = get_stat_data( 'contraceptive_use', geo, session, key_order=['Modern', 'Traditional', 'Not using']) modern = contraceptive_use_dist_data['Modern']['numerators']['this'] traditional = contraceptive_use_dist_data['Traditional']['numerators']['this'] cpr = modern + traditional contraceptive_modern_method_dist_data, _ = get_stat_data( 'contraceptive_modern_method', geo, session) contraceptive_traditional_method_dist_data, _ = get_stat_data( 'contraceptive_traditional_method', geo, session) return { 'contraceptive_use_distribution': contraceptive_use_dist_data, 'modern_methods_distribution': contraceptive_modern_method_dist_data, 'traditional_methods_distribution': contraceptive_traditional_method_dist_data, 'cpr': { 'name': 'Contraceptive prevalence rate', 'numerators': {'this': cpr}, 'values': {'this': cpr}, } }
def get_households_profile(geo, session, year): with dataset_context(year=year): try: permanency, _ = get_stat_data('household percentage by permanency', geo, session, table_fields=[ 'household percentage by permanency']) except Exception: permanency = LOCATIONNOTFOUND try: light_source, total_households = get_stat_data( 'household distribution by light source', geo, session, table_fields=['household distribution by light source']) energy_source, _ = get_stat_data( 'household distribution by energy source', geo, session, table_fields=['household distribution by energy source']) except Exception: total_households = 0 light_source = LOCATIONNOTFOUND energy_source, _ = LOCATIONNOTFOUND, 0 is_missing = permanency.get('is_missing') and light_source.get('is_missing') and energy_source.get('is_missing') final_data = { 'is_missing': is_missing, 'percentage_by_permanency': permanency, 'light_source_distribution': light_source, 'energy_source_distribution': energy_source, 'total_households': { "name": "Households", "values": {"this": total_households} } } return final_data
def get_health_centers_distribution_profile(geo, session): if geo.geo_level == 'ward': return {} hc_data, total = get_stat_data('health centers', geo=geo, session=session, order_by='-total') ho_data, _ = get_stat_data('health center ownership', geo, session) hiv_centers_data, hiv_c = get_stat_data('hiv centers', geo, session) return { 'total': { 'name': 'Total health centers in operation (2014)', 'numerators': { 'this': total }, 'values': { 'this': total } }, 'hiv_centers': { 'name': 'HIV care and treatment centers (2014)', 'numerators': { 'this': hiv_c }, 'values': { 'this': hiv_c } }, 'health_center_data': hc_data, 'health_center_ownership': ho_data, 'source_link': 'http://www.opendata.go.tz/dataset/list-of-health-facilities-with-geographical-location', 'source_name': 'opendata.go.tz', }
def get_ITN_profile(geo_code, geo_level, session): # household possession and use of ITN possession_dist, _ = get_stat_data(['household possession of itn'], geo_level, geo_code, session) households_with_at_least_one_itn = \ possession_dist['Households with at least one ITN']['numerators']['this'] households_with_at_least_one_itn = get_dictionary('Possess at least one ITN', 'No ITN', households_with_at_least_one_itn, possession_dist) percentage_households_with_ITN_for_every_two_people = \ possession_dist['Percentage households with ITN for every 2 people in household']['numerators']['this'] percentage_households_with_ITN_for_every_two_people = get_dictionary('1:2',\ 'less than 1:2',\ percentage_households_with_ITN_for_every_two_people, possession_dist) average_itn_per_household = possession_dist['Average ITN per household']['numerators']['this'] use_dist, _ = get_stat_data(['household', 'users', 'itn_use'], geo_level, geo_code, session) households_with_at_least_one_itn_use_dist = use_dist['With at least one ITN'] all_households_use_dist = use_dist['All'] return { 'households_with_at_least_one_itn': households_with_at_least_one_itn, 'percentage_households_with_ITN_for_every_two_people': percentage_households_with_ITN_for_every_two_people, 'average_itn_per_household': { 'name': 'Average number of insecticide-treated nets (ITNs) per household', 'numerators': {'this': average_itn_per_household}, 'values': {'this': average_itn_per_household}, 'metadata': possession_dist['metadata'] }, 'households_with_at_least_one_itn_use_dist': households_with_at_least_one_itn_use_dist, 'all_households_use_dist': all_households_use_dist }
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}, }, }, }
def get_access_profile(geo, session): view_ft_users = OrderedDict() view_ft_users_population = OrderedDict() REGION_RECODES = OrderedDict() REGION_POPULATION = OrderedDict() try: if geo.geo_level == 'country': REGION_RECODES = OrderedDict([('country', geo.name), ('continent', geo.parent.name)]) view_ft_users, _ = get_stat_data( ['type'], geo, session, table_name='ft_users_country_continent', recode=dict(REGION_RECODES), key_order=REGION_RECODES.values()) elif geo.geo_level == 'continent': REGION_RECODES = OrderedDict([('continent', geo.name), ('world', geo.parent.name)]) view_ft_users, _ = get_stat_data( ['type'], geo, session, table_name='ft_users_continent_world', recode=dict(REGION_RECODES), key_order=REGION_RECODES.values()) elif geo.geo_level == 'world': view_ft_users, _ = get_stat_data( ['type'], geo, session, table_name='ft_users_world_continent') except Exception as e: view_ft_users = None try: REGION_POPULATION = OrderedDict([('users', 'Users'), ('population', 'Non-users')]) view_ft_users_population, _ = get_stat_data( ['type'], geo, session, table_name='ft_users_population', recode=dict(REGION_POPULATION), key_order=REGION_POPULATION.values()) except Exception as e: view_ft_users_population = {'Non-users': {'numerators': {'this': 0}}} return { 'view_ft_users_population_users': { "name": "World bank data.", "values": { "this": view_ft_users_population['Non-users']['numerators']['this'] } }, 'view_ft_users': view_ft_users, 'view_ft_users_population': view_ft_users_population, }
def get_maternal_care_indicators_profile(geo, session): # maternal care indicators stats maternal_care_indicators_data, _ = get_stat_data( 'maternal care indicators', geo, session) delivery_in_health_facility = \ maternal_care_indicators_data['Percentage delivered in a health facility']['numerators']['this'] antenatal_care_by_skilled_provider = \ maternal_care_indicators_data['Percentage with antenatal care from a skilled provider']['numerators']['this'] antenatal_dist = get_dictionary('From a skilled provider', 'From a non-skilled provider', antenatal_care_by_skilled_provider, maternal_care_indicators_data) anc_visits = maternal_care_indicators_data[ 'Percentage with 4+ ANC visits']['numerators']['this'] anc_visits_dist = get_dictionary('4+', 'Less than 4', anc_visits, maternal_care_indicators_data) delivery_by_skilled_provider = \ maternal_care_indicators_data['Percentage delivered by a skilled provider']['numerators']['this'] delivery_by_skilled_provider_dist = get_dictionary( 'Skilled provider', 'Non-skilled provider', delivery_by_skilled_provider, maternal_care_indicators_data) # Use of IPT use_of_ipt_dist, _ = get_stat_data('use of ipt', geo, session) one_or_more_dist = use_of_ipt_dist['1 or more']['numerators']['this'] one_or_more_dist = get_dictionary('1 or more', 'Less', one_or_more_dist, maternal_care_indicators_data) two_or_more_dist = use_of_ipt_dist['2 or more']['numerators']['this'] two_or_more_dist = get_dictionary('2 or more', 'Less', two_or_more_dist, maternal_care_indicators_data) three_or_more_dist = use_of_ipt_dist['3 or more']['numerators']['this'] three_or_more_dist = get_dictionary('3 or more', 'Less', three_or_more_dist, maternal_care_indicators_data) return { 'maternal_care_indicators': maternal_care_indicators_data, 'antenatal_dist': antenatal_dist, 'anc_visits_dist': anc_visits_dist, 'delivery_by_skilled_provider_dist': delivery_by_skilled_provider_dist, 'delivery_in_health_facility': { 'name': 'babies delivered in a health facility', 'numerators': { 'this': delivery_in_health_facility }, 'values': { 'this': round(delivery_in_health_facility, 2) } }, 'one_or_more_dist': one_or_more_dist, 'two_or_more_dist': two_or_more_dist, 'three_or_more_dist': three_or_more_dist, }
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)) # 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'] _, total_ecd_children = get_stat_data( ['age in completed years'], geo, session, table_name='ageincompletedyears', only=['0', '1', '2', '3', '4', '5']) ecd_children_per_household = ratio(total_ecd_children, total_households) return { 'total_households': { 'name': 'Households', 'values': {'this': total_households}, }, '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}, }, '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}, } }, 'ecd_children_per_household': { 'name': 'Average number of children (aged 0-5) in each household', 'values': {'this': ecd_children_per_household}, }, }
def get_education_profile(geo_code, geo_level, session): # highest level reached edu_dist_data, total_pop = get_stat_data('highest education level reached', geo_level, geo_code, session, key_order=[ 'None', 'Pre-primary', 'Primary', 'Secondary', 'Tertiary', 'University', 'Youth polytechnic', 'Basic literacy', 'Madrassa' ]) secondary_or_higher = 0 for key, data in edu_dist_data.iteritems(): if key in ['Secondary', 'Tertiary', 'University', 'Youth polytechnic']: secondary_or_higher += data['numerators']['this'] # school attendance by sex school_attendance_dist, _ = get_stat_data( ['school attendance', 'sex'], geo_level, geo_code, session, key_order={ 'school attendance': ['Never attended', 'At school', 'Left school', 'Unspecified'], 'sex': ['Female', 'Male'] }) total_never = 0.0 for data in school_attendance_dist['Never attended'].itervalues(): if 'numerators' in data: total_never += data['numerators']['this'] return { 'education_reached_distribution': edu_dist_data, 'education_reached_secondary_or_higher': { 'name': 'Reached Secondary school or higher', 'numerators': { 'this': secondary_or_higher }, 'values': { 'this': round(secondary_or_higher / total_pop * 100, 2) } }, 'school_attendance_distribution': school_attendance_dist, 'school_attendance_never': { 'name': 'Never attended school', 'numerators': { 'this': total_never }, 'values': { 'this': round(total_never / total_pop * 100, 2) } }, }
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}, }, }, }
def get_education_profile(geo, session): edu_dist_data, total_over_20 = get_stat_data( ["highest educational level"], geo, session, recode=COLLAPSED_EDUCATION_CATEGORIES, table_universe="Individuals 20 and older", key_order=EDUCATION_KEY_ORDER, ) GENERAL_EDU = ( EDUCATION_GET_OR_HIGHER if str(current_context().get("year")) == "2011" else EDUCATION_GET_OR_HIGHER_2016 ) general_edu, total_general_edu = get_stat_data( ["highest educational level"], geo, session, table_universe="Individuals 20 and older", only=GENERAL_EDU, ) FURTHER_EDU = ( EDUCATION_FET_OR_HIGHER if str(current_context().get("year")) == "2011" else EDUCATION_FET_OR_HIGHER_2016 ) further_edu, total_further_edu = get_stat_data( ["highest educational level"], geo, session, table_universe="Individuals 20 and older", only=FURTHER_EDU, ) edu_split_data = { "percent_general_edu": { "name": "Completed Grade 9 or higher", "numerators": {"this": total_general_edu}, "values": {"this": round(total_general_edu / total_over_20 * 100, 2)}, }, "percent_further_edu": { "name": "Completed Matric or higher", "numerators": {"this": total_further_edu}, "values": {"this": round(total_further_edu / total_over_20 * 100, 2)}, }, "metadata": general_edu["metadata"], } profile = { "educational_attainment_distribution": edu_dist_data, "educational_attainment": edu_split_data, } return profile
def get_water_sources_profile(geo, session): if geo.geo_level == 'ward' or geo.geo_level == 'district': return {} try: _, total_pop = get_stat_data( 'sex', geo=geo, session=session, table_fields=['age in completed years', 'sex', 'rural or urban']) WATER_POINT_STATUS_RECODES = OrderedDict([ ('functional', 'Functional'), ('nonfunctional', 'Non Functional'), ('needsrepair', 'Functional Needs Repair') ]) water_source_dist, n_sources = get_stat_data('water sources', geo=geo,\ session=session, recode=WATER_POINT_STATUS_RECODES) except Exception as e: #Location not found in dataset return {} #Functional + Needs repair all_functional = water_source_dist['Functional']['numerators']['this'] + \ water_source_dist['Functional Needs Repair']['numerators']['this'] #Number of people per water point n_people_per_point = round(total_pop / all_functional) return { 'water_sources_dist': water_source_dist, 'number_of_water_sources': { 'name': 'Total number of water points', 'numerators': { 'this': n_sources }, 'values': { 'this': n_sources } }, 'source_link': 'http://www.opendata.go.tz/dataset/hali-halisi-ya-vitoa-maji-kwa-mikoa-yote-tanzania', 'source_name': 'opendata.go.tz', 'n_people_per_point': { 'name': 'Number of people using a working water point(Functional/Needs repair water point)', 'numerators': { 'this': n_people_per_point }, 'values': { 'this': n_people_per_point } }, }
def get_type_treatment_profile(geo, session): # Treatment for acute respiratory infection symptoms, fever, and diarrhoea # stats dist, _ = get_stat_data(['type', 'treatment'], geo, session, key_order={ 'type': ['ARI', 'Fever', 'Diarrhoea'], 'treatment': ['Sought treatment from health facility or provider', 'ORT', 'Zinc', 'ORS and zinc', 'Fluid from ORS packet' ] }) # need to use numerators instead of values for key in dist: if key == 'metadata': continue for other_key in dist[key]: if other_key == 'metadata': continue try: dist[key][other_key]['values']['this'] = dist[key][other_key]['numerators']['this'] except BaseException: dist[key][other_key] = {'values': { 'this': 0}, 'numerators': {'this': 0}} ari = dist['ARI']['Sought treatment from health facility or provider']['numerators']['this'] fever = dist['Fever']['Sought treatment from health facility or provider']['numerators']['this'] dist.pop('ARI') dist.pop('Fever') ari_dist = get_dictionary('Sought', 'Did not seek', ari, dist) fever_dist = get_dictionary('Sought', 'Did not seek', fever, dist) treatment_of_chidren_with_fever_dist, _ = get_stat_data( ['treatment of children with fever'], geo, session) children_with_fever = treatment_of_chidren_with_fever_dist['Had fever']['numerators']['this'] treatment_of_chidren_with_fever_dist.pop('Had fever') # need to use numerators instead of values for v in treatment_of_chidren_with_fever_dist: if v == 'metadata': continue treatment_of_chidren_with_fever_dist[v]['values'][ 'this'] = treatment_of_chidren_with_fever_dist[v]['numerators']['this'] return { 'treatment_distribution': dist, 'ari_dist': ari_dist, 'fever_dist': fever_dist, 'treatment_of_chidren_with_fever_dist': treatment_of_chidren_with_fever_dist, 'children_with_fever': { 'name': 'Percentage of children with fever in the two weeks preceding the survey', 'numerators': {'this': children_with_fever}, 'values': {'this': children_with_fever} }, }
def get_land_audit_profile(geo, session): year = current_context().get('year') with dataset_context(year=year): land_use_dist = LOCATIONNOTFOUND land_user_dist = LOCATIONNOTFOUND land_distribution_gender = LOCATIONNOTFOUND land_ownership = LOCATIONNOTFOUND try: land_use_dist, _ = get_stat_data('land_use', geo, session, table_name='landuse', table_fields=['land_use']) except Exception as e: pass try: land_user_dist, _ = get_stat_data('land_user', geo, session, table_name='landuser', table_fields=['land_user']) except Exception: pass try: land_distribution_gender, _ = get_stat_data( 'land_ownership_by_gender', geo, session, table_name='privatelanddistributionbygender', table_fields=['land_ownership_by_gender']) except Exception: pass try: land_ownership, _ = get_stat_data('private_vs_state_ownership', geo, session, table_name='landownership', table_fields=[ 'private_vs_state_ownership']) except Exception: pass is_missing = land_user_dist.get('is_missing') and \ land_use_dist.get('is_missing') and \ land_distribution_gender.get('is_missing') and \ land_ownership.get('is_missing') return { 'is_missing': is_missing, 'land_user_dist': land_user_dist, 'land_use_dist': land_use_dist, 'land_distribution_gender': land_distribution_gender, 'land_ownership': land_ownership, }
def get_demographics_profile(geo, session): geo.version = str(geo.version) # sex try: sex_dist_data, total_pop = get_stat_data('sex', geo, session, table_fields=['sex']) except LocationNotFound: sex_dist_data = LOCATIONNOTFOUND total_pop = 0 # urban/rural by sex total_urbanised = 0 try: urban_dist_data, _ = get_stat_data(['rural or urban'], geo, session, table_fields=['rural or urban']) for data in urban_dist_data['Urban'].itervalues(): if 'numerators' in data: total_urbanised += data['numerators']['this'] except LocationNotFound: urban_dist_data = LOCATIONNOTFOUND final_data = { 'sex_ratio': sex_dist_data, 'urban_distribution': urban_dist_data, 'urbanised': { 'name': 'In urban areas', 'numerators': { 'this': total_urbanised }, }, 'total_population': { "name": "People", "values": { "this": total_pop } } } try: final_data['urbanised']['values'] = { 'this': round(total_urbanised / total_pop * 100, 2) } except ZeroDivisionError: final_data['urbanised']['values'] = {'this': 0} return final_data
def get_households_profile(geocode, geo_level, session): try: light_source, total_households = get_stat_data('household distribution by light source', geo_level, geocode, session, table_fields=['household distribution by light source']) energy_source, _ = get_stat_data('household distribution by energy source', geo_level, geocode, session, table_fields=['household distribution by energy source']) except LocationNotFound: household_dist, total_households = LOCATIONNOTFOUND, 0 light_source = LOCATIONNOTFOUND energy_source, _ = LOCATIONNOTFOUND, 0 total_electrified_lighting = 0 for data, value in light_source.get('Electricity', {}).iteritems(): if data == 'numerators': total_electrified_lighting += value['this'] total_charcoal_energy = 0 for data, value in energy_source.get('Charcoal', {}).iteritems(): if data == 'numerators': total_charcoal_energy += value['this'] final_data = { 'light_source_distribution': light_source, 'electrified_lighting': { 'name': 'Lighting with Electricity', 'numerators': {'this': total_electrified_lighting}, }, 'energy_source_distribution': energy_source, 'charcoal_energy': { 'name': 'Cooking with Charcoal', 'numerators': {'this': total_charcoal_energy}, }, 'total_households': { "name": "Households", "values": {"this": total_households} } } try: final_data['electrified_lighting']['values'] = { 'this': round(total_electrified_lighting / total_households * 100, 2)} except ZeroDivisionError: final_data['electrified_lighting']['values'] = {'this': 0} try: final_data['charcoal_energy']['values'] = { 'this': round(total_electrified_lighting / _ * 100, 2)} except ZeroDivisionError: final_data['charcoal_energy']['values'] = {'this': 0} return final_data
def get_type_treatment_profile(geo_code, geo_level, session): # Treatment for acute respiratory infection symptoms, fever, and diarrhoea stats dist, _ = get_stat_data(['type', 'treatment'], geo_level, geo_code, session, key_order={ 'type': ['ARI', 'Fever','Diarrhoea'], 'treatment': ['Sought treatment from health facility or provider', \ 'ORT', 'Zinc', 'ORS and zinc','Fluid from ORS packet' ] }) # need to use numerators instead of values for key in dist: if key == 'metadata': continue for other_key in dist[key]: if other_key == 'metadata': continue try: dist[key][other_key]['values']['this'] = dist[key][other_key]['numerators']['this'] except: dist[key][other_key] = {'values': {'this': 0}, 'numerators': {'this': 0}} ari = dist['ARI']['Sought treatment from health facility or provider']['numerators']['this'] fever = dist['Fever']['Sought treatment from health facility or provider']['numerators']['this'] dist.pop('ARI') dist.pop('Fever') ari_dist = get_dictionary('Sought', 'Did not seek', ari, dist) fever_dist = get_dictionary('Sought', 'Did not seek', fever, dist) treatment_of_chidren_with_fever_dist, _ = get_stat_data(['treatment of children with fever'], geo_level, geo_code, session) children_with_fever = treatment_of_chidren_with_fever_dist['Had fever']['numerators']['this'] treatment_of_chidren_with_fever_dist.pop('Had fever') # need to use numerators instead of values for v in treatment_of_chidren_with_fever_dist: if v == 'metadata': continue treatment_of_chidren_with_fever_dist[v]['values']['this'] = treatment_of_chidren_with_fever_dist[v]['numerators']['this'] return { 'treatment_distribution': dist, 'ari_dist': ari_dist, 'fever_dist': fever_dist, 'treatment_of_chidren_with_fever_dist': treatment_of_chidren_with_fever_dist, 'children_with_fever': { 'name': 'Percentage of children with fever in the two weeks preceding the survey', 'numerators': {'this': children_with_fever}, 'values': {'this': children_with_fever}, 'metadata': dist['metadata'] }, }
def get_demographics_profile(geo, session, year): with dataset_context(year=year): geo.version = str(geo.version) # sex try: sex_dist_data, total_pop = get_stat_data( 'sex', geo, session, table_fields=['sex']) except Exception: sex_dist_data = LOCATIONNOTFOUND total_pop = 0 # urban/rural by sex total_urbanised = 0 try: urban_dist_data, _ = get_stat_data( ['rural or urban'], geo, session, table_fields=['rural or urban']) for data in urban_dist_data['Urban'].values(): if 'numerators' in data: total_urbanised += data['numerators']['this'] except Exception: urban_dist_data = LOCATIONNOTFOUND is_missing = sex_dist_data.get('is_missing') and urban_dist_data.get('is_missing') final_data = { 'is_missing': is_missing, 'sex_ratio': sex_dist_data, 'urban_distribution': urban_dist_data, 'urbanised': { 'name': 'In urban areas', 'numerators': {'this': total_urbanised}, }, 'total_population': { "name": "People", "values": {"this": total_pop} }} try: final_data['urbanised']['values'] = { 'this': round(total_urbanised / total_pop * 100, 2)} except ZeroDivisionError: final_data['urbanised']['values'] = {'this': 0} return final_data
def get_population(geo, session): with dataset_context(year='2017'): sex_dist, total_population_sex = LOCATIONNOTFOUND, 0 residence_dist, total_population_residence = LOCATIONNOTFOUND, 0 religion_dist, total_population_religion = LOCATIONNOTFOUND, 0 try: sex_dist, total_population_sex = get_stat_data( 'sex', geo, session, table_fields=['sex']) except Exception: pass try: residence_dist, total_population_residence = get_stat_data( 'residence', geo, session, table_fields=['residence']) except Exception: pass try: religion_dist, total_population_religion = get_stat_data( 'religion', geo, session, table_fields=['religion']) except Exception: pass total_population = 0 is_missing = sex_dist.get('is_missing') and \ residence_dist.get('is_missing') and \ religion_dist.get('is_missing') if not is_missing: total_population = total_population_sex if total_population_sex > 0 else total_population_residence total_population_dist = _create_single_value_dist( 'People', total_population) demographics_data = { 'is_missing': is_missing, 'sex_dist': sex_dist, 'residence_dist': residence_dist, 'religion_dist': religion_dist, 'total_population': total_population_dist, } if geo.square_kms: demographics_data['population_density'] = { 'name': "people per square kilometre", 'values': {"this": total_population / geo.square_kms}, } return demographics_data
def get_hospitals_profile(geo, session): # population group _, total_pop = get_stat_data(['population group'], geo, session, table_dataset='Census 2011') # Hospitals table = get_datatable('hospitals_2012') keys = [ 'regional_hospital', 'central_hospital', 'district_hospital', 'clinic', 'chc' ] hospital_breakdown, total_hospitals = table.get_stat_data( geo, keys, percent=False, recode={'chc': 'Community health centre'}) people_per_hospital = ratio(total_pop, total_hospitals) _, ecd_children = get_stat_data(['age in completed years'], geo, session, table_name='ageincompletedyears', only=['0', '1', '2', '3', '4', '5']) children_0_to_5_per_hospital = ratio(ecd_children, total_hospitals) return { "total_hospitals": { "name": "Hospitals / Clinics", "values": { "this": total_hospitals } }, "hospital_breakdown": hospital_breakdown, "people_per_hospital": { "name": "People living in the area for each hospital / clinic", "values": { "this": people_per_hospital } }, "children_0_to_5_per_hospital": { "name": "Children (aged 0-5 years) living in the area for each hospital / clinic", "values": { "this": children_0_to_5_per_hospital } }, }
def get_fertility_profile(geo, session): # fertility dist, _ = get_stat_data(['fertility'], geo, session) percentage_women_age_15_49_currently_pregnant = dist['Pregnant']['numerators']['this'] percentage_women_age_15_49_currently_pregnant = get_dictionary('Pregnant', 'Not pregnant', percentage_women_age_15_49_currently_pregnant, dist) fertility_rate = dist['Rate']['numerators']['this'] mean_number_of_children_ever_born_to_women_aged_40_49 = dist['Mean']['numerators']['this'] return { 'percentage_women_age_15_49_currently_pregnant': percentage_women_age_15_49_currently_pregnant, 'fertility_rate': { 'name': 'Fertility', 'numerators': {'this': fertility_rate}, 'values': {'this': fertility_rate} }, 'mean_number_of_children_ever_born_to_women_aged_40_49': { 'name': 'Mean number of children ever born to women aged 40-49', 'numerators': {'this': mean_number_of_children_ever_born_to_women_aged_40_49}, 'values': {'this': mean_number_of_children_ever_born_to_women_aged_40_49} }, }
def get_health_ratios_profile(geo, session): ratios_dist, _ = get_stat_data("healthratios", geo, session) dr = ratios_dist['Doctor ratio']['numerators']['this'] nr = ratios_dist['Nurse ratio']['numerators']['this'] return { 'doctorratio': { 'name': 'Doctor to population ratio', 'numerators': { 'this': dr }, 'values': { 'this': dr }, }, 'nurseratio': { 'name': 'Nurse to population ratio', 'numerators': { 'this': nr }, 'values': { 'this': nr }, }, 'metdata': ratios_dist['metadata'] }
def get_pupil_teacher_ratios_profile(geo_code, geo_level, session): # pupil teacher ratios ratio_data, _ = get_stat_data( 'pupil teacher ratios', geo_level, geo_code, session) pupil_teacher_ratio = ratio_data['Pupil teacher ratio']['numerators']['this'] pupils_per_textbook = ratio_data['Pupils per textbook']['numerators']['this'] pupil_attendance_rate_dist = \ ratio_data['Government school attendance rate']['numerators']['this'] pupil_attendance_rate_dist = get_dictionary("Attending school", "Absent", pupil_attendance_rate_dist) teachers_absent_dist = ratio_data['Teachers absent']['numerators']['this'] teachers_absent_dist = get_dictionary("Teachers absent", "Teachers present", teachers_absent_dist) return { 'pupil_attendance_rate_dist': pupil_attendance_rate_dist, 'teachers_absent_dist': teachers_absent_dist, 'pupil_teacher_ratio': { 'name': 'For every one teacher there are ' + str(pupil_teacher_ratio) + " pupils", 'numerators': {'this': pupil_teacher_ratio}, 'values': {'this': pupil_teacher_ratio} }, 'pupils_per_textbook': { 'name': 'Pupils per textbook', 'numerators': {'this': pupils_per_textbook}, 'values': {'this': pupils_per_textbook} } }
def get_fertility_profile(geo_code, geo_level, session): # fertility dist, _ = get_stat_data(['fertility'], geo_level, geo_code, session) percentage_women_age_15_49_currently_pregnant = dist['Pregnant']['numerators']['this'] percentage_women_age_15_49_currently_pregnant = get_dictionary('Pregnant', 'Not pregnant',\ percentage_women_age_15_49_currently_pregnant, dist) fertility_rate = dist['Rate']['numerators']['this'] mean_number_of_children_ever_born_to_women_aged_40_49 = dist['Mean']['numerators']['this'] return { 'percentage_women_age_15_49_currently_pregnant': percentage_women_age_15_49_currently_pregnant, 'fertility_rate': { 'name': 'Fertility', 'numerators': {'this': fertility_rate}, 'values': {'this': fertility_rate}, 'metadata': dist['metadata'] }, 'mean_number_of_children_ever_born_to_women_aged_40_49': { 'name': 'Mean number of children ever born to women aged 40-49', 'numerators': {'this': mean_number_of_children_ever_born_to_women_aged_40_49}, 'values': {'this': mean_number_of_children_ever_born_to_women_aged_40_49}, 'metadata': dist['metadata'] }, }
def stat_data(self): """ Calculate the statistical data """ try: self.distribution, self.total = get_stat_data( [self.profile.field_name], self.geo, self.session, table_name=self.profile.table_name.name, exclude_zero=self.profile.exclude_zero, percent=self.profile.percent, recode=self.profile.recode, key_order=self.profile.key_order, exclude=self.profile.exclude, order_by=self.profile.order_by, ) self.compare_geos() if self.profile.group_remainder: group_remainder(self.distribution, self.profile.group_remainder) self.distribution = enhance_api_data(self.distribution) return {"stat_values": self.distribution} except KeyError as error: log.warn( "Unable to calculate statistics for profile: %s", self.profile.title ) log.warn("error: %s", error) return {} except DataNotFound as error: log.warn( "Unable to calculate statistics for profile: %s", self.profile.title ) log.warn("Unable to find data for this indicator: %s", error) return {}
def get_tetanus_vaccine_profile(geo, session): if geo.geo_level != 'region' and geo.geo_level != 'country': return {} tetanus_vaccine_data, _ = get_stat_data('tetanus vaccine', geo=geo, session=session, order_by='-total') number_vaccinated = tetanus_vaccine_data['Vaccinated']['numerators'][ 'this'] coverage = tetanus_vaccine_data['Coverage']['numerators']['this'] return { 'number_vaccinated': { 'name': 'Number of pregnant women received two or more Tetanus Toxoid vaccine (TT2+) (2014)', 'numerators': { 'this': number_vaccinated }, 'values': { 'this': number_vaccinated } }, 'coverage': { 'name': 'Coverage of pregnant women who received two or more Tetanus Toxoid vaccine (TT2+) (2014)', 'numerators': { 'this': coverage }, 'values': { 'this': coverage } }, 'source_link': 'http://www.opendata.go.tz/dataset/number-and-percentage-of-pregnant-women-received-two-or-more-tetanus-toxoid-vaccine-tt2-by-region', 'source_name': 'opendata.go.tz', }
def get_v6marketshare_profile(geo, session): view_ft_marketshare_v6users = OrderedDict() ispAmount = 0 try: view_ft_marketshare_v6users, _ = get_stat_data( 'asname', geo, session, order_by='-total', table_fields='total', table_name='ft_marketshare_v6users', exclude_zero=True) ispAmount = len(view_ft_marketshare_v6users) - 1 except Exception as e: ispAmount = 0 view_ft_marketshare_users = None return { 'isp_count': { "name": "IPv6 ISP's", "values": { 'this': ispAmount } }, 'view_ft_marketshare_v6users': view_ft_marketshare_v6users }
def dataset_context_stat_data(self): """ Calulate statistical data with a particular context """ with dataset_context(year=str(self.profile.dataset_context)): try: self.distribution, self.total = get_stat_data( [self.profile.field_name], self.geo, self.session, table_name=self.profile.table_name.name, exclude_zero=self.profile.exclude_zero, percent=self.profile.percent, recode=self.profile.recode, key_order=self.profile.key_order, exclude=self.profile.exclude, order_by=self.profile.order_by, ) self.compare_geos() if self.profile.group_remainder: group_remainder(self.distribution, self.profile.group_remainder) self.distribution = enhance_api_data(self.distribution) return {"stat_values": self.distribution} except DataNotFound: return {}
def get_primary_school_teachers_profile(geo, session): if geo.geo_level == 'ward': return {} try: ps_teachers, n_teachers = get_stat_data('primary school teachers',\ geo=geo, session=session, order_by='-total') except Exception as e: ps_teachers = None n_teachers = None return { 'number_of_teachers': { 'name': 'Number of primary school teachers (2016)', 'numerators': { 'this': n_teachers }, 'values': { 'this': n_teachers } }, 'source_name': 'opendata.go.tz', 'source_link': 'http://www.opendata.go.tz/dataset/number-of-primary-school-teachers', 'primary_school_teachers': ps_teachers }
def get_election_data(geo, election, session): party_data, total_valid_votes = get_stat_data( ['party'], geo, session, table_dataset=election['dataset'], recode=lambda f, v: make_party_acronym(v), order_by='-total') results = { 'name': election['name'], 'party_distribution': party_data, 'geo_version': election['geo_version'] } # voter registration and turnout table = get_datatable('voter_turnout_%s' % election['table_code']) results.update( table.get_stat_data( geo, 'registered_voters', percent=False, recode={'registered_voters': 'Number of registered voters'})[0]) results.update( table.get_stat_data( geo, 'total_votes', percent=True, total='registered_voters', recode={'total_votes': 'Of registered voters cast their vote'})[0]) return results
def get_gdpyearly_profile(geo,session,request): yearSetsIni = [] ExcYearSet = [] for i in range(2001,2018): yearSetsIni.append(i) cyear = 2011 for y in yearSetsIni: if y != cyear: ExcYearSet.append(y) ExcYearSet = map(str, ExcYearSet) if request.GET.get('release') is not None: ExcYearSet = [] ryear = int(request.GET.get('release')) ExcYearSet = [e for e in yearSetsIni if e not in {ryear}] #ExcYearSet = list(set(yearSetsIni)-set(ryear)) ExcYearSet = map(str, ExcYearSet) gdp_single_year,t_lit = get_stat_data( ['gdpyear'],geo,session, table_fields =['gdpyear'], exclude = ExcYearSet, ) final_data = { 'gdp_ratio': gdp_single_year, 'total_gdp':{ "name": "Total GDP in crore", "values": {"this":t_lit} } } return final_data
def get_elections2016_profile(geocode, geo_level, session): try: candidate, total_votes = get_stat_data('presidential candidate', geo_level, geocode, session, table_fields=['presidential candidate']) except LocationNotFound: candidate, total_votes = LOCATIONNOTFOUND, 0 total_besigye = 0 for data, value in candidate.get('Kizza besigye', {}).iteritems(): if data == 'numerators': total_besigye += value['this'] final_data = { 'candidate_distribution': candidate, 'besigye_votes': { 'name': 'Besigye Votes', 'numerators': {'this': total_besigye}, }, 'total_votes': { "name": "Votes", "values": {"this": total_votes} } } try: final_data['besigye_votes']['values'] = { 'this': round(total_besigye / total_votes * 100, 2)} except ZeroDivisionError: final_data['besigye_votes']['values'] = {'this': 0} return final_data
def get_drinkingsource_profile(geo,session,request): table = 'drinkingsource_2011' if request.GET.get('release') is not None: tablereq = 'drinkingsource_' + request.GET.get('release') if tablereq in ('drinkingsource_2011'): table = tablereq else: table = 'drinkingsource_default' drinking_source_dis_data,t_lit= get_stat_data( 'drinkingsource',geo,session, table_fields=['drinkingsource'], table_name=table ) final_data = { 'drinkingsource_ratio':drinking_source_dis_data, 'total_population':{ "name": "Total Household", "values": {"this":t_lit} } } return final_data
def get_employment_profile(geo, session): # employment status employment_activity_dist, total_workers = get_stat_data( ['employment activity status', 'sex'], geo, session, recode={'employment activity status': dict(EMPLOYMENT_RECODES)}, key_order={ 'employment activity status': EMPLOYMENT_RECODES.values(), 'sex': ['Female', 'Male'] }) total_employed = 0.0 for data in employment_activity_dist['Employed'].itervalues(): if 'numerators' in data: total_employed += data['numerators']['this'] return { 'activity_status_distribution': employment_activity_dist, 'employed': { 'name': 'Employment', 'numerators': { 'this': total_employed }, 'values': { 'this': round(total_employed / total_workers * 100, 2) }, }, }
def get_ruralpopcovered_profile(geo,session,request): table = 'ruralpopcovered_2017' if request.GET.get('release') is not None: tablereq = 'ruralpopcovered_' + request.GET.get('release') if tablereq in ('ruralpopcovered_2017'): table = tablereq else: table = 'ruralpopcovered_default' if request.GET.get('table') is not None: if request.GET.get('table') in ('ruralpopcovered_2017'): table = request.GET.get('table') ruralpopcovered_dis_data,t_lit = get_stat_data( ['ruralpopcovered'],geo,session, table_fields=['ruralpopcovered','year'], table_name=table ) final_data = { 'ruralpopcovered_dis_data_distribution': ruralpopcovered_dis_data, 'total_population': { "name": "Total rural population covered", "values": {"this": t_lit} } } return final_data
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}, } }
def get_vaccinations_profile(geo_code, geo_level, session): # vaccinations dist, _ = get_stat_data(['vaccinations'], geo_level, geo_code, session, key_order=['BCG','Pentavalent 1','Pentavalent 2','Pentavalent 3','Polio 0','Polio 1', \ 'Polio 2','Polio 3','Measles','Pneumococcal 1', \ 'Pneumococcal 2','Pneumococcal 3',], only=['BCG','Pentavalent 1','Pentavalent 2','Pentavalent 3','Polio 0','Polio 1', \ 'Polio 2','Polio 3','Measles','Pneumococcal 1', \ 'Pneumococcal 2','Pneumococcal 3',], ) # need to use numerators instead of values for key in dist: if key == 'metadata': continue dist[key]['values']['this'] = dist[key]['numerators']['this'] vacc_dist, _ = get_stat_data(['vaccinations'], geo_level, geo_code, session, # only=['All basic vaccinations', 'Percentage with vaccination card',\ # 'Fully vaccinated', 'Not vaccinated'], ) fully_vaccinated = vacc_dist['Fully vaccinated']['numerators']['this'] fully_vaccinated = get_dictionary('Fully vaccinated', 'Not fully vaccinated', fully_vaccinated, dist) all_basic_vaccinations = vacc_dist['All basic vaccinations']['numerators']['this'] all_basic_vaccinations = get_dictionary('All basic vaccinations', 'Basic vaccinations not administered', \ all_basic_vaccinations, dist) percentage_with_vaccination_cards = vacc_dist['Percentage with vaccination card']['numerators']['this'] percentage_with_vaccination_cards = get_dictionary('Have vaccination cards', 'No vaccination cards',\ percentage_with_vaccination_cards, dist) not_vaccinated = vacc_dist['Not vaccinated']['numerators']['this'] return { 'distribution': dist, 'fully_vaccinated': fully_vaccinated, 'all_basic_vaccinations': all_basic_vaccinations, 'percentage_with_vaccination_cards': percentage_with_vaccination_cards, 'not_vaccinated': { 'name': 'Not vaccinated', 'numerators': {'this': not_vaccinated}, 'values': {'this': not_vaccinated}, 'metadata': dist['metadata'] }, }
def get_voterregistration_profile(geo_code, geo_level, session): stats_dist, _ = get_stat_data("voterregistration", geo_level, geo_code, session) ids_issued = stats_dist['IDs issued']['numerators']['this'] dead_with_ids = stats_dist['Dead with IDs']['numerators']['this'] reg_centers = stats_dist['Registration centers']['numerators']['this'] reg = stats_dist['Registered voters']['numerators']['this'] aft = stats_dist['Additional voters registered']['numerators']['this'] total = stats_dist['Total registered']['numerators']['this'] not_registered = stats_dist['Potential voting population not registered']['numerators']['this'] return { 'ids_issued': { 'name': 'Number of IDs issued as at Dec 2015', 'numerators': {'this': ids_issued}, 'values': {'this': ids_issued}, }, 'dead_with_ids': { 'name': 'Projected dead with IDs 10.57%', 'numerators': {'this': dead_with_ids}, 'values': {'this': dead_with_ids}, }, 'reg_centers': { 'name': 'Number of registration centers', 'numerators': {'this': reg_centers}, 'values': {'this': reg_centers}, }, 'total': { 'name': 'Total population registered to vote', 'numerators': {'this': total}, 'values': {'this': total}, }, 'registration': { 'march': { 'name': 'As at Mar 2013', 'numerators': {'this': reg}, 'values': {'this': round((reg / total) * 100) }, }, 'oct': { 'name': 'As at Oct 2015', 'numerators': {'this': aft}, 'values': {'this':round((aft / total) * 100)}, }, 'metadata': stats_dist['metadata'] }, 'registration_ratio': { 'march': { 'name': 'Registered', 'numerators': {'this': total}, 'values': {'this': round((total / (total + not_registered)) * 100) }, }, 'oct': { 'name': 'Not registered', 'numerators': {'this': not_registered}, 'values': {'this':round((not_registered / (total + not_registered)) * 100)}, }, 'metadata': stats_dist['metadata'] }, }
def test_get_stat_data_nulls(self): self.field_table(['gender'], """ lev,code,Male,10 lev,code,Female, """) data, total = get_stat_data(['gender'], self.geo, self.s) self.assertEqual(total, 10) self.assertEqual(data['Male']['numerators']['this'], 10) self.assertEqual(data['Male']['values']['this'], 100) self.assertIsNone(data['Female']['numerators']['this']) self.assertIsNone(data['Female']['values']['this']) data, total = get_stat_data(['gender'], self.geo, self.s, percent=False) self.assertEqual(total, 10) self.assertIsNone(data['Male']['numerators']['this']) self.assertEqual(data['Male']['values']['this'], 10) self.assertIsNone(data['Female']['numerators']['this']) self.assertIsNone(data['Female']['values']['this'])
def get_schools_profile(geo, session): # population group _, total_pop = get_stat_data( ['population group'], geo, session, table_dataset='Census 2011') # Schools table = get_datatable('schools_2015') keys = ['primary_schools', 'combined_schools', 'intermediate_schools', 'secondary_schools'] school_breakdown, total_schools = table.get_stat_data( geo, keys, percent=False) primary_school_ages = ['6', '7', '8', '9', '10', '11', '12', '13'] secondary_school_ages = ['14', '15', '16', '17', '18'] _, total_primary_children = get_stat_data( ['age in completed years'], geo, session, table_name='ageincompletedyears', only=primary_school_ages) _, total_secondary_children = get_stat_data( ['age in completed years'], geo, session, table_name='ageincompletedyears', only=secondary_school_ages) children_per_primary_school = ratio(total_primary_children, school_breakdown['primary_schools']['values']['this']) children_per_secondary_school = ratio(total_secondary_children, school_breakdown['secondary_schools']['values']['this']) final_data = { 'total_schools': { "name": "Schools", "values": {"this": total_schools} }, "school_breakdown": school_breakdown, "children_per_primary_school": { "name": "Children (6-13 years) in the area for each primary school", "values": {"this": children_per_primary_school} }, "children_per_secondary_school": { "name": "Children (14-18 years) for each secondary school", "values": {"this": children_per_secondary_school} } } return final_data
def get_maternal_care_indicators_profile(geo_code, geo_level, session): # maternal care indicators stats maternal_care_indicators_data, _ = get_stat_data( 'maternal care indicators', geo_level, geo_code, session) delivery_in_health_facility = \ maternal_care_indicators_data['Percentage delivered in a health facility']['numerators']['this'] antenatal_care_by_skilled_provider = \ maternal_care_indicators_data['Percentage with antenatal care from a skilled provider']['numerators']['this'] antenatal_dist = get_dictionary('From a skilled provider', 'From a non-skilled provider', antenatal_care_by_skilled_provider,maternal_care_indicators_data ) anc_visits = maternal_care_indicators_data['Percentage with 4+ ANC visits']['numerators']['this'] anc_visits_dist = get_dictionary('4+', 'Less than 4', anc_visits, maternal_care_indicators_data ) delivery_by_skilled_provider = \ maternal_care_indicators_data['Percentage delivered by a skilled provider']['numerators']['this'] delivery_by_skilled_provider_dist = get_dictionary('Skilled provider', 'Non-skilled provider', delivery_by_skilled_provider, maternal_care_indicators_data) #Use of IPT use_of_ipt_dist, _ = get_stat_data('use of ipt', geo_level, geo_code, session) one_or_more_dist = use_of_ipt_dist['1 or more']['numerators']['this'] one_or_more_dist = get_dictionary('1 or more', 'Less', one_or_more_dist,maternal_care_indicators_data) two_or_more_dist = use_of_ipt_dist['2 or more']['numerators']['this'] two_or_more_dist = get_dictionary('2 or more', 'Less', two_or_more_dist, maternal_care_indicators_data) three_or_more_dist = use_of_ipt_dist['3 or more']['numerators']['this'] three_or_more_dist = get_dictionary('3 or more', 'Less', three_or_more_dist, maternal_care_indicators_data) return { 'maternal_care_indicators': maternal_care_indicators_data, 'antenatal_dist': antenatal_dist, 'anc_visits_dist': anc_visits_dist, 'delivery_by_skilled_provider_dist': delivery_by_skilled_provider_dist, 'delivery_in_health_facility': { 'name': 'babies delivered in a health facility', 'numerators': {'this': delivery_in_health_facility}, 'values': {'this': round(delivery_in_health_facility, 2)} }, 'one_or_more_dist': one_or_more_dist, 'two_or_more_dist': two_or_more_dist, 'three_or_more_dist': three_or_more_dist, }
def get_education_profile(geo_code, geo_level, session): youth_completed_grade9, _ = get_stat_data(['completed grade9'], geo_level, geo_code, session, table_name='youth_age_16_to_17_gender_completed_grade9') youth_gender_completed_grade9, _ = get_stat_data(['gender', 'completed grade9'], geo_level, geo_code, session, table_name='youth_age_16_to_17_gender_completed_grade9') db_model_gender_completed_grade9 = get_model_from_fields(['gender'], geo_level, table_name='youth_age_16_to_17_gender_completed_grade9') gender_completed_grade9_data = OrderedDict(( # census data refers to sex as gender ('Female', { "name": "Female", "values": {"this": youth_gender_completed_grade9['Female']['Yes']['values']['this']}, "numerators": {"this": youth_gender_completed_grade9['Female']['Yes']['numerators']['this']}, }), ('Male', { "name": "Male", "values": {"this": youth_gender_completed_grade9['Male']['Yes']['values']['this']}, "numerators": {"this": youth_gender_completed_grade9['Male']['Yes']['numerators']['this']}, }), )) add_metadata(gender_completed_grade9_data, db_model_gender_completed_grade9) youth_education_level, youth_pop_20_to_24 = get_stat_data(['education level'], geo_level, geo_code, session, table_name='youth_age_20_to_24_gender_education_level') matric_or_equiv = ( youth_education_level['Matric']['numerators']['this'] + youth_education_level['Tertiary']['numerators']['this'] + youth_education_level['Some secondary']['numerators']['this']) final_data = { 'youth_completed_grade9': youth_completed_grade9, 'youth_perc_completed_grade9': { "name": "Of youth aged 16-17 have completed grade 9", "values": {"this": youth_completed_grade9['Yes']['values']['this']}, }, 'youth_gender_completed_grade9': gender_completed_grade9_data, 'youth_perc_matric': { "name": "Of youth aged 20-24 have completed matric or matric equivalent", "values": {"this": percent(matric_or_equiv, youth_pop_20_to_24)}, }, 'youth_education_level': youth_education_level } return final_data
def get_protests_profile(geo_code, geo_level, session): number_of_protests_dist, _ = get_stat_data("protests", geo_level, geo_code, session) number_of_protests = number_of_protests_dist['Number of protests']['numerators']['this'] return { 'number_of_protests': { 'name': 'Number of protests', 'numerators': {'this': number_of_protests}, 'values': {'this': number_of_protests}, 'metadata': number_of_protests_dist['metadata'] }, }
def get_crimereport_profile(geo_code, geo_level, session): stats_dist, s_ = get_stat_data("crimereport", geo_level, geo_code, session) crimes_dist,c_ = get_stat_data("typesofcrime", geo_level, geo_code, session) crimes = stats_dist['Crimes']['numerators']['this'] crimeindex = stats_dist['Crimesindex']['numerators']['this'] return { 'crimes': { 'name': 'Number of crimes', 'numerators': {'this': crimes}, 'values': {'this': crimes}, 'metadata': crimes_dist['metadata'] }, 'crimesindex': { 'name': 'Crime index per 100,000 people', 'numerators': {'this': crimeindex}, 'values': {'this': crimeindex}, 'metadata': crimes_dist['metadata'] }, 'crimes_dist': crimes_dist, }
def get_crop_production_profile(geo_code, geo_level, session): dist, _ = get_stat_data("crop_production", geo_level, geo_code, session, percent = False) area_dist = OrderedDict() area_dist['maize'] = dist['Maize'] area_dist['beans'] = dist['Beans'] area_dist['wheat'] = dist['Wheat'] area_dist['barley'] = dist['Barley'] area_dist['rice'] = dist['Rice'] area_dist['sorghum'] = dist['Sorghum'] area_dist['millet'] = dist['Millet'] area_dist['cowpeas'] = dist['Cowpeas'] area_dist['greengrams'] = dist['Greengrams'] area_dist['sweetpotatoes'] = dist['Sweetpotatoes'] area_dist['cassava'] = dist['Cassava'] area_dist['cocoyam'] = dist['Cocoyam'] area_dist['irishpotatoes'] = dist['Irishpotatoes'] area_dist['metadata'] = dist['metadata'] prod_dist_k = OrderedDict() prod_dist_k['maize'] = replace_name(dist['Maize (90kg bags)'], 'Maize') prod_dist_k['beans'] = replace_name(dist['Beans (90kg bags)'], 'Beans') prod_dist_k['wheat'] = replace_name(dist['Wheat (90kg bags)'], 'Wheat') prod_dist_k['sorghum'] = replace_name(dist['Sorghum (90kg bags)'], 'Sorghum') prod_dist_k['millet'] = replace_name(dist['Millet (90kg bags)'], 'Millet') prod_dist_k['cowpeas'] = replace_name(dist['Cowpeas (90kg bags)'], 'Cowpeas') prod_dist_k['greengrams'] = replace_name(dist['Greengrams (90kg bags)'], 'Green grams') prod_dist_t = OrderedDict() prod_dist_t['barley'] = replace_name(dist['Barley (Ton)'], 'Barley') prod_dist_t['rice'] = replace_name(dist['Rice (Ton)'], 'Rice') prod_dist_t['sweetpotatoes'] = replace_name(dist['Sweetpotatoes (Ton)'], 'Sweet Potatoes') prod_dist_t['cassava'] = replace_name( dist['Cassava (Ton)'], 'Cassava') prod_dist_t['cocoyam'] = replace_name(dist['Cocoyam (Ton)'], 'Cocoyam') prod_dist_t['irishpotatoes'] = replace_name(dist['Irishpotatoes (Ton)'], 'Irish potatoes') prod_dist_t['metadata'] = dist['metadata'] yield_dist = OrderedDict() yield_dist['maize'] = replace_name(dist['Maize yield'], 'Maize') yield_dist['beans'] = replace_name(dist['Beans yield'], 'Beans') yield_dist['wheat'] = replace_name(dist['Wheat yield'], 'Wheat') yield_dist['barley'] = replace_name(dist['Barley yield'], 'Barley') yield_dist['rice'] = replace_name(dist['Rice yield'], 'Rice') yield_dist['sorghum'] = replace_name(dist['Sorghum yield'], 'Sorghum') yield_dist['millet'] = replace_name(dist['Millet yield'], 'Millet') yield_dist['cowpeas'] = replace_name(dist['Cowpeas yield'], 'Cowpeas') yield_dist['greengrams'] = replace_name(dist['Greengrams yield'], 'Green grams') yield_dist['sweetpotatoes'] = replace_name(dist['Sweetpotatoes yield'], 'Sweet potatoes') yield_dist['cassava'] = replace_name(dist['Cassava yield'], 'Cassava') yield_dist['cocoyam'] = replace_name(dist['Cocoyam yield'], 'Cocoyam') yield_dist['irishpotatoes'] = replace_name(dist['Irishpotatoes yield'], 'Irish potatoes') yield_dist['metadata'] = dist['metadata'] return { 'area_dist': area_dist, 'prod_dist_t': prod_dist_t, 'prod_dist_k': prod_dist_k, 'yield_dist': yield_dist, }
def get_schoolfires_profile(geo_code, geo_level, session): school_fires_dist, number_of_school_fires = get_stat_data("schoolfires", geo_level, geo_code, session) schools = school_fires_dist[school_fires_dist.keys()[0]]['name'].replace(',', '<br>').replace('"','') return { 'schoolfires': { 'name': 'Number of school fires', 'numerators': {'this': number_of_school_fires}, 'values': {'this': number_of_school_fires}, 'metadata': school_fires_dist['metadata'], }, 'schools': schools }
def get_disabilities_profile(geo, session, year): with dataset_context(year=year): try: disabled_or_not, total_ = get_stat_data('disabled or not', geo, session, table_fields=[ 'disabled or not']) disability, _ = get_stat_data('disability', geo, session, table_fields=['disability']) except Exception: disabled_or_not, total_ = LOCATIONNOTFOUND, 0 disability = LOCATIONNOTFOUND total_disabled = 0 for data, value in disabled_or_not.get('With disability', {}).items(): if data == 'numerators': total_disabled += value['this'] is_missing = disability.get('is_missing') and disabled_or_not.get('is_missing') final_data = { 'is_missing': is_missing, 'disabled_or_not_distribution': disabled_or_not, 'disability': disability, 'total_disabled': { 'name': 'Disabled', 'numerators': {'this': total_disabled}, }, 'total_': { "name": "Population", "values": {"this": total_} } } try: final_data['total_disabled']['values'] = { 'this': round(total_disabled / total_ * 100, 2)} except ZeroDivisionError: final_data['total_disabled']['values'] = {'this': 0} return final_data
def test_get_stat_data_nulls_with_denominator_key(self): table = self.field_table(['household goods'], None, universe='Households', denominator_key='total households') self.load_data(table, """ lev,code,fridge,10 lev,code,computer,5 lev,code,total households, """) data, total = get_stat_data(['household goods'], self.geo, self.s) self.assertEqual(total, None) self.assertEqual(data['Fridge']['numerators']['this'], 10) self.assertIsNone(data['Fridge']['values']['this']) self.assertEqual(data['Computer']['numerators']['this'], 5) self.assertIsNone(data['Computer']['values']['this'])