~ubuntu-branches/ubuntu/precise/dbacl/precise

« back to all changes in this revision

Viewing changes to src/mailtest.functions.in

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2005-05-07 12:59:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050507125953-xzy2bwkb2qamglwm
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
3
# Copyright (C) 2002 Laird Breyer
 
4
#  
 
5
# This program is free software; you can redistribute it and/or modify
 
6
# it under the terms of the GNU General Public License as published by
 
7
# the Free Software Foundation; either version 2 of the License, or
 
8
# (at your option) any later version.
 
9
 
10
# This program is distributed in the hope that it will be useful,
 
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
# GNU General Public License for more details.
 
14
 
15
# You should have received a copy of the GNU General Public License
 
16
# along with this program; if not, write to the Free Software
 
17
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
18
 
19
# Author:   Laird Breyer <laird@lbreyer.com>
 
20
#
 
21
# These functions are included by the test scripts. They could be
 
22
# just sourced, but then where to put the common functions in the filesystem? 
 
23
 
 
24
# begin mailtest.functions
 
25
 
 
26
prerequisite_command() {
 
27
    if [ -z "`type -p $1`" ]; then
 
28
        echo "Error: $1 not found. Please install $2 to proceed."
 
29
        exit 1
 
30
    fi
 
31
}
 
32
 
 
33
clean_working_tree() {
 
34
    if [ -e $MXDIR ]; then
 
35
        rm -rf $MXDIR
 
36
    else
 
37
        echo "Nothing to clean"
 
38
    fi
 
39
}
 
40
 
 
41
make_dummy_mbox() {
 
42
    cat > $MXDIR/mbox/dummy.mailbox <<EOF
 
43
From MAILER-DAEMON Thu Dec  4 13:50:55 2003
 
44
Date: 04 Dec 2003 13:50:55 +1000
 
45
From: Mail System Internal Data <MAILER-DAEMON@scooby>
 
46
Subject: DON\'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA
 
47
Message-ID: <1070509855@scooby>
 
48
X-IMAP: 1023007291 0000010227
 
49
Status: RO
 
50
 
 
51
This text is part of the internal format of your mail folder, and is not
 
52
a real message.  It is created automatically by the mail system software.
 
53
If deleted, important folder data will be lost, and it will be re-created
 
54
with the data reset to initial values.
 
55
 
 
56
EOF
 
57
}
 
58
 
 
59
prepare_working_tree() {
 
60
    if [ -d $MXDIR ]; then 
 
61
        echo "Error: Directory $MXDIR already exists. Remove it or use it.";
 
62
        usage;
 
63
    elif [ -n "$1" ]; then
 
64
        prerequisite_command "seq" "shellutils"
 
65
        NUM=`expr $1 - 1`;
 
66
        if [ $NUM -gt -1 ]; then
 
67
            mkdir "$MXDIR" && \
 
68
            mkdir "$MXDIR/tmp" && \
 
69
            mkdir "$MXDIR/log" && \
 
70
            mkdir "$MXDIR/plots" && \
 
71
            mkdir "$MXDIR/mbox" && \
 
72
            mkdir "$MXDIR/review" && \
 
73
            mkdir "$MXDIR/filters" && \
 
74
            for i in `seq 0 $NUM`; do mkdir "$MXDIR/$i"; done && \
 
75
            echo "=== prepare_working_tree $*" >> $ALOG
 
76
 
 
77
        else
 
78
            echo "Error: Please specify a number greater than zero."
 
79
            usage
 
80
        fi
 
81
 
 
82
    else
 
83
        echo "Error: Please specify a number."
 
84
        usage
 
85
    fi
 
86
}
 
87
 
 
88
get_categories() {
 
89
    CATS=`find $MXDIR -type f -name '*.mbox' -exec basename {} \; | sort -u`
 
90
    [ -z "$CATS" ] && echo "No categories found"
 
91
}
 
92
 
 
93
get_filters() {
 
94
    FILTS=`find $FILTERS -type f -perm +0111 -exec basename {} \; | sort -u`
 
95
    [ -z "$FILTS" ] && echo "No filters found"
 
96
}
 
97
 
 
98
get_number_of_subsets() {
 
99
    NUM=`ls $MXDIR | grep '^[0-9]' | wc -l`
 
100
    if [ $(($NUM)) -le 0 ]; then
 
101
        echo "error: you need to prepare first."
 
102
        usage
 
103
    fi
 
104
}
 
105
 
 
106
review_misclassified() {
 
107
    SM=$1
 
108
    shift
 
109
    cat > "$SM" <<EOF
 
110
#!/bin/sh
 
111
cat > msg.tmp.\$POSITION.\$FILENO
 
112
M=\`head -1 msg.tmp.\$POSITION.\$FILENO | grep -e "\$EMAIL.*\$DATE"\`
 
113
if [ -n "\$M" ]; then
 
114
    mv msg.tmp.\$POSITION.\$FILENO $MXDIR/review/\$1.\$2.\$POSITION.\$FILENO
 
115
else
 
116
    rm msg.tmp.\$POSITION.\$FILENO
 
117
fi
 
118
EOF
 
119
    
 
120
    if [ -d "$MXDIR/review" ]; then
 
121
        rm -f "$MXDIR/review/*"
 
122
    else
 
123
        echo "Error: You need to prepare first."
 
124
        usage
 
125
    fi
 
126
 
 
127
    if [ -s "$CLOG" ]; then
 
128
        grep " $1 $2 " "$CLOG" | \
 
129
            while read f; do
 
130
                BOX=`echo $f | cut -d' ' -f2`
 
131
                export POSITION=`echo $f | cut -d' ' -f1`
 
132
                export EMAIL=`echo $f | cut -d' ' -f4`
 
133
                export DATE=`echo $f | cut -d' ' -f5-`
 
134
                echo "$f"
 
135
                [ -e "$MXDIR/$POSITION/$BOX.mbox" ] || export POSITION=mbox
 
136
                cat "$MXDIR/$POSITION/$BOX.mbox" | formail -s /bin/sh "$SM" "$1" "$2"
 
137
            done
 
138
    else
 
139
        echo "Error: There are no logs - run the classifier(s) first" 
 
140
    fi
 
141
 
 
142
    rm -f "$SM"
 
143
}
 
144
 
 
145
testsuite_list_wrappers() {
 
146
    if [ -d $BOOTSTRAP ] ; then
 
147
        echo -ne "The following classification wrappers are selectable:\n\n"
 
148
        for f in `ls $BOOTSTRAP` ; do
 
149
            [ -x "$BOOTSTRAP/$f" ] && echo "$f - `$BOOTSTRAP/$f describe`"
 
150
        done
 
151
    else
 
152
        echo "Bootstrap directory $BOOTSTRAP does not exist."
 
153
    fi
 
154
}
 
155
 
 
156
testsuite_deselect_wrappers() {
 
157
    shift
 
158
    if [ -z "$*" ] ; then
 
159
        usage
 
160
    else
 
161
        for f in "$@" ; do 
 
162
            if [ -e $FILTERS/$f ] ; then
 
163
                echo "deselecting $f"
 
164
                rm -f $FILTERS/$f
 
165
            else
 
166
                echo "$f: no such filter."
 
167
            fi
 
168
        done
 
169
    fi
 
170
}
 
171
 
 
172
testsuite_select_wrappers() {
 
173
    shift
 
174
    if [ -z "$*" ] ; then
 
175
        usage
 
176
    else
 
177
        for f in $* ; do
 
178
            if [ -x $BOOTSTRAP/$f ] ; then
 
179
                $BOOTSTRAP/$f bootstrap $FILTERS
 
180
            else
 
181
                echo "The wrapper $f cannot be selected, skipping."
 
182
            fi
 
183
    done
 
184
    fi
 
185
}
 
186
 
 
187
summarize_log() {
 
188
    awk -v "num=$NUM" -v "cats=${CATS//.mbox/}" '
 
189
BEGIN{
 
190
 
 
191
    split(cats,names)
 
192
 
 
193
}
 
194
/^[^#]/{
 
195
 
 
196
    f[$2,$3]++
 
197
    fn[$3]++
 
198
    fp[$2]++
 
199
 
 
200
}
 
201
END{
 
202
 
 
203
    printf("Where do misclassifications go?\n(numbers on diagonal represent \"recall\")\n\n")
 
204
 
 
205
    printf("  true     | but predicted as...\n")
 
206
    printf("    *      | ")
 
207
    for(c in names) printf("%10s", names[c])
 
208
    printf("\n")
 
209
 
 
210
    for(c in names) {
 
211
        printf("%-10s | ", names[c])
 
212
        for(d in names) {
 
213
            printf("%9.2f%%", 100 * f[names[c],names[d]]/fp[names[c]])
 
214
        }
 
215
        printf("\n")
 
216
    }
 
217
 
 
218
    printf("\n")
 
219
 
 
220
    printf("What is really in each category after prediction?\n(numbers on diagonal represent \"precision\")\n\n")
 
221
 
 
222
    printf("category   | contains mixture of...\n")
 
223
    printf("    *      | ")
 
224
    for(c in names) printf("%10s", names[c])
 
225
    printf("\n")
 
226
 
 
227
    for(c in names) {
 
228
        printf("%-10s | ", names[c])
 
229
        for(d in names) {
 
230
            printf("%9.2f%%", 100 * f[names[d],names[c]]/fn[names[c]])
 
231
        }
 
232
        printf("\n")
 
233
    }
 
234
 
 
235
    printf("\n")
 
236
 
 
237
    x = y = 0
 
238
    for(c in names) {
 
239
        x += f[names[c],names[c]]
 
240
        for(d in names) {
 
241
            y += f[names[c],names[d]]
 
242
        }
 
243
    }
 
244
 
 
245
    printf("Total correct classifications: %9.2f%%\n\n", (100 * x)/y)
 
246
 
 
247
}
 
248
'
 
249
}
 
250
 
 
251
plot_errors() {
 
252
    OUTFILE=$MXDIR/plots/`basename $1 .log`
 
253
    CMDFILE=$OUTFILE.cmd
 
254
    DATAFILE=$OUTFILE.plotdata
 
255
    TITLE=$2
 
256
    cat $1 \
 
257
        | grep -v '^#' \
 
258
        | cut -f1,2,3 -d ' ' \
 
259
        | awk '{count[$1]++; ecount[$1] += ($2 != $3); print (count[$1] == 1) ? "\n" : "", count[$1], ecount[$1]}' \
 
260
        > $DATAFILE
 
261
    shift 2
 
262
    OUTPUT="set terminal x11"
 
263
    SCALE="unset logscale"
 
264
    PAUSE="pause -1 \"Press any key...\""
 
265
    OUTPRINT=""
 
266
    for o in "$@"; do
 
267
        if [ "$o" = "ps" ]; then
 
268
            OUTPUT="set terminal postscript"
 
269
            OUTPRINT="set output \"$OUTFILE.ps\""
 
270
            PAUSE=""
 
271
            echo "writing $OUTFILE.ps"
 
272
        elif [ "$o" = "logscale" ]; then
 
273
            SCALE="set logscale"
 
274
        fi
 
275
    done
 
276
    cat > $CMDFILE <<EOF
 
277
$OUTPUT
 
278
$OUTPRINT
 
279
$SCALE
 
280
unset key
 
281
set title "$TITLE"
 
282
plot "$DATAFILE" with points
 
283
$PAUSE
 
284
EOF
 
285
    gnuplot $CMDFILE
 
286
}
 
287
 
 
288
plot_biplot() {
 
289
    OUTFILE=$MXDIR/plots/$1
 
290
    CMDFILE=$OUTFILE.cmd
 
291
    DATAFILE=$OUTFILE.plotdata
 
292
    ERRORFILE=$OUTFILE.ploterrors
 
293
    TITLE=$1
 
294
    cat $2 \
 
295
        | grep -v '^#' \
 
296
        | awk '{print $5, $14}' \
 
297
        > $DATAFILE
 
298
    cat $2 \
 
299
        | grep -v '^#' \
 
300
        | awk '{if( (($2 == $3) && ($5 > $14)) || (($2 != $3) && ($5 < $14)) ) print }' \
 
301
        | awk '{print $5, $14}' \
 
302
        > $ERRORFILE
 
303
    shift 2
 
304
    OUTPUT="set terminal x11"
 
305
    SCALE="unset logscale"
 
306
    PAUSE="pause -1 \"Press any key...\""
 
307
    OUTPRINT=""
 
308
    for o in "$@"; do
 
309
        if [ "$o" = "ps" ]; then
 
310
            OUTPUT="set terminal postscript"
 
311
            OUTPRINT="set output \"$OUTFILE.ps\""
 
312
            PAUSE=""
 
313
            echo "writing $OUTFILE.ps"
 
314
        elif [ "$o" = "logscale" ]; then
 
315
            SCALE="set logscale"
 
316
        fi
 
317
    done
 
318
    cat > $CMDFILE <<EOF
 
319
$OUTPUT
 
320
$OUTPRINT
 
321
$SCALE
 
322
unset key
 
323
set title "$TITLE"
 
324
plot "$DATAFILE" using 1:2 pt 1, "$ERRORFILE" using 1:2 pt 5, x
 
325
 
 
326
$PAUSE
 
327
EOF
 
328
    gnuplot $CMDFILE
 
329
}
 
330
 
 
331
# end mailtest.functions