-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.py
More file actions
266 lines (206 loc) · 7.29 KB
/
tools.py
File metadata and controls
266 lines (206 loc) · 7.29 KB
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
#!/usr/bin/env python3.6
import os
import sys
import subprocess
import tempfile
import random
import time
from collections import defaultdict
import pdb
import numpy as np
import logging
TMP_DIR = '/tmp/labtest'
if not os.path.isdir(TMP_DIR):
os.makedirs(TMP_DIR)
def getVriusTotalHashKey():
key = 'fd2d772ac104caac9b92cee4d45d9043144bf18eb4bb6df5c97b4a764345ab89'
return key
def get_date():
return time.strftime("%Y_%m_%d", time.localtime())
def get_time():
return time.strftime("time_%H_%M_%S", time.localtime())
def get_pid():
return str(os.getpid())
def makeTempFile(suffix='', prefix='', dir=None, keepfile=False):
if not prefix:
prefix = 'tmp{}_'.format(get_pid())
if not dir:
dir = TMP_DIR
dir = os.path.join(dir, get_date())
if not os.path.isdir(dir):
os.makedirs(dir)
fd, fname = tempfile.mkstemp(suffix=suffix, prefix=prefix, dir=dir)
if not keepfile:
os.close(fd)
return fname
def makeTempDir(suffix='', prefix='', dir=None):
if not prefix:
prefix = 'tmp{}_'.format(get_pid())
if not dir:
dir = TMP_DIR
dir = os.path.join(dir, get_date())
if not os.path.isdir(dir):
os.makedirs(dir)
dname = tempfile.mkdtemp(suffix=suffix, prefix=prefix, dir=dir)
return dname
def getLogger(appName='default is empty'):
logger = logging.getLogger(appName)
logger.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formater = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formater)
logger.addHandler(ch)
return logger
def getSectionList(start, end, interval):
rangeList = [start]
while 1:
tmpPoint = start + interval
if tmpPoint >= end:
break
rangeList.append(tmpPoint)
start = tmpPoint
rangeList[-1] = end
secList = [0 for n in range(len(rangeList)-1)]
return rangeList, secList
def computeRange(rangeList, feature):
l = len(rangeList) - 1
for i in range(l):
x1 = rangeList[i]
x2 = rangeList[i+1]
if x1 <= feature < x2:
return i
raise ValueError('the value of feature == {} exceed the rangeList'.format(feature))
def shuffleData(X, y):
if not isinstance(X, np.ndarray):
X = np.array(X)
if not isinstance(y, np.ndarray):
y = np.array(y)
assert(X.shape[0] == y.shape[0])
# pair up
tupList = []
for i in range(y.shape[0]):
if 2 == len(X.shape):
tmp_tuple = (X[i, :], y[i])
elif 1 == len(X.shape):
tmp_tuple = (X[i], y[i])
elif 3 == len(X.shape):
tmp_tuple = (X[i, :, :], y[i])
else:
raise ValueError('data shape {} not supported yet'.format(X.shape))
tupList.append(tmp_tuple)
random.shuffle(tupList)
X, y = [], []
for i in range(len(tupList)):
X.append(tupList[i][0])
y.append(tupList[i][1])
X = np.array(X)
y = np.array(y)
return X, y
def datadict2data(datadict, keys=[], shuffle=True):
allData, allLabel = [], []
if not keys:
keys = list(datadict.keys())
for key in keys:
oneCls = datadict[key]
oneLabel = np.ones(len(oneCls)) * int(float(key))
allData.extend(oneCls)
allLabel.extend(oneLabel)
if shuffle:
allData, allLabel = shuffleData(allData, allLabel)
return allData, allLabel
def data2datadict(allData, allLabel, clsLimit=0, sampleLimit=0):
'''
expected input are numpy ndarry
'''
if not isinstance(allData, np.ndarray):
allData = np.array(allData)
if not isinstance(allLabel, np.ndarray):
allLabel = np.array(allLabel)
datadict = defaultdict(list)
allCls = list(set(allLabel))
if clsLimit:
allCls = random.sample(allCls, clsLimit)
for i in range(len(allLabel)):
label = allLabel[i]
if label in allCls:
if len(allData.shape) == 2:
sample = allData[i, :]
elif len(allData.shape) == 1:
sample = allData[i]
else:
raise ValueError('data shape {} not supported yet'.format(allData.shape))
datadict[label].append(sample)
count = 0
new_dict = defaultdict(list)
for key in datadict.keys():
oneClsData = datadict[key]
new_dict[count] = oneClsData
count += 1
del datadict
if sampleLimit:
for key in new_dict.keys():
oneClsData = new_dict[key]
if sampleLimit >= len(oneClsData):
new_samp = oneClsData[:sampleLimit]
else:
new_samp = random.sample(oneClsData, sampleLimit)
new_dict[key] = new_samp
return new_dict
def limitData(allData, allLabel, clsLimit=0, sampleLimit=0):
dataDict = data2datadict(allData, allLabel, clsLimit, sampleLimit)
x_new, y_new = datadict2data(dataDict)
return x_new, y_new
def divideData(allData, allLabel, train_sample_num=5, train_pool_size=20):
data_dict = data2datadict(allData, allLabel)
train_data, train_label, test_data, test_label = [], [], [], []
oneClsNum = len(list(data_dict[0]))
test_sample_num = oneClsNum - train_pool_size
for key in data_dict.keys():
oneCls = list(data_dict[key])
random.shuffle(oneCls)
train_pool = []
for i in range(train_pool_size):
tmp = oneCls.pop()
train_pool.append(tmp)
train_data.extend(train_pool[:train_sample_num])
tmpLabels = np.ones(train_sample_num, dtype=np.int) * key
train_label.extend(tmpLabels)
test_data.extend(oneCls[:test_sample_num])
tmpLabels = np.ones(test_sample_num, dtype=np.int) * key
test_label.extend(tmpLabels)
train_data, train_label = shuffleData(train_data, train_label)
test_data, test_label = shuffleData(test_data, test_label)
return train_data, train_label, test_data, test_label
def divideDataDict(data_dict, train_sample_num=5, train_pool_size=20):
train_data, train_label, test_data, test_label = [], [], [], []
keys = list(data_dict.keys())
oneClsNum = len(list(data_dict[keys[0]]))
test_sample_num = oneClsNum - train_pool_size
for key in data_dict.keys():
oneCls = list(data_dict[key])
random.shuffle(oneCls)
train_pool = []
for i in range(train_pool_size):
tmp = oneCls.pop()
train_pool.append(tmp)
train_data.extend(train_pool[:train_sample_num])
tmpLabels = np.ones(train_sample_num, dtype=np.int) * key
train_label.extend(tmpLabels)
tmpData = oneCls[:test_sample_num]
tmpLabels = np.ones(len(tmpData), dtype=np.int) * key
test_data.extend(tmpData)
test_label.extend(tmpLabels)
train_data, train_label = shuffleData(train_data, train_label)
test_data, test_label = shuffleData(test_data, test_label)
return train_data, train_label, test_data, test_label
def highLighPrint(msg):
print('\033[40;33m {} \033[0m'.format(msg))
if __name__ == "__main__":
aaa = np.array([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]])
bbb = np.array([1, 2, 3, 4])
xxx, yyy = shuffleData(aaa, bbb)
print(xxx)
print(yyy)