def refineMarkerPositions(tiltSeriesName, markerFileName, firstProj, lastProj, finealigfile, ireftilt=0, irefmark=1, cent=[1025, 1025], dimBox=32, verbose=False): """ refine coordinates of markers @param tiltSeriesName: name of tilt series (<Name>_index.em or <Name>_index.mrc) @type tiltSeriesName: L{str} @param markerFileName: name of marker file @type markerFileName: L{str} @param firstProj: index of first projection @type firstProj: L{int} @param lastProj: index of last projection @type lastProj: L{int} @param finealigfile: output file with refined marker coordinates @type finealigfile: L{str} @param dimBox: dimension of box for markers @type dimBox: L{int} @param verbose: verbose for debugging @type verbose: L{bool} @author: FF """ from pytom.reconstruction.TiltAlignmentStructures import TiltAlignmentParameters, TiltSeries, TiltAlignment #read data MyTiltAlignmentParas = TiltAlignmentParameters(dmag=False, drot=False, dbeam=False, finealig=True, finealigfile=finealigfile, grad=False, irefmark=irefmark, ireftilt=ireftilt, r=None, cent=cent, handflip=False, optimizer='leastsq', maxIter=0) MyTiltSeries = TiltSeries(tiltSeriesName=tiltSeriesName, TiltAlignmentParas=MyTiltAlignmentParas, alignedTiltSeriesName='dummy', markerFileName=markerFileName, firstProj=firstProj, lastProj=lastProj) MyTiltAlignment = TiltAlignment(MyTiltSeries) #MyTiltAlignment.computeCarseAlignment( MyTiltSeries, mute=True) MyTiltAlignment.refineMarkerPositions(TiltSeries_=MyTiltSeries, dimBox=dimBox, finealigfile=finealigfile, verbose=False)
def refineMarkerPositions( tiltSeriesName, markerFileName, firstProj, lastProj, finealigfile, dimBox=32, projIndices=None, tiltSeriesFormat='em',ret=False,write=True,ireftilt=21,size=30000): """ refine coordinates of markers """ mask = None #read data MyTiltAlignmentParas=TiltAlignmentParameters( dmag=False, drot=False, dbeam=False, finealig=True, finealigfile=finealigfile, grad=False, irefmark=1, ireftilt=ireftilt, r=None, cent= [1025,1025], handflip=False, optimizer='leastsq', maxIter=0) MyTiltSeries= TiltSeries(tiltSeriesName=tiltSeriesName, TiltAlignmentParas=MyTiltAlignmentParas, alignedTiltSeriesName='dummy', markerFileName=markerFileName, firstProj=firstProj, lastProj=lastProj,projIndices=projIndices,tiltSeriesFormat=tiltSeriesFormat) MyTiltAlignment = TiltAlignment(MyTiltSeries) MyTiltAlignment.computeCoarseAlignment( MyTiltSeries) #MyTiltAlignment.alignFromFiducials() dxdy_markers = numpy.zeros((len(projIndices),len(MyTiltSeries._Markers),2),dtype=float) missing = [] for (imark, marker) in enumerate(MyTiltSeries._Markers): markerImageStack = ImageStack(verbose=False) len_stack = 0 for itilt in range(0,len(marker._projIndices)): x = round(marker.get_xProj(itilt)) y = round(marker.get_yProj(itilt)) if (x-dimBox//2 -2 > 0.01) and ( y -dimBox//2 -2 > 0.01) and size-x > dimBox//2+1 and size-y > dimBox//2+1: proj = MyTiltSeries._ProjectionList[itilt] filename = proj.getFilename() #copy data to ImageStack xmin, ymin = x-dimBox//2-2, y-dimBox//2-2 xend, yend= min(xmin+dimBox, size), min(ymin+dimBox,size) xoff, yoff = dimBox-(xend-xmin), dimBox-(yend-ymin) markerImageStack.addImageFromFile(filename=filename, boxCoords=[max(0,xmin), max(0,ymin), 0], dims=[dimBox, dimBox, 1], shiftX=0, shiftY=0, rotation=0, appliedShiftX=0, appliedShiftY=0, index=itilt) len_stack +=1 else: proj = MyTiltSeries._ProjectionList[itilt] print("marker not clicked in "+proj.getFilename()) #markerImageStack.normalize( normtype="StdMeanInMask", mask=mask) if len_stack == 0: continue markerImageStack.bandpass( lowfreq=dimBox/32., hifreq=6.*dimBox/32., smooth=2.*dimBox/32., bpf=None) markerImageStack.normalize( normtype="StdMean") # smoothen edges markerImageStack.taper_edges( width=dimBox/8.) markerImageStack.exMaxAlign( niter=10, mask=mask) if write: dirname = os.path.dirname(tiltSeriesName) markerImageStack.writeWorkingCopies( filename="{}/Marker_{:02d}.em".format(dirname,imark) ) markerImageStack.writeAverage( filename='{}/av_{:02d}.em'.format(dirname,imark) ) # set new coordinates irun = 0 for itilt in range(0,len(marker._projIndices)): x = round(marker.get_xProj(itilt)) y = round(marker.get_yProj(itilt)) if ( x > -1 ) and ( y > -1 ): try: dx = x+markerImageStack.images[irun].shiftX dy = y+markerImageStack.images[irun].shiftY except: continue dxdy_markers[itilt][imark][0] = dy dxdy_markers[itilt][imark][1] = dx marker.set_xProj(itilt, x+ markerImageStack.images[irun].shiftX) marker.set_yProj(itilt, y+ markerImageStack.images[irun].shiftY) irun = irun + 1 #(psiindeg, shiftX, shiftY, x, y, z, distLine, diffX, diffY, # shiftVarX, shiftVarY) = MyTiltAlignment.computeCoarseAlignment( MyTiltSeries) #for i in range(len(MyTiltSeries._Markers)): errors = MyTiltAlignment.alignmentResidual() transX = MyTiltAlignment._alignmentTransY - MyTiltAlignment.TiltSeries_._TiltAlignmentParas.cent[0] transY = MyTiltAlignment._alignmentTransX - MyTiltAlignment.TiltSeries_._TiltAlignmentParas.cent[1] if ret: return dxdy_markers, errors, transX, transY
def alignWeightReconstructOld(tiltSeriesName, markerFileName, lastProj, tltfile=None, prexgfile=None, preBin=None, volumeName=None, volumeFileType='em', alignResultFile='', voldims=None, recCent=[0, 0, 0], tiltSeriesFormat='st', firstProj=1, irefmark=1, ireftilt=1, handflip=False, alignedTiltSeriesName='align/myTilt', weightingType=-1, lowpassFilter=1., projBinning=1, outMarkerFileName=None, verbose=False, outfile='', write_images=True): """ @param tiltSeriesName: Name of tilt series (set of image files in .em or .mrc format) or stack file (ending '.st').\ Note: the actual file ending should NOT be provided. @type tiltSeriesName: str @param markerFileName: name of EM markerfile or IMOD wimp File containing marker coordinates @type markerFileName: str @param lastProj: index of last projection @param tltfile: ascii file containing the tilt angles @type tltfile: str @type lastProj: int @param prexgfile: file containing pre-shifts (IMOD way of doing things) @type prexgfile: str @param preBin: pre-binning in IMOD used for marker determination @type preBin: int @param volumeName: Filename of volume @type volumeName: str @param volumeFileType: type of output volume - em or mrc @type volumeFileType: str @param voldims: dimensions of reconstruction volume, e.g. [512,512,128] - if None chosen only aligned projs are \ written @type voldims: list @param recCent: offset from center of volume - for example choose z!=0 to shift in z (post-binning coordinates) - \ default (0,0,0) @type recCent: list @param tiltSeriesFormat: file format of tilt series: 'em', 'mrc' or 'st' @type tiltSeriesFormat: str @param firstProj: index of first projection - default 1 @type firstProj: int @param irefmark: index of reference marker for alignment @type irefmark: int @param ireftilt: index of reference tilt @type ireftilt: int @param handflip: add 180 deg to default tilt axis resulting in change of handedness? @type handflip: bool @param alignedTiltSeriesName: name of ailgned tilt series @type alignedTiltSeriesName: str @param weightingType: type of weighting - -1 is analytical 'ramp' weighting and 0 is no weighting @type weightingType: int @param lowpassFilter: lowpass filter in Nyquist @type lowpassFilter: float @param projBinning: binning of projection (factor rather than power of two!) @type projBinning: int or float @param outMarkerFileName: filename of output marker file @type outMarkerFileName: str @param verbose: verbose? @type verbose: bool @author: FF """ from pytom.reconstruction.TiltAlignmentStructures import TiltSeries, TiltAlignment, TiltAlignmentParameters if not alignResultFile: if verbose: print("Function alignWeightReconstruct started") mute = False else: mute = True from pytom.reconstruction.TiltAlignmentStructures import TiltSeries, TiltAlignment, TiltAlignmentParameters tiltParas = TiltAlignmentParameters(dmag=True, drot=True, dbeam=False, finealig=True, finealigfile='xxx.txt', grad=False, irefmark=irefmark, ireftilt=ireftilt, r=None, cent=[2049, 2049], handflip=handflip, optimizer='leastsq', maxIter=1000) markerFileType = markerFileName.split('.')[-1] # align with wimpfile if not preBin: preBin = 1 if markerFileType == 'wimp': if verbose: print(" WIMP file used for alignment") tiltSeries = TiltSeries( tiltSeriesName=tiltSeriesName, TiltAlignmentParas=tiltParas, alignedTiltSeriesName=alignedTiltSeriesName, markerFileName=None, firstProj=firstProj, lastProj=lastProj, tiltSeriesFormat=tiltSeriesFormat) tiltSeries.readIMODwimp(markerFileName=markerFileName, prexgfile=prexgfile, tltfile=tltfile, preBin=preBin, verbose=False) else: if verbose: print(" EM markerfile file used for alignment") tiltSeries = TiltSeries( tiltSeriesName=tiltSeriesName, TiltAlignmentParas=tiltParas, alignedTiltSeriesName=alignedTiltSeriesName, markerFileName=markerFileName, firstProj=firstProj, lastProj=lastProj, tiltSeriesFormat=tiltSeriesFormat) if tltfile: tiltSeries.getTiltAnglesFromIMODfile(tltfile=tltfile) tiltAlignment = TiltAlignment(TiltSeries_=tiltSeries) if outMarkerFileName: tiltSeries.writeMarkerFile(markerFileName=outMarkerFileName) tiltSeries._markerFileName = outMarkerFileName tiltAlignment.resetAlignmentCenter() # overrule cent in Paras print(outfile) tiltAlignment.computeCoarseAlignment(tiltSeries, mute=mute, outfile=outfile) tiltAlignment.alignFromFiducials(mute=mute) # creating dir for aligned tilt series if default filename if alignedTiltSeriesName == 'align/myTilt': from os import mkdir try: mkdir('align') except OSError: print( " dir 'align' already exists - writing aligned files into existing dir" ) tiltSeries.write_aligned_projs(weighting=weightingType, lowpassFilter=lowpassFilter, binning=projBinning, verbose=verbose, write_images=write_images) if voldims: # overrule tiltSeriesFormat - aligned tiltseries is always a series of em files #tiltSeries._tiltSeriesFormat = 'em' vol_bp = tiltSeries.reconstructVolume( dims=voldims, reconstructionPosition=recCent, binning=1) vol_bp.write(volumeName, volumeFileType) else: print('new code') tiltSeries = TiltSeries(tiltSeriesName=tiltSeriesName) vol_bp = tiltSeries.reconstructVolume(dims=voldims, reconstructionPosition=recCent, binning=projBinning, alignResultFile=alignResultFile) vol_bp.write(volumeName, volumeFileType)
def alignWeightReconstruct(tiltSeriesName, markerFileName, lastProj, tltfile=None, prexgfile=None, preBin=None, volumeName=None, volumeFileType='em', alignResultFile='', voldims=None, recCent=[0, 0, 0], tiltSeriesFormat='st', firstProj=0, irefmark=1, ireftilt=1, handflip=False, alignedTiltSeriesName='align/myTilt', weightingType=-1, lowpassFilter=1., projBinning=1, outMarkerFileName=None, verbose=False, outfile='', write_images=True, shift_markers=True, logfile_residual='', refMarkTomo=''): """ @param tiltSeriesName: Name of tilt series (set of image files in .em or .mrc format) or stack file (ending '.st').\ Note: the actual file ending should NOT be provided. @type tiltSeriesName: str @param markerFileName: name of EM markerfile or IMOD wimp File containing marker coordinates @type markerFileName: str @param lastProj: index of last projection @param tltfile: ascii file containing the tilt angles @type tltfile: str @type lastProj: int @param prexgfile: file containing pre-shifts (IMOD way of doing things) @type prexgfile: str @param preBin: pre-binning in IMOD used for marker determination @type preBin: int @param volumeName: Filename of volume @type volumeName: str @param volumeFileType: type of output volume - em or mrc @type volumeFileType: str @param voldims: dimensions of reconstruction volume, e.g. [512,512,128] - if None chosen only aligned projs are \ written @type voldims: list @param recCent: offset from center of volume - for example choose z!=0 to shift in z (post-binning coordinates) - \ default (0,0,0) @type recCent: list @param tiltSeriesFormat: file format of tilt series: 'em', 'mrc' or 'st' @type tiltSeriesFormat: str @param firstProj: index of first projection - default 1 @type firstProj: int @param irefmark: index of reference marker for alignment @type irefmark: int @param ireftilt: index of reference tilt @type ireftilt: int @param handflip: add 180 deg to default tilt axis resulting in change of handedness? @type handflip: bool @param alignedTiltSeriesName: name of ailgned tilt series @type alignedTiltSeriesName: str @param weightingType: type of weighting - -1 is analytical 'ramp' weighting and 0 is no weighting @type weightingType: int @param lowpassFilter: lowpass filter in Nyquist @type lowpassFilter: float @param projBinning: binning of projection (factor rather than power of two!) @type projBinning: int or float @param outMarkerFileName: filename of output marker file @type outMarkerFileName: str @param verbose: verbose? @type verbose: bool @author: FF """ from pytom.reconstruction.TiltAlignmentStructures import TiltSeries, TiltAlignment, TiltAlignmentParameters from pytom.gui.guiFunctions import savestar, headerMarkerResults, fmtMR import numpy if not alignResultFile: if verbose: print("Function alignWeightReconstruct started") mute = False else: mute = True from pytom.reconstruction.TiltAlignmentStructures import TiltSeries, TiltAlignment, TiltAlignmentParameters if not shift_markers: irefmarkInit = irefmark irefmark = refMarkTomo if refMarkTomo else 0 tiltParas = TiltAlignmentParameters(dmag=True, drot=True, dbeam=False, finealig=True, finealigfile='xxx.txt', grad=False, irefmark=irefmark, ireftilt=ireftilt, r=None, cent=[2049, 2049], handflip=handflip, optimizer='leastsq', maxIter=1000) markerFileType = markerFileName.split('.')[-1] # align with wimpfile if not preBin: preBin = 1 if markerFileType == 'wimp': if verbose: print(" WIMP file used for alignment") tiltSeries = TiltSeries( tiltSeriesName=tiltSeriesName, TiltAlignmentParas=tiltParas, alignedTiltSeriesName=alignedTiltSeriesName, markerFileName=None, firstProj=firstProj, lastProj=lastProj, tiltSeriesFormat=tiltSeriesFormat) tiltSeries.readIMODwimp(markerFileName=markerFileName, prexgfile=prexgfile, tltfile=tltfile, preBin=preBin, verbose=False) else: if verbose: format = markerFileName.split('.')[-1].upper() print(f"{format} markerfile file used for alignment") tiltSeries = TiltSeries( tiltSeriesName=tiltSeriesName, TiltAlignmentParas=tiltParas, alignedTiltSeriesName=alignedTiltSeriesName, markerFileName=markerFileName, firstProj=firstProj, lastProj=lastProj, tiltSeriesFormat=tiltSeriesFormat) if tltfile: tiltSeries.getTiltAnglesFromIMODfile(tltfile=tltfile) tiltAlignment = TiltAlignment(TiltSeries_=tiltSeries) if outMarkerFileName: tiltSeries.writeMarkerFile(markerFileName=outMarkerFileName) tiltSeries._markerFileName = outMarkerFileName tiltAlignment.resetAlignmentCenter() # overrule cent in Paras tiltAlignment.computeCoarseAlignment(tiltSeries, mute=mute, optimizeShift=shift_markers) ireftilt = int( numpy.argwhere( tiltAlignment._projIndices.astype(int) == tiltSeries._TiltAlignmentParas.ireftilt)[0][0]) tiltAlignment.alignFromFiducials(mute=mute, shift_markers=True, logfile_residual=logfile_residual) if not shift_markers: tiltSeries._TiltAlignmentParas.irefmark = irefmarkInit tiltParas.irefmark = irefmarkInit for (imark, Marker) in enumerate(tiltAlignment._Markers): r = Marker.get_r() # Marker.set_r(numpy.array([r[0] - tiltSeries._ProjectionList[ireftilt]._alignmentTransX, # r[1] - tiltSeries._ProjectionList[ireftilt]._alignmentTransY, r[2]])) tiltAlignment.alignFromFiducials(mute=mute, shift_markers=False, logfile_residual=logfile_residual) values = [] if outfile: # Retrieve the index of reference image ireftilt = int( numpy.argwhere( tiltAlignment._projIndices.astype(int) == tiltSeries._TiltAlignmentParas.ireftilt)[0][0]) # for n, i in enumerate(tiltAlignment._projIndices.astype(int)): # # # print("{:3.0f} {:6.0f} {:6.0f} {:6.0f} {:6.0f}".format(tiltAlignment._tiltAngles[n], # tiltAlignment._Markers[tiltSeries._TiltAlignmentParas.irefmark].xProj[n], # tiltAlignment._Markers[tiltSeries._TiltAlignmentParas.irefmark].yProj[n], # tiltSeries._ProjectionList[int(n)]._alignmentTransX, # tiltSeries._ProjectionList[int(n)]._alignmentTransY)) # Create center point (3D) cent = tiltAlignment.TiltSeries_._TiltAlignmentParas.cent cent.append(float(tiltSeries._imdim // 2 + 1)) # Create shift shift = [ tiltSeries._ProjectionList[ireftilt]._alignmentTransX, tiltSeries._ProjectionList[ireftilt]._alignmentTransY, 0 ] # Retrieve the two relevant angles inPlaneAng = tiltAlignment._alignmentRotations[ireftilt] tiltAng = tiltAlignment._tiltAngles[ireftilt] # Retrieve the original pick position xx = tiltAlignment._Markers[ tiltSeries._TiltAlignmentParas.irefmark].xProj[ireftilt] yy = tiltAlignment._Markers[ tiltSeries._TiltAlignmentParas.irefmark].yProj[ireftilt] dx, dy = float(tiltSeries._imdimX // 2 + 1), float(tiltSeries._imdimY // 2 + 1) dz = min(dx, dy) # Create ref marker point ref = tiltAlignment._Markers[ tiltSeries._TiltAlignmentParas.irefmark].get_r() ref = rotate_vector3D(ref, [0, 0, 0], shift, inPlaneAng, tiltAng) for (imark, Marker) in enumerate(tiltAlignment._Markers): # reference marker irefmark is fixed to standard value r = rotate_vector3D(Marker.get_r(), [0, 0, 0], shift, inPlaneAng, tiltAng) values.append([ imark, r[0] - ref[0], r[1] - ref[1], r[2] - ref[2], r[0] + dx, r[1] + dy, r[2] + dz ]) savestar(outfile, numpy.array(values), header=headerMarkerResults, fmt=fmtMR) # creating dir for aligned tilt series if default filename if alignedTiltSeriesName == 'align/myTilt': from os import mkdir try: mkdir('align') except OSError: print( " dir 'align' already exists - writing aligned files into existing dir" ) tiltSeries.write_aligned_projs(weighting=weightingType, lowpassFilter=lowpassFilter, binning=projBinning, verbose=verbose, write_images=write_images) if voldims: # overrule tiltSeriesFormat - aligned tiltseries is always a series of em files #tiltSeries._tiltSeriesFormat = 'em' vol_bp = tiltSeries.reconstructVolume( dims=voldims, reconstructionPosition=recCent, binning=1) vol_bp.write(volumeName, volumeFileType) else: print('new code') tiltSeries = TiltSeries(tiltSeriesName=tiltSeriesName) vol_bp = tiltSeries.reconstructVolume(dims=voldims, reconstructionPosition=recCent, binning=projBinning, alignResultFile=alignResultFile) vol_bp.write(volumeName, volumeFileType)
reconstructionPosition=[0,0,0] # offset from center of volume - for example choose z!=0 to shift in z (post-binning coordinates) volumeName="./testvol.em" # final tomogram filetype='em' # filetype of final tomogram - can be 'em' or 'mrc' #################################################################### ######## do not edit unless you know what your are doing ########### #################################################################### MyTiltAlignmentParas=TiltAlignmentParameters( dmag=True, drot=True, dbeam=False, finealig=True, finealigfile='xxx.dat', grad=False, irefmark=irefmark, ireftilt=ireftilt, r=None, cent= [1025,1025], handflip=handflip, optimizer='leastsq', maxIter=0) MyTiltSeries= TiltSeries(tiltSeriesName=tiltSeriesName, TiltAlignmentParas=MyTiltAlignmentParas, alignedTiltSeriesName=alignedTiltSeriesName, markerFileName=markerFileName, firstProj=firstProj, lastProj=lastProj) MyTiltAlignment = TiltAlignment(MyTiltSeries) MyTiltAlignment.computeCoarseAlignment( MyTiltSeries) MyTiltAlignment.alignFromFiducials() MyTiltSeries.write_aligned_projs( weighting=-1, lowpassFilter=lowpassFilter, binning=projBinning) if not onlyWeightedProjections: vol_bp = MyTiltSeries.reconstructVolume( dims=voldims, reconstructionPosition=reconstructionPosition, binning=1) vol_bp.write(volumeName, filetype)