Skip to content

Commit b54a7d1

Browse files
committed
Added a unit test for #46
1 parent 4c07a35 commit b54a7d1

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changelog
22
=========
33

4+
Release 2.1.17
5+
--------------
6+
7+
* Added a new function to automatically determine a diffraction mask, :func:`auto_masking` (#46).
8+
49
Release 2.1.16
510
--------------
611

skued/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__author__ = "Laurent P. René de Cotret"
33
__email__ = "laurent.renedecotret@mail.mcgill.ca"
44
__license__ = "GPLv3"
5-
__version__ = "2.1.16"
5+
__version__ = "2.1.17"
66

77
from .affine import (
88
affine_map,

skued/image/center.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -117,36 +117,25 @@ def autocenter(im, mask=None, normalize_bg=True):
117117
return np.array([r_, c_]) + correction / 2
118118

119119

120-
def _center_of_intensity(im, mask=None):
121-
weights = im * mask.astype(im.dtype)
122-
123-
rr, cc = np.indices(im.shape)
124-
r_ = np.average(rr, weights=weights)
125-
c_ = np.average(cc, weights=weights)
126-
return int(r_), int(c_)
127-
128-
129120
def auto_masking(im, threshold=0.1):
130121
"""
131-
Generate a mask based on the darkest fraction of an image
122+
Generate a mask based on the darkest fraction of an image.
123+
124+
.. versionadded:: 2.1.17
132125
133126
Parameters
134127
----------
135-
im : floats, ndarrays of shape (N,M)
128+
im : ndarray of shape (N,M)
136129
image used to generate a mask
137130
threshold: float, optional
138-
fraction of the lowest values to be masked, default = 15%
131+
fraction of the lowest values to be masked, default = 10%
139132
140-
Yields
141-
------
133+
Returns
134+
-------
142135
mask : boolean, ndarrays of shape (N,M)
143136
Mask that evaluates to True on valid pixels.
144-
145137
"""
146138
# Find the median of the highest intensity value of the image to avoid hot spots
147-
max_median = np.median([max(x) for x in np.real(im)])
148-
# Set the threshold value
149-
lower_limit = threshold * max_median
139+
lower_limit = threshold * np.median(np.maximum(im, 0))
150140
# generate a mask
151-
mask = im >= lower_limit
152-
return mask
141+
return im > lower_limit

skued/image/tests/test_center.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# -*- coding: utf-8 -*-
22

3-
from skued import autocenter, gaussian, kinematicsim
3+
from skued import autocenter, gaussian, kinematicsim, auto_masking
44
from crystals import Crystal
55
from scipy.ndimage import gaussian_filter
66
import numpy as np
7-
import itertools as it
87
import pytest
98

109
np.random.seed(23)
@@ -124,3 +123,14 @@ def test_autocenter_single_crystal_ewald_walkoff(rc, cc):
124123
I += 0.01 * I.max() * np.random.random(size=I.shape)
125124

126125
assert np.allclose(autocenter(I, mask=mask), (rc, cc), atol=1)
126+
127+
128+
@pytest.mark.parametrize("seed", range(10))
129+
def test_auto_masking(seed: int):
130+
im = 10 * np.ones(shape=(32, 32))
131+
np.random.seed(seed)
132+
reference_mask = np.random.randint(low=0, high=1 + 1, size=im.shape)
133+
134+
mask = auto_masking(im=im * reference_mask).astype(int)
135+
136+
assert np.allclose(reference_mask, mask)

0 commit comments

Comments
 (0)