|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# --------------------------------------------------------------------------- |
| 4 | +# Copyright 2022 Diamond Light Source Ltd. |
| 5 | +# |
| 6 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +# you may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# --------------------------------------------------------------------------- |
| 18 | +# Created By : Tomography Team at DLS <[email protected]> |
| 19 | +# Created Date: 21/October/2022 |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | +""" Module for data correction. For more detailed information see :ref:`data_correction_module`. |
| 22 | +
|
| 23 | +""" |
| 24 | + |
| 25 | +import numpy as np |
| 26 | +from typing import Union |
| 27 | + |
| 28 | +from httomolibgpu import cupywrapper |
| 29 | + |
| 30 | +cp = cupywrapper.cp |
| 31 | +cupy_run = cupywrapper.cupy_run |
| 32 | + |
| 33 | +from numpy import float32 |
| 34 | +from unittest.mock import Mock |
| 35 | + |
| 36 | +if cupy_run: |
| 37 | + from httomolibgpu.cuda_kernels import load_cuda_module |
| 38 | +else: |
| 39 | + load_cuda_module = Mock() |
| 40 | + |
| 41 | + |
| 42 | +__all__ = [ |
| 43 | + "raven_filter", |
| 44 | +] |
| 45 | + |
| 46 | + |
| 47 | +def raven_filter( |
| 48 | + data: cp.ndarray, |
| 49 | + kernel_size: int = 3, |
| 50 | + dif: float = 0.0, |
| 51 | +) -> cp.ndarray: |
| 52 | + """ |
| 53 | + Applies raven filter to a 3D CuPy array. For more detailed information, see :ref:`method_raven_filter`. |
| 54 | +
|
| 55 | + Parameters |
| 56 | + ---------- |
| 57 | + data : cp.ndarray |
| 58 | + Input CuPy 3D array either float32 or uint16 data type. |
| 59 | + kernel_size : int, optional |
| 60 | + The size of the filter's kernel (a diameter). |
| 61 | + dif : float, optional |
| 62 | + Expected difference value between outlier value and the |
| 63 | + median value of the array, leave equal to 0 for classical median. |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + ndarray |
| 68 | + Median filtered 3D CuPy array either float32 or uint16 data type. |
| 69 | +
|
| 70 | + Raises |
| 71 | + ------ |
| 72 | + ValueError |
| 73 | + If the input array is not three dimensional. |
| 74 | + """ |
| 75 | + input_type = data.dtype |
| 76 | + |
| 77 | + if input_type not in ["float32", "uint16"]: |
| 78 | + raise ValueError("The input data should be either float32 or uint16 data type") |
| 79 | + |
| 80 | + if data.ndim == 3: |
| 81 | + if 0 in data.shape: |
| 82 | + raise ValueError("The length of one of dimensions is equal to zero") |
| 83 | + else: |
| 84 | + raise ValueError("The input array must be a 3D array") |
| 85 | + |
| 86 | + if kernel_size not in [3, 5, 7, 9, 11, 13]: |
| 87 | + raise ValueError("Please select a correct kernel size: 3, 5, 7, 9, 11, 13") |
| 88 | + |
| 89 | + dz, dy, dx = data.shape |
| 90 | + output = cp.copy(data, order="C") |
| 91 | + |
| 92 | + # 3d median or dezinger |
| 93 | + kernel_args = "median_general_kernel3d<{0}, {1}>".format( |
| 94 | + "float" if input_type == "float32" else "unsigned short", kernel_size |
| 95 | + ) |
| 96 | + block_x = 128 |
| 97 | + # setting grid/block parameters |
| 98 | + block_dims = (block_x, 1, 1) |
| 99 | + grid_x = (dx + block_x - 1) // block_x |
| 100 | + grid_y = dy |
| 101 | + grid_z = dz |
| 102 | + grid_dims = (grid_x, grid_y, grid_z) |
| 103 | + params = (data, output, cp.float32(dif), dz, dy, dx) |
| 104 | + |
| 105 | + median_module = load_cuda_module("raven_filter", name_expressions=[kernel_args]) |
| 106 | + median_filt = median_module.get_function(kernel_args) |
| 107 | + |
| 108 | + median_filt(grid_dims, block_dims, params) |
| 109 | + |
| 110 | + return output |
0 commit comments