def xcc(volume, template, mask=None, volumeIsNormalized=False, gpu=False): """ xcc: Calculates the 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: only used for compatibility with nxcc - not used @type volumeIsNormalized: L{bool} @return: A unscaled value @raise exception: Raises a runtime error if volume and template have a different size. @author: Thomas Hrabe """ from pytom.tompy.macros import volumesSameSize if not volumesSameSize(volume, template): raise RuntimeError('Volume and template must have same size!') if mask: # mask is given result = mask * volume * template else: result = volume * template cc = result.sum() cc = cc / float(volume.size) return cc
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.tompy.macros import volumesSameSize if not volumesSameSize(volume, template): raise RuntimeError('Volume and template must have same size!') if mask is None: from pytom.tompy.normalise import mean0std1 if not volumeIsNormalized: v = mean0std1(volume, True) else: v = volume t = mean0std1(template, True) p = volume.size result = v * t else: from pytom.tompy.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 = result.sum() ncc = ncc / float(p) return float(ncc)
def dev(volume, template, mask=None, volumeIsNormalized=False): """ dev: Calculates the squared deviation of volume and template 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: deviation @raise exception: Raises a runtime error if volume and template have a different size. @author: FF """ if gpu: import cupy as xp else: import numpy as xp from pytom_volume import vol, sum from pytom.tools.macros import volumesSameSize assert type(volume) == vol, "dev: volume has to be of type vol!" assert type(template) == vol, "dev: template has to be of type vol!" if not volumesSameSize(volume, template): raise RuntimeError('Volume and template must have same size!') if not mask: p = volume.numelem() result = volume - template else: assert type(mask) == vol, "dev: mask has to be of type vol!" p = sum(mask) result = (volume - template) * mask deviat = sum(result**2) deviat = deviat / float(p) return deviat
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