def getTiltGeom(ordered_imagelist, bin, refimg):
        # get the tilt geometry parameters
        #self.params['aligndir'],self.params['imagedir'] = apProTomo.setProtomoDir(self.params['rundir'])

        # self.params['imagedir'] when not testing with renamed images
        rawimagenames = apProTomo.linkImageFiles(ordered_imagelist, "/ami/data17/leginon/10nov10z/rawdatatest")
        shifts = apTomo.getGlobalShift(ordered_imagelist, corr_bin, refimg)

        tltparams = apProTomo.convertShiftsToParams(self.tilts, shifts, self.center, rawimagenames)
        return tltparams
def getTiltGeom(ordered_imagelist, bin, refimg):
    # get the tilt geometry parameters
    #self.params['aligndir'],self.params['imagedir'] = apProTomo.setProtomoDir(self.params['rundir'])

    # self.params['imagedir'] when not testing with renamed images
    rawimagenames = apProTomo.linkImageFiles(
        ordered_imagelist, "/ami/data17/leginon/10nov10z/rawdatatest")
    shifts = apTomo.getGlobalShift(ordered_imagelist, corr_bin, refimg)

    tltparams = apProTomo.convertShiftsToParams(self.tilts, shifts,
                                                self.center, rawimagenames)
    return tltparams
        def start(self):
        
                ### some of this should go in preloop functions
        
                ###do queries
                sessiondata = apDatabase.getSessionDataFromSessionName(self.params['sessionname'])
                self.sessiondata = sessiondata
                tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(self.params['tiltseries'],sessiondata)
                tiltdata=apTomo.getImageList([tiltseriesdata])
                description = self.params['description']
                apDisplay.printMsg("getting imagelist")

                tilts,ordered_imagelist,ordered_mrc_files,refimg = apTomo.orderImageList(tiltdata)
                #tilts are tilt angles, ordered_imagelist are imagedata, ordered_mrc_files are paths to files, refimg is an int

                ###set up files
                seriesname='series'+str(self.params['tiltseries'])
                tiltfilename=seriesname+'.tlt'
                param_out=seriesname+'.param'
                maxtilt=max([abs(tilts[0]),abs(tilts[-1])])
                apDisplay.printMsg("highest tilt angle is %f" % maxtilt)
                self.params['cos_alpha']=math.cos(maxtilt*math.pi/180)
                self.params['raw_path']=os.path.join(self.params['rundir'],'raw')


                rawexists=apParam.createDirectory(self.params['raw_path'])

                apDisplay.printMsg("copying raw images")
                newfilenames=apProTomo.getImageFiles(ordered_imagelist,self.params['raw_path'], link=False)
        
                ###create tilt file

                #get image size from the first image
                imagesizex=tiltdata[0]['image'].shape[0]
                imagesizey=tiltdata[0]['image'].shape[1]

                #shift half tilt series relative to eachother
                #SS I'm arbitrarily making the bin parameter here 1 because it's not necessary to sample at this point
                shifts = apTomo.getGlobalShift(ordered_imagelist, 1, refimg)
                
                #OPTION: refinement might be more robust by doing one round of IMOD aligment to prealign images before doing protomo refine
                origins=apProTomo2Prep.convertShiftsToOrigin(shifts, imagesizex, imagesizey)

                #determine azimuth
                azimuth=apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
                apProTomo2Prep.writeTileFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
def prepareTiltFile(sessionname, seriesname, tiltfilename, tiltseriesnumber, raw_path, frame_aligned_images, link=False, coarse=True):
	'''
	Creates tlt file from basic image information and copies raw images
	'''
	
	sessiondata = apDatabase.getSessionDataFromSessionName(sessionname)
	tiltseriesdata = apDatabase.getTiltSeriesDataFromTiltNumAndSessionId(tiltseriesnumber,sessiondata)
	tiltdata = apTomo.getImageList([tiltseriesdata])
	apDisplay.printMsg("getting imagelist")
	
	frame_tiltdata, non_frame_tiltdata = frameOrNonFrameTiltdata(tiltdata)
	tilts,ordered_imagelist,accumulated_dose_list,ordered_mrc_files,refimg = apTomo.orderImageList(frame_tiltdata, non_frame_tiltdata, frame_aligned=frame_aligned_images)
	if frame_aligned_images == "True":  #Azimuth is only present in the non-frame aligned images
		a,ordered_imagelist_for_azimuth,c,d,e = apTomo.orderImageList(frame_tiltdata, non_frame_tiltdata, frame_aligned="False")
	
	#tilts are tilt angles, ordered_imagelist are imagedata, ordered_mrc_files are paths to files, refimg is an int
	maxtilt = max([abs(tilts[0]),abs(tilts[-1])])
	apDisplay.printMsg("highest tilt angle is %f" % maxtilt)
	
	if coarse == "True":
		if frame_aligned_images == "True":  #Azimuth is only present in the non-frame aligned images
			azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist_for_azimuth)
		else:
			azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
		
		rawexists = apParam.createDirectory(raw_path)
		
		apDisplay.printMsg("Copying raw images, y-flipping, normalizing, and converting images to float32 for Protomo...") #Linking removed because raw images need to be y-flipped for Protomo:(.
		newfilenames, new_ordered_imagelist = apProTomo.getImageFiles(ordered_imagelist, raw_path, link=False, copy="True")
		
		###create tilt file
		#get image size from the first image
		imagesizex = tiltdata[0]['image'].shape[1]
		imagesizey = tiltdata[0]['image'].shape[0]
		
		#shift half tilt series relative to eachother
		#SS I'm arbitrarily making the bin parameter here 1 because it's not necessary to sample at this point
		shifts = apTomo.getGlobalShift(ordered_imagelist, 1, refimg)
		
		#OPTION: refinement might be more robust by doing one round of IMOD aligment to prealign images before doing protomo refine
		origins = convertShiftsToOrigin(shifts, imagesizex, imagesizey)
	
		writeTiltFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
	
	return tilts, accumulated_dose_list, new_ordered_imagelist, maxtilt
예제 #5
0
	def start(self):
		commit = self.params['commit']
		tiltdatalist = apTomo.getTiltdataList(self.params['tiltseries'],self.params['othertiltseries'])
		sessiondata = tiltdatalist[0]['session']
		description = self.params['description']
		alignsample = self.params['sample']
		cycle = self.params['cycle']
		alignmethod = self.params['alignmethod']
		apDisplay.printMsg("getting imagelist")
		imagelist = apTomo.getImageList(tiltdatalist)
		tilts,ordered_imagelist,ordered_mrc_files,refimg = apTomo.orderImageList(imagelist)
		
		# This parameter is needed for protomo, but not protomo2
		if self.params['refimg']:
			refimg = self.params['refimg']

		if alignmethod != 'protomo2':
			for file in ordered_mrc_files:
				# protomo can not function with a negative origin
				# protomo2 CAN function with negative origin
				apImage.shiftMRCStartToZero(file)
				
			apDisplay.printMsg("getting pixelsize")
			pixelsize = apTomo.getTomoPixelSize(ordered_imagelist[refimg])
			if pixelsize is None:
				apDisplay.printError('Pixel Size not retrieved. Invalid tilt series for processing')

		imgshape = apTomo.getTomoImageShape(ordered_imagelist[refimg])
		corr_bin = apTomo.getCorrelatorBinning(imgshape)
		center = {'x':imgshape[1]/2,'y':imgshape[0]/2}
		default_azimuth = apTomo.getDefaultAzimuthFromLeginon(ordered_imagelist[refimg])

		processdir = os.path.abspath(self.params['rundir'])
		imodseriesname = apTomo.getFilename(tiltdatalist)
		
		# protomo2 does not write out text files, intermediate info is in memory and the final result is binary
		seriesname = 'tomo'+ imodseriesname
		# Write tilt series stack images and tilt angles, not needed for protomo2
		if alignmethod != 'protomo2':
			stackdir = self.params['tiltseriesdir']
			stackname = imodseriesname+".st"
			apTomo.writeTiltSeriesStack(stackdir,stackname,ordered_mrc_files,1e10*pixelsize)
			apImod.writeRawtltFile(stackdir,imodseriesname,tilts)
		
		leginonxcorrlist = []
		for tiltdata in tiltdatalist:
			settingsdata = apTomo.getTomographySettings(sessiondata,tiltdata)
			leginonxcorrlist.append(settingsdata)
			
		# Run protomo2 
		if alignmethod == 'protomo2':
			self.runProtomo2(sessiondata, processdir, seriesname, ordered_imagelist, refimg, center, corr_bin, commit, tilts)
			
			# protomo2 does not need anything beyond this point, so exit
			return
			
		if cycle != 1 or alignmethod != 'protomo':
			cycles=[cycle,]
		else:
			# also process and commit protomo cycle 0 if doing cycle 1
			cycles=[0,1]
		for cycle in cycles:
			if alignmethod == 'protomo' or alignmethod == 'leginon':
				self.params['aligndir'],self.params['imagedir'] =	apProTomo.setProtomoDir(self.params['rundir'],cycle)
				aligndir = self.params['aligndir']
				# Link images into rundir/raw
				rawimagenames = apProTomo.linkImageFiles(ordered_imagelist,self.params['imagedir'])
				tltfile = os.path.join(aligndir,seriesname+'-%02d-itr.tlt' % (cycle,))
				if cycle == 0:
					# get initial shift alignment from leginon tiltcorrelator
					# Assume all tiltdata have the same tomography settings
					shifts = apTomo.getGlobalShift(ordered_imagelist, corr_bin, refimg)
					tltparams = apProTomo.convertShiftsToParams(tilts,shifts,center,default_azimuth,rawimagenames)
					apProTomo.writeTiltFile(tltfile,seriesname, tltparams[0], tltparams[1])
					refineparamdict=apProTomo.createRefineDefaults(len(tilts),
							os.path.join(processdir,'raw'),os.path.join(processdir,'out'))
					refineparamdict = apProTomo.updateRefineParams(refineparamdict,imgshape,alignsample,100,refimg)
				else:
					lasttltfile = os.path.join(aligndir,seriesname+'-%02d-fitted.tlt' % (cycle-1,))
					if self.params['goodcycle']:
						if (self.params['goodstart'] == 0 and self.params['goodend'] == len(ordered_imagelist) - 1):
							# revert goodcycle related params since nothing will be reset
							self.params['goodcycle'] = None
							self.params['goodstart'] = None
							self.params['goodend'] = None
					if not self.params['goodcycle']:
						#default uses last cycle
						shutil.copy(lasttltfile,tltfile)
					else:
						if not (self.params['goodstart'] or self.params['goodend']):
							shutil.copy(lasttltfile,tltfile)
						else:
							# Reset bad ending tilts before alignment cycle
							goodtltfile = os.path.join(aligndir,seriesname+'-%02d-fitted.tlt' % (self.params['goodcycle'],))
							goodtltparams = apProTomo.parseTilt(goodtltfile)
							lasttltparams = apProTomo.parseTilt(lasttltfile)
							tltparams = apProTomo.resetTiltParams(lasttltparams,goodtltparams,self.params['goodstart'],self.params['goodend'])
							apProTomo.writeTiltFile(tltfile,seriesname, tltparams[0], tltparams[1])
					lastrefineparamfile = os.path.join(aligndir,seriesname+'-%02d.param' % (cycle-1,))
					refineparamdict = apProTomo.parseRefineParamFile(lastrefineparamfile)
					refineparamdict = apProTomo.updateRefineParams(refineparamdict,imgshape,alignsample,self.params['region'],refimg)
				# Write param file in rundir/align
				paramfilepath = os.path.join(seriesname+'-%02d.param' % (cycle))
				fullparamfilepath = os.path.join(aligndir,paramfilepath)
				apProTomo.writeRefineParamFile(refineparamdict,fullparamfilepath)
				# run porjalign script
				os.chdir(aligndir)
				if cycle > 0:
					self.runProjalign(paramfilepath)
				else:
					lasttltfile = os.path.join(aligndir,seriesname+'-%02d-itr.tlt' % (cycle,))
					newtltfile = os.path.join(aligndir,seriesname+'-%02d-fitted.tlt' % (cycle,))
					shutil.copy(lasttltfile,newtltfile)
				# convert to imod alignment
				alignpathprefix = os.path.join(processdir, seriesname)
				centertuple = (center['x'],center['y'])
				apProTomo.convertGlobalTransformProtomoToImod(seriesname+'-%02d' %(cycle),imodseriesname, centertuple)
			elif alignmethod == 'imod-shift':
				# Correlation by Coarse correlation in IMOD
				aligndir = processdir
				imodxcorrdata = apImod.coarseAlignment(stackdir, processdir, imodseriesname, commit)
				# Global Transformation
				gtransforms = apImod.convertToGlobalAlignment(processdir, imodseriesname)
			# Create Aligned Stack for record
			bin = int(math.ceil(min(imgshape) / 512.0))
			apImod.createAlignedStack(stackdir, aligndir, imodseriesname,bin)
			if alignmethod == 'protomo' or alignmethod == 'leginon':
				alifilename = imodseriesname+'-%02d.ali' % cycle
				os.rename(imodseriesname+'.ali',alifilename)
			else:
				alifilename = imodseriesname+'.ali'
			alifilepath = os.path.join(aligndir,alifilename)
			# commit to database
			if commit:
				if alignmethod == 'protomo' or alignmethod == 'leginon':
					# -- Commit Parameters --
					protomodata = apProTomo.insertProtomoParams(seriesname)
					alignrun = apTomo.insertTomoAlignmentRun(sessiondata,leginonxcorrlist[0],None,protomodata,None,1,self.params['runname'],self.params['rundir'],self.params['description'])
					self.cycle_description = self.params['description']
					self.params['cycle'] = cycle
					if cycle == 0:
						# temporarily change aligner description on the initial 0 cycle
						self.params['description'] = 'leginon correlation results'
					# insert sample and window size and all the other user defined params into ApProtomoRefinementParamsData
					alignerdata = apProTomo.insertAlignIteration(alignrun, protomodata, self.params, refineparamdict,ordered_imagelist[refimg])
					# -- Commit Results --
					resulttltfile = os.path.join(aligndir,seriesname+'-%02d-fitted.tlt' % (cycle,))
					resulttltparams = apProTomo.parseTilt(resulttltfile)
					if resulttltparams:
						# commit the geometry parameters (psi, theta, phi, azimuth)
						modeldata = apProTomo.insertModel(alignerdata, resulttltparams)
						# insert results into ApProtomoAlignmentData (also used by imod) for each image
						for i,imagedata in enumerate(ordered_imagelist):
							apProTomo.insertTiltAlignment(alignerdata,imagedata,i,resulttltparams[0][i],center)
					self.params['description'] = self.cycle_description
				else:
					alignrun = apTomo.insertTomoAlignmentRun(sessiondata,None,imodxcorrdata,None,None,1,self.params['runname'],self.params['rundir'],self.params['description'])
					alignerdata = apTomo.insertAlignerParams(alignrun,self.params)
					#results
					prexgfile = os.path.join(aligndir,imodseriesname+'.prexg')
					shifts = apImod.readShiftPrexgFile(aligndir, imodseriesname)
					resulttltparams = apProTomo.convertShiftsToParams(tilts,shifts,center,default_azimuth)
					if resulttltparams:
						modeldata = apProTomo.insertModel(alignerdata, resulttltparams)
						for i,imagedata in enumerate(ordered_imagelist):
							apProTomo.insertTiltAlignment(alignerdata,imagedata,i,resulttltparams[0][i],center)
				# multiple tilt series in one alignrun
				for i in range(0,len(tiltdatalist)):
					if i == 0:
						primary = True
					else:
						primary = False
					apTomo.insertTiltsInAlignRun(alignrun, tiltdatalist[i],leginonxcorrlist[i],primary)
			apTomo.makeAlignStackMovie(alifilepath)
			os.chdir(processdir)
	
		###create tilt file

		#get image size from the first image
<<<<<<< .mine
		imagesizex = tiltdata[0]['image'].shape[1]
		imagesizey = tiltdata[0]['image'].shape[0]
		
=======
		imagesizex = tiltdata[0]['image'].shape[0]
		imagesizey = tiltdata[0]['image'].shape[1]

>>>>>>> .r18846
		#shift half tilt series relative to eachother
		#SS I'm arbitrarily making the bin parameter here 1 because it's not necessary to sample at this point
		shifts = apTomo.getGlobalShift(ordered_imagelist, 1, refimg)
		
		#OPTION: refinement might be more robust by doing one round of IMOD aligment to prealign images before doing protomo refine
		origins = apProTomo2Prep.convertShiftsToOrigin(shifts, imagesizex, imagesizey)

		#determine azimuth
<<<<<<< .mine
		azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
		apProTomo2Prep.writeTiltFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
=======
		azimuth = apTomo.getAverageAzimuthFromSeries(ordered_imagelist)
		if self.params['azimuth'] is not None:
			azimuth = self.params['azimuth']
		apProTomo2Prep.writeTileFile2(tiltfilename, seriesname, newfilenames, origins, tilts, azimuth, refimg)
>>>>>>> .r18846