def proposal_countries_map(request, proposal_id): filename = join(settings.MEDIA_DIRECTORY, 'img', 'trends', 'proposal', "%s-countries-map.svg" % proposal_id) cache = get_content_cache(request, filename) if cache: return HttpResponse(cache, mimetype="image/svg+xml") proposal = get_object_or_404(Proposal, id=proposal_id) countries = {} for country in Country.objects.order_by('code'): countries[country.code] = Score.objects.filter(proposal=proposal, representative__mep__countrymep__country=country).aggregate(average_score=Avg('value'))['average_score'] current_country = None out = "" for line in open(join(settings.MEDIA_DIRECTORY, "grey_europe_map.svg"), "r").readlines(): if ' {}' in line: get = re.match('.*([a-z][a-z]).*', line) if get and get.group(1).upper() in countries.keys(): current_country = get.group(1).upper() # HAHAHAHA blam those who can't write a human uzable xml lib for python if current_country: if countries[current_country]: line = re.sub("{}", "{fill:rgb(%s, %s, %s);}" % color(countries[current_country]), line) current_country = None out += line check_dir(filename) open(filename, "w").write(out) return HttpResponse(out, mimetype="image/svg+xml")
def score_color(self): return "rgb(%s, %s, %s)" % color(self.total_score)
def group_proposal_score_heatmap(request, proposal_id): filename = join(settings.MEDIA_DIRECTORY, 'img', 'trends', 'group', "groups-%s-repartition-heatmap.png" % proposal_id) cache = get_content_cache(request, filename) if cache: return cache proposal = get_object_or_404(Proposal, id=proposal_id) countries= proposal.countries groups = proposal.groups fig = pyplot.figure(figsize=(len(countries) / 2.8, len(groups) / 2.8)) ax = fig.add_subplot(111) ax.set_xlim(0, len(proposal.countries)) ax.set_ylim(0, len(groups)) biggest_group_of_a_country = 0 for country in countries: for group in groups: meps = MEP.objects.filter(score__proposal=proposal, groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, countrymep__begin__lte=proposal.date, countrymep__end__gte=proposal.date, countrymep__country=country, groupmep__group=group).distinct().count() if meps > biggest_group_of_a_country: biggest_group_of_a_country = meps biggest_group_of_a_country = float(biggest_group_of_a_country) a = 0 for country in countries: b = 0 for group in groups: meps = MEP.objects.filter(score__proposal=proposal, groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, countrymep__begin__lte=proposal.date, countrymep__end__gte=proposal.date, countrymep__country=country, groupmep__group=group).distinct().count() score = Score.objects.filter(proposal=proposal, representative__mep__groupmep__group=group, representative__mep__groupmep__begin__lte=proposal.date, representative__mep__groupmep__end__gte=proposal.date, representative__mep__countrymep__country=country, representative__mep__countrymep__begin__lte=proposal.date, representative__mep__countrymep__end__gte=proposal.date).aggregate(Avg('value'))['value__avg'] if score: ax.scatter(a + 0.5,b + 0.5, s=250*(meps/biggest_group_of_a_country), c=map(lambda x: x/255., color(score)), alpha=0.5) b += 1 a += 1 #group_bar = {} #scores = [] #maxeu = 0 #for score_range in range(0, 100, 10): #meps = MEP.objects.filter(groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, score__proposal=proposal, score__value__lt=score_range + 10 if score_range != 90 else 101, score__value__gte=score_range).distinct().count() #if meps > maxeu: #maxeu = meps #scores.append(meps) #for mep in MEP.objects.filter(score__proposal=proposal, groupmep__group=group): #for group in proposal.groups: #for score_range in range(0, 100, 10): #meps = group.mep_set.filter(groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, score__proposal=proposal, score__value__lt=score_range + 10 if score_range != 90 else 101, score__value__gte=score_range).distinct().count() #scores[score_range/10] -= meps #group_bar[group.abbreviation] = pyplot.bar(score_range/10 + 0.1, meps + scores[score_range/10], width=0.8, color=group_color.get(group.abbreviation, '#FFFFFF')) #a, b = zip(*group_bar.items()) #pyplot.legend(list(b), list(a), 'best', shadow=False) pyplot.title("Heatmap of group/country on %s" % proposal.short_name if proposal.short_name else proposal.title) #pyplot.xticks(range(11), range(0, 110, 10)) pyplot.xticks(map(lambda x: x + 0.5, range(len(countries))), map(lambda x: x.code, countries)) pyplot.yticks(map(lambda x: x + 0.5, range(len(groups))), map(lambda x: x.abbreviation, groups)) #pyplot.xlabel("Score range 10 by 10") #pyplot.ylabel("MEPs per group") #pyplot.axis([0, 10.1, 0, maxeu + 3]) check_dir(filename) pyplot.savefig(filename, format="png", bbox_inches='tight') pyplot.figure(figsize=(8, 6)) pyplot.clf() return send_file(request, filename, content_type="image/png")
def color_tuple(self): return color(self.value)
def color(self): red, green, _ = color(self.value) return "rgb(%d, %d, 0)" % (red, green)
def group_proposal_score_heatmap(request, proposal_id): filename = join(settings.MEDIA_DIRECTORY, 'img', 'trends', 'group', get_language() + "-groups-%s-repartition-heatmap.png" % proposal_id) cache = get_content_cache(request, filename) if cache: return cache proposal = get_object_or_404(Proposal, id=proposal_id) countries= proposal.countries groups = proposal.groups if proposal.institution != "EU": return HttpResponse("") fig = pyplot.figure(figsize=(len(countries) / 2.8, len(groups) / 2.8)) ax = fig.add_subplot(111) ax.set_xlim(0, len(proposal.countries)) ax.set_ylim(0, len(groups)) biggest_group_of_a_country = 0 for country in countries: for group in groups: meps = MEP.objects.filter(score__proposal=proposal, groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, countrymep__begin__lte=proposal.date, countrymep__end__gte=proposal.date, countrymep__country=country, groupmep__group=group).distinct().count() if not meps: meps = MEP.objects.filter(score__proposal=proposal, countrymep__country=country, groupmep__group=group).distinct().count() if meps > biggest_group_of_a_country: biggest_group_of_a_country = meps biggest_group_of_a_country = float(biggest_group_of_a_country) a = 0.5 for country in countries: b = 0.5 for group in groups: meps = MEP.objects.filter(score__proposal=proposal, groupmep__end__gte=proposal.date, groupmep__begin__lte=proposal.date, countrymep__begin__lte=proposal.date, countrymep__end__gte=proposal.date, countrymep__country=country, groupmep__group=group).distinct().count() if not meps: meps = MEP.objects.filter(score__proposal=proposal, countrymep__country=country, groupmep__group=group).distinct().count() score = Score.objects.filter(proposal=proposal, representative__mep__groupmep__group=group, representative__mep__groupmep__begin__lte=proposal.date, representative__mep__groupmep__end__gte=proposal.date, representative__mep__countrymep__country=country, representative__mep__countrymep__begin__lte=proposal.date, representative__mep__countrymep__end__gte=proposal.date).aggregate(Avg('value'))['value__avg'] if not score: score = Score.objects.filter(proposal=proposal, representative__mep__groupmep__group=group, representative__mep__countrymep__country=country).aggregate(Avg('value'))['value__avg'] if score: ax.scatter(a, b, s=280*(meps/biggest_group_of_a_country), c=map(lambda x: x/255., color(score)), alpha=0.6) b += 1 a += 1 pyplot.title(ugettext("Heatmap of group/country on %(proposal)s") % {"proposal": proposal.short_name if proposal.short_name else proposal.title}) pyplot.xticks(map(lambda x: x + 0.5, range(len(countries))), map(lambda x: x.code, countries)) pyplot.yticks(map(lambda x: x + 0.5, range(len(groups))), map(lambda x: x.abbreviation, groups)) check_dir(filename) pyplot.savefig(filename, format="png", bbox_inches='tight') pyplot.figure(figsize=(8, 6)) pyplot.clf() return send_file(request, filename, content_type="image/png")