From 1d3169e4aefed21bbdc1b3dc206e1afdd2d7280e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yuan-Ting=20Hsieh=20=28=E8=AC=9D=E6=B2=85=E5=BB=B7=29?= Date: Fri, 18 Mar 2022 15:21:18 -0700 Subject: [PATCH] Add event_type to AnalyticsSender (#330) --- .../config/config_fed_client.json | 2 +- .../hello-pt-tb/config/config_fed_client.json | 2 +- nvflare/app_common/app_event_type.py | 3 --- nvflare/app_common/widgets/streaming.py | 22 +++++++++++-------- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/examples/cifar10/configs/cifar10_fedavg_stream_tb/config/config_fed_client.json b/examples/cifar10/configs/cifar10_fedavg_stream_tb/config/config_fed_client.json index 828c5d3fa17..bc7a7724c8a 100644 --- a/examples/cifar10/configs/cifar10_fedavg_stream_tb/config/config_fed_client.json +++ b/examples/cifar10/configs/cifar10_fedavg_stream_tb/config/config_fed_client.json @@ -36,7 +36,7 @@ { "id": "analytic_sender", "name": "AnalyticsSender", - "args": {} + "args": {"event_type": "analytix_log_stats"} }, { "id": "tb_analytics_receive", diff --git a/examples/hello-pt-tb/config/config_fed_client.json b/examples/hello-pt-tb/config/config_fed_client.json index be41fcb3916..42c01f3214f 100644 --- a/examples/hello-pt-tb/config/config_fed_client.json +++ b/examples/hello-pt-tb/config/config_fed_client.json @@ -34,7 +34,7 @@ { "id": "analytic_sender", "name": "AnalyticsSender", - "args": {} + "args": {"event_type": "analytix_log_stats"} }, { "id": "event_to_fed", diff --git a/nvflare/app_common/app_event_type.py b/nvflare/app_common/app_event_type.py index 8ace359bcb0..68072b9c392 100644 --- a/nvflare/app_common/app_event_type.py +++ b/nvflare/app_common/app_event_type.py @@ -66,6 +66,3 @@ class AppEventType(object): CROSS_VAL_INIT = "_cross_val_init" VALIDATION_RESULT_RECEIVED = "_validation_result_received" RECEIVE_BEST_MODEL = "_receive_best_model" - - ANALYTIC_EVENT_TYPE = "analytix_log_stats" - LOGGING_EVENT_TYPE = "analytix_logging" diff --git a/nvflare/app_common/widgets/streaming.py b/nvflare/app_common/widgets/streaming.py index b2896a40d00..c9ad0479120 100644 --- a/nvflare/app_common/widgets/streaming.py +++ b/nvflare/app_common/widgets/streaming.py @@ -23,13 +23,12 @@ from nvflare.apis.fl_constant import EventScope, FLContextKey, ReservedKey from nvflare.apis.fl_context import FLContext from nvflare.apis.shareable import Shareable -from nvflare.app_common.app_event_type import AppEventType from nvflare.widgets.widget import Widget +ANALYTIC_EVENT_TYPE = "analytix_log_stats" -def send_analytic_dxo( - comp: FLComponent, dxo: DXO, fl_ctx: FLContext, event_type: str = AppEventType.ANALYTIC_EVENT_TYPE -): + +def send_analytic_dxo(comp: FLComponent, dxo: DXO, fl_ctx: FLContext, event_type: str = ANALYTIC_EVENT_TYPE): """Sends analytic dxo. Args: @@ -67,14 +66,19 @@ def create_analytic_dxo(tag: str, value, data_type: AnalyticsDataType, **kwargs) class AnalyticsSender(Widget): - def __init__(self): + def __init__(self, event_type=ANALYTIC_EVENT_TYPE): """Sends analytics data. - This class implements some common methods follows signatures from PyTorch SummaryWriter. - It provides a convenient way for Learner to use. + Note:: + This class implements some common methods follows signatures from PyTorch SummaryWriter. + It provides a convenient way for Learner to use. + + Args: + event_type (str): event type to fire. """ super().__init__() self.engine = None + self.event_type = event_type def handle_event(self, event_type: str, fl_ctx: FLContext): if event_type == EventType.START_RUN: @@ -95,7 +99,7 @@ def _add( kwargs["global_step"] = global_step dxo = create_analytic_dxo(tag=tag, value=value, data_type=data_type, **kwargs) with self.engine.new_context() as fl_ctx: - send_analytic_dxo(self, dxo=dxo, fl_ctx=fl_ctx) + send_analytic_dxo(self, dxo=dxo, fl_ctx=fl_ctx, event_type=self.event_type) def add_scalar(self, tag: str, scalar: float, global_step: Optional[int] = None, **kwargs): """Sends a scalar. @@ -163,7 +167,7 @@ def __init__(self, events: Optional[List[str]] = None): """ super().__init__() if events is None: - events = [AppEventType.ANALYTIC_EVENT_TYPE, f"fed.{AppEventType.ANALYTIC_EVENT_TYPE}"] + events = [ANALYTIC_EVENT_TYPE, f"fed.{ANALYTIC_EVENT_TYPE}"] self.events = events self._save_lock = Lock() self._end = False