Skip to content

Commit 86b5ae0

Browse files
committed
enabling data validation for the remaining methods
1 parent 30d78c9 commit 86b5ae0

File tree

12 files changed

+76
-108
lines changed

12 files changed

+76
-108
lines changed

httomolibgpu/misc/corr.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
else:
3737
load_cuda_module = Mock()
3838

39-
from httomolibgpu.misc.supp_func import _naninfs_check, _zeros_check
39+
from httomolibgpu.misc.supp_func import data_checker
4040

4141
__all__ = [
4242
"median_filter",
@@ -82,19 +82,7 @@ def median_filter(
8282
else:
8383
raise ValueError("The input array must be a 3D array")
8484

85-
verbosity_enabled = True # printing the data-related warnings
86-
method_name = "median_filter"
87-
88-
data = _naninfs_check(
89-
data, correction=True, verbosity=verbosity_enabled, method_name=method_name
90-
)
91-
92-
_zeros_check(
93-
data,
94-
verbosity=verbosity_enabled,
95-
percentage_threshold=50,
96-
method_name=method_name,
97-
)
85+
data = data_checker(data, verbosity=True, method_name="median_filter_or_remove_outlier")
9886

9987
if kernel_size not in [3, 5, 7, 9, 11, 13]:
10088
raise ValueError("Please select a correct kernel size: 3, 5, 7, 9, 11, 13")

httomolibgpu/misc/denoise.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
cp = cupywrapper.cp
2929
cupy_run = cupywrapper.cupy_run
3030

31-
from numpy import float32
3231
from unittest.mock import Mock
3332

34-
from httomolibgpu.misc.supp_func import _naninfs_check, _zeros_check
33+
from httomolibgpu.misc.supp_func import data_checker
3534

3635
if cupy_run:
3736
from ccpi.filters.regularisersCuPy import ROF_TV, PD_TV
@@ -82,19 +81,8 @@ def total_variation_ROF(
8281
ValueError
8382
If the input array is not float32 data type.
8483
"""
85-
verbosity_enabled = True # printing the data-related warnings
86-
method_name = "total_variation_ROF"
8784

88-
data = _naninfs_check(
89-
data, correction=True, verbosity=verbosity_enabled, method_name=method_name
90-
)
91-
92-
_zeros_check(
93-
data,
94-
verbosity=verbosity_enabled,
95-
percentage_threshold=50,
96-
method_name=method_name,
97-
)
85+
data = data_checker(data,verbosity=True,method_name="total_variation_ROF")
9886

9987
return ROF_TV(
10088
data, regularisation_parameter, iterations, time_marching_parameter, gpu_id
@@ -140,19 +128,8 @@ def total_variation_PD(
140128
ValueError
141129
If the input array is not float32 data type.
142130
"""
143-
verbosity_enabled = True # printing the data-related warnings
144-
method_name = "total_variation_PD"
145131

146-
data = _naninfs_check(
147-
data, correction=True, verbosity=verbosity_enabled, method_name=method_name
148-
)
149-
150-
_zeros_check(
151-
data,
152-
verbosity=verbosity_enabled,
153-
percentage_threshold=50,
154-
method_name=method_name,
155-
)
132+
data_checker(data,verbosity=True,method_name="total_variation_PD")
156133

157134
methodTV = 0
158135
if not isotropic:

httomolibgpu/misc/morph.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
from typing import Literal
3737

38-
from httomolibgpu.misc.supp_func import _naninfs_check, _zeros_check
38+
from httomolibgpu.misc.supp_func import data_checker
3939

4040
__all__ = [
4141
"sino_360_to_180",
@@ -68,19 +68,7 @@ def sino_360_to_180(
6868
if data.ndim != 3:
6969
raise ValueError("only 3D data is supported")
7070

71-
verbosity_enabled = True # printing the data-related warnings
72-
method_name = "sino_360_to_180"
73-
74-
data = _naninfs_check(
75-
data, correction=True, verbosity=verbosity_enabled, method_name=method_name
76-
)
77-
78-
_zeros_check(
79-
data,
80-
verbosity=verbosity_enabled,
81-
percentage_threshold=50,
82-
method_name=method_name,
83-
)
71+
data = data_checker(data, verbosity=True, method_name="sino_360_to_180")
8472

8573
dx, dy, dz = data.shape
8674

@@ -152,19 +140,7 @@ def data_resampler(
152140
data = cp.expand_dims(data, 1)
153141
axis = 1
154142

155-
verbosity_enabled = True # printing the data-related warnings
156-
method_name = "data_resampler"
157-
158-
data = _naninfs_check(
159-
data, correction=True, verbosity=verbosity_enabled, method_name=method_name
160-
)
161-
162-
_zeros_check(
163-
data,
164-
verbosity=verbosity_enabled,
165-
percentage_threshold=50,
166-
method_name=method_name,
167-
)
143+
data = data_checker(data, verbosity=True, method_name="data_resampler")
168144

169145
N, M, Z = cp.shape(data)
170146

httomolibgpu/misc/rescale.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
from typing import Literal, Optional, Tuple, Union
3030

31-
from httomolibgpu.misc.supp_func import _naninfs_check, _zeros_check
31+
from httomolibgpu.misc.supp_func import data_checker
3232

3333
__all__ = [
3434
"rescale_to_int",
@@ -80,19 +80,7 @@ def rescale_to_int(
8080
else:
8181
output_dtype = np.uint32
8282

83-
verbosity_enabled = True # printing the data-related warnings
84-
method_name = "rescale_to_int"
85-
86-
data = _naninfs_check(
87-
data, correction=True, verbosity=verbosity_enabled, method_name=method_name
88-
)
89-
90-
_zeros_check(
91-
data,
92-
verbosity=verbosity_enabled,
93-
percentage_threshold=50,
94-
method_name=method_name,
95-
)
83+
data = data_checker(data, verbosity=True, method_name="rescale_to_int")
9684

9785
if cupy_run:
9886
xp = cp.get_array_module(data)

httomolibgpu/prep/alignment.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
from typing import Dict, List, Tuple
3737

38+
from httomolibgpu.misc.supp_func import data_checker
39+
3840
__all__ = [
3941
"distortion_correction_proj_discorpy",
4042
]
@@ -86,6 +88,8 @@ def distortion_correction_proj_discorpy(
8688
if len(data.shape) == 2:
8789
data = cp.expand_dims(data, axis=0)
8890

91+
data = data_checker(data, verbosity=True, method_name="distortion_correction_proj_discorpy")
92+
8993
# Get info from metadata txt file
9094
xcenter, ycenter, list_fact = _load_metadata_txt(metadata_path)
9195

httomolibgpu/prep/normalize.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
from numpy import float32
3737
from typing import Tuple
3838

39+
from httomolibgpu.misc.supp_func import data_checker
40+
3941
__all__ = ["normalize"]
4042

4143

@@ -80,7 +82,7 @@ def normalize(
8082
cp.ndarray
8183
Normalised 3D tomographic data as a CuPy array.
8284
"""
83-
_check_valid_input(data, flats, darks)
85+
_check_valid_input_normalise(data, flats, darks)
8486

8587
dark0 = cp.empty(darks.shape[1:], dtype=float32)
8688
flat0 = cp.empty(flats.shape[1:], dtype=float32)
@@ -128,7 +130,7 @@ def normalize(
128130
return out
129131

130132

131-
def _check_valid_input(data, flats, darks) -> None:
133+
def _check_valid_input_normalise(data, flats, darks) -> None:
132134
"""Helper function to check the validity of inputs to normalisation functions"""
133135
if data.ndim != 3:
134136
raise ValueError("Input data must be a 3D stack of projections")
@@ -143,3 +145,7 @@ def _check_valid_input(data, flats, darks) -> None:
143145
flats = flats[cp.newaxis, :, :]
144146
if darks.ndim == 2:
145147
darks = darks[cp.newaxis, :, :]
148+
149+
data_checker(data,verbosity=True,method_name="normalize_data")
150+
data_checker(flats,verbosity=True,method_name="normalize_flats")
151+
data_checker(darks,verbosity=True,method_name="normalize_darks")

httomolibgpu/prep/phase.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
from typing import Tuple
4242
import math
4343

44+
from httomolibgpu.misc.supp_func import data_checker
45+
4446
__all__ = [
4547
"paganin_filter_savu",
4648
"paganin_filter_tomopy",
@@ -105,6 +107,8 @@ def paganin_filter_savu(
105107
" please provide a stack of 2D projections."
106108
)
107109

110+
data = data_checker(data, verbosity=True, method_name="paganin_filter_savu")
111+
108112
# Setup various values for the filter
109113
_, height, width = data.shape
110114
micron = 1e-6
@@ -296,6 +300,8 @@ def paganin_filter_tomopy(
296300
f"Invalid number of dimensions in data: {tomo.ndim},"
297301
" please provide a stack of 2D projections."
298302
)
303+
304+
tomo = data_checker(tomo, verbosity=True, method_name="paganin_filter_tomopy")
299305

300306
dz_orig, dy_orig, dx_orig = tomo.shape
301307

httomolibgpu/prep/stripe.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343

4444
from typing import Union
4545

46+
from httomolibgpu.misc.supp_func import data_checker
47+
4648
__all__ = [
4749
"remove_stripe_based_sorting",
4850
"remove_stripe_ti",
@@ -80,6 +82,9 @@ def remove_stripe_based_sorting(
8082
Corrected 3D tomographic data as a CuPy or NumPy array.
8183
8284
"""
85+
86+
data = data_checker(data, verbosity=True, method_name="remove_stripe_based_sorting")
87+
8388
if size is None:
8489
if data.shape[2] > 2000:
8590
size = 21
@@ -134,7 +139,13 @@ def remove_stripe_ti(
134139
ndarray
135140
3D array of de-striped projections.
136141
"""
137-
# TODO: detector dimensions must be even otherwise error
142+
data = data_checker(data, verbosity=True, method_name="remove_stripe_ti")
143+
144+
_, _, dx_orig = data.shape
145+
if (dx_orig % 2) != 0:
146+
# the horizontal detector size is odd, data needs to be padded/cropped, for now raising the error
147+
raise ValueError("The horizontal detector size must be even")
148+
138149
gamma = beta * ((1 - beta) / (1 + beta)) ** cp.abs(
139150
cp.fft.fftfreq(data.shape[-1]) * data.shape[-1]
140151
)
@@ -201,6 +212,8 @@ def remove_all_stripe(
201212
Corrected 3D tomographic data as a CuPy or NumPy array.
202213
203214
"""
215+
data = data_checker(data, verbosity=True, method_name="remove_all_stripe")
216+
204217
matindex = _create_matindex(data.shape[2], data.shape[0])
205218
for m in range(data.shape[1]):
206219
sino = data[:, m, :]
@@ -372,9 +385,10 @@ def raven_filter(
372385
ValueError
373386
If the input array is not three dimensional.
374387
"""
375-
376388
if data.dtype != cp.float32:
377389
raise ValueError("The input data should be float32 data type")
390+
391+
data = data_checker(data, verbosity=True, method_name="raven_filter")
378392

379393
# Padding of the sinogram
380394
data = cp.pad(data, ((pad_y, pad_y), (0, 0), (pad_x, pad_x)), mode=pad_method)

httomolibgpu/recon/algorithm.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
from numpy import float32, complex64
4141
from typing import Optional, Type
4242

43+
from httomolibgpu.misc.supp_func import data_checker
44+
4345

4446
__all__ = [
4547
"FBP2d_astra",
@@ -103,6 +105,8 @@ def FBP2d_astra(
103105
np.ndarray
104106
The FBP reconstructed volume as a numpy array.
105107
"""
108+
data = data_checker(data, verbosity=True, method_name="FBP2d_astra")
109+
106110
data_shape = np.shape(data)
107111
if recon_size is None:
108112
recon_size = data_shape[2]
@@ -175,6 +179,8 @@ def FBP3d_tomobar(
175179
cp.ndarray
176180
FBP reconstructed volume as a CuPy array.
177181
"""
182+
data = data_checker(data, verbosity=True, method_name="FBP3d_tomobar")
183+
178184
RecToolsCP = _instantiate_direct_recon_class(
179185
data, angles, center, recon_size, gpu_id
180186
)
@@ -227,6 +233,9 @@ def LPRec3d_tomobar(
227233
cp.ndarray
228234
The Log-polar Fourier reconstructed volume as a CuPy array.
229235
"""
236+
237+
data = data_checker(data, verbosity=True, method_name="LPRec3d_tomobar")
238+
230239
RecToolsCP = _instantiate_direct_recon_class(data, angles, center, recon_size, 0)
231240

232241
reconstruction = RecToolsCP.FOURIER_INV(
@@ -281,6 +290,8 @@ def SIRT3d_tomobar(
281290
cp.ndarray
282291
The SIRT reconstructed volume as a CuPy array.
283292
"""
293+
data = data_checker(data, verbosity=True, method_name="SIRT3d_tomobar")
294+
284295
RecToolsCP = _instantiate_iterative_recon_class(
285296
data,
286297
angles,
@@ -346,6 +357,8 @@ def CGLS3d_tomobar(
346357
cp.ndarray
347358
The CGLS reconstructed volume as a CuPy array.
348359
"""
360+
data = data_checker(data, verbosity=True, method_name="CGLS3d_tomobar")
361+
349362
RecToolsCP = _instantiate_iterative_recon_class(
350363
data, angles, center, recon_size, gpu_id, datafidelity="LS"
351364
)

0 commit comments

Comments
 (0)