def _createSetOfParts(self, nMics=10, nOptics=2, partsPerMic=10): micSet = self._createSetOfMics(nMics, nOptics) outputSqlite = self.getOutputPath('particles.sqlite') cleanPath(outputSqlite) print(">>> Writing to particles db: %s" % outputSqlite) outputParts = SetOfParticles(filename=outputSqlite) outputParts.setSamplingRate(1.234) outputParts.setAcquisition(micSet.getAcquisition()) part = SetOfParticles.ITEM_TYPE() coord = Coordinate() for mic in micSet: for i in range(1, partsPerMic + 1): part.setLocation(i, mic.getFileName().replace('mrc', 'mrcs')) coord.setPosition(x=np.random.randint(0, 1000), y=np.random.randint(0, 1000)) coord.setMicrograph(mic) part.setObjId(None) part.setCoordinate(coord) part.setAcquisition(mic.getAcquisition()) outputParts.append(part) outputParts.write() return outputParts
def _checkNewOutput(self): if getattr(self, 'finished', False): return self.finished = self.streamClosed and self.checkedMics == self.processedMics streamMode = Set.STREAM_CLOSED if self.finished else Set.STREAM_OPEN newFiles = getFiles(self._getTmpPath()) if newFiles or self.finished: # when finished to close the output set outSet = self._loadOutputSet(SetOfCoordinates, 'coordinates.sqlite') for fnTmp in newFiles: coords = np.loadtxt(fnTmp) moveFile(fnTmp, self._getExtraPath()) if coords.size == 2: # special case with only one coordinate coords = [coords] for coord in coords: newCoord = Coordinate() micrographs = self.getMainInput().getMicrographs() newCoord.setMicrograph(micrographs[self.getMicId(fnTmp)]) newCoord.setPosition(coord[0], coord[1]) outSet.append(newCoord) firstTime = not self.hasAttribute(self.outputName) self._updateOutputSet(self.outputName, outSet, streamMode) if firstTime: self.defineRelations(outSet) outSet.close() if self.finished: # Unlock createOutputStep if finished all jobs outputStep = self._getFirstJoinStep() if outputStep and outputStep.isWaiting(): outputStep.setStatus(STATUS_NEW)
def readCoordinates(mic, fileName, coordsSet): if exists(fileName): md = emlib.MetaData() md.readPlain(fileName, 'xcoor ycoor') for objId in md: x = md.getValue(emlib.MDL_XCOOR, objId) y = md.getValue(emlib.MDL_YCOOR, objId) coord = Coordinate() coord.setPosition(x, y) coord.setMicrograph(mic) coordsSet.append(coord)
def _fillParticles(self, outputParticles, outputCoords, inputParticles): """ Create the SetOfParticles and SetOfCoordinates""" myParticles = md.MetaData(self._getExtraPath('final_images.xmd')) outputParticles.enableAppend() outputCoords.enableAppend() #Calculating the scale that relates the coordinates with the actual # position in the mic scale = inputParticles.getSamplingRate() / \ self.inputMics.get().getSamplingRate() #Dictionary with the name and id of the inpt mics micDictname = {} micDictId = {} for mic in self.inputMics.get(): micKey = mic.getMicName() micDictname[micKey] = mic.clone() micKey2 = mic.getObjId() micDictId[micKey2] = mic.clone() for row in md.iterRows(myParticles): #To create the new particle p = rowToParticle(row) #To create the new coordinate newCoord = Coordinate() coord = p.getCoordinate() if coord.getMicName() is not None: micKey = coord.getMicName() micDict = micDictname else: micKey = coord.getMicId() micDict = micDictId mic = micDict.get(micKey, None) if mic is None: print("Skipping particle, key %s not found" % micKey) else: newCoord.copyObjId(p) x, y = coord.getPosition() newCoord.setPosition(x * scale, y * scale) newCoord.setMicrograph(mic) outputCoords.append(newCoord) p.setCoordinate(newCoord) #Storing the new particle outputParticles.append(p) boxSize = inputParticles.getXDim() * scale outputCoords.setBoxSize(boxSize)
def readCoordinates(mic, fn, coordsSet): """ Parse coords file and populate coordsSet. :param mic: input micrograph object :param fn: input file to parse :param coordsSet: output set of coords """ if exists(fn): with open(fn, 'r') as f: for line in f: values = line.strip().split() # plt coords are in Imagic style x = float(values[1]) y = float(mic.getYDim() - float(values[0])) coord = Coordinate() coord.setPosition(x, y) coord.setMicrograph(mic) coordsSet.append(coord)
def readCoordsFromMics(self, workingDir, micList, coordSet): coordSet.setBoxSize(self.radius.get() * 2) for mic in micList: micFn = mic.getFileName() micDir = self._getMicDir(micFn) coordFile = os.path.join(micDir, self._getMicPosFn(micFn)) if os.path.exists(coordFile): coordMd = md.MetaData() coordMd.readPlain(coordFile, 'xcoor ycoor') for objId in coordMd: x = coordMd.getValue(md.MDL_XCOOR, objId) y = coordMd.getValue(md.MDL_YCOOR, objId) coord = Coordinate() coord.setPosition(x, y) coord.setMicrograph(mic) coordSet.append(coord) else: print("Coordinate file '%s' not found. " % coordFile)
def readSetOfCoordinates(coordinatesCsvFn, micSet, coordSet, scale): """ Read coordinates produced by Topaz. Coordinates are expected in a single csv file, with the following columns: first: image_name (mic id) second: x_coord third: y_coord forth: score """ csv = CsvCoordinateList(coordinatesCsvFn, score=True) lastMicId = None coord = Coordinate() coord._topazScore = Float() micDict = {} # loop to generate a dictionary --> micBaseName : Micrograph for mic in micSet: micNew = mic.clone() micDict[mic.getObjId()] = micNew #loop the Topaz outputfile for row in csv: micId = int(row[0]) if micId != lastMicId: mic = micDict[micId] if mic is None: print("Missing id: ", micId) else: coord.setMicrograph(mic) lastMicId = micId coord.setPosition(int(round(float(row[1]) * scale)), int(round(float(row[2]) * scale))) coord._topazScore.set(float(row[3])) coord.setObjId(None) coordSet.append(coord) csv.close()