MEDfl 0.1.7__py3-none-any.whl → 0.1.16__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/NetManager/node.py CHANGED
@@ -1,181 +1,187 @@
1
- import pandas as pd
2
-
3
- from scripts.base import *
4
- from .net_helper import *
5
- from .net_manager_queries import *
6
- from Medfl.LearningManager.utils import params
7
-
8
-
9
- class Node:
10
- """
11
- A class representing a node in the network.
12
-
13
- Attributes:
14
- name (str): The name of the node.
15
- train (int): An integer flag representing whether the node is used for training (1) or testing (0).
16
- test_fraction (float, optional): The fraction of data used for testing when train=1. Default is 0.2.
17
- """
18
-
19
- def __init__(
20
- self, name: str, train: int, test_fraction: float = 0.2, engine=my_eng
21
- ):
22
- """
23
- Initialize a Node instance.
24
-
25
- Parameters:
26
- name (str): The name of the node.
27
- train (int): An integer flag representing whether the node is used for training (1) or testing (0).
28
- test_fraction (float, optional): The fraction of data used for testing when train=1. Default is 0.2.
29
- """
30
- self.name = name
31
- self.train = train
32
- self.test_fraction = 1.0 if self.train == 0 else test_fraction
33
- self.engine = engine
34
-
35
- def validate(self):
36
- """Validate name, train, test_fraction"""
37
- if not isinstance(self.name, str):
38
- raise TypeError("name argument must be a string")
39
-
40
- if not isinstance(self.train, int):
41
- raise TypeError("train argument must be an int")
42
-
43
- if not isinstance(self.test_fraction, float):
44
- raise TypeError("test_fraction argument must be a float")
45
-
46
- def create_node(self, NetId: int):
47
- """Create a node in the database.
48
- Parameters:
49
- NetId (int): The ID of the network to which the node belongs.
50
-
51
- Returns:
52
- None
53
- """
54
- self.engine.execute(
55
- text(INSERT_NODE_QUERY.format(self.name, NetId, self.train))
56
- )
57
-
58
- def delete_node(self):
59
- """Delete the node from the database."""
60
- self.engine.execute(text(DELETE_NODE_QUERY.format(self.name)))
61
-
62
- def check_dataset_compatibility(self, data_df):
63
- """Check if the dataset is compatible with the master dataset.
64
- Parameters:
65
- data_df (DataFrame): The dataset to check.
66
-
67
- Returns:
68
- None
69
- """
70
- if master_table_exists() != 1:
71
- print("MasterDataset doesn't exist")
72
- else:
73
- columns = data_df.columns.tolist()
74
- # get master_dataset columns
75
- master_table_columns = pd.read_sql(
76
- text(SELECT_MASTER_COLUMNS_QUERY), self.engine
77
- ).columns.tolist()
78
- assert [x == y for x, y in zip(master_table_columns, columns)]
79
-
80
- def update_node(self):
81
- """Update the node information (not implemented)."""
82
- pass
83
-
84
- def get_dataset(self, column_name: str = None):
85
- """Get the dataset for the node based on the given column name.
86
- Parameters:
87
- column_name (str, optional): The column name to filter the dataset. Default is None.
88
-
89
- Returns:
90
- DataFrame: The dataset associated with the node.
91
- """
92
- NodeId = get_nodeid_from_name(self.name)
93
- if column_name is not None:
94
-
95
- node_dataset = pd.read_sql(
96
- text(
97
- SELECT_DATASET_BY_COLUMN_QUERY.format(
98
- column_name, self.name
99
- )
100
- ),
101
- self.engine,
102
- )
103
-
104
- else:
105
- node_dataset = pd.read_sql(
106
- text(SELECT_DATASET_BY_NODE_ID_QUERY.format(NodeId)),
107
- self.engine,
108
- )
109
- return node_dataset
110
-
111
- def upload_dataset(self, dataset_name: str, path_to_csv: str = params['path_to_test_csv']):
112
- """Upload the dataset to the database for the node.
113
- Parameters:
114
- dataset_name (str): The name of the dataset.
115
- path_to_csv (str, optional): Path to the CSV file containing the dataset. Default is the path in params.
116
-
117
- Returns:
118
- None
119
- """
120
- data_df = pd.read_csv(path_to_csv)
121
-
122
- nodeId = get_nodeid_from_name(self.name)
123
- columns = data_df.columns.tolist()
124
- self.check_dataset_compatibility(data_df)
125
-
126
- data_df = process_eicu(data_df)
127
- for index, row in data_df.iterrows():
128
- query_1 = "INSERT INTO DataSets(DataSetName,nodeId," + "".join(
129
- f"{x}," for x in columns
130
- )
131
- query_2 = f" VALUES ('{dataset_name}',{nodeId}, " + "".join(
132
- f"{is_str(data_df, row, x)}," for x in columns
133
- )
134
- query = query_1[:-1] + ")" + query_2[:-1] + ")"
135
- self.engine.execute(text(query))
136
-
137
- def assign_dataset(self, dataset_name:str):
138
- """Assigning existing dataSet to node
139
- Parameters:
140
- dataset_name (str): The name of the dataset to assign.
141
-
142
- Returns:
143
- None
144
- """
145
-
146
- nodeId = get_nodeid_from_name(self.name)
147
- query = f"UPDATE DataSets SET nodeId = {nodeId} WHERE DataSetName = '{dataset_name}'"
148
- self.engine.execute(text(query))
149
-
150
- def unassign_dataset(self, dataset_name:str):
151
- """unssigning existing dataSet to node
152
- Parameters:
153
- dataset_name (str): The name of the dataset to assign.
154
-
155
- Returns:
156
- None
157
- """
158
-
159
- query = f"UPDATE DataSets SET nodeId = {-1} WHERE DataSetName = '{dataset_name}'"
160
- self.engine.execute(text(query))
161
-
162
- def list_alldatasets(self):
163
- """List all datasets associated with the node.
164
- Returns:
165
- DataFrame: A DataFrame containing information about all datasets associated with the node.
166
-
167
- """
168
- return pd.read_sql(
169
- text(SELECT_ALL_DATASETS_QUERY.format(self.name)), my_eng
170
- )
171
-
172
- @staticmethod
173
- def list_allnodes():
174
- """List all nodes in the database.
175
- Returns:
176
- DataFrame: A DataFrame containing information about all nodes in the database.
177
-
178
- """
179
- query = text(SELECT_ALL_NODES_QUERY)
180
- res = pd.read_sql(query, my_eng)
181
- return res
1
+ import pandas as pd
2
+
3
+ from .net_helper import *
4
+ from .net_manager_queries import *
5
+ from Medfl.LearningManager.utils import params
6
+
7
+
8
+ class Node:
9
+ """
10
+ A class representing a node in the network.
11
+
12
+ Attributes:
13
+ name (str): The name of the node.
14
+ train (int): An integer flag representing whether the node is used for training (1) or testing (0).
15
+ test_fraction (float, optional): The fraction of data used for testing when train=1. Default is 0.2.
16
+ """
17
+
18
+ def __init__(
19
+ self, name: str, train: int, test_fraction: float = 0.2, engine=None
20
+ ):
21
+ """
22
+ Initialize a Node instance.
23
+
24
+ Parameters:
25
+ name (str): The name of the node.
26
+ train (int): An integer flag representing whether the node is used for training (1) or testing (0).
27
+ test_fraction (float, optional): The fraction of data used for testing when train=1. Default is 0.2.
28
+ """
29
+ self.name = name
30
+ self.train = train
31
+ self.test_fraction = 1.0 if self.train == 0 else test_fraction
32
+
33
+ db_manager = DatabaseManager()
34
+ db_manager.connect()
35
+ self.engine = db_manager.get_connection()
36
+
37
+ def validate(self):
38
+ """Validate name, train, test_fraction"""
39
+ if not isinstance(self.name, str):
40
+ raise TypeError("name argument must be a string")
41
+
42
+ if not isinstance(self.train, int):
43
+ raise TypeError("train argument must be an int")
44
+
45
+ if not isinstance(self.test_fraction, float):
46
+ raise TypeError("test_fraction argument must be a float")
47
+
48
+ def create_node(self, NetId: int):
49
+ """Create a node in the database.
50
+ Parameters:
51
+ NetId (int): The ID of the network to which the node belongs.
52
+
53
+ Returns:
54
+ None
55
+ """
56
+ self.engine.execute(
57
+ text(INSERT_NODE_QUERY.format(self.name, NetId, self.train))
58
+ )
59
+
60
+ def delete_node(self):
61
+ """Delete the node from the database."""
62
+ self.engine.execute(text(DELETE_NODE_QUERY.format(self.name)))
63
+
64
+ def check_dataset_compatibility(self, data_df):
65
+ """Check if the dataset is compatible with the master dataset.
66
+ Parameters:
67
+ data_df (DataFrame): The dataset to check.
68
+
69
+ Returns:
70
+ None
71
+ """
72
+ if master_table_exists() != 1:
73
+ print("MasterDataset doesn't exist")
74
+ else:
75
+ columns = data_df.columns.tolist()
76
+ # get master_dataset columns
77
+ master_table_columns = pd.read_sql(
78
+ text(SELECT_MASTER_COLUMNS_QUERY), self.engine
79
+ ).columns.tolist()
80
+ assert [x == y for x, y in zip(master_table_columns, columns)]
81
+
82
+ def update_node(self):
83
+ """Update the node information (not implemented)."""
84
+ pass
85
+
86
+ def get_dataset(self, column_name: str = None):
87
+ """Get the dataset for the node based on the given column name.
88
+ Parameters:
89
+ column_name (str, optional): The column name to filter the dataset. Default is None.
90
+
91
+ Returns:
92
+ DataFrame: The dataset associated with the node.
93
+ """
94
+ NodeId = get_nodeid_from_name(self.name)
95
+ if column_name is not None:
96
+
97
+ node_dataset = pd.read_sql(
98
+ text(
99
+ SELECT_DATASET_BY_COLUMN_QUERY.format(
100
+ column_name, self.name
101
+ )
102
+ ),
103
+ self.engine,
104
+ )
105
+
106
+ else:
107
+ node_dataset = pd.read_sql(
108
+ text(SELECT_DATASET_BY_NODE_ID_QUERY.format(NodeId)),
109
+ self.engine,
110
+ )
111
+ return node_dataset
112
+
113
+ def upload_dataset(self, dataset_name: str, path_to_csv: str = params['path_to_test_csv']):
114
+ """Upload the dataset to the database for the node.
115
+ Parameters:
116
+ dataset_name (str): The name of the dataset.
117
+ path_to_csv (str, optional): Path to the CSV file containing the dataset. Default is the path in params.
118
+
119
+ Returns:
120
+ None
121
+ """
122
+ data_df = pd.read_csv(path_to_csv)
123
+
124
+ nodeId = get_nodeid_from_name(self.name)
125
+ columns = data_df.columns.tolist()
126
+ self.check_dataset_compatibility(data_df)
127
+
128
+ data_df = process_eicu(data_df)
129
+ for index, row in data_df.iterrows():
130
+ query_1 = "INSERT INTO DataSets(DataSetName,nodeId," + "".join(
131
+ f"{x}," for x in columns
132
+ )
133
+ query_2 = f" VALUES ('{dataset_name}',{nodeId}, " + "".join(
134
+ f"{is_str(data_df, row, x)}," for x in columns
135
+ )
136
+ query = query_1[:-1] + ")" + query_2[:-1] + ")"
137
+ self.engine.execute(text(query))
138
+
139
+ def assign_dataset(self, dataset_name:str):
140
+ """Assigning existing dataSet to node
141
+ Parameters:
142
+ dataset_name (str): The name of the dataset to assign.
143
+
144
+ Returns:
145
+ None
146
+ """
147
+
148
+ nodeId = get_nodeid_from_name(self.name)
149
+ query = f"UPDATE DataSets SET nodeId = {nodeId} WHERE DataSetName = '{dataset_name}'"
150
+ self.engine.execute(text(query))
151
+
152
+ def unassign_dataset(self, dataset_name:str):
153
+ """unssigning existing dataSet to node
154
+ Parameters:
155
+ dataset_name (str): The name of the dataset to assign.
156
+
157
+ Returns:
158
+ None
159
+ """
160
+
161
+ query = f"UPDATE DataSets SET nodeId = {-1} WHERE DataSetName = '{dataset_name}'"
162
+ self.engine.execute(text(query))
163
+
164
+ def list_alldatasets(self):
165
+ """List all datasets associated with the node.
166
+ Returns:
167
+ DataFrame: A DataFrame containing information about all datasets associated with the node.
168
+
169
+ """
170
+ return pd.read_sql(
171
+ text(SELECT_ALL_DATASETS_QUERY.format(self.name)), self.engine
172
+ )
173
+
174
+ @staticmethod
175
+ def list_allnodes():
176
+ """List all nodes in the database.
177
+ Returns:
178
+ DataFrame: A DataFrame containing information about all nodes in the database.
179
+
180
+ """
181
+ db_manager = DatabaseManager() ;
182
+ db_manager.connect() ;
183
+ my_eng = db_manager.get_connection()
184
+
185
+ query = text(SELECT_ALL_NODES_QUERY)
186
+ res = pd.read_sql(query, my_eng)
187
+ return res
Medfl/__init__.py CHANGED
@@ -1,2 +1,2 @@
1
- from .LearningManager import *
1
+ from .LearningManager import *
2
2
  from .NetManager import *
@@ -1,22 +1,22 @@
1
- echo '#!/bin/bash
2
-
3
- # Update package lists
4
- sudo apt update
5
-
6
- # Install MySQL
7
- sudo apt install mysql-server
8
-
9
- # Secure MySQL installation
10
- sudo mysql_secure_installation
11
-
12
- # Install phpMyAdmin
13
- sudo apt install phpmyadmin
14
-
15
- # Create symbolic link for Apache
16
- sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
17
- sudo a2enconf phpmyadmin
18
- sudo systemctl reload apache2
19
-
20
- # Print completion message
21
- echo "MySQL and phpMyAdmin setup complete."
22
- ' > setup_mysql.sh && chmod +x setup_mysql.sh && python3 scripts/create_db.py
1
+ echo '#!/bin/bash
2
+
3
+ # Update package lists
4
+ sudo apt update
5
+
6
+ # Install MySQL
7
+ sudo apt install mysql-server
8
+
9
+ # Secure MySQL installation
10
+ sudo mysql_secure_installation
11
+
12
+ # Install phpMyAdmin
13
+ sudo apt install phpmyadmin
14
+
15
+ # Create symbolic link for Apache
16
+ sudo ln -s /etc/phpmyadmin/apache.conf /etc/apache2/conf-available/phpmyadmin.conf
17
+ sudo a2enconf phpmyadmin
18
+ sudo systemctl reload apache2
19
+
20
+ # Print completion message
21
+ echo "MySQL and phpMyAdmin setup complete."
22
+ ' > setup_mysql.sh && chmod +x setup_mysql.sh && python3 scripts/create_db.py