def getImageDim(request, imagePath): projectPath = request.session['projectPath'] # imgFn = os.path.join(projectPath, imagePath) from pyworkflow.em.packages.xmipp3.convert import xmippToLocation location = xmippToLocation(imagePath) x, y, z, n = ImageHandler().getDimensions(location) return x, y, z, n
def __setXmippImage(label): attr = '_xmipp_' + xmipp.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 _loadClassesInfo(self, filename): """ Read some information about the produced 2D classes from the metadata file. """ self._classesInfo = {} # store classes info, indexed by class id mdClasses = md.MetaData(filename) for classNumber, row in enumerate(md.iterRows(mdClasses)): index, fn = xmippToLocation(row.getValue(md.MDL_IMAGE)) # Store info indexed by id, we need to store the row.clone() since # the same reference is used for iteration self._classesInfo[classNumber + 1] = (index, fn, row.clone())
def copyDeformationsStep(self, deformationMd): copyFile(deformationMd, self.imgsFn) # We need to update the image name with the good ones # and the same with the ids. inputSet = self.inputParticles.get() md = xmipp.MetaData(self.imgsFn) for objId in md: imgPath = md.getValue(xmipp.MDL_IMAGE, objId) index, fn = xmippToLocation(imgPath) # Conside the index is the id in the input set particle = inputSet[index] md.setValue(xmipp.MDL_IMAGE, getImageLocation(particle), objId) md.setValue(xmipp.MDL_ITEM_ID, long(particle.getObjId()), objId) md.write(self.imgsFn)
def copyDeformationsStep(self, deformationMd): copyFile(deformationMd, self.imgsFn) # We need to update the image name with the good ones # and the same with the ids. inputSet = self.inputParticles.get() mdImgs = md.MetaData(self.imgsFn) for objId in mdImgs: imgPath = mdImgs.getValue(md.MDL_IMAGE, objId) index, fn = xmippToLocation(imgPath) # Conside the index is the id in the input set particle = inputSet[index] mdImgs.setValue(md.MDL_IMAGE, getImageLocation(particle), objId) mdImgs.setValue(md.MDL_ITEM_ID, long(particle.getObjId()), objId) mdImgs.write(self.imgsFn)
def get_image_filter_spider(request): """ Function to get the computing image with a spider filter applied """ pars = {} imagePath = request.GET.get('image', None) dim = request.GET.get('dim', None) filterType = int(request.GET.get('filterType', None)) pars["filterType"] = filterType pars["filterMode"] = int(request.GET.get('filterMode', None)) pars["usePadding"] = request.GET.get('usePadding', None) pars["op"] = "FQ" # Copy image to filter to Tmp project folder outputName = os.path.join("Tmp", "filtered_particle") outputPath = outputName + ".spi" outputLoc = (1, outputPath) ih = ImageHandler() ih.convert(xmippToLocation(imagePath), outputLoc) outputLocSpiStr = locationToSpider(1, outputName) # check values if filterType < 2: pars["filterRadius"] = request.GET.get('radius', None) else: pars["highFreq"] = float(request.GET.get('highFreq', None)) pars["lowFreq"] = float(request.GET.get('lowFreq', None)) if filterType == 2: pars["temperature"] = request.GET.get('temperature', None) filter_spider(outputLocSpiStr, outputLocSpiStr, **pars) # Get output image and update filtered image img = xmipp.Image() locXmippStr = locationToXmipp(1, outputPath) img.read(locXmippStr) # from PIL import Image img = getPILImage(img, dim) response = HttpResponse(mimetype="image/png") img.save(response, "PNG") return response
def get_image_filter_spider(request): """ Function to get the computing image with a spider filter applied """ pars={} imagePath = request.GET.get('image', None) dim = request.GET.get('dim', None) filterType = int(request.GET.get('filterType', None)) pars["filterType"] = filterType pars["filterMode"] = int(request.GET.get('filterMode', None)) pars["usePadding"] = request.GET.get('usePadding', None) pars["op"]="FQ" # Copy image to filter to Tmp project folder outputName = os.path.join("Tmp", "filtered_particle") outputPath = outputName + ".spi" outputLoc = (1, outputPath) ih = ImageHandler() ih.convert(xmippToLocation(imagePath), outputLoc) outputLocSpiStr = locationToSpider(1, outputName) # check values if filterType < 2: pars["filterRadius"] = request.GET.get('radius', None) else: pars["highFreq"] = float(request.GET.get('highFreq', None)) pars["lowFreq"] = float(request.GET.get('lowFreq', None)) if filterType == 2: pars["temperature"] = request.GET.get('temperature', None) filter_spider(outputLocSpiStr, outputLocSpiStr, **pars) # Get output image and update filtered image img = xmipp.Image() locXmippStr = locationToXmipp(1, outputPath) img.read(locXmippStr) # from PIL import Image img = getPILImage(img, dim) response = HttpResponse(mimetype="image/png") img.save(response, "PNG") return response
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 _preprocessParticleRow(self, img, imgRow): if self._imgPath: # Create a link or copy files to extraPath # and update the Row properly index, fn = xmippToLocation(imgRow.getValue(md.MDL_IMAGE)) imgBase = basename(fn) imgDst = self.protocol._getExtraPath(imgBase) if not exists(imgDst): self.copyOrLink(join(self._imgPath, fn), imgDst) imgRow.setValue(md.MDL_IMAGE, locationToXmipp(index, imgDst)) if self._micIdOrName: micId = imgRow.getValue(md.MDL_MICROGRAPH_ID, None) micName = imgRow.getValue(md.MDL_MICROGRAPH, None) # Check which is the key to identify micrographs (id or name) if micId is not None: micKey = micId else: micKey = micName mic = self.micDict.get(micKey, None) # First time I found this micrograph (either by id or name) if mic is None: mic = Micrograph() mic.setObjId(micId) if micName is None: micName = self.protocol._getExtraPath('fake_micrograph%6d' % micId) mic.setFileName(micName) self.micSet.append(mic) # Update dict with new Micrograph self.micDict[micKey] = mic # Update the row to set a MDL_MICROGRAPH_ID imgRow.setValue(md.MDL_MICROGRAPH_ID, long(mic.getObjId()))
def _processMovie(self, movieId, movieName, movieFolder, shifts): ###pasar shifts movieName = os.path.join(movieFolder, movieName) boxSize = self.boxSize.get() # Read movie dimensions to iterate through each frame imgh = ImageHandler() x, y, z, n = imgh.getDimensions(movieName) first = self.firstFrame.get() if first <= 1: first = 1 last = self.lastFrame.get() if last <= 0 or last >= n: last = n numberOfFrames = last - first + 1 if shifts is None: frames = max(z, n) shifts = [0] * (2 * frames) stkIndex = 0 movieStk = self._getMovieName(movieId, '.stk') movieMdFile = self._getMovieName(movieId, '.xmd') movieMd = md.MetaData() frameMd = md.MetaData() frameMdImages = md.MetaData() frameRow = md.Row() for frame in range(first, last + 1): # Get the frame shifts index = frame - first shiftX = shifts[2 * index] shiftY = shifts[2 * index + 1] frameRoot = os.path.join(movieFolder, 'frame_%02d' % frame) frameName = frameRoot + '.mrc' frameMdFile = frameRoot + '.xmd' framePosFile = frameRoot + '_coordinates.xmd' coordinatesName = frameRoot + '_coordinates.xmd' hasCoordinates = self._writeXmippPosFile(movieId, movieName, coordinatesName, shiftX, shiftY) if hasCoordinates: self.info("Writing frame: %s" % frameName) #TODO: there is no need to write the frame and then operate #the input of the first operation should be the movie imgh.convert(tuple([frame, movieName]), frameName) if self.doRemoveDust: self.info("Removing Dust") self._runNoDust(frameName) self.info("Extracting particles") frameImages = frameRoot + '_images' args = '-i %(frameName)s --pos %(coordinatesName)s ' \ '-o %(frameRoot)s --Xdim %(boxSize)d' % locals() if self.doInvert: args += " --invert" args += " --downsampling %f " % self.factor self.runJob('xmipp_micrograph_scissor', args) cleanPath(frameName) frameStk = frameRoot + '.stk' self.info("Combining particles into one stack.") frameMdImages.read(frameMdFile) frameMd.read('particles@%s' % framePosFile) frameMd.merge(frameMdImages) for objId in frameMd: stkIndex += 1 frameRow.readFromMd(frameMd, objId) location = xmippToLocation(frameRow.getValue(md.MDL_IMAGE)) newLocation = (stkIndex, movieStk) imgh.convert(location, newLocation) # Fix the name to be accesible from the Project directory # so we know that the movie stack file will be moved # to final particles folder newImageName = '%d@%s' % newLocation frameRow.setValue(md.MDL_IMAGE, newImageName) frameRow.setValue(md.MDL_MICROGRAPH_ID, long(movieId)) frameRow.setValue(md.MDL_MICROGRAPH, str(movieId)) frameRow.setValue(md.MDL_FRAME_ID, long(frame)) frameRow.setValue(md.MDL_PARTICLE_ID, frameRow.getValue(md.MDL_ITEM_ID)) frameRow.writeToMd(movieMd, movieMd.addObject()) movieMd.addItemId() movieMd.write(movieMdFile) cleanPath(frameStk) if self.doNormalize: self._runNormalize(movieStk, numberOfFrames)
def _processMovie(self, movie): movId = movie.getObjId() x, y, n = movie.getDim() iniFrame, lastFrame, _ = movie.getFramesRange() frame0, frameN = self._getRange(movie) boxSize = self.boxSize.get() if movie.hasAlignment() and self.applyAlignment: shiftX, shiftY = movie.getAlignment().getShifts() # lists. else: shiftX = [0] * (lastFrame - iniFrame + 1) shiftY = shiftX stkIndex = 0 movieStk = self._getMovieName(movie, '.stk') movieMdFile = self._getMovieName(movie, '.xmd') movieMd = md.MetaData() frameMd = md.MetaData() frameMdImages = md.MetaData() frameRow = md.Row() imgh = ImageHandler() for frame in range(frame0, frameN + 1): indx = frame - iniFrame print "Index: ", indx, shiftX, shiftY frameName = self._getFnRelated('frameMic', movId, frame) frameMdFile = self._getFnRelated('frameMdFile', movId, frame) coordinatesName = self._getFnRelated('frameCoords', movId, frame) frameImages = self._getFnRelated('frameImages', movId, frame) frameStk = self._getFnRelated('frameStk', movId, frame) hasCoordinates = self._writeXmippPosFile(movie, coordinatesName, shiftX[indx], shiftY[indx]) if hasCoordinates: self.info("Writing frame: %s" % frameName) #TODO: there is no need to write the frame and then operate #the input of the first operation should be the movie movieName = imgh.fixXmippVolumeFileName(movie) print "Passing Ih: ", frame, movieName, frameName imgh.convert(tuple([frame - 1, movieName]), frameName) if self.doRemoveDust: self.info("Removing Dust") self._runNoDust(frameName) self.info("Extracting particles") args = '-i %(frameName)s --pos %(coordinatesName)s ' \ '-o %(frameImages)s --Xdim %(boxSize)d' % locals() if self.doInvert: args += " --invert" args += " --downsampling %f " % self.factor self.runJob('xmipp_micrograph_scissor', args) cleanPath(frameName) self.info("Combining particles into one stack.") frameMdImages.read(frameMdFile) frameMd.read('particles@%s' % coordinatesName) frameMd.merge(frameMdImages) for objId in frameMd: stkIndex += 1 frameRow.readFromMd(frameMd, objId) location = xmippToLocation(frameRow.getValue(md.MDL_IMAGE)) newLocation = (stkIndex, movieStk) imgh.convert(location, newLocation) # Fix the name to be accesible from the Project directory # so we know that the movie stack file will be moved # to final particles folder newImageName = '%d@%s' % newLocation frameRow.setValue(md.MDL_IMAGE, newImageName) frameRow.setValue(md.MDL_MICROGRAPH_ID, long(movId)) frameRow.setValue(md.MDL_MICROGRAPH, str(movId)) frameRow.setValue(md.MDL_FRAME_ID, long(frame)) frameRow.setValue(md.MDL_PARTICLE_ID, frameRow.getValue(md.MDL_ITEM_ID)) frameRow.writeToMd(movieMd, movieMd.addObject()) movieMd.addItemId() movieMd.write(movieMdFile) cleanPath(frameStk) if self.doNormalize: numberOfFrames = frameN - frame0 + 1 self._runNormalize(movieStk, numberOfFrames)
def _processMovie(self, movie): movId = movie.getObjId() x, y, n = movie.getDim() iniFrame, lastFrame, _ = movie.getFramesRange() frame0, frameN = self._getRange(movie) boxSize = self.boxSize.get() if movie.hasAlignment() and self.applyAlignment: shiftX, shiftY = movie.getAlignment().getShifts() # lists. else: shiftX = [0] * (lastFrame-iniFrame+1) shiftY = shiftX stkIndex = 0 movieStk = self._getMovieName(movie, '.stk') movieMdFile = self._getMovieName(movie, '.xmd') movieMd = md.MetaData() frameMd = md.MetaData() frameMdImages = md.MetaData() frameRow = md.Row() if self._hasCoordinates(movie): imgh = ImageHandler() for frame in range(frame0, frameN+1): indx = frame-iniFrame frameName = self._getFnRelated('frameMic',movId, frame) frameMdFile = self._getFnRelated('frameMdFile',movId, frame) coordinatesName = self._getFnRelated('frameCoords',movId, frame) frameImages = self._getFnRelated('frameImages',movId, frame) frameStk = self._getFnRelated('frameStk', movId, frame) self._writeXmippPosFile(movie, coordinatesName, shiftX[indx], shiftY[indx]) self.info("Writing frame: %s" % frameName) # TODO: there is no need to write the frame and then operate # the input of the first operation should be the movie movieName = imgh.fixXmippVolumeFileName(movie) imgh.convert((frame, movieName), frameName) if self.doRemoveDust: self.info("Removing Dust") self._runNoDust(frameName) self.info("Extracting particles") args = '-i %(frameName)s --pos %(coordinatesName)s ' \ '-o %(frameImages)s --Xdim %(boxSize)d' % locals() if self.doInvert: args += " --invert" if self.doBorders: args += " --fillBorders" args += " --downsampling %f " % self.getBoxScale() self.runJob('xmipp_micrograph_scissor', args) cleanPath(frameName) self.info("Combining particles into one stack.") frameMdImages.read(frameMdFile) frameMd.read('particles@%s' % coordinatesName) frameMd.merge(frameMdImages) for objId in frameMd: stkIndex += 1 frameRow.readFromMd(frameMd, objId) location = xmippToLocation(frameRow.getValue(md.MDL_IMAGE)) newLocation = (stkIndex, movieStk) imgh.convert(location, newLocation) # Fix the name to be accessible from the Project directory # so we know that the movie stack file will be moved # to final particles folder newImageName = '%d@%s' % newLocation frameRow.setValue(md.MDL_IMAGE, newImageName) frameRow.setValue(md.MDL_MICROGRAPH_ID, long(movId)) frameRow.setValue(md.MDL_MICROGRAPH, str(movId)) frameRow.setValue(md.MDL_FRAME_ID, long(frame)) frameRow.setValue(md.MDL_PARTICLE_ID, frameRow.getValue(md.MDL_ITEM_ID)) frameRow.writeToMd(movieMd, movieMd.addObject()) movieMd.addItemId() movieMd.write(movieMdFile) cleanPath(frameStk) if self.doNormalize: numberOfFrames = frameN - frame0 + 1 self._runNormalize(movieStk, numberOfFrames)
def _updateLocation(self, item, row): index, filename = xmippToLocation(row.getValue(md.MDL_IMAGE)) item.setLocation(index, filename)
def getImageDim(request, imagePath): imagePath = getImageFullPathFromRequest(request, imagePath) from pyworkflow.em.packages.xmipp3.convert import xmippToLocation location = xmippToLocation(imagePath) x, y, z, n = ImageHandler().getDimensions(location) return x, y, z, n
def _processMovie(self, movieId, movieName, movieFolder, shifts):###pasar shifts movieName = os.path.join(movieFolder, movieName) boxSize = self.boxSize.get() # Read movie dimensions to iterate through each frame imgh = ImageHandler() x, y, z, n = imgh.getDimensions(movieName) first = self.firstFrame.get() if first <= 1: first = 1 last = self.lastFrame.get() if last <= 0 or last >= n: last = n numberOfFrames = last - first + 1 stkIndex = 0 movieStk = self._getMovieName(movieId, '.stk') movieMdFile = self._getMovieName(movieId, '.xmd') movieMd = md.MetaData() frameMd = md.MetaData() frameMdImages = md.MetaData() frameRow = md.Row() for frame in range(first, last+1): # Get the frame shifts index = frame - first shiftX = shifts[2*index] shiftY = shifts[2*index+1] frameRoot = os.path.join(movieFolder, 'frame_%02d' % frame) frameName = frameRoot + '.mrc' frameMdFile = frameRoot + '.xmd' framePosFile = frameRoot + '_coordinates.xmd' coordinatesName = frameRoot + '_coordinates.xmd' hasCoordinates = self._writeXmippPosFile(movieId, movieName, coordinatesName, shiftX, shiftY) if hasCoordinates: self.info("Writing frame: %s" % frameName) #TODO: there is no need to write the frame and then operate #the input of the first operation should be the movie imgh.convert(tuple([frame, movieName]), frameName) if self.doRemoveDust: self.info("Removing Dust") self._runNoDust(frameName) self.info("Extracting particles") frameImages = frameRoot + '_images' args = '-i %(frameName)s --pos %(coordinatesName)s ' \ '-o %(frameRoot)s --Xdim %(boxSize)d' % locals() if self.doInvert: args += " --invert" args += " --downsampling %f " % self.factor self.runJob('xmipp_micrograph_scissor', args) cleanPath(frameName) frameStk = frameRoot + '.stk' self.info("Combining particles into one stack.") frameMdImages.read(frameMdFile) frameMd.read('particles@%s' % framePosFile) frameMd.merge(frameMdImages) for objId in frameMd: stkIndex += 1 frameRow.readFromMd(frameMd, objId) location = xmippToLocation(frameRow.getValue(md.MDL_IMAGE)) newLocation = (stkIndex, movieStk) imgh.convert(location, newLocation) # Fix the name to be accesible from the Project directory # so we know that the movie stack file will be moved # to final particles folder newImageName = '%d@%s' % newLocation frameRow.setValue(md.MDL_IMAGE, newImageName) frameRow.setValue(md.MDL_MICROGRAPH_ID, long(movieId)) frameRow.setValue(md.MDL_MICROGRAPH, str(movieId)) frameRow.setValue(md.MDL_FRAME_ID, long(frame)) frameRow.setValue(md.MDL_PARTICLE_ID, frameRow.getValue(md.MDL_ITEM_ID)) frameRow.writeToMd(movieMd, movieMd.addObject()) movieMd.addItemId() movieMd.write(movieMdFile) cleanPath(frameStk) if self.doNormalize: self._runNormalize(movieStk, numberOfFrames)
def _preprocessClass(self, classItem, classRow): classItem.average = Particle() classItem.average.setLocation(xmippToLocation(classRow.getValue(xmipp.MDL_IMAGE2)))