def __correctFrameImage(self,framelist,use_full_raw_area=False):
		# load raw frames
		corrected_array = self.sumupFrames(self.rawframe_dir,framelist)
		if corrected_array is False:
			return False
		if save_jpg:
			numpil.write(corrected,'%s_gain_corrected.jpg' % ddtype,'jpeg')
		return corrected_array
示例#2
0
 def __correctFrameImage(self, framelist, use_full_raw_area=False):
     # load raw frames
     corrected_array = self.sumupFrames(self.rawframe_dir, framelist)
     if corrected_array is False:
         return False
     if save_jpg:
         numpil.write(corrected, '%s_gain_corrected.jpg' % ddtype, 'jpeg')
     return corrected_array
def array2jpg(pictpath,im,imin=None,imax=None,size=512):
        jpgpath = pictpath+'.jpg'
        imshape = im.shape
        scale = float(size)/imshape[1]
        im = apImage.scaleImage(im,scale)
        stats = arraystats.all(im)
        if imin is not None and imax is not None:
                range = (imin,imax)
        else:
                range = stats['mean']-3*stats['std'],stats['mean']+3*stats['std']
        numpil.write(im,jpgpath, format = 'JPEG', limits=range)
def array2jpg(pictpath, im, imin=None, imax=None, size=512):
    jpgpath = pictpath + '.jpg'
    imshape = im.shape
    scale = float(size) / imshape[1]
    im = apImage.scaleImage(im, scale)
    stats = arraystats.all(im)
    if imin is not None and imax is not None:
        range = (imin, imax)
    else:
        range = stats['mean'] - 3 * stats['std'], stats[
            'mean'] + 3 * stats['std']
    numpil.write(im, jpgpath, format='JPEG', limits=range)
	def start(self):
		self.params['output_fileformat'] = 'mrc'
		newstackname='framealigned.hed'
		stackdata=apStack.getStackParticlesFromId(self.params['stackid'])
		stackrundata=apStack.getOnlyStackData(self.params['stackid'])
		apix=stackrundata['pixelsize']*1e10
		kev=stackdata[0]['particle']['image']['scope']['high tension']/1000
		origstackpath=os.path.join(stackrundata['path']['path'],stackrundata['name'])
		boxsize=stackdata[0]['stackRun']['stackParams']['boxSize']
		binning=stackdata[0]['stackRun']['stackParams']['bin']	
		
		#determine camera type
		cameratype=stackdata[0]['particle']['image']['camera']['ccdcamera']['name']
		if self.params['override_camera'] is not None:
			cameratype=self.params['override_camera']
		
		#create sorted boxfiles
		imagedict={}
		masterlist=[]
		for particle in stackdata:
			parentimage=particle['particle']['image']['filename']
			if parentimage in imagedict.keys():
				imagedict[parentimage].append(particle['particle'])
			else:
				imagedict[parentimage]=[]
				imagedict[parentimage].append(particle['particle'])
			index=len(imagedict[parentimage])-1
			masterlist.append({'particle':particle,'key':parentimage,'index':index})
		#print masterlist
		
		for key in imagedict:
			particlelst=imagedict[key]
			parentimage=key
			framespath=particlelst[0]['image']['session']['frame path']
			
			print cameratype
			if 'Gatan' in cameratype:
				#prepare frames
				print framespath
				
				#prepare frame directory
				framespathname=os.path.join(self.params['rundir'],parentimage+'.frames')
				if os.path.exists(framespathname):
					pass
				else:
					os.mkdir(framespathname)
				print framespathname
				
				mrcframestackname=parentimage+'.frames.mrc'
				
				print mrcframestackname
				
				nframes=particlelst[0]['image']['camera']['nframes']
				
				print "Extracting frames for", mrcframestackname
				for n in range(nframes):
					a=mrc.read(os.path.join(framespath,mrcframestackname),n)
					numpil.write(a,imfile=os.path.join(framespathname,'RawImage_%d.tif' % (n)), format='tiff')
				
			elif 'DE' in cameratype:
				framespathname=os.path.join(framespath,parentimage+'.frames')
			
			print os.getcwd()
			print framespathname
			#generate DE script call
			if os.path.exists(framespathname):
				print "found frames for", parentimage

				nframes=particlelst[0]['image']['camera']['nframes']
				boxname=parentimage + '.box'
				boxpath=os.path.join(framespathname,boxname)
				shiftdata={'scale':1,'shiftx':0,'shifty':0}

				#flatfield references
				brightrefpath=particlelst[0]['image']['bright']['session']['image path']
				brightrefname=particlelst[0]['image']['bright']['filename']
				brightnframes=particlelst[0]['image']['bright']['camera']['nframes']
				darkrefpath=particlelst[0]['image']['dark']['session']['image path']
				darkrefname=particlelst[0]['image']['dark']['filename']
				darknframes=particlelst[0]['image']['dark']['camera']['nframes']
				brightref=os.path.join(brightrefpath,brightrefname+'.mrc')
				darkref=os.path.join(darkrefpath,darkrefname+'.mrc')
				print brightref
				print darkref			
				apBoxer.processParticleData(particle['particle']['image'],boxsize,particlelst,shiftdata,boxpath)
				print framespathname			

				#set appion specific options
				self.params['gainreference_filename']=brightref
				self.params['gainreference_framecount']=brightnframes
				self.params['darkreference_filename']=darkref
				self.params['darkreference_framecount']=darknframes
				self.params['input_framecount']=nframes
				self.params['boxes_fromfiles']=1
				#self.params['run_verbosity']=3
				self.params['output_invert']=0
				#self.params['radiationdamage_apix=']=apix
				self.params['radiationdamage_voltage']=kev
				#self.params['boxes_boxsize']=boxsize

				outpath=os.path.join(self.params['rundir'],key)
				if os.path.exists(outpath):
					shutil.rmtree(outpath)
				os.mkdir(outpath)
				
				command=['deProcessFrames.py']
				keys=self.params.keys()
				keys.sort()
				for key in keys:
					param=self.params[key]
					#print key, param, type(param)
					if param == None or param=='':
						pass
					else:
						option='--%s=%s' % (key,param)
						command.append(option)
				command.append(outpath)
				command.append(framespathname)
				print command
				if self.params['dryrun'] is False:
					subprocess.call(command)
					
		
		#recreate particle stack
		for n,particledict in enumerate(masterlist):
			parentimage=particledict['key']
			correctedpath=os.path.join(self.params['rundir'],parentimage)
			print correctedpath
			if os.path.exists(correctedpath):
			
				correctedparticle=glob.glob(os.path.join(correctedpath,('%s.*.region_%03d.*' % (parentimage,particledict['index']))))
				print os.path.join(correctedpath,('%s.*.region_%03d.*' % (parentimage,particledict['index'])))
				print correctedparticle
				#sys.exit()
				command=['proc2d',correctedparticle[0], newstackname]
				if self.params['output_rotation'] !=0:
					command.append('rot=%d' % self.params['output_rotation'])
				
				if self.params['show_DE_command'] is True:
					print command
				subprocess.call(command)
			else:
				print "did not find frames for ", parentimage
				command=['proc2d', origstackpath, newstackname,('first=%d' % n), ('last=%d' % n)]
				print command
				if self.params['dryrun'] is False:
					subprocess.call(command)
				
		#upload stack
		
		#make keep file
		self.params['keepfile']='keepfile.txt'
		f=open(self.params['keepfile'],'w')
		for n in range(len(masterlist)):
			f.write('%d\n' % (n))
		f.close()
		
		apStack.commitSubStack(self.params, newname=newstackname)
		apStack.averageStack(stack=newstackname)
		
		print "Done!!!!"
示例#6
0
    def start(self):
        self.params['output_fileformat'] = 'mrc'
        newstackname = 'framealigned.hed'
        stackdata = apStack.getStackParticlesFromId(self.params['stackid'])
        stackrundata = apStack.getOnlyStackData(self.params['stackid'])
        apix = stackrundata['pixelsize'] * 1e10
        kev = stackdata[0]['particle']['image']['scope']['high tension'] / 1000
        origstackpath = os.path.join(stackrundata['path']['path'],
                                     stackrundata['name'])
        boxsize = stackdata[0]['stackRun']['stackParams']['boxSize']
        binning = stackdata[0]['stackRun']['stackParams']['bin']

        #determine camera type
        cameratype = stackdata[0]['particle']['image']['camera']['ccdcamera'][
            'name']
        if self.params['override_camera'] is not None:
            cameratype = self.params['override_camera']

        #create sorted boxfiles
        imagedict = {}
        masterlist = []
        for particle in stackdata:
            parentimage = particle['particle']['image']['filename']
            if parentimage in imagedict.keys():
                imagedict[parentimage].append(particle['particle'])
            else:
                imagedict[parentimage] = []
                imagedict[parentimage].append(particle['particle'])
            index = len(imagedict[parentimage]) - 1
            masterlist.append({
                'particle': particle,
                'key': parentimage,
                'index': index
            })
        #print masterlist

        for key in imagedict:
            particlelst = imagedict[key]
            parentimage = key
            framespath = particlelst[0]['image']['session']['frame path']

            print cameratype
            if 'Gatan' in cameratype:
                #prepare frames
                print framespath

                #prepare frame directory
                framespathname = os.path.join(self.params['rundir'],
                                              parentimage + '.frames')
                if os.path.exists(framespathname):
                    pass
                else:
                    os.mkdir(framespathname)
                print framespathname

                mrcframestackname = parentimage + '.frames.mrc'

                print mrcframestackname

                nframes = particlelst[0]['image']['camera']['nframes']

                print "Extracting frames for", mrcframestackname
                for n in range(nframes):
                    a = mrc.read(os.path.join(framespath, mrcframestackname),
                                 n)
                    numpil.write(a,
                                 imfile=os.path.join(framespathname,
                                                     'RawImage_%d.tif' % (n)),
                                 format='tiff')

            elif 'DE' in cameratype:
                framespathname = os.path.join(framespath,
                                              parentimage + '.frames')

            print os.getcwd()
            print framespathname
            #generate DE script call
            if os.path.exists(framespathname):
                print "found frames for", parentimage

                nframes = particlelst[0]['image']['camera']['nframes']
                boxname = parentimage + '.box'
                boxpath = os.path.join(framespathname, boxname)
                shiftdata = {'scale': 1, 'shiftx': 0, 'shifty': 0}

                #flatfield references
                brightrefpath = particlelst[0]['image']['bright']['session'][
                    'image path']
                brightrefname = particlelst[0]['image']['bright']['filename']
                brightnframes = particlelst[0]['image']['bright']['camera'][
                    'nframes']
                darkrefpath = particlelst[0]['image']['dark']['session'][
                    'image path']
                darkrefname = particlelst[0]['image']['dark']['filename']
                darknframes = particlelst[0]['image']['dark']['camera'][
                    'nframes']
                brightref = os.path.join(brightrefpath, brightrefname + '.mrc')
                darkref = os.path.join(darkrefpath, darkrefname + '.mrc')
                print brightref
                print darkref
                apBoxer.processParticleData(particle['particle']['image'],
                                            boxsize, particlelst, shiftdata,
                                            boxpath)
                print framespathname

                #set appion specific options
                self.params['gainreference_filename'] = brightref
                self.params['gainreference_framecount'] = brightnframes
                self.params['darkreference_filename'] = darkref
                self.params['darkreference_framecount'] = darknframes
                self.params['input_framecount'] = nframes
                self.params['boxes_fromfiles'] = 1
                #self.params['run_verbosity']=3
                self.params['output_invert'] = 0
                #self.params['radiationdamage_apix=']=apix
                self.params['radiationdamage_voltage'] = kev
                #self.params['boxes_boxsize']=boxsize

                outpath = os.path.join(self.params['rundir'], key)
                if os.path.exists(outpath):
                    shutil.rmtree(outpath)
                os.mkdir(outpath)

                command = ['deProcessFrames.py']
                keys = self.params.keys()
                keys.sort()
                for key in keys:
                    param = self.params[key]
                    #print key, param, type(param)
                    if param == None or param == '':
                        pass
                    else:
                        option = '--%s=%s' % (key, param)
                        command.append(option)
                command.append(outpath)
                command.append(framespathname)
                print command
                if self.params['dryrun'] is False:
                    subprocess.call(command)

        #recreate particle stack
        for n, particledict in enumerate(masterlist):
            parentimage = particledict['key']
            correctedpath = os.path.join(self.params['rundir'], parentimage)
            print correctedpath
            if os.path.exists(correctedpath):

                correctedparticle = glob.glob(
                    os.path.join(correctedpath,
                                 ('%s.*.region_%03d.*' %
                                  (parentimage, particledict['index']))))
                print os.path.join(correctedpath,
                                   ('%s.*.region_%03d.*' %
                                    (parentimage, particledict['index'])))
                print correctedparticle
                #sys.exit()
                command = ['proc2d', correctedparticle[0], newstackname]
                if self.params['output_rotation'] != 0:
                    command.append('rot=%d' % self.params['output_rotation'])

                if self.params['show_DE_command'] is True:
                    print command
                subprocess.call(command)
            else:
                print "did not find frames for ", parentimage
                command = [
                    'proc2d', origstackpath, newstackname, ('first=%d' % n),
                    ('last=%d' % n)
                ]
                print command
                if self.params['dryrun'] is False:
                    subprocess.call(command)

        #upload stack

        #make keep file
        self.params['keepfile'] = 'keepfile.txt'
        f = open(self.params['keepfile'], 'w')
        for n in range(len(masterlist)):
            f.write('%d\n' % (n))
        f.close()

        apStack.commitSubStack(self.params, newname=newstackname)
        apStack.averageStack(stack=newstackname)

        print "Done!!!!"