Beispiel #1
0
def get_prevalence_of_undernutrition_data_chart(domain,
                                                config,
                                                loc_level,
                                                show_test=False,
                                                icds_features_flag=False):
    month = datetime(*config['month'])
    three_before = datetime(*config['month']) - relativedelta(months=3)

    config['month__range'] = (three_before, month)
    del config['month']
    if config['aggregation_level'] >= AggregationLevels.SUPERVISOR:
        chm_filter = get_filters_from_config_for_chart_view(config)
        chm_queryset = ChildHealthMonthlyView.objects.filter(**chm_filter)
    else:
        chm_queryset = AggChildHealthMonthly.objects.filter(**config)
    chart_data = chm_queryset.values('month', '%s_name' % loc_level).annotate(
        moderately_underweight=Sum('nutrition_status_moderately_underweight'),
        normal=Sum('nutrition_status_normal'),
        severely_underweight=Sum('nutrition_status_severely_underweight'),
        weighed=Sum('nutrition_status_weighed'),
        total=Sum('wer_eligible'),
    ).order_by('month')

    if not show_test:
        chart_data = apply_exclude(domain, chart_data)

    if 'age_tranche' not in config:
        chart_data = chart_data.filter(age_tranche__lt=72)

    data = {
        'peach': OrderedDict(),
        'orange': OrderedDict(),
        'red': OrderedDict()
    }

    dates = [dt for dt in rrule(MONTHLY, dtstart=three_before, until=month)]

    for date in dates:
        miliseconds = int(date.strftime("%s")) * 1000
        data['peach'][miliseconds] = {'y': 0, 'weighed': 0, 'unweighed': 0}
        data['orange'][miliseconds] = {'y': 0, 'weighed': 0, 'unweighed': 0}
        data['red'][miliseconds] = {'y': 0, 'weighed': 0, 'unweighed': 0}

    best_worst = {}

    if 'month' not in config:
        config['month'] = month
    location_launched_status = get_location_launched_status(config, loc_level)

    for row in chart_data:
        if location_launched_status:
            launched_status = location_launched_status.get(row['%s_name' %
                                                               loc_level])
            if launched_status is None or launched_status <= 0:
                continue
        date = row['month']
        weighed = row['weighed'] or 0
        location = row['%s_name' % loc_level]
        severely_underweight = row['severely_underweight'] or 0
        moderately_underweight = row['moderately_underweight'] or 0
        total = row['total'] or 0
        normal = row['normal'] or 0

        underweight = (moderately_underweight +
                       severely_underweight) * 100 / float(weighed or 1)

        best_worst[location] = underweight

        date_in_miliseconds = int(date.strftime("%s")) * 1000

        data['peach'][date_in_miliseconds]['y'] += normal
        data['peach'][date_in_miliseconds]['unweighed'] += (total - weighed)
        data['peach'][date_in_miliseconds]['weighed'] += weighed
        data['orange'][date_in_miliseconds]['y'] += moderately_underweight
        data['orange'][date_in_miliseconds]['unweighed'] += (total - weighed)
        data['orange'][date_in_miliseconds]['weighed'] += weighed
        data['red'][date_in_miliseconds]['y'] += severely_underweight
        data['red'][date_in_miliseconds]['unweighed'] += (total - weighed)
        data['red'][date_in_miliseconds]['weighed'] += weighed

    all_locations = [{
        'loc_name': key,
        'percent': value
    } for key, value in best_worst.items()]
    all_locations_sorted_by_name = sorted(all_locations,
                                          key=lambda x: x['loc_name'])
    all_locations_sorted_by_percent_and_name = sorted(
        all_locations_sorted_by_name, key=lambda x: x['percent'])

    return {
        "chart_data": [{
            "values": [{
                'x': key,
                'y': value['y'] / float(value['weighed'] or 1),
                'weighed': value['weighed'],
                'unweighed': value['unweighed'],
            } for key, value in data['peach'].items()],
            "key":
            "% Normal",
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            ChartColors.PINK
        }, {
            "values": [{
                'x': key,
                'y': value['y'] / float(value['weighed'] or 1),
                'weighed': value['weighed'],
                'unweighed': value['unweighed'],
            } for key, value in data['orange'].items()],
            "key":
            "% Moderately Underweight (-2 SD)",
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            ChartColors.ORANGE
        }, {
            "values": [{
                'x': key,
                'y': value['y'] / float(value['weighed'] or 1),
                'weighed': value['weighed'],
                'unweighed': value['unweighed'],
            } for key, value in data['red'].items()],
            "key":
            "% Severely Underweight (-3 SD) ",
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            ChartColors.RED
        }],
        "all_locations":
        all_locations_sorted_by_percent_and_name,
        "top_five":
        all_locations_sorted_by_percent_and_name[:5],
        "bottom_five":
        all_locations_sorted_by_percent_and_name[-5:],
        "location_type":
        loc_level.title()
        if loc_level != LocationTypes.SUPERVISOR else 'Sector'
    }
Beispiel #2
0
def get_prevalence_of_severe_data_chart(domain,
                                        config,
                                        loc_level,
                                        show_test=False,
                                        icds_feature_flag=False):
    month = datetime(*config['month'])
    three_before = datetime(*config['month']) - relativedelta(months=3)

    config['month__range'] = (three_before, month)
    del config['month']
    # using child health monthly while querying for sector level due to performance issues
    if icds_feature_flag and config[
            'aggregation_level'] >= AggregationLevels.SUPERVISOR:
        chm_filter = get_filters_from_config_for_chart_view(config)
        chm_queryset = ChildHealthMonthlyView.objects.filter(**chm_filter)
    else:
        chm_queryset = AggChildHealthMonthly.objects.filter(**config)

    chart_data = chm_queryset.values('month', '%s_name' % loc_level).annotate(
        moderate=Sum(wasting_moderate_column(icds_feature_flag)),
        severe=Sum(wasting_severe_column(icds_feature_flag)),
        normal=Sum(wasting_normal_column(icds_feature_flag)),
        total_height_eligible=Sum('height_eligible'),
        total_weighed=Sum('nutrition_status_weighed'),
        total_measured=Sum(wfh_recorded_in_month_column(icds_feature_flag)),
    ).order_by('month')

    if not show_test:
        chart_data = apply_exclude(domain, chart_data)
    if 'age_tranche' not in config:
        chart_data = chart_data.filter(age_tranche__lt=72)

    data = {
        'red': OrderedDict(),
        'orange': OrderedDict(),
        'peach': OrderedDict()
    }

    dates = [dt for dt in rrule(MONTHLY, dtstart=three_before, until=month)]

    for date in dates:
        miliseconds = int(date.strftime("%s")) * 1000
        data['red'][miliseconds] = {
            'y': 0,
            'total_weighed': 0,
            'total_measured': 0,
            'total_height_eligible': 0
        }
        data['orange'][miliseconds] = {
            'y': 0,
            'total_weighed': 0,
            'total_measured': 0,
            'total_height_eligible': 0
        }
        data['peach'][miliseconds] = {
            'y': 0,
            'total_weighed': 0,
            'total_measured': 0,
            'total_height_eligible': 0
        }

    best_worst = {}
    if icds_feature_flag:
        if 'month' not in config:
            config['month'] = month
        location_launched_status = get_location_launched_status(
            config, loc_level)
    else:
        location_launched_status = None
    for row in chart_data:
        if location_launched_status:
            launched_status = location_launched_status.get(row['%s_name' %
                                                               loc_level])
            if launched_status is None or launched_status <= 0:
                continue
        date = row['month']
        total_weighed = row['total_weighed'] or 0
        total_measured = row['total_measured'] or 0
        total_height_eligible = row['total_height_eligible'] or 0
        location = row['%s_name' % loc_level]
        severe = row['severe'] or 0
        moderate = row['moderate'] or 0
        normal = row['normal'] or 0

        underweight = moderate + severe

        best_worst[location] = underweight * 100 / float(total_measured or 1)

        date_in_miliseconds = int(date.strftime("%s")) * 1000

        data['peach'][date_in_miliseconds]['y'] += normal
        data['peach'][date_in_miliseconds]['total_weighed'] += total_weighed
        data['peach'][date_in_miliseconds]['total_measured'] += total_measured
        data['peach'][date_in_miliseconds][
            'total_height_eligible'] += total_height_eligible
        data['orange'][date_in_miliseconds]['y'] += moderate
        data['orange'][date_in_miliseconds]['total_weighed'] += total_weighed
        data['orange'][date_in_miliseconds]['total_measured'] += total_measured
        data['orange'][date_in_miliseconds][
            'total_height_eligible'] += total_height_eligible
        data['red'][date_in_miliseconds]['y'] += severe
        data['red'][date_in_miliseconds]['total_weighed'] += total_weighed
        data['red'][date_in_miliseconds]['total_measured'] += total_measured
        data['red'][date_in_miliseconds][
            'total_height_eligible'] += total_height_eligible

    top_locations = sorted([
        dict(loc_name=key, percent=value) for key, value in best_worst.items()
    ],
                           key=lambda x: (x['percent'], x['loc_name']))

    return {
        "chart_data": [{
            "values": [{
                'x': key,
                'y': value['y'] / float(value['total_measured'] or 1),
                'total_weighed': value['total_weighed'],
                'total_measured': value['total_measured'],
                'total_height_eligible': value['total_height_eligible']
            } for key, value in data['peach'].items()],
            "key":
            "% normal",
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            ChartColors.PINK
        }, {
            "values": [{
                'x': key,
                'y': value['y'] / float(value['total_measured'] or 1),
                'total_weighed': value['total_weighed'],
                'total_measured': value['total_measured'],
                'total_height_eligible': value['total_height_eligible']
            } for key, value in data['orange'].items()],
            "key":
            "% moderately wasted (moderate acute malnutrition)",
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            ChartColors.ORANGE
        }, {
            "values": [{
                'x': key,
                'y': value['y'] / float(value['total_measured'] or 1),
                'total_weighed': value['total_weighed'],
                'total_measured': value['total_measured'],
                'total_height_eligible': value['total_height_eligible']
            } for key, value in data['red'].items()],
            "key":
            "% severely wasted (severe acute malnutrition)",
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            ChartColors.RED
        }],
        "all_locations":
        top_locations,
        "top_five":
        top_locations[:5],
        "bottom_five":
        top_locations[-5:],
        "location_type":
        loc_level.title()
        if loc_level != LocationTypes.SUPERVISOR else 'Sector'
    }
Beispiel #3
0
def get_early_initiation_breastfeeding_chart(domain, config, loc_level, show_test=False, icds_features_flag=False):
    month = datetime(*config['month'])
    three_before = datetime(*config['month']) - relativedelta(months=3)

    config['month__range'] = (three_before, month)
    del config['month']

    # using child health monthly while querying for sector level due to performance issues
    if icds_features_flag and config['aggregation_level'] >= AggregationLevels.SUPERVISOR:
        chm_filter = get_filters_from_config_for_chart_view(config)
        chm_queryset = ChildHealthMonthlyView.objects.filter(**chm_filter)
    else:
        chm_queryset = AggChildHealthMonthly.objects.filter(**config)
    chart_data = chm_queryset.values(
        'month', '%s_name' % loc_level
    ).annotate(
        birth=Sum('bf_at_birth'),
        in_month=Sum('born_in_month'),
    ).order_by('month')

    if not show_test:
        chart_data = apply_exclude(domain, chart_data)

    data = {
        'blue': OrderedDict()
    }

    dates = [dt for dt in rrule(MONTHLY, dtstart=three_before, until=month)]

    for date in dates:
        miliseconds = int(date.strftime("%s")) * 1000
        data['blue'][miliseconds] = {'y': 0, 'all': 0, 'birth': 0}

    best_worst = {}
    if icds_features_flag:
        if 'month' not in config:
            config['month'] = month
        location_launched_status = get_location_launched_status(config, loc_level)
    else:
        location_launched_status = None

    for row in chart_data:
        if location_launched_status:
            launched_status = location_launched_status.get(row['%s_name' % loc_level])
            if launched_status is None or launched_status <= 0:
                continue
        date = row['month']
        in_month = row['in_month']
        location = row['%s_name' % loc_level]
        birth = row['birth']

        best_worst[location] = (birth or 0) * 100 / float(in_month or 1)

        date_in_miliseconds = int(date.strftime("%s")) * 1000
        data_for_month = data['blue'][date_in_miliseconds]

        data_for_month['all'] += in_month
        data_for_month['birth'] += birth
        data_for_month['y'] = data_for_month['birth'] / float(data_for_month['all'] or 1)

    all_locations = [
        {
            'loc_name': key,
            'percent': val
        }
        for key, val in best_worst.items()
    ]
    all_locations_sorted_by_name = sorted(all_locations, key=lambda x: x['loc_name'])
    all_locations_sorted_by_percent_and_name = sorted(
        all_locations_sorted_by_name, key=lambda x: x['percent'], reverse=True)

    return {
        "chart_data": [
            {
                "values": [
                    {
                        'x': key,
                        'y': val['y'],
                        'all': val['all'],
                        'birth': val['birth']
                    } for key, val in data['blue'].items()
                ],
                "key": "% Early Initiation of Breastfeeding",
                "strokeWidth": 2,
                "classed": "dashed",
                "color": ChartColors.BLUE
            }
        ],
        "all_locations": all_locations_sorted_by_percent_and_name,
        "top_five": all_locations_sorted_by_percent_and_name[:5],
        "bottom_five": all_locations_sorted_by_percent_and_name[-5:],
        "location_type": loc_level.title() if loc_level != LocationTypes.SUPERVISOR else 'Sector'
    }
Beispiel #4
0
def get_immunization_coverage_data_chart(domain,
                                         config,
                                         loc_level,
                                         show_test=False,
                                         icds_features_flag=False):
    month = datetime(*config['month'])
    three_before = datetime(*config['month']) - relativedelta(months=3)

    config['month__range'] = (three_before, month)
    # Retrieving children of age 1-2 years
    config['age_tranche__lte'] = '24'
    del config['month']
    # using child health monthly while querying for sector level due to performance issues
    if config['aggregation_level'] >= AggregationLevels.SUPERVISOR:
        chm_filter = get_filters_from_config_for_chart_view(config)
        chm_queryset = ChildHealthMonthlyView.objects.filter(**chm_filter)
    else:
        chm_queryset = AggChildHealthMonthly.objects.filter(**config)
    chart_data = chm_queryset.values('month', '%s_name' % loc_level).annotate(
        in_month=Sum('fully_immunized_on_time') + Sum('fully_immunized_late'),
        eligible=Sum('fully_immunized_eligible'),
    ).order_by('month')

    if not show_test:
        chart_data = apply_exclude(domain, chart_data)

    data = {
        'blue': OrderedDict(),
    }

    dates = [dt for dt in rrule(MONTHLY, dtstart=three_before, until=month)]

    for date in dates:
        miliseconds = int(date.strftime("%s")) * 1000
        data['blue'][miliseconds] = {'y': 0, 'all': 0, 'in_month': 0}

    best_worst = defaultdict(lambda: {'in_month': 0, 'all': 0})

    if 'month' not in config:
        config['month'] = month
    location_launched_status = get_location_launched_status(config, loc_level)

    for row in chart_data:
        if location_launched_status:
            launched_status = location_launched_status.get(row['%s_name' %
                                                               loc_level])
            if launched_status is None or launched_status <= 0:
                continue
        date = row['month']
        in_month = row['in_month']
        location = row['%s_name' % loc_level]
        valid = row['eligible']

        best_worst[location]['in_month'] = in_month
        best_worst[location]['all'] = (valid or 0)

        date_in_miliseconds = int(date.strftime("%s")) * 1000
        data_for_month = data['blue'][date_in_miliseconds]

        data_for_month['all'] += valid
        data_for_month['in_month'] += in_month
        data_for_month['y'] = data_for_month['in_month'] / float(
            data_for_month['all'] or 1)

    all_locations = [{
        'loc_name':
        key,
        'percent': (value['in_month'] * 100) / float(value['all'] or 1)
    } for key, value in best_worst.items()]
    all_locations_sorted_by_name = sorted(all_locations,
                                          key=lambda x: x['loc_name'])
    all_locations_sorted_by_percent_and_name = sorted(
        all_locations_sorted_by_name, key=lambda x: x['percent'], reverse=True)
    return {
        "chart_data": [{
            "values": [{
                'x': key,
                'y': value['y'],
                'all': value['all'],
                'in_month': value['in_month']
            } for key, value in data['blue'].items()],
            "key":
            "% Children between 1-2 years old who received complete immunizations by 1 year",
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            ChartColors.BLUE
        }],
        "all_locations":
        all_locations_sorted_by_percent_and_name,
        "top_five":
        all_locations_sorted_by_percent_and_name[:5],
        "bottom_five":
        all_locations_sorted_by_percent_and_name[-5:],
        "location_type":
        loc_level.title()
        if loc_level != LocationTypes.SUPERVISOR else 'Sector'
    }