#!/usr/bin/env python
# v0.1 6 October, 2008
import sys, re
import simpleSVG
from optparse import OptionParser
usage="usage: %prog INFILE SVGFILE [options]"
parser = OptionParser(usage=usage, version="%prog 0.1")
parser.add_option("--x1", dest="x1",type="float",help="lower limit to x-axis",default= 0.0)
parser.add_option("--x2", dest="x2",type="float",help="upper limit to x-axis")
parser.add_option("--y1", dest="y1",type="float",help="lower limit to y-axis",default=0)
parser.add_option("--y2", dest="y2",type="float",help="upper limit to y-axis")
parser.add_option("-t", dest="title",type="string",help="title")
parser.add_option("--xl", dest="xlab",type="string",help="x-axis label")
parser.add_option("--yl", dest="ylab",type="string",help="y-axis label")
(opt, args) = parser.parse_args()
try:
	infilename=args[0]
except:
	print usage
	sys.exit();
try:
	svgfile=args[1]
except:
	svgfile="out.svg"
#sys.exit()
inf=open(infilename,'r')
alpha=[]
train=[]
veri=[]
iter=[]
while 1:
	aline=inf.readline().strip()
	if not aline: break
	if re.match('\#',aline): continue
	[i,a,t,v,blank]=[float(x) for x in aline.split()]
	iter.append(i)
	alpha.append(a)
	train.append(t)
	veri.append(v)
b=simpleSVG.svg_class(fname=svgfile,bbx=800,bby=400)
b.group(fill='black')
xmin=opt.x1
ymin=opt.y1
if opt.x2:
	xmax=opt.x2
else:
	xmax=float(iter[-1])
if opt.y2:
	ymax=opt.y2
else:
	ymax=alpha[0]
b.scale(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,leftmarg=80,rightmarg=20)
b.xaxis(form='%d')
b.yaxis(form='%5.2f',dy=ymax/5.)
#plot points
b.group(clip_path=r'url(#marginmask)',stroke_width=.5)
b.draw(zip(iter,alpha),stroke_dasharray="2 3",fill='none')
b.draw(zip(iter,train),stroke='red',fill='none')
b.draw(zip(iter,veri),stroke='green',fill='none')
b.group()
#make legend:
b.draw([(.7j,.2j),(.8j,.2j)],stroke_dasharray="2 3",fill='none')
b.text(.81j,.2j,0.,'alpha',font_size="14pt")
b.draw([(.7j,.3j),(.8j,.3j)],stroke='red',fill='none')
b.text(.81j,.3j,0.,'training',font_size="14pt")
b.draw([(.7j,.4j),(.8j,.4j)],stroke='green',fill='none')
b.text(.81j,.4j,0.,'verification',font_size="14pt")
#make labels:
if opt.title: b.text(.56j, .07j,0.,opt.title,font_size="24pt",text_anchor="middle")
if opt.xlab: b.text(.5j,.98j,0.,opt.xlab,font_size="14pt")
if opt.ylab: b.text(.05j,.5j,90.,opt.ylab,font_size="14pt")
b.close()
