def objectToRow(obj, row, attrDict, extraLabels={}): """ This function will convert an EMObject into a XmippMdRow. Params: obj: the EMObject instance (input) row: the XmippMdRow instance (output) attrDict: dictionary with the map between obj attributes(keys) and row MDLabels in Xmipp (values). extraLabels: a list with extra labels that could be included as _xmipp_labelName """ if obj.isEnabled(): enabled = True else: enabled = False row.setValue(md.RLN_IMAGE_ENABLED, enabled) for attr, label in attrDict.iteritems(): if hasattr(obj, attr): valueType = md.label2Python(label) row.setValue(label, valueType(getattr(obj, attr).get())) attrLabels = attrDict.values() for label in extraLabels: attrName = '_' + md.label2Str(label) if label not in attrLabels and hasattr(obj, attrName): value = obj.getAttributeValue(attrName) row.setValue(label, value)
def rowToObject(row, obj, attrDict, extraLabels={}): """ This function will convert from a XmippMdRow to an EMObject. Params: row: the XmippMdRow instance (input) obj: the EMObject instance (output) attrDict: dictionary with the map between obj attributes(keys) and row MDLabels in Xmipp (values). extraLabels: a list with extra labels that could be included as properties with the label name such as: _rlnSomeThing """ obj.setEnabled(row.getValue(md.RLN_IMAGE_ENABLED, 1) > 0) for attr, label in attrDict.iteritems(): value = row.getValue(label) if not hasattr(obj, attr): setattr(obj, attr, ObjectWrap(value)) else: getattr(obj, attr).set(value) attrLabels = attrDict.values() for label in extraLabels: if label not in attrLabels and row.hasLabel(label): labelStr = md.label2Str(label) setattr(obj, '_' + labelStr, row.getValueAsObject(label))
def test_particlesFromStar(self): """ Read a set of particles from an .star file. """ fnStar = self.getFile('relion_it020_data') print ">>> Reading star file: ", fnStar mdAll = md.MetaData(fnStar) print "labels: ", [md.label2Str(l) for l in mdAll.getActiveLabels()] print "size: ", mdAll.size()
def setRelionAttributes(obj, objRow, *labels): """ Set an attribute to obj from a label that is not basic ones. The new attribute will be named _rlnLabelName and the datatype will be set correctly. """ for label in labels: setattr(obj, '_%s' % md.label2Str(label), objRow.getValueAsObject(label))
def _findImagesPath(self, label, warnings=True): row = md.getFirstRow(self._starFile) if row is None: raise Exception("Can not import from an empty metadata: %s" % self._starFile) if not row.containsLabel(label): raise Exception("Label *%s* is missing in metadata: %s" % (md.label2Str(label), self._starFile)) index, fn = relionToLocation(row.getValue(label)) self._imgPath = findRootFrom(self._starFile, fn) if warnings and self._imgPath is None: self.protocol.warning( "Binary data was not found from metadata: %s" % self._starFile) if (self._starFile.endswith('_data.star') and self._getModelFile(self._starFile)): self._modelStarFile = self._getModelFile(self._starFile) modelRow = md.getFirstRow(self._modelStarFile) classDimensionality = modelRow.getValue( 'rlnReferenceDimensionality') self._optimiserFile = self._starFile.replace( '_data.star', '_optimiser.star') if not exists(self._optimiserFile): raise Exception("Missing required optimiser star file: %s" % self._optimiserFile) optimiserRow = md.getFirstRow(self._optimiserFile) autoRefine = optimiserRow.containsLabel('rlnModelStarFile2') self.alignType = ALIGN_PROJ if not autoRefine: if classDimensionality == 3: self._classesFunc = self.protocol._createSetOfClasses3D else: self._classesFunc = self.protocol._createSetOfClasses2D self.alignType = ALIGN_2D else: self._classesFunc = None else: self.alignType = ALIGN_NONE self._classesFunc = None self._modelStarFile = None modelRow = None # Check if the MetaData contains either MDL_MICROGRAPH_ID # or MDL_MICROGRAPH, this will be used when imported # particles to keep track of the particle's micrograph self._micIdOrName = (row.containsLabel('rlnMicrographName') or row.containsLabel('rlnMicrographId')) #init dictionary. It will be used in the preprocessing self.micDict = {} return row, modelRow
def _findImagesPath(self, label, warnings=True): row = md.getFirstRow(self._starFile) if row is None: raise Exception("Can not import from an empty metadata: %s" % self._starFile) if not row.containsLabel(label): raise Exception("Label *%s* is missing in metadata: %s" % (md.label2Str(label), self._starFile)) index, fn = relionToLocation(row.getValue(label)) self._imgPath = findRootFrom(self._starFile, fn) if warnings and self._imgPath is None: self.protocol.warning("Binary data was not found from metadata: %s" % self._starFile) if (self._starFile.endswith('_data.star') and self._getModelFile(self._starFile)): self._modelStarFile = self._getModelFile(self._starFile) modelRow = md.getFirstRow(self._modelStarFile) classDimensionality = modelRow.getValue('rlnReferenceDimensionality') self._optimiserFile = self._starFile.replace('_data.star', '_optimiser.star') if not exists(self._optimiserFile): raise Exception("Missing required optimiser star file: %s" % self._optimiserFile) optimiserRow = md.getFirstRow(self._optimiserFile) autoRefine = optimiserRow.containsLabel('rlnModelStarFile2') self.alignType = ALIGN_PROJ if not autoRefine: if classDimensionality == 3: self._classesFunc = self.protocol._createSetOfClasses3D else: self._classesFunc = self.protocol._createSetOfClasses2D self.alignType = ALIGN_2D else: self._classesFunc = None else: self.alignType = ALIGN_NONE self._classesFunc = None self._modelStarFile = None modelRow = None # Check if the MetaData contains either MDL_MICROGRAPH_ID # or MDL_MICROGRAPH, this will be used when imported # particles to keep track of the particle's micrograph self._micIdOrName = (row.containsLabel('rlnMicrographName') or row.containsLabel('rlnMicrographId')) #init dictionary. It will be used in the preprocessing self.micDict = {} return row, modelRow
def test_particlesFromStar(self): """ Read a set of particles from an .star file. """ fnStar = self.getFile('relion_it020_data') print ">>> Reading star file: ", fnStar mdAll = md.MetaData(fnStar) goldLabels = ['rlnVoltage', 'rlnDefocusU', 'rlnDefocusV', 'rlnDefocusAngle', 'rlnSphericalAberration', 'rlnAmplitudeContrast', 'rlnImageName', 'rlnImageId', 'rlnCoordinateX', 'rlnCoordinateY', 'rlnMagnificationCorrection', 'rlnNormCorrection', 'rlnMicrographName', 'rlnGroupNumber', 'rlnOriginX', 'rlnOriginY', 'rlnAngleRot', 'rlnAngleTilt', 'rlnAnglePsi', 'rlnClassNumber', 'rlnLogLikeliContribution', 'rlnNrOfSignificantSamples', 'rlnMaxValueProbDistribution'] self.assertEqual(goldLabels, [md.label2Str(l) for l in mdAll.getActiveLabels()]) self.assertEqual(4700, mdAll.size())
def test_particlesFromStar(self): """ Read a set of particles from an .star file. """ fnStar = self.getFile('relion_it020_data') print (">>> Reading star file: ", fnStar) mdAll = md.MetaData(fnStar) goldLabels = ['rlnVoltage', 'rlnDefocusU', 'rlnDefocusV', 'rlnDefocusAngle', 'rlnSphericalAberration', 'rlnAmplitudeContrast', 'rlnImageName', 'rlnImageId', 'rlnCoordinateX', 'rlnCoordinateY', 'rlnMagnificationCorrection', 'rlnNormCorrection', 'rlnMicrographName', 'rlnGroupNumber', 'rlnOriginX', 'rlnOriginY', 'rlnAngleRot', 'rlnAngleTilt', 'rlnAnglePsi', 'rlnClassNumber', 'rlnLogLikeliContribution', 'rlnNrOfSignificantSamples', 'rlnMaxValueProbDistribution'] self.assertEqual(goldLabels, [md.label2Str(l) for l in mdAll.getActiveLabels()]) self.assertEqual(4700, mdAll.size())
def _findPathAndCtf(self, label, warnings=True): """ Find the relative path from which the micrographs exists repect to the metadata location. Also check if it contains CTF information and their relative root. """ row = md.getFirstRow(self._mdFile) if row is None: raise Exception("Can not import from an empty metadata: " "%s" % self._mdFile) if not row.containsLabel(label): raise Exception("Label *%s* is missing in metadata: " "%s" % (md.label2Str(label), self._mdFile)) # take only the filename part after the @ index, fn = xmippToLocation(row.getValue(label)) self._imgPath = findRootFrom(self._mdFile, fn) if warnings and self._imgPath is None: self.protocol.warning("Binary data was not found from metadata: " "%s" % self._mdFile) if row.containsLabel(md.MDL_CTF_MODEL): self._ctfPath = findRootFrom(self._mdFile, row.getValue(md.MDL_CTF_MODEL)) else: self._ctfPath = None # means no CTF info from micrographs metadata if row.containsLabel(md.MDL_REF): self._classFunc = self.protocol._createSetOfClasses2D elif row.containsLabel(md.MDL_REF3D): self._classFunc = self.protocol._createSetOfClasses3D else: self._classLabel = None self._classFunc = None # Check if the MetaData contains either MDL_MICROGRAPH_ID # or MDL_MICROGRAPH, this will be used when imported # particles to keep track of the particle's micrograph self._micIdOrName = (row.containsLabel(md.MDL_MICROGRAPH_ID) or row.containsLabel(md.MDL_MICROGRAPH)) #init dictionary. It will be used in the preprocessing self.micDict = {} return row
def createAngDistribution(inputMd, splitLabel, classes): # List of list of 3 elements containing angleTilt, anglePsi, weight projectionListDict = {} def getCloseProjection(angleRot, angleTilt, projectionList): """ Get an existing projection close to angleRot, angleTilt. Return None if not found close enough. """ for projection in projectionList: if (abs(projection[0] - angleRot) <= 0.01 and abs(projection[1] - angleTilt) <= 0.01): return projection return None weight = 1. for row in md.iterRows(inputMd): splitValue = row.getValue(splitLabel) if classes and splitValue not in classes: continue angleRot = row.getValue('rlnAngleRot') angleTilt = row.getValue('rlnAngleTilt') if splitValue is None: raise Exception('Label %s not found' % md.label2Str(splitLabel)) if not splitValue in projectionListDict: projectionListDict[splitValue] = [] projectionList = projectionListDict[splitValue] projection = getCloseProjection(angleRot, angleTilt, projectionList) if projection is None: projectionList.append([angleRot, angleTilt, weight]) else: projection[2] = projection[2] + weight return projectionListDict
def particleToRow(part, partItem, **kwargs): """ Set labels values from Particle to md row. """ partRow = md.Row() coord = part.getCoordinate() if coord is not None: coordinateToRow(coord, partRow, md.RLN_MICROGRAPH_ID, copyId=False) if part.hasMicId(): partRow.setValue(md.RLN_MICROGRAPH_ID, long(part.getMicId())) # If the row does not contains the micrgraphs name # use a fake micrograph name using id to relion # could at least group for CTF using that if not partRow.hasLabel(md.RLN_MICROGRAPH_NAME): partRow.setValue(md.RLN_MICROGRAPH_NAME, 'fake_micrograph_%06d.mrc' % part.getMicId()) if part.hasAttribute('_rlnParticleId'): partRow.setValue(md.RLN_PARTICLE_ID, long(part._rlnParticleId.get())) imageToRow(part, partRow, md.RLN_IMAGE_NAME, **kwargs) for label, value in partRow._labelDict.iteritems(): labelStr = md.label2Str(label) setattr(partItem, labelStr, value)