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 = xmipp.MetaData() # store result mdQuery.importObjects(md, xmipp.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" % (xmipp.label2Str(label), value)) return row
def runAssignImagesToReferences(self, iterN, **kwargs): ''' assign the images to the different references based on the crosscorrelation coeficient #if only one reference it just copy the docfile generated in the previous step ''' numberOfCtfGroups = self.numberOfCtfGroups.get() #first we need a list with the references used. That is, #read all docfiles and map referecendes to a mdl_order mdAux = xmipp.MetaData() mdSort = xmipp.MetaData() md = xmipp.MetaData() md1 = xmipp.MetaData() mdout = xmipp.MetaData() mdout.setComment("Metadata with images, the winner reference as well as the ctf group") mycounter = 1L for ctfN in self.allCtfGroups(): ctfFilePrefix = self._getBlockFileName(ctfBlockName, ctfN, '') for refN in self.allRefs(): projMatchRootName = self._getFileName('projMatchRootNames', iter=iterN, ref=refN) inputdocfile = ctfFilePrefix + projMatchRootName md.read(inputdocfile) for id in md: t = md.getValue(xmipp.MDL_REF, id) i = mdSort.addObject() mdSort.setValue(xmipp.MDL_REF, t, i) mdSort.removeDuplicates() for id in mdSort: mdSort.setValue(xmipp.MDL_ORDER, mycounter, id) mycounter += 1 #################### outputdocfile = self.docFileInputAngles[iterN] cleanPath(outputdocfile) mdout2 = xmipp.MetaData() for ctfN in self.allCtfGroups(): mdAux.clear() ctfFilePrefix = self._getBlockFileName(ctfBlockName, ctfN, '') for refN in self.allRefs(): projMatchRootName = self._getFileName('projMatchRootNames', iter=iterN, ref=refN) inputdocfile = ctfFilePrefix + projMatchRootName md.clear() md.read(inputdocfile) #In practice you should not get duplicates md.removeDuplicates() md.setValueCol(xmipp.MDL_REF3D, refN) md.setValueCol(xmipp.MDL_DEFGROUP, ctfN) #MD.setValueCol(xmipp.MDL_CTF_MODEL,ctfFilePrefix[:-1]) mdAux.unionAll(md) mdAux.sort() md.aggregate(mdAux, xmipp.AGGR_MAX, xmipp.MDL_IMAGE, xmipp.MDL_MAXCC, xmipp.MDL_MAXCC) #if a single image is assigned to two references with the same #CC use it in both reconstruction #recover atribbutes after aggregate function md1.joinNatural(md, mdAux) mdout.joinNatural(md1, mdSort) mdout.write(ctfFilePrefix + outputdocfile, xmipp.MD_APPEND) mdout2.unionAll(mdout) mdout2.write(self.blockWithAllExpImages + '@' + outputdocfile, xmipp.MD_APPEND) #Aggregate for ref3D and print warning if all images has been assigned to a single volume #ROB md1.clear() md1.aggregate(mdout2, xmipp.AGGR_COUNT, xmipp.MDL_REF3D, xmipp.MDL_REF3D, xmipp.MDL_COUNT) import sys md1_size = md1.size() numberOfReferences = self.numberOfReferences if md1_size != numberOfReferences: print >> sys.stderr,"********************************************" print >> sys.stderr, md1 print >> sys.stderr,"ERROR: Some 3D references do not have assigned any projection assigned to them" print >> sys.stderr,"Consider reducing the number of 3D references" print >> sys.stderr,"Number of References:" ,numberOfReferences print >> sys.stderr,"Number of Empty references", numberOfReferences - md1_size raise Exception('runAssignImagesToReferences failed') #!a original_angles too #we are done but for the future it is convenient to create more blocks #with the pairs ctf_group reference for ctfN in self.allCtfGroups(): ctfFilePrefix = self._getBlockFileName(ctfBlockName, ctfN, '') #print 'read file: ', ctfFilePrefix+outputdocfile mdAux.read(ctfFilePrefix + outputdocfile) for refN in self.allRefs(): auxOutputdocfile = self._getRefBlockFileName(ctfBlockName, ctfN, refBlockName, refN, '') #select images with ref3d=iRef3D mdout.importObjects(mdAux, xmipp.MDValueEQ(xmipp.MDL_REF3D, refN)) mdout.write(auxOutputdocfile + outputdocfile, xmipp.MD_APPEND)
def _plotHistogramAngularMovement(self, paramName=None): from numpy import arange from matplotlib.ticker import FormatStrFormatter plots = [] colors = ['g', 'b', 'r', 'y', 'c', 'm', 'k'] lenColors = len(colors) numberOfBins = self.numberOfBins.get() md = xmipp.MetaData() for it in self._iterations: mdFn = self.protocol._mdDevitationsFn(it) if xmipp.existsBlockInMetaDataFile(mdFn): md.read(mdFn) if not self.usePsi: md.fillConstant(xmipp.MDL_ANGLE_PSI, 0.) nrefs = len(self._refsList) gridsize = self._getGridSize(nrefs) xplotterShift = XmippPlotter(*gridsize, mainTitle='Iteration_%d\n' % it, windowTitle="ShiftDistribution") xplotter = XmippPlotter(*gridsize, mainTitle='Iteration_%d' % it, windowTitle="AngularDistribution") for ref3d in self._refsList: mDoutRef3D = xmipp.MetaData() mDoutRef3D.importObjects( md, xmipp.MDValueEQ(xmipp.MDL_REF3D, ref3d)) _frequency = "Frequency (%d)" % mDoutRef3D.size() xplotterShift.createSubPlot( "%s_ref3D_%d" % (xmipp.label2Str(xmipp.MDL_SHIFT_DIFF), ref3d), "pixels", _frequency) xplotter.createSubPlot( "%s_ref3D_%d" % (xmipp.label2Str(xmipp.MDL_ANGLE_DIFF), ref3d), "degrees", _frequency) #mDoutRef3D.write("*****@*****.**",MD_APPEND) xplotter.plotMd( mDoutRef3D, xmipp.MDL_ANGLE_DIFF, xmipp.MDL_ANGLE_DIFF, color=colors[ref3d % lenColors], #nbins=50 nbins=int(numberOfBins) ) #if nbins is present do an histogram xplotterShift.plotMd( mDoutRef3D, xmipp.MDL_SHIFT_DIFF, xmipp.MDL_SHIFT_DIFF, color=colors[ref3d % lenColors], nbins=int(numberOfBins )) #if nbins is present do an histogram if self.angleSort: mDoutRef3D.sort(xmipp.MDL_ANGLE_DIFF) fn = xmipp.FileName() baseFileName = self.protocol._getTmpPath( "angle_sort.xmd") fn = self.protocol._getRefBlockFileName( "angle_iter", it, "ref3D", ref3d, baseFileName) mDoutRef3D.write(fn, xmipp.MD_APPEND) print "File with sorted angles saved in:", fn if self.shiftSort: mDoutRef3D.sort(xmipp.MDL_SHIFT_DIFF) fn = xmipp.FileName() baseFileName = self.protocol._getTmpPath( "angle_sort.xmd") fn = self.protocol._getRefBlockFileName( "shift_iter", it, "ref3D", ref3d, baseFileName) mDoutRef3D.write(fn, xmipp.MD_APPEND) print "File with sorted shifts saved in:", fn plots.append(xplotterShift) plots.append(xplotter) else: print "File %s does not exist" % mdFn return plots
def _showProjMatchLibAndImages(self, paramName=None): from numpy import array, dot #map stack position with ref number imgAndClasses = [] mdIn = xmipp.MetaData() mdOut = xmipp.MetaData() mdTmp = xmipp.MetaData() for ref3d in self._refsList: for it in self._iterations: convert_refno_to_stack_position = {} file_name = self.protocol._getFileName( 'projectLibrarySampling', iter=it, ref=ref3d) file_nameReferences = 'projectionDirections@' + file_name if exists(file_name): #last reference name mdReferences = xmipp.MetaData(file_nameReferences) mdReferencesSize = mdReferences.size() for id in mdReferences: convert_refno_to_stack_position[mdReferences.getValue( xmipp.MDL_NEIGHBOR, id)] = id file_nameImages = "ctfGroup[0-9][0-9][0-9][0-9][0-9][0-9]@" + self.protocol._getFileName( 'docfileInputAnglesIters', iter=it) mdTmp.read(file_nameImages) #query with ref3D mdIn.importObjects(mdTmp, xmipp.MDValueEQ(xmipp.MDL_REF3D, ref3d)) mdOut.clear() for i in mdIn: id1 = mdOut.addObject() mdOut.setValue(xmipp.MDL_IMAGE, mdIn.getValue(xmipp.MDL_IMAGE, i), id1) psi = -1. * mdIn.getValue(xmipp.MDL_ANGLE_PSI, i) flip = mdIn.getValue(xmipp.MDL_FLIP, i) if (flip): psi = -psi eulerMatrix = xmipp.Euler_angles2matrix(0., 0., psi) x = mdIn.getValue(xmipp.MDL_SHIFT_X, i) y = mdIn.getValue(xmipp.MDL_SHIFT_Y, i) shift = array([x, y, 0]) shiftOut = dot(eulerMatrix, shift) [x, y, z] = shiftOut if flip: x = -x mdOut.setValue(xmipp.MDL_ANGLE_PSI, psi, id1) mdOut.setValue(xmipp.MDL_SHIFT_X, x, id1) mdOut.setValue(xmipp.MDL_SHIFT_Y, y, id1) mdOut.setValue(xmipp.MDL_FLIP, flip, id1) ref2D = mdIn.getValue(xmipp.MDL_REF, i) file_references = self.protocol._getFileName( 'projectLibraryStk', iter=it, ref=ref3d) file_reference = xmipp.FileName() file_reference.compose( convert_refno_to_stack_position[ref2D], file_references) id2 = mdOut.addObject() mdOut.setValue(xmipp.MDL_IMAGE, file_reference, id2) mdOut.setValue(xmipp.MDL_ANGLE_PSI, 0., id2) if mdOut.size() == 0: print "Empty metadata" else: file_nameReferences = self.protocol._getFileName( 'projectLibrarySampling', iter=it, ref=ref3d) sfn = createUniqueFileName(file_nameReferences) file_nameReferences = 'projectionDirections@' + sfn mdOut.write(file_nameReferences) imgAndClasses.append( self.createDataView(file_nameReferences)) else: print "File %s does not exist" % file_name return imgAndClasses