MEDfl 0.1.26__py3-none-any.whl → 0.1.28__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.
- MEDfl/LearningManager/__init__.py +13 -0
- MEDfl/LearningManager/client.py +150 -0
- MEDfl/LearningManager/dynamicModal.py +287 -0
- MEDfl/LearningManager/federated_dataset.py +60 -0
- MEDfl/LearningManager/flpipeline.py +192 -0
- MEDfl/LearningManager/model.py +223 -0
- MEDfl/LearningManager/params.yaml +14 -0
- MEDfl/LearningManager/params_optimiser.py +442 -0
- MEDfl/LearningManager/plot.py +229 -0
- MEDfl/LearningManager/server.py +181 -0
- MEDfl/LearningManager/strategy.py +82 -0
- MEDfl/LearningManager/utils.py +308 -0
- MEDfl/NetManager/__init__.py +9 -0
- MEDfl/NetManager/database_connector.py +48 -0
- MEDfl/NetManager/dataset.py +92 -0
- MEDfl/NetManager/flsetup.py +320 -0
- MEDfl/NetManager/net_helper.py +248 -0
- MEDfl/NetManager/net_manager_queries.py +137 -0
- MEDfl/NetManager/network.py +174 -0
- MEDfl/NetManager/node.py +178 -0
- MEDfl/__init__.py +2 -0
- {MEDfl-0.1.26.data → MEDfl-0.1.28.data}/scripts/setup_mysql.sh +22 -22
- {MEDfl-0.1.26.dist-info → MEDfl-0.1.28.dist-info}/METADATA +127 -127
- MEDfl-0.1.28.dist-info/RECORD +54 -0
- {MEDfl-0.1.26.dist-info → MEDfl-0.1.28.dist-info}/WHEEL +1 -1
- Medfl/LearningManager/__init__.py +13 -13
- Medfl/LearningManager/client.py +150 -150
- Medfl/LearningManager/dynamicModal.py +287 -287
- Medfl/LearningManager/federated_dataset.py +60 -60
- Medfl/LearningManager/flpipeline.py +192 -192
- Medfl/LearningManager/model.py +223 -223
- Medfl/LearningManager/params.yaml +14 -14
- Medfl/LearningManager/params_optimiser.py +442 -442
- Medfl/LearningManager/plot.py +229 -229
- Medfl/LearningManager/server.py +181 -181
- Medfl/LearningManager/strategy.py +82 -82
- Medfl/LearningManager/utils.py +308 -308
- Medfl/NetManager/__init__.py +9 -9
- Medfl/NetManager/database_connector.py +48 -48
- Medfl/NetManager/dataset.py +92 -92
- Medfl/NetManager/flsetup.py +320 -320
- Medfl/NetManager/net_helper.py +248 -248
- Medfl/NetManager/net_manager_queries.py +137 -137
- Medfl/NetManager/network.py +174 -174
- Medfl/NetManager/node.py +178 -178
- Medfl/__init__.py +1 -1
- alembic/env.py +61 -61
- scripts/base.py +29 -29
- scripts/config.ini +5 -5
- scripts/create_db.py +133 -133
- scripts/db_config.ini +6 -0
- MEDfl-0.1.26.dist-info/RECORD +0 -32
- {MEDfl-0.1.26.dist-info → MEDfl-0.1.28.dist-info}/top_level.txt +0 -0
@@ -1,137 +1,137 @@
|
|
1
|
-
from .net_helper import is_str
|
2
|
-
|
3
|
-
INSERT_DATASET = """
|
4
|
-
INSERT INTO DataSets(DataSetName, NodeId, {columns})
|
5
|
-
VALUES (:name, :NodeId, {values})
|
6
|
-
"""
|
7
|
-
DELETE_DATASET = """
|
8
|
-
DELETE FROM DataSets WHERE DataSetName = :name
|
9
|
-
"""
|
10
|
-
|
11
|
-
SELECT_ALL_DATASET_NAMES = """
|
12
|
-
SELECT DISTINCT DataSetName,NodeId FROM DataSets
|
13
|
-
"""
|
14
|
-
|
15
|
-
SELECT_DATASET_BY_NAME = """
|
16
|
-
SELECT * FROM DataSets WHERE DataSetName = :name
|
17
|
-
"""
|
18
|
-
|
19
|
-
# node queries
|
20
|
-
# sql_queries.py
|
21
|
-
|
22
|
-
INSERT_NODE_QUERY = (
|
23
|
-
"INSERT INTO Nodes(NodeName,NetId,train) VALUES ('{}',{}, {})"
|
24
|
-
)
|
25
|
-
DELETE_NODE_QUERY = "DELETE FROM Nodes WHERE NodeName = '{}'"
|
26
|
-
SELECT_MASTER_COLUMNS_QUERY = "SELECT * FROM MasterDataset LIMIT 1"
|
27
|
-
SELECT_DATASET_BY_COLUMN_QUERY = "SELECT * FROM MasterDataset WHERE {} = '{}'"
|
28
|
-
SELECT_DATASET_BY_NODE_ID_QUERY = "SELECT * FROM DataSets WHERE NodeId = {}"
|
29
|
-
|
30
|
-
SELECT_ALL_DATASETS_QUERY = "SELECT DISTINCT DataSetName,NodeName FROM DataSets,Nodes WHERE Nodes.NodeName = '{}' and Nodes.NodeId = DataSets.NodeId"
|
31
|
-
SELECT_ALL_NODES_QUERY = "SELECT * FROM Nodes"
|
32
|
-
|
33
|
-
|
34
|
-
# SQL query to insert a new network
|
35
|
-
INSERT_NETWORK_QUERY = "INSERT INTO Networks(NetName) VALUES ('{name}')"
|
36
|
-
|
37
|
-
# SQL query to delete a network
|
38
|
-
DELETE_NETWORK_QUERY = "DELETE FROM Networks WHERE NetName = '{name}'"
|
39
|
-
|
40
|
-
# SQL query to delete a network
|
41
|
-
GET_NETWORK_QUERY = "SELECT * FROM Networks WHERE NetName = '{name}'"
|
42
|
-
|
43
|
-
|
44
|
-
# SQL query to update a network
|
45
|
-
UPDATE_NETWORK_QUERY = (
|
46
|
-
"UPDATE Networks SET FLsetupId = {FLsetupId} WHERE NetId = {id}"
|
47
|
-
)
|
48
|
-
|
49
|
-
# SQL query to retrieve all nodes for a network
|
50
|
-
LIST_ALL_NODES_QUERY = "SELECT Nodes.NodeName, Networks.NetName FROM Nodes, Networks WHERE Networks.NetName = '{name}' AND Networks.NetId = Nodes.NetId"
|
51
|
-
|
52
|
-
# SQL query to create the MasterDataset table
|
53
|
-
CREATE_MASTER_DATASET_TABLE_QUERY = """
|
54
|
-
CREATE TABLE IF NOT EXISTS MasterDataset (
|
55
|
-
PatientId INT NOT NULL AUTO_INCREMENT,
|
56
|
-
{},
|
57
|
-
PRIMARY KEY (PatientId)
|
58
|
-
);
|
59
|
-
"""
|
60
|
-
|
61
|
-
# SQL query to create the datasets table
|
62
|
-
CREATE_DATASETS_TABLE_QUERY = """
|
63
|
-
CREATE TABLE IF NOT EXISTS Datasets (
|
64
|
-
DataSetId INT NOT NULL AUTO_INCREMENT,
|
65
|
-
DataSetName VARCHAR(255),
|
66
|
-
NodeId INT,
|
67
|
-
{},
|
68
|
-
PRIMARY KEY (DataSetId)
|
69
|
-
|
70
|
-
);
|
71
|
-
"""
|
72
|
-
|
73
|
-
# SQL query to insert dataset values
|
74
|
-
INSERT_DATASET_VALUES_QUERY = "INSERT INTO MasterDataset({columns}, NodeId) VALUES ('{name}', {nodeId}, {values})"
|
75
|
-
|
76
|
-
|
77
|
-
# FL setup_queries
|
78
|
-
# sql_queries.py
|
79
|
-
|
80
|
-
CREATE_FLSETUP_QUERY = """
|
81
|
-
INSERT INTO FLsetup (name, description, creation_date, NetId, column_name)
|
82
|
-
VALUES (:name, :description, :creation_date, :net_id, :column_name)
|
83
|
-
"""
|
84
|
-
|
85
|
-
DELETE_FLSETUP_QUERY = """
|
86
|
-
DELETE FROM FLsetup
|
87
|
-
WHERE name = :name
|
88
|
-
"""
|
89
|
-
|
90
|
-
UPDATE_FLSETUP_QUERY = UPDATE_NETWORK_QUERY = (
|
91
|
-
"UPDATE FLsetup SET column_name ='{column_name}' WHERE name ='{FLsetupName}'"
|
92
|
-
)
|
93
|
-
|
94
|
-
|
95
|
-
READ_SETUP_QUERY = """
|
96
|
-
SELECT * FROM FLsetup
|
97
|
-
WHERE FLsetupId = :flsetup_id
|
98
|
-
"""
|
99
|
-
|
100
|
-
READ_ALL_SETUPS_QUERY = """
|
101
|
-
SELECT * FROM FLsetup
|
102
|
-
"""
|
103
|
-
|
104
|
-
READ_NETWORK_BY_ID_QUERY = """
|
105
|
-
SELECT * FROM Networks
|
106
|
-
WHERE NetId = :net_id
|
107
|
-
"""
|
108
|
-
|
109
|
-
READ_DISTINCT_NODES_QUERY = """
|
110
|
-
SELECT DISTINCT {} FROM MasterDataset
|
111
|
-
"""
|
112
|
-
|
113
|
-
|
114
|
-
# FederatedDataset Queries
|
115
|
-
INSERT_FLDATASET_QUERY = (
|
116
|
-
"INSERT INTO FedDatasets(name, FLsetupId) VALUES (:name, :FLsetupId)"
|
117
|
-
)
|
118
|
-
DELETE_FLDATASET_BY_SETUP_AND_PIPELINE_QUERY = "DELETE FROM FedDatasets WHERE FLsetupId = :FLsetupId AND FLpipeId = :FLpipeId"
|
119
|
-
|
120
|
-
|
121
|
-
UPDATE_FLDATASET_QUERY = (
|
122
|
-
"UPDATE FedDatasets SET FLpipeId = :FLpipeId WHERE FedId = :FedId"
|
123
|
-
)
|
124
|
-
SELECT_FLDATASET_BY_NAME_QUERY = "SELECT * FROM FedDatasets WHERE name = :name"
|
125
|
-
|
126
|
-
CREATE_FLPIPELINE_QUERY = """
|
127
|
-
INSERT INTO FLpipeline (name, description, creation_date, results)
|
128
|
-
VALUES ('{name}', '{description}', '{creation_date}', '{result}')
|
129
|
-
"""
|
130
|
-
DELETE_FLPIPELINE_QUERY = "DELETE FROM FLpipeline WHERE name = '{name}'"
|
131
|
-
|
132
|
-
SELECT_FLPIPELINE_QUERY = "SELECT FROM FLpipeline WHERE name = '{name}'"
|
133
|
-
|
134
|
-
CREATE_TEST_RESULTS_QUERY = """
|
135
|
-
INSERT INTO testResults (pipelineid, nodename, confusionmatrix, accuracy , sensivity, ppv , npv , f1score , fpr , tpr )
|
136
|
-
VALUES ('{pipelineId}', '{nodeName}', '{confusion_matrix}', '{accuracy}' , '{sensivity}' , '{ppv}' , '{npv}' , '{f1score}' , '{fpr}' , '{tpr}')
|
137
|
-
"""
|
1
|
+
from .net_helper import is_str
|
2
|
+
|
3
|
+
INSERT_DATASET = """
|
4
|
+
INSERT INTO DataSets(DataSetName, NodeId, {columns})
|
5
|
+
VALUES (:name, :NodeId, {values})
|
6
|
+
"""
|
7
|
+
DELETE_DATASET = """
|
8
|
+
DELETE FROM DataSets WHERE DataSetName = :name
|
9
|
+
"""
|
10
|
+
|
11
|
+
SELECT_ALL_DATASET_NAMES = """
|
12
|
+
SELECT DISTINCT DataSetName,NodeId FROM DataSets
|
13
|
+
"""
|
14
|
+
|
15
|
+
SELECT_DATASET_BY_NAME = """
|
16
|
+
SELECT * FROM DataSets WHERE DataSetName = :name
|
17
|
+
"""
|
18
|
+
|
19
|
+
# node queries
|
20
|
+
# sql_queries.py
|
21
|
+
|
22
|
+
INSERT_NODE_QUERY = (
|
23
|
+
"INSERT INTO Nodes(NodeName,NetId,train) VALUES ('{}',{}, {})"
|
24
|
+
)
|
25
|
+
DELETE_NODE_QUERY = "DELETE FROM Nodes WHERE NodeName = '{}'"
|
26
|
+
SELECT_MASTER_COLUMNS_QUERY = "SELECT * FROM MasterDataset LIMIT 1"
|
27
|
+
SELECT_DATASET_BY_COLUMN_QUERY = "SELECT * FROM MasterDataset WHERE {} = '{}'"
|
28
|
+
SELECT_DATASET_BY_NODE_ID_QUERY = "SELECT * FROM DataSets WHERE NodeId = {}"
|
29
|
+
|
30
|
+
SELECT_ALL_DATASETS_QUERY = "SELECT DISTINCT DataSetName,NodeName FROM DataSets,Nodes WHERE Nodes.NodeName = '{}' and Nodes.NodeId = DataSets.NodeId"
|
31
|
+
SELECT_ALL_NODES_QUERY = "SELECT * FROM Nodes"
|
32
|
+
|
33
|
+
|
34
|
+
# SQL query to insert a new network
|
35
|
+
INSERT_NETWORK_QUERY = "INSERT INTO Networks(NetName) VALUES ('{name}')"
|
36
|
+
|
37
|
+
# SQL query to delete a network
|
38
|
+
DELETE_NETWORK_QUERY = "DELETE FROM Networks WHERE NetName = '{name}'"
|
39
|
+
|
40
|
+
# SQL query to delete a network
|
41
|
+
GET_NETWORK_QUERY = "SELECT * FROM Networks WHERE NetName = '{name}'"
|
42
|
+
|
43
|
+
|
44
|
+
# SQL query to update a network
|
45
|
+
UPDATE_NETWORK_QUERY = (
|
46
|
+
"UPDATE Networks SET FLsetupId = {FLsetupId} WHERE NetId = {id}"
|
47
|
+
)
|
48
|
+
|
49
|
+
# SQL query to retrieve all nodes for a network
|
50
|
+
LIST_ALL_NODES_QUERY = "SELECT Nodes.NodeName, Networks.NetName FROM Nodes, Networks WHERE Networks.NetName = '{name}' AND Networks.NetId = Nodes.NetId"
|
51
|
+
|
52
|
+
# SQL query to create the MasterDataset table
|
53
|
+
CREATE_MASTER_DATASET_TABLE_QUERY = """
|
54
|
+
CREATE TABLE IF NOT EXISTS MasterDataset (
|
55
|
+
PatientId INT NOT NULL AUTO_INCREMENT,
|
56
|
+
{},
|
57
|
+
PRIMARY KEY (PatientId)
|
58
|
+
);
|
59
|
+
"""
|
60
|
+
|
61
|
+
# SQL query to create the datasets table
|
62
|
+
CREATE_DATASETS_TABLE_QUERY = """
|
63
|
+
CREATE TABLE IF NOT EXISTS Datasets (
|
64
|
+
DataSetId INT NOT NULL AUTO_INCREMENT,
|
65
|
+
DataSetName VARCHAR(255),
|
66
|
+
NodeId INT,
|
67
|
+
{},
|
68
|
+
PRIMARY KEY (DataSetId)
|
69
|
+
|
70
|
+
);
|
71
|
+
"""
|
72
|
+
|
73
|
+
# SQL query to insert dataset values
|
74
|
+
INSERT_DATASET_VALUES_QUERY = "INSERT INTO MasterDataset({columns}, NodeId) VALUES ('{name}', {nodeId}, {values})"
|
75
|
+
|
76
|
+
|
77
|
+
# FL setup_queries
|
78
|
+
# sql_queries.py
|
79
|
+
|
80
|
+
CREATE_FLSETUP_QUERY = """
|
81
|
+
INSERT INTO FLsetup (name, description, creation_date, NetId, column_name)
|
82
|
+
VALUES (:name, :description, :creation_date, :net_id, :column_name)
|
83
|
+
"""
|
84
|
+
|
85
|
+
DELETE_FLSETUP_QUERY = """
|
86
|
+
DELETE FROM FLsetup
|
87
|
+
WHERE name = :name
|
88
|
+
"""
|
89
|
+
|
90
|
+
UPDATE_FLSETUP_QUERY = UPDATE_NETWORK_QUERY = (
|
91
|
+
"UPDATE FLsetup SET column_name ='{column_name}' WHERE name ='{FLsetupName}'"
|
92
|
+
)
|
93
|
+
|
94
|
+
|
95
|
+
READ_SETUP_QUERY = """
|
96
|
+
SELECT * FROM FLsetup
|
97
|
+
WHERE FLsetupId = :flsetup_id
|
98
|
+
"""
|
99
|
+
|
100
|
+
READ_ALL_SETUPS_QUERY = """
|
101
|
+
SELECT * FROM FLsetup
|
102
|
+
"""
|
103
|
+
|
104
|
+
READ_NETWORK_BY_ID_QUERY = """
|
105
|
+
SELECT * FROM Networks
|
106
|
+
WHERE NetId = :net_id
|
107
|
+
"""
|
108
|
+
|
109
|
+
READ_DISTINCT_NODES_QUERY = """
|
110
|
+
SELECT DISTINCT {} FROM MasterDataset
|
111
|
+
"""
|
112
|
+
|
113
|
+
|
114
|
+
# FederatedDataset Queries
|
115
|
+
INSERT_FLDATASET_QUERY = (
|
116
|
+
"INSERT INTO FedDatasets(name, FLsetupId) VALUES (:name, :FLsetupId)"
|
117
|
+
)
|
118
|
+
DELETE_FLDATASET_BY_SETUP_AND_PIPELINE_QUERY = "DELETE FROM FedDatasets WHERE FLsetupId = :FLsetupId AND FLpipeId = :FLpipeId"
|
119
|
+
|
120
|
+
|
121
|
+
UPDATE_FLDATASET_QUERY = (
|
122
|
+
"UPDATE FedDatasets SET FLpipeId = :FLpipeId WHERE FedId = :FedId"
|
123
|
+
)
|
124
|
+
SELECT_FLDATASET_BY_NAME_QUERY = "SELECT * FROM FedDatasets WHERE name = :name"
|
125
|
+
|
126
|
+
CREATE_FLPIPELINE_QUERY = """
|
127
|
+
INSERT INTO FLpipeline (name, description, creation_date, results)
|
128
|
+
VALUES ('{name}', '{description}', '{creation_date}', '{result}')
|
129
|
+
"""
|
130
|
+
DELETE_FLPIPELINE_QUERY = "DELETE FROM FLpipeline WHERE name = '{name}'"
|
131
|
+
|
132
|
+
SELECT_FLPIPELINE_QUERY = "SELECT FROM FLpipeline WHERE name = '{name}'"
|
133
|
+
|
134
|
+
CREATE_TEST_RESULTS_QUERY = """
|
135
|
+
INSERT INTO testResults (pipelineid, nodename, confusionmatrix, accuracy , sensivity, ppv , npv , f1score , fpr , tpr )
|
136
|
+
VALUES ('{pipelineId}', '{nodeName}', '{confusion_matrix}', '{accuracy}' , '{sensivity}' , '{ppv}' , '{npv}' , '{f1score}' , '{fpr}' , '{tpr}')
|
137
|
+
"""
|
Medfl/NetManager/network.py
CHANGED
@@ -1,174 +1,174 @@
|
|
1
|
-
# src/MEDfl/NetManager/network.py
|
2
|
-
|
3
|
-
from MEDfl.LearningManager.utils import *
|
4
|
-
|
5
|
-
from .net_helper import *
|
6
|
-
from .net_manager_queries import (CREATE_MASTER_DATASET_TABLE_QUERY,
|
7
|
-
CREATE_DATASETS_TABLE_QUERY,
|
8
|
-
DELETE_NETWORK_QUERY,
|
9
|
-
INSERT_NETWORK_QUERY, LIST_ALL_NODES_QUERY,
|
10
|
-
UPDATE_NETWORK_QUERY, GET_NETWORK_QUERY)
|
11
|
-
from .node import Node
|
12
|
-
import pandas as pd
|
13
|
-
from MEDfl.LearningManager.utils import params
|
14
|
-
|
15
|
-
from sqlalchemy import text
|
16
|
-
|
17
|
-
|
18
|
-
class Network:
|
19
|
-
"""
|
20
|
-
A class representing a network.
|
21
|
-
|
22
|
-
Attributes:
|
23
|
-
name (str): The name of the network.
|
24
|
-
mtable_exists (int): An integer flag indicating whether the MasterDataset table exists (1) or not (0).
|
25
|
-
"""
|
26
|
-
|
27
|
-
def __init__(self, name: str = ""):
|
28
|
-
"""
|
29
|
-
Initialize a Network instance.
|
30
|
-
|
31
|
-
Parameters:
|
32
|
-
name (str): The name of the network.
|
33
|
-
"""
|
34
|
-
self.name = name
|
35
|
-
self.mtable_exists = int(master_table_exists())
|
36
|
-
self.validate()
|
37
|
-
|
38
|
-
db_manager = DatabaseManager() ;
|
39
|
-
db_manager.connect() ;
|
40
|
-
self.eng = db_manager.get_connection()
|
41
|
-
|
42
|
-
def validate(self):
|
43
|
-
"""Validate name"""
|
44
|
-
|
45
|
-
if not isinstance(self.name, str):
|
46
|
-
raise TypeError("name argument must be a string")
|
47
|
-
|
48
|
-
def create_network(self):
|
49
|
-
"""Create a new network in the database."""
|
50
|
-
self.eng.execute(text(INSERT_NETWORK_QUERY.format(name=self.name)))
|
51
|
-
self.id = get_netid_from_name(self.name)
|
52
|
-
|
53
|
-
def use_network(self, network_name: str):
|
54
|
-
"""Use a network in the database.
|
55
|
-
|
56
|
-
Parameters:
|
57
|
-
network_name (str): The name of the network to use.
|
58
|
-
|
59
|
-
Returns:
|
60
|
-
Network or None: An instance of the Network class if the network exists, else None.
|
61
|
-
|
62
|
-
"""
|
63
|
-
network = pd.read_sql(
|
64
|
-
text(GET_NETWORK_QUERY.format(name=network_name)),
|
65
|
-
self.eng,
|
66
|
-
)
|
67
|
-
|
68
|
-
if (network.NetId[0]):
|
69
|
-
self.name = network.NetName[0]
|
70
|
-
self.id = network.NetId[0]
|
71
|
-
self.mtable_exists = int(master_table_exists())
|
72
|
-
self.validate()
|
73
|
-
return self
|
74
|
-
else:
|
75
|
-
return None
|
76
|
-
|
77
|
-
def delete_network(self):
|
78
|
-
"""Delete the network from the database."""
|
79
|
-
self.eng.execute(text(DELETE_NETWORK_QUERY.format(name=self.name)))
|
80
|
-
|
81
|
-
def update_network(self, FLsetupId: int):
|
82
|
-
"""Update the network's FLsetupId in the database.
|
83
|
-
|
84
|
-
Parameters:
|
85
|
-
FLsetupId (int): The FLsetupId to update.
|
86
|
-
"""
|
87
|
-
self.eng.execute(
|
88
|
-
text(UPDATE_NETWORK_QUERY.format(FLsetupId=FLsetupId, id=self.id))
|
89
|
-
)
|
90
|
-
|
91
|
-
def add_node(self, node: Node):
|
92
|
-
"""Add a node to the network.
|
93
|
-
|
94
|
-
Parameters:
|
95
|
-
node (Node): The node to add.
|
96
|
-
"""
|
97
|
-
node.create_node(self.id)
|
98
|
-
|
99
|
-
def list_allnodes(self):
|
100
|
-
"""List all nodes in the network.
|
101
|
-
|
102
|
-
Parameters:
|
103
|
-
None
|
104
|
-
|
105
|
-
Returns:
|
106
|
-
DataFrame: A DataFrame containing information about all nodes in the network.
|
107
|
-
|
108
|
-
"""
|
109
|
-
query = text(LIST_ALL_NODES_QUERY.format(name=self.name))
|
110
|
-
result_proxy = self.eng.execute(query)
|
111
|
-
result_df = pd.DataFrame(result_proxy.fetchall(), columns=result_proxy.keys())
|
112
|
-
return result_df
|
113
|
-
|
114
|
-
def create_master_dataset(self, path_to_csv: str = params['path_to_master_csv']):
|
115
|
-
"""
|
116
|
-
Create the MasterDataset table and insert dataset values.
|
117
|
-
|
118
|
-
:param path_to_csv: Path to the CSV file containing the dataset.
|
119
|
-
"""
|
120
|
-
print(path_to_csv)
|
121
|
-
# Read the CSV file into a Pandas DataFrame
|
122
|
-
data_df = pd.read_csv(path_to_csv)
|
123
|
-
|
124
|
-
# Process the data if needed (e.g., handle missing values, encode categorical variables)
|
125
|
-
# ...
|
126
|
-
|
127
|
-
# Check if the MasterDataset table exists
|
128
|
-
|
129
|
-
if self.mtable_exists != 1:
|
130
|
-
columns = data_df.columns.tolist()
|
131
|
-
columns_str = ",\n".join(
|
132
|
-
[
|
133
|
-
f"{col} {column_map[str(data_df[col].dtype)]}"
|
134
|
-
for col in columns
|
135
|
-
]
|
136
|
-
)
|
137
|
-
self.eng.execute(
|
138
|
-
text(CREATE_MASTER_DATASET_TABLE_QUERY.format(columns_str))
|
139
|
-
)
|
140
|
-
self.eng.execute(text(CREATE_DATASETS_TABLE_QUERY.format(columns_str)))
|
141
|
-
|
142
|
-
# Get the list of columns in the DataFrame
|
143
|
-
|
144
|
-
data_df = process_eicu(data_df)
|
145
|
-
# Insert the dataset values into the MasterDataset table
|
146
|
-
|
147
|
-
for index, row in data_df.iterrows():
|
148
|
-
query_1 = "INSERT INTO MasterDataset(" + "".join(
|
149
|
-
f"{x}," for x in columns
|
150
|
-
)
|
151
|
-
query_2 = f"VALUES (" + "".join(
|
152
|
-
f"{is_str(data_df, row, x)}," for x in columns
|
153
|
-
)
|
154
|
-
query = query_1[:-1] + ")" + query_2[:-1] + ")"
|
155
|
-
self.eng.execute(text(query))
|
156
|
-
|
157
|
-
# Set mtable_exists flag to True
|
158
|
-
self.mtable_exists = 1
|
159
|
-
|
160
|
-
@staticmethod
|
161
|
-
def list_allnetworks():
|
162
|
-
"""List all networks in the database.
|
163
|
-
Returns:
|
164
|
-
DataFrame: A DataFrame containing information about all networks in the database.
|
165
|
-
|
166
|
-
"""
|
167
|
-
db_manager = DatabaseManager() ;
|
168
|
-
db_manager.connect() ;
|
169
|
-
my_eng = db_manager.get_connection() ;
|
170
|
-
|
171
|
-
result_proxy = my_eng.execute("SELECT * FROM Networks")
|
172
|
-
result = result_proxy.fetchall()
|
173
|
-
return result
|
174
|
-
|
1
|
+
# src/MEDfl/NetManager/network.py
|
2
|
+
|
3
|
+
from MEDfl.LearningManager.utils import *
|
4
|
+
|
5
|
+
from .net_helper import *
|
6
|
+
from .net_manager_queries import (CREATE_MASTER_DATASET_TABLE_QUERY,
|
7
|
+
CREATE_DATASETS_TABLE_QUERY,
|
8
|
+
DELETE_NETWORK_QUERY,
|
9
|
+
INSERT_NETWORK_QUERY, LIST_ALL_NODES_QUERY,
|
10
|
+
UPDATE_NETWORK_QUERY, GET_NETWORK_QUERY)
|
11
|
+
from .node import Node
|
12
|
+
import pandas as pd
|
13
|
+
from MEDfl.LearningManager.utils import params
|
14
|
+
|
15
|
+
from sqlalchemy import text
|
16
|
+
|
17
|
+
|
18
|
+
class Network:
|
19
|
+
"""
|
20
|
+
A class representing a network.
|
21
|
+
|
22
|
+
Attributes:
|
23
|
+
name (str): The name of the network.
|
24
|
+
mtable_exists (int): An integer flag indicating whether the MasterDataset table exists (1) or not (0).
|
25
|
+
"""
|
26
|
+
|
27
|
+
def __init__(self, name: str = ""):
|
28
|
+
"""
|
29
|
+
Initialize a Network instance.
|
30
|
+
|
31
|
+
Parameters:
|
32
|
+
name (str): The name of the network.
|
33
|
+
"""
|
34
|
+
self.name = name
|
35
|
+
self.mtable_exists = int(master_table_exists())
|
36
|
+
self.validate()
|
37
|
+
|
38
|
+
db_manager = DatabaseManager() ;
|
39
|
+
db_manager.connect() ;
|
40
|
+
self.eng = db_manager.get_connection()
|
41
|
+
|
42
|
+
def validate(self):
|
43
|
+
"""Validate name"""
|
44
|
+
|
45
|
+
if not isinstance(self.name, str):
|
46
|
+
raise TypeError("name argument must be a string")
|
47
|
+
|
48
|
+
def create_network(self):
|
49
|
+
"""Create a new network in the database."""
|
50
|
+
self.eng.execute(text(INSERT_NETWORK_QUERY.format(name=self.name)))
|
51
|
+
self.id = get_netid_from_name(self.name)
|
52
|
+
|
53
|
+
def use_network(self, network_name: str):
|
54
|
+
"""Use a network in the database.
|
55
|
+
|
56
|
+
Parameters:
|
57
|
+
network_name (str): The name of the network to use.
|
58
|
+
|
59
|
+
Returns:
|
60
|
+
Network or None: An instance of the Network class if the network exists, else None.
|
61
|
+
|
62
|
+
"""
|
63
|
+
network = pd.read_sql(
|
64
|
+
text(GET_NETWORK_QUERY.format(name=network_name)),
|
65
|
+
self.eng,
|
66
|
+
)
|
67
|
+
|
68
|
+
if (network.NetId[0]):
|
69
|
+
self.name = network.NetName[0]
|
70
|
+
self.id = network.NetId[0]
|
71
|
+
self.mtable_exists = int(master_table_exists())
|
72
|
+
self.validate()
|
73
|
+
return self
|
74
|
+
else:
|
75
|
+
return None
|
76
|
+
|
77
|
+
def delete_network(self):
|
78
|
+
"""Delete the network from the database."""
|
79
|
+
self.eng.execute(text(DELETE_NETWORK_QUERY.format(name=self.name)))
|
80
|
+
|
81
|
+
def update_network(self, FLsetupId: int):
|
82
|
+
"""Update the network's FLsetupId in the database.
|
83
|
+
|
84
|
+
Parameters:
|
85
|
+
FLsetupId (int): The FLsetupId to update.
|
86
|
+
"""
|
87
|
+
self.eng.execute(
|
88
|
+
text(UPDATE_NETWORK_QUERY.format(FLsetupId=FLsetupId, id=self.id))
|
89
|
+
)
|
90
|
+
|
91
|
+
def add_node(self, node: Node):
|
92
|
+
"""Add a node to the network.
|
93
|
+
|
94
|
+
Parameters:
|
95
|
+
node (Node): The node to add.
|
96
|
+
"""
|
97
|
+
node.create_node(self.id)
|
98
|
+
|
99
|
+
def list_allnodes(self):
|
100
|
+
"""List all nodes in the network.
|
101
|
+
|
102
|
+
Parameters:
|
103
|
+
None
|
104
|
+
|
105
|
+
Returns:
|
106
|
+
DataFrame: A DataFrame containing information about all nodes in the network.
|
107
|
+
|
108
|
+
"""
|
109
|
+
query = text(LIST_ALL_NODES_QUERY.format(name=self.name))
|
110
|
+
result_proxy = self.eng.execute(query)
|
111
|
+
result_df = pd.DataFrame(result_proxy.fetchall(), columns=result_proxy.keys())
|
112
|
+
return result_df
|
113
|
+
|
114
|
+
def create_master_dataset(self, path_to_csv: str = params['path_to_master_csv']):
|
115
|
+
"""
|
116
|
+
Create the MasterDataset table and insert dataset values.
|
117
|
+
|
118
|
+
:param path_to_csv: Path to the CSV file containing the dataset.
|
119
|
+
"""
|
120
|
+
print(path_to_csv)
|
121
|
+
# Read the CSV file into a Pandas DataFrame
|
122
|
+
data_df = pd.read_csv(path_to_csv)
|
123
|
+
|
124
|
+
# Process the data if needed (e.g., handle missing values, encode categorical variables)
|
125
|
+
# ...
|
126
|
+
|
127
|
+
# Check if the MasterDataset table exists
|
128
|
+
|
129
|
+
if self.mtable_exists != 1:
|
130
|
+
columns = data_df.columns.tolist()
|
131
|
+
columns_str = ",\n".join(
|
132
|
+
[
|
133
|
+
f"{col} {column_map[str(data_df[col].dtype)]}"
|
134
|
+
for col in columns
|
135
|
+
]
|
136
|
+
)
|
137
|
+
self.eng.execute(
|
138
|
+
text(CREATE_MASTER_DATASET_TABLE_QUERY.format(columns_str))
|
139
|
+
)
|
140
|
+
self.eng.execute(text(CREATE_DATASETS_TABLE_QUERY.format(columns_str)))
|
141
|
+
|
142
|
+
# Get the list of columns in the DataFrame
|
143
|
+
|
144
|
+
data_df = process_eicu(data_df)
|
145
|
+
# Insert the dataset values into the MasterDataset table
|
146
|
+
|
147
|
+
for index, row in data_df.iterrows():
|
148
|
+
query_1 = "INSERT INTO MasterDataset(" + "".join(
|
149
|
+
f"{x}," for x in columns
|
150
|
+
)
|
151
|
+
query_2 = f"VALUES (" + "".join(
|
152
|
+
f"{is_str(data_df, row, x)}," for x in columns
|
153
|
+
)
|
154
|
+
query = query_1[:-1] + ")" + query_2[:-1] + ")"
|
155
|
+
self.eng.execute(text(query))
|
156
|
+
|
157
|
+
# Set mtable_exists flag to True
|
158
|
+
self.mtable_exists = 1
|
159
|
+
|
160
|
+
@staticmethod
|
161
|
+
def list_allnetworks():
|
162
|
+
"""List all networks in the database.
|
163
|
+
Returns:
|
164
|
+
DataFrame: A DataFrame containing information about all networks in the database.
|
165
|
+
|
166
|
+
"""
|
167
|
+
db_manager = DatabaseManager() ;
|
168
|
+
db_manager.connect() ;
|
169
|
+
my_eng = db_manager.get_connection() ;
|
170
|
+
|
171
|
+
result_proxy = my_eng.execute("SELECT * FROM Networks")
|
172
|
+
result = result_proxy.fetchall()
|
173
|
+
return result
|
174
|
+
|