def FSCSum(volume, reference, numberOfBands, wedgeAngle=-1, gpu=False): """ FSCSum: Determines the sum of the Fourier Shell Correlation coefficient for a volume and reference. @param volume: A volume @type volume: L{pytom_volume.vol} @param reference: A reference of same size as volume @type reference: L{pytom_volume.vol} @param numberOfBands: Number of bands @param wedgeAngle: A optional wedge angle @return: The sum FSC coefficient @rtype: float @author: Thomas Hrabe """ if gpu: import cupy as xp else: import numpy as xp from pytom.tompy.correlation import bandCC result = 0 numberVoxels = 0 #volume.write('vol.em'); #reference.write('ref.em'); fvolume = xp.fft.fftn(volume) freference = xp.fft.fftn(reference) numelem = volume.size fvolume = shiftscale(fvolume, 0, 1 / float(numelem)) freference = shiftscale(fvolume, 0, 1 / float(numelem)) #print '-----' for i in range(numberOfBands): #process bandCorrelation band = [] band[0] = i * volume.shape[0] / numberOfBands band[1] = (i + 1) * volume.shape[0] / numberOfBands r = bandCC(fvolume, freference, band, gpu=gpu) cc = r[0] #print cc result = result + cc #print '-----' return result * (1 / float(numberOfBands))
def FSC(volume1, volume2, numberBands=None, mask=None, verbose=False, filename=None, num_procs=1): """ FSC - Calculates the Fourier Shell Correlation for two volumes @param volume1: volume one @type volume1: L{pytom_volume.vol} @param volume2: volume two @type volume2: L{pytom_volume.vol} @param numberBands: number of shells for FSC @type numberBands: int @param filename: write FSC to ascii file if specified @type filename: string @return: Returns a list of cc values @author: Thomas Hrabe @rtype: list[floats] """ from pytom.tompy.correlation import bandCC from pytom.basic.structures import Mask from pytom.tompy.io import read, write from pytom.tompy.tools import volumesSameSize import time t = time.time() if not volumesSameSize(volume1, volume2): raise RuntimeError('Volumes must have the same size!') numberBands = volume1.shape[0] // 2 if numberBands is None else numberBands if not mask is None: if mask.__class__ == xp.array([0]).__class__: volume1 = volume1 * mask volume2 = volume2 * mask elif mask.__class__ == Mask: mask = mask.getVolume() volume1 = volume1 * mask volume2 = volume2 * mask elif mask.__class__ == str: mask = read(mask) volume1 = volume1 * mask volume2 = volume2 * mask else: raise RuntimeError( 'FSC: Mask must be a volume OR a Mask object OR a string path to a mask!' ) fscResult = [] band = [-1, -1] increment = int(volume1.shape[0] / 2 * 1 / numberBands) import time fvolume1 = xp.fft.fftn(volume1) fvolume2 = xp.fft.fftn(volume2) for n, i in enumerate(range(0, volume1.shape[0] // 2, increment)): band[0] = i band[1] = i + increment if verbose: print('Band : ', band) res = bandCC(fvolume1, fvolume2, band, verbose) if i == 0 and increment == 1: #force a 1 for correlation of the zero frequency res = 1 if verbose: print('Correlation ', res) fscResult.append(res) if filename: f = open(filename, 'w') for item in fscResult: f.write("%s\n" % item) f.close() return fscResult
def weightedXCC(volume, reference, numberOfBands, wedgeAngle=-1, gpu=False): """ weightedXCC: Determines the band weighted correlation coefficient for a volume and reference. Notation according Steward/Grigorieff paper @param volume: A volume @type volume: L{pytom_volume.vol} @param reference: A reference of same size as volume @type reference: L{pytom_volume.vol} @param numberOfBands: Number of bands @param wedgeAngle: A optional wedge angle @return: The weighted correlation coefficient @rtype: float @author: Thomas Hrabe """ if gpu: import cupy as xp else: import numpy as xp from pytom.tompy.transform import fft, fourier_reduced2full from pytom.basic.structures import WedgeInfo result = 0 numberVoxels = 0 #volume.write('vol.em'); #reference.write('ref.em'); wedge = WedgeInfo(wedgeAngle) wedgeVolume = wedge.returnWedgeVolume(volume.shape[0], volume.shape[1], volume.shape[2]) increment = int(volume.shape[0] / 2 * 1 / numberOfBands) band = [0, 100] for i in range(0, volume.shape[0] / 2, increment): band[0] = i band[1] = i + increment r = bandCC(volume, reference, band, gpu=gpu) cc = r[0] #print cc; filter = r[1] #get bandVolume bandVolume = filter.getWeightVolume(True) filterVolumeReduced = bandVolume * wedgeVolume filterVolume = fourier_reduced2full(filterVolumeReduced) #determine number of voxels != 0 N = (filterVolume != 0).sum() w = xp.sqrt(1 / float(N)) #add to number of total voxels numberVoxels = numberVoxels + N #print 'w',w; #print 'cc',cc; #print 'N',N; cc2 = cc * cc #print 'cc2',cc2; if cc <= 0.0: cc = cc2 else: cc = cc2 / (cc + w) #print 'cc',cc; cc = cc * cc * cc #no abs #print 'cc',cc; #add up result result = result + cc * N return result * (1 / float(numberVoxels))