def rotationDistanceMatrix(self): """ rotationDistanceMatrix: Generate a matrix of rotation distances. The distance is determined by L{pytom.angles.quaternions.Quaternion.distance}. @return: L{pytom_volume.vol} of rotation distances @author: Thomas Hrabe """ from pytom_volume import vol from pytom.tools.maths import rotation_distance numberOfRotations = len(self) distanceMatrix = vol(numberOfRotations,numberOfRotations,1) distanceMatrix.setAll(0) for i in range(len(self)): if i < len(self): for j in range(i+1,len(self)): ri = self[i] rj = self[j] d = rotation_distance(ri,rj) distanceMatrix(d,i,j,0) distanceMatrix(d,j,i,0) distanceMatrix(0,i,i,0) return distanceMatrix
def test_FRM(self): import swig_frm from sh_alignment.frm import frm_align from pytom_volume import vol, rotate, shift from pytom.basic.structures import Rotation, Shift from pytom.tools.maths import rotation_distance v = vol(32,32,32) v.setAll(0) vMod = vol(32,32,32) vRot = vol(32,32,32) v.setV(1,10,10,10) v.setV(1,20,20,20) v.setV(1,15,15,15) v.setV(1,7,21,7) rotation = Rotation(10,20,30) shiftO = Shift(1,-3,5) rotate(v,vRot,rotation.getPhi(),rotation.getPsi(),rotation.getTheta()) shift(vRot,vMod,shiftO.getX(),shiftO.getY(),shiftO.getZ()) pos, ang, score = frm_align(vMod, None, v, None, [4, 64], 10) rotdist = rotation_distance(ang1=rotation, ang2=ang) diffx = shiftO[0] - (pos[0] - 16) diffy = shiftO[1] - (pos[1] - 16) diffz = shiftO[2] - (pos[2] - 16) self.assertTrue( rotdist < 5., msg='Rotations are different') self.assertTrue( diffx < .5, msg='x-difference > .5') self.assertTrue( diffy < .5, msg='y-difference > .5') self.assertTrue( diffz < .5, msg='z-difference > .5')
def compare_angles(self, ang, prevang): from pytom.tools.maths import rotation_distance ang2 = ang.nextRotation() angdist = ang.distanceFunction(rotation=ang2) if prevang: neighbor_dist = rotation_distance(ang1=prevang, ang2=ang2) #print(neighbor_dist) return angdist, ang2
def frm_find_topn_angles_interp(corr, n=5, dist=3.0): """Find the Euler angles corresponding to the top N peaks in the correlation function using interpolation, given the minimal distance between them. Parameters ---------- corr: Correlation function. numpy.array n: Number of peaks. Integer, default 5. dist: Euler angle distance threshold in bandwidth unit, e.g. if bandwidth is b, then the angle distance is dist/b*180 Integer or float, default 3. Returns ------- List: [(phi, psi, theta, peak_value), ...] """ from pytom.tools.maths import rotation_distance b = corr.shape[0] / 2 res = [] sort = corr.argsort(axis=None) for i in xrange(len(sort) - 1, -1, -1): x, y, z = np_transfer_idx(sort[i], corr.shape) # ang = frm_idx2angle(b, x, y, z) pos, peak = find_subpixel_peak_position(corr, [x, y, z]) ang = frm_idx2angle(b, pos[0], pos[1], pos[2]) # eliminate the nearby solution for a in res: if rotation_distance(ang, a[0]) < float(dist) / b * 180.0: break else: # res.append((ang, corr[x,y,z])) res.append((ang, peak)) if len(res) >= n: break return res
def distanceFunction(self, rotation): """ distanceFunction: determines the distance of of given rotation to old rotation @param rotation: rotation @type rotation: L{pytom.basic.structures.Rotation} @return: proposed increment for next iteration @author: FF """ from pytom.tools.maths import rotation_distance increment = rotation_distance(ang1=self._startRotation, ang2=rotation) #from math import sqrt,ceil #sqrt2 = 1.4142135623730951 ##must modulo 360 each value for avoiding negative values #deltaTheta = (self._startX % 360) - (rotation[2] % 360) #deltaPhi = (self._startZ1 % 360) - (rotation[0] % 360) # #increment = sqrt(pow(deltaTheta,2)+pow(deltaPhi,2)) / sqrt2 return increment
angles.reset() t.start() tmp = vol(v) peak = 0.0 angle = angles.nextRotation() while angle != [None, None, None]: rotateSpline(v, tmp, angle[0], angle[1], angle[2]) tmp = wedge.apply(tmp) res = nxcc(v2, tmp, mask) if res >= peak: peak = res ang = angle angle = angles.nextRotation() t1 = t.end() dist_old = rotation_distance([phi, psi, the], ang) # if dist_old > 30: # print [phi, psi, the], ang diff_old.append(dist_old) total_time1 += t1 # new method t.start() res = frm_fourier_constrained_vol(v2, w, v, m) ang2 = frm_find_best_angle(res, b) t2 = t.end() dist_new = rotation_distance([phi, psi, the], ang2) diff_new.append(dist_new) total_time2 += t2
""" Test the angle list used in template matching to see if the angluar distance is actually consistent with the file name. """ from pytom.tools.maths import rotation_distance from pytom.angles.fromFile import AngleListFromEM ang = AngleListFromEM('angles_07_45123.em') # ang = AngleListFromEM('angles_12.85_7112.em') from eulerDist import distEuler for i in xrange(len(ang) - 1): a1 = ang[i] a2 = ang[i + 1] if rotation_distance(a1, a2) - distEuler(a1, a2) > 0.1: print a1, a2, rotation_distance(a1, a2) - distEuler(a1, a2)
res = dummy1 / ((dummy2 * dummy3)**0.5 * dummy4) return res dist = [] dist2 = [] for i in xrange(100): phi = np.random.randint(360) psi = np.random.randint(360) the = np.random.randint(180) rotateSpline(v, v2, phi, psi, the) sf = vol2sf(v, r, b) sf = sf * wedge sf2 = vol2sf(v2, r, b) sf2 = sf2 * wedge res = bart_way(sf2, wedge, sf, wedge) ang = frm_find_best_angle(res, b) dist.append(rotation_distance([phi, psi, the], ang)) res = frm_constrained_corr(sf2, wedge, sf, wedge) ang = frm_find_best_angle(res, b) dist2.append(rotation_distance([phi, psi, the], ang)) print dist[-1], dist2[-1] print 'Diff: ', np.max(dist), np.mean(dist), np.min(dist), np.std(dist) print 'Diff2: ', np.max(dist2), np.mean(dist2), np.min(dist2), np.std(dist2)
from frm import frm_align_vol from pytom.tools.maths import rotation_distance, euclidianDistance from pytom.tools.timing import Timing t = Timing() t.start() dis_offset = [] ang_offset = [] for pp in pl: v = read(pp.getFilename()) pos, ang, score = frm_align_vol(v, [-60.0, 60.0], r, [8, 32], 10) g_pos = pp.getShift().toVector() g_pos = [g_pos[0] + 50, g_pos[1] + 50, g_pos[2] + 50] g_ang = [ pp.getRotation().getZ1(), pp.getRotation().getZ2(), pp.getRotation().getX() ] dis_offset.append(euclidianDistance(g_pos, pos)) ang_offset.append(rotation_distance(g_ang, ang)) print euclidianDistance(g_pos, pos), rotation_distance(g_ang, ang) tt = t.end() import numpy as np print tt / 100, np.mean(dis_offset), np.mean(ang_offset)
from pytom.basic.structures import Rotation, ParticleList pl = ParticleList() pl.fromXMLFile(pl_filename) angles = [] for angle in axis_angles.split('/'): z1, x, z2 = [float(i) for i in angle.split(',')] angles.append(Rotation(z1, z2, x)) from pytom.tools.maths import rotation_distance dist = [] for p in pl: rot = p.getRotation() d = 180 for axis in angles: dd = rotation_distance(rot, axis) if dd < d: d = dd dist.append(d) # cut out only the things in the range if cut_out_range is not None: for i in range(len(dist)): if dist[i] >= cut_out_range[0] and dist[i] <= cut_out_range[1]: dist[i] = 1 else: dist[i] = 0 # write to the disk f = open(output, 'w') for d in dist:
fr = np.array(vol2sf(fr, r, b)) fi = np.array(vol2sf(fi, r, b)) fr2 = np.array(vol2sf(fr2, r, b)) fi2 = np.array(vol2sf(fi2, r, b)) fr2 = fr2 * w fi2 = fi2 * w res = frm_fourier_corr(fr2, fi2, fr, fi, return_real=True) # 3. add two volumes and get the final result # res = frm_constrained_corr(fr2, w, fr, m) + frm_constrained_corr(fi2, w, fi, m) ang = frm_find_best_angle(res, b) dist.append(rotation_distance(ang, [phi, psi, the])) # 4. compare with the constrained version res = frm_fourier_constrained_corr(fr2, fi2, w, fr, fi, m, return_real=True) ang2 = frm_find_best_angle(res, b) dist2.append(rotation_distance(ang2, [phi, psi, the])) # 5. do it in the soft way # res = soft_fourier_corr(fr2, fi2, fr, fi, return_real=True) res = soft_fourier_constrained_corr(fr2, fi2, w, fr, fi, m, return_real=True) ang3 = soft_find_best_angle(res, b) dist3.append(rotation_distance(ang3, [phi, psi, the])) print dist[-1], dist2[-1], dist3[-1]
import numpy as np from sh.soft import * from sh.frm import * from pytom.tools.maths import rotation_distance from pytom_volume import * from sh.vol2sf import vol2sf b = 16 v = read('/fs/home/ychen/matlab/template/binning/temp80SRibosome_bin2.em') v2 = vol(v) r = 8 for i in xrange(100): phi = np.random.randint(360) psi = np.random.randint(360) the = np.random.randint(180) f = vol2sf(v, r, b) rotateSpline(v, v2, phi, psi, the) g = vol2sf(v2, r, b) # 1. the soft way res = soft_corr(g, f) ang = soft_find_best_angle(res, b) # 2. the frm way # res = frm_corr(g, f) # ang2 = frm_find_best_angle(res, b) print rotation_distance(ang, [phi, psi, the]) #, rotation_distance(ang2, [phi, psi, the]), rotation_distance(ang, ang2)
b = 32 start = -60 end = 60 wedge = create_wedge_sf(start, end, b) # m1 = wedge+0.001 whole = np.ones(4 * b**2) sf = np.array(vol2sf(v, r, b)) sf = sf - np.min(sf) + 1. # set all values above 1 # sf = sf / m1 # sf = create_wedge_sf(start, end, b, 1, 10) sf2 = sf * wedge res = frm_corr(sf2, sf) ang = frm_find_best_angle(res, b) print ang, rotation_distance([0, 0, 0], ang) # print res.max(), res[0][b][b] res = frm_constrained_corr(sf2, wedge, sf, whole) ang = frm_find_best_angle(res, b) print ang, rotation_distance([0, 0, 0], ang) # do it in soft way! # res2 = soft_corr(sf, sf2) res2 = soft_constrained_corr(sf2, wedge, sf, whole) ang2 = soft_find_best_angle(res2, b) print ang2, rotation_distance([0, 0, 0], ang2)