def findRow(md, label, value): """ Query the metadata for a row with label=value. Params: md: metadata to query. label: label to check value value: value for equal condition Returns: XmippMdRow object of the row found. None if no row is found with label=value """ mdQuery = xmippLib.MetaData() # store result mdQuery.importObjects(md, xmippLib.MDValueEQ(label, value)) n = mdQuery.size() if n == 0: row = None elif n == 1: row = XmippMdRow() row.readFromMd(mdQuery, mdQuery.firstObject()) else: raise Exception( "findRow: more than one row found matching the query %s = %s" % (xmippLib.label2Str(label), value)) return row
def __setXmippImage(label): attr = '_xmipp_' + xmippLib.label2Str(label) if not hasattr(particle, attr): img = Image() setattr(particle, attr, img) img.setSamplingRate(particle.getSamplingRate()) else: img = getattr(particle, attr) img.setLocation(xmippToLocation(row.getValue(label)))
def calculateDeviationsStep(self, it): """ Calculate both angles and shifts devitations for all iterations """ SL = xmippLib.SymList() mdIter = xmippLib.MetaData() #for it in self.allIters(): mdIter.clear() SL.readSymmetryFile(self._symmetry[it]) md1 = xmippLib.MetaData(self.docFileInputAngles[it]) md2 = xmippLib.MetaData(self.docFileInputAngles[it - 1]) #ignore disabled, md1.removeDisabled() md2.removeDisabled() #first metadata file may not have shiftx and shifty if not md2.containsLabel(xmippLib.MDL_SHIFT_X): md2.addLabel(xmippLib.MDL_SHIFT_X) md2.addLabel(xmippLib.MDL_SHIFT_Y) md2.fillConstant(xmippLib.MDL_SHIFT_X, 0.) md2.fillConstant(xmippLib.MDL_SHIFT_Y, 0.) oldLabels = [ xmippLib.MDL_ANGLE_ROT, xmippLib.MDL_ANGLE_TILT, xmippLib.MDL_ANGLE_PSI, xmippLib.MDL_SHIFT_X, xmippLib.MDL_SHIFT_Y ] newLabels = [ xmippLib.MDL_ANGLE_ROT2, xmippLib.MDL_ANGLE_TILT2, xmippLib.MDL_ANGLE_PSI2, xmippLib.MDL_SHIFT_X2, xmippLib.MDL_SHIFT_Y2 ] md2.renameColumn(oldLabels, newLabels) md2.addLabel(xmippLib.MDL_SHIFT_X_DIFF) md2.addLabel(xmippLib.MDL_SHIFT_Y_DIFF) md2.addLabel(xmippLib.MDL_SHIFT_DIFF) mdIter.join1(md1, md2, xmippLib.MDL_IMAGE, xmippLib.INNER_JOIN) SL.computeDistance(mdIter, False, False, False) xmippLib.activateMathExtensions() #operate in sqlite shiftXLabel = xmippLib.label2Str(xmippLib.MDL_SHIFT_X) shiftX2Label = xmippLib.label2Str(xmippLib.MDL_SHIFT_X2) shiftXDiff = xmippLib.label2Str(xmippLib.MDL_SHIFT_X_DIFF) shiftYLabel = xmippLib.label2Str(xmippLib.MDL_SHIFT_Y) shiftY2Label = xmippLib.label2Str(xmippLib.MDL_SHIFT_Y2) shiftYDiff = xmippLib.label2Str(xmippLib.MDL_SHIFT_Y_DIFF) shiftDiff = xmippLib.label2Str(xmippLib.MDL_SHIFT_DIFF) #timeStr = str(dtBegin) operateString = shiftXDiff + "=" + shiftXLabel + "-" + shiftX2Label operateString += "," + shiftYDiff + "=" + shiftYLabel + "-" + shiftY2Label mdIter.operate(operateString) operateString = shiftDiff+"=sqrt(" \ +shiftXDiff+"*"+shiftXDiff+"+" \ +shiftYDiff+"*"+shiftYDiff+");" mdIter.operate(operateString) iterFile = self._mdDevitationsFn(it) mdIter.write(iterFile, xmippLib.MD_APPEND) self._setLastIter(it)
def test_CTF(self): """ Test the conversion of a SetOfParticles to Xmipp metadata. """ mdCtf = xmippLib.MetaData(self.dataset.getFile('ctfGold')) objId = mdCtf.firstObject() rowCtf = rowFromMd(mdCtf, objId) ctf = rowToCtfModel(rowCtf) ALL_CTF_LABELS = [ xmippLib.MDL_CTF_CA, xmippLib.MDL_CTF_ENERGY_LOSS, xmippLib.MDL_CTF_LENS_STABILITY, xmippLib.MDL_CTF_CONVERGENCE_CONE, xmippLib.MDL_CTF_LONGITUDINAL_DISPLACEMENT, xmippLib.MDL_CTF_TRANSVERSAL_DISPLACEMENT, xmippLib.MDL_CTF_K, xmippLib.MDL_CTF_BG_GAUSSIAN_K, xmippLib.MDL_CTF_BG_GAUSSIAN_SIGMAU, xmippLib.MDL_CTF_BG_GAUSSIAN_SIGMAV, xmippLib.MDL_CTF_BG_GAUSSIAN_CU, xmippLib.MDL_CTF_BG_GAUSSIAN_CV, xmippLib.MDL_CTF_BG_SQRT_K, xmippLib.MDL_CTF_BG_SQRT_U, xmippLib.MDL_CTF_BG_SQRT_V, xmippLib.MDL_CTF_BG_SQRT_ANGLE, xmippLib.MDL_CTF_BG_BASELINE, xmippLib.MDL_CTF_BG_GAUSSIAN2_K, xmippLib.MDL_CTF_BG_GAUSSIAN2_SIGMAU, xmippLib.MDL_CTF_BG_GAUSSIAN2_SIGMAV, xmippLib.MDL_CTF_BG_GAUSSIAN2_CU, xmippLib.MDL_CTF_BG_GAUSSIAN2_CV, xmippLib.MDL_CTF_BG_GAUSSIAN2_ANGLE, #xmippLib.MDL_CTF_CRIT_FITTINGSCORE, xmippLib.MDL_CTF_CRIT_FITTINGCORR13, xmippLib.MDL_CTF_DOWNSAMPLE_PERFORMED, xmippLib.MDL_CTF_CRIT_PSDVARIANCE, xmippLib.MDL_CTF_CRIT_PSDPCA1VARIANCE, xmippLib.MDL_CTF_CRIT_PSDPCARUNSTEST, xmippLib.MDL_CTF_CRIT_FIRSTZEROAVG, xmippLib.MDL_CTF_CRIT_DAMPING, xmippLib.MDL_CTF_CRIT_FIRSTZERORATIO, xmippLib.MDL_CTF_CRIT_PSDCORRELATION90, xmippLib.MDL_CTF_CRIT_PSDRADIALINTEGRAL, xmippLib.MDL_CTF_CRIT_NORMALITY, ] for label in ALL_CTF_LABELS: attrName = '_xmipp_%s' % xmippLib.label2Str(label) self.assertAlmostEquals(mdCtf.getValue(label, objId), ctf.getAttributeValue(attrName))
def createOutputStep(self): # PARTICLES cleanPattern(self._getPath("*.sqlite")) partSet = self._createSetOfParticles() readSetOfParticles(self._getPath("particles.xmd"), partSet) inputSampling = self.inputCoordinates[0].get().getMicrographs( ).getSamplingRate() partSet.setSamplingRate(self._getDownFactor() * inputSampling) boxSize = self._getBoxSize() # COORDINATES writeSet = False if self.checkIfPrevRunIsCompatible("coords_"): writeSet = True if not "OR" in self.coordinatesDict: self.loadCoords(self._getExtraPath( self.CONSENSUS_COOR_PATH_TEMPLATE % 'TRUE'), 'OR', writeSet=False) coordSet = SetOfCoordinates( filename=self._getPath("coordinates.sqlite")) coordSet.copyInfo(self.coordinatesDict['OR']) coordSet.setBoxSize(boxSize) coordSet.setMicrographs(self.coordinatesDict['OR'].getMicrographs()) downFactor = self._getDownFactor() for part in partSet: coord = part.getCoordinate().clone() coord.scale(downFactor) deepZscoreLabel = '_xmipp_%s' % xmipp.label2Str( MD.MDL_ZSCORE_DEEPLEARNING1) setattr(coord, deepZscoreLabel, getattr(part, deepZscoreLabel)) coordSet.append(coord) coordSet.write() partSet.write() self._defineOutputs(outputCoordinates=coordSet) self._defineOutputs(outputParticles=partSet) for inSetOfCoords in self.inputCoordinates: self._defineSourceRelation(inSetOfCoords.get(), coordSet) self._defineSourceRelation(inSetOfCoords.get(), partSet)
def writeToMd(self, md, objId): """ Set back row values to a metadata row. """ for label, value in self._labelDict.iteritems(): # TODO: Check how to handle correctly unicode type # in Xmipp and Scipion if type(value) is unicode: value = str(value) try: md.setValue(label, value, objId) except Exception as ex: import sys print("XmippMdRow.writeToMd: Error writing value to metadata.", file=sys.stderr) print( " label: %s, value: %s, type(value): %s" % (label2Str(label), value, type(value)), file=sys.stderr) raise ex
def _getMdString(self, filename, block=None): md = xmippLib.MetaData() if block: md.read(block + '@' + filename) else: md.read(filename, 1) labels = md.getActiveLabels() msg = "Metadata items: *%d*\n" % md.getParsedLines() msg += "Metadata labels: " + ''.join( ["\n - %s" % xmippLib.label2Str(l) for l in labels]) imgPath = None for label in labels: if xmippLib.labelIsImage(label): imgPath = self._getImgPath( filename, md.getValue(label, md.firstObject())) break if imgPath: self._imgPreview = self._getImagePreview(imgPath) self._imgInfo = self._getImageString(imgPath) return msg
def __str__(self): s = '{' for k, v in self._labelDict.iteritems(): s += ' %s = %s\n' % (label2Str(k), v) return s + '}'