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,126 +1,126 @@
1
- import sys
2
- import sqlite3
3
- import pandas as pd
4
- from configparser import ConfigParser
5
- import os
6
- import ast
7
-
8
- from MEDfl.LearningManager.utils import *
9
-
10
-
11
- def main(csv_file_path):
12
- try:
13
- # Get the directory of the current script
14
- current_directory = os.path.dirname(os.path.abspath(__file__))
15
-
16
- # Load configuration from the config file
17
- # config_file_path = os.path.join(current_directory, 'sqllite_config.ini')*
18
-
19
- config_file_path = load_db_config()
20
-
21
- # config = ConfigParser()
22
- # config.read(config_file_path)
23
- # sqlite_config = config['sqllite']
24
-
25
- sqlite_config = config_file_path
26
-
27
-
28
- print('Im here !')
29
-
30
- # Connect to SQLite database (it will be created if it doesn't exist)
31
- database_path = sqlite_config['database']
32
- conn = sqlite3.connect(database_path)
33
- cursor = conn.cursor()
34
-
35
- # Drop each table if it exists
36
- tables = ['Networks', 'FLsetup', 'Nodes', 'DataSets', 'FLpipeline', 'testResults', 'FedDatasets']
37
- for table in tables:
38
- cursor.execute(f"DROP TABLE IF EXISTS {table}")
39
-
40
- # Create Networks table
41
- cursor.execute(
42
- "CREATE TABLE Networks( \
43
- NetId INTEGER PRIMARY KEY AUTOINCREMENT, \
44
- NetName TEXT \
45
- );"
46
- )
47
-
48
- # Create FLsetup table
49
- cursor.execute("CREATE TABLE FLsetup (\
50
- FLsetupId INTEGER PRIMARY KEY AUTOINCREMENT,\
51
- name TEXT NOT NULL, \
52
- description TEXT NOT NULL,\
53
- creation_date TEXT NOT NULL,\
54
- NetId INTEGER NOT NULL,\
55
- column_name TEXT\
56
- )")
57
-
58
- # Create Nodes table
59
- cursor.execute("CREATE TABLE Nodes ( \
60
- NodeId INTEGER PRIMARY KEY AUTOINCREMENT,\
61
- NodeName TEXT,\
62
- train BOOLEAN DEFAULT 1,\
63
- NetId INTEGER\
64
- )")
65
-
66
- data_df = pd.read_csv(csv_file_path)
67
- columns = data_df.columns.tolist()
68
- column_map = {"object": "TEXT", "int64": "INTEGER", "float64": "REAL"}
69
- sub_query = ", ".join(f"{col} {column_map[str(data_df[col].dtype)]}" for col in columns)
70
-
71
- # Create Datasets table by getting columns from the master csv file
72
- cursor.execute(
73
- f"CREATE TABLE DataSets( \
74
- DataSetId INTEGER PRIMARY KEY AUTOINCREMENT, \
75
- DataSetName TEXT, \
76
- NodeId INTEGER,\
77
- {sub_query}\
78
- )"
79
- )
80
-
81
- # Create FLpipeline table
82
- cursor.execute("CREATE TABLE FLpipeline(\
83
- id INTEGER PRIMARY KEY AUTOINCREMENT,\
84
- name TEXT NOT NULL, \
85
- description TEXT NOT NULL,\
86
- creation_date TEXT NOT NULL,\
87
- results TEXT NOT NULL\
88
- ) ")
89
-
90
- # Create test results table
91
- cursor.execute("CREATE TABLE testResults(\
92
- pipelineId INTEGER,\
93
- nodename TEXT NOT NULL, \
94
- confusionmatrix TEXT,\
95
- accuracy REAL,\
96
- sensivity REAL,\
97
- ppv REAL,\
98
- npv REAL,\
99
- f1score REAL,\
100
- fpr REAL,\
101
- tpr REAL, \
102
- PRIMARY KEY (pipelineId, nodename)\
103
- ) ")
104
-
105
- # Create FederatedDataset table
106
- cursor.execute("CREATE TABLE FedDatasets (\
107
- FedId INTEGER PRIMARY KEY AUTOINCREMENT,\
108
- FLsetupId INTEGER,\
109
- FLpipeId INTEGER,\
110
- name TEXT NOT NULL\
111
- )")
112
-
113
- # Commit and close the cursor
114
- conn.commit()
115
- cursor.close()
116
- conn.close()
117
-
118
- except sqlite3.Error as e:
119
- print(f"Error: {e}")
120
-
121
- if __name__ == "__main__":
122
- if len(sys.argv) != 2:
123
- print("Usage: python script.py <path_to_csv_file>")
124
- sys.exit(1)
125
- csv_file_path = sys.argv[1]
126
- main(csv_file_path)
1
+ import sys
2
+ import sqlite3
3
+ import pandas as pd
4
+ from configparser import ConfigParser
5
+ import os
6
+ import ast
7
+
8
+ from MEDfl.LearningManager.utils import *
9
+
10
+
11
+ def main(csv_file_path):
12
+ try:
13
+ # Get the directory of the current script
14
+ current_directory = os.path.dirname(os.path.abspath(__file__))
15
+
16
+ # Load configuration from the config file
17
+ # config_file_path = os.path.join(current_directory, 'sqllite_config.ini')*
18
+
19
+ config_file_path = load_db_config()
20
+
21
+ # config = ConfigParser()
22
+ # config.read(config_file_path)
23
+ # sqlite_config = config['sqllite']
24
+
25
+ sqlite_config = config_file_path
26
+
27
+
28
+ print('Im here !')
29
+
30
+ # Connect to SQLite database (it will be created if it doesn't exist)
31
+ database_path = sqlite_config['database']
32
+ conn = sqlite3.connect(database_path)
33
+ cursor = conn.cursor()
34
+
35
+ # Drop each table if it exists
36
+ tables = ['Networks', 'FLsetup', 'Nodes', 'DataSets', 'FLpipeline', 'testResults', 'FedDatasets']
37
+ for table in tables:
38
+ cursor.execute(f"DROP TABLE IF EXISTS {table}")
39
+
40
+ # Create Networks table
41
+ cursor.execute(
42
+ "CREATE TABLE Networks( \
43
+ NetId INTEGER PRIMARY KEY AUTOINCREMENT, \
44
+ NetName TEXT \
45
+ );"
46
+ )
47
+
48
+ # Create FLsetup table
49
+ cursor.execute("CREATE TABLE FLsetup (\
50
+ FLsetupId INTEGER PRIMARY KEY AUTOINCREMENT,\
51
+ name TEXT NOT NULL, \
52
+ description TEXT NOT NULL,\
53
+ creation_date TEXT NOT NULL,\
54
+ NetId INTEGER NOT NULL,\
55
+ column_name TEXT\
56
+ )")
57
+
58
+ # Create Nodes table
59
+ cursor.execute("CREATE TABLE Nodes ( \
60
+ NodeId INTEGER PRIMARY KEY AUTOINCREMENT,\
61
+ NodeName TEXT,\
62
+ train BOOLEAN DEFAULT 1,\
63
+ NetId INTEGER\
64
+ )")
65
+
66
+ data_df = pd.read_csv(csv_file_path)
67
+ columns = data_df.columns.tolist()
68
+ column_map = {"object": "TEXT", "int64": "INTEGER", "float64": "REAL"}
69
+ sub_query = ", ".join(f"{col} {column_map[str(data_df[col].dtype)]}" for col in columns)
70
+
71
+ # Create Datasets table by getting columns from the master csv file
72
+ cursor.execute(
73
+ f"CREATE TABLE DataSets( \
74
+ DataSetId INTEGER PRIMARY KEY AUTOINCREMENT, \
75
+ DataSetName TEXT, \
76
+ NodeId INTEGER,\
77
+ {sub_query}\
78
+ )"
79
+ )
80
+
81
+ # Create FLpipeline table
82
+ cursor.execute("CREATE TABLE FLpipeline(\
83
+ id INTEGER PRIMARY KEY AUTOINCREMENT,\
84
+ name TEXT NOT NULL, \
85
+ description TEXT NOT NULL,\
86
+ creation_date TEXT NOT NULL,\
87
+ results TEXT NOT NULL\
88
+ ) ")
89
+
90
+ # Create test results table
91
+ cursor.execute("CREATE TABLE testResults(\
92
+ pipelineId INTEGER,\
93
+ nodename TEXT NOT NULL, \
94
+ confusionmatrix TEXT,\
95
+ accuracy REAL,\
96
+ sensivity REAL,\
97
+ ppv REAL,\
98
+ npv REAL,\
99
+ f1score REAL,\
100
+ fpr REAL,\
101
+ tpr REAL, \
102
+ PRIMARY KEY (pipelineId, nodename)\
103
+ ) ")
104
+
105
+ # Create FederatedDataset table
106
+ cursor.execute("CREATE TABLE FedDatasets (\
107
+ FedId INTEGER PRIMARY KEY AUTOINCREMENT,\
108
+ FLsetupId INTEGER,\
109
+ FLpipeId INTEGER,\
110
+ name TEXT NOT NULL\
111
+ )")
112
+
113
+ # Commit and close the cursor
114
+ conn.commit()
115
+ cursor.close()
116
+ conn.close()
117
+
118
+ except sqlite3.Error as e:
119
+ print(f"Error: {e}")
120
+
121
+ if __name__ == "__main__":
122
+ if len(sys.argv) != 2:
123
+ print("Usage: python script.py <path_to_csv_file>")
124
+ sys.exit(1)
125
+ csv_file_path = sys.argv[1]
126
+ main(csv_file_path)
@@ -0,0 +1,13 @@
1
+ # MEDfl/LearningManager/__init__.py
2
+
3
+ # Import modules from this package
4
+ # from .client import *
5
+ # from .dynamicModal import *
6
+ # from .flpipeline import *
7
+ # from .federated_dataset import *
8
+ # from .model import *
9
+ # from .params_optimiser import *
10
+ # from .plot import *
11
+ # from .server import *
12
+ # from .strategy import *
13
+ # from .utils import *
@@ -0,0 +1,150 @@
1
+ #!/usr/bin/env python3
2
+ import flwr as fl
3
+ from opacus import PrivacyEngine
4
+ from torch.utils.data import DataLoader
5
+
6
+ from .model import Model
7
+ from .utils import params
8
+ import torch
9
+
10
+ class FlowerClient(fl.client.NumPyClient):
11
+ """
12
+ FlowerClient class for creating MEDfl clients.
13
+
14
+ Attributes:
15
+ cid (str): Client ID.
16
+ local_model (Model): Local model of the federated learning network.
17
+ trainloader (DataLoader): DataLoader for training data.
18
+ valloader (DataLoader): DataLoader for validation data.
19
+ diff_priv (bool): Flag indicating whether to use differential privacy.
20
+ """
21
+ def __init__(self, cid: str, local_model: Model, trainloader: DataLoader, valloader: DataLoader, diff_priv: bool = params["diff_privacy"]):
22
+ """
23
+ Initializes the FlowerClient instance.
24
+
25
+ Args:
26
+ cid (str): Client ID.
27
+ local_model (Model): Local model of the federated learning network.
28
+ trainloader (DataLoader): DataLoader for training data.
29
+ valloader (DataLoader): DataLoader for validation data.
30
+ diff_priv (bool): Flag indicating whether to use differential privacy.
31
+ """
32
+ self.cid = cid
33
+ self.local_model = local_model
34
+ self.trainloader = trainloader
35
+ self.valloader = valloader
36
+ if torch.cuda.is_available():
37
+ num_cuda_devices = torch.cuda.device_count()
38
+ if num_cuda_devices > 0:
39
+ device_idx = int(self.cid) % num_cuda_devices
40
+ self.device = torch.device(f"cuda:{device_idx}")
41
+ self.local_model.model.to(self.device)
42
+ else:
43
+ # Handle case where CUDA is available but no CUDA devices are found
44
+ raise RuntimeError("CUDA is available, but no CUDA devices are found.")
45
+ else:
46
+ # Handle case where CUDA is not available
47
+ self.device = torch.device("cpu")
48
+ self.local_model.model.to(self.device)
49
+
50
+ self.privacy_engine = PrivacyEngine(secure_mode=False)
51
+ self.diff_priv = diff_priv
52
+ self.epsilons = []
53
+ self.accuracies = []
54
+ self.losses = []
55
+ if self.diff_priv:
56
+ model, optimizer, self.trainloader = self.privacy_engine.make_private_with_epsilon(
57
+ module=self.local_model.model.train(),
58
+ optimizer=self.local_model.optimizer,
59
+ data_loader=self.trainloader,
60
+ epochs=params["train_epochs"],
61
+ target_epsilon=float(params["EPSILON"]),
62
+ target_delta= float(params["DELTA"]),
63
+ max_grad_norm=params["MAX_GRAD_NORM"],
64
+ )
65
+ setattr(self.local_model, "model", model)
66
+ setattr(self.local_model, "optimizer", optimizer)
67
+ self.validate()
68
+
69
+ def validate(self):
70
+ """Validates cid, local_model, trainloader, valloader."""
71
+ if not isinstance(self.cid, str):
72
+ raise TypeError("cid argument must be a string")
73
+
74
+ if not isinstance(self.local_model, Model):
75
+ raise TypeError("local_model argument must be a MEDfl.LearningManager.model.Model")
76
+
77
+ if not isinstance(self.trainloader, DataLoader):
78
+ raise TypeError("trainloader argument must be a torch.utils.data.dataloader")
79
+
80
+ if not isinstance(self.valloader, DataLoader):
81
+ raise TypeError("valloader argument must be a torch.utils.data.dataloader")
82
+
83
+ if not isinstance(self.diff_priv, bool):
84
+ raise TypeError("diff_priv argument must be a bool")
85
+
86
+ def get_parameters(self, config):
87
+ """
88
+ Returns the current parameters of the local model.
89
+
90
+ Args:
91
+ config: Configuration information.
92
+
93
+ Returns:
94
+ Numpy array: Parameters of the local model.
95
+ """
96
+ print(f"[Client {self.cid}] get_parameters")
97
+ return self.local_model.get_parameters()
98
+
99
+ def fit(self, parameters, config):
100
+ """
101
+ Fits the local model to the received parameters using federated learning.
102
+
103
+ Args:
104
+ parameters: Parameters received from the server.
105
+ config: Configuration information.
106
+
107
+ Returns:
108
+ Tuple: Parameters of the local model, number of training examples, and privacy information.
109
+ """
110
+ print('\n -------------------------------- \n this is the config of the client')
111
+ print(f"[Client {self.cid}] fit, config: {config}")
112
+ # print(config['epochs'])
113
+ print('\n -------------------------------- \n ')
114
+ self.local_model.set_parameters(parameters)
115
+ for _ in range(params["train_epochs"]):
116
+ epsilon = self.local_model.train(
117
+ self.trainloader,
118
+ epoch=_,
119
+ device=self.device,
120
+ privacy_engine=self.privacy_engine,
121
+ diff_priv=self.diff_priv,
122
+ )
123
+ self.epsilons.append(epsilon)
124
+ print(f"epsilon of client {self.cid} : eps = {epsilon}")
125
+ return (
126
+ self.local_model.get_parameters(),
127
+ len(self.trainloader),
128
+ {"epsilon": epsilon},
129
+ )
130
+
131
+ def evaluate(self, parameters, config):
132
+ """
133
+ Evaluates the local model on the validation data and returns the loss and accuracy.
134
+
135
+ Args:
136
+ parameters: Parameters received from the server.
137
+ config: Configuration information.
138
+
139
+ Returns:
140
+ Tuple: Loss, number of validation examples, and accuracy information.
141
+ """
142
+ print(f"[Client {self.cid}] evaluate, config: {config}")
143
+ self.local_model.set_parameters(parameters)
144
+ loss, accuracy , auc = self.local_model.evaluate(
145
+ self.valloader, device=self.device
146
+ )
147
+ self.losses.append(loss)
148
+ self.accuracies.append(accuracy)
149
+
150
+ return float(loss), len(self.valloader), {"accuracy": float(accuracy)}