Source code for nvflare.app_common.statistics.numpy_utils

# Copyright (c) 2022, 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 json
from typing import List, Optional

import numpy as np
from pandas.api.types import is_bool_dtype, is_datetime64_any_dtype, is_float_dtype, is_integer_dtype

from nvflare.app_common.abstract.statistics_spec import Bin, BinRange, DataType


[docs] class NpEncoder(json.JSONEncoder):
[docs] def default(self, obj): if isinstance(obj, np.integer): return int(obj) if isinstance(obj, np.floating): return float(obj) if isinstance(obj, np.ndarray): return obj.tolist() return super(NpEncoder, self).default(obj)
[docs] def dtype_to_data_type(dtype) -> DataType: # Use pandas type-checking functions so that both numpy dtypes and pandas # nullable ExtensionDtypes (Int64Dtype, Float64Dtype, BooleanDtype, StringDtype, …) # are classified correctly. if is_float_dtype(dtype): return DataType.FLOAT elif is_bool_dtype(dtype): # is_bool must be checked before is_integer because BooleanDtype satisfies both return DataType.INT elif is_integer_dtype(dtype): return DataType.INT elif is_datetime64_any_dtype(dtype) or ( hasattr(dtype, "kind") and dtype.kind == "m" # np.timedelta64 has kind "m" ): return DataType.DATETIME else: return DataType.STRING
[docs] def get_std_histogram_buckets(nums: np.ndarray, num_bins: int = 10, br: Optional[BinRange] = None): num_posinf = len(nums[np.isposinf(nums)]) num_neginf = len(nums[np.isneginf(nums)]) if br: counts, buckets = np.histogram(nums, bins=num_bins, range=(br.min_value, br.max_value)) else: counts, buckets = np.histogram(nums, bins=num_bins) histogram_buckets: List[Bin] = [] for bucket_count in range(len(counts)): # Add any negative or positive infinities to the first and last # buckets in the histogram. bucket_low_value = buckets[bucket_count] bucket_high_value = buckets[bucket_count + 1] bucket_sample_count = counts[bucket_count] if bucket_count == 0 and num_neginf > 0: bucket_low_value = float("-inf") bucket_sample_count += num_neginf elif bucket_count == len(counts) - 1 and num_posinf > 0: bucket_high_value = float("inf") bucket_sample_count += num_posinf histogram_buckets.append( Bin(low_value=bucket_low_value, high_value=bucket_high_value, sample_count=bucket_sample_count) ) if buckets is not None and len(buckets) > 0: bucket = None if num_neginf: bucket = Bin(low_value=float("-inf"), high_value=float("-inf"), sample_count=num_neginf) if num_posinf: bucket = Bin(low_value=float("inf"), high_value=float("inf"), sample_count=num_posinf) if bucket: histogram_buckets.append(bucket) return histogram_buckets