Skip to content

Commit 23395bc

Browse files
committed
brightness.from_raw_image: Compute background for features near edges
1 parent 64749e0 commit 23395bc

2 files changed

Lines changed: 16 additions & 18 deletions

File tree

sdt/brightness.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,16 @@
5050
.. autoclass:: Distribution
5151
:members:
5252
"""
53-
import warnings
5453
import math
54+
import warnings
5555

5656
import numpy as np
5757
import pandas as pd
5858
from scipy import signal
5959

60+
from . import config
6061
from .helper import numba
6162
from .image import CircleMask, RectMask
62-
from . import config
6363

6464

6565
def _make_mask_image(feat_idx, mask, shape):
@@ -277,10 +277,11 @@ def _from_raw_image_python(pos, frame, feat_mask, bg_mask, bg_estimator,
277277
if feat_region.shape != feat_mask.shape:
278278
# The signal was too close to the egde of the image, we could not
279279
# read all the pixels we wanted
280-
ret[i, :] = np.nan
281-
continue
282-
283-
feat_pixels = feat_region[feat_mask]
280+
mass_uncorr = signal_uncorr = np.nan
281+
else:
282+
feat_pixels = feat_region[feat_mask]
283+
mass_uncorr = feat_pixels.sum()
284+
signal_uncorr = feat_pixels.max()
284285

285286
if not global_bg:
286287
bg_slice = tuple(
@@ -297,8 +298,6 @@ def _from_raw_image_python(pos, frame, feat_mask, bg_mask, bg_estimator,
297298
bg = np.nan
298299
bg_std = np.nan
299300

300-
mass_uncorr = feat_pixels.sum()
301-
signal_uncorr = feat_pixels.max()
302301
if math.isfinite(bg):
303302
mass = mass_uncorr - feat_mask_ones * bg
304303
signal = signal_uncorr - bg
@@ -378,15 +377,14 @@ def _from_raw_image_numba(pos, frame, feat_mask, bg_mask, bg_estimator,
378377
if feat_pixels.size != feat_mask.size:
379378
# The signal was too close to the egde of the image, we could not
380379
# read all the pixels we wanted
381-
ret[i, :] = np.nan
382-
continue
383-
384-
f_mask_pixels = feat_mask[feat_bd[2, i, 0]:feat_bd[3, i, 0],
385-
feat_bd[2, i, 1]:feat_bd[3, i, 1]].flatten()
386-
387-
feat_pixels_masked = feat_pixels[f_mask_pixels]
388-
mass_uncorr = np.sum(feat_pixels_masked)
389-
signal_uncorr = np.max(feat_pixels_masked)
380+
mass_uncorr = signal_uncorr = np.nan
381+
else:
382+
f_mask_pixels = feat_mask[
383+
feat_bd[2, i, 0] : feat_bd[3, i, 0], feat_bd[2, i, 1] : feat_bd[3, i, 1]
384+
].flatten()
385+
feat_pixels_masked = feat_pixels[f_mask_pixels]
386+
mass_uncorr = np.sum(feat_pixels_masked)
387+
signal_uncorr = np.max(feat_pixels_masked)
390388

391389
if not global_bg:
392390
bg_pixels = np.ravel(frame[bg_bd[0, i, 0]:bg_bd[1, i, 0],

tests/test_brightness.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def test_from_raw_image_helper_nan(self):
169169
res = self.from_raw_image(
170170
np.array([[1, 1]]), self.img, self.fg_mask, self.bg_mask,
171171
self.mean_arg)
172-
np.testing.assert_equal(res, [[np.nan]*4])
172+
np.testing.assert_equal(res, [[np.nan, np.nan, self.bg_fill, 0.0]])
173173

174174
def test_from_raw_image_helper_bg_exclude(self):
175175
"""brightness._from_raw_image_python: Exclude other features for bg

0 commit comments

Comments
 (0)