예제 #1
0
def extractCandidates(jobFilename='', resultFilename='', orientFilename='', sizeParticle=None, maxNumParticle=0,
                      minScore=-1, write2disk=0, margin=None,mask=None, structuredMask=None):
    # construct the original job from the xml file
    if jobFilename=='':
        jobFilename='JobInfo.xml'
        
    from pytom.localization.peak_job import PeakJob
    job = PeakJob()
    job.fromXMLFile(jobFilename)
    
    from pytom.localization.extraction_peak_result import ExPeakResult
    res = ExPeakResult()
    res.volFilename = job.volume.getFilename()


    from pytom.basic.files import read
    from pytom_numpy import vol2npy
    from copy import deepcopy


    if resultFilename=='':
        res.resultFilename='scores.em'
    else:
        res.resultFilename = resultFilename
    if orientFilename=='':
        res.orientFilename='angles.em'
    else:
        res.orientFilename = orientFilename

    if mask:
        resultfile, maskfile = read(res.resultFilename), read(mask)
        resultdata, maskdata = deepcopy(vol2npy(resultfile)), deepcopy(vol2npy(maskfile))

        import mrcfile
        x,y,z,sx,sy,sz = job.volume.subregion

        masked_data = (resultdata*maskdata[x:x+sx,y:y+sy,z:z+sz])
        #masked_data[masked_data < 0.00001] = resultdata.median()

        mrcfile.new(res.resultFilename.replace('.em', '_masked.em'),masked_data.T, overwrite=True)
        res.resultFilename = res.resultFilename.replace('.em', '_masked.em')


    res.angleList = job.rotations[:]
    res.score = job.score.__class__
    
    if maxNumParticle <= 0:
        return None
    
    res.readAll()
    
    if sizeParticle==None:
        ref = job.reference.getVolume()
        sizeParticle = [ref.sizeX(),ref.sizeY(),ref.sizeZ()]
    print(job.volume.subregion[:3])
    particleList = res.findParticles(sizeParticle,maxNumParticle,minScore,write2disk,margin, offset=job.volume.subregion[:3], structured_mask=structuredMask)
    
    return particleList
예제 #2
0
    def test_read(self):
        """
        test general read function
        """
        from pytom.basic.files import read

        v = read('./testData/ribo.em')
        #v = read('testData/emd_1480.map.em_bin_4.em')
        self.assertTrue(v.sizeX() == 100 and v.sizeY() == 100
                        and v.sizeZ() == 100)

        v = read('./testData/ribo.em', binning=[4, 4, 4])
        assert v.sizeX() == 25 and v.sizeY() == 25 and v.sizeZ() == 25
예제 #3
0
파일: mrc2em.py 프로젝트: xmzzaa/PyTom
def mrc2em(filename,destination):
    #from pytom_volume import read
    from pytom.basic.files import read
    from pytom.tools.files import checkFileExists,checkDirExists
    import os
    if not checkFileExists(filename):
        raise RuntimeError('MRC file not found! ',filename)

    if not checkDirExists(destination):
        raise RuntimeError('Destination directory not found! ', destination)

    emfile = read(filename)

    splitName = filename.split(os.sep)
    filename = splitName[len(splitName)-1]


    newFilename = destination + os.sep + filename[0:len(filename)-4] + '.em'

    emfile.write(newFilename,'em')
예제 #4
0
def writeAlignedProjections(TiltSeries_,
                            weighting=None,
                            lowpassFilter=None,
                            binning=None,
                            verbose=False,
                            write_images=True):
    """write weighted and aligned projections to disk1

       @param TiltSeries_: Tilt Series
       @type TiltSeries_: reconstruction.TiltSeries
       @param weighting: weighting (<0: analytical weighting, >1 exact weighting (value corresponds to object diameter in pixel AFTER binning)
       @type weighting: float
       @param lowpassFilter: lowpass filter (in Nyquist)
       @type lowpassFilter: float
       @param binning: binning (default: 1 = no binning). binning=2: 2x2 pixels -> 1 pixel, binning=3: 3x3 pixels -> 1 pixel, etc.

       @author: FF
    """
    import numpy
    from pytom_numpy import vol2npy
    from pytom.basic.files import read_em, write_em
    from pytom.basic.functions import taper_edges
    from pytom.basic.transformations import general_transform2d
    from pytom.basic.fourier import ifft, fft
    from pytom.basic.filter import filter as filterFunction, bandpassFilter
    from pytom.basic.filter import circleFilter, rampFilter, exactFilter, fourierFilterShift, rotateFilter
    from pytom_volume import complexRealMult, vol
    import pytom_freqweight
    from pytom.basic.transformations import resize
    from pytom.gui.guiFunctions import fmtAR, headerAlignmentResults, datatypeAR
    import os

    if binning:
        imdim = int(float(TiltSeries_._imdim) / float(binning) + .5)
    else:
        imdim = TiltSeries_._imdim
    print('imdim', imdim)

    sliceWidth = imdim

    # pre-determine analytical weighting function and lowpass for speedup
    if (weighting != None) and (weighting < -0.001):
        w_func = fourierFilterShift(rampFilter(imdim, imdim))
    print('start weighting')
    # design lowpass filter
    if lowpassFilter:
        if lowpassFilter > 1.:
            lowpassFilter = 1.
            print("Warning: lowpassFilter > 1 - set to 1 (=Nyquist)")
        # weighting filter: arguments: (angle, cutoff radius, dimx, dimy,
        lpf = pytom_freqweight.weight(0.0, lowpassFilter * imdim / 2, imdim,
                                      imdim // 2 + 1, 1,
                                      lowpassFilter / 5. * imdim)
        #lpf = bandpassFilter(volume=vol(imdim, imdim,1),lowestFrequency=0,highestFrequency=int(lowpassFilter*imdim/2),
        #                     bpf=None,smooth=lowpassFilter/5.*imdim,fourierOnly=False)[1]

    tilt_angles = []

    for projection in TiltSeries_._ProjectionList:
        tilt_angles.append(projection._tiltAngle)
    tilt_angles = sorted(tilt_angles)
    print(tilt_angles)
    #q = numpy.matrix(abs(numpy.arange(-imdim//2, imdim//2)))

    alignmentResults = numpy.zeros((len(TiltSeries_._ProjectionList)),
                                   dtype=datatypeAR)
    alignmentResults['TiltAngle'] = tilt_angles

    for (ii, projection) in enumerate(TiltSeries_._ProjectionList):

        alignmentResults['FileName'][ii] = os.path.join(
            os.getcwd(), projection._filename)
        transX = -projection._alignmentTransX / binning
        transY = -projection._alignmentTransY / binning
        rot = -(projection._alignmentRotation + 90.)
        mag = projection._alignmentMagnification

        alignmentResults['AlignmentTransX'][ii] = transX
        alignmentResults['AlignmentTransY'][ii] = transY
        alignmentResults['InPlaneRotation'][ii] = rot
        alignmentResults['Magnification'][ii] = mag

        if write_images:
            if projection._filename.split('.')[-1] == 'st':
                from pytom.basic.files import EMHeader, read
                header = EMHeader()
                header.set_dim(x=imdim, y=imdim, z=1)
                idx = projection._index
                if verbose:
                    print("reading in projection %d" % idx)
                image = read(file=projection._filename,
                             subregion=[
                                 0, 0, idx - 1, TiltSeries_._imdim,
                                 TiltSeries_._imdim, 1
                             ],
                             sampling=[0, 0, 0],
                             binning=[0, 0, 0])
                if not (binning == 1) or (binning == None):
                    image = resize(volume=image, factor=1 / float(binning))[0]
            else:
                # read projection files
                from pytom.basic.files import EMHeader, read, read_em_header
                print(projection._filename)
                image = read(projection._filename)
                image = resize(volume=image, factor=1 / float(binning))[0]

                if projection._filename[-3:] == '.em':
                    header = read_em_header(projection._filename)
                else:
                    header = EMHeader()
                    header.set_dim(x=imdim, y=imdim, z=1)

            if lowpassFilter:
                filtered = filterFunction(volume=image,
                                          filterObject=lpf,
                                          fourierOnly=False)
                image = filtered[0]
            tiltAngle = projection._tiltAngle
            header.set_tiltangle(tiltAngle)
            # normalize to contrast - subtract mean and norm to mean
            immean = vol2npy(image).mean()
            image = (image - immean) / immean
            # smoothen borders to prevent high contrast oscillations
            image = taper_edges(image, imdim // 30)[0]
            # transform projection according to tilt alignment

            if projection._filename.split('.')[-1] == 'st':
                newFilename = (TiltSeries_._alignedTiltSeriesName + "_" +
                               str(projection.getIndex()) + '.em')
            else:
                TiltSeries_._tiltSeriesFormat = 'mrc'
                newFilename = (TiltSeries_._alignedTiltSeriesName + "_" +
                               str(projection.getIndex()) + '.' +
                               TiltSeries_._tiltSeriesFormat)
            if verbose:
                tline = ("%30s" % newFilename)
                tline = tline + (" (tiltAngle=%6.2f)" % tiltAngle)
                tline = tline + (": transX=%6.1f" % transX)
                tline = tline + (", transY=%6.1f" % transY)
                tline = tline + (", rot=%6.2f" % rot)
                tline = tline + (", mag=%5.4f" % mag)
                print(tline)

            image = general_transform2d(v=image,
                                        rot=rot,
                                        shift=[transX, transY],
                                        scale=mag,
                                        order=[2, 1, 0],
                                        crop=True)

            # smoothen once more to avoid edges
            image = taper_edges(image, imdim // 30)[0]

            # analytical weighting
            if (weighting != None) and (weighting < 0):
                image = (ifft(complexRealMult(fft(image), w_func)) /
                         (image.sizeX() * image.sizeY() * image.sizeZ()))

            elif (weighting != None) and (weighting > 0):
                if abs(weighting - 2) < 0.001:
                    w_func = fourierFilterShift(
                        rotateFilter(tilt_angles, tiltAngle, imdim, imdim,
                                     sliceWidth))
                else:
                    w_func = fourierFilterShift(
                        exactFilter(tilt_angles, tiltAngle, imdim, imdim,
                                    sliceWidth))

                image = (ifft(complexRealMult(fft(image), w_func)) /
                         (image.sizeX() * image.sizeY() * image.sizeZ()))
            header.set_tiltangle(tilt_angles[ii])

            if newFilename.endswith('.mrc'):
                data = copy.deepcopy(vol2npy(image))
                mrcfile.new(newFilename,
                            data.T.astype(float32),
                            overwrite=True)
            else:
                write_em(filename=newFilename, data=image, header=header)

            if verbose:
                tline = ("%30s written ..." % newFilename)

    outname = os.path.join(os.path.dirname(TiltSeries_._alignedTiltSeriesName),
                           'alignmentResults.txt')
    numpy.savetxt(outname,
                  alignmentResults,
                  fmt=fmtAR,
                  header=headerAlignmentResults)
    print('Alignment successful. See {} for results.'.format(outname))
예제 #5
0
파일: tt.py 프로젝트: xmzzaa/PyTom
from pytom.basic.correlation import nxcc
from pytom.basic.files import read


print(nxcc(read('GPU/vol_1_3.mrc'), read('GPU/vol_3_1.mrc'), read('CPU/mask.em')))
print(nxcc(read('CPU/vol_1_3.em'),  read('CPU/vol_3_1.em'),  read('CPU/mask.em')))


from pytom.tompy.correlation import nxcc
from pytom.tompy.io import read as readN


print(nxcc(readN('GPU/vol_1_3.mrc'), readN('GPU/vol_3_1.mrc'), readN('CPU/mask.em')))
print(nxcc(readN('CPU/vol_1_3.em'),  readN('CPU/vol_3_1.em'),  readN('CPU/mask.em')))
예제 #6
0
        if line.startswith('sorted') and line.endswith('.mrc')
    ]

    for mrc in a:

        a = mrcfile.open(os.path.join(backup, mrc), permissive=True)
        data = a.data[:, :]
        a.close()
        shapeF = data.shape
        size = min(shapeF)
        datao = data[shapeF[0] // 2 - size // 2:shapeF[0] // 2 + size // 2,
                     shapeF[1] // 2 - size // 2:shapeF[1] // 2 + size // 2]

        mrcfile.new(os.path.join(folder, mrc), datao, overwrite=True)

    if os.path.exists('{}/markerfile.em'.format(backup)):
        from pytom.basic.files import read, write_em
        from pytom_numpy import vol2npy, npy2vol
        import copy

        volume = read('{}/markerfile.em'.format(backup))

        marker = copy.deepcopy(vol2npy(volume)).T

        marker[:, :, 1] -= max(64, abs(shapeF[0] - shapeF[1]) // 2)
        marker[:, :, 1][marker[:, :, 1] < -1] = -1

        markerVolume = npy2vol(array(marker.T, dtype='float32', order='F'), 3)

        write_em('{}/markerfile.em'.format(folder), markerVolume)
예제 #7
0
        print(helper)
        sys.exit()

    try:
        fscCriterion = float(fscCriterion)
    except TypeError:
        fscCriterion = 0.5

    try:
        if pixelSize:
            pixelSize = float(pixelSize)
    except ValueError:
        raise ValueError('The value for pixelsize must be a float!')

    try:
        mask = read(mask)
    except:
        mask = None

    try:
        if numberBands:
            numberBands = int(numberBands)
    except ValueError:
        raise ValueError('The value for numberBands must be a integer!')

    try:
        if randomize is None:
            randomize = 0
        else:
            randomize = float(randomize)
    except ValueError or randomize > 1 or randomize < 0: