From eb0963bb3ab0f8391142e0878e188f1d1088592b Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Tue, 13 Aug 2024 17:19:59 +0200 Subject: [PATCH 1/3] When taking relative norm differences in similarity calculation, use absolute values to get the normalizing factor --- micro_manager/adaptivity/adaptivity.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/micro_manager/adaptivity/adaptivity.py b/micro_manager/adaptivity/adaptivity.py index a977cabd..a7343165 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 ---------- @@ -290,14 +290,19 @@ def _l1rel(self, data: np.ndarray) -> np.ndarray: # divide by data to get relative difference # divide i,j by max(data[i],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 ---------- @@ -313,6 +318,11 @@ def _l2rel(self, data: np.ndarray) -> np.ndarray: # divide by data to get relative difference # divide i,j by max(data[i],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) From 5a51826fcbb292d8ffc7b032dc36873ec213070d Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Tue, 13 Aug 2024 18:12:08 +0200 Subject: [PATCH 2/3] Fix comments with respect to earlier changes --- micro_manager/adaptivity/adaptivity.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/micro_manager/adaptivity/adaptivity.py b/micro_manager/adaptivity/adaptivity.py index a7343165..b1240505 100644 --- a/micro_manager/adaptivity/adaptivity.py +++ b/micro_manager/adaptivity/adaptivity.py @@ -288,7 +288,7 @@ 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 @@ -316,7 +316,7 @@ 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 From d33648defe83ea6e39b918c7c11a4076ced21804 Mon Sep 17 00:00:00 2001 From: Ishaan Desai Date: Wed, 14 Aug 2024 09:22:31 +0200 Subject: [PATCH 3/3] Add CHANGELOG entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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