MEDfl 0.1.31__py3-none-any.whl → 0.1.33__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 (56) hide show
  1. {MEDfl-0.1.31.dist-info → MEDfl-0.1.33.dist-info}/METADATA +127 -128
  2. MEDfl-0.1.33.dist-info/RECORD +34 -0
  3. {MEDfl-0.1.31.dist-info → MEDfl-0.1.33.dist-info}/WHEEL +1 -1
  4. {MEDfl-0.1.31.dist-info → MEDfl-0.1.33.dist-info}/top_level.txt +0 -1
  5. Medfl/LearningManager/__init__.py +13 -13
  6. Medfl/LearningManager/client.py +150 -150
  7. Medfl/LearningManager/dynamicModal.py +287 -287
  8. Medfl/LearningManager/federated_dataset.py +60 -60
  9. Medfl/LearningManager/flpipeline.py +192 -192
  10. Medfl/LearningManager/model.py +223 -223
  11. Medfl/LearningManager/params.yaml +14 -14
  12. Medfl/LearningManager/params_optimiser.py +442 -442
  13. Medfl/LearningManager/plot.py +229 -229
  14. Medfl/LearningManager/server.py +181 -181
  15. Medfl/LearningManager/strategy.py +82 -82
  16. Medfl/LearningManager/utils.py +331 -308
  17. Medfl/NetManager/__init__.py +10 -9
  18. Medfl/NetManager/database_connector.py +43 -48
  19. Medfl/NetManager/dataset.py +92 -92
  20. Medfl/NetManager/flsetup.py +320 -320
  21. Medfl/NetManager/net_helper.py +254 -248
  22. Medfl/NetManager/net_manager_queries.py +142 -137
  23. Medfl/NetManager/network.py +194 -174
  24. Medfl/NetManager/node.py +184 -178
  25. Medfl/__init__.py +3 -2
  26. Medfl/scripts/__init__.py +2 -0
  27. Medfl/scripts/base.py +30 -0
  28. Medfl/scripts/create_db.py +126 -0
  29. alembic/env.py +61 -61
  30. scripts/base.py +29 -29
  31. scripts/config.ini +5 -5
  32. scripts/create_db.py +133 -133
  33. MEDfl/LearningManager/__init__.py +0 -13
  34. MEDfl/LearningManager/client.py +0 -150
  35. MEDfl/LearningManager/dynamicModal.py +0 -287
  36. MEDfl/LearningManager/federated_dataset.py +0 -60
  37. MEDfl/LearningManager/flpipeline.py +0 -192
  38. MEDfl/LearningManager/model.py +0 -223
  39. MEDfl/LearningManager/params.yaml +0 -14
  40. MEDfl/LearningManager/params_optimiser.py +0 -442
  41. MEDfl/LearningManager/plot.py +0 -229
  42. MEDfl/LearningManager/server.py +0 -181
  43. MEDfl/LearningManager/strategy.py +0 -82
  44. MEDfl/LearningManager/utils.py +0 -333
  45. MEDfl/NetManager/__init__.py +0 -9
  46. MEDfl/NetManager/database_connector.py +0 -48
  47. MEDfl/NetManager/dataset.py +0 -92
  48. MEDfl/NetManager/flsetup.py +0 -320
  49. MEDfl/NetManager/net_helper.py +0 -248
  50. MEDfl/NetManager/net_manager_queries.py +0 -137
  51. MEDfl/NetManager/network.py +0 -174
  52. MEDfl/NetManager/node.py +0 -178
  53. MEDfl/__init__.py +0 -2
  54. MEDfl-0.1.31.data/scripts/setup_mysql.sh +0 -22
  55. MEDfl-0.1.31.dist-info/RECORD +0 -54
  56. scripts/db_config.ini +0 -6
@@ -1,128 +1,127 @@
1
- Metadata-Version: 2.1
2
- Name: MEDfl
3
- Version: 0.1.31
4
- Summary: Python Open-source package for simulating federated learning and differential privacy
5
- Home-page: https://github.com/HaithemLamri/MEDfl
6
- Author: MEDomics consortium
7
- Author-email: medomics.info@gmail.com
8
- Project-URL: Documentation, https://
9
- Project-URL: Github, https://github.com/HaithemLamri/MEDfl
10
- Keywords: federated learning differential privacy medical research
11
- Classifier: Development Status :: 3 - Alpha
12
- Classifier: Intended Audience :: Developers
13
- Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
14
- Classifier: License :: OSI Approved :: MIT License
15
- Classifier: Programming Language :: Python :: 3.8
16
- Classifier: Programming Language :: Python :: 3.9
17
- Requires-Python: >=3.8,<3.11
18
- Description-Content-Type: text/markdown
19
- Requires-Dist: flwr (~=1.7.0)
20
- Requires-Dist: matplotlib (~=3.6.3)
21
- Requires-Dist: numpy (~=1.24.2)
22
- Requires-Dist: opacus (~=1.3.0)
23
- Requires-Dist: pandas (~=1.5.2)
24
- Requires-Dist: PyYAML (~=6.0)
25
- Requires-Dist: setuptools (~=68.0.0)
26
- Requires-Dist: Sphinx (~=5.3.0)
27
- Requires-Dist: SQLAlchemy (~=1.4.47)
28
- Requires-Dist: torch (~=1.13.1)
29
- Requires-Dist: datetime (~=5.1)
30
- Requires-Dist: scikit-learn (~=1.1.1)
31
- Requires-Dist: sphinx-jsonschema (==1.19.1)
32
- Requires-Dist: sphinx-rtd-dark-mode (==1.2.4)
33
- Requires-Dist: plotly (==5.19.0)
34
- Requires-Dist: optuna (==3.5.0)
35
- Requires-Dist: mysql
36
- Requires-Dist: mysql-connector-python
37
- Requires-Dist: python-dotenv (==1.0.1)
38
-
39
- # MEDfl : Federated Learning and Differential Privacy Simulation Tool for Tabular Data
40
- ![Python Versions](https://img.shields.io/badge/python-3.9-blue)
41
- ![Build Status](https://travis-ci.org/MEDomics-UdeS/MEDfl.svg?branch=main)
42
-
43
- ![GitHub contributors](https://img.shields.io/github/contributors/scottydocs/README-template.md)
44
- ![License: MIT](https://img.shields.io/badge/license-MIT-green)
45
-
46
-
47
-
48
-
49
- ## Table of Contents
50
- * [1. Introduction](#1-introduction)
51
- * [2. Installation](#2-installation)
52
- * [3. Documentation](#3-documentation)
53
- * [4. Getting started](#4-Getting-started)
54
- * [5. Acknowledgement](#5-acknowledgement)
55
- * [6. Authors](#6-authors)
56
- * [7. Statement](#7-statement)
57
-
58
- ## 1. Introduction
59
- This Python package is an open-source tool designed for simulating federated learning and incorporating differential privacy. It empowers researchers and developers to effortlessly create, execute, and assess federated learning pipelines while seamlessly working with various tabular datasets.
60
-
61
-
62
-
63
-
64
-
65
- ## 2. Installation
66
-
67
- ### Python installation
68
- The MEDfl package requires *python 3.9* or more to be run. If you don't have it installed on your machine, check out the following link [Python ](https://www.python.org/downloads/).
69
- It also requires MySQL database
70
-
71
- ### Package installation
72
- For now, you can install the ``MEDfl``package as:
73
- ```
74
- git clone https://github.com/MEDomics-UdeS/MEDfl.git
75
- cd MEDfl
76
- pip install -e .
77
- ```
78
- ### MySQL DB configuration
79
- MEDfl requires a MySQL DB connection, and this is in order to allow users to work with their own tabular datasets, we have created a bash script to install and configure A MySQL DB with phpmyadmin monitoring system, run the following command then change your credential on the MEDfl/scripts/base.py and MEDfl/scripts/db_config.ini files
80
- ```
81
- sudo bash MEDfl/scripts/setup_mysql.sh
82
- ```
83
-
84
- ### Project Base URL Update
85
- Please ensure to modify the `base_url` parameter in the `MEDfl/global_params.yaml` file. The `base_url` represents the path to the MEDfl project on your local machine. Update this value accordingly.
86
-
87
- ## 3. Documentation
88
- We used sphinx to create the documentation for this project. you can generate and host it locally by compiling the documentation source code using:
89
- ```
90
- cd docs
91
- make clean
92
- make html
93
- ```
94
-
95
- Then open it locally using:
96
-
97
- ```
98
- cd _build/html
99
- python -m http.server
100
- ```
101
-
102
- ## 4. Getting started
103
-
104
- We have created a complete tutorial for the different functionalities of the package. it can be found here [tutorial](https://github.com/MEDomics-UdeS/MEDfl/notebooks/First_tuto.ipynb)
105
-
106
-
107
-
108
- ## 5. Acknowledgment
109
- MEDfl is an open-source package that welcomes any contribution and feedback. We wish that this package could serve the growing private AI research community.
110
-
111
- ## 6. Authors
112
- * [MEDomics](https://github.com/medomics/): MEDomics consortium.
113
-
114
- ## 7. Statement
115
-
116
- This package is part of https://github.com/medomics, a package providing research utility tools for developing precision medicine applications.
117
-
118
- ```
119
- MIT License
120
-
121
- Copyright (C) 2022 MEDomics consortium
122
-
123
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
124
-
125
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
126
-
127
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
128
- ```
1
+ Metadata-Version: 2.1
2
+ Name: MEDfl
3
+ Version: 0.1.33
4
+ Summary: Python Open-source package for simulating federated learning and differential privacy
5
+ Home-page: https://github.com/MEDomics-UdeS/MEDfl
6
+ Author: MEDomics consortium
7
+ Author-email: medomics.info@gmail.com
8
+ Project-URL: Documentation, https://
9
+ Project-URL: Github, https://github.com/MEDomics-UdeS/MEDfl
10
+ Keywords: federated learning differential privacy medical research
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3.8
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Requires-Python: >=3.8,<3.11
18
+ Description-Content-Type: text/markdown
19
+ Requires-Dist: flwr ~=1.7.0
20
+ Requires-Dist: matplotlib ~=3.6.3
21
+ Requires-Dist: numpy ~=1.24.2
22
+ Requires-Dist: opacus ~=1.3.0
23
+ Requires-Dist: pandas ~=1.5.2
24
+ Requires-Dist: PyYAML ~=6.0
25
+ Requires-Dist: setuptools ~=68.0.0
26
+ Requires-Dist: Sphinx ~=5.3.0
27
+ Requires-Dist: SQLAlchemy ~=1.4.47
28
+ Requires-Dist: torch ~=1.13.1
29
+ Requires-Dist: datetime ~=5.1
30
+ Requires-Dist: scikit-learn ~=1.1.1
31
+ Requires-Dist: sphinx-jsonschema ==1.19.1
32
+ Requires-Dist: sphinx-rtd-dark-mode ==1.2.4
33
+ Requires-Dist: plotly ==5.19.0
34
+ Requires-Dist: optuna ==3.5.0
35
+ Requires-Dist: mysql
36
+ Requires-Dist: mysql-connector-python
37
+
38
+ # MEDfl : Federated Learning and Differential Privacy Simulation Tool for Tabular Data
39
+ ![Python Versions](https://img.shields.io/badge/python-3.9-blue)
40
+ ![Build Status](https://travis-ci.org/MEDomics-UdeS/MEDfl.svg?branch=main)
41
+
42
+ ![GitHub contributors](https://img.shields.io/github/contributors/scottydocs/README-template.md)
43
+ ![License: MIT](https://img.shields.io/badge/license-MIT-green)
44
+
45
+
46
+
47
+
48
+ ## Table of Contents
49
+ * [1. Introduction](#1-introduction)
50
+ * [2. Installation](#2-installation)
51
+ * [3. Documentation](#3-documentation)
52
+ * [4. Getting started](#4-Getting-started)
53
+ * [5. Acknowledgement](#5-acknowledgement)
54
+ * [6. Authors](#6-authors)
55
+ * [7. Statement](#7-statement)
56
+
57
+ ## 1. Introduction
58
+ This Python package is an open-source tool designed for simulating federated learning and incorporating differential privacy. It empowers researchers and developers to effortlessly create, execute, and assess federated learning pipelines while seamlessly working with various tabular datasets.
59
+
60
+
61
+
62
+
63
+
64
+ ## 2. Installation
65
+
66
+ ### Python installation
67
+ The MEDfl package requires *python 3.9* or more to be run. If you don't have it installed on your machine, check out the following link [Python ](https://www.python.org/downloads/).
68
+ It also requires MySQL database
69
+
70
+ ### Package installation
71
+ For now, you can install the ``MEDfl``package as:
72
+ ```
73
+ git clone https://github.com/MEDomics-UdeS/MEDfl.git
74
+ cd MEDfl
75
+ pip install -e .
76
+ ```
77
+ ### MySQL DB configuration
78
+ MEDfl requires a MySQL DB connection, and this is in order to allow users to work with their own tabular datasets, we have created a bash script to install and configure A MySQL DB with phpmyadmin monitoring system, run the following command then change your credential on the MEDfl/scripts/base.py and MEDfl/scripts/db_config.ini files
79
+ ```
80
+ sudo bash MEDfl/scripts/setup_mysql.sh
81
+ ```
82
+
83
+ ### Project Base URL Update
84
+ Please ensure to modify the `base_url` parameter in the `MEDfl/global_params.yaml` file. The `base_url` represents the path to the MEDfl project on your local machine. Update this value accordingly.
85
+
86
+ ## 3. Documentation
87
+ We used sphinx to create the documentation for this project. you can generate and host it locally by compiling the documentation source code using:
88
+ ```
89
+ cd docs
90
+ make clean
91
+ make html
92
+ ```
93
+
94
+ Then open it locally using:
95
+
96
+ ```
97
+ cd _build/html
98
+ python -m http.server
99
+ ```
100
+
101
+ ## 4. Getting started
102
+
103
+ We have created a complete tutorial for the different functionalities of the package. it can be found here [tutorial](https://github.com/MEDomics-UdeS/MEDfl/notebooks/First_tuto.ipynb)
104
+
105
+
106
+
107
+ ## 5. Acknowledgment
108
+ MEDfl is an open-source package that welcomes any contribution and feedback. We wish that this package could serve the growing private AI research community.
109
+
110
+ ## 6. Authors
111
+ * [MEDomics](https://github.com/medomics/): MEDomics consortium.
112
+
113
+ ## 7. Statement
114
+
115
+ This package is part of https://github.com/medomics, a package providing research utility tools for developing precision medicine applications.
116
+
117
+ ```
118
+ MIT License
119
+
120
+ Copyright (C) 2022 MEDomics consortium
121
+
122
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
123
+
124
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
125
+
126
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
127
+ ```
@@ -0,0 +1,34 @@
1
+ Medfl/__init__.py,sha256=pMyTkws4slDkLQgPpPdKoWdQf1rZGHXAlNxfPFFZM-I,81
2
+ Medfl/LearningManager/__init__.py,sha256=_lfMR4HOx4wmJnGKCowXnln9oKQEkOZexUzZRBSgfZI,334
3
+ Medfl/LearningManager/client.py,sha256=9WyLYCsI9JuHjneLbbzDf7HtzjYINuLfqwkbxOsrBrE,6083
4
+ Medfl/LearningManager/dynamicModal.py,sha256=0mTvDJlss0uSJ3_EXOuL_d-zRmFyXaKB4W4ql-uEX8Y,10821
5
+ Medfl/LearningManager/federated_dataset.py,sha256=fQqIbhO6LSk16Ob9z6RohaZ8X71Ff-yueynjulrl4M0,2141
6
+ Medfl/LearningManager/flpipeline.py,sha256=M4-OL4nlogv08J_YsyDsGHXR6xe8BWx4HIsuL1QyUvY,7303
7
+ Medfl/LearningManager/model.py,sha256=DA7HP34Eq1Ra65OlkBmjH9d2MD7OEbsOhfxD48l4QOk,7679
8
+ Medfl/LearningManager/params.yaml,sha256=5I-NljhnSaqzjkWVNzrOtrB8z7tnHBKiBmY-mKGhBQM,450
9
+ Medfl/LearningManager/params_optimiser.py,sha256=pjhDskhSPuca-jnarYoJcFVBvRkdD9tD3992q_eMPSE,18060
10
+ Medfl/LearningManager/plot.py,sha256=iPqMV9rVd7hquoFixDL20OzXI5bMpBW41bkVmTKIWtE,7927
11
+ Medfl/LearningManager/server.py,sha256=7edxPkZ9Ju3Mep_BSHQpUNgW9HKfCui3_l996buJVlU,7258
12
+ Medfl/LearningManager/strategy.py,sha256=n0asQajkHfGLDX3QbbV5qntQA-xuJZU8Z92XccZENsA,3517
13
+ Medfl/LearningManager/utils.py,sha256=vEhkpyC7iLsn4wp1wDh7GzAn5MCJ7T69jkS9lfmKA1Y,9936
14
+ Medfl/NetManager/__init__.py,sha256=kR9muJ9fdGMglVmWaraAMiJia_TiTSUcZaNgHS6Cglg,269
15
+ Medfl/NetManager/database_connector.py,sha256=JKfFLom7I4zuykb8m7aY4cUYZy4j0-i9w3R_jkPzjXY,1516
16
+ Medfl/NetManager/dataset.py,sha256=eEuVzCp5dGD4tvDVKq6jlSReecge7T20ByG4d7_cnXU,2869
17
+ Medfl/NetManager/flsetup.py,sha256=CS7531I08eLm6txMIDWFMCIrPP-dNpOLBTaR2BR6X0c,11754
18
+ Medfl/NetManager/net_helper.py,sha256=T5Y-03SVskK8oXXIpiXzASyEDPQJbcuGVpIS9FnmzI8,7066
19
+ Medfl/NetManager/net_manager_queries.py,sha256=bJ7V6NZAjLM0sEPrczN7vUeB7sMZZzYZjO-rnUCpQPc,4228
20
+ Medfl/NetManager/network.py,sha256=D7liCx-1PGx9Z3pctI1_NOGG--gGtvzfr_-BCOdwsOg,7023
21
+ Medfl/NetManager/node.py,sha256=Azye74P4ZUDQxPrQOId4BK8hX-9RzFtN2Ji8NXbDCEM,6726
22
+ Medfl/scripts/__init__.py,sha256=wAcxrhYRhcTI3CAmCCd4D2psvimuig-DPXQyij0BZT4,45
23
+ Medfl/scripts/base.py,sha256=pR7StIt3PpX30aoh53gMkpeNJMHytAPhdc7N09tCITA,781
24
+ Medfl/scripts/create_db.py,sha256=PgA6N68iTSfnrt6zy7FDZX2lLjQQ7Ual1Y0efve8gf4,3943
25
+ alembic/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
26
+ alembic/env.py,sha256=a4zJAzPNLHnIrUlXCqf_8vuAlFu0pceFJJKM1PQaOI4,1649
27
+ scripts/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
+ scripts/base.py,sha256=pR7StIt3PpX30aoh53gMkpeNJMHytAPhdc7N09tCITA,781
29
+ scripts/config.ini,sha256=tDQQlpwZbCFQuSS017yjEiLglLihp6wETbWtNrSWeAA,82
30
+ scripts/create_db.py,sha256=iWE1z33rU_KbIeqoVzdWLhDRLk00TcRf2iYuqpDzOjw,4494
31
+ MEDfl-0.1.33.dist-info/METADATA,sha256=KNlaBmacbxNxH6m4WDWsAR4H4-qt7jGU3f4_l2hGXMI,5582
32
+ MEDfl-0.1.33.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
33
+ MEDfl-0.1.33.dist-info/top_level.txt,sha256=dIL9X8HOFuaVSzpg40DVveDPrymWRoShHtspH7kkjdI,14
34
+ MEDfl-0.1.33.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.37.1)
2
+ Generator: bdist_wheel (0.43.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,3 +1,2 @@
1
1
  MEDfl
2
2
  alembic
3
- scripts
@@ -1,13 +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 *
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 *
@@ -1,150 +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)}
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)}