Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    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 {}
Ejemplo n.º 3
0
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
Ejemplo n.º 4
0
    def get(self, request, geo_level, geo_code):
        results = {}
        version = self.request.GET.get("geo_version",
                                       settings.WAZIMAP["default_geo_version"])

        geography = Geography.objects.get(geo_level=geo_level,
                                          geo_code=geo_code,
                                          version=version)
        year = self.request.GET.get("release",
                                    geo_data.primary_release_year(geography))
        results["geograhy"] = geography.as_dict_deep()

        if settings.WAZIMAP["latest_release_year"] == year:
            year = "latest"

        build_indicator = settings.DYNAMIC_PROFILE_INDICATOR
        if build_indicator:
            module = importlib.import_module(build_indicator["path"])
            build_indicator = getattr(module, build_indicator["class"])
        else:
            build_indicator = BuildIndicator

        session = get_session()
        with dataset_context(year=year):
            section = Section(geography, session)
            section_results = section.build(BuildProfile, build_indicator)
            results["data"] = section_results

        return Response(results, status=status.HTTP_200_OK)
Ejemplo n.º 5
0
    def get_context_data(self, *args, **kwargs):
        json_data = open("takwimu/fixtures/sdg.json")
        data = json.load(json_data)

        # get profileData aka summaries from wagtail
        summary_data = ProfileData.objects.filter(
            country_iso_code=self.geo.geo_code).values('id', 'chart_id',
                                                       'summary')

        page_context = {"profile_data_summaries": list(summary_data)}

        # get data by topic page text description
        try:
            page = DataByTopicPage.objects.get(country=self.geo)
            page_context['profile_data_description'] = page.description
        except DataByTopicPage.DoesNotExist:
            page_context['profile_data_description'] = ''

        # load the profile
        profile_method = takwimu_settings.HURUMAP.get("profile_builder", None)
        self.profile_name = takwimu_settings.HURUMAP.get(
            "default_profile", "default")

        if not profile_method:
            raise ValueError(
                "You must define WAZIMAP.profile_builder in settings.py")
        profile_method = import_string(profile_method)

        year = self.request.GET.get(
            "release", get_primary_release_year_per_geography(self.geo))

        with dataset_context(year=year):
            profile_data = profile_method(self.geo, self.profile_name,
                                          self.request)

        profile_data["geography"] = self.geo.as_dict_deep()
        profile_data["primary_releases"] = get_page_releases_per_country(
            takwimu_settings.HURUMAP["primary_dataset_name"], self.geo, year)

        profile_data = enhance_api_data(profile_data)
        page_context.update(profile_data)

        profile_data_json = SafeString(
            json.dumps(profile_data, cls=DjangoJSONEncoder))

        page_context.update({"profile_data_json": profile_data_json})

        # is this a head-to-head view?
        page_context["head2head"] = "h2h" in self.request.GET

        page_context["sdgs"] = data
        page_context.update(settings(self.request))
        page_context.update(takwimu_countries(self.request))
        page_context.update(takwimu_stories(self.request))
        page_context.update(takwimu_topics(self.request))

        return page_context
Ejemplo n.º 6
0
 def get_context_data(self, *args, **kwargs):
     latest_release_year = self.release.year
     with dataset_context(year=latest_release_year):
         table_columns = self.table.columns()
     return {
         'table': self.table,
         'latest_release_year': latest_release_year,
         'table_columns': table_columns
     }
Ejemplo n.º 7
0
def get_elections2016_profile(geo, session):
    with dataset_context(year='2016'):
        try:
            candidate, total_votes = get_stat_data('presidential candidate', geo,
                                                   session,
                                                   table_fields=[
                                                       'presidential candidate'],
                                                   order_by="-total")

        except Exception:
            candidate, total_votes = LOCATIONNOTFOUND, 0

        total_besigye = 0
        for data, value in candidate.get('Kizza besigye', {}).items():
            if data == 'numerators':
                total_besigye += value['this']
        total_museveni = 0
        for data, value in candidate.get('Yoweri museveni', {}).items():
            if data == 'numerators':
                total_museveni += value['this']

    is_missing = candidate.get('is_missing')

    final_data = {
        'is_missing': is_missing,
        'candidate_distribution': candidate,
        'museveni_votes': {
            'name': 'Museveni Votes',
            'numerators': {'this': total_museveni}
        },
        '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)}
        final_data['museveni_votes']['values'] = {
            'this': round(total_museveni / total_votes * 100, 2)
        }
    except ZeroDivisionError:
        final_data['besigye_votes']['values'] = {'this': 0}
        final_data['museveni_votes']['values'] = {'this': 0}
    return final_data
Ejemplo n.º 8
0
    def get(self, request, *args, **kwargs):
        try:
            self.geo_ids = request.GET.get('geo_ids', '').split(',')
            geo_version = request.GET.get('geo_version', None)
            self.data_geos, self.info_geos = self.get_geos(
                self.geo_ids, geo_version)
        except LocationNotFound as e:
            return render_json_error(e.message, 404)

        try:
            self.table_ids = request.GET.get('table_ids', '').split(',')
            self.tables = [get_datatable(t) for t in self.table_ids]
        except KeyError as e:
            return render_json_error('Unknown table: %s' % e.message, 404)

        # tables should all be from the same dataset
        datasets = set(t.dataset for t in self.tables)
        if len(datasets) > 1:
            return render_json_error(
                "All tables must belong to the same dataset.", 400)
        self.dataset = list(datasets)[0]

        self.year = kwargs['release']
        if settings.WAZIMAP['latest_release_year'] == self.year:
            self.year = 'latest'

        self.available_releases = get_page_releases(self.dataset.name,
                                                    self.data_geos[0],
                                                    self.year,
                                                    filter_releases=False)

        self.release = None
        for table in self.tables:
            release = table.get_release(year=self.year)
            if not release:
                return render_json_error(
                    "No release %s for table %s." %
                    (kwargs['release'], table.name.upper()), 400)

            # different?
            if self.release and self.release != release:
                return render_json_error(
                    "All tables must have the same release.", 400)

            self.release = release

        with dataset_context(year=self.release.year):
            if kwargs.get('action') == 'show':
                return self.show(request)
            if kwargs.get('action') == 'download':
                return self.download(request)
Ejemplo n.º 9
0
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,
    }
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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
Ejemplo n.º 12
0
def get_demographics_profile(geo, session):
    with dataset_context(year='2016'):
        pop_dist_data = LOCATIONNOTFOUND
        total_pop = 0
        try:
            pop_dist_data, total_pop = get_stat_data(
                ['population group'], geo, session)
        except Exception:
            pass

    return {
        'total_population': {
            "name": "People",
            "values": {"this": total_pop},
        },
        'is_missing': pop_dist_data.get('is_missing', False)

    }
Ejemplo n.º 13
0
def get_crime_profile(geo, session):
    with dataset_context(year="2014"):
        child_crime, total = get_stat_data(
            ["crime"],
            geo,
            session,
            table_universe="Crimes",
            only=["Neglect and ill-treatment of children"],
            percent=False,
        )

    return {
        "metadata": child_crime["metadata"],
        "crime_against_children": {
            "name": "Crimes of neglect and ill-treatment of children in 2014",
            "values": {"this": total},
            "metadata": child_crime["metadata"],
        },
    }
Ejemplo n.º 14
0
def get_county_expenditure_ceilings(geo, session):
    try:
        with dataset_context(year='2016'):
            ceilings, _ = get_stat_data(['expenditure', 'year'],
                                        geo, session, table_fields=['expenditure',
                                                                    'year'],
                                        percent=False)

        final_data = {}
        county_assembly = ceilings['County Assembly']
        county_exec = ceilings['County Executive']
        final_data['county_assembly'] = {
            'fy2015_2016': {
                'name': county_assembly['FY 2015/2016'].get('name'),
                'numerators': county_assembly['FY 2015/2016'].get('numerators'),
                'values': county_assembly['FY 2015/2016'].get('values')

            },
            'fy2016_2017': {
                'name': county_assembly['FY 2016/2017'].get('name'),
                'numerators': county_assembly['FY 2016/2017'].get('numerators'),
                'values': county_assembly['FY 2016/2017'].get('values')
            }
        }

        final_data['county_executive'] = {
            'fy2015_2016': {
                'name': county_exec['FY 2015/2016'].get('name'),
                'numerators': county_exec['FY 2015/2016'].get('numerators'),
                'values': county_exec['FY 2015/2016'].get('values')

            },
            'fy2016_2017': {
                'name': county_exec['FY 2016/2017'].get('name'),
                'numerators': county_exec['FY 2016/2017'].get('numerators'),
                'values': county_exec['FY 2016/2017'].get('values')
            }
        }
        return final_data

    except Exception:
        ceilings, _ = LOCATIONNOTFOUND, 0
Ejemplo n.º 15
0
 def context_comparative_geo(self, comp_geo):
     """
     Calculte the dataset_context stat_data for a comparative geo.
     """
     with dataset_context(year=str(self.profile.dataset_context)):
         try:
             distribution, total = get_stat_data(
                 [self.profile.field_name],
                 comp_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,
             )
             return distribution
         except DataNotFound:
             return {}
Ejemplo n.º 16
0
def get_conditional_allocation_2015_2016(geo, session):
    try:
        with dataset_context(year='2016'):
            conditional, _ = get_stat_data(['conditional_fund'], geo, session,
                                           table_fields=['conditional_fund'],
                                           percent=False)
        final_data = {}
        data = ['DANIDA Grant', 'Other Conditional Allocations',
                'World Bank Loan']
        for i in data:
            fund = conditional.get(i)

            final_data[i.lower().replace(' ', '_')] = {
                'name': i,
                'numerators': fund['numerators'],
                'values': fund['values']
            }

        return final_data
    except Exception:
        conditional, _ = LOCATIONNOTFOUND, 0
Ejemplo n.º 17
0
def get_sectionaltitleland_profile(geo, session):
    year = current_context().get('year')
    with dataset_context(year=year):
        topic_profiles = SECTIONS['sectionaltitleland']['profiles']
        profiles_data = {'is_missing': True}

        for profile in topic_profiles:
            try:
                profile_table = profile.lower()
                profile_name = profile.lower().replace(' ', '_')
                profiles_data[profile_name] = LOCATIONNOTFOUND
                profiles_data[profile_name], _ = get_stat_data([profile_table],
                                                               geo, session)
            except Exception:
                pass

            profiles_data['is_missing'] = profiles_data.get('is_missing') and \
                                          profiles_data[profile_name].get(
                                              'is_missing')

    return profiles_data
Ejemplo n.º 18
0
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
Ejemplo n.º 19
0
    def get_context_data(self, **kwargs):
        page_context = {}

        # load the profile
        profile_method = settings.HURUMAP.get('profile_builder', None)
        self.profile_name = settings.HURUMAP.get('default_profile', 'default')

        if not profile_method:
            raise ValueError(
                "You must define WAZIMAP.profile_builder in settings.py")
        profile_method = import_string(profile_method)

        year = self.request.GET.get(
            'release', get_primary_release_year_per_geography(self.geo))
        if settings.HURUMAP['latest_release_year'] == year:
            year = 'latest'

        with dataset_context(year=year):
            profile_data = profile_method(
                self.geo, self.profile_name, self.request)

        profile_data['geography'] = self.geo.as_dict_deep()
        profile_data['primary_releases'] = get_page_releases_per_country(
            settings.HURUMAP['primary_dataset_name'], self.geo, year)

        profile_data = enhance_api_data(profile_data)
        page_context.update(profile_data)

        profile_data_json = SafeString(
            json.dumps(profile_data, cls=DjangoJSONEncoder))

        page_context.update({
            'profile_data_json': profile_data_json
        })

        # is this a head-to-head view?
        page_context['head2head'] = 'h2h' in self.request.GET

        return page_context
Ejemplo n.º 20
0
def get_election_data(geo, election, session):
    with dataset_context(year=election['release_year']):
        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,
                                       year=election['release_year'],
                                       recode={'registered_voters': 'Number of registered voters'})[0])
    results.update(table.get_stat_data(geo, 'total_votes', percent=True, total='registered_voters',
                                       year=election['release_year'],
                                       recode={'total_votes': 'Of registered voters cast their vote'})[0])

    return results
Ejemplo n.º 21
0
    def get_context_data(self, *args, **kwargs):
        page_context = {}

        # load the profile
        profile_method = settings.WAZIMAP.get('profile_builder', None)
        self.profile_name = settings.WAZIMAP.get('default_profile', 'default')

        if not profile_method:
            raise ValueError(
                "You must define WAZIMAP.profile_builder in settings.py")
        profile_method = import_string(profile_method)

        year = self.request.GET.get('release',
                                    geo_data.primary_release_year(self.geo))
        if settings.WAZIMAP['latest_release_year'] == year:
            year = 'latest'

        with dataset_context(year=year):
            profile_data = profile_method(self.geo, self.profile_name,
                                          self.request)

        profile_data['geography'] = self.geo.as_dict_deep()
        profile_data['primary_releases'] = get_page_releases(
            settings.WAZIMAP['primary_dataset_name'], self.geo, year)

        profile_data = enhance_api_data(profile_data)
        page_context.update(profile_data)

        profile_data_json = SafeString(
            json.dumps(profile_data, cls=DjangoJSONEncoder))

        page_context.update({'profile_data_json': profile_data_json})

        # is this a head-to-head view?
        page_context['head2head'] = 'h2h' in self.request.GET

        return page_context
Ejemplo n.º 22
0
def get_schools_profile(geo, session, year):
    schools_dist = LOCATIONNOTFOUND
    region_dist = LOCATIONNOTFOUND
    category_dist = LOCATIONNOTFOUND
    top_schools_40_more = []
    top_schools_40_less = []
    lowest_schools_40_less = []
    lowest_schools_40_more = []
    gpa_dist_data = []
    gender_dist = []
    total_schools = 0
    median = 0

    reg = 'region'
    if geo.geo_level == "country":
        reg = 'region'
    elif geo.geo_level == "region":
        reg = 'district'
    elif geo.geo_level == "district":
        reg = 'ward'

    with dataset_context(year='2017'):
        try:
            schools_dist, total_schools = get_stat_data(
                ['ownership'],
                geo=geo,
                table_name='secondary_school',
                session=session,
                only={'year_of_result': [year]})
        except Exception as e:
            pass

        try:
            region_dist, total_schools = get_stat_data(
                [reg],
                geo=geo,
                session=session,
                only={'year_of_result': [year]})
        except Exception as e:
            pass

        try:
            category_dist, _ = get_stat_data(['more_than_40'],
                                             geo=geo,
                                             session=session,
                                             only={'year_of_result': [year]})
        except Exception as e:
            pass

        try:
            gender_dist, _ = get_stat_data(['gender'],
                                           geo=geo,
                                           session=session,
                                           only={'year_of_result': [year]})
        except Exception:
            pass

        try:
            # ownership status
            # school_dist_data, _ = get_stat_data('age in completed years',geo=geo, session=session, only={'year_of_result': [year]})
            # Choosing sorting option
            # Sorting will only be done using national_rank all, as regional and district ranks are unknown for some result esp historical
            Base.metadata.reflect()
            rank_column = Base.metadata.tables[
                'secondary_school'].c.national_rank_all
            # Getting top for schools with more than 40 students
            top_schools_40_more = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like(
                    "yes%")) \
                .order_by(asc(cast(rank_column, Integer))) \
                .all()
            # Getting top for schools with less than 40 students
            top_schools_40_less = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like("no%")) \
                .order_by(asc(cast(rank_column, Integer))) \
                .all()

            # Getting lowest schools with more than 40 students
            lowest_schools_40_more = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like(
                    "yes%")) \
                .order_by(desc(cast(rank_column, Integer))) \
                .all()
            # Getting lowest for schools with less than 40 students
            lowest_schools_40_less = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like("no%")) \
                .order_by(desc(cast(rank_column, Integer))) \
                .all()
            # median gpa
            db_model_age = get_datatable('code_name_avg_gpa')
            objects = db_model_age.get_rows_for_geo(geo, session)
            median = calculate_median(objects, 'avg_gpa')

            # gpa in 1 point groups
            def gpa_recode(f, x):
                gpa = x
                if gpa >= 4:
                    return '4+'
                bucket = 1 * (gpa / 1)
                return '%d-%d' % (bucket, bucket + 2)

            gpa_dist_data, total_schools = get_stat_data(
                'avg_gpa',
                geo,
                session,
                table_fields=['code', 'name', 'avg_gpa'],
                recode=gpa_recode,
                exclude=['unspecified'],
                only={'year_of_result': [year]})

            total_private = 0.0
            for data in schools_dist.get('Non-Government', {}).values():
                if 'numerators' in data:
                    total_private += data['numerators']['this']
        except Exception as e:
            pass

    return {
        'schools_distribution': schools_dist,
        'region_distribution': region_dist,
        'category_distribution': category_dist,
        'best_schools_more_40': top_schools_40_more,
        'worst_schools_more_40': lowest_schools_40_more,
        'best_schools_less_40': top_schools_40_less,
        'worst_schools_less_40': lowest_schools_40_less,
        'gpa_group_distribution': gpa_dist_data,
        'gender_distribution': gender_dist,
        'total_schools': {
            "name": "Schools",
            "values": {
                "this": total_schools
            }
        },
        'median_gpa': {
            "name": "Median GPA",
            "values": {
                "this": median
            },
        },
    }
Ejemplo n.º 23
0
 def setUp(self):
     self.s = get_session()
     self.ctxt = dataset_context(year='latest')
     self.ctxt.__enter__()
Ejemplo n.º 24
0
def get_afrobarometer_profile(geo, session):
    with dataset_context(year='2016'):
        own_radio = LOCATIONNOTFOUND
        own_motor_vehicle = LOCATIONNOTFOUND
        own_television = LOCATIONNOTFOUND
        own_mobile_phone = LOCATIONNOTFOUND
        election_work_for_a_candidate_or_party = LOCATIONNOTFOUND
        election_attend_a_campaign_rally = LOCATIONNOTFOUND
        last_national_election_attend_a_campaign_meeting = LOCATIONNOTFOUND
        courts_make_binding_decisions = LOCATIONNOTFOUND
        vote_for_which_party = LOCATIONNOTFOUND
        level_of_corruption = LOCATIONNOTFOUND
        chinas_assistance_meets_countrys_needs = LOCATIONNOTFOUND
        employment_status = LOCATIONNOTFOUND
        main_reason_for_not_reporting_corruption = LOCATIONNOTFOUND
        education_of_respondent = LOCATIONNOTFOUND
        people_must_pay_taxes = LOCATIONNOTFOUND
        negative_image_of_china = LOCATIONNOTFOUND
        most_effective_way_to_combat_corruption = LOCATIONNOTFOUND
        people_can_fight_corruption = LOCATIONNOTFOUND
        people_must_obey_the_law = LOCATIONNOTFOUND
        chinas_influence_on_economy = LOCATIONNOTFOUND
        chinas_influence_positive_or_negative = LOCATIONNOTFOUND
        positive_image_of_china = LOCATIONNOTFOUND
        occupation_of_respondent = LOCATIONNOTFOUND
        employer_of_respondent = LOCATIONNOTFOUND
        how_often_use_a_mobile_phone = LOCATIONNOTFOUND
        how_often_use_the_internet = LOCATIONNOTFOUND
        freeness_and_fairness_of_last_national_election = LOCATIONNOTFOUND
        extent_of_democracy = LOCATIONNOTFOUND
        elections_ensure_voters_views_are_reflected = LOCATIONNOTFOUND
        corruption_judges_and_magistrates = LOCATIONNOTFOUND
        election_enable_voters_remove_leaders_from_office = LOCATIONNOTFOUND
        corruption_tax_officials = LOCATIONNOTFOUND
        corruption_police = LOCATIONNOTFOUND
        corruption_local_government_councilors = LOCATIONNOTFOUND
        corruption_government_officials = LOCATIONNOTFOUND
        corruption_members_of_parliament = LOCATIONNOTFOUND
        corruption_office_of_the_presidency = LOCATIONNOTFOUND
        satisfaction_with_democracy = LOCATIONNOTFOUND
        corruption_religious_leaders = LOCATIONNOTFOUND
        corruption_traditional_leaders = LOCATIONNOTFOUND
        difficulty_to_obtain_public_school_services = LOCATIONNOTFOUND
        voting_in_the_most_recent_national_election = LOCATIONNOTFOUND
        difficulty_to_obtain_medical_treatment = LOCATIONNOTFOUND
        pay_bribe_for_school_services = LOCATIONNOTFOUND
        difficulty_to_obtain_identity_document = LOCATIONNOTFOUND
        bribe_for_treatment_at_public_health = LOCATIONNOTFOUND
        difficulty_to_obtain_household_services = LOCATIONNOTFOUND
        pay_bribe_for_document_or_permit = LOCATIONNOTFOUND
        pay_bribe_for_household_services = LOCATIONNOTFOUND

        try:
            own_radio, _ = get_stat_data('own_radio', geo, session,
                                        table_fields=['own_radio'])
        except LocationNotFound:
            pass

        try:
            own_motor_vehicle, _ = get_stat_data('own_motor_vehicle', geo, session,
                                                table_fields=['own_motor_vehicle'])
        except LocationNotFound:
            pass

        try:
            own_television, _ = get_stat_data('own_television', geo, session,
                                            table_fields=['own_television'])
        except LocationNotFound:
            pass

        try:
            own_mobile_phone, _ = get_stat_data('own_mobile_phone', geo, session,
                                                table_fields=['own_mobile_phone'])
        except LocationNotFound:
            pass

        try:
            election_work_for_a_candidate_or_party, _ = get_stat_data(
                'election_work_for_a_candidate_or_party', geo,
                session, table_fields=[
                    'election_work_for_a_candidate_or_party'])
        except LocationNotFound:
            pass

        try:
            election_attend_a_campaign_rally, _ = get_stat_data(
                'election_attend_a_campaign_rally', geo, session,
                table_fields=['election_attend_a_campaign_rally'])
        except LocationNotFound:
            pass

        try:
            last_national_election_attend_a_campaign_meeting, _ = get_stat_data(
                'last_national_election_attend_a_campaign_meeting', geo, session,
                table_fields=['last_national_election_attend_a_campaign_meeting'])
        except LocationNotFound:
            pass

        try:
            courts_make_binding_decisions, _ = get_stat_data(
                'courts_make_binding_decisions', geo, session,
                table_fields=['courts_make_binding_decisions'])
        except LocationNotFound:
            pass

        try:
            vote_for_which_party, _ = get_stat_data('vote_for_which_party', geo,
                                                    session, table_fields=[
                    'vote_for_which_party'])
        except LocationNotFound:
            pass

        try:
            level_of_corruption, _ = get_stat_data('level_of_corruption', geo,
                                                session, table_fields=[
                    'level_of_corruption'])
        except LocationNotFound:
            pass

        try:
            chinas_assistance_meets_countrys_needs, _ = get_stat_data(
                'chinas_assistance_meets_countrys_needs', geo,
                session, table_fields=[
                    'chinas_assistance_meets_countrys_needs'])
        except LocationNotFound:
            pass

        try:
            employment_status, _ = get_stat_data('employment_status', geo, session,
                                                table_fields=['employment_status'])
        except LocationNotFound:
            pass

        try:
            main_reason_for_not_reporting_corruption, _ = get_stat_data(
                'main_reason_for_not_reporting_corruption', geo, session,
                table_fields=['main_reason_for_not_reporting_corruption'])
        except LocationNotFound:
            pass

        try:
            education_of_respondent, _ = get_stat_data('education_of_respondent',
                                                    geo, session, table_fields=[
                    'education_of_respondent'])
        except LocationNotFound:
            pass

        try:
            people_must_pay_taxes, _ = get_stat_data('people_must_pay_taxes', geo,
                                                    session, table_fields=[
                    'people_must_pay_taxes'])
        except LocationNotFound:
            pass

        try:
            negative_image_of_china, _ = get_stat_data('negative_image_of_china',
                                                    geo, session, table_fields=[
                    'negative_image_of_china'])
        except LocationNotFound:
            pass

        try:
            most_effective_way_to_combat_corruption, _ = get_stat_data(
                'most_effective_way_to_combat_corruption', geo, session,
                table_fields=['most_effective_way_to_combat_corruption'])
        except LocationNotFound:
            pass

        try:
            people_can_fight_corruption, _ = get_stat_data(
                'people_can_fight_corruption', geo, session,
                table_fields=['people_can_fight_corruption'])
        except LocationNotFound:
            pass

        try:
            people_must_obey_the_law, _ = get_stat_data('people_must_obey_the_law',
                                                        geo, session, table_fields=[
                    'people_must_obey_the_law'])
        except LocationNotFound:
            pass

        try:
            chinas_influence_on_economy, _ = get_stat_data(
                'chinas_influence_on_economy', geo, session,
                table_fields=['chinas_influence_on_economy'])
        except LocationNotFound:
            pass

        try:
            chinas_influence_positive_or_negative, _ = get_stat_data(
                'chinas_influence_positive_or_negative', geo, session,
                table_fields=['chinas_influence_positive_or_negative'])
        except LocationNotFound:
            pass

        try:
            positive_image_of_china, _ = get_stat_data('positive_image_of_china',
                                                    geo, session, table_fields=[
                    'positive_image_of_china'])
        except LocationNotFound:
            pass

        try:
            occupation_of_respondent, _ = get_stat_data('occupation_of_respondent',
                                                        geo, session, table_fields=[
                    'occupation_of_respondent'])
        except LocationNotFound:
            pass

        try:
            employer_of_respondent, _ = get_stat_data('employer_of_respondent', geo,
                                                    session, table_fields=[
                    'employer_of_respondent'])
        except LocationNotFound:
            pass

        try:
            how_often_use_a_mobile_phone, _ = get_stat_data(
                'how_often_use_a_mobile_phone', geo, session,
                table_fields=['how_often_use_a_mobile_phone'])
        except LocationNotFound:
            pass

        try:
            how_often_use_the_internet, _ = get_stat_data(
                'how_often_use_the_internet', geo, session,
                table_fields=['how_often_use_the_internet'])
        except LocationNotFound:
            pass

        try:
            freeness_and_fairness_of_last_national_election, _ = get_stat_data(
                'freeness_and_fairness_of_last_national_election', geo, session,
                table_fields=[
                    'freeness_and_fairness_of_last_national_election'])
        except LocationNotFound:
            pass

        try:
            extent_of_democracy, _ = get_stat_data('extent_of_democracy', geo,
                                                session, table_fields=[
                    'extent_of_democracy'])
        except LocationNotFound:
            pass

        try:
            elections_ensure_voters_views_are_reflected, _ = get_stat_data(
                'elections_ensure_voters_views_are_reflected', geo, session,
                table_fields=['elections_ensure_voters_views_are_reflected'])
        except LocationNotFound:
            pass

        try:
            corruption_judges_and_magistrates, _ = get_stat_data(
                'corruption_judges_and_magistrates', geo, session,
                table_fields=['corruption_judges_and_magistrates'])
        except LocationNotFound:
            pass

        try:
            election_enable_voters_remove_leaders_from_office, _ = get_stat_data(
                'election_enable_voters_remove_leaders_from_office', geo,
                session, table_fields=[
                    'election_enable_voters_remove_leaders_from_office'])
        except LocationNotFound:
            pass

        try:
            corruption_tax_officials, _ = get_stat_data('corruption_tax_officials',
                                                        geo, session, table_fields=[
                    'corruption_tax_officials'])
        except LocationNotFound:
            pass

        try:
            corruption_police, _ = get_stat_data('corruption_police', geo, session,
                                                table_fields=['corruption_police'])
        except LocationNotFound:
            pass

        try:
            corruption_local_government_councilors, _ = get_stat_data(
                'corruption_local_government_councilors', geo, session,
                table_fields=['corruption_local_government_councilors'])
        except LocationNotFound:
            pass

        try:
            corruption_government_officials, _ = get_stat_data(
                'corruption_government_officials', geo, session,
                table_fields=['corruption_government_officials'])
        except LocationNotFound:
            pass

        try:
            corruption_members_of_parliament, _ = get_stat_data(
                'corruption_members_of_parliament', geo, session,
                table_fields=['corruption_members_of_parliament'])
        except LocationNotFound:
            pass

        try:
            corruption_office_of_the_presidency, _ = get_stat_data(
                'corruption_office_of_the_presidency', geo, session,
                table_fields=['corruption_office_of_the_presidency'])
        except LocationNotFound:
            pass

        try:
            satisfaction_with_democracy, _ = get_stat_data(
                'satisfaction_with_democracy', geo, session,
                table_fields=['satisfaction_with_democracy'])
        except LocationNotFound:
            pass

        try:
            corruption_religious_leaders, _ = get_stat_data(
                'corruption_religious_leaders', geo, session,
                table_fields=['corruption_religious_leaders'])
        except LocationNotFound:
            pass

        try:
            corruption_traditional_leaders, _ = get_stat_data(
                'corruption_traditional_leaders', geo, session,
                table_fields=['corruption_traditional_leaders'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_public_school_services, _ = get_stat_data(
                'difficulty_to_obtain_public_school_services', geo, session,
                table_fields=['difficulty_to_obtain_public_school_services'])
        except LocationNotFound:
            pass

        try:
            voting_in_the_most_recent_national_election, _ = get_stat_data(
                'voting_in_the_most_recent_national_election', geo, session,
                table_fields=['voting_in_the_most_recent_national_election'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_medical_treatment, _ = get_stat_data(
                'difficulty_to_obtain_medical_treatment', geo, session,
                table_fields=['difficulty_to_obtain_medical_treatment'])
        except LocationNotFound:
            pass

        try:
            pay_bribe_for_school_services, _ = get_stat_data(
                'pay_bribe_for_school_services', geo, session,
                table_fields=['pay_bribe_for_school_services'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_identity_document, _ = get_stat_data(
                'difficulty_to_obtain_identity_document', geo, session,
                table_fields=['difficulty_to_obtain_identity_document'])
        except LocationNotFound:
            pass

        try:
            bribe_for_treatment_at_public_health, _ = get_stat_data(
                'bribe_for_treatment_at_public_health', geo,
                session, table_fields=[
                    'bribe_for_treatment_at_public_health'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_household_services, _ = get_stat_data(
                'difficulty_to_obtain_household_services', geo, session,
                table_fields=['difficulty_to_obtain_household_services'])
        except LocationNotFound:
            pass

        try:
            pay_bribe_for_document_or_permit, _ = get_stat_data(
                'pay_bribe_for_document_or_permit', geo, session,
                table_fields=['pay_bribe_for_document_or_permit'])
        except LocationNotFound:
            pass

        try:
            pay_bribe_for_household_services, _ = get_stat_data(
                'pay_bribe_for_household_services', geo, session,
                table_fields=['pay_bribe_for_household_services'])
        except LocationNotFound:
            pass

        is_missing = own_radio.get('is_missing') and own_motor_vehicle.get(
            'is_missing') and own_television.get(
            'is_missing') and own_mobile_phone.get(
            'is_missing') and election_work_for_a_candidate_or_party.get(
            'is_missing') and election_attend_a_campaign_rally.get(
            'is_missing') and last_national_election_attend_a_campaign_meeting.get(
            'is_missing') and  courts_make_binding_decisions.get(
            'is_missing') and vote_for_which_party.get(
            'is_missing') and level_of_corruption.get(
            'is_missing') and chinas_assistance_meets_countrys_needs.get(
            'is_missing') and employment_status.get(
            'is_missing') and main_reason_for_not_reporting_corruption.get(
            'is_missing') and education_of_respondent.get(
            'is_missing') and people_must_pay_taxes.get(
            'is_missing') and negative_image_of_china.get(
            'is_missing') and most_effective_way_to_combat_corruption.get(
            'is_missing') and people_can_fight_corruption.get(
            'is_missing') and people_must_obey_the_law.get(
            'is_missing') and chinas_influence_on_economy.get(
            'is_missing') and chinas_influence_positive_or_negative.get(
            'is_missing') and positive_image_of_china.get(
            'is_missing') and occupation_of_respondent.get(
            'is_missing') and employer_of_respondent.get(
            'is_missing') and how_often_use_a_mobile_phone.get(
            'is_missing') and how_often_use_the_internet.get(
            'is_missing') and freeness_and_fairness_of_last_national_election.get(
            'is_missing') and extent_of_democracy.get(
            'is_missing') and elections_ensure_voters_views_are_reflected.get(
            'is_missing') and corruption_judges_and_magistrates.get(
            'is_missing') and election_enable_voters_remove_leaders_from_office.get(
            'is_missing') and corruption_tax_officials.get(
            'is_missing') and corruption_police.get(
            'is_missing') and corruption_local_government_councilors.get(
            'is_missing') and corruption_government_officials.get(
            'is_missing') and corruption_members_of_parliament.get(
            'is_missing') and corruption_office_of_the_presidency.get(
            'is_missing') and satisfaction_with_democracy.get(
            'is_missing') and corruption_religious_leaders.get(
            'is_missing') and corruption_traditional_leaders.get(
            'is_missing') and difficulty_to_obtain_public_school_services.get(
            'is_missing') and voting_in_the_most_recent_national_election.get(
            'is_missing') and difficulty_to_obtain_medical_treatment.get(
            'is_missing') and pay_bribe_for_school_services.get(
            'is_missing') and difficulty_to_obtain_identity_document.get(
            'is_missing') and bribe_for_treatment_at_public_health.get(
            'is_missing') and difficulty_to_obtain_household_services.get(
            'is_missing') and pay_bribe_for_document_or_permit.get(
            'is_missing') and pay_bribe_for_household_services.get('is_missing')

        if not is_missing:

            free_and_fair_key_order = ["Don't know", "Not free and fair",
                                    "Free and fair, with major problems",
                                    "Free and fair, but with minor problems",
                                    "Completely free and fair",
                                    "Do not understand the question",
                                    "metadata"]

            elections_key_order = ["Don't know", "Not at all well", "Not very well",
                                "Well", "Very well", "metadata"]

            chinas_influence_key_order = ["Don't know / Haven't heard enough",
                                        "Very negative", "Somewhat negative",
                                        "Neither positive nor negative",
                                        "Somewhat positive", "Very positive",
                                        "metadata"]

            china_influence_on_economy_key_order = ["Don't know / Haven't heard enough",
                                                    "None", "Some", "A little", "A lot",
                                                    "metadata"]

            mobile_phone_usage_key_order = ["Don't know", "Never",
                                            "A few times a month", "A few times a week",
                                            "Less than once a month", "Everyday",
                                            "metadata"]

            internet_usage_key_order = mobile_phone_usage_key_order + ["Missing"]
            corruption_amongst_key_order = ["Don't know/ Haven't heard enough", "None",
                                            "Some of them", "Most of them",
                                            "All of them", "metadata"]
            corruption_level_key_order = ["Don't know", "Decreased a lot",
                                        "Decreased somewhat", "Stayed the same",
                                        "Increased somewhat", "Increased a lot",
                                        "metadata"]
            pay_bribes_key_order = ["Don't know", "No contact", "Never",
                                    "Once or Twice", "A Few times", "Often", "metadata"]
            difficulty_key_order = ["Don't Know", "No contact", "Very Easy", "Easy",
                                    "Difficult", "Very Difficult", "metadata"]

            corruption_government_officials = sort_keys(
                corruption_amongst_key_order + ["Missing"],
                corruption_government_officials)
            corruption_judges_and_magistrates = sort_keys(corruption_amongst_key_order,
                                                        corruption_judges_and_magistrates)
            corruption_local_government_councilors = sort_keys(
                corruption_amongst_key_order, corruption_local_government_councilors)
            corruption_members_of_parliament = sort_keys(corruption_amongst_key_order,
                                                        corruption_members_of_parliament)
            corruption_office_of_the_presidency = sort_keys(
                corruption_amongst_key_order, corruption_office_of_the_presidency)
            corruption_police = sort_keys(corruption_amongst_key_order,
                                        corruption_police)
            corruption_religious_leaders = sort_keys(corruption_amongst_key_order,
                                                    corruption_religious_leaders)
            corruption_traditional_leaders = sort_keys(corruption_amongst_key_order,
                                                    corruption_traditional_leaders)
            corruption_tax_officials = sort_keys(corruption_amongst_key_order,
                                                corruption_tax_officials)
            level_of_corruption = sort_keys(corruption_level_key_order,
                                            level_of_corruption)
            pay_bribe_for_document_or_permit = sort_keys(pay_bribes_key_order,
                                                        pay_bribe_for_document_or_permit)
            pay_bribe_for_household_services = sort_keys(pay_bribes_key_order,
                                                        pay_bribe_for_household_services)
            pay_bribe_for_school_services = sort_keys(pay_bribes_key_order,
                                                    pay_bribe_for_school_services)
            bribe_for_treatment_at_public_health = sort_keys(
                pay_bribes_key_order,
                bribe_for_treatment_at_public_health)

            freeness_and_fairness_of_last_national_election = sort_keys(
                free_and_fair_key_order,
                freeness_and_fairness_of_last_national_election)
            elections_ensure_voters_views_are_reflected = sort_keys(elections_key_order,
                                                                    elections_ensure_voters_views_are_reflected)
            election_enable_voters_remove_leaders_from_office = sort_keys(
                elections_key_order,
                election_enable_voters_remove_leaders_from_office)
            chinas_influence_positive_or_negative = sort_keys(
                chinas_influence_key_order, chinas_influence_positive_or_negative)
            chinas_influence_on_economy = sort_keys(
                china_influence_on_economy_key_order, chinas_influence_on_economy)
            how_often_use_a_mobile_phone = sort_keys(mobile_phone_usage_key_order,
                                                    how_often_use_a_mobile_phone)
            how_often_use_the_internet = sort_keys(internet_usage_key_order,
                                                how_often_use_the_internet)

            difficulty_to_obtain_household_services = sort_keys(difficulty_key_order,
                                                                difficulty_to_obtain_household_services)
            difficulty_to_obtain_identity_document = sort_keys(difficulty_key_order,
                                                            difficulty_to_obtain_identity_document)
            difficulty_to_obtain_medical_treatment = sort_keys(difficulty_key_order,
                                                            difficulty_to_obtain_medical_treatment)
            difficulty_to_obtain_public_school_services = sort_keys(
                difficulty_key_order, difficulty_to_obtain_public_school_services)
            people_must_obey_the_law = sort_keys(
                ["Don't Know", "Strongly Disagree", "Disagree",
                "Neither Agree Nor Disagree", "Agree", "Strongly Agree", "metadata"],
                people_must_obey_the_law)
            extent_of_democracy = sort_keys(
                ["Don't know", "Not a democracy", "A democracy, with major problems",
                "A democracy, but with minor problems",
                "Do not understand question / democracy", "A full democracy",
                "metadata"], extent_of_democracy)
            satisfaction_with_democracy = sort_keys(
                ["Do not know", "The country is not a democracy",
                "Not at all satisfied", "Not very satisfied", "Fairly satisfied",
                "Very satisfied", "metadata"], satisfaction_with_democracy)

    final_data = {
        'is_missing': is_missing,
        'own_radio': own_radio,
        'own_motor_vehicle': own_motor_vehicle,
        'own_television': own_television,
        'own_mobile_phone': own_mobile_phone,
        'election_work_for_a_candidate_or_party': election_work_for_a_candidate_or_party,
        'election_attend_a_campaign_rally': election_attend_a_campaign_rally,
        'last_national_election_attend_a_campaign_meeting': last_national_election_attend_a_campaign_meeting,
        'courts_make_binding_decisions': courts_make_binding_decisions,
        'vote_for_which_party': vote_for_which_party,
        'level_of_corruption': level_of_corruption,
        'chinas_assistance_meets_countrys_needs': chinas_assistance_meets_countrys_needs,
        'employment_status': employment_status,
        'main_reason_for_not_reporting_corruption': main_reason_for_not_reporting_corruption,
        'education_of_respondent': education_of_respondent,
        'people_must_pay_taxes': people_must_pay_taxes,
        'negative_image_of_china': negative_image_of_china,
        'most_effective_way_to_combat_corruption': most_effective_way_to_combat_corruption,
        'people_can_fight_corruption': people_can_fight_corruption,
        'people_must_obey_the_law': people_must_obey_the_law,
        'chinas_influence_on_economy': chinas_influence_on_economy,
        'chinas_influence_positive_or_negative': chinas_influence_positive_or_negative,
        'positive_image_of_china': positive_image_of_china,
        'occupation_of_respondent': occupation_of_respondent,
        'employer_of_respondent': employer_of_respondent,
        'how_often_use_a_mobile_phone': how_often_use_a_mobile_phone,
        'how_often_use_the_internet': how_often_use_the_internet,
        'freeness_and_fairness_of_last_national_election': freeness_and_fairness_of_last_national_election,
        'extent_of_democracy': extent_of_democracy,
        'elections_ensure_voters_views_are_reflected': elections_ensure_voters_views_are_reflected,
        'corruption_judges_and_magistrates': corruption_judges_and_magistrates,
        'election_enable_voters_remove_leaders_from_office': election_enable_voters_remove_leaders_from_office,
        'corruption_tax_officials': corruption_tax_officials,
        'corruption_police': corruption_police,
        'corruption_local_government_councilors': corruption_local_government_councilors,
        'corruption_government_officials': corruption_government_officials,
        'corruption_members_of_parliament': corruption_members_of_parliament,
        'corruption_office_of_the_presidency': corruption_office_of_the_presidency,
        'satisfaction_with_democracy': satisfaction_with_democracy,
        'corruption_religious_leaders': corruption_religious_leaders,
        'corruption_traditional_leaders': corruption_traditional_leaders,
        'difficulty_to_obtain_public_school_services': difficulty_to_obtain_public_school_services,
        'voting_in_the_most_recent_national_election': voting_in_the_most_recent_national_election,
        'difficulty_to_obtain_medical_treatment': difficulty_to_obtain_medical_treatment,
        'pay_bribe_for_school_services': pay_bribe_for_school_services,
        'difficulty_to_obtain_identity_document': difficulty_to_obtain_identity_document,
        'bribe_for_treatment_at_public_health': bribe_for_treatment_at_public_health,
        'difficulty_to_obtain_household_services': difficulty_to_obtain_household_services,
        'pay_bribe_for_document_or_permit': pay_bribe_for_document_or_permit,
        'pay_bribe_for_household_services': pay_bribe_for_household_services,
    }

    return final_data
Ejemplo n.º 25
0
def get_afrobarometer_profile(geo, session):
    with dataset_context(year='2016'):
        own_radio = LOCATIONNOTFOUND
        own_motor_vehicle = LOCATIONNOTFOUND
        own_television = LOCATIONNOTFOUND
        own_mobile_phone = LOCATIONNOTFOUND
        election_work_for_candidate_or_party = LOCATIONNOTFOUND
        election_attend_campaign_rally = LOCATIONNOTFOUND
        election_attend_campaign_meeting = LOCATIONNOTFOUND
        courts_make_binding_decisions = LOCATIONNOTFOUND
        vote_for_which_party = LOCATIONNOTFOUND
        level_of_corruption = LOCATIONNOTFOUND
        chinas_assistance_meets_country_needs = LOCATIONNOTFOUND
        employment_status = LOCATIONNOTFOUND
        main_reason_for_not_reporting_corruption = LOCATIONNOTFOUND
        education_of_respondent = LOCATIONNOTFOUND
        people_must_pay_taxes = LOCATIONNOTFOUND
        negative_image_of_china = LOCATIONNOTFOUND
        most_effective_way_to_combat_corruption = LOCATIONNOTFOUND
        people_can_fight_corruption = LOCATIONNOTFOUND
        people_must_obey_the_law = LOCATIONNOTFOUND
        chinas_influence_on_economy = LOCATIONNOTFOUND
        chinas_influence_positive_or_negative = LOCATIONNOTFOUND
        positive_image_of_china = LOCATIONNOTFOUND
        occupation_of_respondent = LOCATIONNOTFOUND
        employer_of_respondent = LOCATIONNOTFOUND
        how_often_use_a_mobile_phone = LOCATIONNOTFOUND
        how_often_use_the_internet = LOCATIONNOTFOUND
        freeness_and_fairness_last_elections = LOCATIONNOTFOUND
        extent_of_democracy = LOCATIONNOTFOUND
        elections_ensure_voters_views_are_reflected = LOCATIONNOTFOUND
        corruption_judges_and_magistrates = LOCATIONNOTFOUND
        elections_to_remove_leaders_from_office = LOCATIONNOTFOUND
        corruption_tax_officials = LOCATIONNOTFOUND
        corruption_police = LOCATIONNOTFOUND
        corruption_local_gov_councilors = LOCATIONNOTFOUND
        corruption_gov_officials = LOCATIONNOTFOUND
        corruption_mps = LOCATIONNOTFOUND
        corruption_president_office = LOCATIONNOTFOUND
        satisfaction_with_democracy = LOCATIONNOTFOUND
        corruption_business_executives = LOCATIONNOTFOUND
        corruption_religious_leaders = LOCATIONNOTFOUND
        corruption_traditional_leaders = LOCATIONNOTFOUND
        difficulty_to_obtain_public_school_services = LOCATIONNOTFOUND
        voting_recent_national_election = LOCATIONNOTFOUND
        difficulty_to_obtain_medical_treatment = LOCATIONNOTFOUND
        pay_bribe_for_school_services = LOCATIONNOTFOUND
        difficulty_to_obtain_identity_document = LOCATIONNOTFOUND
        pay_bribe_treatment_public_health = LOCATIONNOTFOUND
        difficulty_to_obtain_household_services = LOCATIONNOTFOUND
        pay_bribe_for_document_or_permit = LOCATIONNOTFOUND
        pay_bribe_for_household_services = LOCATIONNOTFOUND

        try:
            own_radio, _ = get_stat_data('own_radio', geo, session,
                                         table_fields=['own_radio'])
        except LocationNotFound:
            pass

        try:
            own_motor_vehicle, _ = get_stat_data('own_motor_vehicle', geo, session,
                                                 table_fields=['own_motor_vehicle'])
        except LocationNotFound:
            pass

        try:
            own_television, _ = get_stat_data('own_television', geo, session,
                                              table_fields=['own_television'])
        except LocationNotFound:
            pass

        try:
            own_mobile_phone, _ = get_stat_data('own_mobile_phone', geo, session,
                                                table_fields=['own_mobile_phone'])
        except LocationNotFound:
            pass

        try:
            election_work_for_candidate_or_party, _ = get_stat_data(
                'election_work_for_candidate_or_party', geo, session,
                table_fields=['election_work_for_candidate_or_party'])
        except LocationNotFound:
            pass

        try:
            election_attend_campaign_rally, _ = get_stat_data(
                'election_attend_campaign_rally', geo, session,
                table_fields=['election_attend_campaign_rally'])
        except LocationNotFound:
            pass

        try:
            election_attend_campaign_meeting, _ = get_stat_data(
                'election_attend_campaign_meeting', geo, session,
                table_fields=['election_attend_campaign_meeting'])
        except LocationNotFound:
            pass

        try:
            courts_make_binding_decisions, _ = get_stat_data(
                'courts_make_binding_decisions', geo, session,
                table_fields=['courts_make_binding_decisions'])
        except LocationNotFound:
            pass

        try:
            vote_for_which_party, _ = get_stat_data('vote_for_which_party', geo,
                                                    session, table_fields=[
                    'vote_for_which_party'])
        except LocationNotFound:
            pass

        try:
            level_of_corruption, _ = get_stat_data('level_of_corruption', geo,
                                                   session, table_fields=[
                    'level_of_corruption'])
        except LocationNotFound:
            pass

        try:
            chinas_assistance_meets_country_needs, _ = get_stat_data(
                'chinas_assistance_meets_country_needs', geo, session,
                table_fields=['chinas_assistance_meets_country_needs'])
        except LocationNotFound:
            pass

        try:
            employment_status, _ = get_stat_data('employment_status', geo, session,
                                                 table_fields=['employment_status'])
        except LocationNotFound:
            pass

        try:
            main_reason_for_not_reporting_corruption, _ = get_stat_data(
                'main_reason_for_not_reporting_corruption', geo, session,
                table_fields=['main_reason_for_not_reporting_corruption'])
        except LocationNotFound:
            pass

        try:
            education_of_respondent, _ = get_stat_data('education_of_respondent',
                                                       geo, session, table_fields=[
                    'education_of_respondent'])
        except LocationNotFound:
            pass

        try:
            people_must_pay_taxes, _ = get_stat_data('people_must_pay_taxes', geo,
                                                     session, table_fields=[
                    'people_must_pay_taxes'])
        except LocationNotFound:
            pass

        try:
            negative_image_of_china, _ = get_stat_data('negative_image_of_china',
                                                       geo, session, table_fields=[
                    'negative_image_of_china'])
        except LocationNotFound:
            pass

        try:
            most_effective_way_to_combat_corruption, _ = get_stat_data(
                'most_effective_way_to_combat_corruption', geo, session,
                table_fields=['most_effective_way_to_combat_corruption'])
        except LocationNotFound:
            pass

        try:
            people_can_fight_corruption, _ = get_stat_data(
                'people_can_fight_corruption', geo, session,
                table_fields=['people_can_fight_corruption'])
        except LocationNotFound:
            pass

        try:
            people_must_obey_the_law, _ = get_stat_data('people_must_obey_the_law',
                                                        geo, session, table_fields=[
                    'people_must_obey_the_law'])
        except LocationNotFound:
            pass

        try:
            chinas_influence_on_economy, _ = get_stat_data(
                'chinas_influence_on_economy', geo, session,
                table_fields=['chinas_influence_on_economy'])
        except LocationNotFound:
            pass

        try:
            chinas_influence_positive_or_negative, _ = get_stat_data(
                'chinas_influence_positive_or_negative', geo, session,
                table_fields=['chinas_influence_positive_or_negative'])
        except LocationNotFound:
            pass

        try:
            positive_image_of_china, _ = get_stat_data('positive_image_of_china',
                                                       geo, session, table_fields=[
                    'positive_image_of_china'])
        except LocationNotFound:
            pass

        try:
            occupation_of_respondent, _ = get_stat_data('occupation_of_respondent',
                                                        geo, session, table_fields=[
                    'occupation_of_respondent'])
        except LocationNotFound:
            pass

        try:
            employer_of_respondent, _ = get_stat_data('employer_of_respondent', geo,
                                                      session, table_fields=[
                    'employer_of_respondent'])
        except LocationNotFound:
            pass

        try:
            how_often_use_a_mobile_phone, _ = get_stat_data(
                'how_often_use_a_mobile_phone', geo, session,
                table_fields=['how_often_use_a_mobile_phone'])
        except LocationNotFound:
            pass

        try:
            how_often_use_the_internet, _ = get_stat_data(
                'how_often_use_the_internet', geo, session,
                table_fields=['how_often_use_the_internet'])
        except LocationNotFound:
            pass

        try:
            freeness_and_fairness_last_elections, _ = get_stat_data(
                'freeness_and_fairness_last_elections', geo, session,
                table_fields=['freeness_and_fairness_last_elections'])
        except LocationNotFound:
            pass

        try:
            extent_of_democracy, _ = get_stat_data('extent_of_democracy', geo,
                                                   session, table_fields=[
                    'extent_of_democracy'])
        except LocationNotFound:
            pass

        try:
            elections_ensure_voters_views_are_reflected, _ = get_stat_data(
                'elections_ensure_voters_views_are_reflected', geo, session,
                table_fields=['elections_ensure_voters_views_are_reflected'])
        except LocationNotFound:
            pass

        try:
            corruption_judges_and_magistrates, _ = get_stat_data(
                'corruption_judges_and_magistrates', geo, session,
                table_fields=['corruption_judges_and_magistrates'])
        except LocationNotFound:
            pass

        try:
            elections_to_remove_leaders_from_office, _ = get_stat_data(
                'elections_to_remove_leaders_from_office', geo,
                session, table_fields=[
                    'elections_to_remove_leaders_from_office'])
        except LocationNotFound:
            pass

        try:
            corruption_tax_officials, _ = get_stat_data('corruption_tax_officials',
                                                        geo, session, table_fields=[
                    'corruption_tax_officials'])
        except LocationNotFound:
            pass

        try:
            corruption_police, _ = get_stat_data('corruption_police', geo, session,
                                                 table_fields=['corruption_police'])
        except LocationNotFound:
            pass

        try:
            corruption_local_gov_councilors, _ = get_stat_data(
                'corruption_local_gov_councilors', geo, session,
                table_fields=['corruption_local_gov_councilors'])
        except LocationNotFound:
            pass

        try:
            corruption_gov_officials, _ = get_stat_data('corruption_gov_officials',
                                                        geo, session, table_fields=[
                    'corruption_gov_officials'])
        except LocationNotFound:
            pass

        try:
            corruption_mps, _ = get_stat_data('corruption_mps', geo, session,
                                              table_fields=['corruption_mps'])
        except LocationNotFound:
            pass

        try:
            corruption_president_office, _ = get_stat_data(
                'corruption_president_office', geo, session,
                table_fields=['corruption_president_office'])
        except LocationNotFound:
            pass

        try:
            satisfaction_with_democracy, _ = get_stat_data(
                'satisfaction_with_democracy', geo, session,
                table_fields=['satisfaction_with_democracy'])
        except LocationNotFound:
            pass

        try:
            corruption_business_executives, _ = get_stat_data(
                'corruption_business_executives', geo, session,
                table_fields=['corruption_business_executives'])
        except LocationNotFound:
            pass

        try:
            corruption_religious_leaders, _ = get_stat_data(
                'corruption_religious_leaders', geo, session,
                table_fields=['corruption_religious_leaders'])
        except LocationNotFound:
            pass

        try:
            corruption_traditional_leaders, _ = get_stat_data(
                'corruption_traditional_leaders', geo, session,
                table_fields=['corruption_traditional_leaders'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_public_school_services, _ = get_stat_data(
                'difficulty_to_obtain_public_school_services', geo, session,
                table_fields=['difficulty_to_obtain_public_school_services'])
        except LocationNotFound:
            pass

        try:
            voting_recent_national_election, _ = get_stat_data(
                'voting_recent_national_election', geo, session,
                table_fields=['voting_recent_national_election'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_medical_treatment, _ = get_stat_data(
                'difficulty_to_obtain_medical_treatment', geo, session,
                table_fields=['difficulty_to_obtain_medical_treatment'])
        except LocationNotFound:
            pass

        try:
            pay_bribe_for_school_services, _ = get_stat_data(
                'pay_bribe_for_school_services', geo, session,
                table_fields=['pay_bribe_for_school_services'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_identity_document, _ = get_stat_data(
                'difficulty_to_obtain_identity_document', geo, session,
                table_fields=['difficulty_to_obtain_identity_document'])
        except LocationNotFound:
            pass

        try:
            pay_bribe_treatment_public_health, _ = get_stat_data(
                'pay_bribe_treatment_public_health', geo, session,
                table_fields=['pay_bribe_treatment_public_health'])
        except LocationNotFound:
            pass

        try:
            difficulty_to_obtain_household_services, _ = get_stat_data(
                'difficulty_to_obtain_household_services', geo, session,
                table_fields=['difficulty_to_obtain_household_services'])
        except LocationNotFound:
            pass

        try:
            pay_bribe_for_document_or_permit, _ = get_stat_data(
                'pay_bribe_for_document_or_permit', geo, session,
                table_fields=['pay_bribe_for_document_or_permit'])
        except LocationNotFound:
            pass

        try:
            pay_bribe_for_household_services, _ = get_stat_data(
                'pay_bribe_for_household_services', geo, session,
                table_fields=['pay_bribe_for_household_services'])
        except LocationNotFound:
            pass

        is_missing = own_radio.get('is_missing') and own_motor_vehicle.get(
            'is_missing') and own_television.get(
            'is_missing') and own_mobile_phone.get(
            'is_missing') and election_work_for_candidate_or_party.get(
            'is_missing') and election_attend_campaign_rally.get(
            'is_missing') and election_attend_campaign_meeting.get(
            'is_missing') and courts_make_binding_decisions.get(
            'is_missing') and vote_for_which_party.get(
            'is_missing') and level_of_corruption.get(
            'is_missing') and chinas_assistance_meets_country_needs.get(
            'is_missing') and employment_status.get(
            'is_missing') and main_reason_for_not_reporting_corruption.get(
            'is_missing') and education_of_respondent.get(
            'is_missing') and people_must_pay_taxes.get(
            'is_missing') and negative_image_of_china.get(
            'is_missing') and most_effective_way_to_combat_corruption.get(
            'is_missing') and people_can_fight_corruption.get(
            'is_missing') and people_must_obey_the_law.get(
            'is_missing') and chinas_influence_on_economy.get(
            'is_missing') and chinas_influence_positive_or_negative.get(
            'is_missing') and positive_image_of_china.get(
            'is_missing') and occupation_of_respondent.get(
            'is_missing') and employer_of_respondent.get(
            'is_missing') and how_often_use_a_mobile_phone.get(
            'is_missing') and how_often_use_the_internet.get(
            'is_missing') and freeness_and_fairness_last_elections.get(
            'is_missing') and extent_of_democracy.get(
            'is_missing') and elections_ensure_voters_views_are_reflected.get(
            'is_missing') and corruption_judges_and_magistrates.get(
            'is_missing') and elections_to_remove_leaders_from_office.get(
            'is_missing') and corruption_tax_officials.get(
            'is_missing') and corruption_police.get(
            'is_missing') and corruption_local_gov_councilors.get(
            'is_missing') and corruption_gov_officials.get(
            'is_missing') and corruption_mps.get(
            'is_missing') and corruption_president_office.get(
            'is_missing') and satisfaction_with_democracy.get(
            'is_missing') and corruption_business_executives.get(
            'is_missing') and corruption_religious_leaders.get(
            'is_missing') and corruption_traditional_leaders.get(
            'is_missing') and difficulty_to_obtain_public_school_services.get(
            'is_missing') and voting_recent_national_election.get(
            'is_missing') and difficulty_to_obtain_medical_treatment.get(
            'is_missing') and pay_bribe_for_school_services.get(
            'is_missing') and difficulty_to_obtain_identity_document.get(
            'is_missing') and pay_bribe_treatment_public_health.get(
            'is_missing') and difficulty_to_obtain_household_services.get(
            'is_missing') and pay_bribe_for_document_or_permit.get(
            'is_missing') and pay_bribe_for_household_services.get('is_missing')

    if not is_missing:
        difficulty_to_obtain_household_services = sort_keys(difficulty_key_order, difficulty_to_obtain_household_services)
        difficulty_to_obtain_identity_document = sort_keys(difficulty_key_order, difficulty_to_obtain_identity_document)
        difficulty_to_obtain_medical_treatment = sort_keys(difficulty_key_order, difficulty_to_obtain_medical_treatment)
        difficulty_to_obtain_public_school_services = sort_keys(difficulty_key_order, difficulty_to_obtain_public_school_services)
        election_attend_campaign_meeting = sort_keys(elections_key_order, election_attend_campaign_meeting)
        election_attend_campaign_rally = sort_keys(elections_key_order, election_attend_campaign_rally)
        election_work_for_candidate_or_party = sort_keys(elections_key_order, election_work_for_candidate_or_party)
        elections_to_remove_leaders_from_office = sort_keys(elections_key_order, elections_to_remove_leaders_from_office)
        freeness_and_fairness_last_elections = sort_keys(free_and_fair_key_order, freeness_and_fairness_last_elections)
        chinas_influence_positive_or_negative = sort_keys(chinas_influence_key_order, chinas_influence_positive_or_negative)
        chinas_influence_on_economy = sort_keys(china_influence_on_economy_key_order, chinas_influence_on_economy)
        pay_bribe_for_document_or_permit = sort_keys(pay_bribes_key_order, pay_bribe_for_document_or_permit)
        pay_bribe_for_household_services = sort_keys(pay_bribes_key_order, pay_bribe_for_household_services)
        pay_bribe_for_school_services = sort_keys(pay_bribes_key_order, pay_bribe_for_school_services)
        pay_bribe_treatment_public_health = sort_keys(pay_bribes_key_order, pay_bribe_treatment_public_health)
        how_often_use_a_mobile_phone = sort_keys(mobile_phone_usage_key_order, how_often_use_a_mobile_phone)
        how_often_use_the_internet = sort_keys(internet_usage_key_order, how_often_use_the_internet)
        education_of_respondent = sort_keys(education_of_respondent_key_order, education_of_respondent)
        employment_status = sort_keys(employment_status_key_order, employment_status)
        people_can_fight_corruption = sort_keys(can_people_fight_corruption_key_order, people_can_fight_corruption)
        courts_make_binding_decisions = sort_keys(courts_make_binding_decisions_key_order, courts_make_binding_decisions)
        people_must_pay_taxes = sort_keys(pay_taxes_key_order, people_must_pay_taxes)
        people_must_obey_the_law = sort_keys(people_must_obey_the_law_key_order, people_must_obey_the_law)

    final_data = {
        'is_missing': is_missing,
        'own_radio': own_radio,
        'own_motor_vehicle': own_motor_vehicle,
        'own_television': own_television,
        'own_mobile_phone': own_mobile_phone,
        'election_work_for_candidate_or_party': election_work_for_candidate_or_party,
        'election_attend_campaign_rally': election_attend_campaign_rally,
        'election_attend_campaign_meeting': election_attend_campaign_meeting,
        'courts_make_binding_decisions': courts_make_binding_decisions,
        'vote_for_which_party': vote_for_which_party,
        'level_of_corruption': level_of_corruption,
        'chinas_assistance_meets_country_needs': chinas_assistance_meets_country_needs,
        'employment_status': employment_status,
        'main_reason_for_not_reporting_corruption': main_reason_for_not_reporting_corruption,
        'education_of_respondent': education_of_respondent,
        'people_must_pay_taxes': people_must_pay_taxes,
        'negative_image_of_china': negative_image_of_china,
        'most_effective_way_to_combat_corruption': most_effective_way_to_combat_corruption,
        'people_can_fight_corruption': people_can_fight_corruption,
        'people_must_obey_the_law': people_must_obey_the_law,
        'chinas_influence_on_economy': chinas_influence_on_economy,
        'chinas_influence_positive_or_negative': chinas_influence_positive_or_negative,
        'positive_image_of_china': positive_image_of_china,
        'occupation_of_respondent': occupation_of_respondent,
        'employer_of_respondent': employer_of_respondent,
        'how_often_use_a_mobile_phone': how_often_use_a_mobile_phone,
        'how_often_use_the_internet': how_often_use_the_internet,
        'freeness_and_fairness_last_elections': freeness_and_fairness_last_elections,
        'extent_of_democracy': extent_of_democracy,
        'elections_ensure_voters_views_are_reflected': elections_ensure_voters_views_are_reflected,
        'corruption_judges_and_magistrates': corruption_judges_and_magistrates,
        'elections_to_remove_leaders_from_office': elections_to_remove_leaders_from_office,
        'corruption_tax_officials': corruption_tax_officials,
        'corruption_police': corruption_police,
        'corruption_local_gov_councilors': corruption_local_gov_councilors,
        'corruption_gov_officials': corruption_gov_officials,
        'corruption_mps': corruption_mps,
        'corruption_president_office': corruption_president_office,
        'satisfaction_with_democracy': satisfaction_with_democracy,
        'corruption_business_executives': corruption_business_executives,
        'corruption_religious_leaders': corruption_religious_leaders,
        'corruption_traditional_leaders': corruption_traditional_leaders,
        'difficulty_to_obtain_public_school_services': difficulty_to_obtain_public_school_services,
        'voting_recent_national_election': voting_recent_national_election,
        'difficulty_to_obtain_medical_treatment': difficulty_to_obtain_medical_treatment,
        'pay_bribe_for_school_services': pay_bribe_for_school_services,
        'difficulty_to_obtain_identity_document': difficulty_to_obtain_identity_document,
        'pay_bribe_treatment_public_health': pay_bribe_treatment_public_health,
        'difficulty_to_obtain_household_services': difficulty_to_obtain_household_services,
        'pay_bribe_for_document_or_permit': pay_bribe_for_document_or_permit,
        'pay_bribe_for_household_services': pay_bribe_for_household_services,

    }
    return final_data
Ejemplo n.º 26
0
def get_education_profile(geo, session, display_profile, comparative=False):
    youth_completed_grade9, _ = get_stat_data(
        ['completed grade9'], geo, session,
        table_universe='Youth aged 16 to 17',
        table_dataset='Census and Community Survey',
        key_order=('Completed', 'Not completed'))

    youth_completed_grade9_by_gender, _ = get_stat_data(
        ['completed grade9', 'gender'], geo, session,
        table_universe='Youth aged 16 to 17',
        table_dataset='Census and Community Survey',
        percent_grouping=['gender'], slices=['Completed'],
        key_order={'gender': GENDER_ORDER})

    youth_education_level, youth_pop_20_to_24 = get_stat_data(
        ['education level'], geo, session,
        table_universe='Youth aged 20 to 24',
        table_dataset='Census and Community Survey',
        key_order=EDUCATION_LEVEL_KEY_ORDER)

    youth_perc_matric = None
    if youth_education_level['Matric/matric equivalent']['numerators']['this']:
        matric_or_equiv = (
                youth_education_level['Matric/matric equivalent']['numerators']['this'] +
                youth_education_level['Any tertiary']['numerators']['this'])
        youth_perc_matric = percent(matric_or_equiv, youth_pop_20_to_24)

    youth_education_attendance, _ = get_stat_data(
        ['attendance'], geo, session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_fields=['attendance', 'age in completed years', 'gender'])

    youth_education_attending_by_age, _ = get_stat_data(
        ['attendance', 'age in completed years'], geo, session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_fields=['attendance', 'age in completed years', 'gender'],
        table_name='youth_education_attendance_age_incompleted_years_gender',
        percent_grouping=['age in completed years'], slices=['Yes'])

    youth_education_attending_by_gender, _ = get_stat_data(
        ['attendance', 'gender'], geo, session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_fields=['attendance', 'age in completed years', 'gender'],
        percent_grouping=['gender'], slices=['Yes'],
        key_order={'gender': GENDER_ORDER})

    final_data = {
        'youth_completed_grade9': youth_completed_grade9,
        'youth_perc_completed_grade9': {
            'name': 'Of youth aged 16-17 have completed grade 9 or higher',
            'values': {'this': youth_completed_grade9['Completed']['values']['this']}
        },
        'youth_completed_grade9_by_gender': youth_completed_grade9_by_gender,
        'youth_education_level': youth_education_level,
        'youth_perc_matric': {
            "name": "Of youth aged 20-24 have completed matric/matric equivalent or higher",
            "values": {"this": youth_perc_matric}
        },
        'youth_perc_attending': {
            "name": "Of youth aged 15-24 attend an educational institution",
            "values": {"this": youth_education_attendance['Yes']['values']['this']}
        },
        'youth_education_attending_by_age': youth_education_attending_by_age,
        'youth_education_attending_by_gender': youth_education_attending_by_gender,
    }

    if display_profile == 'WC':
        with dataset_context(year='2017'):
            youth_average_mean_score_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Average mean score in both language and mathematics',
                percent=False)

            youth_average_language_score_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Average score in language',
                percent=False)

            youth_average_maths_score_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Average score in mathematics',
                percent=False)

            youth_language_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'], geo, session,
                table_universe='Percentage passed in language',
                key_order={'outcome': ['Passed', 'Failed']},
                percent=False, slices=['2017'])

            youth_maths_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'], geo, session,
                table_universe='Percentage passed in mathematics',
                key_order={'outcome': ['Passed', 'Failed']},
                percent=False, slices=['2017'])

        with dataset_context(year='2017'):
            youth_matric_outcome_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Matric pass rate',
                only={'outcome': ['Passed']},
                percent=False)

            youth_matric_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'], geo, session,
                table_universe='Matric pass rate',
                key_order={'outcome': ['Passed', 'Failed']},
                percent=False, slices=['2017'])

            youth_matric_throughput_rate_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Matric passes as a % of grade 8 enrolment',
                only={'outcome': ['Passed']},
                percent=False)

            youth_matric_throughput_latest, _ = get_stat_data(
                ['year', 'outcome'], geo, session,
                table_universe='Matric passes as a % of grade 8 enrolment',
                key_order={'outcome': ['Passed', 'Dropped out or failed']},
                percent=False, slices=['2017'])

            youth_bachelor_passes_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Bachelor passes as a % of grade 8 enrolment',
                only={'outcome': ['Bachelor pass']},
                percent=False)

            youth_bachelor_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'], geo, session,
                table_universe='Bachelor passes as a % of grade 8 enrolment',
                key_order={'outcome': ['Bachelor pass', 'No bachelor pass']},
                percent=False, slices=['2017'])

            youth_student_dropout_rate_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Dropout rates between grade 10 and matric',
                only={'outcome': ['Dropped out']},
                percent=False)

            youth_student_dropout_rate_latest, _ = get_stat_data(
                ['year', 'outcome'], geo, session,
                table_universe='Dropout rates between grade 10 and matric',
                percent=False, slices=['2017'])

        final_data.update({
            'youth_ave_mean_score_latest': {
                "name": "Average mean score in both language and mathematics",
                "values": {"this": youth_average_mean_score_by_year['2017']['values']['this']}
            },
            'youth_ave_mean_score_by_year': youth_average_mean_score_by_year,
            'youth_ave_language_score_latest': {
                "name": "Average score in language",
                "values": {"this": youth_average_language_score_by_year['2017']['values']['this']}
            },
            'youth_ave_language_score_by_year': youth_average_language_score_by_year,
            'youth_ave_maths_score_latest': {
                "name": "Average score in mathematics",
                "values": {"this": youth_average_maths_score_by_year['2017']['values']['this']}
            },
            'youth_ave_maths_score_by_year': youth_average_maths_score_by_year,
            'youth_language_outcome_latest': youth_language_outcome_latest,
            'youth_maths_outcome_latest': youth_maths_outcome_latest,
            'youth_matric_pass_rate_latest': {
                "name": "Of students writing matric passed",
                "values": {"this": youth_matric_outcome_latest['Passed']['values']['this']}
            },
            'youth_matric_outcome_latest': youth_matric_outcome_latest,
            'youth_matric_outcome_by_year': youth_matric_outcome_by_year,
            'youth_matric_throughput_rate_latest': {
                "name": "Of Grade 8 students go on to pass matric",
                "values": {"this": youth_matric_throughput_latest['Passed']['values']['this']}
            },
            'youth_matric_throughput_latest': youth_matric_throughput_latest,
            'youth_matric_throughput_rate_by_year': youth_matric_throughput_rate_by_year,
            'youth_bachelor_passes_latest': {
                "name": "Of Grade 8 students go on to pass matric with a bachelor's pass",
                "values": {"this": youth_bachelor_outcome_latest['Bachelor pass']['values']['this']}
            },
            'youth_bachelor_outcome_latest': youth_bachelor_outcome_latest,
            'youth_bachelor_passes_by_year': youth_bachelor_passes_by_year,
            'youth_student_dropout_rate_latest': {
                "name": "Of students drop out between grade 10 and matric",
                "values": {"this": youth_student_dropout_rate_latest['Dropped out']['values']['this']}
            },
            'youth_student_dropout_rate_by_year': youth_student_dropout_rate_by_year
        })

    return final_data
Ejemplo n.º 27
0
def get_safety_profile(geo, session, display_profile, comparative=False):

    def rate_per_10k_pop(value, population):
        if not value:
            return None
        return value / population * 10000

    final_data = {}

    if display_profile == 'WC':
        youth_pop_table = get_datatable('youth_population')
        youth, pop_total = youth_pop_table.get_stat_data(
            geo, total='total_pop', percent='False')

        with dataset_context(year='2015'):
            victims_by_age_group_per_10k_pop, total_victims_per_10k_pop = get_stat_data(
                ['age group'], geo, session,
                table_universe='Victims of crime per 10,000 population in age group',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)
            accused_by_age_group_per_10k_pop, total_accused_per_10k_pop = get_stat_data(
                ['age group'], geo, session,
                table_universe='Accused of crime per 10,000 population in age group',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

            youth_victims_by_offence_per_10k_youth, _ = get_stat_data(
                ['type of offence'], geo, session,
                table_universe='Youth victims of crime, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015')
            youth_accused_by_offence_per_10k_youth, _ = get_stat_data(
                ['type of offence'], geo, session,
                table_universe='Youth accused of crimes, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015')

            youth_victims_by_pop_group_per_10k, _ = get_stat_data(
                ['population group'], geo, session,
                table_universe='Youth victims of crime, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)
            youth_accused_by_pop_group_per_10k, _ = get_stat_data(
                ['population group'], geo, session,
                table_universe='Youth accused of crimes, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

            youth_victims_by_gender_per_10k, _ = get_stat_data(
                ['gender'], geo, session,
                table_universe='Youth victims of crime, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False,
                key_order=GENDER_ORDER)
            youth_accused_by_gender_per_10k, _ = get_stat_data(
                ['gender'], geo, session,
                table_universe='Youth accused of crimes, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False,
                key_order=GENDER_ORDER)

            youth_victims_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Youth victims of crime',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)
            youth_accused_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Youth accused of crimes',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

            crimes_by_year, _ = get_stat_data(
                ['type of crime', 'year'], geo, session,
                table_universe='Crimes',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

        contact_crimes_by_year = crimes_by_year['Contact crime']
        contact_crimes_by_year['metadata'] = crimes_by_year['metadata']
        property_crimes_by_year = crimes_by_year['Property crime']
        property_crimes_by_year['metadata'] = crimes_by_year['metadata']

        contact_crimes_per_10k_pop = rate_per_10k_pop(contact_crimes_by_year['2014-15']['values']['this'], pop_total)
        property_crimes_per_10k_pop = rate_per_10k_pop(property_crimes_by_year['2014-15']['values']['this'], pop_total)

        final_data = {
            'youth_victims_per_10k_youth': {
                "name": "Youth victims of contact crime per 10,000 youth",
                "values": {"this": victims_by_age_group_per_10k_pop['15-24']['values']['this']}
            },
            'youth_accused_per_10k_youth': {
                "name": "Youth accused of contact crime per 10,000 youth",
                "values": {"this": accused_by_age_group_per_10k_pop['15-24']['values']['this']}
            },
            'victims_by_age_group_per_10k_pop': victims_by_age_group_per_10k_pop,
            'accused_by_age_group_per_10k_pop': accused_by_age_group_per_10k_pop,
            'youth_victims_by_offence_per_10k_youth': youth_victims_by_offence_per_10k_youth,
            'youth_accused_by_offence_per_10k_youth': youth_accused_by_offence_per_10k_youth,
            'youth_victims_by_pop_group_per_10k': youth_victims_by_pop_group_per_10k,
            'youth_accused_by_pop_group_per_10k': youth_accused_by_pop_group_per_10k,
            'youth_victims_by_gender_per_10k': youth_victims_by_gender_per_10k,
            'youth_accused_by_gender_per_10k': youth_accused_by_gender_per_10k,
            'youth_victims_by_year': youth_victims_by_year,
            'youth_accused_by_year': youth_accused_by_year,
            'contact_crimes_per_10k_pop': {
                "name": "Contact crimes per 10,000 population",
                "values": {"this": contact_crimes_per_10k_pop}
            },
            'property_crimes_per_10k_pop': {
                "name": "Property-related crime per 10,000 population",
                "values": {"this": property_crimes_per_10k_pop}
            },
            'contact_crimes_by_year': contact_crimes_by_year,
            'property_crimes_by_year': property_crimes_by_year
        }

    return final_data
Ejemplo n.º 28
0
def get_landsales_profile(geo, session):
    #TODO: landsales profile for 2016  data context
    with dataset_context(year='2018'):
        landsales = {'is_missing': True}
        landsalestransaction = landsaleshectares = LOCATIONNOTFOUND
        landsalesaverageprice = landsalespricetrends = LOCATIONNOTFOUND

        landsalestransaction_tot = landsaleshectares_tot = 0

        try:
            landsalestransaction, landsalestransaction_tot = get_stat_data(
                ['class_distribution'], geo, session,
                table_name='land_traded_per_class_statistic_2018',
                only={'statistic': ['Number of transactions in 12 months']},
                key_order=LAND_CLASS,
                percent=False)
        except Exception as e:
            pass

        try:
            landsaleshectares, landsaleshectares_tot = get_stat_data(
                ['class_distribution'], geo, session,
                table_name='land_traded_per_class_statistic_2018',
                only={'statistic': ['Total hectare traded in 12 months(ha)']},
                key_order=LAND_CLASS,
                percent=False)
        except Exception as e:
            pass

        try:
            landsalesaverageprice, _ = get_stat_data(
                ['class_distribution'], geo, session,
                table_name='land_traded_per_class_statistic_2018',
                only={'statistic': ['Average price per hectare in 12 months(R/ha)']},
                key_order=LAND_CLASS,
                percent=False)
        except Exception as e:
            pass
        try:
            landsalespricetrends, _ = get_stat_data(
                ['class_distribution'], geo, session,
                table_name='land_traded_per_class_statistic_2018',
                only={'statistic': ['Price trend per hectare in 12 months(R/ha)']},
                key_order=LAND_CLASS,
                percent=False)
        except Exception as e:
            pass

        try:
            landsalesaveragetrends, _ = get_stat_data(
                ['class_distribution'], geo, session, exclude_zero=True,
                table_name='land_traded_per_class_statistic_2018',
                only={'statistic': ['Average trend in 12 months(%)']},
                key_order=LAND_CLASS,
                percent=False)
        except Exception as e:
            pass

        landsales['landsalestransaction'] = landsalestransaction
        landsales['landsaleshectares'] = landsaleshectares
        landsales['landsalesaverageprice'] = landsalesaverageprice
        landsales['landsalespricetrends'] = landsalespricetrends
        landsales['landsaleshectares_tot'] = \
            {
                "name": " Total number of sold hectares from Dec 2017 to Nov 2018",
                "values": {"this": int(landsaleshectares_tot)},
            }
        landsales['landsalestransaction_tot'] = \
            {
                "name": "Total number of sales transactions from Dec 2017 to Nov 2018",
                "values": {"this": int(landsalestransaction_tot)},
            }
        landsales['is_missing'] = landsalestransaction.get('is_missing') and \
                                  landsaleshectares.get('is_missing')

    return landsales
Ejemplo n.º 29
0
def get_redistributionandrestitution_profile(geo, session):
    with dataset_context(year='2018'):
        redistributedlandusebreakdown = redistributeprogrammeprojectsbyyear = LOCATIONNOTFOUND
        redistributeprogrammehouseholdsbyyear = landcostrestitution = LOCATIONNOTFOUND
        redistributeprogrammebeneficiariesbyyear = femalepartybenefited = LOCATIONNOTFOUND
        youthpartybenefited = disabledpeoplepartybenefited = LOCATIONNOTFOUND
        redistributedlandinhectares = redistributedlandcostinrands = LOCATIONNOTFOUND
        redistributedlandaveragecostperhectares = householdsrestitution = LOCATIONNOTFOUND
        hectarestransferredperprovincebyyear = hectaresacquiredrestitution = LOCATIONNOTFOUND
        projectsrestitution = beneficiariesrestitution = LOCATIONNOTFOUND
        claimssettledrestitution = disabilitiesrestitution = LOCATIONNOTFOUND
        femaleheadedhouseholdsrestitution = financialcompensationrestitution = LOCATIONNOTFOUND

        femaleheadedhouseholdsrestitution_tot = beneficiariesrestitution_tot = projectsrestitution_tot = 0
        financialcompensationrestitution_tot = claimssettledrestitution_tot = 0
        femalepartybenefited_tot = disabledpeoplepartybenefited_tot = youthpartybenefited_tot = 0
        hectarestransferredperprovincebyyear_tot = hectaresacquiredrestitution_tot = 0
        householdsrestitution_tot = disabilitiesrestitution_tot = 0
        redistributedland_avg_cost = redistributedland_tot_cost = redistributedland_hect_tot = 0
        redistributeprogrammehouseholdsbyyear_tot = redistributeprogrammeprojectsbyyear_tot = 0
        redistributeprogrammebeneficiariesbyyear_tot = 0

        redistributionrestitution = {'is_missing': True}

        try:
            redistributedlandusebreakdown, _ = get_stat_data(
                ['redistributed land use breakdown'], geo, session)
        except Exception:
            pass

        try:
            redistributeprogrammeprojectsbyyear, redistributeprogrammeprojectsbyyear_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'outcome of redistribution programme'],
                only={'outcome of redistribution programme': [
                    'projects transferred']},
                percent=False)
        except Exception:
            pass

        try:
            redistributeprogrammehouseholdsbyyear, redistributeprogrammehouseholdsbyyear_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'outcome of redistribution programme'],
                only={'outcome of redistribution programme': [
                    'benefited households']},
                percent=False)
        except Exception:
            pass

        try:
            redistributeprogrammebeneficiariesbyyear, redistributeprogrammebeneficiariesbyyear_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'outcome of redistribution programme'],
                only={'outcome of redistribution programme': [
                    'benefited beneficiaries']},
                percent=False)
        except Exception:
            pass

        try:
            femalepartybenefited, femalepartybenefited_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'party_benefited'],
                only={'party_benefited': ['female']},
                percent=False)
        except Exception:
            pass

        try:
            youthpartybenefited, youthpartybenefited_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'party_benefited'],
                only={'party_benefited': ['youth']},
                percent=False)
        except Exception:
            pass

        try:
            disabledpeoplepartybenefited, disabledpeoplepartybenefited_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'party_benefited'],
                only={'party_benefited': ['disable people']},
                percent=False)
        except Exception:
            pass

        try:
            hectarestransferredperprovincebyyear, hectarestransferredperprovincebyyear_tot = get_stat_data(
                ['year'], geo, session,
                percent=False)
        except Exception:
            pass

        try:
            hectaresacquiredrestitution, hectaresacquiredrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': ['hectares acquired']},
                percent=False)
        except Exception:
            pass

        try:
            claimssettledrestitution, claimssettledrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': ['claims settled']},
                percent=False)
        except Exception:
            pass
        try:
            disabilitiesrestitution, disabilitiesrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': [
                    'people with disabilities']},
                percent=False)
        except Exception:
            pass
        try:
            beneficiariesrestitution, beneficiariesrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': ['beneficiaries']},
                percent=False)
        except Exception:
            pass

        try:
            householdsrestitution, householdsrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': ['households']},
                percent=False)
        except Exception:
            pass

        try:
            landcostrestitution, _ = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': ['landcost']},
                percent=False)
        except Exception:
            pass

        try:
            financialcompensationrestitution, financialcompensationrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={
                    'restitutionoutcomestatistic': ['financial compensation']},
                percent=False)
        except Exception:
            pass

        try:
            projectsrestitution, projectsrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': ['projects']},
                percent=False)
        except Exception:
            pass

        try:
            femaleheadedhouseholdsrestitution, femaleheadedhouseholdsrestitution_tot = get_stat_data(
                ['year'], geo, session,
                table_fields=['year', 'restitutionoutcomestatistic'],
                only={'restitutionoutcomestatistic': [
                    'female headed households']},
                percent=False)
        except Exception:
            pass

        try:
            redistributedlandinhectares, redistributedland_hect_tot = get_stat_data(
                ['redistributedland'], geo, session, percent=False,
                only={'redistributedland': ['number of hectares']})
        except Exception:
            pass

        try:
            redistributedlandcostinrands, redistributedland_tot_cost = get_stat_data(
                ['redistributedland'], geo, session, percent=False,
                only={'redistributedland': ['cost in rands']})
        except Exception:
            pass

        try:
            redistributedlandaveragecostperhectares, redistributedland_avg_cost = get_stat_data(
                ['redistributedland'], geo, session, percent=False,
                only={'redistributedland': ['average cost per hectares']})
        except Exception:
            pass

    redistributionrestitution[
        'redistributedlandusebreakdown'] = redistributedlandusebreakdown
    redistributionrestitution['hectarestransferredperprovincebyyear_stat'] = \
        {
            "name": "Total land in hectares (ha) redistributed from the year 2009 to 2018",
            "values": {"this": hectarestransferredperprovincebyyear_tot}
            }
    redistributionrestitution['redistributedlandhectares_stat'] = \
        {"name": "Redistributed land in hectares for the year 2017/2018",
         "values": {"this": redistributedland_hect_tot}
         }
    redistributionrestitution['redistributedlandcostinrands_stat'] = \
        {
            "name": "Cost in Rands (ZAR) of Redistributed Land for the year 2017/2018",
            "values": {"this": redistributedland_tot_cost}
            }
    redistributionrestitution[
        'redistributedlandaveragecostperhectares_stat'] = \
        {
            "name": "Average Cost in Rands (ZAR) per Hectares for Redistributed Land in 2017/2018",
            "values": {"this": redistributedland_avg_cost}
            }
    redistributionrestitution[
        'redistributeprogrammeprojectsbyyear'] = redistributeprogrammeprojectsbyyear
    redistributionrestitution['redistributeprogrammeprojectsbyyear_stat'] = \
        {
            "name": "Total projects benefitted through Redistribution Programme from 2009/2010 to 2017/2018",
            "values": {"this": redistributeprogrammeprojectsbyyear_tot}
            }

    redistributionrestitution[
        'redistributeprogrammehouseholdsbyyear'] = redistributeprogrammehouseholdsbyyear
    redistributionrestitution[
        'redistributeprogrammehouseholdsbyyear_stat'] = \
        {
            "name": "Total households benefitted though Redistribution Programme from 2009/2010 to 2017/2018",
            "values": {"this": redistributeprogrammehouseholdsbyyear_tot}
            }

    redistributionrestitution[
        'redistributeprogrammebeneficiariesbyyear'] = redistributeprogrammebeneficiariesbyyear
    redistributionrestitution[
        'redistributeprogrammebeneficiariesbyyear_stat'] = \
        {
            "name": "Total number of beneficiaries of Redistribution Programme from the 2009/2010 to 2017/2018",
            "values": {"this": redistributeprogrammebeneficiariesbyyear_tot}
            }

    redistributionrestitution['femalepartybenefited'] = femalepartybenefited
    redistributionrestitution['femalepartybenefited_stat'] = \
        {
            "name": "Number of female benefited through Redistribution programme from 2009/2010 to 2017/2018",
            "values": {"this": femalepartybenefited_tot}
            }

    redistributionrestitution['youthpartybenefited'] = youthpartybenefited
    redistributionrestitution['youthpartybenefited_stat'] = \
        {
            "name": "Number of youth benefited through Redistribution programme from 2009/2010 to 2017/2018",
            "values": {"this": youthpartybenefited_tot}
            }
    redistributionrestitution[
        'disabledpeoplepartybenefited'] = disabledpeoplepartybenefited
    redistributionrestitution['disabledpeoplepartybenefited_stat'] = \
        {
            "name": "People with disabilities benefited in Redistribution programme from 2009/2010 to 2017/2018",
            "values": {"this": disabledpeoplepartybenefited_tot}
            }

    redistributionrestitution[
        'hectarestransferredperprovincebyyear'] = hectarestransferredperprovincebyyear

    redistributionrestitution[
        'hectaresacquiredrestitution'] = hectaresacquiredrestitution
    redistributionrestitution[
        'claimssettledrestitution'] = claimssettledrestitution
    redistributionrestitution[
        'householdsrestitution'] = householdsrestitution
    redistributionrestitution[
        'femaleheadedhouseholdsrestitution'] = femaleheadedhouseholdsrestitution
    redistributionrestitution[
        'disabilitiesrestitution'] = disabilitiesrestitution
    redistributionrestitution['projectsrestitution'] = projectsrestitution
    redistributionrestitution[
        'beneficiariesrestitution'] = beneficiariesrestitution
    redistributionrestitution['landcostrestitution'] = landcostrestitution
    redistributionrestitution[
        'financialcompensationrestitution'] = financialcompensationrestitution
    redistributionrestitution[
        'hectarestransferredperprovincebyyear_tot'] = hectarestransferredperprovincebyyear_tot
    redistributionrestitution[
        'hectaresacquiredrestitution_tot'] = hectaresacquiredrestitution_tot
    redistributionrestitution[
        'claimssettledrestitution_tot'] = claimssettledrestitution_tot
    redistributionrestitution['landcostrestitution'] = landcostrestitution

    redistributionrestitution['hectaresacquiredrestitution_stat'] = \
        {
            "name": "Total hectares acquired in Restitution programme from 2009/2010 to 2017/2018",
            "values": {"this": hectaresacquiredrestitution_tot}
            }
    redistributionrestitution['householdsrestitution_stat'] = \
        {
            "name": "Total households benefited in Restitution programme from 2009/2010 to 2017/2018",
            "values": {"this": householdsrestitution_tot}
            }
    redistributionrestitution['femaleheadedhouseholdsrestitution_stat'] = \
        {
            "name": "Female headed households benefited in restitution programme from 2009/2010 to 2017/2018",
            "values": {"this": femaleheadedhouseholdsrestitution_tot}
            }
    redistributionrestitution['disabilitiesrestitution_stat'] = \
        {
            "name": "Number of people with disabilities benefited in Restitution programme from 2009/2010 to 2017/2018",
            "values": {"this": disabilitiesrestitution_tot}
            }
    redistributionrestitution['projectsrestitution_stat'] = \
        {
            "name": "Number of projects in the restitution programme from 2009/2010 to 2017/2018",
            "values": {"this": projectsrestitution_tot}
            }
    redistributionrestitution['beneficiariesrestitution_stat'] = \
        {
            "name": "Number of beneficiaries in the restitution programme from 2009/2010 to 2017/2018",
            "values": {"this": beneficiariesrestitution_tot}
            }

    # if total hectares of redistruted land is missing
    # and total hectares acquired under restitution is missing, then there's no data
    redistributionrestitution[
        'is_missing'] = hectarestransferredperprovincebyyear.get(
        'is_missing') \
                        and hectaresacquiredrestitution.get('is_missing')

    return redistributionrestitution
Ejemplo n.º 30
0
def get_health_profile(geo, session, display_profile, comparative=False):
    youth_difficulty_by_function, _ = get_stat_data(
        ['function type'],
        geo,
        session,
        table_universe=
        'Youth with non-missing responses to the general health and functioning questions',
        table_dataset='Census and Community Survey',
        key_order=DIFFICULTY_FUNCTIONING_KEY_ORDER)

    youth_female_given_birth, _ = get_stat_data(
        ['given birth'],
        geo,
        session,
        table_universe='Female youth',
        table_dataset='Census and Community Survey',
        key_order=GIVEN_BIRTH_KEY_ORDER)

    youth_female_given_birth['Unspecified']['name'] = 'Unspecified *'

    youth_female_given_birth_by_age_group, _ = get_stat_data(
        ['given birth', 'age in completed years'],
        geo,
        session,
        table_universe='Female youth',
        table_dataset='Census and Community Survey',
        percent_grouping=['age in completed years'],
        slices=['Given birth'],
        key_order={'given birth': GIVEN_BIRTH_KEY_ORDER},
        recode=GIVEN_BIRTH_AGE_GROUP_RECODE)

    final_data = {
        'youth_difficulty_seeing': {
            "name":
            "Of youth experience difficulty in seeing even when using eye glasses",
            "values": {
                "this":
                youth_difficulty_by_function[
                    'Seeing, even when using eye glasses']['values']['this']
            }
        },
        'youth_difficulty_by_function':
        youth_difficulty_by_function,
        'youth_female_have_given_birth': {
            "name": "Of females aged 15-24 have given birth to a child",
            "values": {
                "this":
                youth_female_given_birth['Given birth']['values']['this']
            }
        },
        'youth_female_given_birth':
        youth_female_given_birth,
        'youth_female_given_birth_by_age_group':
        youth_female_given_birth_by_age_group
    }

    if display_profile == 'WC' and geo.geo_level != 'ward':
        # We don't have data on ward level for the following
        with dataset_context(year='2017'):
            youth_pregnancy_rate_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Teenage pregnancy rate by year',
                table_dataset='Department of Health Administrative data',
                percent=False)

            youth_delivery_rate_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Teenage delivery rate by year',
                table_dataset='Department of Health Administrative data',
                percent=False)

            youth_female_causes_of_death, _ = get_stat_data(
                ['cause of death'],
                geo,
                session,
                table_universe='Female youth causes of death',
                table_dataset='Department of Health Administrative data',
                order_by='-total')
            youth_male_causes_of_death, _ = get_stat_data(
                ['cause of death'],
                geo,
                session,
                table_universe='Male youth causes of death',
                table_dataset='Department of Health Administrative data',
                order_by='-total')

        if not comparative:
            youth_female_top10_causes_of_death = OrderedDict()
            youth_male_top10_causes_of_death = OrderedDict()
            for key, value in youth_female_causes_of_death.items()[0:10]:
                youth_female_top10_causes_of_death[key] = value
            for key, value in youth_male_causes_of_death.items()[0:10]:
                youth_male_top10_causes_of_death[key] = value

            youth_female_top10_causes_of_death[
                'metadata'] = youth_female_causes_of_death['metadata']
            youth_male_top10_causes_of_death[
                'metadata'] = youth_male_causes_of_death['metadata']

        final_data.update({
            'youth_preganacy_rate_latest': {
                "name": "Of total pregnancies are to females under 18 years",
                "values": {
                    "this":
                    youth_pregnancy_rate_by_year['2016-17']['values']['this']
                }
            },
            'youth_pregnancy_rate_by_year':
            youth_pregnancy_rate_by_year,
            'youth_delivery_rate_latest': {
                "name": "Of total deliveries are to females under 18 years",
                "values": {
                    "this":
                    youth_delivery_rate_by_year['2016-17']['values']['this']
                }
            },
            'youth_delivery_rate_by_year':
            youth_delivery_rate_by_year,
            'youth_female_HIV_deaths': {
                "name": "Of female youth deaths were due to HIV/AIDS",
                "values": {
                    "this":
                    youth_female_causes_of_death['HIV/AIDS']['values']['this']
                }
            },
            'youth_male_interpersonal_violence_deaths': {
                "name":
                "Of male youth deaths were due to interpersonal violence",
                "values": {
                    "this":
                    youth_male_causes_of_death['Interpersonal violence']
                    ['values']['this']
                }
            },
            'youth_female_top10_causes_of_death':
            youth_female_top10_causes_of_death
            if not comparative else youth_female_causes_of_death,
            'youth_male_top10_causes_of_death':
            youth_male_top10_causes_of_death
            if not comparative else youth_male_causes_of_death
        })

    return final_data
Ejemplo n.º 31
0
def get_afrobarometer_profile(geo, session):
    with dataset_context(year='2017'):
        access_to_information = LOCATIONNOTFOUND
        allow_farmers_retain_land_ownership = LOCATIONNOTFOUND
        maintain_willing_buyer_willing_seller_policy = LOCATIONNOTFOUND
        land_acquisation_challenges = LOCATIONNOTFOUND
        land_to_prioritise_for_redistribution = LOCATIONNOTFOUND
        women_have_equal_right_to_land = LOCATIONNOTFOUND
        women_men_equal_chance_own_land = LOCATIONNOTFOUND

        try:
            access_to_information, _ = get_stat_data('access_to_information',
                                                     geo,
                                                     session, table_fields=[
                    'access_to_information'])
        except Exception:
            pass

        try:
            allow_farmers_retain_land_ownership, _ = get_stat_data(
                'allow_farmers_retain_land_ownership', geo, session,
                table_fields=['allow_farmers_retain_land_ownership'])
        except Exception:
            pass

        try:
            maintain_willing_buyer_willing_seller_policy, _ = get_stat_data(
                'maintain_willing_buyer_willing_seller_policy', geo, session,
                table_fields=['maintain_willing_buyer_willing_seller_policy'])
        except Exception:
            pass

        try:
            land_acquisation_challenges, _ = get_stat_data(
                'land_acquisation_challenges', geo, session,
                table_fields=['land_acquisation_challenges'])
        except Exception:
            pass

        try:
            land_to_prioritise_for_redistribution, _ = get_stat_data(
                'land_to_prioritise_for_redistribution', geo, session,
                table_fields=['land_to_prioritise_for_redistribution'])
        except Exception:
            pass

        is_missing = access_to_information.get('is_missing') and \
                     allow_farmers_retain_land_ownership.get('is_missing') and \
                     maintain_willing_buyer_willing_seller_policy.get(
                         'is_missing') \
                     and land_acquisation_challenges.get('is_missing') and \
                     land_to_prioritise_for_redistribution.get('is_missing') and \
                     women_have_equal_right_to_land.get('is_missing') and \
                     women_men_equal_chance_own_land.get('is_missing')

    final_data = {
        'is_missing': is_missing,
        'access_to_information': access_to_information,
        'allow_farmers_retain_land_ownership': allow_farmers_retain_land_ownership,
        'maintain_willing_buyer_willing_seller_policy': maintain_willing_buyer_willing_seller_policy,
        'land_acquisation_challenges': land_acquisation_challenges,
        'land_to_prioritise_for_redistribution': land_to_prioritise_for_redistribution,
        'women_have_equal_right_to_land': women_have_equal_right_to_land,
        'women_men_equal_chance_own_land': women_men_equal_chance_own_land,
    }

    return final_data
Ejemplo n.º 32
0
def get_education_profile(geo, session, display_profile, comparative=False):
    youth_completed_grade9, _ = get_stat_data(
        ['completed grade9'],
        geo,
        session,
        table_universe='Youth aged 16 to 17',
        table_dataset='Census and Community Survey',
        key_order=('Completed', 'Not completed'))

    youth_completed_grade9_by_gender, _ = get_stat_data(
        ['completed grade9', 'gender'],
        geo,
        session,
        table_universe='Youth aged 16 to 17',
        table_dataset='Census and Community Survey',
        percent_grouping=['gender'],
        slices=['Completed'],
        key_order={'gender': GENDER_ORDER})

    youth_education_level, youth_pop_20_to_24 = get_stat_data(
        ['education level'],
        geo,
        session,
        table_universe='Youth aged 20 to 24',
        table_dataset='Census and Community Survey',
        key_order=EDUCATION_LEVEL_KEY_ORDER)

    youth_perc_matric = None
    if youth_education_level['Matric/matric equivalent']['numerators']['this']:
        matric_or_equiv = (
            youth_education_level['Matric/matric equivalent']['numerators']
            ['this'] +
            youth_education_level['Any tertiary']['numerators']['this'])
        youth_perc_matric = percent(matric_or_equiv, youth_pop_20_to_24)

    youth_education_attendance, _ = get_stat_data(
        ['attendance'],
        geo,
        session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_fields=['attendance', 'age in completed years', 'gender'])

    youth_education_attending_by_age, _ = get_stat_data(
        ['attendance', 'age in completed years'],
        geo,
        session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_fields=['attendance', 'age in completed years', 'gender'],
        table_name='youth_education_attendance_age_incompleted_years_gender',
        percent_grouping=['age in completed years'],
        slices=['Yes'])

    youth_education_attending_by_gender, _ = get_stat_data(
        ['attendance', 'gender'],
        geo,
        session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_fields=['attendance', 'age in completed years', 'gender'],
        percent_grouping=['gender'],
        slices=['Yes'],
        key_order={'gender': GENDER_ORDER})

    final_data = {
        'youth_completed_grade9': youth_completed_grade9,
        'youth_perc_completed_grade9': {
            'name': 'Of youth aged 16-17 have completed grade 9 or higher',
            'values': {
                'this': youth_completed_grade9['Completed']['values']['this']
            }
        },
        'youth_completed_grade9_by_gender': youth_completed_grade9_by_gender,
        'youth_education_level': youth_education_level,
        'youth_perc_matric': {
            "name":
            "Of youth aged 20-24 have completed matric/matric equivalent or higher",
            "values": {
                "this": youth_perc_matric
            }
        },
        'youth_perc_attending': {
            "name": "Of youth aged 15-24 attend an educational institution",
            "values": {
                "this": youth_education_attendance['Yes']['values']['this']
            }
        },
        'youth_education_attending_by_age': youth_education_attending_by_age,
        'youth_education_attending_by_gender':
        youth_education_attending_by_gender,
    }

    if display_profile == 'WC':
        with dataset_context(year='2017'):
            youth_average_mean_score_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe=
                'Average mean score in both language and mathematics',
                percent=False)

            youth_average_language_score_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Average score in language',
                percent=False)

            youth_average_maths_score_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Average score in mathematics',
                percent=False)

            youth_language_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'],
                geo,
                session,
                table_universe='Percentage passed in language',
                key_order={'outcome': ['Passed', 'Failed']},
                percent=False,
                slices=['2017'])

            youth_maths_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'],
                geo,
                session,
                table_universe='Percentage passed in mathematics',
                key_order={'outcome': ['Passed', 'Failed']},
                percent=False,
                slices=['2017'])

        with dataset_context(year='2017'):
            youth_matric_outcome_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Matric pass rate',
                only={'outcome': ['Passed']},
                percent=False)

            youth_matric_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'],
                geo,
                session,
                table_universe='Matric pass rate',
                key_order={'outcome': ['Passed', 'Failed']},
                percent=False,
                slices=['2017'])

            youth_matric_throughput_rate_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Matric passes as a % of grade 8 enrolment',
                only={'outcome': ['Passed']},
                percent=False)

            youth_matric_throughput_latest, _ = get_stat_data(
                ['year', 'outcome'],
                geo,
                session,
                table_universe='Matric passes as a % of grade 8 enrolment',
                key_order={'outcome': ['Passed', 'Dropped out or failed']},
                percent=False,
                slices=['2017'])

            youth_bachelor_passes_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Bachelor passes as a % of grade 8 enrolment',
                only={'outcome': ['Bachelor pass']},
                percent=False)

            youth_bachelor_outcome_latest, _ = get_stat_data(
                ['year', 'outcome'],
                geo,
                session,
                table_universe='Bachelor passes as a % of grade 8 enrolment',
                key_order={'outcome': ['Bachelor pass', 'No bachelor pass']},
                percent=False,
                slices=['2017'])

            youth_student_dropout_rate_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Dropout rates between grade 10 and matric',
                only={'outcome': ['Dropped out']},
                percent=False)

            youth_student_dropout_rate_latest, _ = get_stat_data(
                ['year', 'outcome'],
                geo,
                session,
                table_universe='Dropout rates between grade 10 and matric',
                percent=False,
                slices=['2017'])

        final_data.update({
            'youth_ave_mean_score_latest': {
                "name": "Average mean score in both language and mathematics",
                "values": {
                    "this":
                    youth_average_mean_score_by_year['2017']['values']['this']
                }
            },
            'youth_ave_mean_score_by_year':
            youth_average_mean_score_by_year,
            'youth_ave_language_score_latest': {
                "name": "Average score in language",
                "values": {
                    "this":
                    youth_average_language_score_by_year['2017']['values']
                    ['this']
                }
            },
            'youth_ave_language_score_by_year':
            youth_average_language_score_by_year,
            'youth_ave_maths_score_latest': {
                "name": "Average score in mathematics",
                "values": {
                    "this":
                    youth_average_maths_score_by_year['2017']['values']['this']
                }
            },
            'youth_ave_maths_score_by_year':
            youth_average_maths_score_by_year,
            'youth_language_outcome_latest':
            youth_language_outcome_latest,
            'youth_maths_outcome_latest':
            youth_maths_outcome_latest,
            'youth_matric_pass_rate_latest': {
                "name": "Of students writing matric passed",
                "values": {
                    "this":
                    youth_matric_outcome_latest['Passed']['values']['this']
                }
            },
            'youth_matric_outcome_latest':
            youth_matric_outcome_latest,
            'youth_matric_outcome_by_year':
            youth_matric_outcome_by_year,
            'youth_matric_throughput_rate_latest': {
                "name": "Of Grade 8 students go on to pass matric",
                "values": {
                    "this":
                    youth_matric_throughput_latest['Passed']['values']['this']
                }
            },
            'youth_matric_throughput_latest':
            youth_matric_throughput_latest,
            'youth_matric_throughput_rate_by_year':
            youth_matric_throughput_rate_by_year,
            'youth_bachelor_passes_latest': {
                "name":
                "Of Grade 8 students go on to pass matric with a bachelor's pass",
                "values": {
                    "this":
                    youth_bachelor_outcome_latest['Bachelor pass']['values']
                    ['this']
                }
            },
            'youth_bachelor_outcome_latest':
            youth_bachelor_outcome_latest,
            'youth_bachelor_passes_by_year':
            youth_bachelor_passes_by_year,
            'youth_student_dropout_rate_latest': {
                "name": "Of students drop out between grade 10 and matric",
                "values": {
                    "this":
                    youth_student_dropout_rate_latest['Dropped out']['values']
                    ['this']
                }
            },
            'youth_student_dropout_rate_by_year':
            youth_student_dropout_rate_by_year
        })

    return final_data
Ejemplo n.º 33
0
def get_equitable_allocation_data(geo, session):

    try:
        with dataset_context(year='2016'):
            budget_allocation, _ = get_stat_data('financial_year', geo, session,
                                                 table_fields=[
                                                     'financial_year'],
                                                 percent=False)

        yr2013_2014 = 0
        for data, value in budget_allocation.get('FY 2013/2014',
                                                 {}).items():
            if data == 'values':
                yr2013_2014 += value['this']

        yr2014_2015 = 0
        for data, value in budget_allocation.get('FY 2014/2015',
                                                 {}).items():
            if data == 'values':
                yr2014_2015 += value['this']

        yr2015_2016 = 0
        for data, value in budget_allocation.get('FY 2015/2016',
                                                 {}).items():
            if data == 'values':
                yr2015_2016 += value['this']

        yr2016_2017 = 0
        for data, value in budget_allocation.get('FY 2016/2017',
                                                 {}).items():
            if data == 'values':
                yr2016_2017 += value['this']

        final_data = {
            'budget_allocation': budget_allocation,
            'fy2013_2014': {
                'name': 'FY 2013/2014',
                'numerators': {'this': yr2013_2014},
                'values': {'this': yr2013_2014}
            },
            'fy2014_2015': {
                'name': 'FY 2014/2015',
                'numerators': {'this': yr2014_2015},
                'values': {'this': yr2014_2015}
            },
            'fy2015_2016': {
                'name': 'FY 2015/2016',
                'numerators': {'this': yr2015_2016},
                'values': {'this': yr2015_2016}
            },
            'fy2016_2017': {
                'name': 'FY 2016/2017',
                'numerators': {'this': yr2016_2017},
                'values': {'this': yr2016_2017}
            },
        }

        return final_data

    except Exception:
        budget_allocation, _ = LOCATIONNOTFOUND, 0
Ejemplo n.º 34
0
def get_poverty_profile(geo, session, display_profile, comparative=False):
    youth_income_poor_by_age_group, _ = get_stat_data(
        ['income poverty', 'age group'],
        geo,
        session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        percent_grouping=['age group'],
        slices=['Income-poor'],
        recode=INCOME_POVERTY_AGE_GROUP_RECODE)

    youth_income_poor_by_pop_group, _ = get_stat_data(
        ['income poverty', 'population group'],
        geo,
        session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_name='youth_income_poverty_population_group_gender',
        percent_grouping=['population group'],
        slices=['Income-poor'],
        key_order={'population group': POPULATION_GROUP_ORDER})

    youth_income_poor_by_gender, _ = get_stat_data(
        ['income poverty', 'gender'],
        geo,
        session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        percent_grouping=['gender'],
        slices=['Income-poor'],
        key_order={'gender': GENDER_ORDER})

    with dataset_context(year='2011'):
        youth_multid_poverty, _ = get_stat_data(
            ['multidimensionally poor'],
            geo,
            session,
            table_universe='Youth',
            table_dataset='Multidimensional poverty',
            key_order=('Multidimensionally poor', 'Non-poor'),
        )

        youth_multid_poor_by_pop_group, _ = get_stat_data(
            ['multidimensionally poor', 'population group'],
            geo,
            session,
            table_universe='Youth',
            table_dataset='Multidimensional poverty',
            table_name='youth_multidimensionally_poor_population_group_gender',
            percent_grouping=['population group'],
            slices=['Multidimensionally poor'],
            key_order={'population group': POPULATION_GROUP_ORDER})

        youth_multid_poor_by_gender, _ = get_stat_data(
            ['multidimensionally poor', 'gender'],
            geo,
            session,
            table_universe='Youth',
            table_dataset='Multidimensional poverty',
            percent_grouping=['gender'],
            slices=['Multidimensionally poor'],
            key_order={'gender': GENDER_ORDER})

        youth_mpi_table = get_datatable('youth_mpi_score')

        youth_mpi_score, _ = youth_mpi_table.get_stat_data(geo, percent=False)
        youth_mpi_score['youth_mpi_score']['name'] = 'Youth MPI score (0-1)*'

    final_data = {
        'youth_income_poor': {
            "name": "Of youth live in income-poor households *",
            "values": {
                "this":
                youth_income_poor_by_age_group['Youth (15-24)']['values']
                ['this']
            }
        },
        'youth_income_poor_by_age_group': youth_income_poor_by_age_group,
        'youth_income_poor_by_pop_group': youth_income_poor_by_pop_group,
        'youth_income_poor_by_gender': youth_income_poor_by_gender,
        'youth_multid_poor': {
            "name": "Of youth are multidimensionally poor*",
            "values": {
                "this":
                youth_multid_poverty['Multidimensionally poor']['values']
                ['this']
            }
        },
        'youth_multid_poor_by_pop_group': youth_multid_poor_by_pop_group,
        'youth_multid_poor_by_gender': youth_multid_poor_by_gender,
        'youth_multid_poverty': youth_multid_poverty,
        'youth_mpi_score_stat': youth_mpi_score['youth_mpi_score'],
        'youth_mpi_score': youth_mpi_score
    }

    return final_data
Ejemplo n.º 35
0
def get_health_profile(geo, session, display_profile, comparative=False):
    youth_difficulty_by_function, _ = get_stat_data(
        ['function type'], geo, session,
        table_universe='Youth with non-missing responses to the general health and functioning questions',
        table_dataset='Census and Community Survey',
        key_order=DIFFICULTY_FUNCTIONING_KEY_ORDER)

    youth_female_given_birth, _ = get_stat_data(
        ['given birth'], geo, session,
        table_universe='Female youth',
        table_dataset='Census and Community Survey',
        key_order=GIVEN_BIRTH_KEY_ORDER)

    youth_female_given_birth['Unspecified']['name'] = 'Unspecified *'

    youth_female_given_birth_by_age_group, _ = get_stat_data(
        ['given birth', 'age in completed years'], geo, session,
        table_universe='Female youth',
        table_dataset='Census and Community Survey',
        percent_grouping=['age in completed years'], slices=['Given birth'],
        key_order={'given birth': GIVEN_BIRTH_KEY_ORDER},
        recode=GIVEN_BIRTH_AGE_GROUP_RECODE)

    final_data = {
        'youth_difficulty_seeing': {
            "name": "Of youth experience difficulty in seeing even when using eye glasses",
            "values": {"this": youth_difficulty_by_function['Seeing, even when using eye glasses']['values']['this']}
        },
        'youth_difficulty_by_function': youth_difficulty_by_function,
        'youth_female_have_given_birth': {
            "name": "Of females aged 15-24 have given birth to a child",
            "values": {"this": youth_female_given_birth['Given birth']['values']['this']}
        },
        'youth_female_given_birth': youth_female_given_birth,
        'youth_female_given_birth_by_age_group': youth_female_given_birth_by_age_group
    }

    if display_profile == 'WC' and geo.geo_level != 'ward':
        # We don't have data on ward level for the following
        with dataset_context(year='2017'):
            youth_pregnancy_rate_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Teenage pregnancy rate by year',
                table_dataset='Department of Health Administrative data',
                percent=False)

            youth_delivery_rate_by_year, _ = get_stat_data(
                ['year'], geo, session,
                table_universe='Teenage delivery rate by year',
                table_dataset='Department of Health Administrative data',
                percent=False)

            youth_female_causes_of_death, _ = get_stat_data(
                ['cause of death'], geo, session,
                table_universe='Female youth causes of death',
                table_dataset='Department of Health Administrative data',
                order_by='-total')
            youth_male_causes_of_death, _ = get_stat_data(
                ['cause of death'], geo, session,
                table_universe='Male youth causes of death',
                table_dataset='Department of Health Administrative data',
                order_by='-total')

        if not comparative:
            youth_female_top10_causes_of_death = OrderedDict()
            youth_male_top10_causes_of_death = OrderedDict()
            for key, value in youth_female_causes_of_death.items()[0:10]:
                youth_female_top10_causes_of_death[key] = value
            for key, value in youth_male_causes_of_death.items()[0:10]:
                youth_male_top10_causes_of_death[key] = value

            youth_female_top10_causes_of_death['metadata'] = youth_female_causes_of_death['metadata']
            youth_male_top10_causes_of_death['metadata'] = youth_male_causes_of_death['metadata']

        final_data.update({
            'youth_preganacy_rate_latest': {
                "name": "Of total pregnancies are to females under 18 years",
                "values": {"this":youth_pregnancy_rate_by_year['2016-17']['values']['this']}
            },
            'youth_pregnancy_rate_by_year': youth_pregnancy_rate_by_year,
            'youth_delivery_rate_latest': {
                "name": "Of total deliveries are to females under 18 years",
                "values": {"this":youth_delivery_rate_by_year['2016-17']['values']['this']}
            },
            'youth_delivery_rate_by_year': youth_delivery_rate_by_year,
            'youth_female_HIV_deaths': {
                "name": "Of female youth deaths were due to HIV/AIDS",
                "values": {"this":youth_female_causes_of_death['HIV/AIDS']['values']['this']}
            },
            'youth_male_interpersonal_violence_deaths': {
                "name": "Of male youth deaths were due to interpersonal violence",
                "values": {"this":youth_male_causes_of_death['Interpersonal violence']['values']['this']}
            },
            'youth_female_top10_causes_of_death': youth_female_top10_causes_of_death if not comparative else youth_female_causes_of_death,
            'youth_male_top10_causes_of_death': youth_male_top10_causes_of_death if not comparative else youth_male_causes_of_death
        })

    return final_data
Ejemplo n.º 36
0
def get_schools_profile(geo, session, year):
    schools_dist = LOCATIONNOTFOUND
    region_dist = LOCATIONNOTFOUND
    category_dist = LOCATIONNOTFOUND
    top_schools_40_more = []
    top_schools_40_less = []
    lowest_schools_40_less = []
    lowest_schools_40_more = []
    gpa_dist_data = []
    gender_dist = []
    total_schools = 0
    median = 0

    reg = 'region'
    if geo.geo_level == "country":
        reg = 'region'
    elif geo.geo_level == "region":
        reg = 'district'
    elif geo.geo_level == "district":
        reg = 'ward'

    with dataset_context(year='2017'):
        try:
            schools_dist, total_schools = get_stat_data(['ownership'], geo=geo, table_name='secondary_school',
                                                        session=session, only={
                    'year_of_result': [year]})
        except Exception as e:
            pass

        try:
            region_dist, total_schools = get_stat_data([reg], geo=geo,
                                                       session=session, only={
                    'year_of_result': [year]})
        except Exception as e:
            pass

        try:
            category_dist, _ = get_stat_data(['more_than_40'], geo=geo,
                                             session=session,
                                             only={'year_of_result': [year]})
        except Exception as e:
            pass

        try:
            gender_dist, _ = get_stat_data(['gender'], geo=geo, session=session,
                                           only={'year_of_result': [year]})
        except Exception:
            pass





        try:
            # ownership status
            # school_dist_data, _ = get_stat_data('age in completed years',geo=geo, session=session, only={'year_of_result': [year]})
            # Choosing sorting option
            # Sorting will only be done using national_rank all, as regional and district ranks are unknown for some result esp historical
            Base.metadata.reflect()
            rank_column = Base.metadata.tables[
                'secondary_school'].c.national_rank_all
            # Getting top for schools with more than 40 students
            top_schools_40_more = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like(
                    "yes%")) \
                .order_by(asc(cast(rank_column, Integer))) \
                .all()
            # Getting top for schools with less than 40 students
            top_schools_40_less = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like("no%")) \
                .order_by(asc(cast(rank_column, Integer))) \
                .all()

            # Getting lowest schools with more than 40 students
            lowest_schools_40_more = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like(
                    "yes%")) \
                .order_by(desc(cast(rank_column, Integer))) \
                .all()
            # Getting lowest for schools with less than 40 students
            lowest_schools_40_less = session.query(
                Base.metadata.tables['secondary_school']) \
                .filter(Base.metadata.tables[
                            'secondary_school'].c.geo_level == geo.geo_level) \
                .filter(
                Base.metadata.tables['secondary_school'].c.geo_code == geo.geo_code) \
                .filter(
                Base.metadata.tables['secondary_school'].c.year_of_result == year) \
                .filter(
                Base.metadata.tables['secondary_school'].c.more_than_40.like("no%")) \
                .order_by(desc(cast(rank_column, Integer))) \
                .all()
            # median gpa
            db_model_age = get_datatable('code_name_avg_gpa')
            objects = db_model_age.get_rows_for_geo(geo, session)
            median = calculate_median(objects, 'avg_gpa')

            # gpa in 1 point groups
            def gpa_recode(f, x):
                gpa = x
                if gpa >= 4:
                    return '4+'
                bucket = 1 * (gpa / 1)
                return '%d-%d' % (bucket, bucket + 2)

            gpa_dist_data, total_schools = get_stat_data(
                'avg_gpa', geo, session,
                table_fields=['code', 'name', 'avg_gpa'],
                recode=gpa_recode, exclude=['unspecified'],
                only={'year_of_result': [year]})

            total_private = 0.0
            for data in schools_dist.get('Non-Government', {}).values():
                if 'numerators' in data:
                    total_private += data['numerators']['this']
        except Exception as e:
            pass

    return {
        'schools_distribution': schools_dist,
        'region_distribution': region_dist,
        'category_distribution': category_dist,
        'best_schools_more_40': top_schools_40_more,
        'worst_schools_more_40': lowest_schools_40_more,
        'best_schools_less_40': top_schools_40_less,
        'worst_schools_less_40': lowest_schools_40_less,
        'gpa_group_distribution': gpa_dist_data,
        'gender_distribution': gender_dist,
        'total_schools': {
            "name": "Schools",
            "values": {"this": total_schools}
        },
        'median_gpa': {
            "name": "Median GPA",
            "values": {"this": median},
        },
    }
Ejemplo n.º 37
0
def get_landsalescolour_profile(geo, session):
    with dataset_context(year='2018'):
        landsalescolourhectares = landsalescolourhectaresperMONTH_V2 = LOCATIONNOTFOUND
        landsalescolourcostperMONTH_V2 = landsalescolourcostpermonthbreakdown = LOCATIONNOTFOUND
        landsalescolourcostpermonthbreakdowngu = landsalescolourcostpermonthbreakdownga = LOCATIONNOTFOUND
        landsalescolourcostpermonthbreakdownpr = landsalescolourcostpermonthbreakdownot = LOCATIONNOTFOUND
        landsalescolourtransaction = landsalescolourtransactionpermonthbreakdown = LOCATIONNOTFOUND
        landsalesallvscolourtransaction = landsalescolourhectarespermonthbreakdown = LOCATIONNOTFOUND
        landsalescolourhectarespermonthbreakdowngu = landsalescolourhectarespermonthbreakdownga = LOCATIONNOTFOUND
        landsalescolourhectarespermonthbreakdownpr = landsalescolourhectarespermonthbreakdownot = LOCATIONNOTFOUND
        landsalescolourtransactionpermonthbreakdownga = landsalescolourtransactionpermonthbreakdowngu = LOCATIONNOTFOUND
        landsalescolourtransactionpermonthbreakdownpr = landsalescolourtransactionpermonthbreakdownot = LOCATIONNOTFOUND
        landsalescolourtransactionperMONTH_V2 = LOCATIONNOTFOUND

        landsalescolourhectares_tot = 0
        landsalescolourtransaction_tot = 0
        landsalescolourcost_tot = 0

        try:
            landsalescolourhectares, landsalescolourhectares_tot = get_stat_data(
                ['breakdown_cc'], geo, session,
                table_name='land_traded_colour_hectares_breakdown_2018',
                only={'breakdown_cc': ['Government Agriculture',
                                            'Government Urban', 'Private',
                                            'Other']}
            )
        except Exception as e:
            pass

        try:
            landsalescolourtransaction, landsalescolourtransaction_tot = get_stat_data(
                ['breakdown_cc'], geo, session,
                table_name='land_traded_colour_transactions_2018',
                only={'breakdown_cc': ['Government Agriculture',
                                            'Government Urban', 'Private',
                                            'Other']}
            )
        except Exception as e:
            pass

        try:
            landsalesallvscolourtransaction, _ = get_stat_data(
                ['transaction_group'], geo, session,
                table_name='land_traded_all_vs_colour_2018',
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourtransactionperMONTH_V2, _ = get_stat_data(
                ['month', 'transaction_group'], geo, session,
                table_name='land_traded_all_vs_colour_2018',
                only={'transaction_group': ['all', 'colour']},
                key_order={'month': MONTH_V2,
                           'transaction_group': ['All', 'Colour']},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourhectaresperMONTH_V2, _ = get_stat_data(
                ['month_cc'], geo, session,
                table_name='land_traded_colour_hectares_breakdown_2018',
                key_order=( 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May',\
                 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov'),
                 percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourhectarespermonthbreakdown, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_hectares_breakdown_2018',
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourhectarespermonthbreakdownga, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_hectares_breakdown_2018',
                only= {'breakdown_cc': ['Government Agriculture']},
                key_order={'month_cc': MONTH_V2,
                        'breakdown_cc': ['Government Agriculture']},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourhectarespermonthbreakdowngu, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_hectares_breakdown_2018',
                only= {'breakdown_cc': ['Government Urban']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourhectarespermonthbreakdownpr, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_hectares_breakdown_2018',
                only= {'breakdown_cc': ['Private']},
                key_order={'month_cc': MONTH_V2,
                        'breakdown_cc': ['Private']},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourhectarespermonthbreakdownot, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_hectares_breakdown_2018',
                only= {'breakdown_cc': ['Other']},
                key_order={'month_cc': MONTH_V2,
                'breakdown_cc': ['Other']},
                percent=False
                )
        except Exception as e:
            pass

        try:
            landsalescolourtransactionpermonthbreakdown, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_transactions_2018',
                only={'breakdown_cc': ['Government Agriculture',
                                            'Government Urban', 'Private',
                                            'Other']},
                key_order={'month_cc': MONTH_V2,
                        'breakdown_cc': ['Government Agriculture', 'Private',
                                            'Government Urban',
                                            'Other']},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourtransactionpermonthbreakdowngu, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_transactions_2018',
                only={'breakdown_cc': ['Government Urban']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourtransactionpermonthbreakdownga, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_transactions_2018',
                only={'breakdown_cc': ['Government Agriculture']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourtransactionpermonthbreakdownot, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_transactions_2018',
                only={'breakdown_cc': ['Other']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourtransactionpermonthbreakdownpr, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_transactions_2018',
                only={'breakdown_cc': ['Private']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourcostperMONTH_V2, landsalescolourcost_tot = get_stat_data(
                ['month_cc'], geo, session,
                table_name='land_traded_colour_cost_breakdown_2018',
                key_order=( 'Dec', 'Jan', 'Feb', 'Mar', 'Apr', 'May',\
                 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov'),
                 percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourcostpermonthbreakdown, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_cost_breakdown_2018',
                key_order={'month_cc': MONTH_V2,
                        'breakdown_cc': ['Government Agriculture', 'Private',
                                            'Government Urban',
                                            'Other']},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourcostpermonthbreakdownot, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_cost_breakdown_2018',
                only={'breakdown_cc': ['Other']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourcostpermonthbreakdownpr, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_cost_breakdown_2018',
                only={'breakdown_cc': ['Private']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourcostpermonthbreakdowngu, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_cost_breakdown_2018',
                only={'breakdown_cc': ['Government Urban']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass

        try:
            landsalescolourcostpermonthbreakdownga, _ = get_stat_data(
                ['breakdown_cc','month_cc'], geo, session,
                table_name='land_traded_colour_cost_breakdown_2018',
                only={'breakdown_cc': ['Government Agriculture']},
                key_order={'month_cc': MONTH_V2},
                percent=False
            )
        except Exception as e:
            pass
    return {
        'landsalescolourhectares': landsalescolourhectares,
        'landsalescolourtransaction': landsalescolourtransaction,
        'landsalesallvscolourtransaction': landsalesallvscolourtransaction,
        'landsalescolourtattransactionpermonth': landsalescolourtransactionperMONTH_V2,
        'landsalescolourhectarespermonthbreakdown': landsalescolourhectarespermonthbreakdown,
        'landsalescolourhectarespermonthbreakdowngu': landsalescolourhectarespermonthbreakdowngu,
        'landsalescolourhectarespermonthbreakdownga': landsalescolourhectarespermonthbreakdownga,
        'landsalescolourhectarespermonthbreakdownot': landsalescolourhectarespermonthbreakdownot,
        'landsalescolourhectarespermonthbreakdownpr': landsalescolourhectarespermonthbreakdownpr,
        'landsalescolourtransactionpermonthbreakdown': landsalescolourtransactionpermonthbreakdown,
        'landsalescolourtransactionpermonthbreakdowngu': landsalescolourtransactionpermonthbreakdowngu,
        'landsalescolourtransactionpermonthbreakdownga': landsalescolourtransactionpermonthbreakdownga,
        'landsalescolourtransactionpermonthbreakdownpr': landsalescolourtransactionpermonthbreakdownpr,
        'landsalescolourtransactionpermonthbreakdownot': landsalescolourtransactionpermonthbreakdownot,
        'landsalescolourcostpermonthbreakdown': landsalescolourcostpermonthbreakdown,
        'landsalescolourcostpermonthbreakdowngu': landsalescolourcostpermonthbreakdowngu,
        'landsalescolourcostpermonthbreakdownga': landsalescolourcostpermonthbreakdownga,
        'landsalescolourcostpermonthbreakdownpr': landsalescolourcostpermonthbreakdownpr,
        'landsalescolourcostpermonthbreakdownot': landsalescolourcostpermonthbreakdownot,
        'landsalescolourcostpermonth': landsalescolourcostperMONTH_V2,
        'landsalescolourhectarespermonth': landsalescolourhectaresperMONTH_V2,
        'landsalescolourhectares_stat': {
            "name": "Total hectares (ha) traded from Dec 2017/Nov 2018 for transaction of colour",
            "values": {"this": landsalescolourhectares_tot},
        },
        'landsalescolourcost_stat': {
            "name": "Total Cost in R (million) traded from Dec 2017/Nov 2018 for transaction of colour",
            "values": {"this": landsalescolourcost_tot},
        },
        'landsalescolourtransaction_stat': {
            "name": "Total transactions traded from Dec 2017/Nov 2018 for transaction of colour",
            "values": {"this": landsalescolourtransaction_tot},
        },
        'is_missing': landsalescolourhectares.get('is_missing')

    }
Ejemplo n.º 38
0
def get_poverty_profile(geo, session, display_profile, comparative=False):
    youth_income_poor_by_age_group, _ = get_stat_data(
        ['income poverty', 'age group'], geo, session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        percent_grouping=['age group'], slices=['Income-poor'],
        recode=INCOME_POVERTY_AGE_GROUP_RECODE)

    youth_income_poor_by_pop_group, _ = get_stat_data(
        ['income poverty', 'population group'], geo, session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        table_name='youth_income_poverty_population_group_gender',
        percent_grouping=['population group'], slices=['Income-poor'],
        key_order={'population group': POPULATION_GROUP_ORDER})

    youth_income_poor_by_gender, _ = get_stat_data(
        ['income poverty', 'gender'], geo, session,
        table_universe='Youth',
        table_dataset='Census and Community Survey',
        percent_grouping=['gender'], slices=['Income-poor'],
        key_order={'gender': GENDER_ORDER})

    with dataset_context(year='2011'):
        youth_multid_poverty, _ = get_stat_data(
            ['multidimensionally poor'], geo, session,
            table_universe='Youth',
            table_dataset='Multidimensional poverty',
            key_order=('Multidimensionally poor', 'Non-poor'),)

        youth_multid_poor_by_pop_group, _ = get_stat_data(
            ['multidimensionally poor', 'population group'], geo, session,
            table_universe='Youth',
            table_dataset='Multidimensional poverty',
            table_name='youth_multidimensionally_poor_population_group_gender',
            percent_grouping=['population group'], slices=['Multidimensionally poor'],
            key_order={'population group': POPULATION_GROUP_ORDER})

        youth_multid_poor_by_gender, _ = get_stat_data(
            ['multidimensionally poor', 'gender'], geo, session,
            table_universe='Youth',
            table_dataset='Multidimensional poverty',
            percent_grouping=['gender'], slices=['Multidimensionally poor'],
            key_order={'gender': GENDER_ORDER})

        youth_mpi_table = get_datatable('youth_mpi_score')

        youth_mpi_score, _ = youth_mpi_table.get_stat_data(
            geo, percent=False)
        youth_mpi_score['youth_mpi_score']['name'] = 'Youth MPI score (0-1)*'

    final_data = {
        'youth_income_poor': {
            "name": "Of youth live in income-poor households *",
            "values": {"this": youth_income_poor_by_age_group['Youth (15-24)']['values']['this']}
        },
        'youth_income_poor_by_age_group': youth_income_poor_by_age_group,
        'youth_income_poor_by_pop_group': youth_income_poor_by_pop_group,
        'youth_income_poor_by_gender': youth_income_poor_by_gender,
        'youth_multid_poor': {
            "name": "Of youth are multidimensionally poor*",
            "values": {"this": youth_multid_poverty['Multidimensionally poor']['values']['this']}
        },
        'youth_multid_poor_by_pop_group': youth_multid_poor_by_pop_group,
        'youth_multid_poor_by_gender': youth_multid_poor_by_gender,
        'youth_multid_poverty': youth_multid_poverty,
        'youth_mpi_score_stat': youth_mpi_score['youth_mpi_score'],
        'youth_mpi_score': youth_mpi_score
    }

    return final_data
Ejemplo n.º 39
0
def get_districtdistribution_profile(geo, session):
    with dataset_context(year='2018'):
        towndistrictdistributiontransactions = all_town = LOCATIONNOTFOUND
        towndistrictdistributionhectares = towndistrictdistributionavgprice = LOCATIONNOTFOUND
        towndistrictdistributionpricetrends = LOCATIONNOTFOUND
        towndistrictdistributiontransactionsdata = towndistrictdistributionhectaresdata = LOCATIONNOTFOUND
        towndistrictdistributionpricetrendsdata = towndistrictdistributionavgpricedata = LOCATIONNOTFOUND
        towndistrictdistributionhectares_tot = towndistrictdistributiontransactions_tot = 0
        dist = {}
        towns = []

        try:
            #Get all towns in the geo
            all_town, _ = get_stat_data(
                ['name'], geo, session,
                table_name='land_traded_per_class_statistic_2018',
                exclude_zero=True,
                percent=False)

            for keys, townname in all_town.items():
                if keys != 'metadata':
                    towns.append(keys)
            towndistrictdistributiontransactionsdata = {}
            towndistrictdistributionhectaresdata = {}
            towndistrictdistributionpricetrendsdata = towndistrictdistributionavgpricedata = {}

            for town in towns:
                town_code = town.replace(' ', '_').replace('-', '_').replace(
                    '/',
                    '_').replace(
                    '(', '').replace(')', '').lower()
                try:
                    towndistrictdistributiontransactionsdata[
                        town_code], _ = get_stat_data(
                        ['statistic', 'class_distribution'], geo, session,
                        table_name='land_traded_per_class_statistic_2018',
                        only={'name': [town], 'statistic': ['Number of transactions in 12 months']},
                        key_order={'class_distribution': LAND_CLASS},
                        percent=False)
                except Exception as e:
                    pass

                try:
                    towndistrictdistributionhectaresdata[
                        town_code], _ = get_stat_data(
                        ['statistic', 'class_distribution'], geo, session,
                        table_name='land_traded_per_class_statistic_2018',
                        only={'name': [town], 'statistic': ['Total hectare traded in 12 months(ha)']},
                        key_order={'class_distribution': LAND_CLASS},
                        percent=False)
                except Exception:
                    pass

                try:
                    towndistrictdistributionavgpricedata[
                        town_code], _ = get_stat_data(
                        ['statistic', 'class_distribution'], geo, session,
                        table_name='land_traded_per_class_statistic_2018',
                        only={'name': [town], 'statistic': ['Average price per hectare in 12 months(R/ha)']},
                        key_order={'class_distribution': LAND_CLASS},
                        percent=False)
                except Exception:
                    pass

                    try:
                        towndistrictdistributionpricetrendsdata[
                            town_code], _ = get_stat_data(
                            ['statistic', 'class_distribution'], geo, session,
                            table_name='land_traded_per_class_statistic_2018',
                            only={'name': [town], 'statistic': ['Price trend per hectare in 12 months(R/ha)']},
                            key_order={'class_distribution': LAND_CLASS},
                            percent=False)
                    except Exception as e:
                        pass
        except Exception as e:
            pass

    dist[
        'towndistrictdistributiontransactionsdata'] = towndistrictdistributiontransactionsdata
    dist[
        'towndistrictdistributionhectaresdata'] = towndistrictdistributionhectaresdata
    dist[
        'towndistrictdistributionavgpricedata'] = towndistrictdistributionavgpricedata
    dist[
        'towndistrictdistributionpricetrendsdata'] = towndistrictdistributionpricetrendsdata
    dist['is_missing'] = all_town.get('is_missing')
    return dist
Ejemplo n.º 40
0
 def setUp(self):
     self.s = get_session()
     self.ctxt = dataset_context(year='latest')
     self.ctxt.__enter__()
Ejemplo n.º 41
0
def get_safety_profile(geo, session, display_profile, comparative=False):
    def rate_per_10k_pop(value, population):
        if not value:
            return None
        return value / population * 10000

    final_data = {}

    if display_profile == 'WC':
        youth_pop_table = get_datatable('youth_population')
        youth, pop_total = youth_pop_table.get_stat_data(geo,
                                                         total='total_pop',
                                                         percent='False')

        with dataset_context(year='2015'):
            victims_by_age_group_per_10k_pop, total_victims_per_10k_pop = get_stat_data(
                ['age group'],
                geo,
                session,
                table_universe=
                'Victims of crime per 10,000 population in age group',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)
            accused_by_age_group_per_10k_pop, total_accused_per_10k_pop = get_stat_data(
                ['age group'],
                geo,
                session,
                table_universe=
                'Accused of crime per 10,000 population in age group',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

            youth_victims_by_offence_per_10k_youth, _ = get_stat_data(
                ['type of offence'],
                geo,
                session,
                table_universe='Youth victims of crime, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015')
            youth_accused_by_offence_per_10k_youth, _ = get_stat_data(
                ['type of offence'],
                geo,
                session,
                table_universe='Youth accused of crimes, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015')

            youth_victims_by_pop_group_per_10k, _ = get_stat_data(
                ['population group'],
                geo,
                session,
                table_universe='Youth victims of crime, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)
            youth_accused_by_pop_group_per_10k, _ = get_stat_data(
                ['population group'],
                geo,
                session,
                table_universe='Youth accused of crimes, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

            youth_victims_by_gender_per_10k, _ = get_stat_data(
                ['gender'],
                geo,
                session,
                table_universe='Youth victims of crime, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False,
                key_order=GENDER_ORDER)
            youth_accused_by_gender_per_10k, _ = get_stat_data(
                ['gender'],
                geo,
                session,
                table_universe='Youth accused of crimes, per 10,000 youth',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False,
                key_order=GENDER_ORDER)

            youth_victims_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Youth victims of crime',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)
            youth_accused_by_year, _ = get_stat_data(
                ['year'],
                geo,
                session,
                table_universe='Youth accused of crimes',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

            crimes_by_year, _ = get_stat_data(
                ['type of crime', 'year'],
                geo,
                session,
                table_universe='Crimes',
                table_dataset='Police Crime Statistics 2006-2015',
                percent=False)

        contact_crimes_by_year = crimes_by_year['Contact crime']
        contact_crimes_by_year['metadata'] = crimes_by_year['metadata']
        property_crimes_by_year = crimes_by_year['Property crime']
        property_crimes_by_year['metadata'] = crimes_by_year['metadata']

        contact_crimes_per_10k_pop = rate_per_10k_pop(
            contact_crimes_by_year['2014-15']['values']['this'], pop_total)
        property_crimes_per_10k_pop = rate_per_10k_pop(
            property_crimes_by_year['2014-15']['values']['this'], pop_total)

        final_data = {
            'youth_victims_per_10k_youth': {
                "name": "Youth victims of contact crime per 10,000 youth",
                "values": {
                    "this":
                    victims_by_age_group_per_10k_pop['15-24']['values']['this']
                }
            },
            'youth_accused_per_10k_youth': {
                "name": "Youth accused of contact crime per 10,000 youth",
                "values": {
                    "this":
                    accused_by_age_group_per_10k_pop['15-24']['values']['this']
                }
            },
            'victims_by_age_group_per_10k_pop':
            victims_by_age_group_per_10k_pop,
            'accused_by_age_group_per_10k_pop':
            accused_by_age_group_per_10k_pop,
            'youth_victims_by_offence_per_10k_youth':
            youth_victims_by_offence_per_10k_youth,
            'youth_accused_by_offence_per_10k_youth':
            youth_accused_by_offence_per_10k_youth,
            'youth_victims_by_pop_group_per_10k':
            youth_victims_by_pop_group_per_10k,
            'youth_accused_by_pop_group_per_10k':
            youth_accused_by_pop_group_per_10k,
            'youth_victims_by_gender_per_10k': youth_victims_by_gender_per_10k,
            'youth_accused_by_gender_per_10k': youth_accused_by_gender_per_10k,
            'youth_victims_by_year': youth_victims_by_year,
            'youth_accused_by_year': youth_accused_by_year,
            'contact_crimes_per_10k_pop': {
                "name": "Contact crimes per 10,000 population",
                "values": {
                    "this": contact_crimes_per_10k_pop
                }
            },
            'property_crimes_per_10k_pop': {
                "name": "Property-related crime per 10,000 population",
                "values": {
                    "this": property_crimes_per_10k_pop
                }
            },
            'contact_crimes_by_year': contact_crimes_by_year,
            'property_crimes_by_year': property_crimes_by_year
        }

    return final_data