def transform_scalars(dataset, resampling_factor=[1, 1, 1]): """Resample dataset""" from tomviz import utils import scipy.ndimage import numpy as np array = utils.get_array(dataset) # Transform the dataset. result_shape = utils.zoom_shape(array, resampling_factor) result = np.empty(result_shape, array.dtype, order='F') scipy.ndimage.interpolation.zoom(array, resampling_factor, output=result) # Set the result as the new scalars. utils.set_array(dataset, result) # Update tilt angles if dataset is a tilt series. if resampling_factor[2] != 1: try: tilt_angles = utils.get_tilt_angles(dataset) result_shape = utils.zoom_shape(tilt_angles, resampling_factor[2]) result = np.empty(result_shape, array.dtype, order='F') scipy.ndimage.interpolation.zoom( tilt_angles, resampling_factor[2], output=result) utils.set_tilt_angles(dataset, result) except: # noqa # TODO What exception are we ignoring? pass
def transform_scalars(dataset): """Downsample volume by a factor of 2""" from tomviz import utils import scipy.ndimage import numpy as np array = utils.get_array(dataset) # Downsample the dataset x2 using order 1 spline (linear) # Calculate out array shape zoom = (0.5, 0.5, 0.5) result_shape = utils.zoom_shape(array, zoom) result = np.empty(result_shape, array.dtype, order='F') scipy.ndimage.interpolation.zoom(array, zoom, output=result, order=1, mode='constant', cval=0.0, prefilter=False) # Set the result as the new scalars. utils.set_array(dataset, result) # Update tilt angles if dataset is a tilt series. try: tilt_angles = utils.get_tilt_angles(dataset) result_shape = utils.zoom_shape(tilt_angles, 0.5) result = np.empty(result_shape, array.dtype, order='F') tilt_angles = scipy.ndimage.interpolation.zoom(tilt_angles, 0.5, output=result) utils.set_tilt_angles(dataset, result) except: # noqa # TODO What exception are we ignoring? pass
def transform_scalars(dataset): """Resample dataset""" from tomviz import utils import numpy as np import scipy.ndimage #----USER SPECIFIED VARIABLES-----# #resampingFactor = [1,1,1] #Specify the shifts (x,y,z) applied to data ###resampingFactor### #---------------------------------# array = utils.get_array(dataset) # transform the dataset result = scipy.ndimage.interpolation.zoom(array, resampingFactor) # set the result as the new scalars. utils.set_array(dataset, result) #Update tilt angles if dataset is a tilt series if resampingFactor[2] != 1: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = scipy.ndimage.interpolation.zoom(tilt_angles, resampingFactor[2]) utils.set_tilt_angles(dataset, tilt_angles) except: pass
def _execute_transform(operator_label, transform, arguments, input, progress): # Update the progress attribute to an instance that will work outside the # application and give use a nice progress information. spinner = None supports_progress = hasattr(transform, '__self__') if supports_progress: transform.__self__.progress = progress # Stub out the operator wrapper transform.__self__._operator_wrapper = OperatorWrapper() logger.info('Executing \'%s\' operator' % operator_label) if not supports_progress: print('Operator doesn\'t support progress updates.') result = None # Special case for SetTiltAngles if operator_label == 'SetTiltAngles': # Set the tilt angles so downstream operator can retrieve them # arguments the tilt angles. utils.set_tilt_angles(input, np.array(arguments['angles'], dtype=np.float64)) else: # Now run the operator result = transform(input, **arguments) if spinner is not None: update_spinner.cancel() spinner.finish() logger.info('Execution complete.') return result
def transform_scalars(dataset): """Delete Slices in Dataset""" from tomviz import utils import numpy as np axis = 0; #----USER SPECIFIED VARIABLES-----# ###firstSlice### ###lastSlice### ###axis### #Axis along which to delete the subarray #---------------------------------# #get current dataset array = utils.get_array(dataset) #Get indices of the slices to be deleted indices = np.linspace(firstSlice,lastSlice,lastSlice-firstSlice+1).astype(int) # delete slices array = np.delete(array,indices,axis) #set the result as the new scalars. utils.set_array(dataset, array) #Delete corresponding tilt anlges if dataset is a tilt series if axis == 2: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = np.delete(tilt_angles,indices) utils.set_tilt_angles(dataset, tilt_angles) except: pass
def transform_scalars(dataset, resampling_factor=[1, 1, 1]): """Resample dataset""" from tomviz import utils import scipy.ndimage import numpy as np array = utils.get_array(dataset) # Transform the dataset. result_shape = utils.zoom_shape(array, resampling_factor) result = np.empty(result_shape, array.dtype, order='F') scipy.ndimage.interpolation.zoom(array, resampling_factor, output=result) # Set the result as the new scalars. utils.set_array(dataset, result) # Update tilt angles if dataset is a tilt series. if resampling_factor[2] != 1: try: tilt_angles = utils.get_tilt_angles(dataset) result_shape = utils.zoom_shape(tilt_angles, resampling_factor[2]) result = np.empty(result_shape, array.dtype, order='F') scipy.ndimage.interpolation.zoom(tilt_angles, resampling_factor[2], output=result) utils.set_tilt_angles(dataset, result) except: # noqa # TODO What exception are we ignoring? pass
def transform_scalars(dataset): """Generate Tilt Series from Volume""" from tomviz import utils import numpy as np import scipy.ndimage #----USER SPECIFIED VARIABLES-----# ###startAngle### #Starting angle ###angleIncrement### #Angle increment ###Nproj### #Number of tilts #---------------------------------# # Generate Tilt Angles angles = np.linspace(startAngle,startAngle+(Nproj-1)*angleIncrement,Nproj); array = utils.get_array(dataset) N = array.shape[0]; Nslice = array.shape[2]; #Number of slices along rotation axis tiltSeries = np.zeros((Nslice, N ,Nproj)) for i in range(Nproj): #Rotate volume rotatedArray = scipy.ndimage.interpolation.rotate(array, angles[i],axes=(0,1),reshape=False) #Calculate Projection tiltSeries[:,:,i] = np.sum(rotatedArray,axis=0).transpose() # set the result as the new scalars. utils.set_array(dataset, tiltSeries) # Mark dataset as tilt series utils.mark_as_tiltseries(dataset) # Save tilt angles utils.set_tilt_angles(dataset, angles)
def transform_scalars(dataset): """Downsample volume by a factor of 2""" from tomviz import utils import scipy.ndimage array = utils.get_array(dataset) # Downsample the dataset x2 using order 1 spline (linear) result = scipy.ndimage.interpolation.zoom(array, (0.5, 0.5, 0.5), output=None, order=1, mode='constant', cval=0.0, prefilter=False) # Set the result as the new scalars. utils.set_array(dataset, result) # Update tilt angles if dataset is a tilt series. try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = scipy.ndimage.interpolation.zoom(tilt_angles, 0.5) utils.set_tilt_angles(dataset, tilt_angles) except: # noqa # TODO What exception are we ignoring? pass
def transform_scalars(dataset, firstSlice=None, lastSlice=None, axis=2): """Delete Slices in Dataset""" from tomviz import utils import numpy as np # Get the current dataset. array = utils.get_array(dataset) # Get indices of the slices to be deleted. indices = np.linspace(firstSlice, lastSlice, lastSlice - firstSlice + 1).astype(int) # Delete the specified slices. array = np.delete(array, indices, axis) # Set the result as the new scalars. utils.set_array(dataset, array) # Delete corresponding tilt anlges if dataset is a tilt series. if axis == 2: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = np.delete(tilt_angles, indices) utils.set_tilt_angles(dataset, tilt_angles) except: # noqa # TODO what exception are we ignoring here? pass
def transform_scalars(dataset): """Pad dataset""" from tomviz import utils import numpy as np #----USER SPECIFIED VARIABLES-----# ###padWidthX### ###padWidthY### ###padWidthZ### ###padMode_index### #---------------------------------# padModes = ['constant','edge','wrap','minimum','median'] padMode = padModes[padMode_index] array = utils.get_array(dataset) #get data as numpy array if array is None: #Check if data exists raise RuntimeError("No data array found!") pad_width = (padWidthX,padWidthY,padWidthZ) # pad the data. array = np.lib.pad(array, pad_width, padMode) # Set the data so that it is visible in the application. utils.set_array(dataset, array) # If dataset is marked as tilt series, update tilt angles if padWidthZ[0]+padWidthZ[1] >0: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = np.lib.pad(tilt_angles,padWidthZ, padMode) utils.set_tilt_angles(dataset, tilt_angles) except: pass
def transform_scalars(dataset): """Delete Slices in Dataset""" from tomviz import utils import numpy as np axis = 0; #----USER SPECIFIED VARIABLES-----# ###firstSlice### ###lastSlice### ###axis### #Axis along which to delete the subarray #---------------------------------# # Get the current dataset. array = utils.get_array(dataset) # Get indices of the slices to be deleted. indices = np.linspace(firstSlice,lastSlice,lastSlice-firstSlice+1).astype(int) # Delete the specified slices. array = np.delete(array,indices,axis) # Set the result as the new scalars. utils.set_array(dataset, array) # Delete corresponding tilt anlges if dataset is a tilt series. if axis == 2: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = np.delete(tilt_angles,indices) utils.set_tilt_angles(dataset, tilt_angles) except: pass
def transform_scalars(dataset): """Resample dataset""" from tomviz import utils import numpy as np import scipy.ndimage #----USER SPECIFIED VARIABLES-----# #resampingFactor = [1,1,1] #Specify the shifts (x,y,z) applied to data ###resampingFactor### #---------------------------------# array = utils.get_array(dataset) # Transform the dataset. result = scipy.ndimage.interpolation.zoom(array, resampingFactor) # Set the result as the new scalars. utils.set_array(dataset, result) # Update tilt angles if dataset is a tilt series. if resampingFactor[2] != 1: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = scipy.ndimage.interpolation.zoom(tilt_angles, resampingFactor[2]) utils.set_tilt_angles(dataset, tilt_angles) except: pass
def transform_scalars(dataset): """Generate Tilt Series from Volume""" from tomviz import utils import numpy as np import scipy.ndimage #----USER SPECIFIED VARIABLES-----# ###startAngle### #Starting angle ###angleIncrement### #Angle increment ###Nproj### #Number of tilts #---------------------------------# # Generate Tilt Angles. angles = np.linspace(startAngle,startAngle+(Nproj-1)*angleIncrement,Nproj); array = utils.get_array(dataset) N = array.shape[0]; Nslice = array.shape[2]; # Number of slices along rotation axis. tiltSeries = np.zeros((Nslice, N ,Nproj)) for i in range(Nproj): # Rotate the volume. rotatedArray = scipy.ndimage.interpolation.rotate(array, angles[i],axes=(0,1),reshape=False) # Calculate projection. tiltSeries[:,:,i] = np.sum(rotatedArray,axis=0).transpose() # Set the result as the new scalars. utils.set_array(dataset, tiltSeries) # Mark dataset as tilt series. utils.mark_as_tiltseries(dataset) # Save tilt angles. utils.set_tilt_angles(dataset, angles)
def transform_scalars(dataset, pad_size_before=[0, 0, 0], pad_size_after=[0, 0, 0], pad_mode_index=0): """Pad dataset""" from tomviz import utils import numpy as np padModes = ['constant', 'edge', 'wrap', 'minimum', 'median'] padMode = padModes[pad_mode_index] array = utils.get_array(dataset) #get data as numpy array if array is None: #Check if data exists raise RuntimeError("No data array found!") padWidthX = (pad_size_before[0], pad_size_after[0]) padWidthY = (pad_size_before[1], pad_size_after[1]) padWidthZ = (pad_size_before[2], pad_size_after[2]) pad_width = (padWidthX, padWidthY, padWidthZ) result_shape = np.zeros(3, dtype=int) result_shape[0] = array.shape[0] + pad_size_before[0] + pad_size_after[0] result_shape[1] = array.shape[1] + pad_size_before[1] + pad_size_after[1] result_shape[2] = array.shape[2] + pad_size_before[2] + pad_size_after[2] result = np.empty(result_shape, array.dtype, order='F') # pad the data. result[:] = np.lib.pad(array, pad_width, padMode) # Set the data so that it is visible in the application. extent = list(dataset.GetExtent()) start = [x - y for (x, y) in zip(extent[0::2], pad_size_before)] utils.set_array(dataset, result, start) # If dataset is marked as tilt series, update tilt angles if padWidthZ[0] + padWidthZ[1] > 0: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = np.lib.pad(tilt_angles, padWidthZ, padMode) utils.set_tilt_angles(dataset, tilt_angles) except: # noqa # TODO What exception are we ignoring? pass
def transform_scalars(dataset): """Downsample volume by a factor of 2""" from tomviz import utils import scipy.ndimage import numpy as np array = utils.get_array(dataset) # Downsample the dataset x2 using order 1 spline (linear) # Calculate out array shape def dimensions(array): if array.ndim == 4: zoom = (0.5,0.5,0.5,1) else: zoom = (0.5, 0.5, 0.5) return zoom zoom = dimensions(array) result_shape = utils.zoom_shape(array, zoom) result = np.empty(result_shape, array.dtype, order='F') scipy.ndimage.interpolation.zoom(array, zoom, output=result, order=1, mode='constant', cval=0.0, prefilter=False) # Set the result as the new scalars. utils.set_array(dataset, result) # Update tilt angles if dataset is a tilt series. try: tilt_angles = utils.get_tilt_angles(dataset) result_shape = utils.zoom_shape(tilt_angles, 0.5) result = np.empty(result_shape, array.dtype, order='F') tilt_angles = scipy.ndimage.interpolation.zoom(tilt_angles, 0.5, output=result) utils.set_tilt_angles(dataset, result) except: # noqa # TODO What exception are we ignoring? pass
def transform_scalars(dataset, resampling_factor=[1, 1, 1]): """Resample dataset""" from tomviz import utils import scipy.ndimage array = utils.get_array(dataset) # Transform the dataset. result = scipy.ndimage.interpolation.zoom(array, resampling_factor) # Set the result as the new scalars. utils.set_array(dataset, result) # Update tilt angles if dataset is a tilt series. if resampling_factor[2] != 1: try: tilt_angles = utils.get_tilt_angles(dataset) tilt_angles = scipy.ndimage.interpolation.zoom( tilt_angles, resampling_factor[2]) utils.set_tilt_angles(dataset, tilt_angles) except: # noqa # TODO What exception are we ignoring? pass
def tilt_angles(self, v): utils.set_tilt_angles(self._data_object, v)
def transform_scalars(self, dataset, start_angle=-90.0, angle_increment=3.0, num_tilts=60): """Generate Tilt Series from Volume""" self.progress.maximum = 1 self.progress.message = 'Initialization' # Generate Tilt Angles. angles = np.linspace(start_angle, start_angle + (num_tilts - 1) * angle_increment, num_tilts) volume = utils.get_array(dataset) Ny = volume.shape[1] Nz = volume.shape[2] # calculate the size s.t. it contains the entire volume N = np.round(np.sqrt(Ny**2 + Nz**2)) N = int(np.floor(N / 2.0) * 2 + 1) # make the size an odd integer # pad volume pad_y_pre = int(np.ceil((N - Ny) / 2.0)) pad_y_post = int(np.floor((N - Ny) / 2.0)) pad_z_pre = int(np.ceil((N - Nz) / 2.0)) pad_z_post = int(np.floor((N - Nz) / 2.0)) volume_pad = np.lib.pad(volume, ((0, 0), (pad_y_pre, pad_y_post), (pad_z_pre, pad_z_post)), 'constant') Nslice = volume.shape[0] # Number of slices along rotation axis. tiltSeries = np.zeros((Nslice, N, num_tilts)) self.progress.maximum = num_tilts step = 0 for i in range(num_tilts): if self.canceled: return self.progress.message = 'Generating tilt image No.%d/%d' % ( i + 1, num_tilts) # Rotate volume about x-axis rotatedVolume = scipy.ndimage.interpolation.rotate(volume_pad, angles[i], axes=(1, 2), reshape=False, order=1) # Calculate projection tiltSeries[:, :, i] = np.sum(rotatedVolume, axis=2) step += 1 self.progress.value = step # Set the result as the new scalars. utils.set_array(dataset, tiltSeries) # Mark dataset as tilt series. utils.mark_as_tiltseries(dataset) # Save tilt angles. utils.set_tilt_angles(dataset, angles)