-
Notifications
You must be signed in to change notification settings - Fork 1
/
refine_dataset_using_threshold_aum.py
165 lines (140 loc) · 4.51 KB
/
refine_dataset_using_threshold_aum.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
import os
import argparse
import csv
import ast
from tqdm import tqdm
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
parser = argparse.ArgumentParser()
parser.add_argument('--task', type=str)
parser.add_argument('--data_type', type=str)
parser.add_argument('--model_type', type=str)
parser.add_argument('--data_path', type=str)
parser.add_argument('--sampling_ratio', type=float)
args = parser.parse_args()
print(args)
original_path = './output/'+args.task+'_'+args.model_type+'_original_'+args.data_type+'/aum_values.csv'
threshold_path = './output/'+args.task+'_'+args.model_type+'_threshold_'+args.data_type+'/aum_values.csv'
data_path = args.data_path
class SNLIProcessor:
def load_samples(self, path):
samples = {}
with open(path, newline='') as f:
reader = csv.reader(f, delimiter='\t')
desc = f'loading \'{path}\''
idx = 0
for row in tqdm(reader, desc=desc):
if idx == 0:
header = row
else:
guid = row[1]
samples[guid] = row
idx += 1
return samples, header
class QQPProcessor:
def load_samples(self, path):
samples = {}
with open(path, newline='') as f:
reader = csv.reader(f, delimiter='\t')
desc = f'loading \'{path}\''
idx = 0
for row in tqdm(reader, desc=desc):
#for row in reader:
if idx == 0:
header = row
else:
guid = row[0]
samples[guid] = row
idx += 1
return samples, header
class SWAGProcessor:
def load_samples(self, path):
samples = {}
with open(path, newline='') as f:
reader = csv.reader(f, delimiter='\t')
desc = f'loading \'{path}\''
idx = 0
for row in tqdm(reader, desc=desc):
if idx == 0:
header = row
else:
guid = row[5]
samples[guid] = row
idx += 1
return samples, header
def select_processor():
"""Selects data processor using task name."""
return globals()[f'{args.task}Processor']()
processor = select_processor()
data, header = processor.load_samples(data_path)
#aums = []
th_aum_dict = {}
or_aum_dict = {}
with open(threshold_path) as f:
reader = csv.reader(f, delimiter=',')
desc = f'loading \'{threshold_path}\''
th_aums = []
idx = 0
for row in reader:
if idx == 0:
idx += 1
continue
guid = int(row[0])
aum = float(row[1])
val_dict = {}
val_dict['aum'] = aum
th_aum_dict[guid] = val_dict
idx += 1
th_aums.append(aum)
#aums.append(-aum)
with open(original_path) as f:
reader = csv.reader(f, delimiter=',')
desc = f'loading \'{original_path}\''
or_aums = []
idx = 0
for row in reader:
if idx == 0:
idx += 1
continue
guid = int(row[0])
aum = float(row[1])
val_dict = {}
val_dict['aum'] = aum
or_aum_dict[guid] = val_dict
idx += 1
or_aums.append(aum)
#aums.append(aum)
sns.set_style('whitegrid')
plot = sns.kdeplot(np.array(th_aums), bw=0.5, label='Threhold Examples')
plot = sns.kdeplot(np.array(or_aums), bw=0.5, label='Original Examples')
plot.legend()
plt.savefig(args.task+"_"+args.model_type+"_"+args.data_type+'_output.png')
length = len(th_aums)
th_aums.sort()
th_aums = th_aums[:int(length*args.sampling_ratio)]
alpha = max(th_aums)
refine_data = []
filter_data = []
for key in data:
row = data[key]
if int(key) in or_aum_dict:
aum = or_aum_dict[int(key)]['aum']
if aum > alpha:
refine_data.append(row)
else:
filter_data.append(row)
if '.tsv' in args.data_path:
new_data_path = args.data_path.replace(".tsv","")+"_woMislabeled"+".tsv"
elif '.txt' in args.data_path:
new_data_path = args.data_path.replace(".txt","")+"_woMislabeled"+".txt"
with open(new_data_path, 'w', newline='',encoding='utf-8') as output_file:
output_file.write(str(header)+'\n')
desc = f'writing \'{new_data_path}\''
for item in tqdm(refine_data,desc=desc):
output_file.write(str("\t".join(item))+'\n')
print("The original data # of instances")
print(len(data))
print("The refined data with using threshold instances")
print(len(refine_data))