-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_framer.py
227 lines (170 loc) · 6.5 KB
/
data_framer.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
#!/usr/bin/python
import os
import glob
import pandas as pd
import numpy as np
class DataFramer:
def __init__(self):
self.dic = {}
self.filepaths = []
self.filename = []
self.data = []
self.mean = []
self.size = []
self.dframe = pd.DataFrame()
def get_all_paths(self,this_dir):
self.curr_dir = this_dir
directories = ["Primary_features", "Secondary_features_down", "Secondary_features_up", "Teritiary_features"]
for directory in directories :
features_dir = os.path.join(self.curr_dir, directory)
file_search = os.path.join(features_dir, '*.txt')
self.filepaths.extend(glob.glob(file_search))
# print filepaths
def get_specific_paths(self, paths):
self.filepaths = paths
def extract_data(self):
for i,filepath in enumerate(self.filepaths) :
file = open(filepath, 'r')
tmp = ""
if "_down" in filepath :
tmp = "down_"
elif "_up" in filepath :
tmp = "up_"
self.filename.append(tmp + os.path.splitext(os.path.basename(filepath))[0])
content = file.readlines()
file.close()
self.data.append(content)
for j,val in enumerate(self.data[i]) :
self.data[i][j] = float(self.data[i][j])
self.mean.append(round(np.mean(self.data[i]),6))
self.size.append(len(content))
def make_dataframe(self):
self.max_size = max(self.size)
self.min_size = min(self.size)
# print self.min_size,self.max_size,self.size
for i,filepath in enumerate(self.filepaths) :
if self.size[i] < self.max_size :
for j in range(self.size[i],self.max_size) :
self.data[i].append(self.mean[i])
self.dic[self.filename[i]] = self.data[i]
self.dic["user"] = [0] * self.max_size
self.dframe0 = pd.DataFrame(self.dic)
self.dic["user"] = [1] * self.max_size
self.dframe1 = pd.DataFrame(self.dic)
def make_csv(self):
self.dframe0.to_csv(os.path.join(self.curr_dir, "data_0.csv"), sep='\t')
self.dframe1.to_csv(os.path.join(self.curr_dir, "data_1.csv"), sep='\t')
class TestingDataFramer:
def __init__(self):
# Train Variables
self.train_dic = {}
self.train_filepaths = []
self.train_filename = []
self.train_data = []
self.train_mean = []
self.train_size = []
self.train_dframe = pd.DataFrame()
# Test Variables
self.test_dic = {}
self.test_filepaths = []
self.test_filename = []
self.test_data = []
self.test_mean = []
self.test_size = []
self.test_dframe = pd.DataFrame()
def get_all_paths(self,true_dir,false_dir,test_dir):
self.home_dir = os.getcwd()
self.tft_dirs = [true_dir,false_dir,test_dir]
self.tft_filepaths = [[],[],[]]
#directories = ["Primary_features", "Secondary_features_down", "Secondary_features_up", "Teritiary_features"]
directories = ["Primary_features", "Teritiary_features"]
for i,tft_dir in enumerate(self.tft_dirs):
for directory in directories :
# Test Files
tft_features_dir = os.path.join(tft_dir, directory)
tft_file_search = os.path.join(tft_features_dir, '*.txt')
self.tft_filepaths[i].extend(glob.glob(tft_file_search))
def extract_data(self):
tmp_tft_filepaths = [[],[],[]]
tft_filepath = ['','','']
self.tft_filename = [[],[],[]]
tft_content = [[],[],[]]
self.tft_data = [[],[],[]]
self.tft_mean = [[],[],[]]
self.tft_size = [[],[],[]]
k =0
for i,test_filepath in enumerate(self.tft_filepaths[2]) :
tmp_filepath = []
tmp_filepath.append(os.path.join(os.getcwd(),"true",os.path.basename(os.path.dirname(test_filepath)),os.path.basename(test_filepath)))
tmp_filepath.append(os.path.join(os.getcwd(),"false",os.path.basename(os.path.dirname(test_filepath)),os.path.basename(test_filepath)))
if tmp_filepath[0] in self.tft_filepaths[0] and tmp_filepath[1] in self.tft_filepaths[1] :
tft_filepath[0] = tmp_filepath[0]
tft_filepath[1] = tmp_filepath[1]
tft_filepath[2] = test_filepath
tmp_tft_filepaths[0].append(tft_filepath[0])
tmp_tft_filepaths[1].append(tft_filepath[1])
tft_file = []
for p in range(0,3) :
tft_file.append(open(tft_filepath[p], 'r'))
tmp = ""
if "_down" in test_filepath :
tmp = "down_"
elif "_up" in test_filepath :
tmp = "up_"
for p in range(0,3) :
self.tft_filename[p].append(tmp + os.path.splitext(os.path.basename(tft_filepath[p]))[0])
tft_content[p] = tft_file[p].readlines()
tft_file[p].close()
self.tft_data[p].append(tft_content[p])
for p in range(0,3) :
for j,val in enumerate(self.tft_data[p][k]) :
self.tft_data[p][k][j] = float(self.tft_data[p][k][j])
for p in range(0,3) :
self.tft_mean[p].append(round(np.mean(self.tft_data[p][k]),6))
self.tft_size[p].append(len(tft_content[p]))
k += 1
else:
i -= 1
tmp_tft_filepaths[2].append(test_filepath)
for tmp_test_filepath in tmp_tft_filepaths[2] :
self.tft_filepaths[2].remove(tmp_test_filepath)
for p in range(0,2):
self.tft_filepaths[p] = tmp_tft_filepaths[p]
# print self.tft_filepaths[0]
def make_dataframe(self):
self.tft_max_size = []
self.tft_min_size = []
tft_filepath = ['','','']
self.tft_dic = [{},{},{}]
self.tft_dframe = ['','','']
for p in range(0,3):
self.tft_max_size.append(max(self.tft_size[p]))
self.tft_min_size.append(min(self.tft_size[p]))
self.tft_max_size[0]= 50
self.tft_max_size[1]= 50
for p in range(0,3):
for i,tft_filepath[p] in enumerate(self.tft_filepaths[p]):
if self.tft_size[p][i] < self.tft_max_size[p] :
for j in range(self.tft_size[p][i],self.tft_max_size[p]):
self.tft_data[p][i].append(self.tft_mean[p][i])
else :
self.tft_data[p][i] = self.tft_data[p][i][0:self.tft_max_size[0]]
self.tft_dic[p][self.tft_filename[p][i]] = self.tft_data[p][i]
self.tft_dic[0]["zzzzzz"] = [1] * self.tft_max_size[0]
#print self.tft_dic[0]
self.tft_dframe[0] = pd.DataFrame(self.tft_dic[0])
#self.tft_dframe[0].insert(len(self.tft_filename[0]),"user",[1] * self.tft_max_size[0])
self.tft_dic[1]["zzzzzz"] = [0] * self.tft_max_size[1]
self.tft_dframe[1] = pd.DataFrame(self.tft_dic[1])
self.tft_dic[2]["zzzzzz"] = [0] * self.tft_max_size[2]
self.tft_dframe[2] = pd.DataFrame(self.tft_dic[2])
#print self.tft_dframe[0]
#print self.tft_dframe[1]
def make_csv(self):
train_data = pd.concat([self.tft_dframe[0],self.tft_dframe[1]])
test_data = self.tft_dframe[2]
train_data.to_csv(os.path.join(self.tft_dirs[2], "train_data.csv") ,sep=',' ,index=False ,header=False)
test_data.to_csv(os.path.join(self.tft_dirs[2], "test_data.csv") ,sep=',' ,index=False ,header=False)
#with open(os.path.join(self.tft_dirs[2], "train_data.csv"))
def get_columns(self) :
return self.tft_filename[0]