def nXcf(volume,template,mask=None, stdV=None): """ nXCF: returns the normalised cross correlation function. Autocorrelation of two equal objects would yield a max nxcf peak of 1. @param volume: The search volume @param template: The template searched (this one will be used for conjugate complex multiplication) @type template: L{pytom_volume.vol} @param mask: template mask. If not given, a default sphere mask will be generated which has the same size with the given template. @type mask: L{pytom_volume.vol} @param stdV: Will be unused, only for compatibility reasons with FLCF @return: the calculated nXcf volume @rtype: L{pytom_volume.vol} @author: Thomas Hrabe @change: masking of template implemented """ from pytom.basic.normalise import mean0std1 if mask == None: result = xcf(mean0std1(volume,True),mean0std1(template,True), mask=None, stdV=None) else: from pytom.basic.normalise import normaliseUnderMask result = xcf(normaliseUnderMask(volume=volume, mask=mask, p=None)[0], normaliseUnderMask(volume=template, mask=mask, p=None)[0], mask=mask, stdV=None) #n = result.numelem() #result.shiftscale(0,1/float(n*n)) return result
def nxcc(volume, template, mask=None, volumeIsNormalized=False): """ nxcc: Calculates the normalized cross correlation coefficient in real space @param volume: A volume @type volume: L{pytom_volume.vol} @param template: A template that is searched in volume. Must be of same size as volume. @type template: L{pytom_volume.vol} @param mask: mask to constrain correlation @type mask: L{pytom_volume.vol} @param volumeIsNormalized: speed up if volume is already normalized @type volumeIsNormalized: L{bool} @return: A value between -1 and 1 @raise exception: Raises a runtime error if volume and template have a different size. @author: Thomas Hrabe @change: flag for pre-normalized volume, FF """ from pytom_volume import vol,sum,limit from pytom.tools.macros import volumesSameSize if not volumesSameSize(volume,template): raise RuntimeError('Volume and template must have same size!') if not mask: from pytom.basic.normalise import mean0std1 if not volumeIsNormalized: v = mean0std1(volume, True) t = mean0std1(template, True) p = volume.numelem() result = v*t else: from pytom_numpy import vol2npy from pytom.basic.normalise import normaliseUnderMask if not volumeIsNormalized: (v,p) = normaliseUnderMask(volume, mask) (t,p) = normaliseUnderMask(template, mask, p) t = t * mask # multiply with the mask result = v * t else: (t,p) = normaliseUnderMask(template,mask) t = t * mask # multiply with the mask result = volume * t ncc = sum(result) ncc = ncc / float(p) return ncc
def set_searchVol(self, vol1): """ set search volume (vol1 internally) @param vol1: search volume @type vol1: L{pytom_volume.vol} """ from pytom.basic.normalise import normaliseUnderMask, mean0std1 if self.mask: (self.vol1, p) = normaliseUnderMask(vol1, self.mask) else: self.vol1 = mean0std1(vol1, True)
def normalize(self, normtype="StdMeanInMask", mask=None, p=None): """ normalize image @param normtype: normalization type @type normtype: str ("StdMeanInMask", "StdMean") @param mask: mask volume @type mask: L{ptom_volume.vol} @param p: sum of gray values in mask (if pre-computed) @type p: L{int} or L{float} """ if normtype == "StdMeanInMask": from pytom.basic.normalise import normaliseUnderMask if mask == None: raise ValueError("StdMeanInMask normalization requires mask!") # spherical mask if (type(mask) == float) or (isinstance(mask, (int, long))): if mask <= 0: raise ValueError("Value for mask radius must be > 0!") from pytom.basic.functions import initSphere mask = initSphere(sizeX=self.data.sizeX(), sizeY=self.data.sizeY(), sizeZ=self.data.sizeZ(), radius=mask, smooth=mask / 10., maxradius=0, cent=None) # user-specified generic mask else: if type(mask) != vol: raise TypeError("Mask must be pytom_volume.vol") if ((mask.sizeX() != self.data.sizeX()) or (mask.sizeY() != self.data.sizeY()) or (mask.sizeZ() != self.data.sizeZ())): raise ValueError("Mask have same dimension as image") normvol, p = normaliseUnderMask(volume=self.data, mask=mask, p=p) self.data = normvol #return number of voxels in volume for speed-up return p if normtype == "StdMean": from pytom.basic.normalise import mean0std1 mean0std1(self.data) # return p for consistency return None
def __init__(self, vol1, vol2, score, mask=None, iniRot=None, iniTrans=None, opti='fmin_powell', interpolation='linear', verbose=False): """ alignment of a particle against a reference @param vol1: (constant) volume @type vol1: L{pytom_volume.vol} @param vol2: volume that is matched to reference @type vol2: L{pytom_volume.vol} @param score: score for alignment - e.g., pytom.basic.correlation.nxcc @type score: L{pytom.basic.correlation} @param mask: mask correlation is constrained on @type mask: L{pytom_volume.vol} @param iniRot: initial rotation of vol2 @type iniRot: L{pytom.basic.Rotation} @param iniTrans: initial translation of vol2 @type iniTrans: L{pytom.basic.Shift} @param opti: optimizer ('fmin_powell', 'fmin', 'fmin_cg', 'fmin_slsqp', 'fmin_bfgs') @param interpolation: interpolation type - 'linear' (default) or 'spline' @type interpolation: str @type opti: L{str} @author: FF """ from pytom.basic.normalise import normaliseUnderMask, mean0std1 from pytom.tools.macros import volumesSameSize from pytom_volume import vol from pytom.basic.structures import Rotation, Shift assert isinstance(interpolation, str), "interpolation must be of type str" self.verbose = verbose if not volumesSameSize(vol1, vol2): raise RuntimeError('Vol1 and vol2 must have same size!') # normalize constant volume if mask: (v, p) = normaliseUnderMask(vol1, mask) else: v = mean0std1(vol1, True) self.vol1 = v self.vol2 = vol2 self.rotvol2 = vol(self.vol1.sizeX(), self.vol2.sizeY(), self.vol2.sizeZ()) self.mask = mask if not iniRot: iniRot = Rotation() if not iniTrans: iniTrans = Shift() self.rot_trans = self.transRot2vector(rot=iniRot, trans=iniTrans) self.score = score self.val = -100000. self.centX = int(self.vol1.sizeX() // 2) self.centY = int(self.vol1.sizeY() // 2) self.centZ = int(self.vol1.sizeZ() // 2) self.binning = 1 self.interpolation = interpolation # set optimizer self.opti = opti if opti == 'fmin': self.optimizer = scipy.optimize.fmin elif opti == 'fmin_slsqp': self.optimizer = scipy.optimize.fmin_slsqp elif opti == 'fmin_cg': self.optimizer = scipy.optimize.fmin_cg elif opti == 'fmin_bfgs': self.optimizer = scipy.optimize.fmin_bfgs elif opti == 'fmin_powell': self.optimizer = scipy.optimize.fmin_powell else: raise TypeError('opti must be of type str')