File: heri-eval

package info (click to toggle)
herisvm 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 328 kB
  • sloc: sh: 1,471; ruby: 615; makefile: 28
file content (412 lines) | stat: -rwxr-xr-x 11,064 bytes parent folder | download
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!/usr/bin/env bash

# Copyright (c) 2015 Alexandra Figlovskaya <fglval@gmail.com>
# Copyright (c) 2015-2019 Aleksey Cheusov <vle@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# variables settable by user
: ${SVM_TRAIN_CMD:=svm-train}
: ${SVM_PREDICT_CMD:=svm-predict}

: ${SVM_HERI_STAT_CMD:=heri-stat}
: ${SVM_HERI_STAT_ADDONS_CMD:=heri-stat-addons}
: ${SVM_HERI_SPLIT_CMD:=heri-split}

: ${TMPDIR:=/tmp}

############################################################
set -e
export LC_ALL=C

indent2 (){
    sed '/./ s/^/  /' "$@"
}

sig_handler (){
    on_exit
    trap - "$1"
    kill -"$1" $$
}

on_exit(){
    show_stderr
    if test -z "$keep_tmp"; then
	if test -n "$tmp_dir"; then
	    rm -rf "$tmp_dir"
	fi
    else
	echo "Temporary files are here $tmp_dir" 1>&2
    fi
}

calculate_feature_count (){
    awk '{
	for (i=2; i <= NF; ++i) {
	    if ($i + 0 > m)
		m = $i + 0
	}
    }
    END {
	print m+1
    }' "$@"
}

calculate_feature_count (){
    awk '{
	for (i=2; i <= NF; ++i) {
	    if ($i + 0 > m)
		m = $i + 0
	}
    }
    END {
	print m+1
    }' "$@"
}

predictions_from_testing_sets (){
    if ! test -s "$tmp_dir/testing_fold.txt"; then
	cat "$tmp_dir/outcome_and_prediction1.txt"
	return
    fi

    awk '
    FNR == NR {
	# reading testing_fold.txt
	++obj_num[$1]
	testobj[$1,obj_num[$1]] = NR
	next
    }

    # reading predictions on testing folds
    FNR == 1 {
	++fold_num
    }

    {
	idx = testobj[fold_num, FNR]
	prediction [idx] = $0
    }

    END {
	if ((NR % 2) != 0){
	    print "internal error!" > "/dev/stderr"
	    exit 12
	}
	count = NR/2
	for (i=1; i <= count; ++i){
	    print prediction [i]
	}
    }' "$tmp_dir/testing_fold.txt" $prediction_all
}

show_stderr (){
    if test -z "$last"; then
	return
    fi
    for i in `seq $last`; do
	#
	fn="$tmp_dir/train_stderr${i}.txt"
	if test -s "$fn"; then
	    echo "---- train stderr $i ----" 1>&2
	    cat -- "$fn" 1>&2
	fi
	#
	fn="$tmp_dir/predict_stderr${i}.txt"
	if test -s "$fn"; then
	    echo "---- predict stderr $i ----" 1>&2
	    cat -- "$fn" 1>&2
	fi
    done
}

wait_all (){
    local i
    local ex
    ex=0
    for i in `seq $last`; do
	if wait ${pid[$i]}; then
	    :
	else
	    ex=$?
	fi
    done
    return "$ex"
}

#    heri-eval -t10 -n 5 dataset.libsvm          # 10*5-fold cross-validation
usage(){
    cat 1>&2 <<'EOF'
usage: heri-eval [OPTIONS] training_set [-- SVM_TRAIN_OPTIONS]

OPTIONS:
      -h                Help message

      -n <N>            The number of folds for T*N-fold cross-validation
      -r <ratio>        The ratio (in percents) for training set for hold-out
      -e testing_set    Testing set for hold-out

      -t <T>            The number of runs for T*N-fold cross-validation
                        or holdouts

      -T <threshold>    Threshold for score

      -o <filename>     Save predictions from testing sets
                        to the specified file
                        (outcome_tag prediction_tag [score])
      -O <filename>     Save incorrectly classified objects
                        to the specified file
                        (#object_number: outcome_tag prediction_tag [score])
      -m <filename>     Save confusion matrix to the specified file
                        (frequency : outcome_tag prediction_tag)

      -f                Enable output of per-fold statistics (see -Mf)
      -M <chars>        Output mode:
                           t -- output total statistics,
                           f -- output per-fold statistics,
                           c -- output cross-fold statistics.
      -s <split_opts>   Options passed to heri-split(1)
      -p <stat_opts>    Options passed to heri-stat(1)
      -S <seed>         Seed value passed to heri-split(1).
                        If it is not specified, the dataset is splitted
                        into training and testing datasets randomly.
      -K                Keep temporary directory after exiting
      -D                Debugging mode, implies -K

SVM_TRAIN_OPTIONS: options passed to svm-train(1) and alike

Environment variables:
  SVM_TRAIN_CMD   -- training utility, e.g., liblinear-train
                     (the default is svm-train)
  SVM_PREDICT_CMD -- predicting utility, e.g., liblinear-predict
                     (the default is svm-predict)
  TMPDIR          -- temporary directory (the default is /tmp)

Examples: 
  Ex1: heri-eval -e testing_set.libsvm training_set.libsvm -- -s 0 -t 0
  Ex2: export SVM_TRAIN_CMD='liblinear-train'
       export SVM_PREDICT_CMD='liblinear-predict'
       heri-eval -p '-mr' -n 5 training_set.libsvm -- -s 4 -q
       heri-eval -p '-mr' -n 5 training_set.libsvm -- -s 4 -q

  Ex3: export SVM_TRAIN_CMD='scikit_rf-train --estimators=400'
       export SVM_PREDICT_CMD='scikit_rf-predict'
       heri-eval -p '-c' -Mt -t 50 -r 70 dataset.libsvm
EOF
}

seed=$RANDOM
runs=1
output_mode=tc
times=1
while getopts De:fhKm:M:n:o:O:p:r:s:S:t:T: f; do
    case "$f" in
	'?')
	    usage
	    exit 1;;
	h)
	    usage
	    exit 0;;
	n)
	    number_of_folds="$OPTARG";;
	e)
	    testing_set="$OPTARG";;
	t)
	    times="$OPTARG";;
	T)
	    heristat_args="$heristat_args -t$OPTARG";;
	r)
	    ratio="$OPTARG";;
	m)
	    confusion_matrix="$OPTARG";;
	o)
	    predictions="$OPTARG";;
	O)
	    incorrect_predictions="$OPTARG";;
	s)
	    herisplit_args="$herisplit_args $OPTARG";;
	p)
	    heristat_args="$heristat_args $OPTARG";;
	f)
	    output_mode="f$output_mode";;
	M)
	    output_mode="$OPTARG";;
	S)
	    seed="$OPTARG";;
	K)
	    keep_tmp=1;;
	D)
	    keep_tmp=1
	    debug=1;;
    esac
done
shift `expr $OPTIND - 1`

while test "$#" -gt 0; do
    case "$1" in
	--)
	    shift
	    break;;
	*)
	    print_sh=`printf '%q' "$1"`
	    files="$files $print_sh"
	    shift;;
    esac
done

trap "sig_handler INT"  INT
trap "on_exit" 0

if test -z "$number_of_folds" -a -z "$testing_set" -a -z "$ratio"; then
    echo 'Either -n or -r or -e must be specified, run heri-eval -h for details' 1>&2
    exit 1
fi

if test -z "$files"; then
    echo 'Training set is mandatory, run heri-eval -h for details' 1>&2
    exit 1
fi

tmp_dir=`mktemp -d "$TMPDIR"/svm.XXXXXX`

training_testing (){
    if test -n "$number_of_folds"; then
	${SVM_HERI_SPLIT_CMD} $herisplit_args -c "$number_of_folds" -d "$tmp_dir" -s "$seed" $files
	seed="$((seed+1))"
	last="$number_of_folds"
	for i in `seq $number_of_folds`; do
	    mv "$tmp_dir/test$i.txt" "$tmp_dir/test$i.libsvm"
	    mv "$tmp_dir/train$i.txt" "$tmp_dir/train$i.libsvm"
	done
    elif test -n "$ratio"; then
	${SVM_HERI_SPLIT_CMD} $herisplit_args -R "$ratio" -d "$tmp_dir" -s "$seed" $files
	mv "$tmp_dir/test.txt" "$tmp_dir/test1.libsvm"
	mv "$tmp_dir/train.txt" "$tmp_dir/train1.libsvm"
	rm "$tmp_dir/testing_fold.txt"
	seed="$((seed+1))"
	last=1
    else
	eval "cat -- $files" > "$tmp_dir/train1.libsvm"
	cp "$testing_set" "$tmp_dir/test1.libsvm"
	last=1
    fi

    for i in `seq $last`; do
	${SVM_TRAIN_CMD} "$@" "$tmp_dir/train$i.libsvm" "$tmp_dir/model$i.bin" \
	    2> "$tmp_dir/train_stderr${i}.txt" \
	    >  "$tmp_dir/train_stdout${i}.txt" &
	pid[$i]=$!
    done

    wait_all

    for i in `seq $last`; do
	${SVM_PREDICT_CMD} "$tmp_dir/test$i.libsvm" "$tmp_dir/model$i.bin" \
	    "$tmp_dir/prediction${i}.txt" \
	    2> "$tmp_dir/predict_stderr${i}.txt" \
	    >  "$tmp_dir/predict_stdout${i}.txt" &
	pid[$i]=$!
    done

    wait_all

    rm -f "$tmp_dir/outcome.txt" "$tmp_dir/prediction.txt"
}

show_stat (){
    for t in `seq $times`; do
	prediction_all=''
	for i in `seq $last`; do
	    awk '{print $1}' "$tmp_dir/test${t}_$i.libsvm" > "$tmp_dir/outcome${t}_${i}.txt"
	    paste "$tmp_dir/outcome${t}_${i}.txt" "$tmp_dir/prediction${t}_${i}.txt" | \
		tr '	' ' '  > "$tmp_dir/outcome_and_prediction${t}_${i}.txt"
	    ${SVM_HERI_STAT_CMD} -1R $heristat_args \
		"$tmp_dir/outcome_and_prediction${t}_${i}.txt" > "$tmp_dir/stats${t}_${i}.txt"
	    if [[ "_$output_mode" =~ f ]]; then
		echo "Fold ${t}x$i statistics"
		${SVM_HERI_STAT_CMD} -1 $heristat_args "$tmp_dir/outcome_and_prediction${t}_${i}.txt" |
		indent2
		echo ''
	    fi

	    ln -f "$tmp_dir/outcome_and_prediction${t}_${i}.txt" "$tmp_dir/outcome_and_prediction${i}.txt"
	    prediction_all="$prediction_all $tmp_dir/outcome_and_prediction${t}_${i}.txt"
	done
    done
}

export HERISVM_FC=`calculate_feature_count $files $testing_set`

for t in `seq $times`; do
    training_testing "$@"
#    ls -l "$tmp_dir/"
    for i in `seq $last`; do
	mv "$tmp_dir/train${i}.libsvm" "$tmp_dir/train${t}_$i.libsvm"
	mv "$tmp_dir/test${i}.libsvm" "$tmp_dir/test${t}_$i.libsvm"
	mv "$tmp_dir/prediction${i}.txt" "$tmp_dir/prediction${t}_${i}.txt"
	mv "$tmp_dir/train_stderr${i}.txt" "$tmp_dir/train_stderr${t}_$i.txt"
	mv "$tmp_dir/train_stdout${i}.txt" "$tmp_dir/train_stdout${t}_$i.txt"
	mv "$tmp_dir/predict_stderr${i}.txt" "$tmp_dir/predict_stderr${t}_$i.txt"
	mv "$tmp_dir/predict_stdout${i}.txt" "$tmp_dir/predict_stdout${t}_$i.txt"
	if test -f "$tmp_dir/model${i}.bin"; then
	    mv "$tmp_dir/model${i}.bin" "$tmp_dir/model${t}_$i.bin"
	fi
    done
#    rm "$tmp_dir/test${i}.txt" "$tmp_dir/prediction${i}.txt"
done

#echo before test
#ls -l "$tmp_dir"
show_stat
#echo after test

predictions_from_testing_sets > "$tmp_dir/prediction.txt"

# -o
if test -n "$predictions"; then
    cp "$tmp_dir/prediction.txt" "$predictions"
fi

# -O
if test -n "$incorrect_predictions"; then
    awk '$1 != $2 {print "#" NR, $0}' "$tmp_dir/prediction.txt" \
	> "$incorrect_predictions"
fi

# -m
if test -n "$confusion_matrix"; then
    awk '$1 != $2 {print $1, $2}' "$tmp_dir/prediction.txt" |
    sort | uniq -c | sort -rn |
	awk '{print $1, ":", $2, $3}' > "$confusion_matrix"
fi

rm "$tmp_dir/prediction.txt"

#
if [[ "_$output_mode" =~ t ]]; then
    echo 'Total statistics'
    ${SVM_HERI_STAT_CMD} -1 $heristat_args "$tmp_dir"/outcome_and_prediction*_*.txt | indent2
    echo ''
fi

if test -n "$number_of_folds" && [[ "_$output_mode" =~ c ]]; then
    echo 'Total cross-folds statistics'
    ${SVM_HERI_STAT_ADDONS_CMD} "$tmp_dir"/stats*.txt | indent2
fi