|
| 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 | + |
| 30 | +def _naninfs_check( |
| 31 | + data: cp.ndarray, |
| 32 | + correction: bool = True, |
| 33 | + verbosity: bool = True, |
| 34 | + method_name: Optional[str] = None, |
| 35 | +) -> cp.ndarray: |
| 36 | + """ |
| 37 | + Function finds NaN's, +-Inf's in the input data and then prints the warning and correct the data |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + data : cp.ndarray |
| 42 | + Input CuPy array either float32 or uint16 data type. |
| 43 | + correction : bool |
| 44 | + If correction is enabled then Inf's and NaN's will be replaced by zeros. |
| 45 | + verbosity : bool |
| 46 | + If enabled, then the printing of the warning happens when data contains infs or nans |
| 47 | + method_name : str, optional. |
| 48 | + Method's name for which input data is tested. |
| 49 | +
|
| 50 | + Returns |
| 51 | + ------- |
| 52 | + ndarray |
| 53 | + Corrected (or not) CuPy array. |
| 54 | + """ |
| 55 | + if cupy_run: |
| 56 | + xp = cp.get_array_module(data) |
| 57 | + else: |
| 58 | + import numpy as xp |
| 59 | + |
| 60 | + if not xp.all(xp.isfinite(data)): |
| 61 | + if verbosity: |
| 62 | + print( |
| 63 | + f"Warning!!! Input data to method: {method_name} contains Inf's or/and NaN's." |
| 64 | + ) |
| 65 | + if correction: |
| 66 | + print( |
| 67 | + "Inf's or/and NaN's will be corrected to finite integers (zeros). It is advisable to check the correctness of the input." |
| 68 | + ) |
| 69 | + xp.nan_to_num(data, copy=False, nan=0.0, posinf=0.0, neginf=0.0) |
| 70 | + return data |
| 71 | + |
| 72 | + |
| 73 | +def _zeros_check( |
| 74 | + data: cp.ndarray, |
| 75 | + verbosity: bool = True, |
| 76 | + percentage_threshold: float = 50, |
| 77 | + method_name: Optional[str] = None, |
| 78 | +) -> bool: |
| 79 | + """ |
| 80 | + Function finds NaN's, +-Inf's in the input data and then prints the warning and correct the data |
| 81 | +
|
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + data : cp.ndarray |
| 85 | + Input CuPy array either float32 or uint16 data type. |
| 86 | + verbosity : bool |
| 87 | + If enabled, then the printing of the warning happens when data contains infs or nans |
| 88 | + percentage_threshold: float: |
| 89 | + If the number of zeros in input data is more than the percentage of all data points, then print the data warning |
| 90 | + method_name : str, optional. |
| 91 | + Method's name for which input data is tested. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + bool |
| 96 | + True if the data contains too many zeros |
| 97 | + """ |
| 98 | + if cupy_run: |
| 99 | + xp = cp.get_array_module(data) |
| 100 | + else: |
| 101 | + import numpy as xp |
| 102 | + |
| 103 | + warning_zeros = False |
| 104 | + zero_elements_total = int(xp.count_nonzero(data == 0)) |
| 105 | + nonzero_elements_total = len(data.flatten()) |
| 106 | + if (zero_elements_total / nonzero_elements_total) * 100 >= percentage_threshold: |
| 107 | + warning_zeros = True |
| 108 | + if verbosity: |
| 109 | + print( |
| 110 | + f"Warning!!! Input data to method: {method_name} contains more than {percentage_threshold} percent of zeros." |
| 111 | + ) |
| 112 | + |
| 113 | + return warning_zeros |
0 commit comments