Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute operation Rebin #2127

Merged
merged 1 commit into from
Mar 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 26 additions & 29 deletions mantidimaging/core/operations/rebin/rebin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from __future__ import annotations

from functools import partial
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, List

import skimage.transform
import numpy as np
from skimage.transform import resize

from mantidimaging import helper as h
from mantidimaging.core.operations.base_filter import BaseFilter
from mantidimaging.core.parallel import shared as ps
from mantidimaging.core.parallel import utility as pu
from mantidimaging.core.parallel import utility as pu, shared as ps
from mantidimaging.gui.utility import add_property_to_form
from mantidimaging.gui.utility.qt_helpers import Type

Expand Down Expand Up @@ -44,31 +43,30 @@ def filter_func(images: ImageStack, rebin_param=0.5, mode=None, progress=None) -

:return: The processed 3D numpy.ndarray
"""
h.check_data_stack(images)

if isinstance(rebin_param, tuple):
param_valid = rebin_param[0] > 0 and rebin_param[1] > 0
new_shape = rebin_param
elif isinstance(rebin_param, (int, float)):
current_shape = images.data.shape[1:]
new_shape = (int(current_shape[0] * rebin_param), int(current_shape[1] * rebin_param))
else:
param_valid = rebin_param > 0

if not param_valid:
raise ValueError('Rebin parameter must be greater than 0')
raise ValueError("Invalid type for rebin_param")

empty_resized_data = _create_reshaped_array(images, rebin_param)

f = ps.create_partial(skimage.transform.resize,
ps.return_to_second_at_i,
mode=mode,
output_shape=empty_resized_data.array.shape[1:])
ps.execute(partial_func=f,
arrays=[images.shared_array, empty_resized_data],
num_operations=images.data.shape[0],
msg="Applying Rebin",
progress=progress)
images.shared_array = empty_resized_data
output = _create_reshaped_array(images, rebin_param)

params = {'new_shape': new_shape, 'mode': mode}
ps.run_compute_func(RebinFilter.compute_function, images.data.shape[0], [images.shared_array, output], params,
progress)
images.shared_array = output
return images

@staticmethod
def compute_function(i: int, arrays: List[np.ndarray], params: dict):
array = arrays[0]
output = arrays[1]
new_shape = params['new_shape']
mode = params['mode']
output[i] = resize(array[i], output_shape=new_shape, mode=mode, preserve_range=True)

@staticmethod
def register_gui(form, on_change, view):
# Rebin by uniform factor options
Expand Down Expand Up @@ -148,10 +146,6 @@ def execute_wrapper(rebin_to_dimensions_radio=None,
return partial(RebinFilter.filter_func, mode=mode_field.currentText(), rebin_param=params)


def modes():
return ["constant", "edge", "wrap", "reflect", "symmetric"]


def _create_reshaped_array(images, rebin_param):
old_shape = images.data.shape
num_images = old_shape[0]
Expand All @@ -165,6 +159,9 @@ def _create_reshaped_array(images, rebin_param):
expected_dimy = int(rebin_param * old_shape[1])
expected_dimx = int(rebin_param * old_shape[2])

# allocate memory for images with new dimensions
shape = (num_images, expected_dimy, expected_dimx)
return pu.create_array(shape, images.dtype)


def modes():
return ["constant", "edge", "wrap", "reflect", "symmetric"]
Loading