From 490ae43f7903d6d9a0dffe1a5c9c668b2a9a277f Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Wed, 14 Aug 2024 10:13:55 +0200 Subject: [PATCH] Use absolute values to calculate normalizing factor for relative norms in adaptivity (#125) * When taking relative norm differences in similarity calculation, use absolute values to get the normalizing factor * Fix comments with respect to earlier changes * Add CHANGELOG entry --- CHANGELOG.md | 1 + micro_manager/adaptivity/adaptivity.py | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e23d13ec..4c464ce2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## latest +- Use absolute values to calculate normalizing factor for relative norms in adaptivity https://github.com/precice/micro-manager/pull/125 - Add option to use only one micro simulation object in the snapshot computation https://github.com/precice/micro-manager/pull/123 - Explicitly check if time window has converged using the API function `is_time_window_complete()` https://github.com/precice/micro-manager/pull/118 - Add `MicroManagerSnapshot` enabling snapshot computation and storage of microdata in HDF5 format https://github.com/precice/micro-manager/pull/101 diff --git a/micro_manager/adaptivity/adaptivity.py b/micro_manager/adaptivity/adaptivity.py index a977cabd..b1240505 100644 --- a/micro_manager/adaptivity/adaptivity.py +++ b/micro_manager/adaptivity/adaptivity.py @@ -61,7 +61,7 @@ def _get_similarity_dists( for name in data.keys(): data_vals = data[name] if data_vals.ndim == 1: - # If the adaptivity-data is a scalar for each simulation, + # If the adaptivity data is a scalar for each simulation, # expand the dimension to make it a 2D array to unify the calculation. # The axis is later reduced with a norm. data_vals = np.expand_dims(data_vals, axis=1) @@ -274,7 +274,7 @@ def _l2(self, data: np.ndarray) -> np.ndarray: def _l1rel(self, data: np.ndarray) -> np.ndarray: """ Calculate L1 norm of relative difference of data. - The relative difference is calculated by dividing the difference of two data points by the maximum of the two data points. + The relative difference is calculated by dividing the difference of two data points by the maximum of the absolute value of the two data points. Parameters ---------- @@ -288,16 +288,21 @@ def _l1rel(self, data: np.ndarray) -> np.ndarray: """ pointwise_diff = data[np.newaxis, :] - data[:, np.newaxis] # divide by data to get relative difference - # divide i,j by max(data[i],data[j]) to get relative difference + # divide i,j by max(abs(data[i]),abs(data[j])) to get relative difference relative = np.nan_to_num( - (pointwise_diff / np.maximum(data[np.newaxis, :], data[:, np.newaxis])) + ( + pointwise_diff + / np.maximum( + np.absolute(data[np.newaxis, :]), np.absolute(data[:, np.newaxis]) + ) + ) ) return np.linalg.norm(relative, ord=1, axis=-1) def _l2rel(self, data: np.ndarray) -> np.ndarray: """ Calculate L2 norm of relative difference of data. - The relative difference is calculated by dividing the difference of two data points by the maximum of the two data points. + The relative difference is calculated by dividing the difference of two data points by the maximum of the absolute value of the two data points. Parameters ---------- @@ -311,8 +316,13 @@ def _l2rel(self, data: np.ndarray) -> np.ndarray: """ pointwise_diff = data[np.newaxis, :] - data[:, np.newaxis] # divide by data to get relative difference - # divide i,j by max(data[i],data[j]) to get relative difference + # divide i,j by max(abs(data[i]),abs(data[j])) to get relative difference relative = np.nan_to_num( - (pointwise_diff / np.maximum(data[np.newaxis, :], data[:, np.newaxis])) + ( + pointwise_diff + / np.maximum( + np.absolute(data[np.newaxis, :]), np.absolute(data[:, np.newaxis]) + ) + ) ) return np.linalg.norm(relative, ord=2, axis=-1)