def vector3(tomo1, tomo2, tomo3, theta1, theta2, theta3, center1=None, center2=None, center3=None, num_iter=1, axis1=0, axis2=1, axis3=2): tomo1 = dtype.as_float32(tomo1) tomo2 = dtype.as_float32(tomo2) tomo3 = dtype.as_float32(tomo3) theta1 = dtype.as_float32(theta1) theta2 = dtype.as_float32(theta2) theta3 = dtype.as_float32(theta3) # Initialize tomography data. tomo1 = init_tomo(tomo1, sinogram_order=False, sharedmem=False) tomo2 = init_tomo(tomo2, sinogram_order=False, sharedmem=False) tomo3 = init_tomo(tomo3, sinogram_order=False, sharedmem=False) recon_shape = (tomo1.shape[0], tomo1.shape[2], tomo1.shape[2]) recon1 = np.zeros(recon_shape, dtype=np.float32) recon2 = np.zeros(recon_shape, dtype=np.float32) recon3 = np.zeros(recon_shape, dtype=np.float32) center_arr1 = get_center(tomo1.shape, center1) center_arr2 = get_center(tomo2.shape, center2) center_arr3 = get_center(tomo3.shape, center3) extern.c_vector3(tomo1, tomo2, tomo3, center_arr1, center_arr2, center_arr3, recon1, recon2, recon3, theta1, theta2, theta3, num_gridx=tomo1.shape[2], num_gridy=tomo1.shape[2], num_iter=num_iter, axis1=axis1, axis2=axis2, axis3=axis3) return recon1, recon2, recon3
def vector(tomo, theta, center=None, num_iter=1): tomo = dtype.as_float32(tomo) theta = dtype.as_float32(theta) # Initialize tomography data. tomo = init_tomo(tomo, sinogram_order=False, sharedmem=False) recon_shape = (tomo.shape[0], tomo.shape[2], tomo.shape[2]) recon1 = np.zeros(recon_shape, dtype=np.float32) recon2 = np.zeros(recon_shape, dtype=np.float32) center_arr = get_center(tomo.shape, center) extern.c_vector(tomo, center_arr, recon1, recon2, theta, num_gridx=tomo.shape[2], num_gridy=tomo.shape[2], num_iter=num_iter) return recon1, recon2
def vector(tomo, theta, center=None, num_iter=1, axis=0): tomo = dtype.as_float32(tomo) theta = dtype.as_float32(theta) # Initialize tomography data. tomo = init_tomo(tomo, sinogram_order=False, sharedmem=False) recon_shape = (tomo.shape[0], tomo.shape[2], tomo.shape[2]) recon = np.zeros(recon_shape, dtype=np.float32) center_arr = get_center(tomo.shape, center) extern.c_vector(tomo, center_arr, recon, theta, num_gridx=tomo.shape[2], num_gridy=tomo.shape[2], num_iter=num_iter, axis=axis) return recon
def _get_algorithm_args(shape, theta, center): dx, dy, dz = shape theta = dtype.as_float32(theta) center = get_center(shape, center) return (dx, dy, dz, center, theta)
def recon( tomo, theta, center=None, sinogram_order=False, algorithm=None, init_recon=None, ncore=None, nchunk=None, **kwargs): """ Reconstruct object from projection data. Parameters ---------- tomo : ndarray 3D tomographic data. theta : array Projection angles in radian. center: array, optional Location of rotation axis. sinogram_order: bool, optional Determins whether data is a stack of sinograms (True, y-axis first axis) or a stack of radiographs (False, theta first axis). algorithm : {str, function} One of the following string values. 'art' Algebraic reconstruction technique :cite:`Kak:98`. 'bart' Block algebraic reconstruction technique. 'fbp' Filtered back-projection algorithm. 'gridrec' Fourier grid reconstruction algorithm :cite:`Dowd:99`, :cite:`Rivers:06`. 'mlem' Maximum-likelihood expectation maximization algorithm :cite:`Dempster:77`. 'osem' Ordered-subset expectation maximization algorithm :cite:`Hudson:94`. 'ospml_hybrid' Ordered-subset penalized maximum likelihood algorithm with weighted linear and quadratic penalties. 'ospml_quad' Ordered-subset penalized maximum likelihood algorithm with quadratic penalties. 'pml_hybrid' Penalized maximum likelihood algorithm with weighted linear and quadratic penalties :cite:`Chang:04`. 'pml_quad' Penalized maximum likelihood algorithm with quadratic penalty. 'sirt' Simultaneous algebraic reconstruction technique. num_gridx, num_gridy : int, optional Number of pixels along x- and y-axes in the reconstruction grid. filter_name : str, optional Name of the filter for analytic reconstruction. 'none' No filter. 'shepp' Shepp-Logan filter (default). 'cosine' Cosine filter. 'hann' Cosine filter. 'hamming' Hamming filter. 'ramlak' Ram-Lak filter. 'parzen' Parzen filter. 'butterworth' Butterworth filter. filter_par: list, optional Filter parameters as a list. num_iter : int, optional Number of algorithm iterations performed. num_block : int, optional Number of data blocks for intermediate updating the object. ind_block : array of int, optional Order of projections to be used for updating. reg_par : float, optional Regularization parameter for smoothing. init_recon : ndarray, optional Initial guess of the reconstruction. ncore : int, optional Number of cores that will be assigned to jobs. nchunk : int, optional Chunk size for each core. Returns ------- ndarray Reconstructed 3D object. Warning ------- Filtering is not implemented for fbp. Example ------- >>> import tomopy >>> obj = tomopy.shepp3d() # Generate an object. >>> ang = tomopy.angles(180) # Generate uniformly spaced tilt angles. >>> sim = tomopy.project(obj, ang) # Calculate projections. >>> rec = tomopy.recon(sim, ang, algorithm='art') # Reconstruct object. >>> >>> # Show 64th slice of the reconstructed object. >>> import pylab >>> pylab.imshow(rec[64], cmap='gray') >>> pylab.show() Example using the ASTRA toolbox for recontruction For more information, see http://sourceforge.net/p/astra-toolbox/wiki/Home/ and https://github.com/astra-toolbox/astra-toolbox. To install the ASTRA toolbox with conda, use: conda install -c https://conda.binstar.org/astra-toolbox astra-toolbox >>> import tomopy >>> obj = tomopy.shepp3d() # Generate an object. >>> ang = tomopy.angles(180) # Generate uniformly spaced tilt angles. >>> sim = tomopy.project(obj, ang) # Calculate projections. >>> >>> # Reconstruct object: >>> rec = tomopy.recon(sim, ang, algorithm=tomopy.astra, >>> options={'method':'SART', 'num_iter':10*180, >>> 'proj_type':'linear', >>> 'extra_options':{'MinConstraint':0}}) >>> >>> # Show 64th slice of the reconstructed object. >>> import pylab >>> pylab.imshow(rec[64], cmap='gray') >>> pylab.show() """ # Initialize tomography data. tomo = init_tomo(tomo, sinogram_order) allowed_kwargs = { 'art': ['num_gridx', 'num_gridy', 'num_iter'], 'bart': ['num_gridx', 'num_gridy', 'num_iter', 'num_block', 'ind_block'], 'fbp': ['num_gridx', 'num_gridy', 'filter_name', 'filter_par'], 'gridrec': ['num_gridx', 'num_gridy', 'filter_name', 'filter_par'], 'mlem': ['num_gridx', 'num_gridy', 'num_iter'], 'osem': ['num_gridx', 'num_gridy', 'num_iter', 'num_block', 'ind_block'], 'ospml_hybrid': ['num_gridx', 'num_gridy', 'num_iter', 'reg_par', 'num_block', 'ind_block'], 'ospml_quad': ['num_gridx', 'num_gridy', 'num_iter', 'reg_par', 'num_block', 'ind_block'], 'pml_hybrid': ['num_gridx', 'num_gridy', 'num_iter', 'reg_par'], 'pml_quad': ['num_gridx', 'num_gridy', 'num_iter', 'reg_par'], 'sirt': ['num_gridx', 'num_gridy', 'num_iter'], } generic_kwargs = ['num_gridx', 'num_gridy', 'options'] # Generate kwargs for the algorithm. kwargs_defaults = _get_algorithm_kwargs(tomo.shape) if isinstance(algorithm, six.string_types): # Check whether we have an allowed method if algorithm not in allowed_kwargs: raise ValueError( 'Keyword "algorithm" must be one of %s, or a Python method.' % (list(allowed_kwargs.keys()),)) # Make sure have allowed kwargs appropriate for algorithm. for key, value in list(kwargs.items()): if key not in allowed_kwargs[algorithm]: raise ValueError( '%s keyword not in allowed keywords %s' % (key, allowed_kwargs[algorithm])) else: # Make sure they are numpy arrays. if not isinstance(kwargs, (np.ndarray, np.generic)): kwargs[key] = np.array(value) # Make sure reg_par is float32. if key == 'reg_par': if not isinstance(kwargs['reg_par'], np.float32): kwargs['reg_par'] = np.array(value, dtype='float32') # Make sure filter_par is float32. if key == 'filter_par': if not isinstance(kwargs['filter_par'], np.float32): kwargs['filter_par'] = np.array(value, dtype='float32') # Set kwarg defaults. for kw in allowed_kwargs[algorithm]: kwargs.setdefault(kw, kwargs_defaults[kw]) elif hasattr(algorithm, '__call__'): # Set kwarg defaults. for kw in generic_kwargs: kwargs.setdefault(kw, kwargs_defaults[kw]) else: raise ValueError( 'Keyword "algorithm" must be one of %s, or a Python method.' % (list(allowed_kwargs.keys()),)) # Generate args for the algorithm. center_arr = get_center(tomo.shape, center) args = _get_algorithm_args(theta) # Initialize reconstruction. recon_shape = (tomo.shape[0], kwargs['num_gridx'], kwargs['num_gridy']) recon = _init_recon(recon_shape, init_recon) return _dist_recon( tomo, center_arr, recon, _get_func(algorithm), args, kwargs, ncore, nchunk)
def recon(tomo, theta, center=None, sinogram_order=False, algorithm=None, init_recon=None, ncore=None, nchunk=None, **kwargs): """ Reconstruct object from projection data. Parameters ---------- tomo : ndarray 3D tomographic data. theta : array Projection angles in radian. center: array, optional Location of rotation axis. sinogram_order: bool, optional Determins whether data is a stack of sinograms (True, y-axis first axis) or a stack of radiographs (False, theta first axis). algorithm : {str, function} One of the following string values. 'art' Algebraic reconstruction technique :cite:`Kak:98`. 'bart' Block algebraic reconstruction technique. 'fbp' Filtered back-projection algorithm. 'gridrec' Fourier grid reconstruction algorithm :cite:`Dowd:99`, :cite:`Rivers:06`. 'mlem' Maximum-likelihood expectation maximization algorithm :cite:`Dempster:77`. 'osem' Ordered-subset expectation maximization algorithm :cite:`Hudson:94`. 'ospml_hybrid' Ordered-subset penalized maximum likelihood algorithm with weighted linear and quadratic penalties. 'ospml_quad' Ordered-subset penalized maximum likelihood algorithm with quadratic penalties. 'pml_hybrid' Penalized maximum likelihood algorithm with weighted linear and quadratic penalties :cite:`Chang:04`. 'pml_quad' Penalized maximum likelihood algorithm with quadratic penalty. 'sirt' Simultaneous algebraic reconstruction technique. 'tv' Total Variation reconstruction technique :cite:`Chambolle:11`. 'grad' Gradient descent method with a constant step size num_gridx, num_gridy : int, optional Number of pixels along x- and y-axes in the reconstruction grid. filter_name : str, optional Name of the filter for analytic reconstruction. 'none' No filter. 'shepp' Shepp-Logan filter (default). 'cosine' Cosine filter. 'hann' Cosine filter. 'hamming' Hamming filter. 'ramlak' Ram-Lak filter. 'parzen' Parzen filter. 'butterworth' Butterworth filter. 'custom' A numpy array of size `next_power_of_2(num_detector_columns)/2` specifying a custom filter in Fourier domain. The first element of the filter should be the zero-frequency component. 'custom2d' A numpy array of size `num_projections*next_power_of_2(num_detector_columns)/2` specifying a custom angle-dependent filter in Fourier domain. The first element of each filter should be the zero-frequency component. filter_par: list, optional Filter parameters as a list. num_iter : int, optional Number of algorithm iterations performed. num_block : int, optional Number of data blocks for intermediate updating the object. ind_block : array of int, optional Order of projections to be used for updating. reg_par : float, optional Regularization parameter for smoothing. init_recon : ndarray, optional Initial guess of the reconstruction. ncore : int, optional Number of cores that will be assigned to jobs. nchunk : int, optional Chunk size for each core. Returns ------- ndarray Reconstructed 3D object. Warning ------- Filtering is not implemented for fbp. Example ------- >>> import tomopy >>> obj = tomopy.shepp3d() # Generate an object. >>> ang = tomopy.angles(180) # Generate uniformly spaced tilt angles. >>> sim = tomopy.project(obj, ang) # Calculate projections. >>> rec = tomopy.recon(sim, ang, algorithm='art') # Reconstruct object. >>> >>> # Show 64th slice of the reconstructed object. >>> import pylab >>> pylab.imshow(rec[64], cmap='gray') >>> pylab.show() Example using the ASTRA toolbox for recontruction For more information, see http://sourceforge.net/p/astra-toolbox/wiki/Home/ and https://github.com/astra-toolbox/astra-toolbox. To install the ASTRA toolbox with conda, use: conda install -c https://conda.binstar.org/astra-toolbox astra-toolbox >>> import tomopy >>> obj = tomopy.shepp3d() # Generate an object. >>> ang = tomopy.angles(180) # Generate uniformly spaced tilt angles. >>> sim = tomopy.project(obj, ang) # Calculate projections. >>> >>> # Reconstruct object: >>> rec = tomopy.recon(sim, ang, algorithm=tomopy.astra, >>> options={'method':'SART', 'num_iter':10*180, >>> 'proj_type':'linear', >>> 'extra_options':{'MinConstraint':0}}) >>> >>> # Show 64th slice of the reconstructed object. >>> import pylab >>> pylab.imshow(rec[64], cmap='gray') >>> pylab.show() """ # Initialize tomography data. tomo = init_tomo(tomo, sinogram_order, sharedmem=False) generic_kwargs = ['num_gridx', 'num_gridy', 'options'] # Generate kwargs for the algorithm. kwargs_defaults = _get_algorithm_kwargs(tomo.shape) if isinstance(algorithm, six.string_types): allowed_kwargs = copy.copy(allowed_recon_kwargs) if algorithm in allowed_accelerated_kwargs: allowed_kwargs[algorithm] += allowed_accelerated_kwargs[algorithm] # Check whether we have an allowed method if algorithm not in allowed_kwargs: raise ValueError( 'Keyword "algorithm" must be one of %s, or a Python method.' % (list(allowed_kwargs.keys()), )) # Make sure have allowed kwargs appropriate for algorithm. for key, value in list(kwargs.items()): if key not in allowed_kwargs[algorithm]: raise ValueError('%s keyword not in allowed keywords %s' % (key, allowed_kwargs[algorithm])) else: # Make sure they are numpy arrays. if not isinstance(kwargs[key], (np.ndarray, np.generic)) and not isinstance( kwargs[key], six.string_types): kwargs[key] = np.array(value) # Make sure reg_par and filter_par is float32. if key == 'reg_par' or key == 'filter_par': if not isinstance(kwargs[key], np.float32): kwargs[key] = np.array(value, dtype='float32') # Set kwarg defaults. for kw in allowed_kwargs[algorithm]: kwargs.setdefault(kw, kwargs_defaults[kw]) elif hasattr(algorithm, '__call__'): # Set kwarg defaults. for kw in generic_kwargs: kwargs.setdefault(kw, kwargs_defaults[kw]) else: raise ValueError( 'Keyword "algorithm" must be one of %s, or a Python method.' % (list(allowed_recon_kwargs.keys()), )) # Generate args for the algorithm. center_arr = get_center(tomo.shape, center) args = _get_algorithm_args(theta) # Initialize reconstruction. recon_shape = (tomo.shape[0], kwargs['num_gridx'], kwargs['num_gridy']) recon = _init_recon(recon_shape, init_recon, sharedmem=False) return _dist_recon(tomo, center_arr, recon, _get_func(algorithm), args, kwargs, ncore, nchunk)