-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsweep_and_cut_test.py
227 lines (219 loc) · 7.12 KB
/
sweep_and_cut_test.py
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
import timeit
import numpy as np
from sweep_and_cut import sweep_and_cut, query
from utils import plot, plot_3
from pathlib import Path
import pandas as pd
queries = []
for i in range(1000):
query_x = np.random.randint(0, 100000)
query_y = np.random.randint(0, 100000)
queries.append([query_x, query_y])
ratios = [0.25, 0.5, 0.75, 1.0]
g1 = [4000, 6666, 8000, 10000]
g2 = [16000, 13334, 12000, 10000]
fractions = [0.2, 0.4, 0.6, 0.8, 1.0]
num_of_buckets_list = [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000]
datasets = ["adult", "compas_random_id", "diabetes", "popsim_binary"]
sensitive_attrs = ["sex", "Ethnic_Code_Text", "gender", "race"]
columns = [
["fnlwgt", "education-num"],
["ID", "RawScore"],
["encounter_id", "patient_nbr"],
["lon", "lat"],
]
for idx in range(1,2):
print("=================", datasets[idx], "=================")
preprocessing_time = []
space = []
query_times = []
for frac in fractions:
print("=================", "fraction:", frac, "=================")
path = (
"real_data/"
+ datasets[idx]
+ "/"
+ datasets[idx]
+ "_f_"
+ str(frac)
+ ".csv"
)
n = pd.read_csv(path).shape[0]
num_of_buckets = 100
boundary, hash_buckets, duration = sweep_and_cut(
path, columns[idx], sensitive_attrs[idx], num_of_buckets
)
preprocessing_time.append(duration)
space.append(len(boundary))
query_time = []
for q in queries:
start = timeit.default_timer()
query(q[0], boundary, hash_buckets)
stop = timeit.default_timer()
query_time.append(stop - start)
query_times.append(np.mean(query_time))
print("Varying dataset size (prep time):", preprocessing_time)
print("Varying dataset size (query time):", query_times)
print("Varying dataset size (space):", space)
Path("plots/sweep_and_cut/" + datasets[idx]).mkdir(parents=True, exist_ok=True)
plot(
"plots/sweep_and_cut/" + datasets[idx] + "/varying_size_prep_time.png",
fractions,
preprocessing_time,
fractions,
"Varying dataset size (prep time)",
"Fraction(×" + str(n) + ")",
"Time (sec)",
)
plot(
"plots/sweep_and_cut/" + datasets[idx] + "/varying_size_query_time.png",
fractions,
query_times,
fractions,
"Varying dataset size (query time)",
"Fraction(×" + str(n) + ")",
"Time (sec)",
)
plot(
"plots/sweep_and_cut/" + datasets[idx] + "/varying_size_space.png",
fractions,
space,
fractions,
"Varying dataset size (space)",
"Fraction(×" + str(n) + ")",
"Number of cuts",
)
preprocessing_time = []
space = []
upperbound = []
query_times = []
for idx2, ratio in enumerate(ratios):
print("=================", "ratio:", ratio, "=================")
path = (
"real_data/"
+ datasets[idx]
+ "/"
+ datasets[idx]
+ "_r_"
+ str(ratio)
+ ".csv"
)
num_of_buckets = 100
boundary, hash_buckets, duration = sweep_and_cut(
path, columns[idx], sensitive_attrs[idx], num_of_buckets
)
preprocessing_time.append(duration)
space.append(len(boundary))
data = pd.read_csv(path)
# g1 = data[sensitive_attrs[idx]].value_counts()["Male"]
# g2 = data[sensitive_attrs[idx]].value_counts()["Female"]
# ub = 2 * ((g1 * g2 / data.shape[0]) + num_of_buckets)
# upperbound.append(ub)
query_time = []
for q in queries:
start = timeit.default_timer()
query(q[0], boundary, hash_buckets)
stop = timeit.default_timer()
query_time.append(stop - start)
query_times.append(np.mean(query_time))
print("Varying minority ratio (prep time):", preprocessing_time)
print("Varying minority ratio (query time):", query_times)
print("Varying minority ratio (space):", space)
print("Upper Bound",upperbound)
plot(
"plots/sweep_and_cut/" + datasets[idx] + "/varying_ratio_prep_time.png",
ratios,
preprocessing_time,
ratios,
"Varying ratio (prep time)",
"Ratio",
"Time (sec)",
)
plot(
"plots/sweep_and_cut/" + datasets[idx] + "/varying_ratio_query_time.png",
ratios,
query_times,
ratios,
"Varying ratio (query time)",
"Ratio",
"Time (sec)",
)
plot(
"plots/sweep_and_cut/" + datasets[idx] + "/varying_ratio_space.png",
ratios,
space,
ratios,
"Varying ratio (space)",
"Ratio",
"Number of cuts",
)
plot_3(
"plots/sweep_and_cut/" + datasets[idx] + "/varying_ratio_space_ub.png",
ratios,
space,
upperbound,
ratios,
"Varying ratio (space)",
"Ratio",
"Number of cuts",
)
# preprocessing_time = []
# space = []
# query_times = []
# for num_of_buckets in num_of_buckets_list:
# print(
# "=================",
# "number of buckets:",
# num_of_buckets,
# "=================",
# )
# path = "real_data/" + datasets[idx] + "/" + datasets[idx] + "_r_0.25.csv"
# boundary, hash_buckets, duration = sweep_and_cut(
# path, columns[idx], sensitive_attrs[idx], num_of_buckets
# )
# preprocessing_time.append(duration)
# space.append(len(boundary))
# query_time = []
# for q in queries:
# start = timeit.default_timer()
# query(q[0], boundary, hash_buckets)
# stop = timeit.default_timer()
# query_time.append(stop - start)
# query_times.append(np.mean(query_time))
# print("Varying bucket size (prep time):", preprocessing_time)
# print("Varying bucket size (query time):", query_times)
# print("Varying bucket size (space):", space)
# plot(
# "plots/sweep_and_cut/"
# + datasets[idx]
# + "/varying_num_of_buckets_prep_time.png",
# num_of_buckets_list,
# preprocessing_time,
# num_of_buckets_list,
# "Varying number of buckets (prep time)",
# "Number of buckets",
# "Time (sec)",
# )
# plot(
# "plots/sweep_and_cut/"
# + datasets[idx]
# + "/varying_num_of_buckets_query_time.png",
# num_of_buckets_list,
# query_times,
# num_of_buckets_list,
# "Varying number of buckets (query time)",
# "Number of buckets",
# "Time (sec)",
# )
# plot(
# "plots/sweep_and_cut/" + datasets[idx] + "/varying_num_of_buckets_space.png",
# num_of_buckets_list,
# space,
# num_of_buckets_list,
# "Varying number of buckets (space)",
# "Number of buckets",
# "Number of cuts",
# )
print(
"###############################################################################################################"
)