MEDfl 0.2.1__py3-none-any.whl → 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. MEDfl/LearningManager/__init__.py +13 -13
  2. MEDfl/LearningManager/client.py +150 -181
  3. MEDfl/LearningManager/dynamicModal.py +287 -287
  4. MEDfl/LearningManager/federated_dataset.py +60 -60
  5. MEDfl/LearningManager/flpipeline.py +192 -192
  6. MEDfl/LearningManager/model.py +223 -223
  7. MEDfl/LearningManager/params.yaml +14 -14
  8. MEDfl/LearningManager/params_optimiser.py +442 -442
  9. MEDfl/LearningManager/plot.py +229 -229
  10. MEDfl/LearningManager/server.py +181 -189
  11. MEDfl/LearningManager/strategy.py +82 -138
  12. MEDfl/LearningManager/utils.py +331 -331
  13. MEDfl/NetManager/__init__.py +10 -10
  14. MEDfl/NetManager/database_connector.py +43 -43
  15. MEDfl/NetManager/dataset.py +92 -92
  16. MEDfl/NetManager/flsetup.py +320 -320
  17. MEDfl/NetManager/net_helper.py +254 -254
  18. MEDfl/NetManager/net_manager_queries.py +142 -142
  19. MEDfl/NetManager/network.py +194 -194
  20. MEDfl/NetManager/node.py +184 -184
  21. MEDfl/__init__.py +2 -2
  22. MEDfl/scripts/__init__.py +1 -1
  23. MEDfl/scripts/base.py +29 -29
  24. MEDfl/scripts/create_db.py +126 -126
  25. Medfl/LearningManager/__init__.py +13 -0
  26. Medfl/LearningManager/client.py +150 -0
  27. Medfl/LearningManager/dynamicModal.py +287 -0
  28. Medfl/LearningManager/federated_dataset.py +60 -0
  29. Medfl/LearningManager/flpipeline.py +192 -0
  30. Medfl/LearningManager/model.py +223 -0
  31. Medfl/LearningManager/params.yaml +14 -0
  32. Medfl/LearningManager/params_optimiser.py +442 -0
  33. Medfl/LearningManager/plot.py +229 -0
  34. Medfl/LearningManager/server.py +181 -0
  35. Medfl/LearningManager/strategy.py +82 -0
  36. Medfl/LearningManager/utils.py +331 -0
  37. Medfl/NetManager/__init__.py +10 -0
  38. Medfl/NetManager/database_connector.py +43 -0
  39. Medfl/NetManager/dataset.py +92 -0
  40. Medfl/NetManager/flsetup.py +320 -0
  41. Medfl/NetManager/net_helper.py +254 -0
  42. Medfl/NetManager/net_manager_queries.py +142 -0
  43. Medfl/NetManager/network.py +194 -0
  44. Medfl/NetManager/node.py +184 -0
  45. Medfl/__init__.py +3 -0
  46. Medfl/scripts/__init__.py +2 -0
  47. Medfl/scripts/base.py +30 -0
  48. Medfl/scripts/create_db.py +126 -0
  49. alembic/env.py +61 -61
  50. {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info}/METADATA +120 -108
  51. medfl-2.0.0.dist-info/RECORD +55 -0
  52. {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info}/WHEEL +1 -1
  53. {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info/licenses}/LICENSE +674 -674
  54. MEDfl-0.2.1.dist-info/RECORD +0 -31
  55. {MEDfl-0.2.1.dist-info → medfl-2.0.0.dist-info}/top_level.txt +0 -0
@@ -1,331 +1,331 @@
1
- #!/usr/bin/env python3
2
-
3
- import pkg_resources
4
- import torch
5
- import yaml
6
- from sklearn.metrics import *
7
- from yaml.loader import SafeLoader
8
-
9
-
10
- from MEDfl.NetManager.database_connector import DatabaseManager
11
-
12
- # from scripts.base import *
13
- import json
14
-
15
-
16
- import pandas as pd
17
- import numpy as np
18
-
19
- import os
20
- import configparser
21
-
22
- import subprocess
23
- import ast
24
-
25
- from sqlalchemy import text
26
-
27
-
28
- # Get the directory of the current script
29
- current_directory = os.path.dirname(os.path.abspath(__file__))
30
-
31
- # Load configuration from the config file
32
- yaml_path = os.path.join(current_directory, 'params.yaml')
33
-
34
- with open(yaml_path) as g:
35
- params = yaml.load(g, Loader=SafeLoader)
36
-
37
- # global_yaml_path = pkg_resources.resource_filename(__name__, "../../global_params.yaml")
38
- # with open(global_yaml_path) as g:
39
- # global_params = yaml.load(g, Loader=SafeLoader)
40
-
41
-
42
- # Default path for the config file
43
- DEFAULT_CONFIG_PATH = 'db_config.ini'
44
-
45
-
46
- def load_db_config_dep():
47
- config = os.environ.get('MEDfl_DB_CONFIG')
48
-
49
- if config:
50
- return ast.literal_eval(config)
51
- else:
52
- raise ValueError(f"MEDfl db config not found")
53
-
54
- # Function to allow users to set config path programmatically
55
-
56
-
57
- def set_db_config_dep(config_path):
58
- config = configparser.ConfigParser()
59
- config.read(config_path)
60
- if (config['sqllite']):
61
- os.environ['MEDfl_DB_CONFIG'] = str(dict(config['sqllite']))
62
- else:
63
- raise ValueError(f"mysql key not found in file '{config_path}'")
64
-
65
-
66
-
67
- def load_db_config():
68
- """Read a dictionary from an environment variable."""
69
- obj_str = os.getenv("MEDfl_DB_CONFIG")
70
- if obj_str is not None:
71
- return ast.literal_eval(obj_str)
72
- else:
73
- raise ValueError(f"Environment variable MEDfl_DB_CONFIG not found")
74
-
75
- # Function to allow users to set config path programmatically
76
-
77
-
78
- def set_db_config(config_path):
79
- obj = {"database" : config_path}
80
-
81
- """Store a dictionary as a string in an environment variable."""
82
- obj_str = str(obj)
83
- os.environ['MEDfl_DB_CONFIG'] = obj_str
84
-
85
-
86
-
87
-
88
-
89
-
90
- # Create databas
91
-
92
-
93
- def create_MEDfl_db():
94
- script_path = os.path.join(os.path.dirname(
95
- __file__), 'scripts', 'create_db.sh')
96
- subprocess.run(['sh', script_path], check=True)
97
-
98
-
99
- def custom_classification_report(y_true, y_pred_prob):
100
- """
101
- Compute custom classification report metrics including accuracy, sensitivity, specificity, precision, NPV,
102
- F1-score, false positive rate, and true positive rate.
103
-
104
- Args:
105
- y_true (array-like): True labels.
106
- y_pred (array-like): Predicted labels.
107
-
108
- Returns:
109
- dict: A dictionary containing custom classification report metrics.
110
- """
111
- y_pred = (y_pred_prob).round(
112
- ) # Round absolute values of predicted probabilities to the nearest integer
113
-
114
- auc = roc_auc_score(y_true, y_pred_prob) # Calculate AUC
115
-
116
- tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
117
-
118
- # Accuracy
119
- denominator_acc = tp + tn + fp + fn
120
- acc = (tp + tn) / denominator_acc if denominator_acc != 0 else 0.0
121
-
122
- # Sensitivity/Recall
123
- denominator_sen = tp + fn
124
- sen = tp / denominator_sen if denominator_sen != 0 else 0.0
125
-
126
- # Specificity
127
- denominator_sp = tn + fp
128
- sp = tn / denominator_sp if denominator_sp != 0 else 0.0
129
-
130
- # PPV/Precision
131
- denominator_ppv = tp + fp
132
- ppv = tp / denominator_ppv if denominator_ppv != 0 else 0.0
133
-
134
- # NPV
135
- denominator_npv = tn + fn
136
- npv = tn / denominator_npv if denominator_npv != 0 else 0.0
137
-
138
- # F1 Score
139
- denominator_f1 = sen + ppv
140
- f1 = 2 * (sen * ppv) / denominator_f1 if denominator_f1 != 0 else 0.0
141
-
142
- # False Positive Rate
143
- denominator_fpr = fp + tn
144
- fpr = fp / denominator_fpr if denominator_fpr != 0 else 0.0
145
-
146
- # True Positive Rate
147
- denominator_tpr = tp + fn
148
- tpr = tp / denominator_tpr if denominator_tpr != 0 else 0.0
149
-
150
- return {
151
- "confusion matrix": {"TP": tp, "FP": fp, "FN": fn, "TN": tn},
152
- "Accuracy": round(acc, 3),
153
- "Sensitivity/Recall": round(sen, 3),
154
- "Specificity": round(sp, 3),
155
- "PPV/Precision": round(ppv, 3),
156
- "NPV": round(npv, 3),
157
- "F1-score": round(f1, 3),
158
- "False positive rate": round(fpr, 3),
159
- "True positive rate": round(tpr, 3),
160
- "auc": auc
161
- }
162
-
163
-
164
- def test(model, test_loader, device=torch.device("cpu")):
165
- """
166
- Evaluate a model using a test loader and return a custom classification report.
167
-
168
- Args:
169
- model (torch.nn.Module): PyTorch model to evaluate.
170
- test_loader (torch.utils.data.DataLoader): DataLoader for the test dataset.
171
- device (torch.device, optional): Device for model evaluation. Default is "cpu".
172
-
173
- Returns:
174
- dict: A dictionary containing custom classification report metrics.
175
- """
176
-
177
- model.eval()
178
- with torch.no_grad():
179
- X_test, y_test = test_loader.dataset[:][0].to(
180
- device), test_loader.dataset[:][1].to(device)
181
- y_hat_prob = torch.squeeze(model(X_test), 1).cpu()
182
-
183
- return custom_classification_report(y_test.cpu().numpy(), y_hat_prob.cpu().numpy())
184
-
185
-
186
- column_map = {"object": "VARCHAR(255)", "int64": "INT", "float64": "FLOAT"}
187
-
188
-
189
- def empty_db():
190
- """
191
- Empty the database by deleting records from multiple tables and resetting auto-increment counters.
192
-
193
- Returns:
194
- None
195
- """
196
- db_manager = DatabaseManager()
197
- db_manager.connect()
198
- my_eng = db_manager.get_connection()
199
-
200
- # my_eng.execute(text(f"DELETE FROM {'DataSets'}"))
201
- my_eng.execute(text(f"DELETE FROM {'Nodes'}"))
202
- my_eng.execute(text(f"DELETE FROM {'FedDatasets'}"))
203
- my_eng.execute(text(f"DELETE FROM {'Networks'}"))
204
- my_eng.execute(text(f"DELETE FROM {'FLsetup'}"))
205
-
206
- my_eng.execute(text(f"DELETE FROM {'FLpipeline'}"))
207
- my_eng.execute(text(f"ALTER TABLE {'Nodes'} AUTO_INCREMENT = 1"))
208
- my_eng.execute(text(f"ALTER TABLE {'Networks'} AUTO_INCREMENT = 1"))
209
- my_eng.execute(text(f"ALTER TABLE {'FedDatasets'} AUTO_INCREMENT = 1"))
210
- my_eng.execute(text(f"ALTER TABLE {'FLsetup'} AUTO_INCREMENT = 1"))
211
- my_eng.execute(text(f"ALTER TABLE {'FLpipeline'} AUTO_INCREMENT = 1"))
212
- my_eng.execute(text(f"DELETE FROM {'testResults'}"))
213
- my_eng.execute(text(f"DROP TABLE IF EXISTS {'MasterDataset'}"))
214
- my_eng.execute(text(f"DROP TABLE IF EXISTS {'DataSets'}"))
215
-
216
-
217
- def get_pipeline_from_name(name):
218
- """
219
- Get the pipeline ID from its name in the database.
220
-
221
- Args:
222
- name (str): Name of the pipeline.
223
-
224
- Returns:
225
- int: ID of the pipeline.
226
- """
227
- db_manager = DatabaseManager()
228
- db_manager.connect()
229
- my_eng = db_manager.get_connection()
230
-
231
- NodeId = int(
232
- pd.read_sql(
233
- text(f"SELECT id FROM FLpipeline WHERE name = '{name}'"), my_eng
234
- ).iloc[0, 0]
235
- )
236
- return NodeId
237
-
238
-
239
- def get_pipeline_confusion_matrix(pipeline_id):
240
- """
241
- Get the global confusion matrix for a pipeline based on test results.
242
-
243
- Args:
244
- pipeline_id (int): ID of the pipeline.
245
-
246
- Returns:
247
- dict: A dictionary representing the global confusion matrix.
248
- """
249
- db_manager = DatabaseManager()
250
- db_manager.connect()
251
- my_eng = db_manager.get_connection()
252
-
253
- data = pd.read_sql(
254
- text(
255
- f"SELECT confusionmatrix FROM testResults WHERE pipelineid = '{pipeline_id}'"), my_eng
256
- )
257
-
258
- # Convert the column of strings into a list of dictionaries representing confusion matrices
259
- confusion_matrices = [
260
- json.loads(matrix.replace("'", "\"")) for matrix in data['confusionmatrix']
261
- ]
262
-
263
- # Initialize variables for global confusion matrix
264
- global_TP = global_FP = global_FN = global_TN = 0
265
-
266
- # Iterate through each dictionary and sum the corresponding values for each category
267
- for matrix in confusion_matrices:
268
- global_TP += matrix['TP']
269
- global_FP += matrix['FP']
270
- global_FN += matrix['FN']
271
- global_TN += matrix['TN']
272
-
273
- # Create a global confusion matrix as a dictionary
274
- global_confusion_matrix = {
275
- 'TP': global_TP,
276
- 'FP': global_FP,
277
- 'FN': global_FN,
278
- 'TN': global_TN
279
- }
280
- # Return the list of dictionaries representing confusion matrices
281
- return global_confusion_matrix
282
-
283
-
284
- def get_node_confusion_matrix(pipeline_id, node_name):
285
- """
286
- Get the confusion matrix for a specific node in a pipeline based on test results.
287
-
288
- Args:
289
- pipeline_id (int): ID of the pipeline.
290
- node_name (str): Name of the node.
291
-
292
- Returns:
293
- dict: A dictionary representing the confusion matrix for the specified node.
294
- """
295
- db_manager = DatabaseManager()
296
- db_manager.connect()
297
- my_eng = db_manager.get_connection()
298
-
299
- data = pd.read_sql(
300
- text(
301
- f"SELECT confusionmatrix FROM testResults WHERE pipelineid = '{pipeline_id}' AND nodename = '{node_name}'"), my_eng
302
- )
303
-
304
- # Convert the column of strings into a list of dictionaries representing confusion matrices
305
- confusion_matrices = [
306
- json.loads(matrix.replace("'", "\"")) for matrix in data['confusionmatrix']
307
- ]
308
-
309
- # Return the list of dictionaries representing confusion matrices
310
- return confusion_matrices[0]
311
-
312
-
313
- def get_pipeline_result(pipeline_id):
314
- """
315
- Get the test results for a pipeline.
316
-
317
- Args:
318
- pipeline_id (int): ID of the pipeline.
319
-
320
- Returns:
321
- pandas.DataFrame: DataFrame containing test results for the specified pipeline.
322
- """
323
- db_manager = DatabaseManager()
324
- db_manager.connect()
325
- my_eng = db_manager.get_connection()
326
-
327
- data = pd.read_sql(
328
- text(
329
- f"SELECT * FROM testResults WHERE pipelineid = '{pipeline_id}'"), my_eng
330
- )
331
- return data
1
+ #!/usr/bin/env python3
2
+
3
+ import pkg_resources
4
+ import torch
5
+ import yaml
6
+ from sklearn.metrics import *
7
+ from yaml.loader import SafeLoader
8
+
9
+
10
+ from MEDfl.NetManager.database_connector import DatabaseManager
11
+
12
+ # from scripts.base import *
13
+ import json
14
+
15
+
16
+ import pandas as pd
17
+ import numpy as np
18
+
19
+ import os
20
+ import configparser
21
+
22
+ import subprocess
23
+ import ast
24
+
25
+ from sqlalchemy import text
26
+
27
+
28
+ # Get the directory of the current script
29
+ current_directory = os.path.dirname(os.path.abspath(__file__))
30
+
31
+ # Load configuration from the config file
32
+ yaml_path = os.path.join(current_directory, 'params.yaml')
33
+
34
+ with open(yaml_path) as g:
35
+ params = yaml.load(g, Loader=SafeLoader)
36
+
37
+ # global_yaml_path = pkg_resources.resource_filename(__name__, "../../global_params.yaml")
38
+ # with open(global_yaml_path) as g:
39
+ # global_params = yaml.load(g, Loader=SafeLoader)
40
+
41
+
42
+ # Default path for the config file
43
+ DEFAULT_CONFIG_PATH = 'db_config.ini'
44
+
45
+
46
+ def load_db_config_dep():
47
+ config = os.environ.get('MEDfl_DB_CONFIG')
48
+
49
+ if config:
50
+ return ast.literal_eval(config)
51
+ else:
52
+ raise ValueError(f"MEDfl db config not found")
53
+
54
+ # Function to allow users to set config path programmatically
55
+
56
+
57
+ def set_db_config_dep(config_path):
58
+ config = configparser.ConfigParser()
59
+ config.read(config_path)
60
+ if (config['sqllite']):
61
+ os.environ['MEDfl_DB_CONFIG'] = str(dict(config['sqllite']))
62
+ else:
63
+ raise ValueError(f"mysql key not found in file '{config_path}'")
64
+
65
+
66
+
67
+ def load_db_config():
68
+ """Read a dictionary from an environment variable."""
69
+ obj_str = os.getenv("MEDfl_DB_CONFIG")
70
+ if obj_str is not None:
71
+ return ast.literal_eval(obj_str)
72
+ else:
73
+ raise ValueError(f"Environment variable MEDfl_DB_CONFIG not found")
74
+
75
+ # Function to allow users to set config path programmatically
76
+
77
+
78
+ def set_db_config(config_path):
79
+ obj = {"database" : config_path}
80
+
81
+ """Store a dictionary as a string in an environment variable."""
82
+ obj_str = str(obj)
83
+ os.environ['MEDfl_DB_CONFIG'] = obj_str
84
+
85
+
86
+
87
+
88
+
89
+
90
+ # Create databas
91
+
92
+
93
+ def create_MEDfl_db():
94
+ script_path = os.path.join(os.path.dirname(
95
+ __file__), 'scripts', 'create_db.sh')
96
+ subprocess.run(['sh', script_path], check=True)
97
+
98
+
99
+ def custom_classification_report(y_true, y_pred_prob):
100
+ """
101
+ Compute custom classification report metrics including accuracy, sensitivity, specificity, precision, NPV,
102
+ F1-score, false positive rate, and true positive rate.
103
+
104
+ Args:
105
+ y_true (array-like): True labels.
106
+ y_pred (array-like): Predicted labels.
107
+
108
+ Returns:
109
+ dict: A dictionary containing custom classification report metrics.
110
+ """
111
+ y_pred = (y_pred_prob).round(
112
+ ) # Round absolute values of predicted probabilities to the nearest integer
113
+
114
+ auc = roc_auc_score(y_true, y_pred_prob) # Calculate AUC
115
+
116
+ tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
117
+
118
+ # Accuracy
119
+ denominator_acc = tp + tn + fp + fn
120
+ acc = (tp + tn) / denominator_acc if denominator_acc != 0 else 0.0
121
+
122
+ # Sensitivity/Recall
123
+ denominator_sen = tp + fn
124
+ sen = tp / denominator_sen if denominator_sen != 0 else 0.0
125
+
126
+ # Specificity
127
+ denominator_sp = tn + fp
128
+ sp = tn / denominator_sp if denominator_sp != 0 else 0.0
129
+
130
+ # PPV/Precision
131
+ denominator_ppv = tp + fp
132
+ ppv = tp / denominator_ppv if denominator_ppv != 0 else 0.0
133
+
134
+ # NPV
135
+ denominator_npv = tn + fn
136
+ npv = tn / denominator_npv if denominator_npv != 0 else 0.0
137
+
138
+ # F1 Score
139
+ denominator_f1 = sen + ppv
140
+ f1 = 2 * (sen * ppv) / denominator_f1 if denominator_f1 != 0 else 0.0
141
+
142
+ # False Positive Rate
143
+ denominator_fpr = fp + tn
144
+ fpr = fp / denominator_fpr if denominator_fpr != 0 else 0.0
145
+
146
+ # True Positive Rate
147
+ denominator_tpr = tp + fn
148
+ tpr = tp / denominator_tpr if denominator_tpr != 0 else 0.0
149
+
150
+ return {
151
+ "confusion matrix": {"TP": tp, "FP": fp, "FN": fn, "TN": tn},
152
+ "Accuracy": round(acc, 3),
153
+ "Sensitivity/Recall": round(sen, 3),
154
+ "Specificity": round(sp, 3),
155
+ "PPV/Precision": round(ppv, 3),
156
+ "NPV": round(npv, 3),
157
+ "F1-score": round(f1, 3),
158
+ "False positive rate": round(fpr, 3),
159
+ "True positive rate": round(tpr, 3),
160
+ "auc": auc
161
+ }
162
+
163
+
164
+ def test(model, test_loader, device=torch.device("cpu")):
165
+ """
166
+ Evaluate a model using a test loader and return a custom classification report.
167
+
168
+ Args:
169
+ model (torch.nn.Module): PyTorch model to evaluate.
170
+ test_loader (torch.utils.data.DataLoader): DataLoader for the test dataset.
171
+ device (torch.device, optional): Device for model evaluation. Default is "cpu".
172
+
173
+ Returns:
174
+ dict: A dictionary containing custom classification report metrics.
175
+ """
176
+
177
+ model.eval()
178
+ with torch.no_grad():
179
+ X_test, y_test = test_loader.dataset[:][0].to(
180
+ device), test_loader.dataset[:][1].to(device)
181
+ y_hat_prob = torch.squeeze(model(X_test), 1).cpu()
182
+
183
+ return custom_classification_report(y_test.cpu().numpy(), y_hat_prob.cpu().numpy())
184
+
185
+
186
+ column_map = {"object": "VARCHAR(255)", "int64": "INT", "float64": "FLOAT"}
187
+
188
+
189
+ def empty_db():
190
+ """
191
+ Empty the database by deleting records from multiple tables and resetting auto-increment counters.
192
+
193
+ Returns:
194
+ None
195
+ """
196
+ db_manager = DatabaseManager()
197
+ db_manager.connect()
198
+ my_eng = db_manager.get_connection()
199
+
200
+ # my_eng.execute(text(f"DELETE FROM {'DataSets'}"))
201
+ my_eng.execute(text(f"DELETE FROM {'Nodes'}"))
202
+ my_eng.execute(text(f"DELETE FROM {'FedDatasets'}"))
203
+ my_eng.execute(text(f"DELETE FROM {'Networks'}"))
204
+ my_eng.execute(text(f"DELETE FROM {'FLsetup'}"))
205
+
206
+ my_eng.execute(text(f"DELETE FROM {'FLpipeline'}"))
207
+ my_eng.execute(text(f"ALTER TABLE {'Nodes'} AUTO_INCREMENT = 1"))
208
+ my_eng.execute(text(f"ALTER TABLE {'Networks'} AUTO_INCREMENT = 1"))
209
+ my_eng.execute(text(f"ALTER TABLE {'FedDatasets'} AUTO_INCREMENT = 1"))
210
+ my_eng.execute(text(f"ALTER TABLE {'FLsetup'} AUTO_INCREMENT = 1"))
211
+ my_eng.execute(text(f"ALTER TABLE {'FLpipeline'} AUTO_INCREMENT = 1"))
212
+ my_eng.execute(text(f"DELETE FROM {'testResults'}"))
213
+ my_eng.execute(text(f"DROP TABLE IF EXISTS {'MasterDataset'}"))
214
+ my_eng.execute(text(f"DROP TABLE IF EXISTS {'DataSets'}"))
215
+
216
+
217
+ def get_pipeline_from_name(name):
218
+ """
219
+ Get the pipeline ID from its name in the database.
220
+
221
+ Args:
222
+ name (str): Name of the pipeline.
223
+
224
+ Returns:
225
+ int: ID of the pipeline.
226
+ """
227
+ db_manager = DatabaseManager()
228
+ db_manager.connect()
229
+ my_eng = db_manager.get_connection()
230
+
231
+ NodeId = int(
232
+ pd.read_sql(
233
+ text(f"SELECT id FROM FLpipeline WHERE name = '{name}'"), my_eng
234
+ ).iloc[0, 0]
235
+ )
236
+ return NodeId
237
+
238
+
239
+ def get_pipeline_confusion_matrix(pipeline_id):
240
+ """
241
+ Get the global confusion matrix for a pipeline based on test results.
242
+
243
+ Args:
244
+ pipeline_id (int): ID of the pipeline.
245
+
246
+ Returns:
247
+ dict: A dictionary representing the global confusion matrix.
248
+ """
249
+ db_manager = DatabaseManager()
250
+ db_manager.connect()
251
+ my_eng = db_manager.get_connection()
252
+
253
+ data = pd.read_sql(
254
+ text(
255
+ f"SELECT confusionmatrix FROM testResults WHERE pipelineid = '{pipeline_id}'"), my_eng
256
+ )
257
+
258
+ # Convert the column of strings into a list of dictionaries representing confusion matrices
259
+ confusion_matrices = [
260
+ json.loads(matrix.replace("'", "\"")) for matrix in data['confusionmatrix']
261
+ ]
262
+
263
+ # Initialize variables for global confusion matrix
264
+ global_TP = global_FP = global_FN = global_TN = 0
265
+
266
+ # Iterate through each dictionary and sum the corresponding values for each category
267
+ for matrix in confusion_matrices:
268
+ global_TP += matrix['TP']
269
+ global_FP += matrix['FP']
270
+ global_FN += matrix['FN']
271
+ global_TN += matrix['TN']
272
+
273
+ # Create a global confusion matrix as a dictionary
274
+ global_confusion_matrix = {
275
+ 'TP': global_TP,
276
+ 'FP': global_FP,
277
+ 'FN': global_FN,
278
+ 'TN': global_TN
279
+ }
280
+ # Return the list of dictionaries representing confusion matrices
281
+ return global_confusion_matrix
282
+
283
+
284
+ def get_node_confusion_matrix(pipeline_id, node_name):
285
+ """
286
+ Get the confusion matrix for a specific node in a pipeline based on test results.
287
+
288
+ Args:
289
+ pipeline_id (int): ID of the pipeline.
290
+ node_name (str): Name of the node.
291
+
292
+ Returns:
293
+ dict: A dictionary representing the confusion matrix for the specified node.
294
+ """
295
+ db_manager = DatabaseManager()
296
+ db_manager.connect()
297
+ my_eng = db_manager.get_connection()
298
+
299
+ data = pd.read_sql(
300
+ text(
301
+ f"SELECT confusionmatrix FROM testResults WHERE pipelineid = '{pipeline_id}' AND nodename = '{node_name}'"), my_eng
302
+ )
303
+
304
+ # Convert the column of strings into a list of dictionaries representing confusion matrices
305
+ confusion_matrices = [
306
+ json.loads(matrix.replace("'", "\"")) for matrix in data['confusionmatrix']
307
+ ]
308
+
309
+ # Return the list of dictionaries representing confusion matrices
310
+ return confusion_matrices[0]
311
+
312
+
313
+ def get_pipeline_result(pipeline_id):
314
+ """
315
+ Get the test results for a pipeline.
316
+
317
+ Args:
318
+ pipeline_id (int): ID of the pipeline.
319
+
320
+ Returns:
321
+ pandas.DataFrame: DataFrame containing test results for the specified pipeline.
322
+ """
323
+ db_manager = DatabaseManager()
324
+ db_manager.connect()
325
+ my_eng = db_manager.get_connection()
326
+
327
+ data = pd.read_sql(
328
+ text(
329
+ f"SELECT * FROM testResults WHERE pipelineid = '{pipeline_id}'"), my_eng
330
+ )
331
+ return data