Skip to content

Commit

Permalink
Refactor helper module
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Hellander committed Jan 24, 2024
1 parent 29212a6 commit 6a8396d
Show file tree
Hide file tree
Showing 14 changed files with 27 additions and 30 deletions.
4 changes: 2 additions & 2 deletions docs/fedn.utils.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ fedn.utils.dispatcher module
:undoc-members:
:show-inheritance:

fedn.utils.helpers module
fedn.utils.helpers.helpers module
-------------------------

.. automodule:: fedn.utils.helpers
.. automodule:: fedn.utils.helpers.helpers
:members:
:undoc-members:
:show-inheritance:
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ A *entrypoint.py* example can look like this:
import fire
import torch
from fedn.utils.helpers import get_helper, save_metadata, save_metrics
from fedn.utils.helpers.helpers import get_helper, save_metadata, save_metrics
HELPER_MODULE = 'pytorchhelper'
NUM_CLASSES = 10
Expand Down Expand Up @@ -298,7 +298,7 @@ For validations it is a requirement that the output is saved in a valid json for
python entrypoint.py validate in_model_path out_json_path <extra-args>
In the code example we use the helper function :py:meth:`fedn.utils.helpers.save_metrics` to save the validation scores as a json file.
In the code example we use the helper function :py:meth:`fedn.utils.helpers.helpers.save_metrics` to save the validation scores as a json file.

The Dahboard in the FEDn UI will plot any scalar metric in this json file, but you can include any type in the file assuming that it is valid json. These values can then be obtained (by an athorized user) from the MongoDB database or using the :py:mod:`fedn.network.api.client`.

Expand Down
2 changes: 1 addition & 1 deletion examples/mnist-keras/client/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fire
import numpy as np
import tensorflow as tf

from fedn.utils.helpers import get_helper, save_metadata, save_metrics
from fedn.utils.helpers.helpers import get_helper, save_metadata, save_metrics

HELPER_MODULE = 'numpyhelper'
helper = get_helper(HELPER_MODULE)
Expand Down
2 changes: 1 addition & 1 deletion examples/mnist-pytorch/client/entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import docker
import fire
import torch

from fedn.utils.helpers import get_helper, save_metadata, save_metrics
from fedn.utils.helpers.helpers import get_helper, save_metadata, save_metrics

HELPER_MODULE = 'numpyhelper'
helper = get_helper(HELPER_MODULE)
Expand Down
14 changes: 5 additions & 9 deletions examples/notebooks/API_Example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 7,
"id": "5107f6f9",
"metadata": {},
"outputs": [],
"source": [
"client.set_package('../mnist-pytorch/package.tgz', 'pytorchhelper')\n",
"client.set_package('../mnist-pytorch/package.tgz', 'numpyhelper')\n",
"client.set_initial_model('../mnist-pytorch/seed.npz')\n",
"seed_model = client.get_initial_model()"
]
Expand All @@ -76,7 +76,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 8,
"id": "f0380d35",
"metadata": {},
"outputs": [],
Expand All @@ -94,7 +94,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 9,
"id": "4f70d7d9",
"metadata": {},
"outputs": [],
Expand All @@ -104,7 +104,7 @@
" \"session_id\": \"experiment_fedopt\",\n",
" \"aggregator\": \"fedopt\",\n",
" \"model_id\": seed_model['model_id'],\n",
" \"rounds\": 10\n",
" \"rounds\": 1\n",
" }\n",
"\n",
"result_fedopt = client.start_session(**session_config_fedopt)"
Expand Down Expand Up @@ -133,9 +133,7 @@
"acc = collections.OrderedDict()\n",
"for model in models[\"result\"]:\n",
" model_id = model[\"model\"]\n",
" #print(model_id)\n",
" validations = client.list_validations(modelId=model_id)\n",
" #print(validations.items(),'\\n\\n')\n",
"\n",
" for _ , validation in validations.items(): \n",
" metrics = json.loads(validation['data'])\n",
Expand Down Expand Up @@ -163,9 +161,7 @@
"acc = collections.OrderedDict()\n",
"for model in models[\"result\"]:\n",
" model_id = model[\"model\"]\n",
" #print(model_id)\n",
" validations = client.list_validations(modelId=model_id)\n",
" #print(validations.items(),'\\n\\n')\n",
" for _ , validation in validations.items(): \n",
" metrics = json.loads(validation['data'])\n",
" try:\n",
Expand Down
2 changes: 1 addition & 1 deletion fedn/fedn/network/clients/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from fedn.network.clients.package import PackageRuntime
from fedn.network.clients.state import ClientState, ClientStateToString
from fedn.utils.dispatcher import Dispatcher
from fedn.utils.helpers import get_helper
from fedn.utils.helpers.helpers import get_helper

CHUNK_SIZE = 1024 * 1024
VALID_NAME_REGEX = '^[a-zA-Z0-9_-]*$'
Expand Down
4 changes: 2 additions & 2 deletions fedn/fedn/network/combiner/aggregators/fedavg.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def combine_models(self, helper=None, delete_models=True):
"""Aggregate all model updates in the queue by computing an incremental
weighted average of model parameters.
:param helper: An instance of :class: `fedn.utils.helpers.HelperBase`, ML framework specific helper, defaults to None
:type helper: class: `fedn.utils.helpers.HelperBase`, optional
:param helper: An instance of :class: `fedn.utils.helpers.helpers.HelperBase`, ML framework specific helper, defaults to None
:type helper: class: `fedn.utils.helpers.helpers.HelperBase`, optional
:param time_window: The time window for model aggregation, defaults to 180
:type time_window: int, optional
:param max_nr_models: The maximum number of updates aggregated, defaults to 100
Expand Down
4 changes: 2 additions & 2 deletions fedn/fedn/network/combiner/aggregators/fedopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def __init__(self, storage, server, modelservice, control):
def combine_models(self, helper=None, delete_models=True):
"""Compute pseudo gradients usigng model updates in the queue.
:param helper: An instance of :class: `fedn.utils.helpers.HelperBase`, ML framework specific helper, defaults to None
:type helper: class: `fedn.utils.helpers.HelperBase`, optional
:param helper: An instance of :class: `fedn.utils.helpers.helpers.HelperBase`, ML framework specific helper, defaults to None
:type helper: class: `fedn.utils.helpers.helpers.HelperBase`, optional
:param time_window: The time window for model aggregation, defaults to 180
:type time_window: int, optional
:param max_nr_models: The maximum number of updates aggregated, defaults to 100
Expand Down
6 changes: 3 additions & 3 deletions fedn/fedn/network/combiner/round.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from fedn.common.log_config import logger
from fedn.network.combiner.aggregators.aggregatorbase import get_aggregator
from fedn.utils.helpers import get_helper
from fedn.utils.helpers.helpers import get_helper


class ModelUpdateError(Exception):
Expand Down Expand Up @@ -60,8 +60,8 @@ def push_round_config(self, round_config):
def load_model_update(self, helper, model_id):
"""Load model update in its native format.
:param helper: An instance of :class: `fedn.utils.helpers.HelperBase`, ML framework specific helper, defaults to None
:type helper: class: `fedn.utils.helpers.HelperBase`
:param helper: An instance of :class: `fedn.utils.helpers.helpers.HelperBase`, ML framework specific helper, defaults to None
:type helper: class: `fedn.utils.helpers.helpers.HelperBase`
:param model_id: The ID of the model update, UUID in str format
:type model_id: str
"""
Expand Down
4 changes: 2 additions & 2 deletions fedn/fedn/network/controller/controlbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC, abstractmethod
from time import sleep

import fedn.utils.helpers
import fedn.utils.helpers.helpers
from fedn.network.api.network import Network
from fedn.network.combiner.interfaces import CombinerUnavailableError
from fedn.network.state import ReducerState
Expand Down Expand Up @@ -98,7 +98,7 @@ def get_helper(self):
:rtype: :class:`fedn.utils.plugins.helperbase.HelperBase`
"""
helper_type = self.statestore.get_helper()
helper = fedn.utils.helpers.get_helper(helper_type)
helper = fedn.utils.helpers.helpers.get_helper(helper_type)
if not helper:
raise MisconfiguredHelper(
"Unsupported helper type {}, please configure compute_package.helper !".format(
Expand Down
4 changes: 2 additions & 2 deletions fedn/fedn/utils/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" The plugins package is responsible for loading model helper functions supporting different ML frameworks. The :class:`fedn.utils.plugins.helperbase.HelperBase` is
an abstract class which user can implement their own helper functions to support different ML frameworks. """
""" The helpers package is responsible for serialization/deserialization and numerics operations
for various machine learning frameworks. """
# flake8: noqa
2 changes: 1 addition & 1 deletion fedn/fedn/utils/helpers/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def get_helper(helper_module_name):
:param helper_module_name: The name of the helper plugin module.
:type helper_module_name: str
:return: A helper instance.
:rtype: class: `fedn.utils.helpers.HelperBase`
:rtype: class: `fedn.utils.helpers.helpers.HelperBase`
"""
helper_plugin = HELPER_PLUGIN_PATH.format(helper_module_name)
helper = importlib.import_module(helper_plugin)
Expand Down
3 changes: 2 additions & 1 deletion fedn/fedn/utils/helpers/tests/test_numpyhelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

import numpy as np

from fedn.utils.helpers.plugins.numpyhelper import Helper as NumpyHelper
from fedn.utils.helpers.helpers.plugins.numpyhelper import \
Helper as NumpyHelper


class TestNumpyHelper(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion fedn/fedn/utils/tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import unittest

from fedn.utils.helpers import get_helper, save_metadata, save_metrics
from fedn.utils.helpers.helpers import get_helper, save_metadata, save_metrics


class TestHelpers(unittest.TestCase):
Expand Down

0 comments on commit 6a8396d

Please sign in to comment.