def transform_scalars(dataset, SHIFT=None, rotation_angle=90.0): from tomviz import utils from scipy import ndimage import numpy as np data_py = utils.get_array(dataset) # Get data as numpy array. if data_py is None: #Check if data exists raise RuntimeError("No data array found!") if SHIFT is None: SHIFT = np.zeros(len(data_py.shape), dtype=np.int) data_py_return = np.empty_like(data_py) ndimage.interpolation.shift(data_py, SHIFT, order=0, output=data_py_return) rotation_axis = 2 # This operator always assumes the rotation axis is Z if rotation_angle == []: # If tilt angle not given, assign it to 90 degrees. rotation_angle = 90 axis1 = (rotation_axis + 1) % 3 axis2 = (rotation_axis + 2) % 3 axes = (axis1, axis2) shape = utils.rotate_shape(data_py_return, rotation_angle, axes=axes) data_py_return2 = np.empty(shape, data_py_return.dtype, order='F') ndimage.interpolation.rotate(data_py_return, rotation_angle, output=data_py_return2, axes=axes) utils.set_array(dataset, data_py_return2)
def transform_scalars(dataset, rotation_angle=90.0, rotation_axis=0): from tomviz import utils import numpy as np from scipy import ndimage data_py = utils.get_array(dataset) #get data as numpy array if data_py is None: #Check if data exists raise RuntimeError("No data array found!") if rotation_axis == []: #If tilt axis is not given, assign one. # Find the smallest array dimension, assume it is the tilt angle axis. if data_py.ndim >= 2: rotation_axis = np.argmin(data_py.shape) else: raise RuntimeError("Data Array is not 2 or 3 dimensions!") if rotation_angle == []: # If tilt angle not given, assign it to 90 degrees. rotation_angle = 90 print('Rotating Dataset...') axis1 = (rotation_axis + 1) % 3 axis2 = (rotation_axis + 2) % 3 axes = (axis1, axis2) shape = utils.rotate_shape(data_py, rotation_angle, axes=axes) data_py_return = np.empty(shape, data_py.dtype, order='F') ndimage.interpolation.rotate(data_py, rotation_angle, output=data_py_return, axes=axes) utils.set_array(dataset, data_py_return) print('Rotation Complete')
def transform_scalars(dataset, SHIFT=None, rotation_angle=90.0): from tomviz import utils from scipy import ndimage import numpy as np data_py = utils.get_array(dataset) # Get data as numpy array. if data_py is None: #Check if data exists raise RuntimeError("No data array found!") if SHIFT is None: SHIFT = np.zeros(len(data_py.shape), dtype=np.int) data_py_return = np.empty_like(data_py) ndimage.interpolation.shift(data_py, SHIFT, order=0, output=data_py_return) rotation_axis = 2 # This operator always assumes the rotation axis is Z if rotation_angle == []: # If tilt angle not given, assign it to 90 degrees. rotation_angle = 90 axis1 = (rotation_axis + 1) % 3 axis2 = (rotation_axis + 2) % 3 axes = (axis1, axis2) shape = utils.rotate_shape(data_py_return, rotation_angle, axes=axes) data_py_return2 = np.empty(shape, data_py_return.dtype, order='F') ndimage.interpolation.rotate( data_py_return, rotation_angle, output=data_py_return2, axes=axes) utils.set_array(dataset, data_py_return2)
def transform_scalars(self, dataset): """ Automatic align the tilt axis to horizontal direction (x-axis) """ self.progress.maximum = 1 # Get Tilt Series tiltSeries = utils.get_array(dataset) if tiltSeries is None: #Check if data exists raise RuntimeError("No data array found!") (Nslice, Nray, Nproj) = tiltSeries.shape Intensity = np.zeros(tiltSeries.shape) self.progress.maximum = Nproj + 131 #coarse(90)+fine(41) step = 0 self.progress.message = 'Initialization' #take FFT of all projections for i in range(Nproj): self.progress.message = ('Taking Fourier transform of tilt image' 'No.%d/%d' % (i + 1, Nproj)) tiltImage = tiltSeries[:, :, i] tiltImage_F = np.abs(np.fft.fft2(tiltImage)) if (i == 0): temp = tiltImage_F[0, 0] Intensity[:, :, i] = np.fft.fftshift(tiltImage_F / temp) step += 1 self.progress.value = step #rescale intensity Intensity = np.power(Intensity, 0.2) #calculate variation itensity image Intensity_var = np.var(Intensity, axis=2) #search angles coarseStep = 2 fineStep = 0.1 coarseAngles = np.arange(-90, 90, coarseStep) Nx = Intensity_var.shape[0] Ny = Intensity_var.shape[1] N = np.round(np.min([Nx, Ny]) // 3) #coarse search I = np.zeros((coarseAngles.size, N)) for a in range(coarseAngles.size): if self.canceled: return self.progress.message = ('Calculating line intensity at angle %f' 'degree' % (coarseAngles[a])) I[a, :] = calculateLineIntensity(Intensity_var, coarseAngles[a], N) step += 1 self.progress.value = step I_sum = np.sum(I, axis=1) minIntensityIndex = np.argmin(I_sum) rot_ang = coarseAngles[minIntensityIndex] #now refine fineAngles = np.arange(rot_ang - coarseStep, rot_ang + coarseStep + fineStep, fineStep) #fine search I = np.zeros((fineAngles.size, N)) for a in range(fineAngles.size): if self.canceled: return self.progress.message = ('Calculating line intensity at angle %f' 'degree' % (fineAngles[a])) I[a, :] = calculateLineIntensity(Intensity_var, fineAngles[a], N) step += 1 self.progress.value = step I_sum = np.sum(I, axis=1) minIntensityIndex = np.argmin(I_sum) rot_ang = fineAngles[minIntensityIndex] self.progress.message = 'Rotating tilt series' axes = ((0, 1)) shape = utils.rotate_shape(tiltSeries, -rot_ang, axes=axes) result = np.empty(shape, tiltSeries.dtype, order='F') ndimage.interpolation.rotate( tiltSeries, -rot_ang, axes=axes, output=result) print("rotate tilt series by %f degrees" % -rot_ang) # Set the result as the new scalars. utils.set_array(dataset, result)
def transform_scalars(self, dataset): """ Automatic align the tilt axis to horizontal direction (x-axis) """ self.progress.maximum = 1 # Get Tilt Series tiltSeries = utils.get_array(dataset) if tiltSeries is None: #Check if data exists raise RuntimeError("No data array found!") (Nslice, Nray, Nproj) = tiltSeries.shape Intensity = np.zeros(tiltSeries.shape) self.progress.maximum = Nproj + 131 #coarse(90)+fine(41) step = 0 self.progress.message = 'Initialization' #take FFT of all projections for i in range(Nproj): self.progress.message = ('Taking Fourier transform of tilt image' 'No.%d/%d' % (i + 1, Nproj)) tiltImage = tiltSeries[:, :, i] tiltImage_F = np.abs(np.fft.fft2(tiltImage)) if (i == 0): temp = tiltImage_F[0, 0] Intensity[:, :, i] = np.fft.fftshift(tiltImage_F / temp) step += 1 self.progress.value = step #rescale intensity Intensity = np.power(Intensity, 0.2) #calculate variation itensity image Intensity_var = np.var(Intensity, axis=2) #search angles coarseStep = 2 fineStep = 0.1 coarseAngles = np.arange(-90, 90, coarseStep) Nx = Intensity_var.shape[0] Ny = Intensity_var.shape[1] N = np.round(np.min([Nx, Ny]) // 3) #coarse search I = np.zeros((coarseAngles.size, N)) for a in range(coarseAngles.size): if self.canceled: return self.progress.message = ('Calculating line intensity at angle %f' 'degree' % (coarseAngles[a])) I[a, :] = calculateLineIntensity(Intensity_var, coarseAngles[a], N) step += 1 self.progress.value = step I_sum = np.sum(I, axis=1) minIntensityIndex = np.argmin(I_sum) rot_ang = coarseAngles[minIntensityIndex] #now refine fineAngles = np.arange(rot_ang - coarseStep, rot_ang + coarseStep + fineStep, fineStep) #fine search I = np.zeros((fineAngles.size, N)) for a in range(fineAngles.size): if self.canceled: return self.progress.message = ('Calculating line intensity at angle %f' 'degree' % (fineAngles[a])) I[a, :] = calculateLineIntensity(Intensity_var, fineAngles[a], N) step += 1 self.progress.value = step I_sum = np.sum(I, axis=1) minIntensityIndex = np.argmin(I_sum) rot_ang = fineAngles[minIntensityIndex] self.progress.message = 'Rotating tilt series' axes = ((0, 1)) shape = utils.rotate_shape(tiltSeries, -rot_ang, axes=axes) result = np.empty(shape, tiltSeries.dtype, order='F') ndimage.interpolation.rotate(tiltSeries, -rot_ang, axes=axes, output=result) print("rotate tilt series by %f degrees" % -rot_ang) # Set the result as the new scalars. utils.set_array(dataset, result)