-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogplot.pl
executable file
·81 lines (75 loc) · 1.9 KB
/
logplot.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use Statistics::R;
use List::Util qw'min max';
my @logs = @ARGV;
my $R = Statistics::R->new;
my @names;
{
my $i = 0;
VARNAMES: while(1) {
my @c;
for my $j ( 0 .. $#logs ) {
push @c, substr $logs[$j], $i, 1;
}
my %c = map { $_ => 1 } @c;
if ( scalar(keys(%c)) != 1 ) {
for my $j ( 0 .. $#logs ) {
$names[$j] .= $c[$j] if $c[$j];
}
}
$i++;
my ($longest) = sort { length($b) <=> length($a) } @logs;
last VARNAMES if $i == length($longest);
}
}
my $minIter = 1;
my $maxIter = 0;
my $minLh = 0;
my $maxLh = 0;
for my $i ( 0 .. $#logs ) {
my $data = read_log($logs[$i]);
$R->set( $names[$i], $data );
$maxIter = scalar(@$data) if scalar(@$data) > $maxIter;
my $min = min @$data;
my $max = max @$data;
$minLh = $min if $min < $minLh;
$maxLh = $max if $max > $maxLh or $maxLh == 0;
}
my $n = scalar(@logs);
my $iterations = [ $minIter .. $maxIter ];
$R->set( 'iterations', $iterations );
$R->set( 'xminmax', [ $minIter, $maxIter ] );
$R->set( 'yminmax', [ $minLh, $maxLh ] );
$R->set( 'names', \@names );
$R->run(q`xrange <- range(xminmax)`);
$R->run(q`yrange <- range(yminmax)`);
$R->run(q`plot(xrange, yrange, type="n", xlab="Iterations", ylab="Likelihood")`);
$R->run(qq`colors <- rainbow($n)`);
$R->run(qq`plotchar <- seq(18,18+$n,1)`);
for my $i ( 0 .. $#names ) {
my $ri = $i + 1; # 1-based indexing
my $name = $names[$i];
$name =~ s/"//g;
my $command = "lines(iterations, $name, type=\"l\", lwd=1.5, lty=1, col=colors[$ri])";
$R->run($command);
}
$R->run(q`legend(xrange[1], yrange[2], names, cex=0.8, col=colors, lty=1, title="Hypothesis")`);
system('open Rplots.pdf');
sub read_log {
my $file = shift;
my @Lh;
my $iterflag;
open my $fh, '<', $file or die $!;
while(<$fh>) {
chomp;
if ( $iterflag ) {
my ( $i, $Lh ) = split /\t/, $_;
push @Lh, $Lh;
}
$iterflag++ if /^Iteration\b/;
}
return \@Lh;
}