# -*- 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()
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]:
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):
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):
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_yes = ""
out_abs = ""
out_no = ""
for vote in profile:
for point in sorted(profile[vote]):
if vote == 0:
out_yes = out_yes + str( point * 10 ) + "\t" + str( profile[vote][point] ) + "\n"
if vote == 1:
out_abs = out_abs + str( point + 10 ) + "\t" + str( profile[vote][point] * 10 ) + "\n"
if vote == 2:
out_no = out_no + str( point * 10 + 11 ) + "\t" + str( profile[vote][point] ) + "\n"
f = open( "yes.data", "w" )
f.write( out_yes )
f.close()
f = open( "abs.data", "w" )
f.write( out_abs )
f.close()
f = open( "no.data", "w" )
f.write( out_no )
f.close()
f = open( "plot.plt", "w" )
f.write( """
reset
set title '""" + candidates[candidate] + """'
set terminal png
set xrange [0:21]
set nokey
set yrange [0:86]
set label "Ja" at 4.9,82
set label "Enth." at 9.8,82
set label "Nein" at 15.9,82
set output 'stimmenprofil-praeferenzwahl-avb13-""" + candidate + """.png'
plot \
'yes.data' with filledcurves x1 fs lc rgb "blue", \
'abs.data' with filledcurves x1 fs lc rgb "grey", \
'no.data' with filledcurves x1 fs lc rgb "red"
""" )
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 )