def convolute(v, k, kernel_in_fourier=False): """ @param v: the volume to be convolute @type v: L{pytom_volume.vol} @param k: the convolution kernel in real space @type k: L{pytom_volume.vol} @param kernel_in_fourier: the given kernel is already in Fourier space or not (full size, shifted in the center! \ or reduced size). Default is False. @type kernel_in_fourier: L{bool} @return: Volume convoluted with kernel @rtype: L{pytom_volume.vol} """ fv = fft(v) if not kernel_in_fourier: fk = fft(k) res = fv*fk else: from pytom_volume import complexRealMult, fullToReduced if k.sizeZ() == k.sizeX(): fk = fullToReduced(ftshift(k, inplace=False)) else: fk = k res = complexRealMult(fv, fk) out = ifft(res) out.shiftscale(0.0,1/float(out.sizeX()*out.sizeY()*out.sizeZ())) return out
def create_average(self, sum_ctf_conv, sum_ctf_squared, wedge_weight): """For the master node, this function is rewritten. """ from pytom_volume import vol, complexDiv, fullToReduced, initSphere, complexRealMult, limit from pytom.basic.fourier import fft, ifft, ftshift from pytom.basic.normalise import mean0std1 # limit(wedge_weight, 0.1, 0, 0,0,True,False) # set all the values below the specified value to 0 # for mask out the outside area # mask = vol(sum_ctf_conv) # mask.setAll(0) # initSphere(mask, sum_ctf_conv.sizeX()/2-1, 0,0, sum_ctf_conv.sizeX()/2, sum_ctf_conv.sizeX()/2, sum_ctf_conv.sizeX()/2) # mask = fullToReduced(ftshift(mask, inplace=False)) # Wiener filter numerator = fft(sum_ctf_conv) sum_ctf_squared = fullToReduced(ftshift(sum_ctf_squared, inplace=False)) denominator = (sum_ctf_squared+1)*wedge_weight r = complexDiv(numerator, denominator) # average = ifft(complexRealMult(r, mask)) average = ifft(r) average.shiftscale(0.0,1/float(average.sizeX()*average.sizeY()*average.sizeZ())) # nomalize the average try: average = mean0std1(average, True) except: average *= 1000 # in case the average volume is too small to normalize average = mean0std1(average, True) return average
def rotateWeighting(weighting, z1, z2, x, mask=None, isReducedComplex=None, returnReducedComplex=False, binarize=False): """ rotateWeighting: Rotates a frequency weighting volume around the center. If the volume provided is reduced complex, it will be rescaled to full size, ftshifted, rotated, iftshifted and scaled back to reduced size. @param weighting: A weighting volume @type weighting: L{pytom_volume.vol} @param z1: Z1 rotation angle @type z1: float @param z2: Z2 rotation angle @type z2: float @param x: X rotation angle @type x: float @param mask:=None is there a rotation mask? A mask with all = 1 will be generated otherwise. Such mask should be \ provided anyway. @type mask: L{pytom_volume.vol} @param isReducedComplex: Either set to True or False. Will be determined otherwise @type isReducedComplex: bool @param returnReducedComplex: Return as reduced complex? (Default is False) @type returnReducedComplex: bool @param binarize: binarize weighting @type binarize: bool @return: weight as reduced complex volume @rtype: L{pytom_volume.vol_comp} """ from pytom_volume import vol, limit, vol_comp from pytom_volume import rotate assert type(weighting) == vol or type(weighting) == vol_comp, "rotateWeighting: input neither vol nor vol_comp" isReducedComplex = isReducedComplex or int(weighting.sizeX()/2)+1 == weighting.sizeZ(); if isReducedComplex: #scale weighting to full size from pytom_fftplan import fftShift from pytom_volume import reducedToFull weighting = reducedToFull(weighting) fftShift(weighting, True) if not mask: mask = vol(weighting.sizeX(),weighting.sizeY(),weighting.sizeZ()) mask.setAll(1) weightingRotated = vol(weighting.sizeX(),weighting.sizeY(),weighting.sizeZ()) rotate(weighting,weightingRotated,z1,z2,x) weightingRotated = weightingRotated * mask if returnReducedComplex: from pytom_fftplan import fftShift from pytom_volume import fullToReduced fftShift(weightingRotated,True) returnVolume = fullToReduced(weightingRotated) else: returnVolume = weightingRotated if binarize: limit(returnVolume,0.5,0,0.5,1,True,True) return returnVolume
def shift(volume,shiftX,shiftY,shiftZ,imethod='fourier',twice=False): """ shift: Performs a shift on a volume @param volume: the volume @param shiftX: shift in x direction @param shiftY: shift in y direction @param shiftZ: shift in z direction @param imethod: Select interpolation method. Real space : linear, cubic, spline . Fourier space: fourier @param twice: Zero pad volume into a twice sized volume and perform calculation there. @return: The shifted volume. @author: Yuxiang Chen and Thomas Hrabe """ if imethod == 'fourier': from pytom_volume import vol_comp,reducedToFull,fullToReduced,shiftFourier from pytom.basic.fourier import fft,ifft,ftshift, iftshift fvolume = fft(volume) fullFVolume = reducedToFull(fvolume) destFourier = vol_comp(fullFVolume.sizeX(),fullFVolume.sizeY(),fullFVolume.sizeZ()) shiftFourier(fullFVolume,destFourier,shiftX,shiftY,shiftZ) resFourier = fullToReduced(destFourier) return ifft(resFourier)/volume.numelem() else: from pytom_volume import vol if imethod == 'linear': from pytom_volume import transform elif imethod == 'cubic': from pytom_volume import transformCubic as transform elif imethod == 'spline': from pytom_volume import transformSpline as transform # now results should be consistent with python2 centerX = int(volume.sizeX()/2) centerY = int(volume.sizeY()/2) centerZ = int(volume.sizeZ()/2) res = vol(volume.sizeX(),volume.sizeY(),volume.sizeZ()) transform(volume,res,0,0,0,centerX,centerY,centerZ,shiftX,shiftY,shiftZ,0,0,0) return res