def createOutputStep(self): outVolFn = self._getExtraPath("inputVolumeAligned.mrc") Ts = self.inputVolume.get().getSamplingRate() self.runJob("xmipp_image_header", "-i %s --sampling_rate %f" % (outVolFn, Ts)) outVol = Volume() outVol.setLocation(outVolFn) #set transformation matrix fhInputTranMat = self._getExtraPath('transformation-matrix.txt') transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = Transform() transform.setMatrix(transformationMat) outVol.setTransform(transform) outVol.setSamplingRate(Ts) 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 alignParticlesStep(self): fhInputTranMat = self._getExtraPath('transformation-matrix.txt') outParticlesFn = self._getExtraPath('outputParticles.xmd') transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = 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) cleanPath(self.imgsInputFn)
def createOutputStep(self): Ts = self.inputReference.get().getSamplingRate() vols = [] idx = 1 for vol in self._iterInputVolumes(): outVol = Volume() fnOutVol = self._getExtraPath("vol%02d.mrc" % idx) outVol.setLocation(fnOutVol) outVol.setObjComment(vol.getObjComment()) #set transformation matrix fhInputTranMat = self._getExtraPath( 'transformation-matrix_vol%06d.txt' % idx) transMatFromFile = np.loadtxt(fhInputTranMat) transformationMat = np.reshape(transMatFromFile, (4, 4)) transform = Transform() transform.setMatrix(transformationMat) outVol.setTransform(transform) vols.append(outVol) # Set the sampling rate in the mrc header self.runJob("xmipp_image_header", "-i %s --sampling_rate %f" % (fnOutVol, Ts)) idx += 1 if len(vols) > 1: volSet = self._createSetOfVolumes() volSet.setSamplingRate(Ts) for vol in vols: volSet.append(vol) outputArgs = {'outputVolumes': volSet} else: vols[0].setSamplingRate(Ts) outputArgs = {'outputVolume': vols[0]} self._defineOutputs(**outputArgs) if len(vols) > 1: for pointer in self.inputVolumes: self._defineSourceRelation(pointer, outputArgs['outputVolumes']) else: for pointer in self.inputVolumes: self._defineSourceRelation(pointer, outputArgs['outputVolume'])
def rowToAlignment(alignmentRow, samplingRate): """ Return an Transform object representing the Alignment from a given parFile row. :param alignmentRow: input row object :param samplingRate: input pixel size :return Transform object """ angles = np.zeros(3) shifts = np.zeros(3) alignment = 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 are converted from Angstroms to px 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(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 == ALIGN_2D inverseTransform = True # alignType == em.ALIGN_PROJ alignment = Transform() angles = numpy.zeros(3) shifts = numpy.zeros(3) angles[2] = alignmentRow.get('ANGLE_PSI') shifts[0] = alignmentRow.get('SHIFTX') shifts[1] = alignmentRow.get('SHIFTY') if not is2D: angles[0] = alignmentRow.get('ANGLE_PHI') angles[1] = alignmentRow.get('ANGLE_THE') M = matrixFromGeometry(shifts, angles, inverseTransform) alignment.setMatrix(M) return alignment
def createOutputStep(self): input = self.input.get() imgSetOut = self._createSetOfParticles() imgSetOut.setSamplingRate(input.getSamplingRate()) imgSetOut.setAlignmentProj() for i, subtomo in enumerate(input.iterItems()): idx = subtomo.getObjId() p = Particle() p.setLocation(ih._convertToLocation((i+1, self._getExtraPath("projections.mrcs")))) p._subtomogramID = String(idx) if type(subtomo) == SubTomogram: if subtomo.hasCoordinate3D(): coord = Coordinate() coord.setX(subtomo.getCoordinate3D().getX(const.BOTTOM_LEFT_CORNER)) coord.setY(subtomo.getCoordinate3D().getY(const.BOTTOM_LEFT_CORNER)) p.setCoordinate(coord) p.setClassId(subtomo.getClassId()) if subtomo.hasTransform(): transform = Transform() transform.setMatrix(subtomo.getTransform().getMatrix()) p.setTransform(transform) imgSetOut.append(p) imgSetOut.setObjComment(self.getSummary(imgSetOut)) self._defineOutputs(outputParticles=imgSetOut) self._defineSourceRelation(self.input, imgSetOut) if self.radAvg.get(): avgFile = self._getExtraPath("average.xmp") imgh = ih() avgImage = imgh.computeAverage(imgSetOut) avgImage.write(avgFile) avg = Particle() avg.setLocation(1, avgFile) avg.copyInfo(imgSetOut) self._defineOutputs(outputAverage=avg) self._defineSourceRelation(self.input, avg)
def realignStep(self): inputMdName = self._getExtraPath('inputClasses.xmd') writeSetOfClasses2D(self.inputClasses.get(), inputMdName, writeParticles=True) centeredStackName = self._getExtraPath('centeredStack.stk') self._params = {'input': inputMdName, 'output': centeredStackName} args = ('-i %(input)s -o %(output)s --save_metadata_transform') self.runJob("xmipp_transform_center_image", args % self._params, numberOfMpi=1) centeredMdName = centeredStackName.replace('stk', 'xmd') centeredMd = md.MetaData(centeredMdName) centeredStack = md.MetaData(centeredStackName) listName = [] listTransform=[] for rowStk in md.iterRows(centeredStack): listName.append(rowStk.getValue(md.MDL_IMAGE)) for rowMd in md.iterRows(centeredMd): listTransform.append(rowToAlignment(rowMd, ALIGN_2D)) mdNewClasses = md.MetaData() for i, row in enumerate(md.iterRows(inputMdName)): newRow = md.Row() newRow.setValue(md.MDL_IMAGE, listName[i]) refNum = row.getValue(md.MDL_REF) newRow.setValue(md.MDL_REF, refNum) classCount = row.getValue(md.MDL_CLASS_COUNT) newRow.setValue(md.MDL_CLASS_COUNT, classCount) newRow.addToMd(mdNewClasses) mdNewClasses.write('classes@' + self._getExtraPath('final_classes.xmd'), MD_APPEND) mdImages = md.MetaData() i=0 mdBlocks = md.getBlocksInMetaDataFile(inputMdName) resultMat = Transform() for block in mdBlocks: if block.startswith('class00'): newMat = listTransform[i] newMatrix = newMat.getMatrix() mdClass = md.MetaData(block + "@" + inputMdName) mdNewClass = md.MetaData() i+=1 for rowIn in md.iterRows(mdClass): #To create the transformation matrix (and its parameters) # for the realigned particles if rowIn.getValue(md.MDL_ANGLE_PSI)!=0: flag_psi=True if rowIn.getValue(md.MDL_ANGLE_ROT)!=0: flag_psi=False inMat = rowToAlignment(rowIn, ALIGN_2D) inMatrix = inMat.getMatrix() resultMatrix = np.dot(newMatrix,inMatrix) resultMat.setMatrix(resultMatrix) rowOut=md.Row() rowOut.copyFromRow(rowIn) alignmentToRow(resultMat, rowOut, ALIGN_2D) if flag_psi==False: newAngle = rowOut.getValue(md.MDL_ANGLE_PSI) rowOut.setValue(md.MDL_ANGLE_PSI, 0.) rowOut.setValue(md.MDL_ANGLE_ROT, newAngle) #To create the new coordinates for the realigned particles inPoint = np.array([[0.],[0.],[0.],[1.]]) invResultMat = np.linalg.inv(resultMatrix) centerPoint = np.dot(invResultMat,inPoint) rowOut.setValue(md.MDL_XCOOR, rowOut.getValue( md.MDL_XCOOR)+int(centerPoint[0])) rowOut.setValue(md.MDL_YCOOR, rowOut.getValue( md.MDL_YCOOR)+int(centerPoint[1])) rowOut.addToMd(mdNewClass) mdNewClass.write(block + "@" + self._getExtraPath( 'final_classes.xmd'), MD_APPEND) mdImages.unionAll(mdNewClass) mdImages.write(self._getExtraPath('final_images.xmd'))