Skip to content

Commit

Permalink
Compute Outliers (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc committed Mar 11, 2024
2 parents b95aebb + 4851f64 commit 1ab346e
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions mantidimaging/core/operations/outliers/outliers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import TYPE_CHECKING

import numpy as np
import scipy.ndimage as scipy_ndimage
from scipy.ndimage import median_filter

from mantidimaging.core.operations.base_filter import BaseFilter, FilterGroup
from mantidimaging.core.parallel import shared as ps
Expand Down Expand Up @@ -38,15 +38,6 @@ class OutliersFilter(BaseFilter):
filter_name = "Remove Outliers"
link_histograms = True

@staticmethod
def _execute(data, diff, radius, mode):
# Adapted from tomopy source
median = scipy_ndimage.median_filter(data, radius)
if mode == OUTLIERS_BRIGHT:
return np.where((data - median) > diff, median, data)
else:
return np.where((median - data) > diff, median, data)

@staticmethod
def filter_func(images: ImageStack,
diff=None,
Expand All @@ -68,13 +59,24 @@ def filter_func(images: ImageStack,
if not radius or not radius > 0:
raise ValueError(f'radius parameter must be greater than 0. Value provided was {radius}')

func = ps.create_partial(OutliersFilter._execute, ps.return_to_self, diff=diff, radius=radius, mode=mode)
ps.execute(func, [images.shared_array],
images.data.shape[0],
progress=progress,
msg=f"Outliers with threshold {diff} and kernel {radius}")
params = {'diff': diff, 'radius': radius, 'mode': mode}
ps.run_compute_func(OutliersFilter.compute_function, images.data.shape[0], images.shared_array, params,
progress)

return images

@staticmethod
def compute_function(i: int, array: np.ndarray, params):
diff = params['diff']
radius = params['radius']
mode = params['mode']

median = median_filter(array[i], size=radius)
if mode == OUTLIERS_BRIGHT:
array[i] = np.where((array[i] - median) > diff, median, array[i])
else:
array[i] = np.where((median - array[i]) > diff, median, array[i])

@staticmethod
def register_gui(form, on_change, view):
_, diff_field = add_property_to_form('Difference',
Expand Down

0 comments on commit 1ab346e

Please sign in to comment.