def openPath(self, path): "Try to open the selected path" path = expandPattern(path) # If the path is a dir, open it with scipion browser dir <path> if os.path.isdir(path): dpath = (path if os.path.isabs(path) else os.path.join( os.getcwd(), path)) subprocess.Popen([pw.getScipionScript(), 'view', dpath]) return # If it is a file, interpret it correctly and open it with DataView dirname = os.path.dirname(path) fname = os.path.basename(path) if '@' in fname: path = os.path.join(dirname, fname.split('@', 1)[-1]) else: path = os.path.join(dirname, fname) if os.path.exists(path): import xmipp fn = xmipp.FileName(path) if fn.isImage() or fn.isMetaData(): from pyworkflow.em.viewer import DataView DataView(path).show() else: _open_cmd(path) else: # This is probably one special reference, like sci-open:... that # can be interpreted with our handlers. tag = path.split(':', 1)[0] if ':' in path else None if tag in self.handlers: self.handlers[tag](path.split(':', 1)[-1]) else: print "Can't find %s" % path
def runCreateOutputStep(self): ''' Create standard output results_images, result_classes''' #creating results files imgSet = self.inputParticles.get() lastIter = self.numberOfIterations.get() if self.numberOfReferences != 1: inDocfile = self._getFileName('docfileInputAnglesIters', iter=lastIter) ClassFnTemplate = '%(rootDir)s/reconstruction_Ref3D_%(ref)03d.vol' allExpImagesinDocfile = xmipp.FileName() all_exp_images="all_exp_images" allExpImagesinDocfile.compose(all_exp_images, inDocfile) dataClasses = self._getFileName('sqliteClasses') createClassesFromImages(imgSet, str(allExpImagesinDocfile), dataClasses, SetOfClasses3D, xmipp.MDL_REF3D, ClassFnTemplate, lastIter) classes = self._createSetOfClasses3D(imgSet) clsSet = SetOfClasses3D(dataClasses) classes.appendFromClasses(clsSet) volumes = self._createSetOfVolumes() volumes.setSamplingRate(imgSet.getSamplingRate()) for refN in self.allRefs(): volFn = self._getFileName('reconstructedFileNamesIters', iter=lastIter, ref=refN) vol = Volume() vol.setFileName(volFn) volumes.append(vol) self._defineOutputs(outputVolumes=volumes) self._defineOutputs(outputClasses=classes) self._defineSourceRelation(self.inputParticles, volumes) self._defineSourceRelation(self.inputParticles, classes) self._defineSourceRelation(self.input3DReferences, volumes) self._defineSourceRelation(self.input3DReferences, classes) else: volFn = self._getFileName('reconstructedFileNamesIters', iter=lastIter, ref=1) halfMap1 = self._getFileName('reconstructedFileNamesItersSplit1', iter=lastIter, ref=1) halfMap2 = self._getFileName('reconstructedFileNamesItersSplit2', iter=lastIter, ref=1) vol = Volume() vol.setFileName(volFn) vol.setSamplingRate(imgSet.getSamplingRate()) vol.setHalfMaps([halfMap1, halfMap2]) self._defineOutputs(outputVolume=vol) self._defineSourceRelation(self.inputParticles, vol) self._defineSourceRelation(self.input3DReferences, vol) #create set of images imgSetOut = self._createSetOfParticles("_iter_%03d" %lastIter) self._fillParticlesFromIter(imgSetOut, lastIter) self._defineOutputs(outputParticles=imgSetOut) self._defineSourceRelation(self.inputParticles, imgSetOut) self._defineSourceRelation(self.input3DReferences, imgSetOut)
def _checkOutputs(self, outputs, random=False, errorthreshold=0.001): """ Check that all output files are produced and are equivalent to the ones in goldStandard folder. """ for out in outputs: outFile = os.path.join(self._testDir, self.outputDir, out) fileGoldStd = os.path.join(self.goldDir, out) # Check the expect output file was produced msg = "Missing expected output file:\n output: %s" % outFile self.assertTrue(os.path.exists(outFile), msg) if random: print "WARNING: %s was created using a random seed, check skipped\n " % outFile else: fnGoldStd = xmipp.FileName(fileGoldStd) if fnGoldStd.isImage(): im1 = xmipp.Image(fileGoldStd) im2 = xmipp.Image(outFile) msg = "Images are not equal (+-%f):\n output: %s\n gold: %s" % \ (errorthreshold, outFile, fileGoldStd) self.assertTrue(im1.equal(im2, errorthreshold), msg) elif fnGoldStd.isMetaData(): msg = "MetaDatas are not equal:\n output: %s\n gold: %s" % ( outFile, fileGoldStd) self.assertTrue( xmipp.compareTwoMetadataFiles(outFile, fileGoldStd), msg) else: msg = "Files are not equal:\n output: %s\n gold: %s" % ( outFile, fileGoldStd) self.assertTrue( xmipp.compareTwoFiles(outFile, fileGoldStd, 0), msg)
def createMetaDataFromPattern(pattern, isStack=False, label="image"): ''' Create a metadata from files matching pattern''' import glob files = glob.glob(pattern) files.sort() label = xmipp.str2Label(label) #Check for label value mD = xmipp.MetaData() inFile = xmipp.FileName() nSize = 1 for file in files: fileAux = file if isStack: if file.endswith(".mrc"): fileAux = file + ":mrcs" x, x, x, nSize = xmipp.getImageSize(fileAux) if nSize != 1: counter = 1 for jj in range(nSize): inFile.compose(counter, fileAux) objId = mD.addObject() mD.setValue(label, inFile, objId) mD.setValue(xmipp.MDL_ENABLED, 1, objId) counter += 1 else: objId = mD.addObject() mD.setValue(label, fileAux, objId) mD.setValue(xmipp.MDL_ENABLED, 1, objId) return mD
def xmippToLocation(xmippFilename): """ Return a location (index, filename) given a Xmipp filename with the index@filename structure. """ if '@' in xmippFilename: return xmipp.FileName(xmippFilename).decompose() else: return NO_INDEX, str(xmippFilename)
def _showProjMatchLibAndClasses(self, paramName=None): #map stack position with ref number list = [] mdIn = xmipp.MetaData() mdOut = 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): 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_nameAverages = self.protocol._getFileName( 'outClassesXmd', iter=it, ref=ref3d) file_references = self.protocol._getFileName( 'projectLibraryStk', iter=it, ref=ref3d) if exists(file_nameAverages): mdIn.read(file_nameAverages) mdOut.clear() for i in mdIn: ref2D = mdIn.getValue(xmipp.MDL_REF, i) file_reference = xmipp.FileName() file_reference.compose( convert_refno_to_stack_position[ref2D], file_references) id1 = mdOut.addObject() mdOut.setValue(xmipp.MDL_IMAGE, mdIn.getValue(xmipp.MDL_IMAGE, i), id1) mdOut.setValue(xmipp.MDL_IMAGE2, file_reference, id1) if mdOut.size() == 0: print "Empty metadata: ", file_nameReferences else: file_nameReferences = self.protocol._getFileName( 'projectLibrarySampling', iter=it, ref=ref3d) sfn = createUniqueFileName(file_nameReferences) file_nameReferences = 'projectionDirections@' + sfn mdOut.merge(mdIn) mdOut.write(file_nameReferences) # ToDo: show the metadata in "metadata" form. list.append( self.createDataView(file_nameReferences)) else: print "File %s does not exist" % file_name return list
def _getImgPath(self, mdFn, imgFn): """ Get ups and ups until finding the relative location to images. """ path = dirname(mdFn) index, fn = xmipp.FileName(imgFn).decompose() while path and path != '/': newFn = os.path.join(path, fn) if os.path.exists(newFn): if index: newFn = '%d@%s' % (index, newFn) return newFn path = dirname(path) return None
def isImageFile(self, imgFn): """ Check if imgFn has an image extension. The function is implemented in the Xmipp binding. """ return xmipp.FileName(imgFn).isImage()
def xmippExists(path): return xmipp.FileName(path).exists()
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