http://code.google.com/p/redistricter/source/detail?r=208
Modified:
/trunk/analyze_submissions.py
/trunk/kmppspreadplot.py
=======================================
--- /trunk/analyze_submissions.py Tue Mar 8 11:10:09 2011
+++ /trunk/analyze_submissions.py Thu Mar 10 09:11:01 2011
@@ -428,14 +428,24 @@
out.write('</html></body>\n')
out.close()
- def doDrend(self, cname, data, solutionDszRaw, pngpath):
+ def doDrend(self, cname, data, pngpath, dszpath=None,
solutionDszRaw=None):
args = dict(self.config[cname].drendargs)
- args.update({'--loadSolution': '-', '--pngout': pngpath})
+ args['--pngout'] = pngpath
+ if dszpath:
+ args['--loadSolution'] = dszpath
+ elif solutionDszRaw:
+ args['--loadSolution'] = '-'
+ else:
+ self.stderr.write('error: need dsz or raw dsz bytes for doDrend\n')
+ return None
cmd = [os.path.join(self.options.bindir, 'drend')] +
runallstates.dictToArgList(args)
logging.debug('run %r', cmd)
- p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
shell=False)
- p.stdin.write(solutionDszRaw)
- p.stdin.close()
+ if solutionDszRaw:
+ p = subprocess.Popen(cmd, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, shell=False)
+ p.stdin.write(solutionDszRaw)
+ p.stdin.close()
+ else:
+ p = subprocess.Popen(cmd, stdin=None, stdout=subprocess.PIPE,
shell=False)
retcode = p.wait()
if retcode != 0:
self.stderr.write('error %d running "%s"\n' % (retcode, ' '.join(cmd)))
@@ -581,6 +591,7 @@
points = []
dumpBinLogRE =
re.compile(r'.*kmpp=([.0-9]+).*minPop=([0-9]+).*maxPop=([0-9]+)')
rand = random.Random()
+ bestSpreadPoints = []
for (rid, tpath) in rows:
tpath = self.cleanupSolutionPath(tpath)
data = extractSome(tpath, ('binlog',))
@@ -594,6 +605,7 @@
#raw = p.stdout.read()
(raw, _unused) = p.communicate(data['binlog'])
allpoints = []
+ bestSpreadPoint = None
for line in raw.splitlines():
m = dumpBinLogRE.match(line)
if m:
@@ -602,6 +614,10 @@
maxPop = int(m.group(3))
spread = maxPop - minPop
allpoints.append((spread, kmpp))
+ if (bestSpreadPoint is None) or (spread < bestSpreadPoint[0]):
+ bestSpreadPoint = (spread, kmpp)
+ if bestSpreadPoint:
+ bestSpreadPoints.append(bestSpreadPoint)
if len(allpoints) > 20:
half = len(allpoints) / 2
allpoints = allpoints[half:]
@@ -614,6 +630,8 @@
out = svgplotter(kmpppath)
for (spread, kmpp) in points:
out.xy(spread, kmpp)
+ if bestSpreadPoints:
+ out.comment(repr(bestSpreadPoints))
out.close()
return kmpppath
@@ -672,7 +690,7 @@
# Make images map.png and map500.png
if needsDrend:
- self.doDrend(cname, data, tfparts['solution'], mappath)
+ self.doDrend(cname, data, mappath, dszpath=solpath)
map500path = os.path.join(sdir, 'map500.png')
if newerthan(mappath, map500path):
subprocess.call(['convert', mappath, '-resize', '500x500', map500path])
=======================================
--- /trunk/kmppspreadplot.py Sat Feb 26 12:03:21 2011
+++ /trunk/kmppspreadplot.py Thu Mar 10 09:11:01 2011
@@ -107,6 +107,7 @@
self.yoffset = 30
self.scalex = 1.0
self.scaley = 1.0
+ self.comments = []
def xy(self, x, y):
if (self.minx is None) or (self.minx > x):
@@ -125,6 +126,9 @@
def ty(self, y):
return ((self.maxy - y) * self.scaley) + self.yoffset
+ def comment(self, blah):
+ self.comments.append(blah)
+
def close(self):
if not self.fout:
self.fout = open(self.fname, 'w')
@@ -185,6 +189,8 @@
(minxp + ((maxxp - minxp) * 0.75), minyp + 5, len(self.points)))
self.fout.write('</g>\n')
+ if self.comments:
+ self.fout.write('<!-- ' + '\n'.join(self.comments) + ' -->\n')
self.fout.write('</svg>\n')
self.fout.close()
self.fout = None