def transform_single_vol(vol1, mask): from pytom_volume import sum from pytom.basic.correlation import meanValueUnderMask, stdValueUnderMask, meanUnderMask, stdUnderMask p = sum(mask) meanV = meanUnderMask(vol1, mask, p) stdV = stdUnderMask(vol1, mask, p, meanV) result = (vol1 - meanV) / stdV return result
def bestAlignmentGPU(particleList, rotations, plan, isSphere=True): """ bestAlignment: Determines best alignment of particle relative to the reference @param particle: A particle @type particle: L{pytom_volume.vol} @param reference: A reference @type reference: L{pytom_volume.vol} @param referenceWeighting: Fourier weighting of the reference (sum of wedges for instance) @type referenceWeighting: L{pytom.basic.structures.vol} @param wedgeInfo: What does the wedge look alike? @type wedgeInfo: L{pytom.basic.structures.Wedge} @param rotations: All rotations to be scanned @type rotations: L{pytom.angles.AngleObject} @param scoreObject: @type scoreObject: L{pytom.score.score.Score} @param mask: real-space mask for correlation function @type mask: L{pytom.basic.structures.Particle} @param preprocessing: Class storing preprocessing of particle and reference such as bandpass @type preprocessing: L{pytom.alignment.preprocessing.Preprocessing} @param progressBar: Display progress bar of alignment. False by default. @param binning: binning factor - e.g. binning=2 reduces size by FACTOR of 2 @type binning: int or float @param bestPeak: Initialise best peak with old values. @param verbose: Print out infos. Writes CC volume to disk!!! Default is False @return: Returns the best rotation for particle and the corresponding scoring result. @author: Thomas Hrabe """ from pytom.gpu.tools import add_particle_to_sum from pytom.gpu.prepare_plans import prepare_glocal_plan from pytom.gpu.correlation import subPixelPeak, find_coords_max_ccmap from pytom.gpu.filter import filter, applyWedge from pytom.gpu.preprocessing import applyPreprocessing from pytom.gpu.transformations import resize, resizeFourier, rotate import numpy as np binningType = 'Fourier' # or 'Fourier' bestPeakList = [] for pid, particle in particleList: centerCoordinates = np.array([size//2 for size in plan.shape], dtype=np.int32) # create buffer volume for transformed particle applyWedge(particle) # apply wedge to itself applyPreprocessing(particle, taper=np.float32(shape[0] / 10.) ) # filter particle to some resolution bestPeak = [-1000, [0,0,0], [0,0,0]] for n, currentRotation in enumerate(rotations): if not isSphere: meanUnderMask(particle, plan.mask, plan.p) stdUnderMask(particle, plan.mask, plan.p, plan.meanV) rotateWedge(plan.reference, currentRotation, plan.wedge, plan.mask, centerCoordinates) applyPreprocessing(plan.simulatedVol, bypassFlag=True) # weight particle rotateWeight(plan.reference, plan.referenceWeighting, currentRotation) filterParticle(particle, weight(weightingRotated), plan.particleCopy) cross_correlation(plan.particleCopy, plan.simulatedVol, plan.ccc_map) peakCoordinates = find_coords_max_ccmap(plan.ccc_map) # with subPixelPeak [peakValue, peakPosition] = subPixelPeak(plan.ccc_map, coordinates=peakCoordinates, cropsize=10) # determine shift relative to center shiftX = (peakPosition[0] - centerX) shiftY = (peakPosition[1] - centerY) shiftZ = (peakPosition[2] - centerZ) newPeak = (peakValue, currentRotation, (shiftX, shiftY, shiftZ)) if bestPeak[0] < newPeak[0]: bestPeak = newPeak if pid % 2 == 0: add_particle_to_sum(particle, bestPeak[1][::-1]*-1, plan.evenSum) else: add_particle_to_sum(particle, bestPeak[1][::-1]*-1, plan.oddSum) bestPeakList.append(bestPeak) return bestPeakList
def bestAlignment(particle, reference, referenceWeighting, wedgeInfo, rotations, scoreObject=0, mask=None, preprocessing=None, progressBar=False, binning=1, bestPeak=None, verbose=False): """ bestAlignment: Determines best alignment of particle relative to the reference @param particle: A particle @type particle: L{pytom_volume.vol} @param reference: A reference @type reference: L{pytom_volume.vol} @param referenceWeighting: Fourier weighting of the reference (sum of wedges for instance) @type referenceWeighting: L{pytom.basic.structures.vol} @param wedgeInfo: What does the wedge look alike? @type wedgeInfo: L{pytom.basic.structures.Wedge} @param rotations: All rotations to be scanned @type rotations: L{pytom.angles.AngleObject} @param scoreObject: @type scoreObject: L{pytom.score.score.Score} @param mask: real-space mask for correlation function @type mask: L{pytom.basic.structures.Particle} @param preprocessing: Class storing preprocessing of particle and reference such as bandpass @type preprocessing: L{pytom.alignment.preprocessing.Preprocessing} @param progressBar: Display progress bar of alignment. False by default. @param binning: binning factor - e.g. binning=2 reduces size by FACTOR of 2 @type binning: int or float @param bestPeak: Initialise best peak with old values. @param verbose: Print out infos. Writes CC volume to disk!!! Default is False @return: Returns the best rotation for particle and the corresponding scoring result. @author: Thomas Hrabe """ from pytom.basic.correlation import subPixelPeak, subPixelPeakParabolic from pytom.alignment.structures import Peak from pytom_volume import peak, vol, vol_comp from pytom.basic.filter import filter,rotateWeighting from pytom.basic.structures import Rotation, Shift, Particle, Mask from pytom.angles.angle import AngleObject from pytom.alignment.preprocessing import Preprocessing from pytom.basic.transformations import resize, resizeFourier binningType = 'Fourier' # or 'Fourier' assert isinstance(rotations, AngleObject), "bestAlignment: rotations must be " \ "AngleObject!" currentRotation = rotations.nextRotation() if currentRotation == [None,None,None]: raise Exception('bestAlignment: No rotations are sampled! Something is wrong with input rotations') assert particle.__class__ == vol, "particle not of type vol" assert reference.__class__ == vol, "reference not of type vol" assert (referenceWeighting.__class__ == vol or referenceWeighting.__class__ == str), \ "referenceWeighting not volume or str" if mask: assert mask.__class__ == Mask, "Mask not of type Mask" m = mask.getVolume() if scoreObject == 0 or not scoreObject: from pytom.score.score import xcfScore scoreObject = xcfScore() # fix binning if binning == 0: binning = 1 if binning != 1: particleUnbinned = vol(particle.sizeX(), particle.sizeY(), particle.sizeZ()) particleUnbinned.copyVolume(particle) particle = resize(volume=particle, factor=1./binning, interpolation=binningType) if type(particle) == tuple: particle = particle[0] referenceUnbinned = vol(reference.sizeX(), reference.sizeY(), reference.sizeZ()) referenceUnbinned.copyVolume(reference) reference = resize(volume=reference, factor=1./binning, interpolation=binningType) if type(reference) == tuple: reference = reference[0] if mask: m = resize(volume=m, factor=1./binning, interpolation='Spline') if not referenceWeighting.__class__ == str: referenceWeightingUnbinned = vol_comp(referenceWeighting.sizeX(), referenceWeighting.sizeY(), referenceWeighting.sizeZ()) referenceWeightingUnbinned.copyVolume(referenceWeighting) if binning != 1: referenceWeighting = resizeFourier(fvol=referenceWeighting, factor=1./binning) centerX, centerY, centerZ = int(particle.sizeX()/2), int(particle.sizeY()/2), int(particle.sizeZ()/2) # create buffer volume for transformed particle particleCopy = vol(particle.sizeX(),particle.sizeY(),particle.sizeZ()) particle = wedgeInfo.apply(particle) #apply wedge to itself if preprocessing is None: preprocessing = Preprocessing() preprocessing.setTaper( taper=particle.sizeX()/10.) particle = preprocessing.apply(volume=particle, bypassFlag=True) # filter particle to some resolution particleCopy.copyVolume(particle) # compute standard veviation volume really only if needed if mask and (scoreObject._type=='FLCFScore'): from pytom_volume import sum from pytom.basic.correlation import meanUnderMask, stdUnderMask p = sum(m) meanV = meanUnderMask(particle, m, p) stdV = stdUnderMask(particle, m, p, meanV) else: meanV = None stdV = None while currentRotation != [None,None,None]: if mask: m = mask.getVolume(currentRotation) if binning != 1: m = resize(volume=m, factor=1./binning, interpolation='Spline') #update stdV if mask is not a sphere # compute standard deviation volume really only if needed if not mask.isSphere() and (scoreObject._type=='FLCFScore'): meanV = meanUnderMask(particle, m, p) stdV = stdUnderMask(particle, m, p, meanV) else: m = None simulatedVol = _rotateWedgeReference(reference, currentRotation, wedgeInfo, m, [centerX, centerY, centerZ]) simulatedVol = preprocessing.apply(volume=simulatedVol, bypassFlag=True) #weight particle if not referenceWeighting.__class__ == str: from pytom_freqweight import weight weightingRotated = rotateWeighting(weighting=referenceWeighting, z1=currentRotation[0], z2=currentRotation[1], x=currentRotation[2], isReducedComplex=True, returnReducedComplex=True, binarize=False) particleCopy.copyVolume(particle) r = list(filter(particleCopy, weight(weightingRotated))) particleCopy = r[0] scoringResult = scoreObject.score(particleCopy, simulatedVol, m, stdV) pk = peak(scoringResult) #with subPixelPeak [peakValue,peakPosition] = subPixelPeak(scoreVolume=scoringResult, coordinates=pk, interpolation='Quadratic', verbose=False) #[peakValue,peakPosition] = subPixelPeakParabolic(scoreVolume=scoringResult, coordinates=pk, verbose=False) # determine shift relative to center shiftX = (peakPosition[0] - centerX) * binning shiftY = (peakPosition[1] - centerY) * binning shiftZ = (peakPosition[2] - centerZ) * binning #NANs would fail this test. assert peakValue == peakValue, "peakValue seems to be NaN" newPeak = Peak(peakValue, Rotation(currentRotation), Shift(shiftX, shiftY, shiftZ)) if verbose: print('Rotation: z1=%3.1f, z2=%3.1f, x=%3.1f; Dx=%2.2f, Dy=%2.2f, Dz=%2.2f, CC=%2.3f' % \ (currentRotation[0], currentRotation[1], currentRotation[2], shiftX, shiftY, shiftZ, peakValue)) if bestPeak is None: bestPeak = newPeak if verbose: scoringResult.write('BestScore.em') if bestPeak < newPeak: bestPeak = newPeak if verbose: scoringResult.write('BestScore.em') currentRotation = rotations.nextRotation() # repeat ccf for binned sampling to get translation accurately if binning != 1: m = mask.getVolume(bestPeak.getRotation()) centerX, centerY, centerZ = int(particleUnbinned.sizeX()/2), int(particleUnbinned.sizeY()/2), \ int(particleUnbinned.sizeZ()/2) simulatedVol = _rotateWedgeReference(referenceUnbinned, bestPeak.getRotation(), wedgeInfo, m, [centerX, centerY, centerZ]) simulatedVol = preprocessing.apply(volume=simulatedVol, bypassFlag=True) if mask and scoreObject._type=='FLCFScore': p = sum(m) meanV = meanUnderMask(volume=particleUnbinned, mask=m, p=p) stdV = stdUnderMask(volume=particleUnbinned, mask=m, p=p, meanV=meanV) scoreObject._peakPrior.reset_weight() scoringResult = scoreObject.score(particle=particleUnbinned, reference=simulatedVol, mask=m, stdV=stdV) pk = peak(scoringResult) [peakValue,peakPosition] = subPixelPeak(scoreVolume=scoringResult, coordinates=pk, interpolation='Quadratic', verbose=False) shiftX = (peakPosition[0] - centerX) shiftY = (peakPosition[1] - centerY) shiftZ = (peakPosition[2] - centerZ) bestPeak = Peak(peakValue, bestPeak.getRotation(), Shift(shiftX, shiftY, shiftZ)) if verbose: print('BestAlignment: z1=%3.1f, z2=%3.1f, x=%3.1f; Dx=%2.2f, Dy=%2.2f, Dz=%2.2f, CC=%2.3f' % \ (bestPeak.getRotation()[0], bestPeak.getRotation()[1], bestPeak.getRotation()[2], bestPeak.getShift()[0], bestPeak.getShift()[1], bestPeak.getShift()[2], bestPeak.getScoreValue())) rotations.reset() scoreObject._peakPrior.reset_weight() return bestPeak
def extractPeaks(volume, reference, rotations, scoreFnc=None, mask=None, maskIsSphere=False, wedgeInfo=None, **kwargs): ''' Created on May 17, 2010 @param volume: target volume @type volume: L{pytom_volume.vol} @param reference: reference @type reference: L{pytom_volume.vol} @param rotations: rotation angle list @type rotations: L{pytom.angles.globalSampling.GlobalSampling} @param scoreFnc: score function that is used @type scoreFnc: L{pytom.basic.correlation} @param mask: mask volume @type mask: L{pytom_volume.vol} @param maskIsSphere: flag to indicate whether the mask is sphere or not @type maskIsSphere: boolean @param wedgeInfo: wedge information @type wedgeInfo: L{pytom.basic.structures.WedgeInfo} @return: both the score volume and the corresponding rotation index volume @rtype: L{pytom_volume.vol} @author: chen ''' # from pytom.tools.timing import timing # t = timing(); t.start() # parse the parameters nodeName = kwargs.get('nodeName', '') verbose = kwargs.get('verboseMode', True) if verbose not in [True, False]: verbose = True moreInfo = kwargs.get('moreInfo', False) if moreInfo not in [True, False]: moreInfo = False from pytom.basic.correlation import FLCF from pytom.basic.structures import WedgeInfo, Wedge from pytom_volume import vol, pasteCenter from pytom_volume import rotateSpline as rotate # for more accuracy from pytom_volume import updateResFromIdx from pytom.basic.files import write_em if scoreFnc == None: scoreFnc = FLCF # only FLCF needs mask if scoreFnc == FLCF: if mask.__class__ != vol: # construct a sphere mask by default from pytom_volume import initSphere mask = vol(reference.sizeX(), reference.sizeY(), reference.sizeZ()) mask.setAll(0) initSphere(mask, reference.sizeX() / 2, 0, 0, reference.sizeX() / 2, reference.sizeX() / 2, reference.sizeX() / 2) maskIsSphere = True # result volume which stores the score result = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) result.setAll(-1) # result orientation of the peak value (index) orientation = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) orientation.setAll(0) currentRotation = rotations.nextRotation() index = 0 if verbose == True: from pytom.tools.ProgressBar import FixedProgBar max = rotations.numberRotations() - 1 prog = FixedProgBar(0, max, nodeName) if moreInfo: sumV = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) sumV.setAll(0) sqrV = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) sqrV.setAll(0) else: sumV = None sqrV = None if wedgeInfo.__class__ == WedgeInfo or wedgeInfo.__class__ == Wedge: print('Applied wedge to volume') volume = wedgeInfo.apply(volume) while currentRotation != [None, None, None]: if verbose == True: prog.update(index) # rotate the reference ref = vol(reference.sizeX(), reference.sizeY(), reference.sizeZ()) rotate(reference, ref, currentRotation[0], currentRotation[1], currentRotation[2]) # apply wedge if wedgeInfo.__class__ == WedgeInfo or wedgeInfo.__class__ == Wedge: ref = wedgeInfo.apply(ref) # rotate the mask if it is asymmetric if scoreFnc == FLCF: if maskIsSphere == False: # if mask is not a sphere, then rotate it m = vol(mask.sizeX(), mask.sizeY(), mask.sizeZ()) rotate(mask, m, currentRotation[0], currentRotation[1], currentRotation[2]) else: m = mask # compute the score # if mask is sphere and it is the first run, compute the standard deviation of the volume under mask for late use if scoreFnc == FLCF and index == 0 and maskIsSphere == True: # compute standard deviation of the volume under mask maskV = m if volume.sizeX() != m.sizeX() or volume.sizeY() != m.sizeY( ) or volume.sizeZ() != m.sizeZ(): maskV = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) maskV.setAll(0) pasteCenter(m, maskV) from pytom_volume import sum p = sum(m) from pytom.basic.correlation import meanUnderMask, stdUnderMask meanV = meanUnderMask(volume, maskV, p) stdV = stdUnderMask(volume, maskV, p, meanV) # ref.write('template_cpu.em') if scoreFnc == FLCF: if maskIsSphere == True: score = scoreFnc(volume, ref, m, stdV, wedge=1) else: score = scoreFnc(volume, ref, m) else: # not FLCF, so doesn't need mask as parameter and perhaps the reference should have the same size _ref = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) _ref.setAll(0) pasteCenter(ref, _ref) score = scoreFnc(volume, _ref) # update the result volume and the orientation volume updateResFromIdx(result, score, orientation, index) if moreInfo: sumV = sumV + score sqrV = sqrV + score * score currentRotation = rotations.nextRotation() index = index + 1 # if moreInfo: # sumV = sumV/rotations.numberRotations() # sqrV = sqrV/rotations.numberRotations() # time = t.end(); print 'The overall execution time: %f' % time return [result, orientation, sumV, sqrV]
pasteCenter(maskVOL, maskCentVOL) MCV = vol2npy(maskCentVOL) print('\n center mask results (nd, gpu, vol)') for eval in (maskCentNDA, MCG, MCV): print(eval.mean(), eval.min(), eval.max()) #CALCULATE STDV import pytom.tompy.correlation as corrNDA import pytom.tompy.testGPUcorr as corrGPU import pytom.basic.correlation as corrVOL meanNDA = corrNDA.meanVolUnderMask(voluNDA, maskCentNDA, xp=np) stdNDA = corrNDA.meanVolUnderMask(voluNDA, maskCentNDA, meanNDA) meanVOL = corrVOL.meanUnderMask(voluVOL, maskCentVOL, maskNDA.sum()) stdVOL = corrVOL.stdUnderMask(voluVOL, maskCentVOL, maskNDA.sum(), meanVOL) write_em('stdV.em', stdVol) stdV = vol2npy(stdVOL).copy() stdV = stdV.transpose(2, 1, 0).copy() plan = prepare_template_matching(voluNDA, tempNDA, maskNDA, wedge, stdV) meanVal = corrVOL.meanValueUnderMask(tempVOL, maskVOL, mask.sum()) stdVal = corrVOL.stdValueUnderMask(tempVOL, maskVOL, meanVal, mask.sum()) templateVol = ((tempVOL - meanVal) / stdVal) * maskVOL tvol = vol2npy(templateVol) meanTGPU = meanUnderMask(tempGPU, maskGPU, p=p) stdTGPU = stdUnderMask(tempGPU, maskGPU, meanTGPU, p=p)
from pytom.tompy.correlation import meanVolUnderMask, stdVolUnderMask import pytom.basic.correlation as corr from pytom.basic.files import read as readd from pytom.basic.files import write_em from pytom_numpy import vol2npy, npy2vol from pytom_volume import pasteCenter, vol vv = readd('tomo.mrc', subregion=[0, 0, start, 464, 464, end - start]) mm = readd('mask.em') if vv.sizeX() != mm.sizeX() or vv.sizeY() != mm.sizeY() or vv.sizeZ( ) != mm.sizeZ(): maskV = vol(vv.sizeX(), vv.sizeY(), vv.sizeZ()) maskV.setAll(0) pasteCenter(mm, maskV) meanV = corr.meanUnderMask(vv, maskV, mask.sum()) stdVol = corr.stdUnderMask(vv, maskV, mask.sum(), meanV) write_em('stdV.em', stdVol) stdV = vol2npy(stdVol).copy() stdV = stdV.transpose(2, 1, 0).copy() assert stdV.shape == volume.shape #mask = paste_in_center(mask, np.zeros_like(volume)) start = driver.Event() end = driver.Event() start.record() scores, angles, plan = template_matching_gpu(volume, temp, mask,