Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve fbp speed #1505

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


# Ray transform (= forward projection).
ray_trafo = odl.tomo.RayTransform(reco_space, geometry, impl='astra_cuda')
ray_trafo = odl.tomo.RayTransform(reco_space, geometry, impl='astra_cpu')

# Create filtered back-projection operator
fbp = odl.tomo.fbp_op(ray_trafo)
Expand Down
10 changes: 7 additions & 3 deletions odl/tomo/analytic/filtered_back_projection.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from odl.discr import ResizingOperator
from odl.trafos import FourierTransform, PYFFTW_AVAILABLE
from odl.trafos.util import next_fast_len


__all__ = ('fbp_op', 'fbp_filter_op', 'tam_danielson_window',
Expand Down Expand Up @@ -379,7 +380,8 @@ def fourier_filter(x):
if padding:
# Define padding operator
ran_shp = (ray_trafo.range.shape[0],
ray_trafo.range.shape[1] * 2 - 1)
next_fast_len(
ray_trafo.range.shape[1] * 2 - 1, impl, rfft=True))
resizing = ResizingOperator(ray_trafo.range, ran_shp=ran_shp)

fourier = FourierTransform(resizing.range, axes=1, impl=impl)
Expand Down Expand Up @@ -435,12 +437,14 @@ def fourier_filter(x):
if padding:
# Define padding operator
if used_axes[0]:
padded_shape_u = ray_trafo.range.shape[1] * 2 - 1
padded_shape_u = next_fast_len(
ray_trafo.range.shape[1] * 2 - 1, impl, rfft=True)
else:
padded_shape_u = ray_trafo.range.shape[1]

if used_axes[1]:
padded_shape_v = ray_trafo.range.shape[2] * 2 - 1
padded_shape_v = next_fast_len(
ray_trafo.range.shape[2] * 2 - 1, impl, rfft=True)
else:
padded_shape_v = ray_trafo.range.shape[2]

Expand Down
18 changes: 16 additions & 2 deletions odl/trafos/util/ft_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
is_real_dtype, is_numeric_dtype, is_real_floating_dtype,
is_complex_floating_dtype, complex_dtype, dtype_repr,
is_string,
normalized_scalar_param_list, normalized_axes_tuple)
normalized_scalar_param_list, normalized_axes_tuple, nextpow2)


__all__ = ('reciprocal_grid', 'realspace_grid',
'reciprocal_space',
'dft_preprocess_data', 'dft_postprocess_data')
'dft_preprocess_data', 'dft_postprocess_data',
'next_fast_len')


def reciprocal_grid(grid, shift=True, axes=None, halfcomplex=False):
Expand Down Expand Up @@ -649,6 +650,19 @@ def reciprocal_space(space, axes=None, halfcomplex=False, shift=True,
return recip_spc


def next_fast_len(n, impl, rfft=False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe is_rfft

"""The smallest size for which a fast fft implementation is available."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FFT

n = int(n)
if impl == 'pyfftw':
if rfft:
return 2 ** nextpow2(n)
else:
import pyfftw
return pyfftw.next_fast_len(n)
elif impl == 'numpy':
return 2 ** nextpow2(n)


if __name__ == '__main__':
from doctest import testmod, NORMALIZE_WHITESPACE
testmod(optionflags=NORMALIZE_WHITESPACE)
23 changes: 23 additions & 0 deletions odl/util/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
'repr_string',
'attribute_repr_string',
'method_repr_string',
'nextpow2',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although tiny, I'd probably put this into numerics.py, if only to avoid littering this kitchen sink file.

'run_from_ipython',
'npy_random_seed',
'unique',
Expand Down Expand Up @@ -1607,6 +1608,28 @@ def unique(seq):
return unique_values


def nextpow2(n):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next_int_log2?

"""
Compute the integer which is a power of two.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On first line.
An maybe: "Return the next integer power of 2."
Actually, after the change, it's more like "Return the exponent of the next integer power of 2."


Parameters
----------
n : int

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No text? Not that there's a lot to say, but doc formatting might break.

Examples
========
>>> odl.util.nextpow2(0)
1
>>> odl.util.nextpow2(7)
8
>>> odl.util.nextpow2(513)
1024
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doctests ought to fail now

"""
if n == 0:
return 0
return int(np.ceil(np.log2(n)))


if __name__ == '__main__':
from odl.util.testutils import run_doctests
run_doctests()