def main(): """ Read a selected APR from file and apply Richardson-Lucy deconvolution """ # Get input APR file path from gui io_int = pyapr.filegui.InteractiveIO() fpath_apr = io_int.get_apr_file_name() # Instantiate APR and particles objects apr = pyapr.APR() parts = pyapr.ShortParticles() # parts = pyapr.FloatParticles() # Read from APR file pyapr.io.read(fpath_apr, apr, parts) # Copy particles to float fparts = pyapr.FloatParticles() fparts.copy(parts) # Add a small offset to the particle values to avoid division by 0 offset = 1e-5 * fparts.max() fparts += offset # Display the input image pyapr.viewer.parts_viewer(apr, fparts) # Specify the PSF and number of iterations psf = pyapr.numerics.filter.get_gaussian_stencil(size=5, sigma=1, ndims=3, normalize=True) niter = 20 # Perform richardson-lucy deconvolution output = pyapr.FloatParticles() pyapr.numerics.richardson_lucy(apr, fparts, output, psf, niter, use_stencil_downsample=True, normalize_stencil=True, resume=False) # Alternative using total variation regularization: # reg_factor = 1e-2 # pyapr.numerics.richardson_lucy_tv(apr, fparts, output, psf, niter, reg_factor, use_stencil_downsample=True, # normalize_stencil=True, resume=False) # Alternatively, if PyLibAPR is built with CUDA enabled and psf is of size (3, 3, 3) or (5, 5, 5) # pyapr.numerics.richardson_lucy_cuda(apr, fparts, output, psf, niter, use_stencil_downsample=True, # normalize_stencil=True, resume=False) # Display the result pyapr.viewer.parts_viewer(apr, output)
def setUp(self): self.numel = 113 self.float1 = pyapr.FloatParticles(self.numel) self.float2 = pyapr.FloatParticles(self.numel) self.short1 = pyapr.ShortParticles(self.numel) self.short2 = pyapr.ShortParticles(self.numel) self.float1.fill(1) self.float2.fill(1 / 3) self.short1.fill(2) self.short2.fill(3) self.eps = 1e-6
def main(): """ This demo showcases some of the available numerics functionality on a selected APR """ io_int = pyapr.filegui.InteractiveIO() fpath_apr = io_int.get_apr_file_name() # get APR file path from gui # Instantiate APR and particle objects apr = pyapr.APR() parts = pyapr.ShortParticles() # input particles can be float32 or uint16 # parts = pyapr.FloatParticles() # Read from APR file pyapr.io.read(fpath_apr, apr, parts) output = pyapr.FloatParticles() # Compute gradient along a dimension (Sobel filter). dimension can be 0, 1 or 2 pyapr.numerics.gradient(apr, parts, output, dimension=0, delta=1.0, sobel=True) pyapr.viewer.parts_viewer(apr, output) # Display the result # Compute gradient magnitude (central finite differences) pyapr.numerics.gradient_magnitude(apr, parts, output, deltas=(1.0, 1.0, 1.0), sobel=False) pyapr.viewer.parts_viewer(apr, output) # Display the result # Compute local standard deviation around each particle pyapr.numerics.local_std(apr, parts, output, size=(5, 5, 5)) pyapr.viewer.parts_viewer(apr, output) # Display the result
def reconstruct_smooth(apr: pyapr.APR, parts: (pyapr.ShortParticles, pyapr.LongParticles, pyapr.FloatParticles), tree_parts: (None, pyapr.ShortParticles, pyapr.LongParticles, pyapr.FloatParticles) = None, patch: (None, pyapr.ReconPatch) = None, out_arr: (None, np.ndarray) = None): """ Reconstruct pixel values by smooth interpolation Parameters ---------- apr : pyapr.APR input APR data structure parts : pyapr.FloatParticles or pyapr.ShortParticles input particle intensities tree_parts: None, pyapr.FloatParticles or pyapr.ShortParticles (optional) interior tree particle values used to construct at a lower resolution (if patch.level_delta < 0). If None, they are computed by average downsampling as necessary. (default: None) patch: pyapr.ReconPatch (optional) specify the image region and resolution of the reconstruction. If None, reconstruct the full image volume at original pixel resolution. (default: None) out_arr: None, np.ndarray (optional) preallocated array for the result. If the size is not correct (according to APR dimensions or patch limits), memory for the output is reallocated. (default: None) Returns ------- out_arr : numpy.ndarray the reconstructed pixel values """ if isinstance(parts, pyapr.FloatParticles): _dtype = np.float32 elif isinstance(parts, pyapr.LongParticles): _dtype = np.uint64 elif isinstance(parts, pyapr.ShortParticles): _dtype = np.uint16 else: raise ValueError('parts type not recognized') if patch is not None: if not patch.check_limits(apr): return None if out_arr is None or out_arr.size != patch.size(): out_arr = np.zeros(shape=(patch.z_end-patch.z_begin, patch.x_end-patch.x_begin, patch.y_end-patch.y_begin), dtype=_dtype) if tree_parts is None: tree_parts = pyapr.FloatParticles() reconstruct_smooth_patch_inplace(apr, parts, tree_parts, patch, out_arr) else: _shape = [apr.org_dims(2), apr.org_dims(1), apr.org_dims(0)] if out_arr is None or out_arr.size != np.prod(_shape): out_arr = np.zeros(shape=_shape, dtype=_dtype) reconstruct_smooth_inplace(apr, parts, out_arr) return out_arr
def initialize_particles_type(typestr): if typestr == 'uint16': return pyapr.ShortParticles() if typestr == 'float': return pyapr.FloatParticles() if typestr == 'uint8': return pyapr.ByteParticles() if typestr == 'uint64': return pyapr.LongParticles() print('Deducted datatype {} is currently not supported - returning None'. format(typestr)) return None
def get_apr(image, rel_error=0.1, gradient_smoothing=2, verbose=True, params=None): # check that the image array is c-contiguous if not image.flags['C_CONTIGUOUS']: print( 'WARNING: \'image\' argument given to get_apr is not C-contiguous \n' 'input image has been replaced with a C-contiguous copy of itself') image = np.ascontiguousarray(image) # Initialize objects apr = pyapr.APR() if params is None: par = pyapr.APRParameters() par.auto_parameters = True par.rel_error = rel_error par.gradient_smoothing = gradient_smoothing else: par = params if image.dtype == np.float32: parts = pyapr.FloatParticles() converter = pyapr.converter.FloatConverter() elif image.dtype == np.uint16: parts = pyapr.ShortParticles() converter = pyapr.converter.ShortConverter() # elif image.dtype in {'byte', 'uint8'}: # currently not working # parts = pyapr.ByteParticles() # converter = pyapr.converter.ByteConverter() else: errstr = 'pyapr.converter.get_apr: input image dtype must be numpy.uint16 or numpy.float32, ' \ 'but {} was given'.format(image.dtype) raise TypeError(errstr) converter.set_parameters(par) converter.set_verbose(verbose) # Compute the APR and sample particles converter.get_apr(apr, image) parts.sample_image(apr, image) return apr, parts
def main(): """ This demo reads an APR, applies a convolution operation and displays the result """ io_int = pyapr.filegui.InteractiveIO() fpath_apr = io_int.get_apr_file_name() # get APR file path from gui # Instantiate APR and particle objects apr = pyapr.APR() parts = pyapr.ShortParticles() # input particles can be float32 or uint16 # parts = pyapr.FloatParticles() # Read from APR file pyapr.io.read(fpath_apr, apr, parts) # Stencil and output must be float32 stencil = pyapr.numerics.filter.get_gaussian_stencil(size=5, sigma=1, ndims=3, normalize=True) out = pyapr.FloatParticles() # Convolve using CPU: pyapr.numerics.convolve(apr, parts, out, stencil, use_stencil_downsample=True, normalize_stencil=True, use_reflective_boundary=False) # Alternative CPU convolution method (produces the same result as 'convolve'): # pyapr.numerics.convolve_pencil(apr, parts, out, stencil, use_stencil_downsample=True, # normalize_stencil=True, use_reflective_boundary=False) # Convolve using GPU (stencil must be of shape 3x3x3 or 5x5x5): # pyapr.numerics.convolve_cuda(apr, parts, out, stencil, use_stencil_downsample=True, # normalize_stencil=True, use_reflective_boundary=False) # Display the result pyapr.viewer.parts_viewer(apr, out)
def raycast_viewer(apr, parts): # Raycast viewer currently only works for ShortParticles if isinstance(parts, pyapr.FloatParticles): print( 'Warning: raycast_viewer is currently only implemented for ShortParticles. ' 'Using a uint16 copy of the input particles.') parts_short = pyapr.ShortParticles() parts_short.copy(parts) else: parts_short = parts pg.setConfigOption('background', 'w') pg.setConfigOption('foreground', 'k') pg.setConfigOption('imageAxisOrder', 'row-major') app = QtGui.QApplication.instance() if app is None: app = QtGui.QApplication([]) # Create window with GraphicsView widget win = MainWindowImage() win.show() win.apr_ref = apr win.app_ref = app raycaster = pyapr.viewer.raycaster() raycaster.set_z_anisotropy(1) raycaster.set_radius(0.1) win.view.setMouseEnabled(False, False) win.raycaster_ref = raycaster win.raycaster_ref.set_angle(win.current_theta) win.raycaster_ref.set_phi(win.current_phi) tree_parts = pyapr.FloatParticles() pyapr.viewer.get_down_sample_parts(apr, parts, tree_parts) win.set_image(apr, parts_short, tree_parts) app.exec_()
def get_apr_interactive(image, rel_error=0.1, gradient_smoothing=2, verbose=True, params=None, slider_decimals=1): # check that the image array is c-contiguous if not image.flags['C_CONTIGUOUS']: print( 'WARNING: \'image\' argument given to get_apr_interactive is not C-contiguous \n' 'input image has been replaced with a C-contiguous copy of itself') image = np.ascontiguousarray(image) while image.ndim < 3: image = np.expand_dims(image, axis=0) # Initialize objects io_int = pyapr.InteractiveIO() apr = pyapr.APR() if params is None: par = pyapr.APRParameters() par.auto_parameters = False par.rel_error = rel_error par.gradient_smoothing = gradient_smoothing else: par = params if image.dtype == np.float32: parts = pyapr.FloatParticles() converter = pyapr.converter.FloatConverter() elif image.dtype == np.uint16: parts = pyapr.ShortParticles() converter = pyapr.converter.ShortConverter() # elif image.dtype in {'byte', 'uint8'}: # currently not working # parts = pyapr.ByteParticles() # converter = pyapr.converter.ByteConverter() else: errstr = 'pyapr.converter.get_apr_interactive: input image dtype must be numpy.uint16 or numpy.float32, ' \ 'but {} was given'.format(image.dtype) raise TypeError(errstr) converter.set_parameters(par) converter.set_verbose(verbose) # launch interactive APR converter io_int.interactive_apr(converter, apr, image, slider_decimals=slider_decimals) if verbose: print("Total number of particles: {}".format( apr.total_number_particles())) print("Number of pixels in original image: {}".format(image.size)) cr = image.size / apr.total_number_particles() print("Compuational Ratio: {:7.2f}".format(cr)) # sample particles parts.sample_image(apr, image) return apr, parts