/
extd.py
269 lines (199 loc) · 8.58 KB
/
extd.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
# -*- coding: utf-8 -*-
# Copyright (c) 2016 Felipe Gallego. All rights reserved.
#
# This is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""Class to store the data used during the calculations.
"""
import numpy as np
from ctes import *
from kfiles import extract_list_text, save_data_to_csv
from kscrap import KScrap
class ExtD(object):
def __init__(self, index):
self._index = index
self._lm = [[0 for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
self._ve = [[0 for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
self._qu = [[0 for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
self._q1 = [[0 for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
self._cq = [[0 for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
self._cqp = [[0 for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
self._mean = [[0 for _ in range(NUM_COLS)] for _ in range(NUM_ROWS)]
def __str__(self):
return "lm: %d ve: %d qu: %d q1: %d cq: %d cqp: %d mean: %d" % \
(len(self._lm), len(self._ve), len(self._qu), len(self._q1),
len(self._cq), len(self._cqp))
@property
def lm(self):
return self._lm
@lm.setter
def lm(self, lm_data):
self._lm = lm_data
@property
def lm_exists(self):
return len(self._lm) > 0
@property
def ve(self):
return self._ve
@ve.setter
def ve(self, ve_data):
self._ve = ve_data
@property
def ve_exists(self):
return len(self._ve) > 0
@property
def qu(self):
return self._qu
@qu.setter
def qu(self, qu_data):
self._qu = qu_data
@property
def qu_exists(self):
return len(self._qu) > 0
@property
def q1(self):
return self._q1
@q1.setter
def q1(self, q1_data):
self._q1 = q1_data
@property
def q1_exists(self):
return len(self._q1) > 0
@property
def cq(self):
return self._cq
@cq.setter
def cq(self, cq_data):
self._cq = cq_data
@property
def cq_exists(self):
return len(self._cq) > 0
@property
def cqp(self):
return self._cqp
@cqp.setter
def cqp(self, cqp_data):
self._cqp = cqp_data
@property
def cqp_exists(self):
return len(self._cqp) > 0
@property
def mean(self):
if not len(self._mean):
self.calc_mean()
return self._mean
@property
def ext_data_ok(self):
return len(self._lm) == NUM_ROWS and \
len(self._ve) == NUM_ROWS and \
len(self._qu) == NUM_ROWS and \
len(self._q1) == NUM_ROWS and \
len(self._cq) == NUM_ROWS and \
len(self._cqp) == NUM_ROWS
def _calc_mean(self):
mean_sources = []
self._mean = []
if sum(self._lm[0]):
mean_sources.append(self._lm)
else:
print "Ignoring lm for mean."
if sum(self._ve[0]):
mean_sources.append(self._ve)
else:
print "Ignoring ve for mean."
if sum(self._qu[0]):
mean_sources.append(self._qu)
else:
print "Ignoring qu for mean."
if sum(self._q1[0]):
mean_sources.append(self._q1)
else:
print "Ignoring q1 for mean."
if sum(self._cq[0]):
mean_sources.append(self._cq)
else:
print "Ignoring cq for mean."
if len(mean_sources) > 1:
for i in range(NUM_ROWS):
new_row = []
for j in range(NUM_COLS):
values = [s[i][j] for s in mean_sources]
new_row.append(int(round(np.mean(values))))
self._mean.append(new_row)
def _read_extd(self):
success = True
lines = []
# Reading from local file the rest of data.
file_name = EXTD_FILE_PREFIX + self._index + SCRAPPED_DATA_FILE_EXT
print "Reading data from file: %s" % file_name
try:
with open(file_name, "r") as f:
for l in f:
# Process text line.
l_txt = l[:-1].strip()
if len(l_txt):
if l_txt.find(LM_TEXT) >= 0:
self._lm = extract_list_text(l_txt, NUM_COLS_LM)
print "Read %dx%d from file for LM" % \
(len(self._lm), len(self._lm[0]))
elif l_txt.find(VE_TEXT) >= 0:
self._ve = extract_list_text(l_txt, NUM_COLS_VE)
print "Read %dx%d from file for VE" % \
(len(self._ve), len(self._ve[0]))
elif l_txt.find(QU_TEXT) >= 0:
self._qu = extract_list_text(l_txt, NUM_COLS_QU)
print "Read %dx%d from file for QU" % \
(len(self._qu), len(self._qu[0]))
elif l_txt.find(Q1_TEXT) >= 0:
self._q1 = extract_list_text(l_txt, NUM_COLS_Q1)
print "Read %dx%d from file for Q1" % \
(len(self._q1), len(self._q1[0]))
elif l_txt.find(CQ_TEXT) >= 0:
self._cq = extract_list_text(l_txt, NUM_COLS_CQ)
print "Read %dx%d from file for CQ" % \
(len(self._cq), len(self._cq[0]))
elif l_txt.find(CQP_TEXT) >= 0:
self._cqp = extract_list_text(l_txt, NUM_COLS_CQ)
print "Read %dx%d from file for CQP" % \
(len(self._cqp), len(self._cqp[0]))
except IOError as ioe:
print "ERROR: Reading file '%s'" % file_name
success = False
return success
def _save_extd(self):
out_file_name = EXTD_FILE_PREFIX + self._index + SCRAPPED_DATA_FILE_EXT
try:
with open(out_file_name, 'w') as f:
f.write("%s %s %s\n\n" % (LM_TEXT, SCR_TXT_DELIM, str(self._lm)))
f.write("%s %s %s\n\n" % (VE_TEXT, SCR_TXT_DELIM, str(self._ve)))
f.write("%s %s %s\n\n" % (QU_TEXT, SCR_TXT_DELIM, str(self._qu)))
f.write("%s %s %s\n\n" % (Q1_TEXT, SCR_TXT_DELIM, str(self._q1)))
f.write("%s %s %s\n\n" % (CQ_TEXT, SCR_TXT_DELIM, str(self._cq)))
f.write("%s %s %s\n\n" % (CQP_TEXT, SCR_TXT_DELIM, str(self._cqp)))
print "Data scrapped saved in: %s" % out_file_name
except IOError as ioe:
print "Error saving file: '%s'" % out_file_name
def _save_mean(self):
output_file = MEAN_FILE_NAME_PREFIX + self._index + OUTPUT_FILE_NAME_EXT
save_data_to_csv(output_file, self._mean)
def load_data(self):
if not self._read_extd():
KScrap.lm_scraping(self._lm)
KScrap.ve_scraping(self._ve)
KScrap.qu_scraping(self._qu)
KScrap.q1_scraping(self._q1, self._index)
KScrap.cq_scraping(self._cq, self._cqp)
if self.ext_data_ok:
self._save_extd()
self._calc_mean()
self._save_mean()