Exemplo n.º 1
0
def split_matrix(parId, iterator):
        
    x0 = -1
    y0 = -1
    z0 = -1
    t0 =  0 
    for x in iterator:
        x0 = x[1][0]
        y0 = x[1][1]
        z0 = x[1][2]
        t0 += 1
    
    
    vol10 = npy2vol(BC_vol1.value, 3)
    vol20 = npy2vol(BC_vol2.value, 3)
    boxhalf_x1 = BC_sizeX.value / 2 
    boxhalf_y1 = BC_sizeY.value / 2 
    boxhalf_z1 = BC_sizeZ.value / 2 
    vol1_sub = subvolume(vol10, x0 - boxhalf_x1, y0 - boxhalf_y1, z0 - boxhalf_z1, BC_sizeX.value, BC_sizeY.value, BC_sizeZ.value)
    vol2_sub = subvolume(vol20, x0 - boxhalf_x1, y0 - boxhalf_y1, z0 - boxhalf_z1, BC_sizeX.value, BC_sizeY.value, BC_sizeZ.value)
    
    
    avg1 = mean(vol1_sub)
    vol1_sub1 = hanning_taper(vol1_sub, avg1)
    avg2 = mean(vol2_sub)
    vol2_sub1 = hanning_taper(vol2_sub, avg2)


    res = fsc2_v(vol1_sub1, vol2_sub1, BC_fsc_criterion.value, BC_max_resolution.value, BC_pixelSize.value, x0, y0, z0)
    tup = (round(res,6), x0, y0, z0)
     
    del avg1, avg2, vol1_sub1, vol2_sub1
    del vol1_sub, vol2_sub, res, vol10, vol20, boxhalf_x1, boxhalf_y1, boxhalf_z1, x0, y0, z0, t0
    
    return tup
Exemplo n.º 2
0
def n2v(data):
    """Transfer Numpy array into a Pytom volume.
    Note the data will be shared between Numpy and Pytom!

    @param data: data to convert.
    """
    try:
        from pytom_volume import vol
        from pytom_numpy import npy2vol
    except:
        raise ImportError("Pytom library is not installed or set properly!")

    if data.dtype != np.dtype("float32"):
        data = np.array(data, dtype="float32")

    if len(data.shape) == 3:
        if np.isfortran(data):
            # Fortran order
            v = npy2vol(data, 3)
        else:
            vv = np.asfortranarray(data)
            v = npy2vol(vv, 3)
    elif len(data.shape) == 2:
        if np.isfortran(data):
            v = npy2vol(data, 2)
        else:
            vv = np.asfortranarray(data)
            v = npy2vol(vv, 2)
    else:
        raise Exception("Data shape invalid!")

    return v
Exemplo n.º 3
0
def project(v, rot, verbose=False):
    """
    rotate and subsequently project volume along z
    @param v: volume
    @type v: L{pytom_volume.vol}
    @param rot: rotation - either Rotation object or single angle interpreted as rotation along y-axis
    @type rot: L{pytom.basic.structures.Rotation} or float
    @return: projection (2D image)
    @rtype: L{pytom_volume.vol}
    @author: FF
    """
    from pytom_volume import vol
    from pytom_numpy import vol2npy, npy2vol
    from numpy import ndarray, float32
    from pytom.basic.transformations import general_transform_crop
    from pytom.basic.structures import Rotation
    from numpy import sum as proj

    if not isinstance(v, vol):
        raise TypeError('project: v must be of type pytom_volume.vol! Got ' + str(v.__class__) + ' instead!')
    if isinstance(rot, float):
        rot = Rotation(z1=90., z2=270., x=rot, paradigm='ZXZ')
    if not isinstance(rot, Rotation):
        raise TypeError('project: rot must be of type Rotation or float! Got ' + str(v.__class__) + ' instead!')
    if verbose:
        print("project: Rotation for projection: "+str(rot))
    rotvol = general_transform_crop(v=v, rot=rot, shift=None, scale=None, order=[0, 1, 2])
    # slightly awkward: projection in numpy ...
    npvol = vol2npy(rotvol)
    npprojection = ndarray([v.sizeX(), v.sizeY(), 1], dtype=float32, buffer=None, offset=0, strides=npvol.strides,
                           order='F')
    proj(npvol, axis=2, dtype=float32, out=npprojection)
    projection = npy2vol(npprojection,3)
    return projection
Exemplo n.º 4
0
def gaussian_filter(vol, sigma):
    """
    @param vol: input volume
    @type vol: L{pytom_volume.vol}
    @param sigma: xxx
    @type sigma: xxx
    @return: resulting volume
    @rtype: L{pytom_volume.vol}
    """
#    # construct the Gaussian kernel
#    import numpy as np
#    from scipy import mgrid, exp
#    sizex = vol.sizeX()
#    sizey = vol.sizeY()
#    sizez = vol.sizeZ()
#    
#    [x, y, z] = mgrid[-(sizex/2):(sizex+1)/2, -(sizey/2):(sizey+1)/2, -(sizez/2):(sizez+1)/2]
#    g = exp(-(x**2+y**2+z**2)/(2*float(sigma)**2))
#    g /= g.sum()
#    
#    # transfer to vol obj and do the filtering
#    from pytom_numpy import npy2vol
#    kernel = npy2vol(np.array(g, dtype='float32', order='F'), 3)
#    
#    from pytom.basic.fourier import convolute
#    res = convolute(vol, kernel, False)

    import numpy as np
    from pytom_numpy import vol2npy, npy2vol
    from scipy.ndimage.filters import gaussian_filter
    v = vol2npy(vol)
    r = gaussian_filter(v, sigma)
    res = npy2vol(np.array(r, dtype='float32', order='F'), 3)
    
    return res
Exemplo n.º 5
0
def create_bfactor_vol(size, ps, bfactor, FSC=None, apply_range=None):
    """Create a B-factor volume in Frequency space.
    @param size: The size of the volume, assuming it is a cube
    @param ps: The pixel size in angstrom
    @param bfactor: B factor
    @param FSC: Fourier Shell Correlation
    @param apply_range: The apply range (also in angstrom) of the B factor correction
    """
    if FSC is None:
        FSC = np.ones(size / 2)

    # transfer the pixel size to angstrom
    x = (ps * size) / np.arange(1, size / 2 + 1, 1)  # starts from 1!
    if apply_range is None:
        apply_range_pixel = [1, size / 2]  # starts from 1!
    else:
        assert apply_range[0] > apply_range[1]
        apply_range_pixel = [
            size * ps / apply_range[0], size * ps / apply_range[1]
        ]

    # create the FSC weight
    FSC_weight = np.sqrt((2 * np.array(FSC)) / (1 + np.array(FSC)))

    # calculate the decay function
    decay = FSC_weight * np.exp(-bfactor / (np.power(x, 2) * 4))

    # make the decay volume
    v = sph2cart(decay, size)

    # transfer to the volume format and multiple with the mask
    from pytom_volume import vol, initSphere
    from pytom_numpy import npy2vol
    vv = npy2vol(np.array(v, dtype='float32', order='F'), 3)

    if apply_range_pixel[0] == 1:
        mask = vol(size, size, size)
        initSphere(mask, apply_range_pixel[1] - 1, 0, 0, size / 2, size / 2,
                   size / 2)  # minus 1 to make it consistent with python
    else:
        mask1 = vol(size, size, size)
        mask2 = vol(size, size, size)
        initSphere(mask1, apply_range_pixel[0] - 1, 0, 0, size / 2, size / 2,
                   size / 2)
        initSphere(mask2, apply_range_pixel[1] - 1, 0, 0, size / 2, size / 2,
                   size / 2)
        mask = mask2 - mask1

    return vv * mask
Exemplo n.º 6
0
    def numpy_T(self, v=None):
        import pytom_numpy

        if not v:
            from pytom_volume import vol, gaussianNoise
            v = vol(10, 10, 10)
            gaussianNoise(v, 0, 1)

        nv = pytom_numpy.vol2npy(v)
        #print 'hier'
        #print nv.shape,nv.strides,nv.dtype
        #print v.strideX(),v.strideY(),v.strideZ()
        vv = pytom_numpy.npy2vol(nv, 3)

        self.assertTrue(v.equalsTo(vv), msg='numpy issue :(')
Exemplo n.º 7
0
def alignImagesUsingAlignmentResultFile(alignmentResultsFile,
                                        weighting=None,
                                        lowpassFilter=0.9,
                                        binning=1,
                                        circleFilter=False):
    import os
    from pytom.basic.files import read as readCVol
    from pytom_numpy import vol2npy, npy2vol
    from pytom.gui.guiFunctions import fmtAR, headerAlignmentResults, datatype, datatypeAR, loadstar
    from pytom.reconstruction.reconstructionStructures import Projection, ProjectionList
    from pytom.tompy.io import read, write, read_size
    from pytom.tompy.tools import taper_edges, create_circle
    from pytom.tompy.filter import circle_filter, ramp_filter, exact_filter
    import pytom.voltools as vt
    from pytom.gpu.initialize import xp, device
    print("Create aligned images from alignResults.txt")

    alignmentResults = loadstar(alignmentResultsFile, dtype=datatypeAR)
    imageList = alignmentResults['FileName']
    tilt_angles = alignmentResults['TiltAngle']

    imdim = read_size(imageList[0], 'x')

    if binning > 1:
        imdim = int(float(imdim) / float(binning) + .5)
    else:
        imdim = imdim

    sliceWidth = imdim

    if (weighting != None) and (float(weighting) < -0.001):
        weightSlice = xp.fft.fftshift(ramp_filter(imdim, imdim))

    if circleFilter:
        circleFilterRadius = imdim // 2
        circleSlice = xp.fft.fftshift(
            circle_filter(imdim, imdim, circleFilterRadius))
    else:
        circleSlice = xp.ones((imdim, imdim))

    # design lowpass filter
    if lowpassFilter:
        if lowpassFilter > 1.:
            lowpassFilter = 1.
            print("Warning: lowpassFilter > 1 - set to 1 (=Nyquist)")

        # weighting filter: arguments: (()dimx, dimy), cutoff radius, sigma
        lpf = xp.fft.fftshift(
            create_circle((imdim, imdim),
                          lowpassFilter * (imdim // 2),
                          sigma=0.4 * lowpassFilter * (imdim // 2)))

    projectionList = ProjectionList()
    for n, image in enumerate(imageList):
        atx = alignmentResults['AlignmentTransX'][n] / binning
        aty = alignmentResults['AlignmentTransY'][n] / binning
        rot = alignmentResults['InPlaneRotation'][n]
        mag = 1 / alignmentResults['Magnification'][n]
        projection = Projection(imageList[n],
                                tiltAngle=tilt_angles[n],
                                alignmentTransX=atx,
                                alignmentTransY=aty,
                                alignmentRotation=rot,
                                alignmentMagnification=mag)
        projectionList.append(projection)

    stack = xp.zeros((imdim, imdim, len(imageList)), dtype=xp.float32)
    phiStack = xp.zeros((1, 1, len(imageList)), dtype=xp.float32)
    thetaStack = xp.zeros((1, 1, len(imageList)), dtype=xp.float32)
    offsetStack = xp.zeros((1, 2, len(imageList)), dtype=xp.float32)

    for (ii, projection) in enumerate(projectionList):
        print(f'Align {projection._filename}')
        image = read(str(projection._filename))[::binning, ::binning].squeeze()

        if lowpassFilter:
            image = xp.abs((xp.fft.ifftn(xp.fft.fftn(image) * lpf)))

        tiltAngle = projection._tiltAngle

        # normalize to contrast - subtract mean and norm to mean
        immean = 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
        transX = projection._alignmentTransX / binning
        transY = projection._alignmentTransY / binning
        rot = float(projection._alignmentRotation)
        mag = float(projection._alignmentMagnification)

        inputImage = xp.expand_dims(image, 2).copy()
        outputImage = xp.zeros_like(inputImage, dtype=xp.float32)

        vt.transform(inputImage.astype(xp.float32),
                     rotation=[0, 0, rot],
                     rotation_order='rxyz',
                     output=outputImage,
                     device=device,
                     translation=[transX, transY, 0],
                     scale=[mag, mag, 1],
                     interpolation='filt_bspline')

        image = outputImage.squeeze()

        # 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()))
            image = xp.fft.ifftn(
                xp.fft.fftn(image) * weightSlice * circleSlice)

        elif (weighting != None) and (weighting > 0):
            weightSlice = xp.fft.fftshift(
                exact_filter(tilt_angles, tiltAngle, imdim, imdim, sliceWidth))
            image = xp.fft.ifftn(
                xp.fft.fftn(image) * weightSlice * circleSlice)

        thetaStack[0, 0, ii] = int(round(projection.getTiltAngle()))
        offsetStack[0, :, ii] = xp.array([
            int(round(projection.getOffsetX())),
            int(round(projection.getOffsetY()))
        ])

        stack[:, :, ii] = image

    arrays = []

    for fname, arr in (('stack.mrc', stack), ('offsetStack.mrc', offsetStack),
                       ('thetaStack.mrc', thetaStack), ('phiStack.mrc',
                                                        phiStack)):
        if 'gpu' in device:
            arr = arr.get()
        import numpy as np
        res = npy2vol(np.array(arr, dtype='float32', order='F'), 3)
        arrays.append(res)

    #
    #     write('stack.mrc', stack)
    #     stack = readCVol('stack.mrc')
    # write('offsetstack.mrc', offsetStack)
    # offsetStack = readCVol('offsetstack.mrc')
    # write('thetastack.mrc', thetaStack)
    # thetaStack = readCVol('thetastack.mrc')
    # write('phistack.mrc', phiStack)
    # phiStack = readCVol('phistack.mrc')
    #
    # os.remove('stack.mrc')
    # os.remove('offsetstack.mrc')
    # os.remove('thetastack.mrc')
    # os.remove('psistack.mrc')

    return arrays
Exemplo n.º 8
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)