Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content for logistic regression for DLI #3209

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
DATA_DIR=/tmp/flare/dataset/heart_disease_data

# Install dependencies
#pip install wget
FLAMBY_INSTALL_DIR=$(python3 -c "import sysconfig; print(sysconfig.get_path('purelib'))")
# git clone https://github.com/owkin/FLamby.git && cd FLamby && pip install -e .

# Download data using FLamby
mkdir -p ${DATA_DIR}
python3 ${FLAMBY_INSTALL_DIR}/flamby/datasets/fed_heart_disease/dataset_creation_scripts/download.py --output-folder ${DATA_DIR}

# Convert data to numpy files
python3 ${SCRIPT_DIR}/utils/convert_data_to_np.py ${DATA_DIR}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import argparse
import os

import numpy as np
from flamby.datasets.fed_heart_disease import FedHeartDisease
from torch.utils.data import DataLoader as dl

if __name__ == "__main__":

parser = argparse.ArgumentParser("save UCI Heart Disease as numpy arrays.")
parser.add_argument("save_dir", type=str, help="directory to save converted numpy arrays as .npy files.")
args = parser.parse_args()

if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir, exist_ok=True)

for site in range(4):

for flag in ("train", "test"):

# To load data a pytorch dataset
data = FedHeartDisease(center=site, train=(flag == "train"))

# Save training dataset
data_x = []
data_y = []
for x, y in dl(data, batch_size=1, shuffle=False, num_workers=0):
data_x.append(x.cpu().numpy().reshape(-1))
data_y.append(y.cpu().numpy().reshape(-1))

data_x = np.array(data_x).reshape(-1, 13)
data_y = np.array(data_y).reshape(-1, 1)

print("site {} - {} - variables shape: {}".format(site, flag, data_x.shape))
print("site {} - {} - outcomes shape: {}".format(site, flag, data_y.shape))

save_x_path = "{}/site-{}.{}.x.npy".format(args.save_dir, site + 1, flag)
print("saving data: {}".format(save_x_path))
np.save(save_x_path, data_x)

save_y_path = "{}/site-{}.{}.y.npy".format(args.save_dir, site + 1, flag)
print("saving data: {}".format(save_y_path))
np.save(save_y_path, data_y)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"version": 1,
"disable_existing_loggers": false,
"formatters": {
"baseFormatter": {
"()": "nvflare.fuel.utils.log_utils.BaseFormatter",
"fmt": "%(asctime)s - %(name)s - %(levelname)s - %(fl_ctx)s - %(message)s"
},
"colorFormatter": {
"()": "nvflare.fuel.utils.log_utils.ColorFormatter",
"fmt": "%(asctime)s - %(levelname)s - %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S"
},
"jsonFormatter": {
"()": "nvflare.fuel.utils.log_utils.JsonFormatter",
"fmt": "%(asctime)s - %(identity)s - %(name)s - %(fullName)s - %(levelname)s - %(fl_ctx)s - %(message)s"
}
},
"filters": {
"FLFilter": {
"()": "nvflare.fuel.utils.log_utils.LoggerNameFilter",
"logger_names": ["custom", "nvflare.app_common", "nvflare.app_opt"]
}
},
"handlers": {
"consoleHandler": {
"class": "logging.StreamHandler",
"level": "INFO",
"formatter": "colorFormatter",
"filters": ["FLFilter"],
"stream": "ext://sys.stdout"
},
"logFileHandler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "baseFormatter",
"filename": "log.txt",
"mode": "a",
"maxBytes": 20971520,
"backupCount": 10
},
"errorFileHandler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "ERROR",
"formatter": "baseFormatter",
"filename": "log_error.txt",
"mode": "a",
"maxBytes": 20971520,
"backupCount": 10
},
"jsonFileHandler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "jsonFormatter",
"filename": "log.json",
"mode": "a",
"maxBytes": 20971520,
"backupCount": 10
},
"FLFileHandler": {
"class": "logging.handlers.RotatingFileHandler",
"level": "DEBUG",
"formatter": "baseFormatter",
"filters": ["FLFilter"],
"filename": "log_fl.txt",
"mode": "a",
"maxBytes": 20971520,
"backupCount": 10,
"delay": true
}
},
"loggers": {
"root": {
"level": "INFO",
"handlers": ["consoleHandler", "logFileHandler", "errorFileHandler", "jsonFileHandler", "FLFileHandler"]
}
}
}









Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from src.newton_raphson_persistor import NewtonRaphsonModelPersistor
from src.newton_raphson_workflow import FedAvgNewtonRaphson

from nvflare.app_opt.pt.job_config.base_fed_job import BaseFedJob
from nvflare.client.config import ExchangeFormat
from nvflare.job_config.script_runner import ScriptRunner

if __name__ == "__main__":
n_clients = 4
num_rounds = 5

job = BaseFedJob(
name="logistic_regression_fedavg",
model_persistor=NewtonRaphsonModelPersistor(n_features=13),
)

controller = FedAvgNewtonRaphson(
num_clients=n_clients,
num_rounds=num_rounds,
damping_factor=0.8,
persistor_id="newton_raphson_persistor",
)
job.to(controller, "server")

# Add clients
for i in range(n_clients):
runner = ScriptRunner(
script="src/newton_raphson_train.py",
script_args="--data_root /tmp/flare/dataset/heart_disease_data",
launch_external_process=True,
params_exchange_format=ExchangeFormat.RAW,
)
job.to(runner, f"site-{i + 1}")

job.export_job("/tmp/nvflare/jobs/job_config")
job.simulator_run("/tmp/nvflare/jobs/workdir", gpu="0", log_config="./log_config.json")
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"format_version": 2,
"app_script": "newton_raphson_train.py",
"app_config": "--data_root /tmp/flare/dataset/heart_disease_data",
"executors": [
{
"tasks": [
"train"
],
"executor": {
"path": "nvflare.app_common.executors.client_api_launcher_executor.ClientAPILauncherExecutor",
"args": {
"launcher_id": "launcher",
"pipe_id": "pipe",
"heartbeat_timeout": 60,
"params_exchange_format": "raw",
"params_transfer_type": "FULL",
"train_with_evaluation": false
}
}
}
],
"task_data_filters": [],
"task_result_filters": [],
"components": [
{
"id": "launcher",
"path": "nvflare.app_common.launchers.subprocess_launcher.SubprocessLauncher",
"args": {
"script": "python3 custom/{app_script} {app_config}",
"launch_once": true
}
},
{
"id": "pipe",
"path": "nvflare.fuel.utils.pipe.cell_pipe.CellPipe",
"args": {
"mode": "PASSIVE",
"site_name": "{SITE_NAME}",
"token": "{JOB_ID}",
"root_url": "{ROOT_URL}",
"secure_mode": "{SECURE_MODE}",
"workspace_dir": "{WORKSPACE}"
}
},
{
"id": "metrics_pipe",
"path": "nvflare.fuel.utils.pipe.cell_pipe.CellPipe",
"args": {
"mode": "PASSIVE",
"site_name": "{SITE_NAME}",
"token": "{JOB_ID}",
"root_url": "{ROOT_URL}",
"secure_mode": "{SECURE_MODE}",
"workspace_dir": "{WORKSPACE}"
}
},
{
"id": "metric_relay",
"path": "nvflare.app_common.widgets.metric_relay.MetricRelay",
"args": {
"pipe_id": "metrics_pipe",
"event_type": "fed.analytix_log_stats",
"read_interval": 0.1
}
},
{
"id": "client_api_config_preparer",
"path": "nvflare.app_common.widgets.external_configurator.ExternalConfigurator",
"args": {
"component_ids": ["metric_relay"]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"format_version": 2,
"server": {
"heart_beat_timeout": 600
},
"task_data_filters": [],
"task_result_filters": [],
"components": [
{
"id": "newton_raphson_persistor",
"path": "newton_raphson_persistor.NewtonRaphsonModelPersistor",
"args": {
"n_features": 13
}
},
{
"id": "tb_analytics_receiver",
"path": "nvflare.app_opt.tracking.tb.tb_receiver.TBAnalyticsReceiver",
"args.events": ["fed.analytix_log_stats"]
}
],
"workflows": [
{
"id": "fedavg_newton_raphson",
"path": "newton_raphson_workflow.FedAvgNewtonRaphson",
"args": {
"num_clients": 4,
"num_rounds": 5,
"damping_factor": 0.8,
"persistor_id": "newton_raphson_persistor"
}
}
]
}
Loading
Loading