-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
318 lines (281 loc) · 10.5 KB
/
utils.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
"""
utils.py
@author: Brody Kutt (bkutt@paloaltonetworks.com)
"""
import os
import sys
import numpy as np
from datetime import datetime
def random_seed():
"""
For repeatability.
"""
return 1994
def datetime_format():
"""
"""
return '%Y-%m-%d %H:%M:%S'
def datetime_str():
"""
Get a string that displays the current date and time in standard format
"""
return datetime.now().strftime(datetime_format())
def load_data_npz(fp, load_X=True, load_y=True):
"""
Load the X and y matrices from an npz compressed archive. Used for both SOI
and EV data.
"""
result = {}
data = None
try:
data = np.load(fp, mmap_mode=None, allow_pickle=True)
except IOError:
pass
if (data is None):
try:
fp += '.npz' # Try adding the file extension
data = np.load(fp, mmap_mode=None, allow_pickle=True)
except IOError:
return None
result['basenames'] = data['basenames']
if (load_X):
result['X'] = data['X']
if (load_y):
result['y'] = data['y'].astype('int')
if ('int2label' in data):
result['int2label'] = data['int2label'].item()
return result
def load_y_mask(path):
"""
"""
data = None
try:
data = np.load(path, mmap_mode=None, allow_pickle=True)
except IOError:
pass
if (data is None):
try:
path += '.npz' # Try adding the file extension
data = np.load(path, mmap_mode=None, allow_pickle=True)
except IOError:
return None
return data['y_mask'].astype('int')
def load_dataset(train_fp, val_fp=None, y_mask_fp=None, noise_ratio=0.0):
"""
"""
print('Loading the training data...')
data = {}
result = load_data_npz(train_fp)
if (result is None):
print('ERR: Failed to read: %s' % train_fp)
sys.exit(-1) # Exit failure
data['basenames'] = result['basenames']
data['X'] = result['X']
data['y'] = result['y']
data['int2label'] = result['int2label']
n_classes = len(data['int2label']) - 1
print('--> Loaded %d samples!' % len(data['y']))
for i in range(-1, n_classes):
print(('Class %d: %d' % (i, np.sum((data['y'] == i).astype('int')))))
if (val_fp is not None):
print('\nLoading the validation data...')
result = load_data_npz(val_fp)
if (result is None):
print('ERR: Failed to read: %s' % val_fp)
sys.exit(-1) # Exit failure
data['basenames_val'] = result['basenames']
data['X_val'] = result['X']
data['y_val'] = result['y']
print(('--> Loaded %d samples!' % len(data['y_val'])))
for i in range(-1, n_classes):
print('Class %d: %d' %
(i, np.sum((data['y_val'] == i).astype('int'))))
if (y_mask_fp is not None):
print('\nLoading the y mask...')
y_mask = load_y_mask(y_mask_fp)
if (y_mask is None):
print('ERR: Failed to load: %s' % y_mask_fp)
sys.exit(-1) # Exit failure
data['y_mask'] = y_mask
print('--> Loaded y_mask of size: %d' % len(data['y_mask']))
else:
print('Using y_mask of ones...')
data['y_mask'] = np.ones(len(data['y']), dtype='int')
if (np.sum(data['y_mask'] == -1) > 0):
print('--> Using y_mask to remove entries marked for removal...')
data['basenames'] = data['basenames'][data['y_mask'] != -1]
data['X'] = data['X'][data['y_mask'] != -1]
data['y'] = data['y'][data['y_mask'] != -1]
data['y_mask'] = data['y_mask'][data['y_mask'] != -1]
# Rename the class indices
l_cls_idxs = list(set(data['y'][data['y_mask'] == 1]))
assert (-1 not in l_cls_idxs)
l_cls_idxs.sort()
new_l_cls_idxs = [i for i in range(len(l_cls_idxs))]
new_int2label = {-1: 'Unlabeled'}
old2new = {}
for old_idx, new_idx in zip(l_cls_idxs, new_l_cls_idxs):
new_int2label[new_idx] = data['int2label'][old_idx]
old2new[old_idx] = new_idx
for j in range(len(data['y'])):
if (data['y'][j] not in l_cls_idxs):
data['y'][j] = -1
assert (data['y_mask'][j] == 0)
else:
data['y'][j] = old2new[data['y'][j]]
if (val_fp is not None):
for j in range(len(data['y_val'])):
if (data['y_val'][j] not in l_cls_idxs):
data['y_val'][j] = -1
else:
data['y_val'][j] = old2new[data['y_val'][j]]
data['basenames_val'] = data['basenames_val'][data['y_val'] != -1]
data['X_val'] = data['X_val'][data['y_val'] != -1]
data['y_val'] = data['y_val'][data['y_val'] != -1]
data['int2label'] = new_int2label
# Make a copy of Y before any noise is added
data['y_clean'] = np.copy(data['y'])
if (noise_ratio > 0.0):
print('--> Using noise ratio of %.2f to corrupt the training '
'labels...' % noise_ratio)
n = len(data['y_mask'])
n_cls = len(set(data['y']))
if (-1 in set(data['y'])):
n_cls -= 1
# Break up all indices into class buckets
u_idxs = []
l_idxs = [[] for i in range(n_cls)]
for i in range(n):
if (data['y'][i] == -1 or data['y_mask'][i] == 0):
u_idxs.append(i)
else:
l_idxs[int(data['y'][i])].append(i)
# Convert to NP arrays and shuffle
u_idxs = np.array(u_idxs)
for c in range(n_cls):
l_idxs[c] = np.array(l_idxs[c])
np.random.shuffle(l_idxs[c])
# Get all indices of samples that will have class changed
idxs_to_perturb = np.array([], dtype='int')
for c in range(n_cls):
cls_idxs = l_idxs[c]
idxs_to_perturb = np.concatenate(
(idxs_to_perturb, cls_idxs[:int(noise_ratio * len(cls_idxs))]))
rnd_cls_perturbations = np.random.randint(low=1,
high=n_cls,
size=len(idxs_to_perturb),
dtype=data['y'].dtype)
# Do label perturbation
data['y'][idxs_to_perturb] = np.mod(
data['y'][idxs_to_perturb] + rnd_cls_perturbations, n_cls)
print('\nTraining set summary:')
for i in sorted(set(data['y'])):
print('Class %d (%s):' % (i, data['int2label'][i]))
print('--> Total samples: %d' % np.sum(data['y'] == i))
print('--> Labeled documents: %d' %
np.sum(np.logical_and(data['y'] == i, data['y_mask'] == 1)))
print('--> Unlabeled documents: %d' %
np.sum(np.logical_and(data['y'] == i, data['y_mask'] == 0)))
if (val_fp is not None):
print('\nValidation set summary:')
for i in sorted(set(data['y_val'])):
print('Class %d (%s):' % (i, data['int2label'][i]))
print('--> Total samples: %d' % np.sum(data['y_val'] == i))
return data
def discover_fps(path):
"""
Discovers and returns a list of filepaths for every file located below a
top directory.
"""
fp_list = []
for root, dirnames, filenames in os.walk(path):
for filename in filenames:
fp_list.append(os.path.join(root, filename))
return fp_list
def print_fps_stats(all_fps_by_int, int2label):
"""
Print some information about the class list and frequency distribution.
"""
n = float(np.sum([len(all_fps_by_int[i]) for i in all_fps_by_int]))
print(('-' * 71))
print(('|%-45s|%4s|%8s|%9s|' % ('CLASS_LABEL', 'INT', 'COUNT', 'PERCENT')))
print(('-' * 71))
for i in all_fps_by_int:
fps = all_fps_by_int[i]
print(('|%-45s|%4d|%8d|%8s%%|' %
(int2label[i], i, len(fps),
str(round(((len(fps) / np.maximum(n, 1)) * 100.0), 1)))))
print(('-' * 71))
def get_class_labels(int2label):
"""
Skips over the unlabeled class
"""
return [int2label[i] for i in range(len(int2label) - 1)]
def print_cm(cm, labels, normalize=True):
"""
Print a confusion matrix.
"""
if normalize:
cm = cm.astype("float") / cm.sum(axis=1)[:, np.newaxis]
columnwidth = max([len(x) for x in labels] + [8]) # 8 is value length
empty_cell = " " * columnwidth
# Print header
print(" " + empty_cell, end=" ")
for label in labels:
print("%{0}s".format(columnwidth) % label, end=" ")
print()
# Print rows
for i, label in enumerate(labels):
print(" %{0}s".format(columnwidth) % label, end=" ")
for j in range(len(labels)):
if normalize:
if not np.isnan(cm[i, j]):
cell = "%{0}.3f".format(columnwidth) % cm[i, j]
else:
cell = (" " * (columnwidth - 1)) + "-"
else:
cell = "%{0}d".format(columnwidth) % cm[i, j]
print(cell, end=" ")
print()
def filt_with_y_mask(arr, y_mask):
"""
"""
y_mask_to_idxs = np.nonzero(y_mask)[0]
return arr[y_mask_to_idxs]
def shannon_entropy(pk, qk, base=2.0):
"""
Compute the shannon entropy between probability distributions P and Q with
a specified base.
"""
return np.sum(pk * np.log(pk / np.maximum(qk, 1e-6))) / np.log(np.maximum(base, 1e-6))
def eliminate_duplicates(dict1, dict2):
"""
Remove duplicates between two dictionaries. The larger of the dictionaries
will have the duplicate values removed from it. Returns the number of
duplicates found.
"""
num_duplicates = 0
if (len(dict1) <= len(dict2)):
smaller_dict = dict1
larger_dict = dict2
else:
smaller_dict = dict2
larger_dict = dict1
for key in list(smaller_dict.keys()):
if (key in larger_dict):
num_duplicates += 1
larger_dict.pop(key)
return num_duplicates
def merge_dicts(all_dicts, rm_duplicates=True, v=False):
"""
Merges a list of dictionaries into 1 dictionary containing all entries.
This function will optionally eliminate duplicated keys.
"""
merged = all_dicts[0].copy() # Start with one dict's keys and values
for i in range(1, len(all_dicts)):
if (rm_duplicates):
num_duplicates = eliminate_duplicates(merged, all_dicts[i])
if (v):
print(('%d duplicates found and removed.' % num_duplicates))
merged.update(all_dicts[i]) # Add a new dictionary's keys and values
return merged