def split_matrix(parId, iterator): x0 = -1 y0 = -1 z0 = -1 t0 = 0 for x in iterator: x0 = x[1][0] y0 = x[1][1] z0 = x[1][2] t0 += 1 vol10 = npy2vol(BC_vol1.value, 3) vol20 = npy2vol(BC_vol2.value, 3) boxhalf_x1 = BC_sizeX.value / 2 boxhalf_y1 = BC_sizeY.value / 2 boxhalf_z1 = BC_sizeZ.value / 2 vol1_sub = subvolume(vol10, x0 - boxhalf_x1, y0 - boxhalf_y1, z0 - boxhalf_z1, BC_sizeX.value, BC_sizeY.value, BC_sizeZ.value) vol2_sub = subvolume(vol20, x0 - boxhalf_x1, y0 - boxhalf_y1, z0 - boxhalf_z1, BC_sizeX.value, BC_sizeY.value, BC_sizeZ.value) avg1 = mean(vol1_sub) vol1_sub1 = hanning_taper(vol1_sub, avg1) avg2 = mean(vol2_sub) vol2_sub1 = hanning_taper(vol2_sub, avg2) res = fsc2_v(vol1_sub1, vol2_sub1, BC_fsc_criterion.value, BC_max_resolution.value, BC_pixelSize.value, x0, y0, z0) tup = (round(res,6), x0, y0, z0) del avg1, avg2, vol1_sub1, vol2_sub1 del vol1_sub, vol2_sub, res, vol10, vol20, boxhalf_x1, boxhalf_y1, boxhalf_z1, x0, y0, z0, t0 return tup
def add(volume, SNR=1): """ add Adds white noise to volume @param volume: A volume @param SNR: Signal to Noise ratio of result @type SNR: int or float > 0 @return: Volume containing noise with SNR == SNR @author: Thomas Hrabe """ if (SNR < 0): return volume from math import sqrt from pytom_volume import vol, mean, variance, gaussianNoise m = mean(volume) s = sqrt(variance(volume, False) / SNR) # SNR = Var(signal) / Var(noise) # s = sqrt(variance(volume,False)/SNR)-variance(volume,False) # # if s<0: # return volume # elif s==0: # s =1 noise = vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) gaussianNoise(noise, m, s) # s is actually the std result = volume + noise return result
def mean0std1(volume, copyFlag=False): """ mean0std1: normalises input volume to mean 0 and std 1. Procedure is performed inplace if copyFlag is unspecified!!! @param volume: Data containing either an image or a volume @param copyFlag: If True a copy of volume will be returned. False unless specified otherwise. @return: If copyFlag == True, then return a normalised copy. @author: Thomas Hrabe """ import pytom_volume from math import sqrt from pytom.tools.maths import epsilon if volume.__class__ == pytom_volume.vol_comp: from pytom.basic.fourier import ifft, iftshift volume = iftshift(ifft(volume)) if not copyFlag: volumeMean = pytom_volume.mean(volume) volume.shiftscale(-volumeMean, 1) volumeStd = sqrt(pytom_volume.variance(volume, False)) if volumeStd < epsilon: raise ValueError( 'pytom_normalise.mean0std1 : The standard deviation is too low for division! ' + str(volumeStd)) volume.shiftscale(0, 1. / float(volumeStd)) else: volumeCopy = pytom_volume.vol(volume.sizeX(), volume.sizeY(), volume.sizeZ()) volumeCopy.copyVolume(volume) volumeMean = pytom_volume.mean(volumeCopy) volumeCopy.shiftscale(-1. * volumeMean, 1) volumeStd = sqrt(pytom_volume.variance(volumeCopy, False)) if volumeStd < epsilon: raise ValueError( 'pytom_normalise.mean0std1 : The standard deviation is too low for division! ' + str(volumeStd)) #volumeCopy.shiftscale(-1.*volumeMean,1) volumeCopy.shiftscale(0, 1 / float(volumeStd)) return volumeCopy
def mean0std1_Test(self): import pytom_volume from pytom.basic.normalise import mean0std1 v = pytom_volume.read('./testData/ribo.em') m = mean0std1(v, True) me = pytom_volume.mean(m) var = pytom_volume.variance(m, False) assert epsilon >= me >= -epsilon #mean value test assert 1 + epsilon >= var >= 1 - epsilon
def rescale_to_avg_std(volume1, nuavg, nustd): avg = mean(volume1) std = np.sqrt(variance(volume1, False)) if (std > 1e-30): scale = nustd/std else: scale = 1 shift = nuavg - avg*scale x = volume1.sizeX() y = volume1.sizeY() z = volume1.sizeZ() for zz in xrange(0,z): for yy in xrange(0,y): for xx in xrange(0,x): val = volume1(xx,yy,zz)*scale + shift volume1.setV(round(val,6),xx,yy,zz) return volume1
def calculate_difference_map(v1, band1, v2, band2, mask=None, focus_mask=None, align=True, sigma=None, threshold=0.4): """mask if for alignment, while focus_mask is for difference map. """ from pytom_volume import vol, power, abs, limit, transformSpline, variance, mean, max, min from pytom.basic.normalise import mean0std1 from pytom.basic.filter import lowpassFilter # do lowpass filtering first lv1 = lowpassFilter(v1, band1, band1 / 10.)[0] lv2 = lowpassFilter(v2, band2, band2 / 10.)[0] # do alignment of two volumes, if required. v1 is used as reference. if align: from sh_alignment.frm import frm_align band = int(band1 if band1 < band2 else band2) pos, angle, score = frm_align(lv2, None, lv1, None, [4, 64], band, lv1.sizeX() // 4, mask) shift = [ pos[0] - v1.sizeX() // 2, pos[1] - v1.sizeY() // 2, pos[2] - v1.sizeZ() // 2 ] # transform v2 lvv2 = vol(lv2) transformSpline(lv2, lvv2, -angle[1], -angle[0], -angle[2], lv2.sizeX() // 2, lv2.sizeY() // 2, lv2.sizeZ() // 2, -shift[0], -shift[1], -shift[2], 0, 0, 0) else: lvv2 = lv2 # do normalization mean0std1(lv1) mean0std1(lvv2) # only consider the density beyond certain sigma if sigma is None or sigma == 0: pass elif sigma < 0: # negative density counts assert min(lv1) < sigma assert min(lvv2) < sigma limit(lv1, 0, 0, sigma, 0, False, True) limit(lvv2, 0, 0, sigma, 0, False, True) else: # positive density counts assert max(lv1) > sigma assert max(lvv2) > sigma limit(lv1, sigma, 0, 0, 0, True, False) limit(lvv2, sigma, 0, 0, 0, True, False) # if we want to focus on specific area only if focus_mask: lv1 *= focus_mask lvv2 *= focus_mask # calculate the STD map avg = (lv1 + lvv2) / 2 var1 = avg - lv1 power(var1, 2) var2 = avg - lvv2 power(var2, 2) std_map = var1 + var2 power(std_map, 0.5) # calculate the coefficient of variance map # std_map = std_map/abs(avg) if focus_mask: std_map *= focus_mask # threshold the STD map mv = mean(std_map) threshold = mv + (max(std_map) - mv) * threshold limit(std_map, threshold, 0, threshold, 1, True, True) # do a lowpass filtering std_map1 = lowpassFilter(std_map, v1.sizeX() // 4, v1.sizeX() / 40.)[0] if align: std_map2 = vol(std_map) transformSpline(std_map1, std_map2, angle[0], angle[1], angle[2], v1.sizeX() // 2, v1.sizeY() // 2, v1.sizeZ() // 2, 0, 0, 0, shift[0], shift[1], shift[2]) else: std_map2 = std_map1 limit(std_map1, 0.5, 0, 1, 1, True, True) limit(std_map2, 0.5, 0, 1, 1, True, True) # return the respective difference maps return (std_map1, std_map2)
fsc_criterion=0.143 window_size = L increment= 4 step = increment numberBands = int(window_size/8) sizeX = sizeY = sizeZ = window_size conf = SparkConf().setAppName("parallel local FSC").setMaster("local[*]") sc = SparkContext(conf=conf) vol1File = "./emd_3802_half_map_1.map" vol2File = "./emd_3802_half_map_2.map" hm1 = read(vol1File) hm2 = read(vol2File) avg1 = mean(hm1) std1 = np.sqrt(variance(hm1, False)) hm20 = rescale_to_avg_std(hm2, avg1, std1) nz = hm1.sizeZ() ny = hm1.sizeY() nx = hm1.sizeX() mask_level = -1 vz = vy = vx = window_size/2 pmask = vol(nx,ny,nz) pmask.setAll(1) # set up the mask for which voxels to calculate total_num = 0
def _dspcub(volume, sigma=None, projectionAxis='z'): """ _dspcub: Creates a dspcub image dspcub2PNG: display z-slices in 2D image @param volume: The input volume @type volume: L{pytom_volume.vol} @parameter sigma: thresholding as multiples of standard deviation sigma @type sigma: float @param projectionAxis: Defines the axis. Default is z, means you look on the xy plane. (x equals to 'yz' plane, y to 'xz') @return: Image @rtype: Image @@author: Pia Unverdorben """ if volume.sizeZ() == 1: raise Exception('You can use ') import Image from pytom_volume import min, max, mean, variance, limit, subvolume from math import sqrt, ceil, floor sizeX = volume.sizeX() sizeY = volume.sizeY() sizeZ = volume.sizeZ() if projectionAxis == 'x': imagesPerRow = int(floor(sqrt(sizeX))) size1 = float(sizeX) sizeI = sizeY sizeJ = sizeZ elif projectionAxis == 'y': imagesPerRow = int(floor(sqrt(sizeY))) size1 = float(sizeY) sizeI = sizeX sizeJ = sizeZ elif projectionAxis == 'z': imagesPerRow = int(floor(sqrt(sizeZ))) size1 = float(sizeZ) sizeI = sizeX sizeJ = sizeY numberIterations = imagesPerRow * imagesPerRow if size1 < numberIterations: iterationSteps = float(numberIterations / size1) else: iterationSteps = float(size1 / numberIterations) iterationSteps = int(iterationSteps) if projectionAxis == 'x': img = Image.new('L', (sizeY * imagesPerRow, sizeZ * imagesPerRow)) elif projectionAxis == 'y': img = Image.new('L', (sizeX * imagesPerRow, sizeZ * imagesPerRow)) elif projectionAxis == 'z': img = Image.new('L', (sizeX * imagesPerRow, sizeY * imagesPerRow)) # normalize according to standard deviation if sigma is specified if sigma: meanv = mean(volume) stdv = sqrt(variance(volume, False)) minValue = meanv - float(sigma) * stdv maxValue = meanv + float(sigma) * stdv else: minValue = min(volume) maxValue = max(volume) for sliceNumber in range(0, numberIterations, iterationSteps): if projectionAxis == 'x': png = Image.new('L', (sizeY, sizeZ)) elif projectionAxis == 'y': png = Image.new('L', (sizeX, sizeZ)) elif projectionAxis == 'z': png = Image.new('L', (sizeX, sizeY)) (yvalue, xvalue) = divmod(sliceNumber, imagesPerRow) if projectionAxis == 'x': vol = subvolume(volume, sliceNumber, 0, 0, 1, sizeY, sizeZ) elif projectionAxis == 'y': vol = subvolume(volume, 0, sliceNumber, 0, sizeX, 1, sizeZ) elif projectionAxis == 'z': vol = subvolume(volume, 0, 0, sliceNumber, sizeX, sizeY, 1) vol.shiftscale(-minValue, 1) vol.shiftscale(0, 255 / maxValue) for i in range(sizeI): for j in range(sizeJ): if projectionAxis == 'x': value = vol(0, i, j) elif projectionAxis == 'y': value = vol(i, 0, j) elif projectionAxis == 'z': value = vol(i, j, 0) png.putpixel((i, j), int(value)) img.paste(png, (xvalue * sizeI, yvalue * sizeJ)) return img