-
Notifications
You must be signed in to change notification settings - Fork 1
/
xval_generic.pl
191 lines (159 loc) · 4.79 KB
/
xval_generic.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use Config::Simple;
use File::Path;
use File::Basename;
use Getopt::Long;
$algorithm="svmperftfidf";
$nfold=5;
$seed=1000;
$r = GetOptions (
"Features=s" => \$matrix,
"numFolds=i" => \$nfold,
"randomSeed=i" => \$seed
);
my($filename, $directory) = fileparse($matrix);
$matricesdir=$directory;
$matrixdir=$matricesdir."xval\\matrices\\";
$modeldir=$matricesdir."xval\\models\\";
#$modelcompletedir=$matricesdir."xval\\completemodels\\";
$resultdir=$matricesdir."xval\\results\\";
$evaldir=$matricesdir."xval\\eval\\";
$aggevaldir=$matricesdir."xval\\aggeval\\";
mkpath([$matrixdir,$modeldir,$resultdir,$evaldir,$aggevaldir],1);
$evalmatrix=$evaldir.$filename;
$logfile=$evalmatrix.".XVAL.$nfold.LOG.txt";
open(LOG, ">$logfile") or die "can't open logfile $logfile: $!";
$filesfile=$evalmatrix.".XVAL.$nfold.FILES.txt";
open(FILES, ">$filesfile") or die "can't open logfile $filesfile: $!";
print FILES $matrix."\n";
rand $seed;
@lines=();
open(INPUTFILE, "< $matrix") or die "can't open $matrix: $!";
$samplesize= 0;
while( <INPUTFILE> ) {
$lines[$samplesize]=$samplesize;
$samplesize++;
}
fisher_yates_shuffle( \@lines ); # permutes @array in place
@trainhandles=();
@testhandles=();
@trainfnames=();
@testfnames=();
@modelfnames=();
@resultfnames=();
for ($i = 1; $i <= $nfold; $i++) {
$trainhandle= return_fh();
$testhandle= return_fh();
$trainfname=$matrixdir.$filename."N".$nfold."-".$i."-train.txt";
$testfname=$matrixdir.$filename."N".$nfold."-".$i."-test.txt";
$modelfname=$modeldir.$filename."N".$nfold."-".$i."-model.txt";
$resultfname=$resultdir.$filename."N".$nfold."-".$i."-result.txt";
print FILES "$trainfname,$testfname,$modelfname, $resultfname\n";
$trainhandles[$i]=$trainhandle;
$testhandles[$i]=$testhandle;
$trainfnames[$i]=$trainfname;
$testfnames[$i]=$testfname;
$modelfnames[$i]=$modelfname;
$resultfnames[$i]=$resultfname;
open ($trainhandle, ">$trainfname") or die "can't open trainfname: $!";
open ($testhandle, ">$testfname") or die "can't open testfname: $!";
}
@selection = (); $thisfold = 0; $accu = 0;
open(INPUTFILE, "< $matrix") or die "can't open $matrix: $!";
$linecount= 0;
while( $line=<INPUTFILE> ) {
$bin=$linecount%$nfold+1;
$linecount++;
for ($i = 1; $i <= $nfold; $i++) {
if ($i==$bin) {
$fh=$testhandles[$i];
print $fh $line;
}
else {
$fh=$trainhandles[$i];
print $fh $line;
}
}
}
for ($i = 1; $i <= $nfold; $i++) {
$trainfname=$trainfnames[$i];
$testfname=$testfnames[$i];
$modelfname=$modelfnames[$i];
$resultfname=$resultfnames[$i];
$traincmd ="start /wait /b svm_perf_learn.exe -c 100 -l 10 -w 3 \"$trainfname\" \"$modelfname\" ";
$testcmd="start /wait /realtime /b svm_perf_classify.exe \"$testfname\" \"$modelfname\" \"$resultfname\" ";
$a=`$traincmd`;
$b=`$testcmd`;
}
$collatorcommand="start /wait /b /high perl evalcollator6.pl \"$resultdir\\\"";
print $collatorcommand ."\n";
$collatorresult=`$collatorcommand`;
print LOG "$collatorcommand\n$collatorresult\n";
exit;
#$samplesize=@files;
for ($i = 0; $i < $samplesize; $i++) {
$ir = rand $samplesize;
push @selection, $ir;
splice(@files, $ir, 1); }
foreach $f ( @selection ) { $fold{$f} =
$thisfold++ % $nfold; }
for ( $cf = 0; $cf < 10; $cf++ ) {
%train = (); %test = (); $count = ();
foreach $f ( @selection ) {
($cl{$f}, $name) = split(/\//, $f);
$ref = ($fold{$f}==$cf) ? \%test :
\%train;
open(FILE, $f); while(<FILE>) {
@words = split(/\s+/, $_);
foreach $w ( @words ) { ${$ref}
{$w}++; $count{$f}{$w}++; } }
close(FILE); }
@wlist = sort { $train{$b} <=>
$train{$a} } ( keys %train );
splice(@wlist, N);
open(TR, ">tr$cf"); open(TE, ">te$cf");
foreach $f ( @selection ) {
$handle = ($fold{$f}==$cf) ? TE : TR;
print $handle join(',', map
{$count{f}{$_}+0} @wlist).",$cl{$f}\n"; }
close(TR); close(TE);
$accu += `learner tr$cf te$cf`; }
print "Accuracy: ".($accu/10)."\n";
exit;
# fisher_yates_shuffle( \@array ) : generate a random permutation
# of @array in place
sub fisher_yates_shuffle {
my $array = shift;
my $i;
for ($i = @$array; --$i; ) {
my $j = int rand ($i+1);
next if $i == $j;
@$array[$i,$j] = @$array[$j,$i];
}
}
sub return_fh { # make anon filehandle
local *FH; # must be local, not my
# now open it if you want to, then...
return *FH;
}
sub runCommand {
my $command=shift;
my $exe;
my $subst;
my $pl;
my $ret;
if ($command=~/(perl\s+((.*?).pl))/ig) {
$subst=$1;
$pl=$2;
$exe=$3.".exe";
if (!(-e $exe)) {
if (-e "perl2exe\\perl2exe.exe") {
# $ret=`perl2exe\\perl2exe.exe $pl`;
}
}
if (-e $exe) {
$command=~s/$subst/$exe/ig;
}
}
$ret=`$command`;
return $ret;
}