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
|
#!/bin/bash
usage() {
echo "
Simple smoke test for the decode-dist tool. This will fail if your machine
doesn't have the right R libraries.
Usage:
./test.sh <function name>
Example:
./test.sh decode-assoc-R-smoke # test pure R implementation
./test.sh decode-assoc-cpp-smoke # test with analysis/cpp/fast_em.cc
./test.sh decode-assoc-cpp-converge # run for longer with C++
./test.sh decode-assoc-tensorflow
"
}
set -o nounset
set -o pipefail
set -o errexit
readonly THIS_DIR=$(dirname $0)
readonly RAPPOR_SRC=$(cd $THIS_DIR/.. && pwd)
readonly EM_CPP_EXECUTABLE=$RAPPOR_SRC/analysis/cpp/_tmp/fast_em
source $RAPPOR_SRC/util.sh
readonly ASSOC_TESTDATA_DIR=_tmp/decode-assoc-test
readonly DIST_TESTDATA_DIR=_tmp/decode-dist-test
# Clear the R cache for the map files.
clear-cached-files() {
local dir=$1
find $dir -name '*.rda' | xargs --no-run-if-empty -- rm --verbose
}
write-dist-testdata() {
local input_dir=$DIST_TESTDATA_DIR/input
mkdir -p $input_dir
clear-cached-files $DIST_TESTDATA_DIR
# Right now, we copy a case from regtest.sh. (./demo.sh quick-python creates
# just this case)
local case_dir=$RAPPOR_SRC/_tmp/python/demo3
cp --verbose $case_dir/1/case_counts.csv $input_dir/counts.csv
cp --verbose $case_dir/case_map.csv $input_dir/map.csv
cp --verbose $case_dir/case_params.csv $input_dir/params.csv
}
decode-dist() {
write-dist-testdata
local output_dir=$DIST_TESTDATA_DIR
local input_dir=$DIST_TESTDATA_DIR/input
# Uses the ./demo.sh regtest files
time $RAPPOR_SRC/bin/decode-dist \
--counts $input_dir/counts.csv \
--map $input_dir/map.csv \
--params $input_dir/params.csv \
--output-dir $output_dir
echo
head $output_dir/results.csv
echo
cat $output_dir/metrics.json
}
write-assoc-testdata() {
# 'build' has intermediate build files, 'input' is the final input to the
# decode-assoc tool.
local build_dir=$ASSOC_TESTDATA_DIR/build
local input_dir=$ASSOC_TESTDATA_DIR/input
mkdir -p $build_dir $input_dir
clear-cached-files $ASSOC_TESTDATA_DIR
cat >$build_dir/true_values.csv <<EOF
domain,flag..HTTPS
google.com,1
google.com,1
google.com,1
google.com,1
google.com,0
yahoo.com,1
yahoo.com,0
bing.com,1
bing.com,1
bing.com,0
EOF
local num_bits=8
local num_hashes=1
local num_cohorts=128
local prob_p=0.25
local prob_q=0.75
local prob_f=0.5
# 10 items in the input. 50,000 items is enough to eyeball accuracy of
# results.
local assoc_testdata_count=5000
PYTHONPATH=$RAPPOR_SRC/client/python \
$RAPPOR_SRC/tests/rappor_sim.py \
--assoc-testdata $assoc_testdata_count \
--num-bits $num_bits \
--num-hashes $num_hashes \
--num-cohorts $num_cohorts \
-p $prob_p \
-q $prob_q \
-f $prob_f \
< $build_dir/true_values.csv \
> $input_dir/reports.csv
# Output two bad rows: each row is missing one of the columns.
cat >$build_dir/bad_rows.txt <<EOF
c0,0,10101010,
c0,0,,0
EOF
# Make CSV file with the header
cat - $build_dir/bad_rows.txt > $input_dir/bad_rows.csv <<EOF
client,cohort,domain,flag..HTTPS
EOF
# Make reports file with bad rows
cat $input_dir/reports.csv $build_dir/bad_rows.txt > $input_dir/reports_bad_rows.csv
# Define a string variable and a boolean varaible.
cat >$input_dir/rappor-vars.csv <<EOF
metric, var, var_type, params
m,domain,string,m_params
m,flag..HTTPS,boolean,m_params
EOF
cat >$input_dir/m_params.csv <<EOF
k,h,m,p,q,f
$num_bits,$num_hashes,$num_cohorts,$prob_p,$prob_q,$prob_f
EOF
# Add a string with a double quote to test quoting behavior
cat >$build_dir/domain_candidates.csv <<EOF
google.com
yahoo.com
bing.com
q"q
EOF
# Hash candidates to create map.
$RAPPOR_SRC/bin/hash-candidates $input_dir/m_params.csv \
< $build_dir/domain_candidates.csv \
> $input_dir/domain_map.csv
banner "Wrote testdata in $input_dir (intermediate files in $build_dir)"
}
# Helper function to run decode-assoc with testdata.
decode-assoc-helper() {
write-assoc-testdata
local output_dir=$1
shift
local build_dir=$ASSOC_TESTDATA_DIR/build
local input_dir=$ASSOC_TESTDATA_DIR/input
time $RAPPOR_SRC/bin/decode-assoc \
--metric-name m \
--schema $input_dir/rappor-vars.csv \
--reports $input_dir/reports.csv \
--params-dir $input_dir \
--var1 domain \
--var2 flag..HTTPS \
--map1 $input_dir/domain_map.csv \
--create-bool-map \
--max-em-iters 10 \
--num-cores 2 \
--output-dir $output_dir \
--tmp-dir $output_dir \
"$@"
head $output_dir/assoc-*
# Print true values for comparison
echo
echo "$build_dir/true_values.csv:"
cat "$build_dir/true_values.csv"
}
# Quick smoke test for R version.
decode-assoc-R-smoke() {
local output_dir=_tmp/R
mkdir -p $output_dir
decode-assoc-helper $output_dir
}
# Test what happens when there are bad rows.
decode-assoc-bad-rows() {
local output_dir=_tmp/bad
mkdir -p $output_dir
# Later flags override earlier ones
# Reports + bad rows
decode-assoc-helper $output_dir \
--reports _tmp/reports_bad_rows.csv \
--remove-bad-rows \
"$@"
# ONLY bad rows
decode-assoc-helper $output_dir \
--reports _tmp/bad_rows.csv \
--remove-bad-rows \
"$@"
}
build-em-executable() {
pushd $RAPPOR_SRC/analysis/cpp >/dev/null
./run.sh build-fast-em
popd >/dev/null
}
decode-assoc-cpp-smoke() {
local output_dir=_tmp/cpp
mkdir -p $output_dir
build-em-executable
decode-assoc-helper $output_dir \
--em-executable "$EM_CPP_EXECUTABLE" "$@"
}
decode-assoc-cpp-converge() {
# With the data we have, this converges and exits before 1000 iterations.
decode-assoc-cpp-smoke --max-em-iters 1000
}
decode-assoc-tensorflow() {
local output_dir=_tmp/tensorflow
mkdir -p $output_dir
decode-assoc-helper $output_dir \
--em-executable $RAPPOR_SRC/analysis/tensorflow/fast_em.sh "$@"
}
decode-assoc-tensorflow-converge() {
decode-assoc-tensorflow --max-em-iters 1000
}
if test $# -eq 0 ; then
usage
else
"$@"
fi
|