Skip to content

Commit

Permalink
Clean up main file for starting/launching
Browse files Browse the repository at this point in the history
  • Loading branch information
JMGaljaard committed Sep 4, 2022
1 parent 5ab8071 commit 2f33590
Showing 1 changed file with 43 additions and 31 deletions.
74 changes: 43 additions & 31 deletions fltk/__main__.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import argparse
from argparse import Namespace, ArgumentParser
import logging
from pathlib import Path
from typing import Optional, Any, Dict

import sys

from fltk.launch import launch_extractor, launch_client, launch_single, \
launch_remote, launch_cluster, launch_signature
from fltk.launch import launch_extractor, launch_client, launch_single, launch_remote, launch_cluster, launch_signature
from fltk.util.config import get_distributed_config
from fltk.util.config.arguments import create_all_subparsers
from fltk.util.generate_experiments import generate, run

__run_op_dict: Dict[str, launch_signature] = {
'util-generate': generate,
'util-run': run,
'remote': launch_remote,
'single': launch_single,
'cluster': launch_cluster,
'client': launch_client,
'extractor': launch_extractor
'remote': launch_remote, # Federated experiment (cluster)
'single': launch_single, # Federated experiment (locally_
'cluster': launch_cluster, # Cluster orchestrator
'client': launch_client, # Distributed client
'extractor': launch_extractor # Extractor (local)
}


def _save_get(args, param) -> Optional[Any]:
def _save_get(args: Namespace, param: str) -> Optional[Any]:
"""
Helper function to retrieve parameters from argument namespace.
@param args: Arguments passed from the commandline.
@type args: Namespace
@param param: Parameter to (safely) retrieve from the passed arguments.
@type param: str
@return: Value that was passed from the CLI if it was provided.
@rtype: Optional[Any]
"""
save_argument = None
if args is not None and hasattr(args, param):
save_argument = args.__dict__[param]
Expand All @@ -31,42 +37,45 @@ def _save_get(args, param) -> Optional[Any]:
return save_argument


def __main__():
# noinspection PyBroadException
def main():
"""
Main loop to perform learning (either Federated or Distributed). Note that Orchestrator is part of this setup for
now.
@return: Nothing.
Main loop to perform learning (either Federated or Distributed). Note that Orchestrator is part
of this setup for a unified startup. A future revision may extract the Orchestrator.
@return: None.
@rtype: None
"""
parser = argparse.ArgumentParser(prog='fltk',
description='Experiment launcher for the Federated Learning Testbed (fltk)')
parser = ArgumentParser(prog='fltk',
description='Launcher for the Federated Learning Testbed (fltk)')
subparsers = parser.add_subparsers(dest="action", required=True)
create_all_subparsers(subparsers)
# To create your own parser mirror the construction in the 'client_parser' object.
# Or refer to the ArgumentParser library documentation.
args = parser.parse_args()
distributed_config = get_distributed_config(args)

# Docker based launches rely on different arguments, prepare the placeholder values for a
# unified argument list.
arg_path, conf_path = None, None

try:
arg_path = Path(args.path)
except Exception as _: # pylint: disable=broad-except
except Exception as _:
print('No argument path is provided.')
try:
conf_path = Path(args.config)
except Exception as _: # pylint: disable=broad-except
except Exception as _:
print('No configuration path is provided.')

launch_fn: launch_signature = __run_op_dict[args.action]
launch_fn(arg_path, conf_path,
_save_get(args, 'rank'),
_save_get(args, 'nic'),
_save_get(args, 'host'),
_save_get(args, 'prefix'),
args,
distributed_config)
try:
pass
launch_fn(arg_path, conf_path,
_save_get(args, 'rank'),
_save_get(args, 'nic'),
_save_get(args, 'host'),
_save_get(args, 'prefix'),
args,
distributed_config)
except Exception as e:
print(f"Failed with reason: {e}")
parser.print_help()
Expand All @@ -75,10 +84,13 @@ def __main__():


if __name__ == "__main__":
# Get loger and set format for logging before starting the main loop.
# Get logger and set format for logging before starting the main loop.
root = logging.getLogger()
if root.handlers:
for handler in root.handlers:
root.removeHandler(handler)
logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d-%Y %H:%M:%S', )
__main__()
# noinspection SpellCheckingInspection
logging.basicConfig(format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
datefmt='%m-%d-%Y %H:%M:%S')

main()

0 comments on commit 2f33590

Please sign in to comment.