From f7f966ff67cee54aa569c2e2bf9158dc95b7448a Mon Sep 17 00:00:00 2001 From: Laurenz Kremeyer Date: Sat, 27 Aug 2022 10:31:21 -0400 Subject: [PATCH] improved masking for `bragg_peaks_persistence` (#43) * updated masking for `bragg_peaks_persistence` In the function `bragg_peaks_persistence` from @trbritt the hard edges of the mask are resulting in peaks being detected at those edges. I propose to scan the full image for peaks and throw away the peaks that are in the masked area later. Resulted in improved results on my end. `autocenter` is still done with the mask in place. * Update CHANGELOG.rst * Update indexing.py * version bump --- CHANGELOG.rst | 5 +++++ skued/__init__.py | 2 +- skued/image/indexing.py | 15 +++++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 165923f0..92ed841b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,11 @@ Changelog ========= +Release 2.1.12 +-------------- + +* Improved the masking functionality in `bragg_peaks_persistence`. + Release 2.1.11 -------------- diff --git a/skued/__init__.py b/skued/__init__.py index 09e3d909..31df6efa 100644 --- a/skued/__init__.py +++ b/skued/__init__.py @@ -2,7 +2,7 @@ __author__ = "Laurent P. René de Cotret" __email__ = "laurent.renedecotret@mail.mcgill.ca" __license__ = "GPLv3" -__version__ = "2.1.10" +__version__ = "2.1.12" from .affine import ( affine_map, diff --git a/skued/image/indexing.py b/skued/image/indexing.py index 4a1a6422..0d41e0a9 100644 --- a/skued/image/indexing.py +++ b/skued/image/indexing.py @@ -190,10 +190,8 @@ def bragg_peaks_persistence( """ if mask is None: mask = np.ones_like(im, dtype=bool) - im[~mask] = 0.0 if center is None: center = autocenter(im=im, mask=mask) - image = im g0 = Persistence(im).persistence birth_death = list() @@ -237,6 +235,19 @@ def bragg_peaks_persistence( sorted(candidates, key=lambda p: np.linalg.norm(p - center)) ).reshape(-1, 2) birth_death = np.array(birth_death).reshape(-1, 2) + + # remove peaks that are within the masked area + if mask.sum() != mask.shape[0] * mask.shape[1]: + peaks = np.array([p for p in peaks if mask[p[1], p[0]]]) + birth_death = np.array( + [bd for p, bd in zip(peaks, birth_death) if mask[p[1], p[0]]] + ) + birth_death_indices = np.array( + [bdi for p, bdi in zip(peaks, birth_death_indices) if mask[p[1], p[0]]] + ) + persistencies = np.array( + [pers for p, pers in zip(peaks, persistencies) if mask[p[1], p[0]]] + ) return peaks, birth_death, birth_death_indices, persistencies