|
| 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: 02/June/2025 |
| 20 | +# --------------------------------------------------------------------------- |
| 21 | +"""This is a collection of supplementary functions (utils) to perform various data checks""" |
| 22 | + |
| 23 | +from httomolibgpu import cupywrapper |
| 24 | +from typing import Optional |
| 25 | + |
| 26 | +cp = cupywrapper.cp |
| 27 | +cupy_run = cupywrapper.cupy_run |
| 28 | + |
| 29 | +import numpy as np |
| 30 | + |
| 31 | +from unittest.mock import Mock |
| 32 | + |
| 33 | +if cupy_run: |
| 34 | + from httomolibgpu.cuda_kernels import load_cuda_module |
| 35 | +else: |
| 36 | + load_cuda_module = Mock() |
| 37 | + |
| 38 | + |
| 39 | +def _naninfs_check( |
| 40 | + data: cp.ndarray, |
| 41 | + verbosity: bool = True, |
| 42 | + method_name: Optional[str] = None, |
| 43 | +) -> cp.ndarray: |
| 44 | + """ |
| 45 | + This function finds NaN's, +-Inf's in the input data and then prints the warnings and correct the data if correction is enabled. |
| 46 | +
|
| 47 | + Parameters |
| 48 | + ---------- |
| 49 | + data : cp.ndarray |
| 50 | + Input CuPy or Numpy array either float32 or uint16 data type. |
| 51 | + verbosity : bool |
| 52 | + If enabled, then the printing of the warning happens when data contains infs or nans |
| 53 | + method_name : str, optional. |
| 54 | + Method's name for which input data is tested. |
| 55 | +
|
| 56 | + Returns |
| 57 | + ------- |
| 58 | + ndarray |
| 59 | + Uncorrected or corrected (nans and infs converted to zeros) input array. |
| 60 | + """ |
| 61 | + present_nans_infs_b = False |
| 62 | + |
| 63 | + if cupy_run: |
| 64 | + xp = cp.get_array_module(data) |
| 65 | + else: |
| 66 | + import numpy as xp |
| 67 | + |
| 68 | + if xp.__name__ == "cupy": |
| 69 | + input_type = data.dtype |
| 70 | + if len(data.shape) == 2: |
| 71 | + dy, dx = data.shape |
| 72 | + dz = 1 |
| 73 | + else: |
| 74 | + dz, dy, dx = data.shape |
| 75 | + |
| 76 | + present_nans_infs = cp.zeros(shape=(1)).astype(cp.uint8) |
| 77 | + |
| 78 | + block_x = 128 |
| 79 | + # setting grid/block parameters |
| 80 | + block_dims = (block_x, 1, 1) |
| 81 | + grid_x = (dx + block_x - 1) // block_x |
| 82 | + grid_y = dy |
| 83 | + grid_z = dz |
| 84 | + grid_dims = (grid_x, grid_y, grid_z) |
| 85 | + params = (data, dz, dy, dx, present_nans_infs) |
| 86 | + |
| 87 | + kernel_args = "remove_nan_inf<{0}>".format( |
| 88 | + "float" if input_type == "float32" else "unsigned short" |
| 89 | + ) |
| 90 | + |
| 91 | + module = load_cuda_module("remove_nan_inf", name_expressions=[kernel_args]) |
| 92 | + remove_nan_inf_kernel = module.get_function(kernel_args) |
| 93 | + remove_nan_inf_kernel(grid_dims, block_dims, params) |
| 94 | + |
| 95 | + if present_nans_infs[0].get() == 1: |
| 96 | + present_nans_infs_b = True |
| 97 | + else: |
| 98 | + if not np.all(np.isfinite(data)): |
| 99 | + present_nans_infs_b = True |
| 100 | + np.nan_to_num(data, copy=False, nan=0.0, posinf=0.0, neginf=0.0) |
| 101 | + |
| 102 | + if present_nans_infs_b: |
| 103 | + if verbosity: |
| 104 | + print( |
| 105 | + f"Warning!!! Input data to method: {method_name} contains Inf's or/and NaN's. This will be corrected but it is recommended to check the validity of input to the method." |
| 106 | + ) |
| 107 | + |
| 108 | + return data |
| 109 | + |
| 110 | + |
| 111 | +def _zeros_check( |
| 112 | + data: cp.ndarray, |
| 113 | + verbosity: bool = True, |
| 114 | + percentage_threshold: float = 50, |
| 115 | + method_name: Optional[str] = None, |
| 116 | +) -> bool: |
| 117 | + """ |
| 118 | + This function finds all zeros present in the data. If the amount of zeros is larger than percentage_threshold it prints the warning. |
| 119 | +
|
| 120 | + Parameters |
| 121 | + ---------- |
| 122 | + data : cp.ndarray |
| 123 | + Input CuPy or Numpy array. |
| 124 | + verbosity : bool |
| 125 | + If enabled, then the printing of the warning happens when data contains infs or nans. |
| 126 | + percentage_threshold: float: |
| 127 | + If the number of zeros in input data is more than the percentage of all data points, then print the data warning |
| 128 | + method_name : str, optional. |
| 129 | + Method's name for which input data is tested. |
| 130 | +
|
| 131 | + Returns |
| 132 | + ------- |
| 133 | + bool |
| 134 | + True if the data contains too many zeros |
| 135 | + """ |
| 136 | + if cupy_run: |
| 137 | + xp = cp.get_array_module(data) |
| 138 | + else: |
| 139 | + import numpy as xp |
| 140 | + |
| 141 | + nonzero_elements_total = 1 |
| 142 | + for tot_elements_mult in data.shape: |
| 143 | + nonzero_elements_total *= tot_elements_mult |
| 144 | + |
| 145 | + warning_zeros = False |
| 146 | + zero_elements_total = nonzero_elements_total - int(xp.count_nonzero(data)) |
| 147 | + |
| 148 | + if (zero_elements_total / nonzero_elements_total) * 100 >= percentage_threshold: |
| 149 | + warning_zeros = True |
| 150 | + if verbosity: |
| 151 | + print( |
| 152 | + f"Warning!!! Input data to method: {method_name} contains more than {percentage_threshold} percent of zeros." |
| 153 | + ) |
| 154 | + |
| 155 | + return warning_zeros |
| 156 | + |
| 157 | + |
| 158 | +def data_checker( |
| 159 | + data: cp.ndarray, |
| 160 | + verbosity: bool = True, |
| 161 | + method_name: Optional[str] = None, |
| 162 | +) -> bool: |
| 163 | + """ |
| 164 | + Function that performs the variety of checks on input data, in some cases also correct the data and prints warnings. |
| 165 | + Currently it checks for: the presence of infs and nans in data; the number of zero elements. |
| 166 | +
|
| 167 | + Parameters |
| 168 | + ---------- |
| 169 | + data : xp.ndarray |
| 170 | + Input CuPy or Numpy array either float32 or uint16 data type. |
| 171 | + verbosity : bool |
| 172 | + If enabled, then the printing of the warning happens when data contains infs or nans. |
| 173 | + method_name : str, optional. |
| 174 | + Method's name for which input data is tested. |
| 175 | +
|
| 176 | + Returns |
| 177 | + ------- |
| 178 | + cp.ndarray |
| 179 | + Returns corrected or not data array. |
| 180 | + """ |
| 181 | + |
| 182 | + data = _naninfs_check(data, verbosity=verbosity, method_name=method_name) |
| 183 | + |
| 184 | + _zeros_check( |
| 185 | + data, |
| 186 | + verbosity=verbosity, |
| 187 | + percentage_threshold=50, |
| 188 | + method_name=method_name, |
| 189 | + ) |
| 190 | + |
| 191 | + return data |
0 commit comments