#!/usr/bin/perl -w
# v0.2 2 October 2008
use Getopt::Long;
use vars qw/$opt_help $opt_batch $opt_i $opt_o $opt_a $opt_b $opt_c
          $opt_xlab $opt_ylab $opt_t $opt_xmax $opt_ymax $opt_alpha/;
GetOptions("help" , "batch", "alpha", "i:s", "o:s" , "a:s" , "b:s",
            "c:s" , "xlab:s", "ylab:s" , "t:s", "xmax:i", "ymax:f");
if (defined $opt_help){
  print "usage example:
mseplot.pl -i mse.dat -o mse.png -a training -b verification -alpha 
";
exit(0);  
}
$opt_i='nn_mse.dat' unless defined($opt_i);
$opt_o='mse.png' unless defined($opt_o);
#The fact that nn_b.dat exits means the data in the corresponding 
#column is worthy to plot (i.e., not zero).  This is slimey way of
#making that decision...
(-e 'nn_a.dat') and $opt_a='filea' unless defined($opt_a);
(-e 'nn_b.dat') and $opt_b='fileb' unless defined($opt_b);
(-e 'nn_c.dat') and $opt_c='filec' unless defined($opt_c);
$opt_xlab='training passes' unless defined($opt_xlab);
$opt_ylab='m.s.e.' unless defined($opt_ylab);
$opt_t='m.s.e. history' unless defined($opt_t);
$opt_ymax=0.5 unless defined($opt_ymax);
$commands="#!/usr/bin/gnuplot 
set term png giant size 800,600 
set output '$opt_o'
set data style lines
set yrange[0:$opt_ymax]
set title '$opt_t'
set xlab '$opt_xlab'
set ylab '$opt_ylab'
set bmargin 4
";  
$commands.="set xrange[0:$opt_xmax]\n" if defined $opt_xmax;
$commands.="plot ";
$commands.=" '$opt_i' using 1:2 t 'alpha' with lines 4," if defined $opt_alpha;
$commands.=" '$opt_i' using 1:3 t '$opt_a' with lines 1," if defined $opt_a;
$commands.=" '$opt_i' using 1:4 t '$opt_b' with lines 2," if defined $opt_b;
$commands.=" '$opt_i' using 1:5 t '$opt_c' with lines 3," if defined $opt_c;
 
chop($commands);
$commands.="\n";
open GNU,">temp.gnu";
print GNU $commands; 
close GNU;
system('gnuplot temp.gnu');
system("display $opt_o") unless $opt_batch;
