Skip to content

Commit aebc0e3

Browse files
authored
Merge branch 'main' into phase_cross_correlation-radway-62
2 parents 4d39009 + af88ad0 commit aebc0e3

File tree

13 files changed

+230
-362
lines changed

13 files changed

+230
-362
lines changed

httomolibgpu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from httomolibgpu.misc.morph import sino_360_to_180, data_resampler
66
from httomolibgpu.misc.rescale import rescale_to_int
77
from httomolibgpu.prep.alignment import distortion_correction_proj_discorpy
8-
from httomolibgpu.prep.normalize import normalize
8+
from httomolibgpu.prep.normalize import dark_flat_field_correction, minus_log
99
from httomolibgpu.prep.phase import paganin_filter, paganin_filter_savu_legacy
1010
from httomolibgpu.prep.stripe import (
1111
remove_stripe_based_sorting,

httomolibgpu/prep/normalize.py

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
# ---------------------------------------------------------------------------
2121
"""Modules for raw projection data normalization"""
2222

23-
import numpy as np
2423
from httomolibgpu import cupywrapper
2524

2625
cp = cupywrapper.cp
@@ -34,25 +33,21 @@
3433
mean = Mock()
3534

3635
from numpy import float32
37-
from typing import Tuple
3836

39-
__all__ = ["normalize"]
4037

38+
__all__ = ["dark_flat_field_correction", "minus_log"]
4139

42-
def normalize(
40+
41+
def dark_flat_field_correction(
4342
data: cp.ndarray,
4443
flats: cp.ndarray,
4544
darks: cp.ndarray,
4645
flats_multiplier: float = 1.0,
4746
darks_multiplier: float = 1.0,
4847
cutoff: float = 10.0,
49-
minus_log: bool = True,
50-
nonnegativity: bool = False,
51-
remove_nans: bool = False,
5248
) -> cp.ndarray:
5349
"""
5450
Normalize raw projection data using the flat and dark field projections.
55-
This is a raw CUDA kernel implementation with CuPy wrappers.
5651
5752
Parameters
5853
----------
@@ -68,17 +63,11 @@ def normalize(
6863
A multiplier to apply to darks, can work as an intensity compensation constant.
6964
cutoff : float
7065
Permitted maximum value for the normalised data.
71-
minus_log : bool
72-
Apply negative log to the normalised data.
73-
nonnegativity : bool
74-
Remove negative values in the normalised data.
75-
remove_nans : bool
76-
Remove NaN and Inf values in the normalised data.
7766
78-
Returns
67+
Returns
7968
-------
8069
cp.ndarray
81-
Normalised 3D tomographic data as a CuPy array.
70+
Normalised by dark/flat fields 3D tomographic data as a CuPy array.
8271
"""
8372
_check_valid_input_normalise(data, flats, darks)
8473

@@ -99,16 +88,6 @@ def normalize(
9988
}
10089
float v = (float(data) - float(darks))/denom;
10190
"""
102-
if minus_log:
103-
kernel += "v = -log(v);\n"
104-
kernel_name += "_mlog"
105-
if nonnegativity:
106-
kernel += "if (v < 0.0f) v = 0.0f;\n"
107-
kernel_name += "_nneg"
108-
if remove_nans:
109-
kernel += "if (isnan(v)) v = 0.0f;\n"
110-
kernel += "if (isinf(v)) v = 0.0f;\n"
111-
kernel_name += "_remnan"
11291
kernel += "if (v > cutoff) v = cutoff;\n"
11392
kernel += "if (v < -cutoff) v = cutoff;\n"
11493
kernel += "out = v;\n"
@@ -128,6 +107,24 @@ def normalize(
128107
return out
129108

130109

110+
def minus_log(data: cp.ndarray) -> cp.ndarray:
111+
"""
112+
Apply -log(data) operation
113+
114+
Parameters
115+
----------
116+
data : cp.ndarray
117+
Data as a CuPy array.
118+
119+
Returns
120+
-------
121+
cp.ndarray
122+
data after -log(data)
123+
"""
124+
125+
return -cp.log(data)
126+
127+
131128
def _check_valid_input_normalise(data, flats, darks) -> None:
132129
"""Helper function to check the validity of inputs to normalisation functions"""
133130
if data.ndim != 3:

httomolibgpu/recon/algorithm.py

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ def FBP2d_astra(
6464
filter_d: Optional[float] = None,
6565
recon_size: Optional[int] = None,
6666
recon_mask_radius: float = 0.95,
67-
neglog: bool = False,
6867
gpu_id: int = 0,
6968
) -> np.ndarray:
7069
"""
@@ -96,9 +95,6 @@ def FBP2d_astra(
9695
The radius of the circular mask that applies to the reconstructed slice in order to crop
9796
out some undesirable artifacts. The values outside the given diameter will be set to zero.
9897
To implement the cropping one can use the range [0.7-1.0] or set to 2.0 when no cropping required.
99-
neglog: bool
100-
Take negative logarithm on input data to convert to attenuation coefficient or a density of the scanned object. Defaults to False,
101-
assuming that the negative log is taken either in normalisation procedure on with Paganin filter application.
10298
gpu_id : int
10399
A GPU device index to perform operation on.
104100
@@ -120,8 +116,6 @@ def FBP2d_astra(
120116
reconstruction = np.empty(
121117
(recon_size, detY_size, recon_size), dtype=float32, order="C"
122118
)
123-
_take_neg_log_np(data) if neglog else data
124-
125119
# loop over detY slices
126120
for slice_index in range(0, detY_size):
127121
reconstruction[:, slice_index, :] = np.flipud(
@@ -145,7 +139,6 @@ def FBP3d_tomobar(
145139
filter_freq_cutoff: float = 0.35,
146140
recon_size: Optional[int] = None,
147141
recon_mask_radius: Optional[float] = 0.95,
148-
neglog: bool = False,
149142
gpu_id: int = 0,
150143
) -> cp.ndarray:
151144
"""
@@ -174,9 +167,6 @@ def FBP3d_tomobar(
174167
The radius of the circular mask that applies to the reconstructed slice in order to crop
175168
out some undesirable artifacts. The values outside the given diameter will be set to zero.
176169
To implement the cropping one can use the range [0.7-1.0] or set to 2.0 when no cropping required.
177-
neglog: bool
178-
Take negative logarithm on input data to convert to attenuation coefficient or a density of the scanned object. Defaults to False,
179-
assuming that the negative log is taken either in normalisation procedure on with Paganin filter application.
180170
gpu_id : int
181171
A GPU device index to perform operation on.
182172
@@ -191,7 +181,7 @@ def FBP3d_tomobar(
191181
)
192182

193183
reconstruction = RecToolsCP.FBP(
194-
_take_neg_log(data) if neglog else data,
184+
data,
195185
cutoff_freq=filter_freq_cutoff,
196186
recon_mask_radius=recon_mask_radius,
197187
data_axes_labels_order=input_data_axis_labels,
@@ -214,7 +204,6 @@ def LPRec3d_tomobar(
214204
power_of_2_cropping: Optional[bool] = False,
215205
min_mem_usage_filter: Optional[bool] = True,
216206
min_mem_usage_ifft2: Optional[bool] = True,
217-
neglog: bool = False,
218207
) -> cp.ndarray:
219208
"""
220209
Fourier direct inversion in 3D on unequally spaced (also called as Log-Polar) grids using
@@ -243,9 +232,6 @@ def LPRec3d_tomobar(
243232
The radius of the circular mask that applies to the reconstructed slice in order to crop
244233
out some undesirable artifacts. The values outside the given diameter will be set to zero.
245234
To implement the cropping one can use the range [0.7-1.0] or set to 2.0 when no cropping required.
246-
neglog: bool
247-
Take negative logarithm on input data to convert to attenuation coefficient or a density of the scanned object. Defaults to False,
248-
assuming that the negative log is taken either in normalisation procedure on with Paganin filter application.
249235
250236
Returns
251237
-------
@@ -258,7 +244,7 @@ def LPRec3d_tomobar(
258244
)
259245

260246
reconstruction = RecToolsCP.FOURIER_INV(
261-
_take_neg_log(data) if neglog else data,
247+
data,
262248
recon_mask_radius=recon_mask_radius,
263249
data_axes_labels_order=input_data_axis_labels,
264250
filter_type=filter_type,
@@ -282,7 +268,6 @@ def SIRT3d_tomobar(
282268
recon_mask_radius: float = 0.95,
283269
iterations: int = 300,
284270
nonnegativity: bool = True,
285-
neglog: bool = False,
286271
gpu_id: int = 0,
287272
) -> cp.ndarray:
288273
"""
@@ -312,10 +297,7 @@ def SIRT3d_tomobar(
312297
iterations : int
313298
The number of SIRT iterations.
314299
nonnegativity : bool
315-
Impose nonnegativity constraint on reconstructed image.
316-
neglog: bool
317-
Take negative logarithm on input data to convert to attenuation coefficient or a density of the scanned object. Defaults to False,
318-
assuming that the negative log is taken either in normalisation procedure on with Paganin filter application.
300+
Impose nonnegativity constraint on the reconstructed image.
319301
gpu_id : int
320302
A GPU device index to perform operation on.
321303
@@ -336,7 +318,7 @@ def SIRT3d_tomobar(
336318
)
337319

338320
_data_ = {
339-
"projection_norm_data": _take_neg_log(data) if neglog else data,
321+
"projection_norm_data": data,
340322
"data_axes_labels_order": input_data_axis_labels,
341323
} # data dictionary
342324
_algorithm_ = {
@@ -359,7 +341,6 @@ def CGLS3d_tomobar(
359341
recon_mask_radius: float = 0.95,
360342
iterations: int = 20,
361343
nonnegativity: bool = True,
362-
neglog: bool = False,
363344
gpu_id: int = 0,
364345
) -> cp.ndarray:
365346
"""
@@ -390,9 +371,6 @@ def CGLS3d_tomobar(
390371
The number of CGLS iterations.
391372
nonnegativity : bool
392373
Impose nonnegativity constraint on reconstructed image.
393-
neglog: bool
394-
Take negative logarithm on input data to convert to attenuation coefficient or a density of the scanned object. Defaults to False,
395-
assuming that the negative log is taken either in normalisation procedure on with Paganin filter application.
396374
gpu_id : int, optional
397375
A GPU device index to perform operation on.
398376
@@ -407,7 +385,7 @@ def CGLS3d_tomobar(
407385
)
408386

409387
_data_ = {
410-
"projection_norm_data": _take_neg_log(data) if neglog else data,
388+
"projection_norm_data": data,
411389
"data_axes_labels_order": input_data_axis_labels,
412390
} # data dictionary
413391
_algorithm_ = {
@@ -435,7 +413,6 @@ def FISTA3d_tomobar(
435413
regularisation_iterations: int = 50,
436414
regularisation_half_precision: bool = True,
437415
nonnegativity: bool = True,
438-
neglog: bool = False,
439416
gpu_id: int = 0,
440417
) -> cp.ndarray:
441418
"""
@@ -474,9 +451,6 @@ def FISTA3d_tomobar(
474451
Perform faster regularisation computation in half-precision with a very minimal sacrifice in quality.
475452
nonnegativity : bool
476453
Impose nonnegativity constraint on the reconstructed image.
477-
neglog: bool
478-
Take negative logarithm on input data to convert to attenuation coefficient or a density of the scanned object. Defaults to False,
479-
assuming that the negative log is taken either in normalisation procedure on with Paganin filter application.
480454
gpu_id : int
481455
A GPU device index to perform operation on.
482456
@@ -490,7 +464,7 @@ def FISTA3d_tomobar(
490464
)
491465

492466
_data_ = {
493-
"projection_norm_data": _take_neg_log(data) if neglog else data,
467+
"projection_norm_data": data,
494468
"OS_number": subsets_number,
495469
"data_axes_labels_order": input_data_axis_labels,
496470
}
@@ -649,24 +623,6 @@ def _instantiate_iterative_recon_class(
649623
return RecToolsCP
650624

651625

652-
def _take_neg_log(data: cp.ndarray) -> cp.ndarray:
653-
"""Taking negative log"""
654-
data[data <= 0] = 1
655-
data = -cp.log(data)
656-
data[cp.isnan(data)] = 6.0
657-
data[cp.isinf(data)] = 0
658-
return data
659-
660-
661-
def _take_neg_log_np(data: np.ndarray) -> np.ndarray:
662-
"""Taking negative log"""
663-
data[data <= 0] = 1
664-
data = -np.log(data)
665-
data[np.isnan(data)] = 6.0
666-
data[np.isinf(data)] = 0
667-
return data
668-
669-
670626
def __estimate_detectorHoriz_padding(detX_size) -> int:
671627
det_half = detX_size // 2
672628
padded_value_exact = int(np.sqrt(2 * (det_half**2))) - det_half

tests/test_prep/stripe_cpu_reference.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)