/
resample_confidence.py
175 lines (131 loc) · 5.29 KB
/
resample_confidence.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 15 10:20:29 2022
@author: smuradoglu
"""
import pandas as pd
import os
lang = "nqn"
os.chdir('/home/salkazzar/Documents/Active_learning_in_morphology/data/cycle3/%s' %lang)
#%%##%% Open data files and clean lines
source = open('tst.%s.input' %lang, 'r')
source_lines= source.readlines()
output = open('tst.%s.output' %lang, 'r')
output_lines= output.readlines()
pred = open('tst.%s.guesses' %lang, 'r')
pred_lines= pred.readlines()
loglike = open('tst.%sn.guesses' %lang, 'r')
loglike_lines= loglike.readlines()
src_frm=[]
for i in range(len(source_lines)):
src_frm.append(source_lines[i].rstrip('\n').split('\t'))
trg_frm=[]
for i in range(len(output_lines)):
trg_frm.append(output_lines[i].rstrip('\n').split('\t'))
pred_frm=[]
for i in range(len(pred_lines)):
pred_frm.append(pred_lines[i].rstrip('\n').split('\t'))
log_like=[]
for i in range(len(loglike_lines)):
log_like.append(loglike_lines[i].rstrip('\n').split('\t'))
print(trg_frm[2])
print(pred_frm[2])
#%%# Create Truth/correctness table
correct = []
for i in range(len(trg_frm)):
if trg_frm[i] == pred_frm[i]:
correct.append('1')
else:
correct.append('0')
false = correct.count('0')
true = correct.count('1')
acc = true / (true + false)
print(acc)
#%%# Resample from 1st model test files
#Combine lists
#Get rid of nested lists
clean_source = []
for sources in src_frm:
for source in sources:
clean_source.append(source)
clean_output = []
for outputs in trg_frm:
for output in outputs:
clean_output.append(output)
clean_log = []
for logs in log_like:
for logli in logs:
clean_log.append(logli)
#create dataframe from lists
df = pd.DataFrame(list(zip(clean_source, clean_output, clean_log)),
columns =['input', 'output', 'loglikelihood'])
#%%# Select based on loglikelihood values ##
sample_size =250
#LOW CONFIDENCE FORMS
#sort dataframe based on loglikelihood values (ascending false for low confidence forms)
low = df.sort_values('loglikelihood', ascending=False)
LC_resamp = low.head(sample_size)
LC_remain_tst = low.tail(df.shape[0] -sample_size)
#HIGH CONFIDENCE FORMS
#sort dataframe based on loglikelihood values (ascending True for low confidence forms)
high = df.sort_values('loglikelihood', ascending=True)
HC_resamp = high.head(sample_size)
HC_remain_tst = high.tail(df.shape[0] -sample_size)
HC_resamp
#%%#
#write to file input/output pairs for resampled INC training data
LC_resamp['input'].to_csv('%s.resampleLC.input' %lang, index=False, header= False)
LC_resamp['output'].to_csv('%s.resampleLC.output' %lang, index=False, header= False)
# Generate new test file
LC_remain_tst['input'].to_csv('%s.resampleLC_tst.input' %lang, index=False, header= False)
LC_remain_tst['output'].to_csv('%s.resampleLC_tst.output' %lang, index=False, header= False)
#write to file input/output pairs for resampled CF training data
HC_resamp['input'].to_csv('%s.resampleHC.input' %lang, index=False, header= False)
HC_resamp['output'].to_csv('%s.resampleHC.output' %lang, index=False, header= False)
# Generate new test file
HC_remain_tst['input'].to_csv('%s.resampleHC_tst.input' %lang, index=False, header= False)
HC_remain_tst['output'].to_csv('%s.resampleHC_tst.output' %lang, index=False, header= False)
#%%##Concatenate original training data with the corresponding files generated above
#train files:
filenames = ['train.%s.output'%lang, '%s.resampleLC.output' %lang]
with open('train.%s_resampleLC.output' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
filenames = ['train.%s.input'%lang, '%s.resampleLC.input' %lang]
with open('train.%s_resampleLC.input' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
filenames = ['train.%s.output'%lang, '%s.resampleHC.output' %lang]
with open('train.%s_resampleHC.output' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
filenames = ['train.%s.input'%lang, '%s.resampleHC.input' %lang]
with open('train.%s_resampleHC.input' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
#test files
filenames = ['resample.%s.output'%lang, '%s.resampleLC_tst.output' %lang]
with open('tst.%s_resampleLC.output' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
filenames = ['resample.%s.input'%lang, '%s.resampleLC_tst.input' %lang]
with open('tst.%s_resampleLC.input' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
filenames = ['resample.%s.output'%lang, '%s.resampleHC_tst.output' %lang]
with open('tst.%s_resampleHC.output' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())
filenames = ['resample.%s.input'%lang, '%s.resampleHC_tst.input' %lang]
with open('tst.%s_resampleHC.input' %lang, 'w') as outfile:
for fname in filenames:
with open(fname) as infile:
outfile.write(infile.read())