diff --git a/examples/tutorials/self-paced-training/part-1_federated_learning_introduction/chapter-2_develop_federated_learning_applications/02.4_convert_machine_learning_to_federated_learning/02.3.1_convert_logistic_regression_to_federated_learning/code/src/newton_raphson_workflow.py b/examples/tutorials/self-paced-training/part-1_federated_learning_introduction/chapter-2_develop_federated_learning_applications/02.4_convert_machine_learning_to_federated_learning/02.3.1_convert_logistic_regression_to_federated_learning/code/src/newton_raphson_workflow.py index 83cf87d4ee..52c9083f98 100644 --- a/examples/tutorials/self-paced-training/part-1_federated_learning_introduction/chapter-2_develop_federated_learning_applications/02.4_convert_machine_learning_to_federated_learning/02.3.1_convert_logistic_regression_to_federated_learning/code/src/newton_raphson_workflow.py +++ b/examples/tutorials/self-paced-training/part-1_federated_learning_introduction/chapter-2_develop_federated_learning_applications/02.4_convert_machine_learning_to_federated_learning/02.3.1_convert_logistic_regression_to_federated_learning/code/src/newton_raphson_workflow.py @@ -26,7 +26,7 @@ class FedAvgNewtonRaphson(BaseFedAvg): def __init__(self, damping_factor, epsilon=1.0, *args, **kwargs): super().__init__(*args, **kwargs) - + """ Init function for FedAvgNewtonRaphson. @@ -167,4 +167,4 @@ def update_model(self, model, model_update, replace_meta=True) -> FLModel: model.metrics = model_update.metrics # model.params[NPConstants.NUMPY_KEY] += model_update.params["newton_raphson_updates"] - model.params["weights"] += model_update.params["newton_raphson_updates"] \ No newline at end of file + model.params["weights"] += model_update.params["newton_raphson_updates"] diff --git a/nvflare/app_common/statistics/numeric_stats.py b/nvflare/app_common/statistics/numeric_stats.py index 8e2c1f3324..a8bccc8b70 100644 --- a/nvflare/app_common/statistics/numeric_stats.py +++ b/nvflare/app_common/statistics/numeric_stats.py @@ -19,7 +19,6 @@ from nvflare.app_common.app_constant import StatisticsConstants as StC from nvflare.app_opt.statistics.quantile_stats import get_quantiles from nvflare.fuel.utils.log_utils import get_module_logger - T = TypeVar("T") @@ -49,7 +48,6 @@ def get_global_stats( ordered_target_metrics = StC.ordered_statistics[metric_task] ordered_metrics = [metric for metric in ordered_target_metrics if metric in client_metrics] - for metric in ordered_metrics: if metric not in global_metrics: global_metrics[metric] = {} diff --git a/nvflare/app_opt/statistics/df/df_core_statistics.py b/nvflare/app_opt/statistics/df/df_core_statistics.py index 3da031b8c7..ad0c0ae4d0 100644 --- a/nvflare/app_opt/statistics/df/df_core_statistics.py +++ b/nvflare/app_opt/statistics/df/df_core_statistics.py @@ -100,7 +100,7 @@ def quantiles(self, dataset_name: str, feature_name: str, percents: List) -> Dic if not flag: results[StatisticsConstants.STATS_QUANTILE] = {} return results - + df = self.data[dataset_name] data = df[feature_name] max_bin = self.max_bin if self.max_bin else round(sqrt(len(data))) @@ -116,4 +116,3 @@ def quantiles(self, dataset_name: str, feature_name: str, percents: List) -> Dic # Extract the Q-Digest into a dictionary results[StatisticsConstants.STATS_DIGEST_COORD] = digest.to_dict() return results - \ No newline at end of file diff --git a/nvflare/app_opt/statistics/quantile_stats.py b/nvflare/app_opt/statistics/quantile_stats.py index 4ffa2c17e6..1daef0e6b7 100644 --- a/nvflare/app_opt/statistics/quantile_stats.py +++ b/nvflare/app_opt/statistics/quantile_stats.py @@ -13,12 +13,13 @@ # limitations under the License. from typing import Dict + from nvflare.app_common.app_constant import StatisticsConstants as StC from nvflare.fuel.utils.log_utils import get_module_logger - try: from fastdigest import TDigest + TDIGEST_AVAILABLE = True except ImportError: TDIGEST_AVAILABLE = False @@ -28,7 +29,7 @@ def get_quantiles(stats: Dict, statistic_configs: Dict, precision: int): - + logger.info(f"get_quantiles: stats: {TDIGEST_AVAILABLE=}") if not TDIGEST_AVAILABLE: @@ -36,7 +37,7 @@ def get_quantiles(stats: Dict, statistic_configs: Dict, precision: int): global_digest = {} for client_name in stats: - global_digest = merge_quantiles(stats[client_name],global_digest) + global_digest = merge_quantiles(stats[client_name], global_digest) quantile_config = statistic_configs.get(StC.STATS_QUANTILE) return compute_quantiles(global_digest, quantile_config, precision) @@ -87,7 +88,7 @@ def compute_quantiles(g_digest: dict, quantile_config: Dict, precision: int) -> feature_metrics = g_digest[ds_name] for feature_name in feature_metrics: digest = feature_metrics[feature_name] - percentiles = get_target_quantiles(quantile_config,feature_name) + percentiles = get_target_quantiles(quantile_config, feature_name) quantile_values = {} for percentile in percentiles: quantile_values[percentile] = round(digest.quantile(percentile), precision) @@ -95,4 +96,3 @@ def compute_quantiles(g_digest: dict, quantile_config: Dict, precision: int) -> g_ds_metrics[ds_name][feature_name] = quantile_values return g_ds_metrics - diff --git a/tests/unit_test/app_common/statistics/quantile_test.py b/tests/unit_test/app_common/statistics/quantile_test.py index 8ae125f942..38a85fe964 100644 --- a/tests/unit_test/app_common/statistics/quantile_test.py +++ b/tests/unit_test/app_common/statistics/quantile_test.py @@ -25,6 +25,7 @@ try: from fastdigest import TDigest + TDIGEST_AVAILABLE = True except ImportError: TDIGEST_AVAILABLE = False