def rowToAlignment(alignmentRow, alignType): """ is2D == True-> matrix is 2D (2D images alignment) otherwise matrix is 3D (3D volume alignment or projection) invTransform == True -> for xmipp implies projection """ is2D = alignType == em.ALIGN_2D inverseTransform = True #alignType == em.ALIGN_PROJ if alignmentRow.containsAny(ALIGNMENT_DICT): alignment = em.Transform() angles = numpy.zeros(3) shifts = numpy.zeros(3) angles[2] = alignmentRow.getValue(md.RLN_ORIENT_PSI, 0.) shifts[0] = alignmentRow.getValue(md.RLN_ORIENT_ORIGIN_X, 0.) shifts[1] = alignmentRow.getValue(md.RLN_ORIENT_ORIGIN_Y, 0.) if not is2D: angles[0] = alignmentRow.getValue(md.RLN_ORIENT_ROT, 0.) angles[1] = alignmentRow.getValue(md.RLN_ORIENT_TILT, 0.) shifts[2] = alignmentRow.getValue(md.RLN_ORIENT_ORIGIN_Z, 0.) M = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(M) else: alignment = None return alignment
def createOutputStep(self): vols = [] for vol in self._iterInputVolumes(): outVol = em.Volume() outVol.setLocation(vol.outputName) outVol.setObjLabel(vol.getObjLabel()) outVol.setObjComment(vol.getObjComment()) #set transformation matrix volId = vol.getObjId() fhInputTranMat = self._getExtraPath('transformation-matrix_vol%06d.txt'%volId) transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile,(4,4)) transform = em.Transform() transform.setMatrix(transformationMat) outVol.setTransform(transform) vols.append(outVol) if len(vols) > 1: volSet = self._createSetOfVolumes() volSet.setSamplingRate(self.inputReference.get().getSamplingRate()) for vol in vols: volSet.append(vol) outputArgs = {'outputVolumes': volSet} else: vols[0].setSamplingRate(self.inputReference.get().getSamplingRate()) outputArgs = {'outputVolume': vols[0]} self._defineOutputs(**outputArgs) for pointer in self.inputVolumes: self._defineSourceRelation(pointer, outputArgs.values()[0])
def createOutputStep(self): outVolFn = self._getExtraPath("inputVolumeAligned.vol") outVol = em.Volume() outVol.setLocation(outVolFn) #set transformation matrix fhInputTranMat = self._getExtraPath('transformation-matrix.txt') transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = em.Transform() transform.setMatrix(transformationMat) outVol.setTransform(transform) outVol.setSamplingRate(self.inputVolume.get().getSamplingRate()) outputArgs = {'outputVolume': outVol} self._defineOutputs(**outputArgs) self._defineSourceRelation(self.inputVolume, outVol) #particles.... outParticlesFn = self._getExtraPath('outputParticles.xmd') outputParticles = self._createSetOfParticles() outputParticles.copyInfo(self.inputParticles.get()) outputParticles.setAlignmentProj() readSetOfParticles(outParticlesFn, outputParticles) outputArgs = {'outputParticles': outputParticles} self._defineOutputs(**outputArgs) self._defineSourceRelation(self.inputParticles, outputParticles)
def rowToAlignment(subpart): inverseTransform = True alignment = em.Transform() angles = numpy.zeros(3) shifts = numpy.zeros(3) shifts[0] = subpart.rlnOriginX shifts[1] = subpart.rlnOriginY shifts[2] = subpart.rlnOriginZ or 0 angles[0] = subpart.rlnAngleRot angles[1] = subpart.rlnAngleTilt angles[2] = subpart.rlnAnglePsi M = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(M) return alignment
def rowToAlignment(alignmentRow, samplingRate): """ Return an Transform object representing the Alignment from a given parFile row. """ angles = np.zeros(3) shifts = np.zeros(3) alignment = em.Transform() # PSI THETA PHI SHX SHY angles[0] = float(alignmentRow.get('PSI')) angles[1] = float(alignmentRow.get('THETA')) angles[2] = float(alignmentRow.get('PHI')) shifts[0] = float(alignmentRow.get('SHX')) / samplingRate shifts[1] = float(alignmentRow.get('SHY')) / samplingRate M = matrixFromGeometry(shifts, angles) alignment.setMatrix(M) return alignment
def rowToAlignment(alignmentList, alignType): """ is2D == True-> matrix is 2D (2D images alignment) otherwise matrix is 3D (3D volume alignment or projection) invTransform == True -> for xmipp implies projection """ is2D = alignType == em.ALIGN_2D inverseTransform = alignType == em.ALIGN_PROJ alignment = em.Transform() angles = numpy.zeros(3) shifts = numpy.zeros(3) shifts[0] = alignmentList[3] shifts[1] = alignmentList[4] if not is2D: angles[0] = alignmentList[0] angles[1] = alignmentList[1] shifts[2] = 0 angles[2] = alignmentList[2] else: psi = alignmentList[0] rot = alignmentList[1] if rot !=0. and psi !=0: print "HORROR rot and psi are different from zero" angles[0] = alignmentList[0] + alignmentList[1] #if alignment matrix = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(matrix) #FIXME: now are also storing the alignment parameters since # the conversions to the Transform matrix have not been extensively tested. # After this, we should only keep the matrix #for paramName, label in ALIGNMENT_DICT.iteritems(): # if alignmentRow.hasLabel(label): # setattr(alignment, paramName, alignmentRow.getValueAsObject(label)) return alignment
def rowToAlignment(alignmentList, alignType): """ is2D == True-> matrix is 2D (2D images alignment) otherwise matrix is 3D (3D volume alignment or projection) invTransform == True -> for xmipp implies projection """ # use all angles in 2D since we might have mirrors # is2D = alignType == em.ALIGN_2D inverseTransform = alignType == em.ALIGN_PROJ alignment = em.Transform() angles = numpy.zeros(3) shifts = numpy.zeros(3) shifts[0] = alignmentList[3] shifts[1] = alignmentList[4] shifts[2] = 0 angles[0] = alignmentList[0] angles[1] = alignmentList[1] angles[2] = alignmentList[2] matrix = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(matrix) return alignment
def alignParticlesStep(self): fhInputTranMat = self._getExtraPath('transformation-matrix.txt') outParticlesFn = self._getExtraPath('outputParticles.xmd') transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = em.Transform() transform.setMatrix(transformationMat) resultMat = Transform() outputParts = md.MetaData() mdToAlign = md.MetaData(self.imgsInputFn) for row in md.iterRows(mdToAlign): inMat = rowToAlignment(row, ALIGN_PROJ) partTransformMat = inMat.getMatrix() partTransformMatrix = np.matrix(partTransformMat) newTransformMatrix = np.matmul(transformationMat, partTransformMatrix) resultMat.setMatrix(newTransformMatrix) rowOut = md.Row() rowOut.copyFromRow(row) alignmentToRow(resultMat, rowOut, ALIGN_PROJ) rowOut.addToMd(outputParts) outputParts.write(outParticlesFn)
def create_subparticles(particle, symmetry_matrices, subparticle_vector_list, part_image_size, randomize, subparticles_total, align_subparticles): """ Obtain all subparticles from a given particle and set the properties of each such subparticle. """ # Euler angles that take particle to the orientation of the model matrix_particle = inv(particle.getTransform().getMatrix()) shifts, angles = geometryFromMatrix(matrix_particle) subparticles = [] subparticles_total += 1 symmetry_matrix_ids = range(1, len(symmetry_matrices) + 1) if randomize: # randomize the order of symmetry matrices, prevents preferred views random.shuffle(symmetry_matrix_ids) for subparticle_vector in subparticle_vector_list: matrix_from_subparticle_vector = subparticle_vector.get_matrix() for symmetry_matrix_id in symmetry_matrix_ids: # symmetry_matrix_id can be later written out to find out # which symmetry matrix created this subparticle symmetry_matrix = np.array(symmetry_matrices[symmetry_matrix_id - 1][0:3, 0:3]) subpart = particle.clone() m = np.matmul( matrix_particle[0:3, 0:3], (np.matmul(symmetry_matrix.transpose(), matrix_from_subparticle_vector.transpose()))) angles_org = -1.0 * np.ones(3) * euler_from_matrix(m, 'szyz') if align_subparticles: angles = -1.0 * np.ones(3) * euler_from_matrix(m, 'szyz') else: m2 = np.matmul(matrix_particle[0:3, 0:3], symmetry_matrix.transpose()) angles = -1.0 * np.ones(3) * euler_from_matrix(m2, 'szyz') # subparticle origin d = subparticle_vector.get_length() x = -m[0, 2] * d + shifts[0] y = -m[1, 2] * d + shifts[1] z = -m[2, 2] * d # save the subparticle coordinates (integer part) relative to the # user given image size and as a small shift in the origin (decimal part) x_d, x_i = math.modf(x) y_d, y_i = math.modf(y) alignment = em.Transform() alignmentOrg = em.Transform() M = matrixFromGeometry(np.array([-x_d, -y_d, 0]), angles, True) MOrg = matrixFromGeometry(np.array([-x_d, -y_d, 0]), angles_org, True) alignment.setMatrix(M) alignmentOrg.setMatrix(MOrg) subpart._transorg = alignmentOrg.clone() subpart.setTransform(alignment) coord = Coordinate() coord.setObjId(None) coord.setX(int(part_image_size / 2) - x_i) coord.setY(int(part_image_size / 2) - y_i) coord.setMicId(particle.getObjId()) if subpart.hasCTF(): ctf = subpart.getCTF() ctf.setDefocusU(subpart.getDefocusU() + z) ctf.setDefocusV(subpart.getDefocusV() + z) subpart.setCoordinate(coord) coord._subparticle = subpart.clone() subparticles.append(subpart) return subparticles