-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemutils.py
More file actions
275 lines (223 loc) · 6.79 KB
/
emutils.py
File metadata and controls
275 lines (223 loc) · 6.79 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
267
268
269
270
271
272
273
274
275
# ----------------------------------------------------
# Electromagnetic Mining Array (EMMA)
# Copyright 2017-2018, Pieter Robyns
# ----------------------------------------------------
import numpy as np
import socket
import fcntl
import struct
BANNER = """ _____ __ __ __ __ _
| ____| \/ | \/ | / \\
| _| | |\/| | |\/| | / _ \\
| |___| | | | | | |/ ___ \\
|_____|_| |_|_| |_/_/ \_\\
|Electromagnetic Mining Array
============================="""
def chunks(input_list, chunk_size):
"""
Divide a list into chunks of size 'chunk_size'.
:param input_list:
:param chunk_size:
:return:
"""
for i in range(0, len(input_list), chunk_size):
yield input_list[i:i+chunk_size]
def partition(input_list, num_partitions):
"""
Divide list in 'num_partitions' partitions.
:param input_list:
:param num_partitions:
:return:
"""
n = int(len(input_list) / num_partitions)
for i in range(0, len(input_list), n):
yield input_list[i:i+n]
def numpy_to_hex(np_array):
"""
Convert numpy array to hex offset.
:param np_array:
:return:
"""
result = ""
for elem in np_array:
result += "{:0>2} ".format(hex(elem)[2:])
return result
def pretty_print_subkey_scores(np_array, limit_rows=20, descending=True):
"""
Print score matrix as a nice table.
:param np_array:
:param limit_rows:
:param descending:
:return:
"""
if type(np_array) != np.ndarray:
raise TypeError("Expected np.ndarray")
elif len(np_array.shape) != 2:
raise ValueError("Expected 2D array")
else:
print('')
num_subkeys = np_array.shape[0]
num_guess_values = np_array.shape[1]
# Sort array
sorted_scores = []
for subkey in range(0, num_subkeys):
sorted_subkey = sorted(zip(np_array[subkey, :], range(num_guess_values)), key=lambda f: f[0], reverse=descending)[0:limit_rows]
sorted_scores.append(sorted_subkey)
# Print header
for subkey in range(0, num_subkeys):
print(" {:>2d} ".format(subkey), end='')
print("\n" + "-"*192)
# Print body
for key_guess in range(0, limit_rows):
for subkey in range(0, num_subkeys):
score, byte = sorted_scores[subkey][key_guess]
print(" {:>4.2f} ({:02x}) |".format(float(score), byte), end='')
print('')
# Source: https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python
def get_ip_address(ifname):
"""
Gets the IP address of an interface.
:param ifname:
:return:
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', bytes(ifname[:15], encoding='utf-8'))
)[20:24])
def conf_to_id(conf):
"""
Converts an EMMA configuration object to a string that represents an ID for the experiment. Useful for storing
models that use a certain configuration to separate directories. The directory name is based on the concatenation
of the id_name parameters of the provided actions in the command line arguments.
:param conf:
:return:
"""
conf_dict = conf.__dict__
result = ""
first = True
if 'actions' in conf_dict:
for action in conf_dict['actions'][:-1]:
if action.id_name is None or action.id_name == "": # Skip empty action
continue
if first:
first = False
else:
result += "-"
result += action.id_name
if 'dataset_id' in conf_dict:
result += "-" + conf_dict['dataset_id']
return result
def conf_has_op(conf, target_op):
"""
Checks whether target_action is in conf.actions, ignoring params
:param conf:
:param target_op:
:return:
"""
for action in conf.actions:
if target_op == action.op:
return True
return False
def conf_get_action(conf, target_op):
"""
TODO: Make dedicated conf object instead of dictionary
Returns actions(s) with op name target_op from conf.
:param conf:
:param target_op:
:return:
"""
result = []
for action in conf.actions:
if target_op == action.op:
result.append(action)
if len(result) == 0:
return None
elif len(result) == 1:
return result[0]
else:
return result
def conf_delete_action(conf, target_op):
"""
TODO: Make dedicated conf object instead of dictionary
Returns actions(s) with op name target_op from conf.
:param conf:
:param target_op:
:return:
"""
for action in conf.actions:
if target_op == action.op:
target = action
break
conf.actions.remove(target)
def shuffle_random_multiple(lists):
"""
Shuffle n same-length lists in the same random order.
:param lists:
:return:
"""
result = []
if len(lists) < 1:
raise EMMAException("shuffle_random_xy expects at least 1 array")
length = len(lists[0])
random_indices = np.arange(length)
np.random.shuffle(random_indices)
for l in lists:
if len(l) != length:
raise EMMAException("Array length %d != %d. Expecting same-size lists." % (len(l), length))
shuffled_l = np.take(l, random_indices, axis=0)
result.append(shuffled_l)
del lists
return result
def int_to_one_hot(integer, num_classes):
"""
Convert integer to one-hot vector with num_classes elements.
:param integer:
:param num_classes:
:return:
"""
r = np.zeros(num_classes, dtype=np.float32)
r[integer] = 1.0
return r
def get_default_keras_loss_names():
"""
Get the default available losses from Keras and return them as list of strings.
:return:
"""
return ['categorical_crossentropy']
def hamming_distance(v1, v2):
"""
Get Hamming distance between integers v1 and v2.
:param v1:
:param v2:
:return:
"""
return bin(v1 ^ v2).count("1")
def random_bytes(n):
"""
Get n random bytes from /dev/urandom and return as numpy array of uint8.
:param n:
:return:
"""
result = None
with open("/dev/urandom", "rb") as f:
result = f.read(n)
return np.array(bytearray(result))
class Window(object):
"""
Helper object for specifying a range between begin (inclusive) and end (exclusive).
"""
def __init__(self, begin, end):
self.begin = begin
self.end = end
if end is not None and begin is not None:
self.size = end - begin
else:
self.size = None
class EMMAException(Exception):
pass
class EMMAConfException(EMMAException):
pass
class MaxPlotsReached(EMMAException):
pass