-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathresplit.py
310 lines (240 loc) · 14.8 KB
/
resplit.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
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
from __future__ import division
import argparse
import sys
import os
import json
from sklearn.model_selection import GroupShuffleSplit, train_test_split
from functools import reduce
import numpy as np
import pandas as pd
def get_single_terms(data):
single_terms = reduce(lambda x, y: x+y, data["schema_terms"].values.tolist())
return single_terms
def get_schema_terms(data):
schema_terms = dict()
for idx, row in data.iterrows():
if tuple(sorted(row["schema_terms"])) not in schema_terms:
schema_terms[tuple(sorted(row["schema_terms"]))] = len(schema_terms)
return schema_terms
def group_schema_terms(data):
terms2group = dict()
def func(x):
if tuple(sorted(x)) not in terms2group:
terms2group[tuple(sorted(x))] = len(terms2group)
return terms2group[tuple(sorted(x))]
data["schema_terms_group_idx"] = data["schema_terms"].map(lambda x: func(x))
return data
def determine_level(schema_terms, single_terms_train, schema_terms_train):
unseen_terms = list(set(schema_terms).difference(set(single_terms_train)))
if tuple(sorted(schema_terms)) not in schema_terms_train:
if len(unseen_terms) != 0: # zero-shot
level="zero-shot"
else:
level="compositional"
else:
level="i.i.d."
return level
def sample_zeroshot_questions(data, sampling_ratio, n_splits, random_seed):
group_ids = data["schema_terms_group_idx"].tolist()
groups = np.array(group_ids)
gss = GroupShuffleSplit(n_splits=n_splits, train_size=1-sampling_ratio, random_state=random_seed)
train_zeroshot_splits = []
unique_group_splits = dict()
for train_idx, zeroshot_idx in gss.split(X=data, groups=groups):
if tuple(sorted(train_idx)) not in unique_group_splits:
unique_group_splits[tuple(sorted(train_idx))] = zeroshot_idx
for train_idx, zeroshot_idx in unique_group_splits.items():
train_idx = list(train_idx)
train_set = data.iloc[train_idx]
zeroshot_set = data.iloc[zeroshot_idx]
train_zeroshot_splits.append((train_set, zeroshot_set))
return train_zeroshot_splits
def sample_compositional_questions(data, sampling_ratio, n_splits, random_seed):
if sampling_ratio == 0.0:
return []
group_ids = data["schema_terms_group_idx"].tolist()
groups = np.array(group_ids)
gss = GroupShuffleSplit(n_splits=n_splits, train_size=1-sampling_ratio, random_state=random_seed)
train_compos_splits = []
unique_group_splits = dict()
for train_idx, compo_idx in gss.split(X=data, groups=groups):
if tuple(sorted(train_idx)) not in unique_group_splits:
unique_group_splits[tuple(sorted(train_idx))] = compo_idx
for train_idx, compo_idx in unique_group_splits.items():
train_idx = list(train_idx)
train_set = data.iloc[train_idx]
compo_set = data.iloc[compo_idx]
train_compos_splits.append((train_set, compo_set))
return train_compos_splits
def data_filter(data_src, train_set):
single_schema_terms_train = get_single_terms(train_set)
schema_terms_train = get_schema_terms(train_set)
data_src["level"] = data_src["schema_terms"].map(lambda x: determine_level(x, single_schema_terms_train, schema_terms_train))
zero_set = data_src[data_src['level']=="zero-shot"]
compo_set = data_src[data_src['level']=="compositional"]
iid_set = data_src[data_src['level']=="i.i.d."]
return zero_set, compo_set, iid_set
def zeroshot_filter(zeroshot_set, train_set):
zeroshot_set, compositional_set, iid_set = data_filter(zeroshot_set, train_set)
non_zeroshot_set = pd.concat([compositional_set, iid_set])
return zeroshot_set, non_zeroshot_set
def compositional_filter(compositional_set, train_set):
zeroshot_set, compositional_set, iid_set = data_filter(compositional_set, train_set)
non_compositional_set = pd.concat([zeroshot_set, iid_set])
return compositional_set, non_compositional_set
def sample_iid_questions(data, sampling_ratio, random_seed):
train_set, iid_set = train_test_split(data, train_size=1-sampling_ratio, random_state=random_seed)
return train_set, iid_set
def main(arguments):
parser = argparse.ArgumentParser()
parser.add_argument("--dataset_id", type=str, help="the unique identifier of the dataset newly generated.")
parser.add_argument("--input_path", type=str, help="full path of datasets to be resplitted.")
parser.add_argument("--output_dir", default="output_dir", type=str, help="directory to save the resplitted datasets.")
parser.add_argument("--n_splits_zero", default=1, type=int, help="the number of splits for zeroshot generalization.")
parser.add_argument("--n_splits_compo", default=1, type=int, help="the number of splits for compositional generalization.")
parser.add_argument("--random_seed", default=42, type=int, help="random seed.")
parser.add_argument("--sampling_ratio_zero", default=.4, type=float, help="the ratio for sampling zeroshot questions from the data set.")
parser.add_argument("--sampling_ratio_compo", default=.1, type=float, help="the ratio for sampling compositional questions from the data set.")
parser.add_argument("--sampling_ratio_iid", default=.1, type=float, help="the ratio for sampling iid questions from the data set.")
parser.add_argument("--validation_size", default=.33, type=float, help="the size of validation set splitted from the test size.")
args = parser.parse_args(arguments)
if not os.path.isdir(args.output_dir):
os.mkdir(args.output_dir)
data_sets = pd.read_json(args.input_path, orient="records")
data_sets = group_schema_terms(data_sets)
num_samples = len(data_sets)
empty_data = pd.DataFrame(columns=data_sets.columns)
candidate_zero_compo_iid_train_splits = []
if args.sampling_ratio_iid == 0.0 and args.sampling_ratio_compo == 0.0 and args.sampling_ratio_zero > 0.0:
candidate_train_zeroshot_splits = sample_zeroshot_questions(data_sets, args.sampling_ratio_zero,
args.n_splits_zero,
args.random_seed)
for train_set_1, zeroshot_set_1 in candidate_train_zeroshot_splits:
zeroshot_set, non_zeroshot_set = zeroshot_filter(zeroshot_set_1, train_set_1)
train_set = pd.concat([train_set_1, non_zeroshot_set])
candidate_zero_compo_iid_train_splits.append((zeroshot_set, empty_data, empty_data, train_set))
elif args.sampling_ratio_iid == 0.0 and args.sampling_ratio_compo > 0.0 and args.sampling_ratio_zero == 0.0:
candidate_train_compo_splits = sample_compositional_questions(data_sets,
args.sampling_ratio_compo,
args.n_splits_compo, args.random_seed)
for train_set_2, compositional_set_1 in candidate_train_compo_splits:
compositional_set, non_compositional_set = compositional_filter(compositional_set_1, train_set_2)
train_set = pd.concat([train_set_2, non_compositional_set])
candidate_zero_compo_iid_train_splits.append((empty_data, compositional_set, empty_data, train_set))
elif args.sampling_ratio_iid > 0.0 and args.sampling_ratio_compo == 0.0 and args.sampling_ratio_zero == 0.0:
train_set, iid_set = sample_iid_questions(data_sets, args.sampling_ratio_iid, args.random_seed)
candidate_zero_compo_iid_train_splits.append((empty_data, empty_data, iid_set, train_set))
else:
if args.sampling_ratio_zero > 0.0:
candidate_train_zeroshot_splits = sample_zeroshot_questions(data_sets, args.sampling_ratio_zero, args.n_splits_zero,
args.random_seed)
for train_set_1, zeroshot_set_1 in candidate_train_zeroshot_splits:
zeroshot_set, non_zeroshot_set = zeroshot_filter(zeroshot_set_1, train_set_1)
intermediate_train_set_1 = pd.concat([train_set_1, non_zeroshot_set])
if args.sampling_ratio_compo > 0.0:
candidate_train_compo_splits = sample_compositional_questions(intermediate_train_set_1, args.sampling_ratio_compo,
args.n_splits_compo, args.random_seed)
for train_set_2, compositional_set_1 in candidate_train_compo_splits:
compositional_set, non_compositional_set = compositional_filter(compositional_set_1, train_set_2)
intermediate_train_set_2 = pd.concat([train_set_2, non_compositional_set])
if args.sampling_ratio_iid > 0.0:
train_set, iid_set = sample_iid_questions(intermediate_train_set_2, args.sampling_ratio_iid, args.random_seed)
candidate_zero_compo_iid_train_splits.append((zeroshot_set, compositional_set, iid_set, train_set))
else:
candidate_zero_compo_iid_train_splits.append(
(zeroshot_set, compositional_set, empty_data, intermediate_train_set_2))
else:
train_set, iid_set = sample_iid_questions(intermediate_train_set_1, args.sampling_ratio_iid,
args.random_seed)
candidate_zero_compo_iid_train_splits.append((zeroshot_set, empty_data, iid_set, train_set))
else:
candidate_train_compo_splits = sample_compositional_questions(data_sets,
args.sampling_ratio_compo,
args.n_splits_compo, args.random_seed)
for train_set_2, compositional_set_1 in candidate_train_compo_splits:
compositional_set, non_compositional_set = compositional_filter(compositional_set_1, train_set_2)
intermediate_train_set_2 = pd.concat([train_set_2, non_compositional_set])
train_set, iid_set = sample_iid_questions(intermediate_train_set_2, args.sampling_ratio_iid,
args.random_seed)
candidate_zero_compo_iid_train_splits.append((empty_data, compositional_set, iid_set, train_set))
train_zero_compo_iid_splits = []
for zero, compo, iid, train in candidate_zero_compo_iid_train_splits:
zero_set_tmp = pd.DataFrame(columns=train.columns)
compo_set_tmp = pd.DataFrame(columns=train.columns)
iid_set_tmp = pd.DataFrame(columns=train.columns)
if not zero.empty:
zero_set_1, compo_set_1, iid_set_1 = data_filter(zero, train)
zero_set_tmp = pd.concat([zero_set_tmp, zero_set_1])
compo_set_tmp = pd.concat([compo_set_tmp, compo_set_1])
iid_set_tmp = pd.concat([iid_set_tmp, iid_set_1])
if not compo.empty:
zero_set_2, compo_set_2, iid_set_2 = data_filter(compo, train)
zero_set_tmp = pd.concat([zero_set_tmp, zero_set_2])
compo_set_tmp = pd.concat([compo_set_tmp, compo_set_2])
iid_set_tmp = pd.concat([iid_set_tmp, iid_set_2])
if not iid.empty:
zero_set_3, compo_set_3, iid_set_3 = data_filter(iid, train)
zero_set_tmp = pd.concat([zero_set_tmp, zero_set_3])
compo_set_tmp = pd.concat([compo_set_tmp, compo_set_3])
iid_set_tmp = pd.concat([iid_set_tmp, iid_set_3])
if zero.empty:
train = pd.concat([train, zero_set_tmp])
zero_set = empty_data
else:
zero_set = zero_set_tmp
if iid.empty:
train = pd.concat([train, iid_set_tmp])
iid_set = empty_data
else:
iid_set = iid_set_tmp
if compo.empty:
train = pd.concat([train, compo_set_tmp])
compo_set = empty_data
else:
compo_set = compo_set_tmp
train_zero_compo_iid_splits.append((train, zero_set, compo_set, iid_set))
for idx, splits in enumerate(train_zero_compo_iid_splits, 1):
split_dir_name = "new_split_"+str(idx) if len(train_zero_compo_iid_splits) > 1 else "new_split"
split_dir = os.path.join(args.output_dir, split_dir_name)
if not os.path.isdir(split_dir):
os.makedirs(split_dir)
train, zero, compo, iid = splits
train = train[["id", "question", "query", "answers", "schema_terms"]]
test = pd.concat([zero, compo, iid])[["id", "question", "query", "answers", "schema_terms", "level"]]
if args.validation_size > 0.0:
validation, test, _, _ = train_test_split(test, test["level"], train_size=args.validation_size, stratify=test["level"], random_state=args.random_seed)
json_rslt_valid = {
"dataset": {
"id": f"{args.dataset_id}-valid"
},
"questions": json.loads(validation.to_json(orient="records"))
}
json.dump(json_rslt_valid, open(os.path.join(split_dir, f"{args.dataset_id}-valid.json"), "w"), indent=2)
json_rslt_train = {
"dataset": {
"id": f"{args.dataset_id}-train"
},
"questions": json.loads(train.to_json(orient="records"))
}
json.dump(json_rslt_train, open(os.path.join(split_dir, f"{args.dataset_id}-train.json"), "w"), indent=2)
json_rslt_test = {
"dataset": {
"id": f"{args.dataset_id}-test"
},
"questions": json.loads(test.to_json(orient="records"))
}
json.dump(json_rslt_test, open(os.path.join(split_dir, f"{args.dataset_id}-test.json"), "w"), indent=2)
stats_file = open(os.path.join(split_dir, "stats.txt"), "w")
stats_file.write("==============Split %d==============\n" % idx)
if args.validation_size > 0.0:
stats_file.write(
f"total: {num_samples}\ntrain: {len(train)} ({len(train)/num_samples*100} %%)\nvalid: {len(validation)}({len(validation)/num_samples*100} %%)\ntest: {len(test)}({len(test)/num_samples*100} %%)\nzero: {len(zero)} ({len(zero)/num_samples*100} %%)\ncompo: {len(compo)} ({len(compo)/num_samples*100} %%)\niid: {len(iid)} ({len(iid)/num_samples*100} %%)"
)
else:
stats_file.write(
f"total: {num_samples}\ntrain: {len(train)} ({len(train) / num_samples * 100} %%)\ntest: {len(test)}({len(test) / num_samples * 100} %%)\nzero: {len(zero)} ({len(zero) / num_samples * 100} %%)\ncompo: {len(compo)} ({len(compo) / num_samples * 100} %%)\niid: {len(iid)} ({len(iid) / num_samples * 100} %%)"
)
stats_file.write("\n\n")
stats_file.close()
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))