Skip to content

Commit

Permalink
Clip value compute (#2091)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtygier-stfc committed Mar 8, 2024
2 parents d82757f + 9b4a6d6 commit 94ba17c
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions mantidimaging/core/operations/clip_values/clip_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
from __future__ import annotations

from functools import partial
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, Any

import numpy as np

from mantidimaging.core.operations.base_filter import BaseFilter
from mantidimaging.core.utility.progress_reporting import Progress
from mantidimaging.core.parallel import shared as ps

if TYPE_CHECKING:
from mantidimaging.core.data import ImageStack
Expand Down Expand Up @@ -55,27 +57,30 @@ def filter_func(data,
# We're using is None because 0.0 is a valid value
if clip_min is None and clip_max is None:
raise ValueError('At least one of clip_min or clip_max must be supplied')
params = {
'clip_min': clip_min,
'clip_max': clip_max,
'clip_min_new_value': clip_min_new_value,
'clip_max_new_value': clip_max_new_value
}

progress = Progress.ensure_instance(progress, num_steps=2, task_name='Clipping Values.')
with progress:
sample = data.data
progress.update(msg="Determining clip min and clip max")
clip_min = clip_min if clip_min is not None else sample.min()
clip_max = clip_max if clip_max is not None else sample.max()

clip_min_new_value = clip_min_new_value if clip_min_new_value is not None else clip_min

clip_max_new_value = clip_max_new_value if clip_max_new_value is not None else clip_max

progress.update(msg=f"Clipping data with values min {clip_min} and max {clip_max}")

# this is the fastest way to clip the values, np.clip does not do
# the clipping in place and ends up copying the data
sample[sample < clip_min] = clip_min_new_value
sample[sample > clip_max] = clip_max_new_value
ps.run_compute_func(ClipValuesFilter.compute_function, data.data.shape[0], [data.shared_array], params,
progress)

return data

@staticmethod
def compute_function(i: int, array: np.ndarray, params: Dict[str, Any]):
slice = array[i]
clip_min = params['clip_min'] if params['clip_min'] is not None else slice.min()
clip_max = params['clip_max'] if params['clip_max'] is not None else slice.max()
clip_min_new_value = params['clip_min_new_value'] if params['clip_min_new_value'] is not None else clip_min
clip_max_new_value = params['clip_max_new_value'] if params['clip_max_new_value'] is not None else clip_max

# Clip the values
slice[slice < clip_min] = clip_min_new_value
slice[slice > clip_max] = clip_max_new_value

@staticmethod
def register_gui(form, on_change, view):
from mantidimaging.gui.utility import add_property_to_form
Expand Down

0 comments on commit 94ba17c

Please sign in to comment.