From 7771196fc6d2fbebdf8dc46c74e4281a2e58a792 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oriol=20Tint=C3=B3?= <oriol.tinto@lmu.de> Date: Wed, 14 Jun 2023 15:35:56 +0200 Subject: [PATCH] For the analysis, using the time average of any given metric. --- .../compression/analyzer/analyzer_utils.py | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/enstools/compression/analyzer/analyzer_utils.py b/enstools/compression/analyzer/analyzer_utils.py index dc2697b..a1c7870 100644 --- a/enstools/compression/analyzer/analyzer_utils.py +++ b/enstools/compression/analyzer/analyzer_utils.py @@ -6,6 +6,7 @@ import logging from typing import List, Dict import xarray +import numpy as np from enstools.compression.analyzer.analysis_options import AnalysisOptions from enstools.compression.metrics import DataArrayMetrics @@ -23,7 +24,8 @@ def get_metrics(reference_data: xarray.DataArray, recovered_data: xarray.DataArr :return: a dictionary with the requested metrics """ metrics = DataArrayMetrics(reference_data, recovered_data) - return {metric: float(metrics[metric]) for metric in metric_names if metric != "compression_ratio"} + # TODO: Is the average the proper thing to use here? + return {metric: float(np.average(metrics[metric])) for metric in metric_names if metric != "compression_ratio"} def check_compression_ratio(compression_ratio: float, thresholds: dict): @@ -228,6 +230,7 @@ def discrete_bisection_method(parameters_list: list, retry_repeated=5, threshold=0.1, direct_relation=True, + results=None, ): """ Apply the bisection method on a set of discrete parameters. @@ -248,6 +251,10 @@ def discrete_bisection_method(parameters_list: list, :raises: Exception if the maximum depth is reached. """ + + if results is None: + results = {} + middle_index = len(parameters_list) // 2 middle = parameters_list[middle_index] @@ -260,13 +267,20 @@ def discrete_bisection_method(parameters_list: list, middle, float(value_at_middle)) + results[middle] = value_at_middle + # If the value at the middle is positive (all thresholds are fulfilled) we can return the parameter at the middle, # otherwise select the safer one. parameter_to_return = middle if value_at_middle > 0.0 else parameters_list[-1] # In case the accuracy exit condition is reached, return the parameter value at that point - if 0.0 <= value_at_middle < threshold: - return parameter_to_return + if 0.0 <= value_at_middle < threshold or depth >= max_depth or\ + (value_at_middle == last_value and retry_repeated == 0): + positive_results = {k: v for k,v in results.items() if v > 0} + if positive_results: + return min(positive_results, key=positive_results.get) + else: + return middle # If the value is the same that the last try, we can retry few times if value_at_middle == last_value: @@ -274,9 +288,6 @@ def discrete_bisection_method(parameters_list: list, return parameter_to_return retry_repeated -= 1 - # In case having reached the maximum depth, return the proper value - if depth >= max_depth: - return parameter_to_return # # Otherwise, set new parameter range and call the function again if comparison(value_at_middle, direct_relation=direct_relation): @@ -293,4 +304,5 @@ def discrete_bisection_method(parameters_list: list, retry_repeated=retry_repeated, threshold=threshold, direct_relation=direct_relation, + results=results, ) -- GitLab