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) tiltseriessettings= apTomo.getTomographySettings(sessiondata,tiltseriesdata) tiltdata=apTomo.getImageList([tiltseriesdata]) description = self.params['description'] apDisplay.printMsg("getting imagelist") print "ordering list" 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=self.params['seriesname'] print type(seriesname) # param_out=seriesname+'.param' ###insert protomo run params print "first insert" protomodata = apProTomo.insertProtomoParams(seriesname) print "second insert" alignrun = apTomo.insertTomoAlignmentRun(sessiondata,tiltseriessettings,None,protomodata,None,1,self.params['runname'],self.params['rundir'],self.params['description']) ###insert protomo alignment ###hack to get around need to parse protomo param file #should read imgref from tlt file refineparamdict={'alismp':None,'alibox_x':None,'alibox_y':None,'cormod':None,'imgref':None} ### self.params['goodcycle']=None if self.params['goodrange'] is None: self.params['goodstart']=1 self.params['goodend']=len(tilts) alignerdata = apProTomo.insertAlignIteration(alignrun, protomodata, self.params, refineparamdict,ordered_imagelist[refimg]) # read tlt file print "third insert" alignmentdict, geometrydict, seriesname = apProTomo.parseTilt(self.params['tltfile']) # insert geometry model modeldata = apProTomo.insertModel2(alignerdata, geometrydict) #insert image alignments for i,imagedata in enumerate(ordered_imagelist): #Caution...assumes ordered_imagelist is in same order as tlt file apProTomo.insertTiltAlignment(alignerdata,imagedata,i,alignmentdict[i+1],center=None) print "fourth insert" apTomo.insertTiltsInAlignRun(alignrun, tiltseriesdata,tiltseriessettings,True)
def commitResultsToDB(self): return # -- Commit Parameters -- protomodata = apProTomo.insertProtomoParams(self.seriesname) alignrun = apTomo.insertTomoAlignmentRun(self.sessiondata, None, None, protomodata, None, 1, self.params['runname'], self.params['rundir'], self.params['description']) cycle_description = self.params['description'] # create refinement parameter dictionary refinedict = apProTomo.createRefineDefaults(0, '', '', tmp='') refinedict['alismp'] = self.params['sample'] refinedict['alibox_x'] = self.params['windowsize_x'] refinedict['alibox_y'] = self.params['windowsize_y'] refinedict['cormod'] = self.params['correlation_mode'] refinedict['imgref'] = self.params['refimg'] alignerdata = apProTomo.insertAlignIteration( alignrun, protomodata, self.params, refinedict, self.ordered_imagelist[self.refimg]) # -- Commit Results -- # Parse the results file which holds rotation and Correction Factors correctionFactors = self.parseCorrectionFactors( self.correctionFactorFile) # commit the geometry parameters (psi, theta, phi, azimuth), not sure about this. if correctionFactors is not None: self.insertModel(alignerdata, correctionFactors) # insert results into ApProtomoAlignmentData for each image for i, imagedata in enumerate(self.ordered_imagelist): try: apProTomo.insertTiltAlignment(alignerdata, imagedata, i, correctionFactors[i], self.center) except: if i is not self.refimg: apDisplay.printWarning( "Unable to update ApProtomoAlignmentData database table for image: %d" % i) # TODO: raise exception once the refimg is figured out correctly self.params['description'] = cycle_description
def commitResultsToDB(self): return # -- Commit Parameters -- protomodata = apProTomo.insertProtomoParams(self.seriesname) alignrun = apTomo.insertTomoAlignmentRun(self.sessiondata, None, None, protomodata, None, 1, self.params['runname'],self.params['rundir'],self.params['description']) cycle_description = self.params['description'] # create refinement parameter dictionary refinedict = apProTomo.createRefineDefaults(0, '', '', tmp='') refinedict['alismp'] = self.params['sample'] refinedict['alibox_x'] = self.params['windowsize_x'] refinedict['alibox_y'] = self.params['windowsize_y'] refinedict['cormod'] = self.params['correlation_mode'] refinedict['imgref'] = self.params['refimg'] alignerdata = apProTomo.insertAlignIteration(alignrun, protomodata, self.params, refinedict, self.ordered_imagelist[self.refimg]) # -- Commit Results -- # Parse the results file which holds rotation and Correction Factors correctionFactors = self.parseCorrectionFactors( self.correctionFactorFile ) # commit the geometry parameters (psi, theta, phi, azimuth), not sure about this. if correctionFactors is not None: self.insertModel(alignerdata, correctionFactors) # insert results into ApProtomoAlignmentData for each image for i,imagedata in enumerate(self.ordered_imagelist): try: apProTomo.insertTiltAlignment(alignerdata,imagedata,i,correctionFactors[i],self.center) except: if i is not self.refimg : apDisplay.printWarning("Unable to update ApProtomoAlignmentData database table for image: %d" % i ) # TODO: raise exception once the refimg is figured out correctly self.params['description'] = cycle_description
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)
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) tiltseriessettings = apTomo.getTomographySettings( sessiondata, tiltseriesdata) tiltdata = apTomo.getImageList([tiltseriesdata]) description = self.params['description'] apDisplay.printMsg("getting imagelist") print "ordering list" 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 = self.params['seriesname'] print type(seriesname) # param_out=seriesname+'.param' ###insert protomo run params print "first insert" protomodata = apProTomo.insertProtomoParams(seriesname) print "second insert" alignrun = apTomo.insertTomoAlignmentRun(sessiondata, tiltseriessettings, None, protomodata, None, 1, self.params['runname'], self.params['rundir'], self.params['description']) ###insert protomo alignment ###hack to get around need to parse protomo param file #should read imgref from tlt file refineparamdict = { 'alismp': None, 'alibox_x': None, 'alibox_y': None, 'cormod': None, 'imgref': None } ### self.params['goodcycle'] = None if self.params['goodrange'] is None: self.params['goodstart'] = 1 self.params['goodend'] = len(tilts) alignerdata = apProTomo.insertAlignIteration(alignrun, protomodata, self.params, refineparamdict, ordered_imagelist[refimg]) # read tlt file print "third insert" alignmentdict, geometrydict, seriesname = apProTomo.parseTilt( self.params['tltfile']) # insert geometry model modeldata = apProTomo.insertModel2(alignerdata, geometrydict) #insert image alignments for i, imagedata in enumerate(ordered_imagelist): #Caution...assumes ordered_imagelist is in same order as tlt file apProTomo.insertTiltAlignment(alignerdata, imagedata, i, alignmentdict[i + 1], center=None) print "fourth insert" apTomo.insertTiltsInAlignRun(alignrun, tiltseriesdata, tiltseriessettings, True)