federated-rsf 0.1.0__tar.gz
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.
- federated_rsf-0.1.0/.github/workflows/publish.yml +38 -0
- federated_rsf-0.1.0/.gitignore +209 -0
- federated_rsf-0.1.0/LICENSE +21 -0
- federated_rsf-0.1.0/PKG-INFO +204 -0
- federated_rsf-0.1.0/README.md +182 -0
- federated_rsf-0.1.0/examples/save_load.ipynb +264 -0
- federated_rsf-0.1.0/examples/single_dataset.ipynb +350 -0
- federated_rsf-0.1.0/examples/single_dataset_figures/aids_c_index_comparison.png +0 -0
- federated_rsf-0.1.0/examples/single_dataset_figures/breast_cancer_c_index_comparison.png +0 -0
- federated_rsf-0.1.0/examples/single_dataset_figures/flchain_c_index_comparison.png +0 -0
- federated_rsf-0.1.0/examples/single_dataset_figures/gbsg2_c_index_comparison.png +0 -0
- federated_rsf-0.1.0/examples/single_dataset_figures/veterans_lung_cancer_c_index_comparison.png +0 -0
- federated_rsf-0.1.0/examples/single_dataset_figures/whas500_c_index_comparison.png +0 -0
- federated_rsf-0.1.0/examples/true_federation.ipynb +586 -0
- federated_rsf-0.1.0/pyproject.toml +32 -0
- federated_rsf-0.1.0/src/federated_rsf/__init__.py +6 -0
- federated_rsf-0.1.0/src/federated_rsf/models.py +543 -0
- federated_rsf-0.1.0/src/federated_rsf/persistence.py +37 -0
- federated_rsf-0.1.0/src/federated_rsf/schema.py +296 -0
- federated_rsf-0.1.0/src/federated_rsf/testing.py +153 -0
- federated_rsf-0.1.0/tests/manual_end_to_end_testing.py +48 -0
- federated_rsf-0.1.0/tests/readme_testing.py +100 -0
- federated_rsf-0.1.0/tests/test_FederatedRandomSurvivalForest.py +98 -0
- federated_rsf-0.1.0/tests/test_LocalRandomSurvivalForest.py +133 -0
- federated_rsf-0.1.0/tests/test_SchemaAligner.py +79 -0
- federated_rsf-0.1.0/tests/test_SchemaCreator.py +79 -0
- federated_rsf-0.1.0/tests/test_create_dummy_data.py +33 -0
- federated_rsf-0.1.0/tests/test_end_to_end.py +75 -0
- federated_rsf-0.1.0/tests/test_federate_data.py +140 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
release:
|
|
9
|
+
types: [published]
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
test:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v4
|
|
16
|
+
- uses: actions/setup-python@v5
|
|
17
|
+
with:
|
|
18
|
+
python-version: "3.11"
|
|
19
|
+
- run: pip install -e ".[test]"
|
|
20
|
+
- run: pytest
|
|
21
|
+
|
|
22
|
+
publish:
|
|
23
|
+
needs: test
|
|
24
|
+
runs-on: ubuntu-latest
|
|
25
|
+
if: github.event_name == 'release' && github.event.action == 'published'
|
|
26
|
+
environment:
|
|
27
|
+
name: pypi
|
|
28
|
+
url: https://pypi.org/p/federated-rsf
|
|
29
|
+
permissions:
|
|
30
|
+
id-token: write
|
|
31
|
+
steps:
|
|
32
|
+
- uses: actions/checkout@v4
|
|
33
|
+
- uses: actions/setup-python@v5
|
|
34
|
+
with:
|
|
35
|
+
python-version: "3.11"
|
|
36
|
+
- run: pip install build
|
|
37
|
+
- run: python -m build
|
|
38
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
*.pkl
|
|
2
|
+
# Byte-compiled / optimized / DLL files
|
|
3
|
+
__pycache__/
|
|
4
|
+
*.py[codz]
|
|
5
|
+
*$py.class
|
|
6
|
+
|
|
7
|
+
# C extensions
|
|
8
|
+
*.so
|
|
9
|
+
|
|
10
|
+
# Distribution / packaging
|
|
11
|
+
.Python
|
|
12
|
+
build/
|
|
13
|
+
develop-eggs/
|
|
14
|
+
dist/
|
|
15
|
+
downloads/
|
|
16
|
+
eggs/
|
|
17
|
+
.eggs/
|
|
18
|
+
lib/
|
|
19
|
+
lib64/
|
|
20
|
+
parts/
|
|
21
|
+
sdist/
|
|
22
|
+
var/
|
|
23
|
+
wheels/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.tox/
|
|
43
|
+
.nox/
|
|
44
|
+
.coverage
|
|
45
|
+
.coverage.*
|
|
46
|
+
.cache
|
|
47
|
+
nosetests.xml
|
|
48
|
+
coverage.xml
|
|
49
|
+
*.cover
|
|
50
|
+
*.py.cover
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
cover/
|
|
54
|
+
|
|
55
|
+
# Translations
|
|
56
|
+
*.mo
|
|
57
|
+
*.pot
|
|
58
|
+
|
|
59
|
+
# Django stuff:
|
|
60
|
+
*.log
|
|
61
|
+
local_settings.py
|
|
62
|
+
db.sqlite3
|
|
63
|
+
db.sqlite3-journal
|
|
64
|
+
|
|
65
|
+
# Flask stuff:
|
|
66
|
+
instance/
|
|
67
|
+
.webassets-cache
|
|
68
|
+
|
|
69
|
+
# Scrapy stuff:
|
|
70
|
+
.scrapy
|
|
71
|
+
|
|
72
|
+
# Sphinx documentation
|
|
73
|
+
docs/_build/
|
|
74
|
+
|
|
75
|
+
# PyBuilder
|
|
76
|
+
.pybuilder/
|
|
77
|
+
target/
|
|
78
|
+
|
|
79
|
+
# Jupyter Notebook
|
|
80
|
+
.ipynb_checkpoints
|
|
81
|
+
|
|
82
|
+
# IPython
|
|
83
|
+
profile_default/
|
|
84
|
+
ipython_config.py
|
|
85
|
+
|
|
86
|
+
# pyenv
|
|
87
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
88
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
89
|
+
# .python-version
|
|
90
|
+
|
|
91
|
+
# pipenv
|
|
92
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
93
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
94
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
95
|
+
# install all needed dependencies.
|
|
96
|
+
#Pipfile.lock
|
|
97
|
+
|
|
98
|
+
# UV
|
|
99
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
100
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
101
|
+
# commonly ignored for libraries.
|
|
102
|
+
#uv.lock
|
|
103
|
+
|
|
104
|
+
# poetry
|
|
105
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
106
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
107
|
+
# commonly ignored for libraries.
|
|
108
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
109
|
+
#poetry.lock
|
|
110
|
+
#poetry.toml
|
|
111
|
+
|
|
112
|
+
# pdm
|
|
113
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
114
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
115
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
116
|
+
#pdm.lock
|
|
117
|
+
#pdm.toml
|
|
118
|
+
.pdm-python
|
|
119
|
+
.pdm-build/
|
|
120
|
+
|
|
121
|
+
# pixi
|
|
122
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
123
|
+
#pixi.lock
|
|
124
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
125
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
126
|
+
.pixi
|
|
127
|
+
|
|
128
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
129
|
+
__pypackages__/
|
|
130
|
+
|
|
131
|
+
# Celery stuff
|
|
132
|
+
celerybeat-schedule
|
|
133
|
+
celerybeat.pid
|
|
134
|
+
|
|
135
|
+
# SageMath parsed files
|
|
136
|
+
*.sage.py
|
|
137
|
+
|
|
138
|
+
# Environments
|
|
139
|
+
.env
|
|
140
|
+
.envrc
|
|
141
|
+
.venv
|
|
142
|
+
env/
|
|
143
|
+
venv/
|
|
144
|
+
311env/
|
|
145
|
+
ENV/
|
|
146
|
+
env.bak/
|
|
147
|
+
venv.bak/
|
|
148
|
+
|
|
149
|
+
# Spyder project settings
|
|
150
|
+
.spyderproject
|
|
151
|
+
.spyproject
|
|
152
|
+
|
|
153
|
+
# Rope project settings
|
|
154
|
+
.ropeproject
|
|
155
|
+
|
|
156
|
+
# mkdocs documentation
|
|
157
|
+
/site
|
|
158
|
+
|
|
159
|
+
# mypy
|
|
160
|
+
.mypy_cache/
|
|
161
|
+
.dmypy.json
|
|
162
|
+
dmypy.json
|
|
163
|
+
|
|
164
|
+
# Pyre type checker
|
|
165
|
+
.pyre/
|
|
166
|
+
|
|
167
|
+
# pytype static type analyzer
|
|
168
|
+
.pytype/
|
|
169
|
+
|
|
170
|
+
# Cython debug symbols
|
|
171
|
+
cython_debug/
|
|
172
|
+
|
|
173
|
+
# PyCharm
|
|
174
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
175
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
176
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
177
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
178
|
+
#.idea/
|
|
179
|
+
|
|
180
|
+
# Abstra
|
|
181
|
+
# Abstra is an AI-powered process automation framework.
|
|
182
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
183
|
+
# Learn more at https://abstra.io/docs
|
|
184
|
+
.abstra/
|
|
185
|
+
|
|
186
|
+
# Visual Studio Code
|
|
187
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
188
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
189
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
190
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
191
|
+
# .vscode/
|
|
192
|
+
|
|
193
|
+
# Ruff stuff:
|
|
194
|
+
.ruff_cache/
|
|
195
|
+
|
|
196
|
+
# PyPI configuration file
|
|
197
|
+
.pypirc
|
|
198
|
+
|
|
199
|
+
# Cursor
|
|
200
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
201
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
202
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
203
|
+
.cursorignore
|
|
204
|
+
.cursorindexingignore
|
|
205
|
+
|
|
206
|
+
# Marimo
|
|
207
|
+
marimo/_static/
|
|
208
|
+
marimo/_lsp/
|
|
209
|
+
__marimo__/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jonas H
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: federated-rsf
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Implementation of Federated Random Survival Forest for partially overlapping data.
|
|
5
|
+
Author-email: Jonas Harriehausen <jonas.harriehausen@uni-giessen.de>
|
|
6
|
+
License: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Python: >=3.11
|
|
9
|
+
Requires-Dist: joblib>=1.5.0
|
|
10
|
+
Requires-Dist: numpy>=2.0.0
|
|
11
|
+
Requires-Dist: pandas>=2.3.0
|
|
12
|
+
Requires-Dist: scikit-learn>=1.8.0
|
|
13
|
+
Requires-Dist: scikit-survival>=0.27.0
|
|
14
|
+
Provides-Extra: dev
|
|
15
|
+
Requires-Dist: jupyter; extra == 'dev'
|
|
16
|
+
Requires-Dist: pytest-cov; extra == 'dev'
|
|
17
|
+
Requires-Dist: pytest>=9.0.0; extra == 'dev'
|
|
18
|
+
Provides-Extra: test
|
|
19
|
+
Requires-Dist: pytest-cov; extra == 'test'
|
|
20
|
+
Requires-Dist: pytest>=9.0.0; extra == 'test'
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
|
|
23
|
+
# Federated Random Survival Forest for Partially overlapping Data
|
|
24
|
+
|
|
25
|
+
federated-rsf is a python implementation of the Federated Random Survival Forest algorithm for partially overlapping data.
|
|
26
|
+
|
|
27
|
+
## Overview
|
|
28
|
+
|
|
29
|
+
Federated Random Survival Forest (federated-rsf) enables training
|
|
30
|
+
random survival forest models across multiple institutions without sharing raw data.
|
|
31
|
+
It is designed for partially overlapping feature spaces and privacy-sensitive
|
|
32
|
+
biomedical datasets.
|
|
33
|
+
|
|
34
|
+
## Features
|
|
35
|
+
|
|
36
|
+
- Federated survival random forest training
|
|
37
|
+
- Support for partially overlapping feature spaces
|
|
38
|
+
- Compatible with scikit-survival data structures and evaluation methods
|
|
39
|
+
- Privacy-preserving model aggregation
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
## Installation
|
|
43
|
+
|
|
44
|
+
### Dependencies
|
|
45
|
+
|
|
46
|
+
federated-rsf requires:
|
|
47
|
+
- numpy (>=2.0.0)
|
|
48
|
+
- pandas (>=2.3.0)
|
|
49
|
+
- scikit-learn(>=1.8.0)
|
|
50
|
+
- scikit-survival (>=0.27.0)
|
|
51
|
+
|
|
52
|
+
### User installation
|
|
53
|
+
|
|
54
|
+
The easiest way to install federated-rsf is using pip
|
|
55
|
+
```
|
|
56
|
+
pip install -U federated-rsf
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
To install in editable mode, clone the repository and then install it using pip
|
|
60
|
+
```bash
|
|
61
|
+
git https://github.com/HauschildLab/FRSF4POD.git
|
|
62
|
+
cd FRSF4POD
|
|
63
|
+
pip install -U .
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
To install in editable mode it with optional testing or development libraries uses
|
|
67
|
+
```bash
|
|
68
|
+
pip install -U -e .[dev]
|
|
69
|
+
```
|
|
70
|
+
or
|
|
71
|
+
```bash
|
|
72
|
+
pip install -U -e .[test]
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Quick Start
|
|
76
|
+
|
|
77
|
+
federated-rsf uses three main steps to train federated models.
|
|
78
|
+
- First the local data schema of all the clients has to be unified into a global schema
|
|
79
|
+
to facilitate the aggregation of models.
|
|
80
|
+
- Second is the training of the local-rsf models on the local data
|
|
81
|
+
- Third is the aggregation and distribution of the local estimators from the clients.
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from federated_rsf.models import (
|
|
86
|
+
FederatedRandomSurvivalForest,
|
|
87
|
+
LocalRandomSurvivalForest,
|
|
88
|
+
)
|
|
89
|
+
from federated_rsf.preprocessing import SchemaAligner, SchemaCreator
|
|
90
|
+
from federated_rsf.testing import create_dummy_data, federate_data
|
|
91
|
+
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
In this example we create a dummy dataset using the testing module.
|
|
95
|
+
This module can be used valiate the federated learning pipeline is case of missing access to the actual data.
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
# Parameters
|
|
99
|
+
n_samples = 500
|
|
100
|
+
n_features = 10
|
|
101
|
+
n_clients = 5
|
|
102
|
+
random_state = 0
|
|
103
|
+
|
|
104
|
+
# Create Dummy Dataset
|
|
105
|
+
X, y = create_dummy_data(
|
|
106
|
+
n_samples,
|
|
107
|
+
n_features,
|
|
108
|
+
random_state=random_state,
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
# Split Dataset samples up to all clients
|
|
112
|
+
X_list, y_list = federate_data(
|
|
113
|
+
X,
|
|
114
|
+
y,
|
|
115
|
+
n_clients,
|
|
116
|
+
drop_feature_percentage=0.33,
|
|
117
|
+
random_state=random_state,
|
|
118
|
+
)
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
Next the columns of the local datasets are aligned to a global schema using the `SchemaCreator` and the local
|
|
122
|
+
```python
|
|
123
|
+
# Create global Schema
|
|
124
|
+
schema_creator = SchemaCreator(anonymize=False)
|
|
125
|
+
local_columns = [DatasetSchema(X_local.columns) for X_local in X_list]
|
|
126
|
+
dataset_schemas = schema_creator.fit_transform(local_columns)
|
|
127
|
+
|
|
128
|
+
# Align local datasets
|
|
129
|
+
X_list_aligned = []
|
|
130
|
+
|
|
131
|
+
for X_local, schema in zip(X_list, dataset_schemas):
|
|
132
|
+
schema_aligner = SchemaAligner()
|
|
133
|
+
X_aligned = schema_aligner.fit_transform(X_local, schema)
|
|
134
|
+
X_list_aligned.append(X_aligned)
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The local models can then be trained on the processed local data.
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
# Train local models
|
|
141
|
+
local_models: list[LocalRandomSurvivalForest] = []
|
|
142
|
+
|
|
143
|
+
for X_local, y_local in zip(X_list_aligned, y_list):
|
|
144
|
+
local_model = LocalRandomSurvivalForest(
|
|
145
|
+
random_state=random_state,
|
|
146
|
+
)
|
|
147
|
+
local_model = local_model.fit(X_local, y_local)
|
|
148
|
+
local_models.append(local_model)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
The trained local models are then aggregated and the estimators are redistributed using the federated model.
|
|
152
|
+
|
|
153
|
+
```python
|
|
154
|
+
# Distribute trees between local models
|
|
155
|
+
fed_model = FederatedRandomSurvivalForest(local_models=local_models)
|
|
156
|
+
fed_model.distribute_trees()
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Lastly you can compare the local and the federated model performance for example using the predict `predict_survival_function` and `predict_cumulative_hazard_function`
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
# Example visualization of survival function and cumulative hazard function
|
|
164
|
+
client_index = 0
|
|
165
|
+
n_lines = 5
|
|
166
|
+
|
|
167
|
+
survival_local = local_models[client_index].predict_survival_function(
|
|
168
|
+
X_list_aligned[client_index]
|
|
169
|
+
)
|
|
170
|
+
|
|
171
|
+
hazard_local = local_models[client_index].predict_cumulative_hazard_function(
|
|
172
|
+
X_list_aligned[client_index]
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
local_models[client_index].use_federated_estimators()
|
|
176
|
+
|
|
177
|
+
survival_federated = local_models[client_index].predict_survival_function(
|
|
178
|
+
X_list_aligned[client_index]
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
hazard_federated = local_models[client_index].predict_cumulative_hazard_function(
|
|
182
|
+
X_list_aligned[client_index]
|
|
183
|
+
)
|
|
184
|
+
from matplotlib import pyplot as plt
|
|
185
|
+
|
|
186
|
+
for surv in [survival_local, survival_federated]:
|
|
187
|
+
for i, s in enumerate(surv[:n_lines]):
|
|
188
|
+
plt.step(s.x, s.y, where="post", label=str(i))
|
|
189
|
+
plt.ylabel("Survival probability")
|
|
190
|
+
plt.xlabel("Time in days")
|
|
191
|
+
plt.legend()
|
|
192
|
+
plt.grid(True)
|
|
193
|
+
plt.show()
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
for hazard in [hazard_local, hazard_federated]:
|
|
197
|
+
for i, s in enumerate(hazard[:n_lines]):
|
|
198
|
+
plt.step(s.x, s.y, where="post", label=str(i))
|
|
199
|
+
plt.ylabel("Cumulative hazard")
|
|
200
|
+
plt.xlabel("Time in days")
|
|
201
|
+
plt.legend()
|
|
202
|
+
plt.grid(True)
|
|
203
|
+
plt.show()
|
|
204
|
+
```
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Federated Random Survival Forest for Partially overlapping Data
|
|
2
|
+
|
|
3
|
+
federated-rsf is a python implementation of the Federated Random Survival Forest algorithm for partially overlapping data.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Federated Random Survival Forest (federated-rsf) enables training
|
|
8
|
+
random survival forest models across multiple institutions without sharing raw data.
|
|
9
|
+
It is designed for partially overlapping feature spaces and privacy-sensitive
|
|
10
|
+
biomedical datasets.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- Federated survival random forest training
|
|
15
|
+
- Support for partially overlapping feature spaces
|
|
16
|
+
- Compatible with scikit-survival data structures and evaluation methods
|
|
17
|
+
- Privacy-preserving model aggregation
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
### Dependencies
|
|
23
|
+
|
|
24
|
+
federated-rsf requires:
|
|
25
|
+
- numpy (>=2.0.0)
|
|
26
|
+
- pandas (>=2.3.0)
|
|
27
|
+
- scikit-learn(>=1.8.0)
|
|
28
|
+
- scikit-survival (>=0.27.0)
|
|
29
|
+
|
|
30
|
+
### User installation
|
|
31
|
+
|
|
32
|
+
The easiest way to install federated-rsf is using pip
|
|
33
|
+
```
|
|
34
|
+
pip install -U federated-rsf
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
To install in editable mode, clone the repository and then install it using pip
|
|
38
|
+
```bash
|
|
39
|
+
git https://github.com/HauschildLab/FRSF4POD.git
|
|
40
|
+
cd FRSF4POD
|
|
41
|
+
pip install -U .
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To install in editable mode it with optional testing or development libraries uses
|
|
45
|
+
```bash
|
|
46
|
+
pip install -U -e .[dev]
|
|
47
|
+
```
|
|
48
|
+
or
|
|
49
|
+
```bash
|
|
50
|
+
pip install -U -e .[test]
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Quick Start
|
|
54
|
+
|
|
55
|
+
federated-rsf uses three main steps to train federated models.
|
|
56
|
+
- First the local data schema of all the clients has to be unified into a global schema
|
|
57
|
+
to facilitate the aggregation of models.
|
|
58
|
+
- Second is the training of the local-rsf models on the local data
|
|
59
|
+
- Third is the aggregation and distribution of the local estimators from the clients.
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
from federated_rsf.models import (
|
|
64
|
+
FederatedRandomSurvivalForest,
|
|
65
|
+
LocalRandomSurvivalForest,
|
|
66
|
+
)
|
|
67
|
+
from federated_rsf.preprocessing import SchemaAligner, SchemaCreator
|
|
68
|
+
from federated_rsf.testing import create_dummy_data, federate_data
|
|
69
|
+
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
In this example we create a dummy dataset using the testing module.
|
|
73
|
+
This module can be used valiate the federated learning pipeline is case of missing access to the actual data.
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
# Parameters
|
|
77
|
+
n_samples = 500
|
|
78
|
+
n_features = 10
|
|
79
|
+
n_clients = 5
|
|
80
|
+
random_state = 0
|
|
81
|
+
|
|
82
|
+
# Create Dummy Dataset
|
|
83
|
+
X, y = create_dummy_data(
|
|
84
|
+
n_samples,
|
|
85
|
+
n_features,
|
|
86
|
+
random_state=random_state,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
# Split Dataset samples up to all clients
|
|
90
|
+
X_list, y_list = federate_data(
|
|
91
|
+
X,
|
|
92
|
+
y,
|
|
93
|
+
n_clients,
|
|
94
|
+
drop_feature_percentage=0.33,
|
|
95
|
+
random_state=random_state,
|
|
96
|
+
)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
Next the columns of the local datasets are aligned to a global schema using the `SchemaCreator` and the local
|
|
100
|
+
```python
|
|
101
|
+
# Create global Schema
|
|
102
|
+
schema_creator = SchemaCreator(anonymize=False)
|
|
103
|
+
local_columns = [DatasetSchema(X_local.columns) for X_local in X_list]
|
|
104
|
+
dataset_schemas = schema_creator.fit_transform(local_columns)
|
|
105
|
+
|
|
106
|
+
# Align local datasets
|
|
107
|
+
X_list_aligned = []
|
|
108
|
+
|
|
109
|
+
for X_local, schema in zip(X_list, dataset_schemas):
|
|
110
|
+
schema_aligner = SchemaAligner()
|
|
111
|
+
X_aligned = schema_aligner.fit_transform(X_local, schema)
|
|
112
|
+
X_list_aligned.append(X_aligned)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The local models can then be trained on the processed local data.
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
# Train local models
|
|
119
|
+
local_models: list[LocalRandomSurvivalForest] = []
|
|
120
|
+
|
|
121
|
+
for X_local, y_local in zip(X_list_aligned, y_list):
|
|
122
|
+
local_model = LocalRandomSurvivalForest(
|
|
123
|
+
random_state=random_state,
|
|
124
|
+
)
|
|
125
|
+
local_model = local_model.fit(X_local, y_local)
|
|
126
|
+
local_models.append(local_model)
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The trained local models are then aggregated and the estimators are redistributed using the federated model.
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
# Distribute trees between local models
|
|
133
|
+
fed_model = FederatedRandomSurvivalForest(local_models=local_models)
|
|
134
|
+
fed_model.distribute_trees()
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Lastly you can compare the local and the federated model performance for example using the predict `predict_survival_function` and `predict_cumulative_hazard_function`
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
```python
|
|
141
|
+
# Example visualization of survival function and cumulative hazard function
|
|
142
|
+
client_index = 0
|
|
143
|
+
n_lines = 5
|
|
144
|
+
|
|
145
|
+
survival_local = local_models[client_index].predict_survival_function(
|
|
146
|
+
X_list_aligned[client_index]
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
hazard_local = local_models[client_index].predict_cumulative_hazard_function(
|
|
150
|
+
X_list_aligned[client_index]
|
|
151
|
+
)
|
|
152
|
+
|
|
153
|
+
local_models[client_index].use_federated_estimators()
|
|
154
|
+
|
|
155
|
+
survival_federated = local_models[client_index].predict_survival_function(
|
|
156
|
+
X_list_aligned[client_index]
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
hazard_federated = local_models[client_index].predict_cumulative_hazard_function(
|
|
160
|
+
X_list_aligned[client_index]
|
|
161
|
+
)
|
|
162
|
+
from matplotlib import pyplot as plt
|
|
163
|
+
|
|
164
|
+
for surv in [survival_local, survival_federated]:
|
|
165
|
+
for i, s in enumerate(surv[:n_lines]):
|
|
166
|
+
plt.step(s.x, s.y, where="post", label=str(i))
|
|
167
|
+
plt.ylabel("Survival probability")
|
|
168
|
+
plt.xlabel("Time in days")
|
|
169
|
+
plt.legend()
|
|
170
|
+
plt.grid(True)
|
|
171
|
+
plt.show()
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
for hazard in [hazard_local, hazard_federated]:
|
|
175
|
+
for i, s in enumerate(hazard[:n_lines]):
|
|
176
|
+
plt.step(s.x, s.y, where="post", label=str(i))
|
|
177
|
+
plt.ylabel("Cumulative hazard")
|
|
178
|
+
plt.xlabel("Time in days")
|
|
179
|
+
plt.legend()
|
|
180
|
+
plt.grid(True)
|
|
181
|
+
plt.show()
|
|
182
|
+
```
|