def iterMdRows(md): """ Iterate over the rows of the given metadata. """ # If md is string, take as filename and create the metadata if isinstance(md, basestring): md = xmipp.MetaData(md) row = XmippMdRow() for objId in md: row.readFromMd(md, objId) yield row
def writeCTFModel(ctfModel, ctfFile): """ Given a CTFModel object write as Xmipp ctfparam """ md = xmipp.MetaData() objId = md.addObject() ctfRow = XmippMdRow() ctfModelToRow(ctfModel, ctfRow) ctfRow.writeToMd(md, objId) md.setColumnFormat(False) md.write(ctfFile)
def writeSetOfCoordinates(posDir, coordSet): """ Write a pos file on metadata format for each micrograph on the coordSet. Params: posDir: the directory where the .pos files will be written. coordSet: the SetOfCoordinates that will be read. """ posFiles = [] boxSize = coordSet.getBoxSize() or 100 # Write pos metadatas (one per micrograph) for mic in coordSet.iterMicrographs(): micName = mic.getFileName() posFn = join(posDir, replaceBaseExt(micName, "pos")) md = xmipp.MetaData() for coord in coordSet.iterCoordinates(micrograph=mic): objId = md.addObject() coordRow = XmippMdRow() coordinateToRow(coord, coordRow) coordRow.writeToMd(md, objId) if not md.isEmpty(): md2 = xmipp.MetaData() objId = md2.addObject() md2.setValue(xmipp.MDL_PICKING_MICROGRAPH_STATE, 'Manual', objId) # Write header block md2.write('header@%s' % posFn) # Write particles block md.write('particles@%s' % posFn, xmipp.MD_APPEND) posFiles.append(posFn) # Write config.xmd metadata configFn = join(posDir, 'config.xmd') md = xmipp.MetaData() # Write properties block objId = md.addObject() micName = removeBaseExt(micName) md.setValue(xmipp.MDL_MICROGRAPH, str(micName), objId) #md.setValue(xmipp.MDL_COLOR, int(-16776961), objId) md.setValue(xmipp.MDL_PICKING_PARTICLE_SIZE, int(boxSize), objId) md.setValue(xmipp.MDL_PICKING_STATE, 'Manual', objId) md.write('properties@%s' % configFn) # Write filters block md = xmipp.MetaData() objId = md.addObject() md.setValue(xmipp.MDL_MACRO_CMD, 'Gaussian_Blur...', objId) md.setValue(xmipp.MDL_MACRO_CMD_ARGS, 'sigma=2', objId) md.write('filters@%s' % configFn, xmipp.MD_APPEND) return posFiles
def writeSetOfClassesVol(classesVolSet, filename, classesBlock='classes'): """ This function will write a SetOfClassesVol as Xmipp metadata. Params: classesVolSet: the SetOfClassesVol instance. filename: the filename where to write the metadata. """ classFn = '%s@%s' % (classesBlock, filename) classMd = xmipp.MetaData() classMd.write(classFn) # Empty write to ensure the classes is the first block classRow = XmippMdRow() for classVol in classesVolSet: classVolToRow(classVol, classRow) classRow.writeToMd(classMd, classMd.addObject()) ref = class3D.getObjId() imagesFn = 'class%06d_images@%s' % (ref, filename) imagesMd = xmipp.MetaData() imgRow = XmippMdRow() for vol in classVol: volumeToRow(vol, imgRow) imgRow.writeToMd(imagesMd, imagesMd.addObject()) imagesMd.write(imagesFn, xmipp.MD_APPEND) classMd.write(classFn, xmipp.MD_APPEND) # Empty write to ensure the classes is the first block
def getMdFirstRow(filename): """ Create a MetaData but only read the first row. This method should be used for validations of labels or metadata size, but the full metadata is not needed. """ md = xmipp.MetaData() md.read(filename, 1) if md.getParsedLines(): row = XmippMdRow() row.readFromMd(md, md.firstObject()) else: row = None return row
def writeSetOfClasses2D(classes2DSet, filename, classesBlock='classes', writeParticles=True): """ This function will write a SetOfClasses2D as Xmipp metadata. Params: classes2DSet: the SetOfClasses2D instance. filename: the filename where to write the metadata. """ classFn = '%s@%s' % (classesBlock, filename) classMd = xmipp.MetaData() classMd.write(classFn) # Empty write to ensure the classes is the first block classRow = XmippMdRow() for class2D in classes2DSet: class2DToRow(class2D, classRow) classRow.writeToMd(classMd, classMd.addObject()) if writeParticles: ref = class2D.getObjId() imagesFn = 'class%06d_images@%s' % (ref, filename) imagesMd = xmipp.MetaData() imgRow = XmippMdRow() if class2D.getSize() > 0: for img in class2D: particleToRow(img, imgRow) imgRow.writeToMd(imagesMd, imagesMd.addObject()) imagesMd.write(imagesFn, xmipp.MD_APPEND) classMd.write(classFn, xmipp.MD_APPEND) # Empty write to ensure the classes is the first block
def setOfImagesToMd(imgSet, md, imgToFunc, **kwargs): """ This function will fill Xmipp metadata from a SetOfMicrographs Params: imgSet: the set of images to be converted to metadata md: metadata to be filled rowFunc: this function can be used to setup the row before adding to metadata. """ if 'alignType' not in kwargs: kwargs['alignType'] = imgSet.getAlignment() for img in imgSet: objId = md.addObject() imgRow = XmippMdRow() imgToFunc(img, imgRow, **kwargs) imgRow.writeToMd(md, objId)
def writeSetOfMovies(moviesSet, filename, moviesBlock='movies'): """ This function will write a SetOfMovies as Xmipp metadata. Params: moviesSet: the SetOfMovies instance. filename: the filename where to write the metadata. """ for movie in moviesSet: ref = movie.getObjId() micrographsFn = 'movie%06d_micrographs@%s' % (ref, filename) micrographsMd = xmipp.MetaData() micRow = XmippMdRow() for mic in movie: micrographToRow(mic, micRow) micRow.writeToMd(micrographsMd, micrographsMd.addObject()) micrographsMd.write(micrographsFn, xmipp.MD_APPEND)
def micrographToCTFParam(mic, ctfparam): """ This function is used to convert a Micrograph object to the .ctfparam metadata needed by some Xmipp programs. If the micrograph already comes from xmipp, the ctfparam will be returned, if not, the new file. """ ctf = mic.getCTF() if hasattr(ctf, '_xmippMd'): return ctf._xmippMd.get() md = xmipp.MetaData() md.setColumnFormat(False) row = XmippMdRow() ctfModelToRow(ctf, row) acquisitionToRow(mic.getAcquisition(), row) row.writeToMd(md, md.addObject()) md.write(ctfparam) return ctfparam
def writeSetOfDefocusGroups(defocusGroupSet, fnDefocusGroup): # also metadata """ Write a defocuGroupSet on metadata format. Params: defocusGroupSet: the SetOfDefocus that will be read. fnDefocusGroup: The file where defocusGroup should be written. """ md = xmipp.MetaData() for defocusGroup in defocusGroupSet: objId = md.addObject() defocusGroupRow = XmippMdRow() defocusGroupSetToRow(defocusGroup, defocusGroupRow) defocusGroupRow.setValue(xmipp.MDL_CTF_GROUP, defocusGroup.getObjId()) defocusGroupRow.setValue(xmipp.MDL_MIN, defocusGroup.getDefocusMin()) defocusGroupRow.setValue(xmipp.MDL_MAX, defocusGroup.getDefocusMax()) defocusGroupRow.setValue(xmipp.MDL_AVG, defocusGroup.getDefocusAvg()) defocusGroupRow.writeToMd(md, objId) md.write(fnDefocusGroup) defocusGroupSet._xmippMd = String(fnDefocusGroup)
def writeSetOfMicrographsPairs(uSet, tSet, filename): """ This function will write a MicrographsTiltPair as Xmipp metadata. Params: uSet: the untilted set of micrographs to be written tSet: the tilted set of micrographs to be written filename: the filename where to write the metadata. """ md = xmipp.MetaData() for micU, micT in izip(uSet, tSet): objId = md.addObject() pairRow = XmippMdRow() pairRow.setValue(xmipp.MDL_ITEM_ID, long(micU.getObjId())) pairRow.setValue(xmipp.MDL_MICROGRAPH, micU.getFileName()) pairRow.setValue(xmipp.MDL_MICROGRAPH_TILTED, micT.getFileName()) pairRow.writeToMd(md, objId) md.write(filename)
def writeSetOfCTFs(ctfSet, mdCTF): """ Write a ctfSet on metadata format. Params: ctfSet: the SetOfCTF that will be read. mdCTF: The file where metadata should be written. """ md = xmipp.MetaData() for ctfModel in ctfSet: objId = md.addObject() ctfRow = XmippMdRow() ctfRow.setValue(xmipp.MDL_MICROGRAPH, ctfModel.getMicFile()) if ctfModel.getPsdFile(): ctfRow.setValue(xmipp.MDL_PSD, ctfModel.getPsdFile()) ctfModelToRow(ctfModel, ctfRow) ctfRow.writeToMd(md, objId) md.write(mdCTF) ctfSet._xmippMd = String(mdCTF)
def _appendRctImages(self, particles): blockMd = "class%06d_images@%s" % (particles.getObjId(), self.rctClassesFn) classMd = xmipp.MetaData() uImages = self.inputParticlesTiltPair.get().getUntilted() tImages = self.inputParticlesTiltPair.get().getTilted() sangles = self.inputParticlesTiltPair.get().getCoordsPair().getAngles() uMics = self.inputParticlesTiltPair.get().getCoordsPair().getMicsPair().getUntilted() tMics = tImages.getCoordinates().getMicrographs() scaleFactor = uImages.getSamplingRate()/particles.getSamplingRate() for img in particles: imgId = img.getObjId() uImg = uImages[imgId] tImg = tImages[imgId] objId = classMd.addObject() pairRow = XmippMdRow() pairRow.setValue(xmipp.MDL_IMAGE, getImageLocation(uImg)) uCoord = uImg.getCoordinate() micId = uCoord.getMicId() uMic = uMics[micId] angles = sangles[micId] pairRow.setValue(xmipp.MDL_MICROGRAPH, uMic.getFileName()) pairRow.setValue(xmipp.MDL_XCOOR, uCoord.getX()) pairRow.setValue(xmipp.MDL_YCOOR, uCoord.getY()) pairRow.setValue(xmipp.MDL_ENABLED, 1) pairRow.setValue(xmipp.MDL_ITEM_ID, long(imgId)) pairRow.setValue(xmipp.MDL_REF, 1) alignment = img.getTransform() # Scale alignment by scaleFactor alignment.scale(scaleFactor) alignmentToRow(alignment, pairRow, alignType=ALIGN_2D) pairRow.setValue(xmipp.MDL_IMAGE_TILTED, getImageLocation(tImg)) tMic = tMics[micId] pairRow.setValue(xmipp.MDL_MICROGRAPH_TILTED, tMic.getFileName()) (angleY, angleY2, angleTilt) = angles.getAngles() pairRow.setValue(xmipp.MDL_ANGLE_Y, float(angleY)) pairRow.setValue(xmipp.MDL_ANGLE_Y2, float(angleY2)) pairRow.setValue(xmipp.MDL_ANGLE_TILT, float(angleTilt)) pairRow.writeToMd(classMd, objId) classMd.write(blockMd, xmipp.MD_APPEND)
def rowFromMd(md, objId): row = XmippMdRow() row.readFromMd(md, objId) return row
def _appendRctImages(self, particles): blockMd = "class%06d_images@%s" % (particles.getObjId(), self.rctClassesFn) classMd = xmipp.MetaData() uImages = self.inputParticlesTiltPair.get().getUntilted() tImages = self.inputParticlesTiltPair.get().getTilted() sangles = self.inputParticlesTiltPair.get().getCoordsPair().getAngles() uMics = self.inputParticlesTiltPair.get().getCoordsPair().getMicsPair( ).getUntilted() tMics = tImages.getCoordinates().getMicrographs() scaleFactor = uImages.getSamplingRate() / particles.getSamplingRate() for img in particles: imgId = img.getObjId() uImg = uImages[imgId] tImg = tImages[imgId] if uImg is None or tImg is None: print ">>> Warning, for id %d, tilted or untilted particle was not found. Ignored." % imgId else: objId = classMd.addObject() pairRow = XmippMdRow() pairRow.setValue(xmipp.MDL_IMAGE, getImageLocation(uImg)) uCoord = uImg.getCoordinate() micId = uCoord.getMicId() uMic = uMics[micId] angles = sangles[micId] pairRow.setValue(xmipp.MDL_MICROGRAPH, uMic.getFileName()) pairRow.setValue(xmipp.MDL_XCOOR, uCoord.getX()) pairRow.setValue(xmipp.MDL_YCOOR, uCoord.getY()) pairRow.setValue(xmipp.MDL_ENABLED, 1) pairRow.setValue(xmipp.MDL_ITEM_ID, long(imgId)) pairRow.setValue(xmipp.MDL_REF, 1) alignment = img.getTransform() # Scale alignment by scaleFactor alignment.scale(scaleFactor) alignmentToRow(alignment, pairRow, alignType=ALIGN_2D) pairRow.setValue(xmipp.MDL_IMAGE_TILTED, getImageLocation(tImg)) tMic = tMics[micId] pairRow.setValue(xmipp.MDL_MICROGRAPH_TILTED, tMic.getFileName()) (angleY, angleY2, angleTilt) = angles.getAngles() pairRow.setValue(xmipp.MDL_ANGLE_Y, float(angleY)) pairRow.setValue(xmipp.MDL_ANGLE_Y2, float(angleY2)) pairRow.setValue(xmipp.MDL_ANGLE_TILT, float(angleTilt)) pairRow.writeToMd(classMd, objId) classMd.write(blockMd, xmipp.MD_APPEND)