-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
1395 lines (1017 loc) · 50.7 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
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import itertools
import json
import glob
import os
import pickle
import platform
import random
import re
import sys
from decimal import Decimal
from typing import Dict, Tuple, List, Union
import networkx as nx
import numpy as np
import pandas as pd
import psutil
import seaborn as sns
import torch
import yaml
from matplotlib import pyplot as plt
from pgmpy.factors.discrete import TabularCPD
from pgmpy.models import BayesianNetwork
from scipy.cluster.hierarchy import linkage, fcluster, dendrogram
from scipy.spatial.distance import squareform
from scipy.stats import ks_2samp
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from tqdm import tqdm
from causality_vmas import LABEL_kind_group_var, LABEL_value_group_var, LABEL_approximation_parameters, \
LABEL_dataframe_approximated, LABEL_discrete_intervals, LABEL_grouped_features
" ******************************************************************************************************************** "
def detach_and_cpu(tensor):
"""Detach a tensor from the computation graph and move it to the CPU."""
return tensor.detach().cpu()
def detach_dict(d, func=detach_and_cpu):
"""Recursively apply a function to all tensors in a dictionary, list, or tuple."""
if isinstance(d, dict):
return {k: detach_dict(v, func) for k, v in d.items()}
elif isinstance(d, list):
return [detach_dict(v, func) for v in d]
elif isinstance(d, tuple):
return tuple(detach_dict(v, func) for v in d)
elif isinstance(d, torch.Tensor):
return func(d)
else:
return d
" ******************************************************************************************************************** "
def list_to_graph(graph: list) -> nx.DiGraph:
# Create a new directed graph
dg = nx.DiGraph()
# Add edges to the directed graph
for cause, effect in graph:
dg.add_edge(cause, effect)
return dg
def graph_to_list(digraph: nx.DiGraph) -> list:
return list(digraph.edges())
def get_parents_and_children_sequence(graph: nx.DiGraph):
# Dictionary to store parents and children for each node
parent_child_sequence = {}
for node in graph.nodes:
# Get parents of the current node
parents = list(graph.predecessors(node))
# Get children of the current node
children = list(graph.successors(node))
# Store the parents and children in the dictionary
parent_child_sequence[node] = {
"parents": parents,
"children": children
}
return parent_child_sequence
" ******************************************************************************************************************** "
def IQM_mean_std(data: list) -> tuple:
# Convert data to a numpy array
data_array = np.array(data)
# Sort the data
sorted_data = np.sort(data_array)
# Calculate quartiles
Q1 = np.percentile(sorted_data, 25)
Q3 = np.percentile(sorted_data, 75)
# Calculate IQR
IQR = Q3 - Q1
# Find indices of data within 1.5*IQR from Q1 and Q3
lower_bound = Q1 - 1.5 * IQR
upper_bound = Q3 + 1.5 * IQR
within_iqr_indices = np.where((sorted_data >= lower_bound) & (sorted_data <= upper_bound))[0]
# Calculate IQM (Interquartile Mean)
try:
iq_mean = Decimal(np.mean(sorted_data[within_iqr_indices])).quantize(Decimal('0.001'))
except:
iq_mean = np.mean(sorted_data[within_iqr_indices])
# Calculate IQM standard deviation (IQM_std)
try:
iq_std = Decimal(np.std(sorted_data[within_iqr_indices])).quantize(Decimal('0.001'))
except:
iq_std = np.std(sorted_data[within_iqr_indices])
return iq_mean, iq_std
"""def compute_avg_series_for_agent(agent_data, metric_key):
episode_mean_series = []
timestep_data = {}
# Iterate through each episode
for episode in range(len(agent_data[metric_key])):
# Collect data for each timestep across all environments within the current episode
for step in range(len(agent_data[metric_key][episode])):
for env in range(len(agent_data[metric_key][episode][step])):
data_series = agent_data[metric_key][episode][step][env]
if step not in timestep_data:
timestep_data[step] = []
if data_series:
timestep_data[step].append(data_series)
mean_list = []
for step, data_series_list in timestep_data.items():
combined_data = [value for series in data_series_list for value in series]
if combined_data:
if metric_key == 'rewards':
mean_list.append(np.mean(combined_data))
elif metric_key == 'time':
mean_list.append(np.sum(combined_data))
elif metric_key == 'n_collisions':
mean_list.append(len([element for element in combined_data if element != 0]))
elif metric_key == 'actions':
mean_list.append(np.sum([element for element in combined_data if element != 0]))
episode_mean_series.append(mean_list)
avg_mean_series = np.mean(np.array(episode_mean_series), axis=0).tolist()
return avg_mean_series"""
def compute_avg_series_for_agent(agent_data, metric_key):
n_episodes = len(agent_data[metric_key])
n_steps = len(agent_data[metric_key][0])
n_env = len(agent_data[metric_key][0][0])
list_episodes_series = []
for episode in range(n_episodes):
episode_series = []
for step in range(n_steps):
value_env = 0
for env in range(n_env):
for value in agent_data[metric_key][episode][step][env]:
if metric_key == 'rewards' or metric_key == 'time':
value_env += value
elif metric_key == 'actions':
value_env += 1
elif metric_key == 'n_collisions':
value_env += 1 if value != 0 else 0
episode_series.append(value_env / n_env)
list_episodes_series.append(episode_series)
mean_list = np.mean(np.array(list_episodes_series), axis=0).tolist()
return mean_list
" ******************************************************************************************************************** "
def _state_to_tuple(state):
"""Convert tensor state to a tuple to be used as a dictionary key."""
return tuple(state.cpu().numpy())
def exploration_action(action_reward_values: Dict) -> int:
weights = [s / (sum(action_reward_values.values())) for s in action_reward_values.values()]
return random.choices(list(action_reward_values.keys()), weights=weights, k=1)[0]
def get_rl_knowledge(filepath, agent_id):
with open(f'{filepath}', 'r') as file:
data = json.load(file)
# Generate the agent key
agent_key = f'agent_{agent_id}'
# Retrieve the rl_knowledge for the given agent
if agent_key in data:
return data[agent_key]['rl_knowledge']
else:
raise KeyError(f'Agent ID {agent_id} not found in the data')
" ******************************************************************************************************************** "
def get_df_boundaries(dataframe: pd.DataFrame):
for col in dataframe.columns.to_list():
# if isinstance(dataframe[col][0], (int, float, np.int64, np.float64)):
iqm_mean, iqm_std = IQM_mean_std(dataframe[col])
print(
f'{col} -> unique_values: {len(dataframe[col].value_counts())}, max: {dataframe[col].max()}, min: {dataframe[col].min()}, mean: {dataframe[col].mean()}, std: {dataframe[col].std()}, iqm_mean: {iqm_mean}, iqm_std: {iqm_std}')
# fig = plt.figure(dpi=500, figsize=(16, 9))
# plt.plot(dataframe['agent_0_reward'])
# plt.show()
" ******************************************************************************************************************** "
FONT_SIZE_NODE_GRAPH = 7
ARROWS_SIZE_NODE_GRAPH = 30
NODE_SIZE_GRAPH = 1000
def plot_graph(graph: nx.DiGraph, title: str, if_show: bool = False, path_name_save: str = None):
import warnings
warnings.filterwarnings("ignore")
fig = plt.figure(dpi=1000)
plt.title(title, fontsize=16)
nx.draw(graph, with_labels=True, font_size=FONT_SIZE_NODE_GRAPH,
arrowsize=ARROWS_SIZE_NODE_GRAPH, arrows=True,
edge_color='orange', node_size=NODE_SIZE_GRAPH, font_weight='bold', node_color='skyblue',
pos=nx.circular_layout(graph))
structure_to_save = graph_to_list(graph)
if path_name_save is not None:
plt.savefig(f'{path_name_save}.png')
with open(f'{path_name_save}.json', 'w') as json_file:
json.dump(structure_to_save, json_file)
if if_show:
plt.show()
plt.close(fig)
def _get_numeric_suffix(var, label):
if label in var:
try:
return int(var.split('_')[-1])
except ValueError:
return None
return None
def constraints_causal_graph(causal_graph: nx.DiGraph):
# no value_sensor -> value_sensor or kind_sensor -> kind_sensor
# no value_sensor1 -> kind_sensor0 or kind_sensor1 -> value_sensor0
# actions have no parents
# Remove edges where both nodes are value_sensor or both are kind_sensor
edges_to_remove_sensor = [(u, v) for u, v in causal_graph.edges() if
(LABEL_value_group_var in u and LABEL_value_group_var in v) or
(LABEL_kind_group_var in u and LABEL_kind_group_var in v)]
causal_graph.remove_edges_from(edges_to_remove_sensor)
# Remove edges where one node is value_sensor and the other is kind_sensor
edges_to_remove_sensor = [(u, v) for u, v in causal_graph.edges() if
((_get_numeric_suffix(u, LABEL_value_group_var) is not None and
_get_numeric_suffix(v, LABEL_kind_group_var) is not None) or
(_get_numeric_suffix(v, LABEL_value_group_var) is not None and
_get_numeric_suffix(u, LABEL_kind_group_var) is not None))]
causal_graph.remove_edges_from(edges_to_remove_sensor)
# Remove edges where the target node has "action" in its label (no parents for action nodes)
edges_to_remove_action = [(u, v) for u, v in causal_graph.edges() if 'action' in v]
causal_graph.remove_edges_from(edges_to_remove_action)
# Remove outgoing edges from nodes labeled "reward" (no children for reward nodes)
edges_to_remove_reward = [(u, v) for u, v in causal_graph.edges() if 'reward' in u]
causal_graph.remove_edges_from(edges_to_remove_reward)
# Add edges between action and reward if they don't already exist
action_nodes = [n for n in causal_graph.nodes() if 'action' in n]
reward_nodes = [n for n in causal_graph.nodes() if 'reward' in n]
for action_node in action_nodes:
for reward_node in reward_nodes:
if not causal_graph.has_edge(action_node, reward_node):
causal_graph.add_edge(action_node, reward_node)
# print(causal_graph)
return causal_graph
def markov_blanket(graph: nx.DiGraph, target: str) -> nx.DiGraph:
# Parents of the target (nodes with edges to the target)
parents = set(graph.predecessors(target))
# Children of the target (nodes that the target points to)
children = set(graph.successors(target))
# Markov blanket is the union of parents, children, and the target itself
markov_blanket_nodes = parents.union(children).union({target})
# Return the Markov blanket subgraph
return graph.subgraph(markov_blanket_nodes)
" ******************************************************************************************************************** "
def find_bin(value: int, intervals: list[float]) -> float | int:
if isinstance(value, (int, float)):
if np.isnan(value): # Check if the value is NaN
return np.NaN
if value == intervals[-1]:
return len(intervals) - 2
return next(i for i in range(len(intervals) - 1) if intervals[i] <= value < intervals[i + 1])
else:
return np.NaN
def values_to_bins(values: List, intervals: List) -> List[int]:
# Assuming intervals is a list where the first element is a label and the second is the list of floats
label, interval_values = intervals
# Sort only the interval values
sorted_intervals = sorted(interval_values) # Ensure intervals are in ascending order
# Now apply the find_bin function using the sorted intervals
results = list(map(lambda value: find_bin(value, sorted_intervals), values))
return results
def discretize_value(value: int | float, intervals: List) -> int | float:
idx = np.digitize(value, intervals, right=False)
if idx == 0:
return intervals[0]
elif idx >= len(intervals):
return intervals[-1]
else:
if abs(value - intervals[idx - 1]) <= abs(value - intervals[idx]):
return intervals[idx - 1]
else:
return intervals[idx]
def _create_intervals(min_val: int | float, max_val: int | float, n_intervals: int, scale='linear') -> List:
if scale == 'exponential':
# Generate n_intervals points using exponential scaling
intervals = np.logspace(0, 1, n_intervals, base=10) - 1
intervals = intervals / (10 - 1) # Normalize to range 0-1
intervals = min_val + (max_val - min_val) * intervals
elif scale == 'linear':
intervals = np.linspace(min_val, max_val, n_intervals)
else:
raise ValueError("Unsupported scale type. Use 'exponential' or 'linear'.")
return list(intervals)
def check_discretization(df: pd.DataFrame, col: str, n_bins: int, not_discretize_these: List):
unique_values = df[col].unique()
if len(unique_values) > n_bins and col not in not_discretize_these:
print(f'*** {n_bins} bins) Discretization problem in {col}: {len(unique_values)}, {unique_values} ***')
def discretize_column(column, intervals):
return column.apply(lambda x: discretize_value(x, intervals))
def discretize_dataframe(df: pd.DataFrame, n_bins: int = 50, scale='linear', not_discretize_these: List = None):
not_discretize_these = not_discretize_these or []
def process_column(column):
if column.name not in not_discretize_these:
min_value = column.min()
max_value = column.max()
intervals = _create_intervals(min_value, max_value, n_bins, scale)
return discretize_column(column, intervals), (column.name, intervals)
return column, None
results = [process_column(df[col]) for col in df.columns]
discrete_df = pd.DataFrame({col: res[0] for col, res in zip(df.columns, results)})
variable_discrete_intervals = {col: res[1] for col, res in zip(df.columns, results) if res[1] is not None}
return discrete_df, variable_discrete_intervals
def group_row_variables(input_obs: Union[Dict, List], variables_to_group: list, N: int = 1) -> Dict:
def get_row_dict(obs, variable_columns):
return {i: obs[i] for i in variable_columns} if isinstance(obs, list) else {col: obs[col] for col in
variable_columns}
def process_variable(i, variable_name, variable_value):
try:
variable_number = ''.join(filter(str.isdigit, str(variable_name)))
kind_key = f'{LABEL_kind_group_var}_{i}'
value_key = f'{LABEL_value_group_var}_{i}'
return {
kind_key: int(variable_number),
value_key: variable_value,
}
except (IndexError, ValueError):
return {
f'{LABEL_kind_group_var}_{i}': None,
f'{LABEL_value_group_var}_{i}': None,
}
def remove_variable_columns(obs, variable_columns):
if not isinstance(obs, list):
return {k: v for k, v in obs.items() if k not in variable_columns}
else:
return [None if i in variable_columns else v for i, v in enumerate(obs)]
obs = input_obs.copy()
row_dict = get_row_dict(obs, variables_to_group)
# Handling None values during sorting
sorted_variables = sorted(row_dict.items(),
key=lambda x: (x[1] is None, x[1] if x[1] is not None else float('-inf')), reverse=True)[
:N]
obs_grouped = {k: v for d in [process_variable(i, k, v) for i, (k, v) in enumerate(sorted_variables)] for k, v in
d.items()}
# Add empty groups if N is greater than the number of sorted variables
for i in range(len(sorted_variables), N):
obs_grouped[f'{LABEL_kind_group_var}_{i}'] = None
obs_grouped[f'{LABEL_value_group_var}_{i}'] = None
obs = remove_variable_columns(obs, variables_to_group)
# Combine the remaining original observation and grouped parts
combined_obs = {**obs, **obs_grouped}
return combined_obs
def group_df_variables(dataframe: pd.DataFrame, variable_to_group: str | list, N: int = 1) -> pd.DataFrame:
first_key = LABEL_kind_group_var
second_key = LABEL_value_group_var
# Step 1: Identify columns to group
if isinstance(variable_to_group, str):
variable_columns = [col for col in dataframe.columns if variable_to_group in col]
elif isinstance(variable_to_group, list):
variable_columns = [col for col in dataframe.columns if col in variable_to_group]
else:
raise ValueError('variable to group must be list or str')
# Step 2: Create columns for the top N variables and their max values
for i in range(N):
dataframe[f'{first_key}_{i}'] = None
dataframe[f'{second_key}_{i}'] = None
# Step 3: Determine top N variable values per row using group_variables function
grouped_data = dataframe.apply(lambda row: group_row_variables(row, variable_columns, N), axis=1)
for i in range(N):
dataframe[f'{first_key}_{i}'] = grouped_data.apply(lambda x: x[f'{first_key}_{i}'])
dataframe[f'{second_key}_{i}'] = grouped_data.apply(lambda x: x[f'{second_key}_{i}'])
# Step 5: Drop original variable columns if needed
dataframe.drop(columns=variable_columns, inplace=True)
return dataframe
def _navigation_approximation(input_elements: Tuple[pd.DataFrame, Dict]) -> Dict:
df, params = input_elements
n_bins = params.get('n_bins', 20)
n_rays = params.get('n_rays', 1)
n_rows = params.get('n_rows', int(len(df) / 2))
variables_to_group = df.columns.to_list()[6:-2]
df = group_df_variables(df, variables_to_group, n_rays)
not_discretize_these = [s for s in df.columns.to_list() if
len(df[s].unique()) <= n_bins or
LABEL_kind_group_var in s or
'action' in s]
df, discrete_intervals = discretize_dataframe(df, n_bins, not_discretize_these=not_discretize_these)
new_df = df.loc[:n_rows - 1, :]
map(lambda col: check_discretization(new_df, col, n_bins, not_discretize_these), new_df.columns.to_list())
approx_dict = {LABEL_approximation_parameters: {'n_bins': n_bins, 'n_rays': n_rays, 'n_rows': n_rows},
LABEL_discrete_intervals: discrete_intervals,
LABEL_dataframe_approximated: new_df,
LABEL_grouped_features: (n_rays, variables_to_group)}
return approx_dict
def _navigation_inverse_approximation(input_obs: Dict, **kwargs) -> Dict:
n_groups, features_group = kwargs[LABEL_grouped_features] # 2, [obs4-ob5-...]
obs_grouped = group_row_variables(input_obs, features_group, n_groups)
discrete_intervals = kwargs[LABEL_discrete_intervals]
final_obs = {key: discretize_value(value, discrete_intervals[key]) for key, value in obs_grouped.items() if key in discrete_intervals.keys()}
return final_obs
def _discovery_approximation(input_elements: Tuple[pd.DataFrame, Dict]) -> Dict:
df, params = input_elements
n_bins = params.get('n_bins', 20)
n_rays = params.get('n_rays', 1)
n_rows = params.get('n_rows', int(len(df) / 2))
variables_to_group = df.columns.to_list()[6:-2]
df = group_df_variables(df, variables_to_group, n_rays)
not_discretize_these = [s for s in df.columns.to_list() if
len(df[s].unique()) <= n_bins or
LABEL_kind_group_var in s or
'action' in s]
df, discrete_intervals = discretize_dataframe(df, n_bins, not_discretize_these=not_discretize_these)
new_df = df.loc[:n_rows - 1, :]
map(lambda col: check_discretization(new_df, col, n_bins, not_discretize_these), new_df.columns.to_list())
approx_dict = {LABEL_approximation_parameters: {'n_bins': n_bins, 'n_rays': n_rays, 'n_rows': n_rows},
LABEL_discrete_intervals: discrete_intervals,
LABEL_dataframe_approximated: new_df,
LABEL_grouped_features: (n_rays, variables_to_group)}
return approx_dict
def _discovery_inverse_approximation(input_obs: Dict, **kwargs) -> Dict:
n_groups, features_group = kwargs[LABEL_grouped_features] # 2, [obs4-ob5-...]
obs_grouped = group_row_variables(input_obs, features_group, n_groups)
discrete_intervals = kwargs[LABEL_discrete_intervals]
final_obs = {key: discretize_value(value, discrete_intervals[key]) for key, value in obs_grouped.items() if key in discrete_intervals.keys()}
return final_obs
def _flocking_approximation(input_elements: Tuple[pd.DataFrame, Dict]) -> Dict:
df, params = input_elements
n_bins = params.get('n_bins', 20)
n_rays = params.get('n_rays', 1)
n_rows = params.get('n_rows', int(len(df) / 2))
variables_to_group = df.columns.to_list()[6:-2]
df = group_df_variables(df, variables_to_group, n_rays)
not_discretize_these = [s for s in df.columns.to_list() if
len(df[s].unique()) <= n_bins or
LABEL_kind_group_var in s or
'action' in s]
df, discrete_intervals = discretize_dataframe(df, n_bins, not_discretize_these=not_discretize_these)
new_df = df.loc[:n_rows - 1, :]
map(lambda col: check_discretization(new_df, col, n_bins, not_discretize_these), new_df.columns.to_list())
approx_dict = {LABEL_approximation_parameters: {'n_bins': n_bins, 'n_rays': n_rays, 'n_rows': n_rows},
LABEL_discrete_intervals: discrete_intervals,
LABEL_dataframe_approximated: new_df,
LABEL_grouped_features: (n_rays, variables_to_group)}
return approx_dict
def _flocking_inverse_approximation(input_obs: Dict, **kwargs) -> Dict:
n_groups, features_group = kwargs[LABEL_grouped_features] # 2, [obs4-ob5-...]
obs_grouped = group_row_variables(input_obs, features_group, n_groups)
discrete_intervals = kwargs[LABEL_discrete_intervals]
final_obs = {key: discretize_value(value, discrete_intervals[key]) for key, value in obs_grouped.items() if key in discrete_intervals.keys()}
return final_obs
def _give_way_approximation(input_elements: Tuple[pd.DataFrame, Dict]) -> Dict:
df, params = input_elements
n_bins = params.get('n_bins', 20)
n_rows = params.get('n_rows', int(len(df) / 2))
not_discretize_these = [s for s in df.columns.to_list() if
len(df[s].unique()) <= n_bins or
LABEL_kind_group_var in s or
'action' in s]
df, discrete_intervals = discretize_dataframe(df, n_bins, not_discretize_these=not_discretize_these)
new_df = df.loc[:n_rows - 1, :]
map(lambda col: check_discretization(new_df, col, n_bins, not_discretize_these), new_df.columns.to_list())
approx_dict = {LABEL_approximation_parameters: {'n_bins': n_bins, 'n_rows': n_rows},
LABEL_discrete_intervals: discrete_intervals,
LABEL_dataframe_approximated: new_df,
LABEL_grouped_features: (0, [])}
return approx_dict
def _give_way_inverse_approximation(input_obs: Dict, **kwargs) -> Dict:
discrete_intervals = kwargs[LABEL_discrete_intervals]
final_obs = {key: discretize_value(value, discrete_intervals[key]) for key, value in input_obs.items() if key in discrete_intervals}
return final_obs
def _balance_inverse_approximation(input_obs: Dict, **kwargs) -> Dict:
discrete_intervals = kwargs[LABEL_discrete_intervals]
final_obs = {key: discretize_value(value, discrete_intervals[key]) for key, value in input_obs.items() if key in discrete_intervals}
return final_obs
def _balance_approximation(input_elements: Tuple[pd.DataFrame, Dict]) -> Dict:
df, params = input_elements
n_bins = params.get('n_bins', 20)
n_rows = params.get('n_rows', int(len(df) / 2))
not_discretize_these = [s for s in df.columns.to_list() if
len(df[s].unique()) <= n_bins or
LABEL_kind_group_var in s or
'action' in s]
new_df, discrete_intervals = discretize_dataframe(df, n_bins, not_discretize_these=not_discretize_these)
new_df = new_df.loc[:n_rows - 1, :]
map(lambda col: check_discretization(new_df, col, n_bins, not_discretize_these), new_df.columns.to_list())
approx_dict = {LABEL_approximation_parameters: {'n_bins': n_bins, 'n_rows': n_rows},
LABEL_discrete_intervals: discrete_intervals,
LABEL_dataframe_approximated: new_df,
LABEL_grouped_features: (0, [])}
return approx_dict
def _dropout_approximation(input_elements: Tuple[pd.DataFrame, Dict]) -> Dict:
df, params = input_elements
n_bins = params.get('n_bins', 20)
n_rows = params.get('n_rows', int(len(df) / 2))
not_discretize_these = [s for s in df.columns.to_list() if
len(df[s].unique()) <= n_bins or
LABEL_kind_group_var in s or
'action' in s]
df, discrete_intervals = discretize_dataframe(df, n_bins, not_discretize_these=not_discretize_these)
new_df = df.loc[:n_rows - 1, :]
map(lambda col: check_discretization(new_df, col, n_bins, not_discretize_these), new_df.columns.to_list())
approx_dict = {LABEL_approximation_parameters: {'n_bins': n_bins, 'n_rows': n_rows},
LABEL_discrete_intervals: discrete_intervals,
LABEL_dataframe_approximated: new_df,
LABEL_grouped_features: (0, [])}
return approx_dict
def _dropout_inverse_approximation(input_obs: Dict, **kwargs) -> Dict:
discrete_intervals = kwargs[LABEL_discrete_intervals]
final_obs = {key: discretize_value(value, discrete_intervals[key]) for key, value in input_obs.items() if key in discrete_intervals}
return final_obs
def inverse_approximation_function(task: str):
if task == 'navigation':
return _navigation_inverse_approximation
elif task == 'discovery':
return _discovery_inverse_approximation
elif task == 'flocking':
return _flocking_inverse_approximation
elif task == 'give_way':
return _give_way_inverse_approximation
elif task == 'balance':
return _balance_inverse_approximation
elif task == 'dropout':
return _dropout_inverse_approximation
elif task == 'wheel':
raise NotImplementedError('not implemented yet')
else:
raise NotImplementedError("The inverse approximation function for this task has not been implemented")
def my_approximation(df_original: pd.DataFrame, task_name: str) -> List[Dict]:
df = df_original.copy()
with open(f'./s2_0_params_sensitive_analysis.yaml', 'r') as file:
config_approximation = yaml.safe_load(file)
approx_params_task = config_approximation[task_name]
params = {key: values for key, values in approx_params_task.items()}
all_combs = list(itertools.product(*params.values()))
formatted_combinations = [dict(zip(params.keys(), comb)) for comb in all_combs]
all_params_list = [(df.copy(), single_task_combo_params) for single_task_combo_params in formatted_combinations]
if task_name == 'navigation':
approximator = _navigation_approximation
elif task_name == 'flocking':
approximator = _flocking_approximation
elif task_name == 'discovery':
approximator = _discovery_approximation
elif task_name == 'give_way':
approximator = _give_way_approximation
elif task_name == 'balance':
approximator = _balance_approximation
elif task_name == 'dropout':
approximator = _dropout_approximation
elif task_name == 'wheel':
approximator = _dropout_approximation
# TODO: others
else:
raise NotImplementedError("The approximation function for this task has not been implemented")
approximations = list(map(approximator, tqdm(all_params_list,
total=len(all_params_list),
desc='Computing approximations...')))
return approximations
" ******************************************************************************************************************** "
def _get_next_index(folder_path, prefix):
existing_files = os.listdir(folder_path)
indices = []
for filename in existing_files:
if filename.startswith(prefix):
idx_str = filename[len(prefix):].split('.')[0]
if idx_str.isdigit():
indices.append(int(idx_str))
return max(indices) + 1 if indices else 0
def save_file_incrementally(file_content, folder_path, prefix, extension, is_binary=False):
os.makedirs(folder_path, exist_ok=True)
next_index = _get_next_index(folder_path, prefix)
file_path = os.path.join(folder_path, f'{prefix}{next_index}.{extension}')
if extension == 'pkl':
with open(file_path, 'wb') as f:
pickle.dump(file_content, f)
elif is_binary:
with open(file_path, 'wb') as f:
f.write(file_content)
else:
with open(file_path, 'w') as f:
f.write(file_content)
# print(f'Saved file to {file_path}')
def save_json_incrementally(data, folder_path, prefix):
os.makedirs(folder_path, exist_ok=True)
next_index = _get_next_index(folder_path, prefix)
file_path = os.path.join(folder_path, f'{prefix}{next_index}.json')
with open(file_path, 'w') as f:
json.dump(data, f, indent=8)
# print(f'Saved JSON to {file_path}')
" ******************************************************************************************************************** "
def is_folder_empty(folder_path):
# Get the list of files and directories in the specified folder
contents = os.listdir(folder_path)
# Check if the list is empty
if not contents:
return True
else:
return False
" ******************************************************************************************************************** "
def bn_to_dict(model: BayesianNetwork):
model_data = {
"nodes": list(model.nodes()),
"edges": list(model.edges()),
"cpds": {}
}
for cpd in model.get_cpds():
cpd_data = {
"variable": cpd.variable,
"variable_card": cpd.variable_card,
"values": cpd.values.tolist(),
"evidence": cpd.variables[1:],
"evidence_card": cpd.cardinality[1:].tolist() if len(cpd.variables) > 1 else [],
"state_names": cpd.state_names
}
model_data["cpds"][cpd.variable] = cpd_data
return model_data
def dict_to_bn(model_data) -> BayesianNetwork:
model = BayesianNetwork()
model.add_nodes_from(model_data["nodes"])
model.add_edges_from(model_data["edges"])
for variable, cpd_data in model_data["cpds"].items():
variable_card = cpd_data["variable_card"]
evidence_card = cpd_data["evidence_card"]
values = np.array(cpd_data["values"])
if evidence_card:
values = values.reshape(variable_card, np.prod(evidence_card))
else:
values = values.reshape(variable_card, 1)
cpd = TabularCPD(
variable=cpd_data["variable"],
variable_card=variable_card,
values=values.tolist(),
evidence=cpd_data["evidence"],
evidence_card=evidence_card,
state_names=cpd_data["state_names"]
)
model.add_cpds(cpd)
model.check_model()
return model
" ******************************************************************************************************************** "
def split_dataframe(df: pd.DataFrame, num_splits: int) -> List:
return [df.iloc[i::num_splits, :] for i in range(num_splits)]
" ******************************************************************************************************************** "
def group_features_by_distribution(df, auto_threshold=True, threshold=None):
# TODO: fai da -1 a 1, così capisci la direzione
"""
Groups features with similar distributions in a dataframe.
Parameters:
- df: pandas.DataFrame
The input dataframe containing the features to be grouped.
- auto_threshold: bool (default=True)
Whether to automatically determine the threshold for clustering.
- threshold: float (default=None)
The threshold for the clustering algorithm to determine the number of clusters. Ignored if auto_threshold is True.
Returns:
- feature_clusters: dict
A dictionary mapping each feature to a cluster.
"""
# Step 1: Standardize the Data
scaler = StandardScaler()
scaled_df = scaler.fit_transform(df)
scaled_df = pd.DataFrame(scaled_df, columns=df.columns)
# Step 2: Measure Similarity
correlation_matrix = scaled_df.corr().abs()
# Handle NaN values in the correlation matrix
correlation_matrix = correlation_matrix.fillna(0)
# Step 3: Cluster Features
distance_matrix = 1 - correlation_matrix
# Ensure the distance matrix is symmetric
distance_matrix = (distance_matrix + distance_matrix.T) / 2
# Set diagonal elements to zero
np.fill_diagonal(distance_matrix.values, 0)
# Check if the distance matrix is symmetric
if not np.allclose(distance_matrix, distance_matrix.T, atol=1e-8):
print("Distance matrix:\n", distance_matrix)
print("Transpose of distance matrix:\n", distance_matrix.T)
raise ValueError("Distance matrix is not symmetric")
linked = linkage(squareform(distance_matrix), 'ward')
if auto_threshold:
# Create dendrogram and determine the threshold automatically
dendro = dendrogram(linked, no_plot=True)
distances = dendro['dcoord']
distances = sorted([y for x in distances for y in x[1:3]], reverse=True)
diff = np.diff(distances)
threshold = distances[np.argmax(diff) + 1]
# Plot the dendrogram
plt.figure(figsize=(10, 7), dpi=1000)
sns.clustermap(correlation_matrix, row_linkage=linked, col_linkage=linked, cmap='coolwarm', annot=True)
plt.title('Hierarchical Clustering Dendrogram')
plt.show()
# Define clusters
clusters = fcluster(linked, t=threshold, criterion='distance')
# Map features to clusters
feature_clusters = {df.columns[i]: clusters[i] for i in range(len(df.columns))}
return feature_clusters
def plot_distributions(df):
"""
Plots the distribution of each variable in the dataframe.
Parameters:
- df: pandas.DataFrame
The input dataframe containing the variables to be plotted.
"""
num_columns = len(df.columns)
num_rows = (num_columns + 2) // 3 # Adjust rows for a better fit
plt.figure(figsize=(15, num_rows * 5))
for i, column in enumerate(df.columns, 1):
plt.subplot(num_rows, 3, i)
sns.histplot(df[column], kde=True)
plt.title(f'Distribution of {column}')
plt.xlabel(column)
plt.ylabel('Frequency')
plt.tight_layout()
plt.show()
def get_numeric_part(input_string: str) -> int:
numeric_part = re.findall(r'\d+', input_string)
out_number = int(numeric_part[0]) if numeric_part else None
return out_number
" ******************************************************************************************************************** "
def get_MeanAbsoluteError(errors):
return np.mean(np.abs(errors))
def get_MeanSquaredError(errors):
return np.mean(errors ** 2)
def get_RootMeanSquaredError(errors):
return np.sqrt(get_MeanSquaredError(errors))
def get_MedianAbsoluteError(errors):
return np.median(np.abs(errors))
" ******************************************************************************************************************** "
def get_adjacency_matrix(graph, order_nodes=None, weight=False) -> np.array:
"""Retrieve the adjacency matrix from the nx.DiGraph or numpy array."""
if isinstance(graph, np.ndarray):
return graph
elif isinstance(graph, nx.DiGraph):
if order_nodes is None:
order_nodes = graph.nodes()
if not weight:
return np.array(nx.adjacency_matrix(graph, order_nodes, weight=None).todense())
else:
return np.array(nx.adjacency_matrix(graph, order_nodes).todense())
else:
raise TypeError("Only networkx.DiGraph and np.ndarray (adjacency matrixes) are supported.")
def get_StructuralHammingDistance(target, pred, double_for_anticausal=True) -> float: