Benutzer:Jorges/Präferenzprofile/Sourcecode
evaluate.py -- als Parameter braucht es candidatesfile und ballotsfile, die gibt es hier: BE:Parteitag/2011.1/Rohdaten. Weiterhin muss Gnuplot installiert sein, und natürlich Python.
# -*- coding: utf-8 -*- import sys import os import numpy def getCandidates( filename ): candidates = {} f = open( filename, "r") for line in f: ( key, name ) = line.split("#") candidates[key.strip()] = name.strip() #candidates.append(line.strip()) f.close() return candidates def getBallots( filename ): ballots = [] f = open( filename, "r") for line in f: ballot = [] yes_abs_no = line.split("/") for i in range(3): prefstrings = yes_abs_no[i].split(";") # delete last prefstring in no-area - it is the ballot number if i == 2: prefstrings.pop(len(prefstrings)-1) prefs = [] for prefstring in prefstrings: prefs.append(prefstring.split(",")) ballot.append( prefs ) ballots.append(ballot) f.close() return ballots def getPref(candidate, ballot): # vote = 0,1,2 <=> yes,abs,no # return (vote, pref, prefCount) # iterate over yes,abs,no for i in range(len(ballot)): # iterate over prefs for j in range(len(ballot[i])): # iterate over sameprefs for k in range(len(ballot[i][j])): if candidate == ballot[i][j][k]: #print ballot[i] return ( i, j+1, len(ballot[i]) ) # if candidate not in ballot: count as abstention return (1,1,1) def getPoints(): points = [] for j in range(2,12): #print list(numpy.linspace(0, 10, num=j)) points = points + list(numpy.linspace(0, 1, num=j)) points = list(set(points)) # create also endpoints endpoints = [] for point in points: if point > 0: endpoints.append(point-0.001) points = points + endpoints #for point in sorted(points): # print point return points def profile(candidates, ballots): #candidates = {"thiel": "..name.."} #candidates = {"magalski": "..name.."} for candidate in candidates.keys(): # init profile profile = { 0: dict.fromkeys( getPoints(), 0 ), 1: dict.fromkeys( getPoints(), 0 ), 2: dict.fromkeys( getPoints(), 0 ) } #ballots = [ballots[1]] for ballot in ballots: ( vote, pref, prefCount ) = getPref( candidate, ballot ) # get start and end of area to increase start, end = (pref-1)/float(prefCount), pref/float(prefCount) #print ( vote, pref, prefCount ), start, end, float(1)/prefCount for point in profile[vote]: if point >= start and point < end: profile[vote][point] = profile[vote][point] + prefCount/float(10) out = "" for vote in profile: #print profile[vote] #continue for point in sorted(profile[vote]): if vote == 0: out = out + str( point * 10 ) + "\t" + str( profile[vote][point] ) + "\n" if vote == 1: out = out + str( point + 10 ) + "\t" + str( profile[vote][point] * 10 ) + "\n" if vote == 2: out = out + str( point * 10 + 11 ) + "\t" + str( profile[vote][point] ) + "\n" #print out #return f = open( "plot.data", "w" ) f.write( out ) f.close() f = open( "plot.plt", "w" ) f.write( """ reset set title '""" + candidates[candidate] + """' set terminal png set xrange [0:21] #set notics #set xtics nomirror #set border 0 set nokey set yrange [0:26] set label "Ja" at 4.9,24 set label "Enth." at 9.8,24 set label "Nein" at 15.9,24 set output 'stimmenprofil-praeferenzwahl-ahw11-""" + candidate + """.png' plot 'plot.data' with filledcurves x1 fs lc rgb "red", 'bg-abstention.data' with filledcurves x1 fs lc rgb "#c0c0c0" """ ) f.close() os.system("gnuplot plot.plt") if len(sys.argv) != 3: print "Usage:", sys.argv[0], "candidatesfile", "ballotsfile" exit() candidates = getCandidates( sys.argv[1] ) ballots = getBallots( sys.argv[2] ) profile(candidates, ballots )
bg-abstention.data (für den grauen Hintergrund bei Enthaltung
10 26 10.01 0 10.99 0 11 26