def validateSameSize(fileList, errors, errorPrefix='References'): '''Validate if a list of images(or volumes) have the same dimensions. The dimensions tuple is returned''' from xmipp import getImageSize firstFile = fileList[0] dim1 = getImageSize(firstFile) for filename in fileList[1:]: dim2 = getImageSize(filename) if dim1 != dim2: errors.append("%s: %s and %s have not the same size" % \ (errorPrefix, firstFile, filename)) return dim1
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 createMetaDataFromPattern(pattern, isStack=False, label="image"): ''' Create a metadata from files matching pattern''' import glob files = glob.glob(pattern) files.sort() from xmipp import MetaData, FileName, getImageSize, MDL_ENABLED, str2Label label = str2Label(label) #Check for label value mD = MetaData() inFile = FileName() nSize = 1 for file in files: fileAux=file if isStack: if file.endswith(".mrc"): fileAux=file+":mrcs" x, x, x, nSize = 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(MDL_ENABLED, 1, objId) counter += 1 else: objId = mD.addObject() mD.setValue(label, fileAux, objId) mD.setValue(MDL_ENABLED, 1, objId) return mD
def coarseSearch(log,WorkingDir,InputVolume,CylinderRadius,Rot0, RotF, RotStep, Z0, ZF, ZStep, NumberOfThreads,fnCoarse): args="-i %s --sym helical -z %f %f %f --rotHelical %f %f %f --thr %d -o %s"%(InputVolume,float(Z0),float(ZF),float(ZStep), float(Rot0),float(RotF),float(RotStep),NumberOfThreads,fnCoarse) if CylinderRadius>0: [xdim,ydim,zdim,ndim]=getImageSize(InputVolume) args+=" --mask cylinder %d %d"%(int(-CylinderRadius),int(-xdim)) runJob(log,'xmipp_volume_find_symmetry',args)
def invertStack(self, inputFn, outputFn): #get input dim (x,y,z,n) = xmipp.getImageSize(inputFn) #Create empty output stack for efficiency xmipp.createEmptyFile(outputFn,x,y,z,n) # handle image formats for i in range(1, n+1): self.invert((i, inputFn), (i, outputFn))
def convertStack(self, inputFn, outputFn, firstImg=None, lastImg=None, inFormat=None, outFormat=None): """ Convert an input stack file into another. It is possible to only use a subset of frames to be written in the output stack. If outFormat/inFomat=None then there will be inferred from extension.If firstFrame/lastFrame are not None, the output stack will be a subset of input stack. If it are none, the conversion is over the whole stack. If the input format is ".dm4" or ".img" only is allowed the conversion of the whole stack. """ inputLower = inputFn.lower() outputLower = outputFn.lower() if outputLower.endswith('.img'): if (firstImg and lastImg) is None: # FIXME Since now we can not read dm4 format in Scipion natively # or writing recent .img format # we are opening an Eman2 process to read the dm4 file from pyworkflow.em.packages.eman2.convert import convertImage convertImage(inputFn, outputFn) else: ext = os.path.splitext(outputFn)[1] raise Exception("if convert from %s, firstImg and lastImg " "must be None" % ext) # elif inputLower.endswith('.tif'): # # FIXME: It seems that we have some flip problem with compressed # # tif files, we need to check that # if outputLower.endswith('.mrc'): # self.runJob('tif2mrc', '%s %s' % (inputFn, outputFn)) # else: # raise Exception("Conversion from tif to %s is not " # "implemented yet. " % pwutils.getExt(outputFn)) else: # get input dim (x, y, z, n) = xmipp.getImageSize(inputFn) location = self._convertToLocation(inputFn) self._img.read(location, xmipp.HEADER) dataType = self.getSupportedDataType(self._img.getDataType(), outputLower) if (firstImg and lastImg) is None: n = max(z, n) firstImg = 1 lastImg = n else: n = lastImg - firstImg + 1 # Create empty output stack file to reserve desired space xmipp.createEmptyFile(outputFn, x, y, 1, n, dataType) for i, j in izip(range(firstImg, lastImg + 1), range(1, n + 1)): self.convert((i, inputFn), (j, outputFn))
def estimateImageSize(input): memory = 0 input = FileName(input) if input.exists() and input.isImage(): (Xdim, Ydim, Zdim, Ndim) = xmipp.getImageSize(input) memory = Xdim * Ydim * Zdim * Ndim * 8 return memory
def fineSearch(log,WorkingDir,InputVolume,CylinderRadius,fnCoarse,Rot0, RotF, Z0, ZF,fnFine): md=MetaData(fnCoarse) id=md.firstObject() rot0=md.getValue(MDL_ANGLE_ROT,id) z0=md.getValue(MDL_SHIFT_Z,id) args="-i %s --sym helical -z %f %f --rotHelical %f %f --localHelical %f %f -o %s"%(InputVolume,float(Z0),float(ZF), float(Rot0),float(RotF),z0,rot0,fnFine) if CylinderRadius>0: [xdim,ydim,zdim,ndim]=getImageSize(InputVolume) args+=" --mask cylinder %d %d"%(int(-CylinderRadius),int(-xdim)) runJob(log,'xmipp_volume_find_symmetry',args)
def symmetrize(log,WorkingDir,InputVolume,OutputVolume,CylinderRadius,fnFine): md=MetaData(fnFine) id=md.firstObject() rot0=md.getValue(MDL_ANGLE_ROT,id) z0=md.getValue(MDL_SHIFT_Z,id) args="-i %s --sym helical --helixParams %f %f -o %s"%(InputVolume,z0,rot0,OutputVolume) runJob(log,'xmipp_transform_symmetrize',args) if CylinderRadius>0: [xdim,ydim,zdim,ndim]=getImageSize(InputVolume) args="-i %s --mask cylinder %d %d"%(OutputVolume,int(-CylinderRadius),int(-xdim)) runJob(log,'xmipp_transform_mask',args)
def convertStack(self, inputFn, outputFn, inFormat=None, outFormat=None): """ convert format of stack file. Output/input format is specified by outFormat/inFormat. If outFormat/inFomat=None then there will be inferred from extension """ #get input dim (x,y,z,n) = xmipp.getImageSize(inputFn) #Create empty output stack for efficiency xmipp.createEmptyFile(outputFn,x,y,z,n) # handle image formats for i in range(1, n+1): self.convert((i, inputFn), (i, outputFn))
def generateChimeraScript(log,WorkingDir,MaskMode,Threshold,InputStructure,PseudoAtomRadius,Sampling): fhCmd=open(os.path.join(WorkingDir,"chimera.cmd"),'w') fhCmd.write("open pseudoatoms.pdb\n") fhCmd.write("rangecol bfactor,a 0 white 1 red\n") fhCmd.write("setattr a radius "+str(PseudoAtomRadius*Sampling)+"\n") fhCmd.write("represent sphere\n") fhCmd.write("open "+os.path.basename(InputStructure)+"\n") if MaskMode!="Threshold": Threshold=0.01 [xdim,ydim,zdim,ndim]=getImageSize(InputStructure) fhCmd.write("volume #1 level "+str(Threshold)+" transparency 0.5 voxelSize "+str(Sampling)+" originIndex "+str(xdim/2)+"\n") fhCmd.close()
def convertStack(self, inputFn, outputFn, firstImg=None, lastImg=None, inFormat=None, outFormat=None): """ Convert an input stack file into another. It is possible to only use a subset of frames to be written in the output stack. If outFormat/inFomat=None then there will be inferred from extension.If firstFrame/lastFrame are not None, the output stack will be a subset of input stack. If it are none, the conversion is over the whole stack. If the input format is ".dm4" or ".img" only is allowed the conversion of the whole stack. """ inputLower = inputFn.lower() outputLower = outputFn.lower() if outputLower.endswith('.img'): if (firstImg and lastImg) is None: # FIXME Since now we can not read dm4 format in Scipion natively # or writing recent .img format # we are opening an Eman2 process to read the dm4 file from pyworkflow.em.packages.eman2.convert import convertImage convertImage(inputFn, outputFn) else: ext = os.path.splitext(outputFn)[1] raise Exception("if convert from %s, firstImg and lastImg " "must be None" % ext) # elif inputLower.endswith('.tif'): # # FIXME: It seems that we have some flip problem with compressed # # tif files, we need to check that # if outputLower.endswith('.mrc'): # self.runJob('tif2mrc', '%s %s' % (inputFn, outputFn)) # else: # raise Exception("Conversion from tif to %s is not " # "implemented yet. " % pwutils.getExt(outputFn)) else: # get input dim (x, y, z, n) = xmipp.getImageSize(inputFn) location = self._convertToLocation(inputFn) self._img.read(location, xmipp.HEADER) dataType = self.getSupportedDataType(self._img.getDataType(), outputLower) if (firstImg and lastImg) is None: n = max(z, n) firstImg = 1 lastImg = n else: n = lastImg - firstImg + 1 # Create empty output stack file to reserve desired space xmipp.createEmptyFile(outputFn,x,y,1,n, dataType) for i, j in izip(range(firstImg, lastImg + 1), range(1, n+1)): self.convert((i, inputFn), (j, outputFn))
def estimateMemory(input): from math import log, ceil MD = xmipp.MetaData(input) memory = 0 for id in MD: if MD.containsLabel(xmipp.MDL_IMAGE): fnImg = MD.getValue(xmipp.MDL_IMAGE, id) elif MD.containsLabel(xmipp.MDL_MICROGRAPH): fnImg = MD.getValue(xmipp.MDL_MICROGRAPH, id) (Xdim, Ydim, Zdim, Ndim) = xmipp.getImageSize(fnImg) memory = max(memory, Xdim * Ydim * 8) memoryMb = int((2 ** ceil(log(memory, 2))) / (2 ** 20)); # Memory size in megabytes return memoryMb
def findDihedrical(log,WorkingDir,InputVolume,CylinderRadius): fnRotated=os.path.join(WorkingDir,'tmp','volume_rotated.vol') runJob(log,"xmipp_transform_geometry","-i %s -o %s --rotate_volume axis 180 1 0 0"%(InputVolume,fnRotated)) if CylinderRadius>0: [xdim,ydim,zdim,ndim]=getImageSize(InputVolume) maskArgs=" --mask cylinder %d %d"%(int(-CylinderRadius),int(-xdim)) else: maskArgs="" runJob(log,"xmipp_volume_align","--i1 %s --i2 %s --rot 0 360 3 -z -2 2 0.5 --apply"%(InputVolume,fnRotated)+maskArgs) runJob(log,"xmipp_volume_align","--i1 %s --i2 %s --local --apply"%(InputVolume,fnRotated)+maskArgs) runJob(log,"xmipp_image_operate","-i %s --plus %s -o %s"%(InputVolume,fnRotated,InputVolume)) runJob(log,"xmipp_image_operate","-i %s --divide 2"%(InputVolume)) runJob(log,"xmipp_transform_mask","-i %s "%(InputVolume)+maskArgs) deleteFile(log,fnRotated)
def _getImageString(self, filename): if isStandardImage(filename): return "Image file." x, y, z, n = xmipp.getImageSize(filename) objType = 'Image' dimMsg = "*%(objType)s file*\n dimensions: %(x)d x %(y)d" expMsg = "Columns x Rows " if z > 1: dimMsg += " x %(z)d" expMsg += " x Slices" objType = 'Volume' if n > 1: dimMsg += " x %(n)d" expMsg += " x Objects" objType = 'Stack' return (dimMsg + "\n" + expMsg) % locals()
def convertStack(self, inputFn, outputFn, firstImg=None, lastImg=None, inFormat=None, outFormat=None): """ Convert an input stack file into another. It is possible to only use a subset of frames to be written in the output stack. If outFormat/inFomat=None then there will be inferred from extension.If firstFrame/lastFrame are not None, the output stack will be a subset of input stack. If it are none, the conversion is over the whole stack. If the input format is ".dm4" or ".img" only is allowed the conversion of the whole stack. """ if (inputFn.lower().endswith('.dm4') or outputFn.lower().endswith('.img')): if (firstImg and lastImg) is None: # FIXME Since now we can not read dm4 format in Scipion natively # or writing recent .img format # we are opening an Eman2 process to read the dm4 file from pyworkflow.em.packages.eman2.convert import convertImage convertImage(inputFn, outputFn) else: ext = os.path.splitext(outputFn)[1] raise Exception("if convert from %s, firstImg and lastImg " "must be None" % ext) else: # get input dim (x, y, z, n) = xmipp.getImageSize(inputFn) location = self._convertToLocation(inputFn) self._img.read(location, xmipp.HEADER) dataType = self._img.getDataType() if (firstImg and lastImg) is None: n = max(z, n) firstImg = 1 lastImg = n else: n = lastImg - firstImg + 1 # Create empty output stack file to reserve desired space xmipp.createEmptyFile(outputFn, x, y, 1, n, dataType) for i, j in izip(range(firstImg, lastImg + 1), range(1, n + 1)): self.convert((i, inputFn), (j, outputFn))
def preprocess(self): import launch_job,xmipp if self.DoFilter: slope=max(self.Highpass/2,0.01) fnOut=self.WorkingDir+'/preprocessedImages.stk' self.selFileToUse=fnOut if not self.doStep1 and os.path.exists(fnOut): numberOfImages=xmipp.getImageSize(fnOut)[3] from protlib_xmipp import getMdSize size = getMdSize(self.InSelFile) if size != numberOfImages: self.doStep1 = True if not stepPerformed("Step 1",self.WorkingDir + "/status.txt"): self.doStep1 = True if self.doStep1: params= '-i '+str(self.InSelFile)+\ ' --fourier_mask raised_cosine '+str(slope)+\ ' -o '+fnOut if self.Highpass>0 and self.Lowpass>0: params+=" --band_pass "+str(self.Highpass)+" "+str(self.Lowpass) elif self.Highpass>0: params+=" --high_pass "+str(self.Highpass) elif self.Lowpass>0: params+=" --low_pass "+str(self.Lowpass) launchJob("xmipp_fourier_filter", params, self.log, False, 1, 1, self.SystemFlavour) # Update status if os.path.exists(self.WorkingDir+'/preprocessedImages.stk'): fh=open(self.WorkingDir + "/status.txt", "a") fh.write("Step 1: Preprocessing finished at " + time.asctime() + "\n") fh.close() else: self.selFileToUse=self.InSelFile
def validateVolumeZdim(fileName,errors): (Xdim, Ydim, Zdim, Ndim) = xmipp.getImageSize(fileName) if Zdim == 1: errors.append("Volume %s has Zdim=1. If MRC file add :mrc at the end of the fileName")
def validate(self): errors = [] (Xdim,Ydim,Zdim,Ndim)=getImageSize(self.Volume) if Xdim!=self.Xdim: errors.append("Make sure that the volume and the images have the same size") return errors