Skip to content

Commit

Permalink
Style changes, change ValueError into TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
adriaangraas committed Jan 28, 2020
1 parent 7ddafcc commit d9fb3f1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 34 deletions.
16 changes: 8 additions & 8 deletions odl/tomo/backends/astra_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,8 @@ def default_astra_proj_type(geom):
elif isinstance(geom, DivergentBeamGeometry):
return 'line_fanflat' if geom.ndim == 2 else 'linearcone'
else:
raise TypeError(
'no default exists for {}, `astra_proj_type` must be given explicitly'
''.format(type(geom))
)
raise TypeError('no default exists for {}, `astra_proj_type` '
'must be given explicitly'''.format(type(geom)))


def astra_cpu_forward_projector(vol_data, geometry, proj_space, out=None,
Expand Down Expand Up @@ -202,8 +200,8 @@ def astra_cpu_back_projector(proj_data, geometry, vol_space, out=None,
"".format(vol_space.impl))
if vol_space.ndim != geometry.ndim:
raise ValueError('dimensions {} of reconstruction space and {} of '
'geometry do not match'.format(
vol_space.ndim, geometry.ndim))
'geometry do not match'.format(vol_space.ndim,
geometry.ndim))
if out is None:
out = vol_space.element()
else:
Expand Down Expand Up @@ -269,11 +267,13 @@ def supports_geometry(cls, geometry):

def call_forward(self, x_real, out_real, **kwargs):
return astra_cpu_forward_projector(
x_real, self.geometry, self.proj_space.real_space, out_real, **kwargs)
x_real, self.geometry, self.proj_space.real_space,
out_real, **kwargs)

def call_backward(self, x_real, out_real, **kwargs):
return astra_cpu_back_projector(
x_real, self.geometry, self.reco_space.real_space, out_real, **kwargs)
x_real, self.geometry, self.reco_space.real_space,
out_real, **kwargs)


if __name__ == '__main__':
Expand Down
9 changes: 6 additions & 3 deletions odl/tomo/backends/astra_cuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from odl.discr import DiscreteLp
from odl.tomo.backends.astra_setup import (
ASTRA_VERSION, astra_algorithm, astra_data, astra_projection_geometry,
astra_projector, astra_volume_geometry, astra_supports, astra_versions_supporting)
astra_projector, astra_volume_geometry, astra_supports,
astra_versions_supporting)
from odl.tomo.geometry import (
ConeBeamGeometry, FanBeamGeometry, Geometry, Parallel2dGeometry,
Parallel3dAxisGeometry)
Expand Down Expand Up @@ -74,7 +75,8 @@ def supports_geometry(cls, geometry):
# single-axis geometry -- this is broken in some ASTRA versions
if (isinstance(geometry, Parallel3dAxisGeometry) and
not astra_supports('par3d_det_mid_pt_perp_to_axis')):
req_ver = astra_versions_supporting('par3d_det_mid_pt_perp_to_axis')
req_ver = astra_versions_supporting(
'par3d_det_mid_pt_perp_to_axis')
axis = geometry.axis
mid_pt = geometry.det_params.mid_pt
for i, angle in enumerate(geometry.angles):
Expand Down Expand Up @@ -114,7 +116,8 @@ def create_ids(self):
astra_vol_shape = self.reco_space.shape

self.vol_array = np.empty(astra_vol_shape, dtype='float32', order='C')
self.proj_array = np.empty(astra_proj_shape, dtype='float32', order='C')
self.proj_array = np.empty(astra_proj_shape, dtype='float32',
order='C')

# Create ASTRA data structures
vol_geom = astra_volume_geometry(self.reco_space)
Expand Down
8 changes: 4 additions & 4 deletions odl/tomo/backends/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def __init__(self, geometry, reco_space, proj_space):
'{!r}'.format(geometry))

if not isinstance(reco_space, DiscreteLp):
raise TypeError('`reco_space` must be a `DiscreteLP` instance, got '
'{!r}'.format(reco_space))
raise TypeError('`reco_space` must be a `DiscreteLP` instance, got'
' {!r}'.format(reco_space))

if not isinstance(proj_space, DiscreteLp):
raise TypeError('`proj_space` must be a `DiscreteLP` instance, got '
'{!r}'.format(proj_space))
raise TypeError('`proj_space` must be a `DiscreteLP` instance, got'
' {!r}'.format(proj_space))

self.geometry = geometry
self.reco_space = reco_space
Expand Down
45 changes: 26 additions & 19 deletions odl/tomo/operators/ray_trafo.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from odl.tomo.backends import (
ASTRA_AVAILABLE, ASTRA_CUDA_AVAILABLE, SKIMAGE_AVAILABLE,
RayTransformImplBase,
SkimageRayTransformImpl, AstraCudaRayTransformImpl, AstraCpuRayTransformImpl)
SkimageRayTransformImpl, AstraCudaRayTransformImpl,
AstraCpuRayTransformImpl)
from odl.tomo.geometry import Geometry

# Backends that are implemented in ODL and can be specified via `impl`
Expand Down Expand Up @@ -120,9 +121,9 @@ def __init__(self, reco_space, geometry, variant, **kwargs):

if not reco_space.is_weighted:
weighting = None
elif (isinstance(reco_space.weighting, ConstWeighting) and
np.isclose(reco_space.weighting.const,
reco_space.cell_volume)):
elif (isinstance(reco_space.weighting, ConstWeighting)
and np.isclose(reco_space.weighting.const,
reco_space.cell_volume)):
# Approximate cell volume
# TODO: find a way to treat angles and detector differently
# regarding weighting. While the detector should be uniformly
Expand Down Expand Up @@ -224,8 +225,8 @@ def __init__(self, reco_space, geometry, variant, **kwargs):
RuntimeWarning)
else:
warnings.warn(
"The `impl` backend indicates that it might be too slow "
" for volumes of the input size.",
"The `impl` backend indicates that it might be too "
"slow for volumes of the input size.",
RuntimeWarning)

else:
Expand All @@ -235,14 +236,15 @@ def __init__(self, reco_space, geometry, variant, **kwargs):
raise ValueError('`impl` {!r} not understood'.format(impl))

if impl.lower() not in _AVAILABLE_IMPLS:
raise ValueError('{!r} back-end not available'.format(impl))
raise ValueError(
'{!r} back-end not available'.format(impl))

impl_type = _SUPPORTED_IMPL[impl.lower()]
elif isinstance(impl, type):
# User gave the type and leaves instantiation to us
if not issubclass(impl, RayTransformImplBase):
raise ValueError('Type {!r} must be a subclass of '
'`RayTransformImplBase`.'.format(impl))
raise TypeError('Type {!r} must be a subclass of '
'`RayTransformImplBase`.'.format(impl))

impl_type = impl
elif isinstance(impl, RayTransformImplBase):
Expand All @@ -252,15 +254,17 @@ def __init__(self, reco_space, geometry, variant, **kwargs):
self.__cached_impl = impl
else:
raise TypeError(
'Given `impl` should be a `str`, or the type or subclass of '
'`RayTransformImplBase`, now it is a {!r}.'.format(type(impl)))
'Given `impl` should be a `str`, or the type or subclass '
'of `RayTransformImplBase`, '
'now it is a {!r}.'.format(type(impl)))

# Sanity checks
geometry_support = impl_type.supports_geometry(geometry)
if not geometry_support:
raise geometry_support

reco_space_support = impl_type.supports_reco_space(reco_space, reco_name)
reco_space_support = impl_type.supports_reco_space(reco_space,
reco_name)
if not reco_space_support:
raise reco_space_support

Expand Down Expand Up @@ -413,7 +417,9 @@ def adjoint(self):
# initiate adjoint with cached implementation if `use_cache`
self._adjoint = RayBackProjection(
self.domain, self.geometry,
impl=self.impl if not self.use_cache else self.create_impl(self.use_cache),
impl=(self.impl
if not self.use_cache
else self.create_impl(self.use_cache)),
use_cache=self.use_cache,
**kwargs)

Expand Down Expand Up @@ -499,14 +505,15 @@ def adjoint(self):
# initiate adjoint with cached implementation if `use_cache`
self._adjoint = RayTransform(
self.range, self.geometry,
impl=self.impl if not self.use_cache else self.create_impl(self.use_cache),
impl=(self.impl
if not self.use_cache
else self.create_impl(self.use_cache)),
use_cache=self.use_cache,
**kwargs)

return self._adjoint

return self._adjoint

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

run_doctests()
run_doctests()

0 comments on commit d9fb3f1

Please sign in to comment.