MEDfl 0.2.1__py3-none-any.whl → 2.0.1__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 +4 -3
  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.1.dist-info}/METADATA +120 -108
  51. medfl-2.0.1.dist-info/RECORD +55 -0
  52. {MEDfl-0.2.1.dist-info → medfl-2.0.1.dist-info}/WHEEL +1 -1
  53. {MEDfl-0.2.1.dist-info → medfl-2.0.1.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.1.dist-info}/top_level.txt +0 -0
@@ -1,254 +1,254 @@
1
- from sklearn.preprocessing import LabelEncoder
2
- from sklearn.impute import SimpleImputer
3
-
4
- from sqlalchemy import text
5
-
6
- import torch
7
- import pandas as pd
8
- from torch.utils.data import TensorDataset
9
- import numpy as np
10
-
11
- from MEDfl.NetManager.database_connector import DatabaseManager
12
-
13
-
14
- def is_str(data_df, row, x):
15
- """
16
- Check if a column in a DataFrame is of type 'object' and convert the value accordingly.
17
-
18
- Args:
19
- data_df (pandas.DataFrame): DataFrame containing the data.
20
- row (pandas.Series): Data row.
21
- x (str): Column name.
22
-
23
- Returns:
24
- str or float: Processed value based on the column type.
25
- """
26
- if data_df[x].dtype == "object":
27
- x = f"'{row[x]}'"
28
- else:
29
- x = row[x]
30
- return x
31
-
32
-
33
- def process_eicu(data_df):
34
- """
35
- Process eICU data by filling missing values with mean and replacing NaNs with 'Unknown'.
36
-
37
- Args:
38
- data_df (pandas.DataFrame): Input data.
39
-
40
- Returns:
41
- pandas.DataFrame: Processed data.
42
- """
43
- # Identify numeric and non-numeric columns
44
- numeric_columns = data_df.select_dtypes(include=[np.number]).columns
45
- non_numeric_columns = data_df.select_dtypes(exclude=[np.number]).columns
46
-
47
- # Fill NaN in numeric columns with mean
48
- data_df[numeric_columns] = data_df[numeric_columns].fillna(
49
- data_df[numeric_columns].mean())
50
-
51
- # Fill NaN in non-numeric columns with 'Unknown'
52
- data_df[non_numeric_columns] = data_df[non_numeric_columns].fillna(
53
- 'Unknown')
54
-
55
- try:
56
- data_df = data_df.reset_index(drop=True)
57
- except:
58
- pass
59
-
60
- return data_df
61
-
62
-
63
- # remove indiserd columns after reading from the DB
64
- def process_data_after_reading(data, output, fill_strategy="mean", fit_encode=[], to_drop=[]):
65
- """
66
- Process data after reading from the database, including encoding, dropping columns, and creating a PyTorch TensorDataset.
67
-
68
- Args:
69
- data (pandas.DataFrame): Input data.
70
- output (str): Output column name.
71
- fill_strategy (str, optional): Imputation strategy for missing values. Default is "mean".
72
- fit_encode (list, optional): List of columns to be label-encoded. Default is an empty list.
73
- to_drop (list, optional): List of columns to be dropped from the DataFrame. Default is an empty list.
74
-
75
- Returns:
76
- torch.utils.data.TensorDataset: Processed data as a PyTorch TensorDataset.
77
- """
78
-
79
- # Check if there is a DataSet assigned to the node
80
- if (len(data) == 0):
81
- raise "Node doesn't Have dataSet"
82
-
83
- encoder = LabelEncoder()
84
- # En Code some columns
85
- for s in fit_encode:
86
- try:
87
- data[s] = encoder.fit_transform(data[s])
88
- except:
89
- raise print(s)
90
-
91
- # The output of the DATA
92
- y = data[output]
93
-
94
- X = data
95
-
96
- # remove indisered columns when reading the dataframe from the DB
97
- for column in to_drop:
98
- try:
99
- X = X.drop(
100
- [column], axis=1
101
- )
102
- except Exception as e:
103
- raise e
104
-
105
- # Get the DATAset Features
106
- features = [col for col in X.columns if col != output]
107
-
108
- # Impute missing values using the mean strategy
109
- try:
110
- imputer = SimpleImputer(strategy=fill_strategy)
111
- X[features] = imputer.fit_transform(X[features])
112
- except:
113
- print()
114
-
115
- X = torch.tensor(X.values, dtype=torch.float32)
116
- y = torch.tensor(y.values, dtype=torch.float32)
117
- data = TensorDataset(X, y)
118
-
119
- return data
120
-
121
-
122
- def get_nodeid_from_name(name):
123
- """
124
- Get the NodeId from the Nodes table based on the NodeName.
125
-
126
- Args:
127
- name (str): Node name.
128
-
129
- Returns:
130
- int or None: NodeId or None if not found.
131
- """
132
- db_manager = DatabaseManager()
133
- db_manager.connect()
134
- my_eng = db_manager.get_connection()
135
-
136
- result_proxy = my_eng.execute(f"SELECT NodeId FROM Nodes WHERE NodeName = '{name}'")
137
- NodeId = int(result_proxy.fetchone()[0])
138
- return NodeId
139
-
140
-
141
- def get_netid_from_name(name):
142
- """
143
- Get the Network Id from the Networks table based on the NetName.
144
-
145
- Args:
146
- name (str): Network name.
147
-
148
- Returns:
149
- int or None: NetId or None if not found.
150
- """
151
- db_manager = DatabaseManager()
152
- db_manager.connect()
153
- my_eng = db_manager.get_connection()
154
-
155
- try:
156
- result_proxy = my_eng.execute(f"SELECT NetId FROM Networks WHERE NetName = '{name}'")
157
- NetId = int(result_proxy.fetchone()[0])
158
- except:
159
- NetId = None
160
- return NetId
161
-
162
-
163
- def get_flsetupid_from_name(name):
164
- """
165
- Get the FLsetupId from the FLsetup table based on the FL setup name.
166
-
167
- Args:
168
- name (str): FL setup name.
169
-
170
- Returns:
171
- int or None: FLsetupId or None if not found.
172
- """
173
- db_manager = DatabaseManager()
174
- db_manager.connect()
175
- my_eng = db_manager.get_connection()
176
-
177
- try:
178
-
179
- result_proxy = my_eng.execute(f"SELECT FLsetupId FROM FLsetup WHERE name = '{name}'")
180
- id = int(result_proxy.fetchone()[0])
181
-
182
- except:
183
- id = None
184
- return id
185
-
186
-
187
- def get_flpipeline_from_name(name):
188
- """
189
- Get the FLpipeline Id from the FLpipeline table based on the FL pipeline name.
190
-
191
- Args:
192
- name (str): FL pipeline name.
193
-
194
- Returns:
195
- int or None: FLpipelineId or None if not found.
196
- """
197
- db_manager = DatabaseManager()
198
- db_manager.connect()
199
- my_eng = db_manager.get_connection()
200
-
201
- try:
202
-
203
- result_proxy = my_eng.execute(f"SELECT id FROM FLpipeline WHERE name = '{name}'")
204
- id = int(result_proxy.fetchone()[0])
205
- except:
206
- id = None
207
- return id
208
-
209
-
210
- def get_feddataset_id_from_name(name):
211
- """
212
- Get the Federated dataset Id from the FedDatasets table based on the federated dataset name.
213
-
214
- Args:
215
- name (str): Federated dataset name.
216
-
217
- Returns:
218
- int or None: FedId or None if not found.
219
- """
220
- db_manager = DatabaseManager()
221
- db_manager.connect()
222
- my_eng = db_manager.get_connection()
223
-
224
- try:
225
-
226
- result_proxy = my_eng.execute(f"SELECT FedId FROM FedDatasets WHERE name = '{name}'")
227
- id = int(result_proxy.fetchone()[0])
228
- except:
229
- id = None
230
- return id
231
-
232
-
233
- def master_table_exists():
234
- """
235
- Check if the MasterDataset table exists in the database.
236
-
237
- Returns:
238
- bool: True if the table exists, False otherwise.
239
- """
240
- try:
241
- db_manager = DatabaseManager()
242
- db_manager.connect()
243
- my_eng = db_manager.get_connection()
244
-
245
- # SQLite-specific query to check if table exists
246
- sql_query = text("SELECT name FROM sqlite_master WHERE type='table' AND name='MasterDataset'")
247
- result = my_eng.execute(sql_query)
248
- exists = result.fetchone() is not None
249
- return exists
250
-
251
- except Exception as e:
252
- print(f"Error checking MasterDataset table existence: {e}")
253
- return False
254
-
1
+ from sklearn.preprocessing import LabelEncoder
2
+ from sklearn.impute import SimpleImputer
3
+
4
+ from sqlalchemy import text
5
+
6
+ import torch
7
+ import pandas as pd
8
+ from torch.utils.data import TensorDataset
9
+ import numpy as np
10
+
11
+ from MEDfl.NetManager.database_connector import DatabaseManager
12
+
13
+
14
+ def is_str(data_df, row, x):
15
+ """
16
+ Check if a column in a DataFrame is of type 'object' and convert the value accordingly.
17
+
18
+ Args:
19
+ data_df (pandas.DataFrame): DataFrame containing the data.
20
+ row (pandas.Series): Data row.
21
+ x (str): Column name.
22
+
23
+ Returns:
24
+ str or float: Processed value based on the column type.
25
+ """
26
+ if data_df[x].dtype == "object":
27
+ x = f"'{row[x]}'"
28
+ else:
29
+ x = row[x]
30
+ return x
31
+
32
+
33
+ def process_eicu(data_df):
34
+ """
35
+ Process eICU data by filling missing values with mean and replacing NaNs with 'Unknown'.
36
+
37
+ Args:
38
+ data_df (pandas.DataFrame): Input data.
39
+
40
+ Returns:
41
+ pandas.DataFrame: Processed data.
42
+ """
43
+ # Identify numeric and non-numeric columns
44
+ numeric_columns = data_df.select_dtypes(include=[np.number]).columns
45
+ non_numeric_columns = data_df.select_dtypes(exclude=[np.number]).columns
46
+
47
+ # Fill NaN in numeric columns with mean
48
+ data_df[numeric_columns] = data_df[numeric_columns].fillna(
49
+ data_df[numeric_columns].mean())
50
+
51
+ # Fill NaN in non-numeric columns with 'Unknown'
52
+ data_df[non_numeric_columns] = data_df[non_numeric_columns].fillna(
53
+ 'Unknown')
54
+
55
+ try:
56
+ data_df = data_df.reset_index(drop=True)
57
+ except:
58
+ pass
59
+
60
+ return data_df
61
+
62
+
63
+ # remove indiserd columns after reading from the DB
64
+ def process_data_after_reading(data, output, fill_strategy="mean", fit_encode=[], to_drop=[]):
65
+ """
66
+ Process data after reading from the database, including encoding, dropping columns, and creating a PyTorch TensorDataset.
67
+
68
+ Args:
69
+ data (pandas.DataFrame): Input data.
70
+ output (str): Output column name.
71
+ fill_strategy (str, optional): Imputation strategy for missing values. Default is "mean".
72
+ fit_encode (list, optional): List of columns to be label-encoded. Default is an empty list.
73
+ to_drop (list, optional): List of columns to be dropped from the DataFrame. Default is an empty list.
74
+
75
+ Returns:
76
+ torch.utils.data.TensorDataset: Processed data as a PyTorch TensorDataset.
77
+ """
78
+
79
+ # Check if there is a DataSet assigned to the node
80
+ if (len(data) == 0):
81
+ raise "Node doesn't Have dataSet"
82
+
83
+ encoder = LabelEncoder()
84
+ # En Code some columns
85
+ for s in fit_encode:
86
+ try:
87
+ data[s] = encoder.fit_transform(data[s])
88
+ except:
89
+ raise print(s)
90
+
91
+ # The output of the DATA
92
+ y = data[output]
93
+
94
+ X = data
95
+
96
+ # remove indisered columns when reading the dataframe from the DB
97
+ for column in to_drop:
98
+ try:
99
+ X = X.drop(
100
+ [column], axis=1
101
+ )
102
+ except Exception as e:
103
+ raise e
104
+
105
+ # Get the DATAset Features
106
+ features = [col for col in X.columns if col != output]
107
+
108
+ # Impute missing values using the mean strategy
109
+ try:
110
+ imputer = SimpleImputer(strategy=fill_strategy)
111
+ X[features] = imputer.fit_transform(X[features])
112
+ except:
113
+ print()
114
+
115
+ X = torch.tensor(X.values, dtype=torch.float32)
116
+ y = torch.tensor(y.values, dtype=torch.float32)
117
+ data = TensorDataset(X, y)
118
+
119
+ return data
120
+
121
+
122
+ def get_nodeid_from_name(name):
123
+ """
124
+ Get the NodeId from the Nodes table based on the NodeName.
125
+
126
+ Args:
127
+ name (str): Node name.
128
+
129
+ Returns:
130
+ int or None: NodeId or None if not found.
131
+ """
132
+ db_manager = DatabaseManager()
133
+ db_manager.connect()
134
+ my_eng = db_manager.get_connection()
135
+
136
+ result_proxy = my_eng.execute(f"SELECT NodeId FROM Nodes WHERE NodeName = '{name}'")
137
+ NodeId = int(result_proxy.fetchone()[0])
138
+ return NodeId
139
+
140
+
141
+ def get_netid_from_name(name):
142
+ """
143
+ Get the Network Id from the Networks table based on the NetName.
144
+
145
+ Args:
146
+ name (str): Network name.
147
+
148
+ Returns:
149
+ int or None: NetId or None if not found.
150
+ """
151
+ db_manager = DatabaseManager()
152
+ db_manager.connect()
153
+ my_eng = db_manager.get_connection()
154
+
155
+ try:
156
+ result_proxy = my_eng.execute(f"SELECT NetId FROM Networks WHERE NetName = '{name}'")
157
+ NetId = int(result_proxy.fetchone()[0])
158
+ except:
159
+ NetId = None
160
+ return NetId
161
+
162
+
163
+ def get_flsetupid_from_name(name):
164
+ """
165
+ Get the FLsetupId from the FLsetup table based on the FL setup name.
166
+
167
+ Args:
168
+ name (str): FL setup name.
169
+
170
+ Returns:
171
+ int or None: FLsetupId or None if not found.
172
+ """
173
+ db_manager = DatabaseManager()
174
+ db_manager.connect()
175
+ my_eng = db_manager.get_connection()
176
+
177
+ try:
178
+
179
+ result_proxy = my_eng.execute(f"SELECT FLsetupId FROM FLsetup WHERE name = '{name}'")
180
+ id = int(result_proxy.fetchone()[0])
181
+
182
+ except:
183
+ id = None
184
+ return id
185
+
186
+
187
+ def get_flpipeline_from_name(name):
188
+ """
189
+ Get the FLpipeline Id from the FLpipeline table based on the FL pipeline name.
190
+
191
+ Args:
192
+ name (str): FL pipeline name.
193
+
194
+ Returns:
195
+ int or None: FLpipelineId or None if not found.
196
+ """
197
+ db_manager = DatabaseManager()
198
+ db_manager.connect()
199
+ my_eng = db_manager.get_connection()
200
+
201
+ try:
202
+
203
+ result_proxy = my_eng.execute(f"SELECT id FROM FLpipeline WHERE name = '{name}'")
204
+ id = int(result_proxy.fetchone()[0])
205
+ except:
206
+ id = None
207
+ return id
208
+
209
+
210
+ def get_feddataset_id_from_name(name):
211
+ """
212
+ Get the Federated dataset Id from the FedDatasets table based on the federated dataset name.
213
+
214
+ Args:
215
+ name (str): Federated dataset name.
216
+
217
+ Returns:
218
+ int or None: FedId or None if not found.
219
+ """
220
+ db_manager = DatabaseManager()
221
+ db_manager.connect()
222
+ my_eng = db_manager.get_connection()
223
+
224
+ try:
225
+
226
+ result_proxy = my_eng.execute(f"SELECT FedId FROM FedDatasets WHERE name = '{name}'")
227
+ id = int(result_proxy.fetchone()[0])
228
+ except:
229
+ id = None
230
+ return id
231
+
232
+
233
+ def master_table_exists():
234
+ """
235
+ Check if the MasterDataset table exists in the database.
236
+
237
+ Returns:
238
+ bool: True if the table exists, False otherwise.
239
+ """
240
+ try:
241
+ db_manager = DatabaseManager()
242
+ db_manager.connect()
243
+ my_eng = db_manager.get_connection()
244
+
245
+ # SQLite-specific query to check if table exists
246
+ sql_query = text("SELECT name FROM sqlite_master WHERE type='table' AND name='MasterDataset'")
247
+ result = my_eng.execute(sql_query)
248
+ exists = result.fetchone() is not None
249
+ return exists
250
+
251
+ except Exception as e:
252
+ print(f"Error checking MasterDataset table existence: {e}")
253
+ return False
254
+