-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_missing_pair_experiment.py
261 lines (205 loc) · 8.61 KB
/
run_missing_pair_experiment.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
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
import sys
sys.path.append("LogisticCircuit")
sys.path.append("pypsdd")
sys.path.append('..')
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import BernoulliNB
import argparse
import pickle
import gzip
try:
from time import perf_counter
except:
from time import time
perf_counter = time
import datetime
import os
import logging
import pickle
import gzip
import json
import numpy as np
from utils_missing import run_missing_exp, plot_results_paper
from LogisticCircuit.structure.Vtree import generate_random_vtree
from LogisticCircuit.algo.LogisticCircuit import learn_logistic_circuit
from LogisticCircuit.util.DataSet import DataSet
from LogisticCircuit.algo.RegressionCircuit import learn_regression_circuit, RegressionCircuit
import psdd_io
from manager import PSddManager
from algo.LogisticCircuit import LogisticCircuit
from structure.Vtree import Vtree as LC_Vtree
from vtree import Vtree as PSDD_Vtree
from sklearn.metrics import mean_squared_error
def dump_data_csv(X, data_path):
with open(data_path, 'w') as f:
for x in X:
f.write('{}\n'.format(','.join(str(s) for s in x)))
if __name__ == '__main__':
start_all = perf_counter()
#########################################
# creating the opt parser
parser = argparse.ArgumentParser()
parser.add_argument("dataset", type=str,
help='Path to data dir')
parser.add_argument('-o', '--output', type=str,
default='./exp/missing/',
help='Output path to exp result')
parser.add_argument('--seed', type=int, nargs='?',
default=1337,
help='Seed for the random generator')
parser.add_argument('--exp-id', type=str,
default=None,
help='Dataset output suffix')
parser.add_argument('--repeat', type=int,
default=10,
help='How many times to repeat for each missingness')
parser.add_argument('--taylor', type=int,
default=3,
help='How many taylor terms (only for classification)')
parser.add_argument('--miss-perc', type=int, nargs='+',
default=[0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
help='How many taylor terms (only for classification)')
parser.add_argument("--vtree", type=str, default="balanced",
help="Path for vtree or mode to get it")
# parser.add_argument('--psdd', type=str,
# default=None,
# help='Path to psdd')
# parser.add_argument('--glc', type=str,
# default=None,
# help='Path to glc (logistic circuit or regression circuit)')
parser.add_argument('--regression', action='store_true',
help='Regression instead of classification')
parser.add_argument('-v', '--verbose', type=int, nargs='?',
default=1,
help='Verbosity level')
#
# parsing the args
args = parser.parse_args()
#
# setting verbosity level
if args.verbose == 1:
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
elif args.verbose == 2:
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
#
# creating output dirs if they do not exist
date_string = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
dataset_name = os.path.basename(args.dataset).replace('.pklz', '')
# if args.exp_id:
# out_path = os.path.join(args.output, args.exp_id)
# else:
# out_path = os.path.join(args.output, '{}_{}'.format(dataset_name, date_string))
# os.makedirs(out_path, exist_ok=True)
out_path = args.output
args_out_path = os.path.join(out_path, 'miss.args.json')
json_args = json.dumps(vars(args))
logging.info("Starting with arguments:\n%s\n\tdumped at %s", json_args, args_out_path)
with open(args_out_path, 'w') as f:
f.write(json_args)
#
# setting up the seed
rand_gen = np.random.RandomState(args.seed)
out_log_path = os.path.join(out_path, 'miss.exp.log')
logging.info('Opening log file... {}'.format(out_log_path))
#
# loading up datasets
with gzip.open(args.dataset, 'rb') as f:
data_splits = pickle.load(f)
#
# unpacking splits
(x_train, y_train), (x_valid, y_valid), (x_test, y_test) = data_splits
if not args.regression:
y_train = y_train.astype(np.int8)
y_valid = y_valid.astype(np.int8)
y_test = y_test.astype(np.int8)
n_features = x_train.shape[1]
assert x_valid.shape[1] == n_features
assert x_train.shape[0] == y_train.shape[0]
assert x_test.shape[1] == n_features
assert x_valid.shape[0] == y_valid.shape[0]
assert x_test.shape[0] == y_test.shape[0]
logging.info(f'\nLoaded dataset splits of shapes:')
logging.info(f'\t\ttrain {x_train.shape} {y_train.shape}')
logging.info(f'\t\tvalid {x_valid.shape} {y_valid.shape}')
logging.info(f'\t\ttest {x_test.shape} {y_test.shape}')
# load the vtrees
if args.vtree is None:
vtree_path = os.path.join(out_path, f'{dataset_name}.vtree')
else:
vtree_path = args.vtree
glc_vtree = LC_Vtree.read(vtree_path)
#
# Load logistic/Regression circuit
# TODO: accomodate also for a regression circuit
if not args.regression:
n_classes = np.max(y_train) + 1
else:
n_classes = 1
one_hot = not args.regression
train_data = DataSet(x_train, y_train, one_hot)
valid_data = DataSet(x_valid, y_valid, one_hot)
test_data = DataSet(x_test, y_test, one_hot)
circuit_path = os.path.join(out_path, f'{dataset_name}.glc')
if args.regression:
with open(circuit_path) as circuit_file:
circuit = RegressionCircuit(glc_vtree, circuit_file=circuit_file)
else:
with open(circuit_path) as circuit_file:
circuit = LogisticCircuit(glc_vtree, n_classes, circuit_file=circuit_file)
if args.regression:
#
# evaluate
# FIXME: change name from images
train_data.features = circuit.calculate_features(train_data.images)
train_acc = circuit.calculate_error(train_data)
logging.info(f'\t\ttrain error: {train_acc:.5f}')
valid_data.features = circuit.calculate_features(valid_data.images)
valid_acc = circuit.calculate_error(valid_data)
logging.info(f'\t\tvalid error: {valid_acc:.5f}')
test_data.features = circuit.calculate_features(test_data.images)
test_acc = circuit.calculate_error(test_data)
logging.info(f'\t\ttest error: {test_acc:.5f}')
else:
#
# evaluate
# FIXME: change name from images
train_data.features = circuit.calculate_features(train_data.images)
train_acc = circuit.calculate_accuracy(train_data)
logging.info(f'\t\ttrain accuracy: {train_acc:.5f}')
valid_data.features = circuit.calculate_features(valid_data.images)
valid_acc = circuit.calculate_accuracy(valid_data)
logging.info(f'\t\tvalid accuracy: {valid_acc:.5f}')
test_data.features = circuit.calculate_features(test_data.images)
test_acc = circuit.calculate_accuracy(test_data)
logging.info(f'\t\ttest accuracy: {test_acc:.5f}')
# load PSDD
vtree_psdd = PSDD_Vtree.read(vtree_path)
manager = PSddManager(vtree_psdd)
psdd_path = os.path.join(out_path, f'{dataset_name}.psdd')
psdd = psdd_io.psdd_yitao_read(psdd_path, manager)
def sqrt_mse(x, y): return np.sqrt(mean_squared_error(x, y))
REPEAT = int(args.repeat) if args.repeat is not None else 10
TAYLOR = int(args.taylor) if args.taylor is not None else 3
setting = {
'k': args.miss_perc,
'percentage': True,
'repeat': REPEAT,
'T': TAYLOR,
'sample_method': False, # whether or not run monte carlo estimation
'sample_size': 200,
}
if args.regression:
setting["function"] = sqrt_mse
setting["regression"] = args.regression
setting['T'] = 0
print("----SETTING----")
print(setting)
print("---------------")
result = run_missing_exp(x_test, y_test, psdd, circuit, setting)
result_path = os.path.join(out_path, f'{dataset_name}_{args.exp_id}_missing_result.pickle')
result_path_z = result_path + "z"
logging.info("Writing results into {}".format(result_path_z))
with gzip.open(result_path_z, "wb") as gfile:
pickle.dump(result, gfile)
end_all = perf_counter()
logging.info("Total time: {}".format(end_all - start_all))