def  filter_volume(_log
                   , FourierMaxFrequencyOfInterest
                   , ReconstructedVolume
                   , ReconstructedFilteredVolume
                   , DoComputeResolution
                   , OuterRadius
                   , DoLowPassFilter
                   , UseFscForFilter
                   , ConstantToAddToFiltration
                   , ResolutionXmdPrevIterMax
                   , ResolSam
                   ):

    if (not DoLowPassFilter):
        copyFile(_log, ReconstructedVolume, ReconstructedFilteredVolume)
    else:   
        if (UseFscForFilter):
           if (FourierMaxFrequencyOfInterest == -1):
               md = MetaData(ResolutionXmdPrevIterMax)
               id = md.firstObject()
               FourierMaxFrequencyOfInterest = md.getValue(MDL_RESOLUTION_FREQREAL, id)
               FourierMaxFrequencyOfInterest = ResolSam / FourierMaxFrequencyOfInterest
            
           filter_in_pixels_at = float(FourierMaxFrequencyOfInterest) + float(ConstantToAddToFiltration)
        else:
           filter_in_pixels_at = float(ConstantToAddToFiltration)

        if (filter_in_pixels_at > 0.5):
           copyFile(_log, ReconstructedVolume, ReconstructedFilteredVolume)
        else:
           command = " -i " + ReconstructedVolume + \
                     " -o " + ReconstructedFilteredVolume + ' --fourier low_pass ' + \
                     str (filter_in_pixels_at)
           runJob(_log, "xmipp_transform_filter", command)
def getBestVolumes(log,WorkingDir,NRansac,NumVolumes,UseAll):
    volumes = []
    inliers = []
    
    for n in range(NRansac):
        fnAngles = os.path.join(WorkingDir,"tmp/angles_ransac%05d"%n+".xmd")
        md=MetaData("inliers@"+fnAngles)
        numInliers=md.getValue(MDL_WEIGHT,md.firstObject())
        volumes.append(fnAngles)
        inliers.append(numInliers)
    
    index = sorted(range(inliers.__len__()), key=lambda k: inliers[k])
    fnBestAngles = ''
    threshold=getCCThreshold(WorkingDir)
 
    i=NRansac-1
    indx = 0
    while i>=0 and indx<NumVolumes:
        fnBestAngles = volumes[index[i]]
        fnBestAnglesOut=os.path.join(WorkingDir,"volumeProposed%05d"%indx+".xmd")
        copyFile(log,fnBestAngles,fnBestAnglesOut)
        print("Best volume "+str(indx)+" = "+fnBestAngles)
        if not UseAll:
            runJob(log,"xmipp_metadata_utilities","-i %s -o %s --query select \"maxCC>%f \" --mode append" %(fnBestAnglesOut,fnBestAnglesOut,threshold))
            if getMdSize(fnBestAnglesOut) > 0:
                indx += 1
        else:
            indx += 1
        i -= 1
        
    # Remove unnecessary files
    for n in range(NRansac):
        fnAngles = os.path.join(WorkingDir,"tmp/angles_ransac%05d"%n+".xmd")
        deleteFile(log, fnAngles)
def executeMask(_log,
                  DoMask,
                  DoSphericalMask,
                  maskedFileName,
                  maskRadius,
                  ReconstructedFilteredVolume,
                  userSuppliedMask
                  ):
    _log.debug("executeMask")
    print "executeMask", maskRadius
    if DoMask:
        command = ' -i ' + ReconstructedFilteredVolume + \
                  ' -o ' + maskedFileName
        if DoSphericalMask:
            command += ' --mask circular -' + str(maskRadius)
        else:
            command += ' --mask binary_file ' + userSuppliedMask
        runJob(_log, "xmipp_transform_mask", command)
    else:
        copyFile(_log, ReconstructedFilteredVolume, maskedFileName)
Exemplo n.º 4
0
def gatherResults(log, 
                  WorkingDir,
                  extraDir,
                  currProtMicrographClass,
                  currProtMicrograph,
                  currProtMicroscope,
                  currProtAcquisitionInfo,
                  prevProtMicrograph,
                  prevProtMicroscope,
                  prevProtAcquisitionInfo
                  ):
    
    import glob
    #fnList=glob.glob(os.path.join(extraDir,'*_aligned.spi'))
    copyFile(log,prevProtMicrograph,currProtMicrograph) 
    copyFile(log,prevProtMicroscope,currProtMicroscope) 
    copyFile(log,prevProtAcquisitionInfo,currProtAcquisitionInfo) 
    md=xmipp.MetaData(currProtMicrograph)
    for id  in md:
        inputMovie = md.getValue(xmipp.MDL_MICROGRAPH, id)
        movieNameList = os.path.basename(inputMovie)
        movieName = os.path.splitext(movieNameList)[0]
        file = _getFilename('movieAverage',movieDir=extraDir,baseName=movieName) 
        md.setValue(xmipp.MDL_MICROGRAPH,file,id)
        if exists(file):
            enabled = 1
        else:
            enabled = -1
        md.setValue(xmipp.MDL_ENABLED,enabled)

    md.sort(xmipp.MDL_MICROGRAPH)
    md.write(currProtMicrographClass,xmipp.MD_APPEND)
def angular_project_library(_log
                                , AngSamplingRateDeg
                                , BlockWithAllExpImages
                                , ConstantToAddToFiltration
                                , CtfGroupSubsetFileName
                                , DoCtfCorrection
                                , DocFileInputAngles
                                , DoParallel
                                , DoRestricSearchbyTiltAngle
                                , FourierMaxFrequencyOfInterest
                                , KernelAngularProjection
                                , MaxChangeInAngles
                                , maskedFileNamesIter
                                , MpiJobSize
                                , NumberOfMpi
                                , NumberOfThreads
                                , OnlyWinner
                                , PaddingAngularProjection
                                , PerturbProjectionDirections
                                , ProjectLibraryRootName
                                , ProjectionMethod
                                , ResolSam
                                , ResolutionXmdPrevIterMax
                                , SymmetryGroup
                                , SymmetryGroupNeighbourhood
                                , Tilt0
                                , TiltF):
    _log.debug("execute_projection_matching")
    ###need one block per reference
    # Project all references
    print '* Create projection library'
    (Xdim, Ydim, Zdim, Ndim, _) = MetaDataInfo(maskedFileNamesIter)
    memoryUsed=(Xdim*Xdim*Xdim*8.0)/pow(2,20)
    parameters = ' -i ' + maskedFileNamesIter + \
              ' --experimental_images ' + BlockWithAllExpImages + '@' + DocFileInputAngles + \
              ' -o ' + ProjectLibraryRootName + \
              ' --sampling_rate ' + AngSamplingRateDeg + \
              ' --sym ' + SymmetryGroup + 'h' + \
              ' --compute_neighbors' + \
              ' --method ' + ProjectionMethod 
    if ProjectionMethod == 'fourier':
        memoryUsed=memoryUsed*6
        if FourierMaxFrequencyOfInterest == -1:
                md = MetaData(ResolutionXmdPrevIterMax)
                id = md.firstObject()
                FourierMaxFrequencyOfInterest = md.getValue(MDL_RESOLUTION_FREQREAL, id)
                FourierMaxFrequencyOfInterest = ResolSam / FourierMaxFrequencyOfInterest + float(ConstantToAddToFiltration)
                if FourierMaxFrequencyOfInterest > 0.5:
                    FourierMaxFrequencyOfInterest = 0.5
                elif FourierMaxFrequencyOfInterest < 0.:
                    FourierMaxFrequencyOfInterest = 0.001

        parameters += " " + str(PaddingAngularProjection)
        parameters += " " + str(FourierMaxFrequencyOfInterest)
        parameters += " " + str(KernelAngularProjection)

    if (string.atof(MaxChangeInAngles) < 181.):
        parameters += \
              ' --near_exp_data --angular_distance ' + str(MaxChangeInAngles)
    else:
        parameters += \
              ' --angular_distance -1'

    if (PerturbProjectionDirections):
        perturb = math.sin(math.radians(float(AngSamplingRateDeg))) / 4.
        parameters += \
           ' --perturb ' + str(perturb)

    if (DoRestricSearchbyTiltAngle):
        parameters += \
              ' --min_tilt_angle ' + str(Tilt0) + \
              ' --max_tilt_angle ' + str(TiltF)

    if (DoCtfCorrection):
        parameters += \
              ' --groups ' + CtfGroupSubsetFileName
    processorsToUse=NumberOfMpi * NumberOfThreads
    if processorsToUse>1:
        memoryAvailable=getMemoryAvailable()
        processorsToUse=min(processorsToUse,floor(memoryAvailable/memoryUsed))
    if (DoParallel and processorsToUse>1):
        parameters = parameters + ' --mpi_job_size ' + str(MpiJobSize)
    if (len(SymmetryGroupNeighbourhood) > 1):
        parameters += \
          ' --sym_neigh ' + SymmetryGroupNeighbourhood + 'h'
    if (OnlyWinner):
        parameters += \
              ' --only_winner '

    runJob(_log, 'xmipp_angular_project_library',
                         parameters,
                         processorsToUse)
    if (not DoCtfCorrection):
        src = ProjectLibraryRootName.replace(".stk", '_sampling.xmd')
        dst = src.replace('sampling.xmd', 'group%06d_sampling.xmd' % 1)
        copyFile(_log, src, dst)
def projection_matching(_log
                            , AvailableMemory
                            , CtfGroupRootName
                            , CtfGroupDirectory
                            , DocFileInputAngles
                            , DoComputeResolution
                            , DoCtfCorrection
                            , DoScale
                            , DoParallel
                            , InnerRadius
                            , MaxChangeOffset
                            , MpiJobSize
                            , NumberOfMpi
                            , NumberOfThreads
                            , OuterRadius
                            , PaddingFactor
                            , ProjectLibraryRootName
                            , ProjMatchRootName
                            , ReferenceIsCtfCorrected
                            , ScaleStep
                            , ScaleNumberOfSteps
                            , Search5DShift
                            , Search5DStep
                            ):
    # Loop over all CTF groups
    # Use reverse order to have same order in add_to docfiles from angular_class_average
    # get all ctf groups
    _DoCtfCorrection = DoCtfCorrection
    _ProjMatchRootName = ProjMatchRootName
    refname = str(ProjectLibraryRootName)
    file_name = join(CtfGroupDirectory, CtfGroupRootName) + 'Info.xmd'
    if exists(file_name):
        auxMD = MetaData("numberGroups@" + file_name)
        NumberOfCtfGroups = auxMD.getValue(MDL_COUNT, auxMD.firstObject())
    else:
        NumberOfCtfGroups = 1

    
    CtfGroupName = CtfGroupRootName #,ictf+1,'')
    CtfGroupName = CtfGroupDirectory + '/' + CtfGroupName
    #remove output metadata
    if os.path.exists(_ProjMatchRootName):
        os.remove(_ProjMatchRootName)
    
    for ii in range(NumberOfCtfGroups):
        if NumberOfCtfGroups > 1 :
            print 'Focus Group: ', ii + 1, '/', NumberOfCtfGroups
        ictf = NumberOfCtfGroups - ii 
        
        inputdocfile = CtfBlockName + str(ictf).zfill(FILENAMENUMBERLENGTH) + '@' + DocFileInputAngles
        outputname = CtfBlockName + str(ictf).zfill(FILENAMENUMBERLENGTH) + '@' + _ProjMatchRootName
        baseTxtFile = refname[:-len('.stk')] 
        neighbFile = baseTxtFile + '_sampling.xmd'
        if (os.path.exists(neighbFile)):
            os.remove(neighbFile)
        neighbFileb = baseTxtFile + '_group' + str(ictf).zfill(FILENAMENUMBERLENGTH) + '_sampling.xmd'
        print 'neighbFileb: ', neighbFileb
        copyFile(_log, neighbFileb, neighbFile)

        parameters = ' -i ' + inputdocfile + \
                    ' -o ' + outputname + \
                    ' --ref ' + refname + \
                    ' --Ri ' + str(InnerRadius) + \
                    ' --Ro ' + str(OuterRadius) + \
                    ' --max_shift ' + str(MaxChangeOffset) + \
                    ' --search5d_shift ' + str(Search5DShift) + \
                    ' --search5d_step  ' + str(Search5DStep) + \
                    ' --mem ' + str(AvailableMemory * NumberOfThreads) + \
                    ' --thr ' + str(NumberOfThreads) + \
                    ' --append '

        
        if (DoScale):
            parameters += \
                    ' --scale ' + str(ScaleStep) + ' ' + str(ScaleNumberOfSteps) 
        
        if (_DoCtfCorrection and ReferenceIsCtfCorrected):
            ctffile = str(ictf).zfill(FILENAMENUMBERLENGTH) + '@' + CtfGroupName + '_ctf.stk'
            parameters += \
                      ' --pad ' + str(PaddingFactor) + \
                      ' --ctf ' + ctffile
        
        if (DoParallel):
            parameters = parameters + ' --mpi_job_size ' + str(MpiJobSize)
        
        runJob(_log, 'xmipp_angular_projection_matching',
                            parameters,
                            NumberOfMpi,
                            NumberOfThreads
                            )