def get_prevalence_of_undernutrition_data_map(domain, config, loc_level, show_test=False, icds_features_flag=False): config['month'] = datetime(*config['month']) def get_data_for(filters): queryset = AggChildHealthMonthly.objects.filter(**filters).values( '%s_name' % loc_level, '%s_map_location_name' % loc_level).annotate( moderately_underweight=Sum( 'nutrition_status_moderately_underweight'), severely_underweight=Sum( 'nutrition_status_severely_underweight'), normal=Sum('nutrition_status_normal'), weighed=Sum('nutrition_status_weighed'), total=Sum('wer_eligible'), ).order_by('%s_name' % loc_level, '%s_map_location_name' % loc_level) if not show_test: queryset = apply_exclude(domain, queryset) if 'age_tranche' not in config: queryset = queryset.filter(age_tranche__lt=72) return queryset data_for_map = defaultdict( lambda: { 'moderately_underweight': 0, 'severely_underweight': 0, 'normal': 0, 'weighed': 0, 'total': 0, 'original_name': [] }) moderately_underweight_total = 0 severely_underweight_total = 0 normal_total = 0 all_total = 0 weighed_total = 0 values_to_calculate_average = {'numerator': 0, 'denominator': 0} location_launched_status = get_location_launched_status(config, loc_level) for row in get_data_for(config): 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 weighed = row['weighed'] or 0 total = row['total'] or 0 name = row['%s_name' % loc_level] on_map_name = row['%s_map_location_name' % loc_level] or name severely_underweight = row['severely_underweight'] or 0 moderately_underweight = row['moderately_underweight'] or 0 normal = row['normal'] or 0 values_to_calculate_average[ 'numerator'] += moderately_underweight if moderately_underweight else 0 values_to_calculate_average[ 'numerator'] += severely_underweight if severely_underweight else 0 values_to_calculate_average['denominator'] += weighed if weighed else 0 moderately_underweight_total += moderately_underweight severely_underweight_total += severely_underweight normal_total += normal all_total += total weighed_total += weighed data_for_map[on_map_name][ 'severely_underweight'] += severely_underweight data_for_map[on_map_name][ 'moderately_underweight'] += moderately_underweight data_for_map[on_map_name]['normal'] += normal data_for_map[on_map_name]['total'] += total data_for_map[on_map_name]['weighed'] += weighed data_for_map[on_map_name]['original_name'].append(name) for data_for_location in data_for_map.values(): numerator = data_for_location[ 'moderately_underweight'] + data_for_location[ 'severely_underweight'] value = numerator * 100 / (data_for_location['weighed'] or 1) if value < 20: data_for_location.update({'fillKey': '0%-20%'}) elif 20 <= value < 35: data_for_location.update({'fillKey': '20%-35%'}) elif value >= 35: data_for_location.update({'fillKey': '35%-100%'}) fills = OrderedDict() fills.update({'0%-20%': MapColors.PINK}) fills.update({'20%-35%': MapColors.ORANGE}) fills.update({'35%-100%': MapColors.RED}) fills.update({'Not Launched': MapColors.GREY}) fills.update({'defaultFill': MapColors.GREY}) average = ((values_to_calculate_average['numerator'] * 100) / float(values_to_calculate_average['denominator'] or 1)) gender_label, age_label, chosen_filters = chosen_filters_to_labels( config, default_interval='0 - 5 years') return { "slug": "moderately_underweight", "label": "Percent of Children{gender} Underweight ({age})".format( gender=gender_label, age=age_label), "fills": fills, "rightLegend": { "average": format_decimal(average), "info": underweight_children_help_text(age_label=age_label, html=True), "extended_info": [{ 'indicator': 'Total Children{} weighed in given month:'.format( chosen_filters), 'value': indian_formatted_number(weighed_total) }, { 'indicator': 'Number of children unweighed{}:'.format(chosen_filters), 'value': indian_formatted_number(all_total - weighed_total) }, { 'indicator': '% Severely Underweight{}:'.format(chosen_filters), 'value': '%.2f%%' % (severely_underweight_total * 100 / float(weighed_total or 1)) }, { 'indicator': '% Moderately Underweight{}:'.format(chosen_filters), 'value': '%.2f%%' % (moderately_underweight_total * 100 / float(weighed_total or 1)) }, { 'indicator': '% Normal{}:'.format(chosen_filters), 'value': '%.2f%%' % (normal_total * 100 / float(weighed_total or 1)) }] }, "data": dict(data_for_map) }
def test_fucntion(x): return format_decimal(x) if x else DATA_NOT_ENTERED
def get_prevalence_of_undernutrition_data_map(domain, config, loc_level, show_test=False): def get_data_for(filters): filters['month'] = datetime(*filters['month']) queryset = AggChildHealthMonthly.objects.filter( **filters ).values( '%s_name' % loc_level, '%s_map_location_name' % loc_level ).annotate( moderately_underweight=Sum('nutrition_status_moderately_underweight'), severely_underweight=Sum('nutrition_status_severely_underweight'), normal=Sum('nutrition_status_normal'), weighed=Sum('nutrition_status_weighed'), total=Sum('wer_eligible'), ).order_by('%s_name' % loc_level, '%s_map_location_name' % loc_level) if not show_test: queryset = apply_exclude(domain, queryset) if 'age_tranche' not in config: queryset = queryset.exclude(age_tranche=72) return queryset data_for_map = defaultdict(lambda: { 'moderately_underweight': 0, 'severely_underweight': 0, 'normal': 0, 'weighed': 0, 'total': 0, 'original_name': [] }) moderately_underweight_total = 0 severely_underweight_total = 0 normal_total = 0 all_total = 0 weighed_total = 0 values_to_calculate_average = {'numerator': 0, 'denominator': 0} for row in get_data_for(config): weighed = row['weighed'] or 0 total = row['total'] or 0 name = row['%s_name' % loc_level] on_map_name = row['%s_map_location_name' % loc_level] or name severely_underweight = row['severely_underweight'] or 0 moderately_underweight = row['moderately_underweight'] or 0 normal = row['normal'] or 0 values_to_calculate_average['numerator'] += moderately_underweight if moderately_underweight else 0 values_to_calculate_average['numerator'] += severely_underweight if severely_underweight else 0 values_to_calculate_average['denominator'] += weighed if weighed else 0 moderately_underweight_total += moderately_underweight severely_underweight_total += severely_underweight normal_total += normal all_total += total weighed_total += weighed data_for_map[on_map_name]['severely_underweight'] += severely_underweight data_for_map[on_map_name]['moderately_underweight'] += moderately_underweight data_for_map[on_map_name]['normal'] += normal data_for_map[on_map_name]['total'] += total data_for_map[on_map_name]['weighed'] += weighed data_for_map[on_map_name]['original_name'].append(name) for data_for_location in six.itervalues(data_for_map): numerator = data_for_location['moderately_underweight'] + data_for_location['severely_underweight'] value = numerator * 100 / (data_for_location['weighed'] or 1) if value < 20: data_for_location.update({'fillKey': '0%-20%'}) elif 20 <= value < 35: data_for_location.update({'fillKey': '20%-35%'}) elif value >= 35: data_for_location.update({'fillKey': '35%-100%'}) fills = OrderedDict() fills.update({'0%-20%': MapColors.PINK}) fills.update({'20%-35%': MapColors.ORANGE}) fills.update({'35%-100%': MapColors.RED}) fills.update({'defaultFill': MapColors.GREY}) average = ( (values_to_calculate_average['numerator'] * 100) / float(values_to_calculate_average['denominator'] or 1) ) gender_label, age_label, chosen_filters = chosen_filters_to_labels(config, default_interval='0 - 5 years') return { "slug": "moderately_underweight", "label": "Percent of Children{gender} Underweight ({age})".format( gender=gender_label, age=age_label ), "fills": fills, "rightLegend": { "average": format_decimal(average), "info": underweight_children_help_text(age_label=age_label, html=True), "extended_info": [ { 'indicator': 'Total Children{} weighed in given month:'.format(chosen_filters), 'value': indian_formatted_number(weighed_total) }, { 'indicator': 'Number of children unweighed{}:'.format(chosen_filters), 'value': indian_formatted_number(all_total - weighed_total) }, { 'indicator': '% Severely Underweight{}:'.format(chosen_filters), 'value': '%.2f%%' % (severely_underweight_total * 100 / float(weighed_total or 1)) }, { 'indicator': '% Moderately Underweight{}:'.format(chosen_filters), 'value': '%.2f%%' % (moderately_underweight_total * 100 / float(weighed_total or 1)) }, { 'indicator': '% Normal{}:'.format(chosen_filters), 'value': '%.2f%%' % (normal_total * 100 / float(weighed_total or 1)) } ] }, "data": dict(data_for_map) }