-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_sim_results_time.py
482 lines (406 loc) · 14.8 KB
/
gen_sim_results_time.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
import numpy as np
from itertools import combinations
from scipy.special import binom
#from indices.indices_info import Indices
from indices_info import Indices
import time
import glob
def _file_name_gen(key):
"""Generate the name of the file containing a given index.
Arguments
---------
key : str
Key of the Indices dict that corresponds to a given index.
Returns
-------
name : str
Name of the file containing the class corresponding to the given index.
"""
parts = key.split("-")
if len(parts) == 1:
name = key.lower()
else:
name = ""
for part in parts[:-1]:
name += part.lower()+"_"
name += parts[-1].lower()
return name
def generate_bitstring(size):
"""Generate a random fingerprint.
Arguments
---------
size : int
Size (or length) of the fingerprint.
Returns
-------
fingerprint : np.ndarray
Fingerprint as a numpy array.
"""
if not isinstance(size, int):
raise TypeError("size can only be an integer.")
return np.random.choice([0, 1], size=size)
def gen_fingerprints(fp_total, fp_size):
"""Generates random fingerprints.
Arguments
---------
fp_total : int
Number of fingerprints.
fp_size : int
Size (or length) of the fingerprints.
Returns
-------
total_fingerprints : np.array
Numpy array containing the fingerprints.
"""
total_fingerprints = []
for i in range(fp_total):
total_fingerprints.append(generate_bitstring(fp_size))
total_fingerprints = np.array(total_fingerprints)
return total_fingerprints
def calc_indices(indices=Indices, fp_total=2,
total_fingerprints=np.array([np.array([1]), np.array([1])]),
n=2, c_threshold=None, w_factor="fraction"):
"""Calculate the indices and generates the output.
Arguments
---------
indices : dict
Dictionary with the indices that will be calculated.
fp_total : int
Total number of fingerprints.
total_fingerprints : np.ndarray
Numpy array containing the fingerprints that will be compared.
n : int
Number of fingerprints that will be compared simultaneously.
c_threshold : {None, 'dissimilar', int}
Coincidence threshold.
w_factor : {"fraction", "power_n"}
Type of weight function that will be used.
Raises
------
TypeError
If n is not an integer.
ValueError
If the number of fingerprints in total_fingerprints is not equal to fp_total.
If n is less than 2.
If n is greater than f_total.
Returns
-------
Results : dict
Dictionary with the results of the comparisons.
"""
if not isinstance(n, int):
raise TypeError("n must be an integer.")
if len(total_fingerprints) != fp_total:
raise ValueError("The number of fingerprints in total_fingerprints must be equal"
"to fp_total.")
if n < 2:
raise ValueError("n cannot be less than 2.")
if n > fp_total:
raise ValueError("n cannot be greater than fp_total.")
# Dictionary that will contain the results of all the comparisons.
# Its structure is: Results[index] = (class_name, [index_values])
Results = {}
for s_index in indices:
for variant in Indices[s_index][2]:
Results[indices[s_index][1] + "_" + variant] = (Indices[s_index][0], [])
# Sets of n numbers that indicate which fingerprints will be compared at a given time.
index_list = list(combinations(range(fp_total), n))
# Populating the Results dict with the results of the comparisons.
for inds in index_list:
fingerprints = total_fingerprints[list(inds)]
for s_index in sorted(Results):
h = "fingerprints=np.array([np.array([1, 0]), np.array([0, 1])]), "
h += "c_threshold = 1, "
h += "w_factor=None"
h = "(" + h + ")"
exec("index = " + Results[s_index][0] + h, None, globals())
index.__init__(fingerprints=fingerprints, c_threshold=c_threshold, w_factor=w_factor)
exec("result = index." + s_index, None, globals())
Results[s_index][1].append(result)
return Results
def _indices_values(results, fp_total=2, n=2, methods=[]):
"""Generate output with the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
fp_total : int
Total number of fingerprints.
n : int
Number of fingerprints that will be compared simultaneously.
methods : list
List that contains the methods used to calculate the indices.
Returns
-------
s : str
String with the results of the comparisons of the given indices.
"""
s = ""
for method in methods:
s += method + " "
s += "\n"
for i in range(int(binom(fp_total, n))):
s += "{:<13d}".format(i + 1)
for method in methods:
l = len(method)
s += "{:^{}.6f} ".format(results[method][1][i], l + 1)
s += "\n"
s += "\n "
for method in methods:
s += method + " "
return s
def _max(results, methods=[]):
"""Generate the maxima of the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
methods : list
List that contains the methods used to calculate the indices.
Returns
-------
s : str
String with the maxima of the comparisons of the given indices.
"""
s = ""
s += "\nMax "
for method in methods:
max = np.amax(np.array(results[method][1]))
l = len(method)
s += "{:^{}.6f} ".format(max, l + 1)
return s
def _min(results, methods=[]):
"""Generate the minima of the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
methods : list
List that contains the methods used to calculate the indices.
Returns
-------
s : str
String with the minima of the comparisons of the given indices.
"""
s = ""
s += "\nMin "
for method in methods:
min = np.amin(np.array(results[method][1]))
l = len(method)
s += "{:^{}.6f} ".format(min, l + 1)
return s
def _abs_max(results, methods=[]):
"""Generate the abs maxima of the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
methods : list
List that contains the methods used to calculate the indices.
Returns
-------
s : str
String with the abs maxima of the comparisons of the given indices.
"""
s = ""
s += "\nAbsMax "
for method in methods:
absmax = np.amax(np.abs(np.array(results[method][1])))
l = len(method)
s += "{:^{}.6f} ".format(absmax, l + 1)
return s
def _abs_min(results, methods=[]):
"""Generate the abs minima of the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
methods : list
List that contains the methods used to calculate the indices.
Returns
-------
s : str
String with the abs minima of the comparisons of the given indices.
"""
s = ""
s += "\nAbsMin "
for method in methods:
absmin = np.amin(np.abs(np.array(results[method][1])))
l = len(method)
s += "{:^{}.6f} ".format(absmin, l + 1)
return s
def _average(results, methods=[]):
"""Generate the averages of the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
methods : list
List that contains the methods used to calculate the indices.
Returns
-------
s : str
String with the averages of the comparisons of the given indices.
"""
s = ""
s += "\nAverage "
for method in methods:
average = np.average(np.array(results[method][1]))
l = len(method)
s += "{:^{}.6f} ".format(average, l + 1)
return s
def _abs_average(results, methods=[]):
"""Generate the abs averages of the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
methods : list
List that contains the methods used to calculate the indices.
Returns
-------
String with the abs averages of the comparisons of the given indices.
"""
s = ""
s += "\nAbsAverage "
for method in methods:
absaverage = np.average(np.abs(np.array(results[method][1])))
l = len(method)
s += "{:^{}.6f} ".format(absaverage, l + 1)
return s
def indices_output(results, fp_total=2, fp_size=1, n=2):
"""Generate output file with the results of the comparisons.
Arguments
---------
results : dict
Dictionary with the results of the comparisons.
fp_total : int
Total number of fingerprints.
fp_size : int
Size of the fingerprints.
n : int
Number of fingerprints that will be compared simultaneously.
"""
# Generic header for the output file.
s = "Similarity analysis\n\n"
s += "Fingerprint size (m)\n" + str(fp_size) + "\n\n"
s += "Total number of fingerprints\n" + str(fp_total) + "\n\n"
s += "Fingerprints compared simultaneously (n)\n" + str(n) + "\n\n"
s += "# "
# Weighted indices.
w = []
# Unweighted indices.
no_w = []
for s_index in sorted(results):
if "w" in s_index:
w.append(s_index)
else:
no_w.append(s_index)
r_w = s
r_no_w = s
r_w += _indices_values(results=results, fp_total=fp_total, n=n, methods=w)
r_no_w += _indices_values(results=results, fp_total=fp_total, n=n, methods=no_w)
r_w += _max(results=results, methods=w)
r_no_w += _max(results=results, methods=no_w)
r_w += _abs_max(results=results, methods=w)
r_no_w += _abs_max(results=results, methods=no_w)
r_w += _min(results=results, methods=w)
r_no_w += _min(results=results, methods=no_w)
r_w += _abs_min(results=results, methods=w)
r_no_w += _abs_min(results=results, methods=no_w)
r_w += _average(results=results, methods=w)
r_no_w += _average(results=results, methods=no_w)
r_w += _abs_average(results=results, methods=w)
r_no_w += _abs_average(results=results, methods=no_w)
with open("wFP"+str(fp_total)+"m"+str(fp_size)+"n"+str(n)+".sim", "w") as outfile:
outfile.write(r_w)
with open("nwFP"+str(fp_total)+"m"+str(fp_size)+"n"+str(n)+".sim", "w") as outfile:
outfile.write(r_no_w)
def read_fps(file):
with open(file, "r") as infile:
raw_lines = infile.readlines()
fp_total = len(raw_lines)
total_fingerprints = []
for line in raw_lines:
s_fp = line.strip().split()[-1]
np_fp = np.fromstring(s_fp, 'u1') - ord('0')
total_fingerprints.append(np_fp)
fp_size = len(total_fingerprints[0])
total_fingerprints = np.array(total_fingerprints)
return fp_total, fp_size, total_fingerprints
def time_output(t_summary, comp, fp_type, t_out_name):
if t_out_name:
pass
else:
names = []
for key in t_summary:
if fp_type == "MACCS":
name = key.split("_")[0] + "_" + key.split("_")[2]
else:
name = key.split("_")[0] + "_" + key.split("_")[2] + "_" + key.split("_")[3]
if name not in names:
names.append(name)
if len(names) == 1:
t_out_name = names[0] + "_{}.time".format(comp)
else:
raise TypeError("Please, provide an unambiguous name for the output file.")
s = "{:20}".format("fp_total")
s += "{:20}".format("n")
s += "{:20}\n".format("time (s)")
for key in reversed(sorted(t_summary)):
s += "{:<20}{:<20}{:<20.6f}\n".format(t_summary[key]["fp_total"],
t_summary[key]["n"], t_summary[key]["time"])
with open(t_out_name, "w") as outfile:
outfile.write(s)
if __name__ == "__main__":
# Imports the classes corresponding to the similarity indices.
for key in Indices:
# s = "from indices." + file_name_gen(key) + " import " + Indices[key][0]
s = "from " + _file_name_gen(key) + " import " + Indices[key][0]
exec(s)
# Sample run with randomly generated fingerprints.
# Coincidence threshold.
c_threshold = None
# Weight factor.
w_factor = "fraction"
# List of files with the fingerprints.
fp_file_list = glob.glob("*.txt")
t_summary = {}
if fp_file_list:
for fp_file in fp_file_list:
fp_total, fp_size, total_fingerprints = read_fps(fp_file)
# n is the number of fingerprints that will be compared simultaneously.
# Here we will focus on two cases mainly:
# n = 2 binary comparisons
# n = fp_total n-ary comparisons
n = fp_total
t_summary[fp_file.split(".")[0]] = {"fp_total": fp_total,
"n": n,
"time": None}
start_time = time.time()
results = calc_indices(indices=Indices, fp_total=fp_total,
total_fingerprints=total_fingerprints,
n=n, c_threshold=c_threshold, w_factor=w_factor)
run_time = time.time() - start_time
t_summary[fp_file.split(".")[0]]["time"] = run_time
indices_output(results=results, fp_total=fp_total, fp_size=fp_size, n=n)
if n == 2:
comp = "binary"
else:
comp = "n-ary"
fp_type = "MACCS"
time_output(t_summary, comp, fp_type, t_out_name=None)
else:
# Fingerprint sizes. The analysis will be ran for every given size.
fp_sizes = [1000]
# Total fingerprint numbers. The analysis will be ran for every set of fingerprints.
fp_totals = [100]
for fp_size in fp_sizes:
for fp_total in fp_totals:
total_fingerprints = gen_fingerprints(fp_total, fp_size)
for n in [2, fp_totals[0]]:
# n values. Possible values are 2 <= n <= fp_total.
results = calc_indices(indices=Indices, fp_total=fp_total,
total_fingerprints=total_fingerprints,
n=n, c_threshold=c_threshold, w_factor=w_factor)
indices_output(results=results, fp_total=fp_total, fp_size=fp_size, n=n)