-
Notifications
You must be signed in to change notification settings - Fork 3
/
homological_utils.py
584 lines (463 loc) · 24 KB
/
homological_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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
import math
from pytorch_lightning.trainer.supporters import CombinedLoader
from sklearn.preprocessing import RobustScaler, StandardScaler, MinMaxScaler
from torch.utils.data import DataLoader
import params
from tmfg_bootstrapped import *
from bootstrapped_network import *
def get_final_X_4(X, final_b_cliques_4):
final_X = None
X = pd.DataFrame(X)
for e, c in enumerate(final_b_cliques_4):
if final_X is None:
final_X = X[final_b_cliques_4[e]]
else:
final_X = pd.concat(
[final_X, X[final_b_cliques_4[e]]], ignore_index=True, axis=1
)
return final_X
def get_final_X_3(X, final_b_cliques_3):
final_X = None
X = pd.DataFrame(X)
for e, c in enumerate(final_b_cliques_3):
if final_X is None:
final_X = X[final_b_cliques_3[e]]
else:
final_X = pd.concat(
[final_X, X[final_b_cliques_3[e]]], ignore_index=True, axis=1
)
return final_X
def get_final_X_2(X, final_b_cliques_2):
final_X = None
X = pd.DataFrame(X)
for e, c in enumerate(final_b_cliques_2):
if final_X is None:
final_X = X[final_b_cliques_2[e]]
else:
final_X = pd.concat(
[final_X, X[final_b_cliques_2[e]]], ignore_index=True, axis=1
)
return final_X
def h_input_transform(X_train, X_val, X_test, y_train, y_val, y_test, tmfg_repetitions, tmfg_confidence,
tmfg_similarity, filtering_type):
if filtering_type == 'TMFG_Bootstrapping':
print('TMFG_Bootstrapping')
print(tmfg_confidence)
cliques, separators, original_tmfg, _, adjacency_matrix = TMFG_Bootstrapped(X_train,
tmfg_similarity,
tmfg_repetitions,
tmfg_confidence,
parallel=True).compute_tmfg_bootstrapping()
else:
print('Stat_Network')
print(tmfg_confidence)
cliques, separators, adjacency_matrix = Bootstrapped_Similarity_Matrix(X_train,
tmfg_similarity,
tmfg_repetitions,
parallel=True).compute_bootstrapping()
original_tmfg = adjacency_matrix
c = nx.degree_centrality(adjacency_matrix)
keys = np.array(list(c.keys()))
values = np.array(list(c.values()))
nodes_list = sorted(list(keys[values != 0]))
print(len(c.keys()))
print(len(nodes_list))
simplexes = []
x = None
x_train = None
x_val = None
x_test = None
for i in nx.enumerate_all_cliques(original_tmfg):
if len(i) == 2:
simplexes.append(sorted(i))
b_cliques_4 = []
b_cliques_3 = []
b_cliques_2 = []
b_cliques_all = nx.enumerate_all_cliques(adjacency_matrix)
for i in b_cliques_all:
if len(i) == 2:
b_cliques_2.append(sorted(i))
if len(i) == 3:
b_cliques_3.append(sorted(i))
if len(i) == 4:
b_cliques_4.append(sorted(i))
final_b_cliques_4 = b_cliques_4
final_b_cliques_3 = b_cliques_3
final_b_cliques_2 = b_cliques_2
# Uncomment to prevent overlapping geometrical structures.
'''
new_b_cliques_3 = []
if len(final_b_cliques_4) == 0:
new_b_cliques_3 = final_b_cliques_3
else:
for t in final_b_cliques_3:
flag = False
for f in final_b_cliques_4:
if set(t).issubset(set(f)):
flag = True
if not flag:
new_b_cliques_3.append(t)
final_b_cliques_3 = new_b_cliques_3
new_b_cliques_2 = []
if len(final_b_cliques_3) == 0:
new_b_cliques_2 = final_b_cliques_2
else:
for t in final_b_cliques_2:
flag = False
for f in final_b_cliques_3:
if set(t).issubset(set(f)):
flag = True
if not flag:
new_b_cliques_2.append(t)
final_b_cliques_2 = new_b_cliques_2
new_b_cliques_2 = []
if len(final_b_cliques_4) == 0:
new_b_cliques_2 = final_b_cliques_2
else:
for t in final_b_cliques_2:
flag = False
for f in final_b_cliques_4:
if set(t).issubset(set(f)):
flag = True
if not flag:
new_b_cliques_2.append(t)
final_b_cliques_2 = new_b_cliques_2
'''
# Comment to prevent overlapping geometrical structures.
try:
if x is None:
final_train_X_4 = get_final_X_4(X_train, final_b_cliques_4)
final_val_X_4 = get_final_X_4(X_val, final_b_cliques_4)
final_test_X_4 = get_final_X_4(X_test, final_b_cliques_4)
else:
final_train_X_4 = get_final_X_4(x_train, final_b_cliques_4)
final_val_X_4 = get_final_X_4(x_val, final_b_cliques_4)
final_test_X_4 = get_final_X_4(x_test, final_b_cliques_4)
except:
final_train_X_4 = None
final_val_X_4 = None
final_test_X_4 = None
try:
if x is None:
final_train_X_3 = get_final_X_3(X_train, final_b_cliques_3)
final_val_X_3 = get_final_X_3(X_val, final_b_cliques_3)
final_test_X_3 = get_final_X_3(X_test, final_b_cliques_3)
else:
final_train_X_3 = get_final_X_3(x_train, final_b_cliques_3)
final_val_X_3 = get_final_X_3(x_val, final_b_cliques_3)
final_test_X_3 = get_final_X_3(x_test, final_b_cliques_3)
except:
final_train_X_3 = None
final_val_X_3 = None
final_test_X_3 = None
try:
if x is None:
final_train_X_2 = get_final_X_2(X_train, final_b_cliques_2)
final_val_X_2 = get_final_X_2(X_val, final_b_cliques_2)
final_test_X_2 = get_final_X_2(X_test, final_b_cliques_2)
else:
final_train_X_2 = get_final_X_2(x_train, final_b_cliques_2)
final_val_X_2 = get_final_X_2(x_val, final_b_cliques_2)
final_test_X_2 = get_final_X_2(x_test, final_b_cliques_2)
except:
final_train_X_2 = None
final_val_X_2 = None
final_test_X_2 = None
try:
scaler = None
if params.SCALING_SCHEME == 'RobustScaler':
scaler = RobustScaler()
elif params.SCALING_SCHEME == 'StandardScaler':
scaler = StandardScaler()
elif params.SCALING_SCHEME == 'MinMaxScaler':
scaler = MinMaxScaler()
final_train_X_4 = scaler.fit_transform(final_train_X_4)
final_val_X_4 = scaler.transform(final_val_X_4)
final_test_X_4 = scaler.transform(final_test_X_4)
except:
pass
try:
scaler = None
if params.SCALING_SCHEME == 'RobustScaler':
scaler = RobustScaler()
elif params.SCALING_SCHEME == 'StandardScaler':
scaler = StandardScaler()
elif params.SCALING_SCHEME == 'MinMaxScaler':
scaler = MinMaxScaler()
final_train_X_3 = scaler.fit_transform(final_train_X_3)
final_val_X_3 = scaler.transform(final_val_X_3)
final_test_X_3 = scaler.transform(final_test_X_3)
except:
pass
try:
scaler = None
if params.SCALING_SCHEME == 'RobustScaler':
scaler = RobustScaler()
elif params.SCALING_SCHEME == 'StandardScaler':
scaler = StandardScaler()
elif params.SCALING_SCHEME == 'MinMaxScaler':
scaler = MinMaxScaler()
final_train_X_2 = scaler.fit_transform(final_train_X_2)
final_val_X_2 = scaler.transform(final_val_X_2)
final_test_X_2 = scaler.transform(final_test_X_2)
except:
pass
try:
final_train_X_4 = final_train_X_4.reshape(final_train_X_4.shape[0], 1, final_train_X_4.shape[1], 1)
final_val_X_4 = final_val_X_4.reshape(final_val_X_4.shape[0], 1, final_val_X_4.shape[1], 1)
final_test_X_4 = final_test_X_4.reshape(final_test_X_4.shape[0], 1, final_test_X_4.shape[1], 1)
except:
pass
try:
final_train_X_3 = final_train_X_3.reshape(final_train_X_3.shape[0], 1, final_train_X_3.shape[1], 1)
final_val_X_3 = final_val_X_3.reshape(final_val_X_3.shape[0], 1, final_val_X_3.shape[1], 1)
final_test_X_3 = final_test_X_3.reshape(final_test_X_3.shape[0], 1, final_test_X_3.shape[1], 1)
except:
pass
try:
final_train_X_2 = final_train_X_2.reshape(final_train_X_2.shape[0], 1, final_train_X_2.shape[1], 1)
final_val_X_2 = final_val_X_2.reshape(final_val_X_2.shape[0], 1, final_val_X_2.shape[1], 1)
final_test_X_2 = final_test_X_2.reshape(final_test_X_2.shape[0], 1, final_test_X_2.shape[1], 1)
except:
pass
print(f'# Cliques: {len(final_b_cliques_4)}')
print(f'# Triangles: {len(final_b_cliques_3)}')
print(f'# Simplexes: {len(final_b_cliques_2)}')
shape_4 = None
shape_3 = None
shape_2 = None
try:
shape_4 = final_train_X_4.shape[2]
except:
pass
try:
shape_3 = final_train_X_3.shape[2]
except:
pass
try:
shape_2 = final_train_X_2.shape[2]
except:
pass
try:
final_train_X_4 = final_train_X_4.reshape(final_train_X_4.shape[0], final_train_X_4.shape[2])
except:
pass
try:
final_train_X_3 = final_train_X_3.reshape(final_train_X_3.shape[0], final_train_X_3.shape[2])
except:
pass
try:
final_train_X_2 = final_train_X_2.reshape(final_train_X_2.shape[0], final_train_X_2.shape[2])
except:
pass
try:
final_val_X_4 = final_val_X_4.reshape(final_val_X_4.shape[0], final_val_X_4.shape[2])
except:
pass
try:
final_val_X_3 = final_val_X_3.reshape(final_val_X_3.shape[0], final_val_X_3.shape[2])
except:
pass
try:
final_val_X_2 = final_val_X_2.reshape(final_val_X_2.shape[0], final_val_X_2.shape[2])
except:
pass
try:
final_test_X_4 = final_test_X_4.reshape(final_test_X_4.shape[0], final_test_X_4.shape[2])
except:
pass
try:
final_test_X_3 = final_test_X_3.reshape(final_test_X_3.shape[0], final_test_X_3.shape[2])
except:
pass
try:
final_test_X_2 = final_test_X_2.reshape(final_test_X_2.shape[0], final_test_X_2.shape[2])
except:
pass
if final_train_X_4 is not None and final_train_X_3 is not None and final_train_X_2 is not None:
X_train = {'tetrahedra': final_train_X_4, 'triangles': final_train_X_3, 'simplex': final_train_X_2}
X_val = {'tetrahedra': final_val_X_4, 'triangles': final_val_X_3, 'simplex': final_val_X_2}
X_test = {'tetrahedra': final_test_X_4, 'triangles': final_test_X_3, 'simplex': final_test_X_2}
if final_train_X_4 is None and final_train_X_3 is not None and final_train_X_2 is not None:
X_train = {'triangles': final_train_X_3, 'simplex': final_train_X_2}
X_val = {'triangles': final_val_X_3, 'simplex': final_val_X_2}
X_test = {'triangles': final_test_X_3, 'simplex': final_test_X_2}
if final_train_X_4 is not None and final_train_X_3 is None and final_train_X_2 is not None:
X_train = {'tetrahedra': final_train_X_4, 'simplex': final_train_X_2}
X_val = {'tetrahedra': final_val_X_4, 'simplex': final_val_X_2}
X_test = {'tetrahedra': final_test_X_4, 'simplex': final_test_X_2}
if final_train_X_4 is not None and final_train_X_3 is None and final_train_X_2 is None:
X_train = {'tetrahedra': final_train_X_4}
X_val = {'tetrahedra': final_val_X_4}
X_test = {'tetrahedra': final_test_X_4}
if final_train_X_4 is None and final_train_X_3 is not None and final_train_X_2 is None:
X_train = {'triangles': final_train_X_3}
X_val = {'triangles': final_val_X_3}
X_test = {'triangles': final_test_X_3}
if final_train_X_4 is None and final_train_X_3 is None and final_train_X_2 is not None:
X_train = {'simplex': final_train_X_2}
X_val = {'simplex': final_val_X_2}
X_test = {'simplex': final_test_X_2}
return len(nodes_list), shape_4, shape_3, shape_2, X_train, X_val, X_test, y_train, y_val, y_test
def prepare_dataloaders(X_train, X_val, X_test, y_train, y_val, y_test, batch_size=32):
combined_loaders_train = None
combined_loaders_val = None
combined_loaders_test = None
# Dataloaders training side.
if 'tetrahedra' in X_train.keys() and 'triangles' in X_train.keys() and 'simplex' in X_train.keys():
loader_tetrahedra = DataLoader(X_train['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_triangles = DataLoader(X_train['triangles'], batch_size=batch_size, drop_last=True)
loader_simplex = DataLoader(X_train['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_train, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, "triangles": loader_triangles, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_train = CombinedLoader(loaders)
if 'tetrahedra' in X_train.keys() and 'triangles' in X_train.keys() and 'simplex' not in X_train.keys():
loader_tetrahedra = DataLoader(X_train['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_triangles = DataLoader(X_train['triangles'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_train, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, "triangles": loader_triangles, 'targets': loader_targets}
combined_loaders_train = CombinedLoader(loaders)
if 'tetrahedra' in X_train.keys() and 'triangles' not in X_train.keys() and 'simplex' in X_train.keys():
loader_tetrahedra = DataLoader(X_train['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_simplex = DataLoader(X_train['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_train, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_train = CombinedLoader(loaders)
if 'tetrahedra' in X_train.keys() and 'triangles' not in X_train.keys() and 'simplex' not in X_train.keys():
loader_tetrahedra = DataLoader(X_train['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_train, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, 'targets': loader_targets}
combined_loaders_train = CombinedLoader(loaders)
if 'tetrahedra' not in X_train.keys() and 'triangles' in X_train.keys() and 'simplex' in X_train.keys():
loader_triangles = DataLoader(X_train['triangles'], batch_size=batch_size, drop_last=True)
loader_simplex = DataLoader(X_train['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_train, batch_size=batch_size, drop_last=True)
loaders = {"triangles": loader_triangles, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_train = CombinedLoader(loaders)
if 'tetrahedra' not in X_train.keys() and 'triangles' in X_train.keys() and 'simplex' not in X_train.keys():
loader_triangles = DataLoader(X_train['triangles'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_train, batch_size=batch_size, drop_last=True)
loaders = {"triangles": loader_triangles, 'targets': loader_targets}
combined_loaders_train = CombinedLoader(loaders)
if 'tetrahedra' not in X_train.keys() and 'triangles' not in X_train.keys() and 'simplex' in X_train.keys():
loader_simplex = DataLoader(X_train['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_train, batch_size=batch_size, drop_last=True)
loaders = {'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_train = CombinedLoader(loaders)
# Dataloaders validation side.
if 'tetrahedra' in X_val.keys() and 'triangles' in X_val.keys() and 'simplex' in X_val.keys():
loader_tetrahedra = DataLoader(X_val['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_triangles = DataLoader(X_val['triangles'], batch_size=batch_size, drop_last=True)
loader_simplex = DataLoader(X_val['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_val, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, "triangles": loader_triangles, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_val = CombinedLoader(loaders)
if 'tetrahedra' in X_val.keys() and 'triangles' in X_val.keys() and 'simplex' not in X_val.keys():
loader_tetrahedra = DataLoader(X_val['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_triangles = DataLoader(X_val['triangles'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_val, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, "triangles": loader_triangles, 'targets': loader_targets}
combined_loaders_val = CombinedLoader(loaders)
if 'tetrahedra' in X_val.keys() and 'triangles' not in X_val.keys() and 'simplex' in X_val.keys():
loader_tetrahedra = DataLoader(X_val['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_simplex = DataLoader(X_val['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_val, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_val = CombinedLoader(loaders)
if 'tetrahedra' in X_val.keys() and 'triangles' not in X_val.keys() and 'simplex' not in X_val.keys():
loader_tetrahedra = DataLoader(X_val['tetrahedra'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_val, batch_size=batch_size, drop_last=True)
loaders = {"tetrahedra": loader_tetrahedra, 'targets': loader_targets}
combined_loaders_val = CombinedLoader(loaders)
if 'tetrahedra' not in X_val.keys() and 'triangles' in X_val.keys() and 'simplex' in X_val.keys():
loader_triangles = DataLoader(X_val['triangles'], batch_size=batch_size, drop_last=True)
loader_simplex = DataLoader(X_val['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_val, batch_size=batch_size, drop_last=True)
loaders = {"triangles": loader_triangles, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_val = CombinedLoader(loaders)
if 'tetrahedra' not in X_val.keys() and 'triangles' in X_val.keys() and 'simplex' not in X_val.keys():
loader_triangles = DataLoader(X_val['triangles'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_val, batch_size=batch_size, drop_last=True)
loaders = {"triangles": loader_triangles, 'targets': loader_targets}
combined_loaders_val = CombinedLoader(loaders)
if 'tetrahedra' not in X_val.keys() and 'triangles' not in X_val.keys() and 'simplex' in X_val.keys():
loader_simplex = DataLoader(X_val['simplex'], batch_size=batch_size, drop_last=True)
loader_targets = DataLoader(y_val, batch_size=batch_size, drop_last=True)
loaders = {'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_val = CombinedLoader(loaders)
# Dataloaders test side.
if 'tetrahedra' in X_test.keys() and 'triangles' in X_test.keys() and 'simplex' in X_test.keys():
loader_tetrahedra = DataLoader(X_test['tetrahedra'], batch_size=batch_size)
loader_triangles = DataLoader(X_test['triangles'], batch_size=batch_size)
loader_simplex = DataLoader(X_test['simplex'], batch_size=batch_size)
loader_targets = DataLoader(y_test, batch_size=batch_size)
loaders = {"tetrahedra": loader_tetrahedra, "triangles": loader_triangles, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_test = CombinedLoader(loaders)
if 'tetrahedra' in X_test.keys() and 'triangles' in X_test.keys() and 'simplex' not in X_test.keys():
loader_tetrahedra = DataLoader(X_test['tetrahedra'], batch_size=batch_size)
loader_triangles = DataLoader(X_test['triangles'], batch_size=batch_size)
loader_targets = DataLoader(y_test, batch_size=batch_size)
loaders = {"tetrahedra": loader_tetrahedra, "triangles": loader_triangles, 'targets': loader_targets}
combined_loaders_test = CombinedLoader(loaders)
if 'tetrahedra' in X_test.keys() and 'triangles' not in X_test.keys() and 'simplex' in X_test.keys():
loader_tetrahedra = DataLoader(X_test['tetrahedra'], batch_size=batch_size)
loader_simplex = DataLoader(X_test['simplex'], batch_size=batch_size)
loader_targets = DataLoader(y_test, batch_size=batch_size)
loaders = {"tetrahedra": loader_tetrahedra, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_test = CombinedLoader(loaders)
if 'tetrahedra' in X_test.keys() and 'triangles' not in X_test.keys() and 'simplex' not in X_test.keys():
loader_tetrahedra = DataLoader(X_test['tetrahedra'], batch_size=batch_size)
loader_targets = DataLoader(y_test, batch_size=batch_size)
loaders = {"tetrahedra": loader_tetrahedra, 'targets': loader_targets}
combined_loaders_test = CombinedLoader(loaders)
if 'tetrahedra' not in X_test.keys() and 'triangles' in X_test.keys() and 'simplex' in X_test.keys():
loader_triangles = DataLoader(X_test['triangles'], batch_size=batch_size)
loader_simplex = DataLoader(X_test['simplex'], batch_size=batch_size)
loader_targets = DataLoader(y_test, batch_size=batch_size)
loaders = {"triangles": loader_triangles, 'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_test = CombinedLoader(loaders)
if 'tetrahedra' not in X_test.keys() and 'triangles' in X_test.keys() and 'simplex' not in X_test.keys():
loader_triangles = DataLoader(X_test['triangles'], batch_size=batch_size)
loader_targets = DataLoader(y_test, batch_size=batch_size)
loaders = {"triangles": loader_triangles, 'targets': loader_targets}
combined_loaders_test = CombinedLoader(loaders)
if 'tetrahedra' not in X_test.keys() and 'triangles' not in X_test.keys() and 'simplex' in X_test.keys():
loader_simplex = DataLoader(X_test['simplex'], batch_size=batch_size)
loader_targets = DataLoader(y_test, batch_size=batch_size)
loaders = {'simplex': loader_simplex, 'targets': loader_targets}
combined_loaders_test = CombinedLoader(loaders)
return combined_loaders_train, combined_loaders_val, combined_loaders_test
def batch_decomposition(batch):
try:
batch_tetrahedra = batch["tetrahedra"]
except:
batch_tetrahedra = None
try:
batch_triangles = batch["triangles"]
except:
batch_triangles = None
try:
batch_simplex = batch["simplex"]
except:
batch_simplex = None
try:
batch_targets = batch["targets"]
except:
batch_targets = None
return batch_tetrahedra, batch_triangles, batch_simplex, batch_targets
def transform_outputs(outputs):
preds_list = []
targets_list = []
probs_list = []
for _ in outputs:
preds = _['preds'].cpu().numpy().tolist()
targets = _['targets'].cpu().numpy().tolist()
probs = _['probs'].cpu().numpy()
preds_list.extend(preds)
targets_list.extend(targets)
probs_list.extend(probs)
probs_list = np.stack(probs_list, axis=0)
return targets_list, preds_list, probs_list
def get_openai_lr(model):
num_params = sum(p.numel() for p in model.parameters())
return 0.01