Exemplo n.º 1
0
    def fromFile(self, filename=None):
        from pytom.tompy.io import read
        from pytom.tools.files import checkFileExists

        filename = filename or self._filename

        if checkFileExists(filename):
            self._weight = read(filename)
        else:
            raise RuntimeError('PeakPrior: File ' + filename + ' not found')
Exemplo n.º 2
0
    def getTransformedVolume(self):
        from pytom.tompy.io import read
        from pytom.tompy.transform import translate3d, rotate3d

        v = read(self.filename)
        v2 = translate3d(v, -self.shift_x, -self.shift_y, -self.shift_z)
        v3 = rotate3d(v2, -self.rotation_psi, -self.rotation_phi,
                      -self.rotation_the)

        return v3
Exemplo n.º 3
0
def write_rotation_angles(filename, z1=0, z2=0, x=0):
    assert filename.endswith('.mrc')
    header = read_header(filename)
    header[43:46] = [z1, x, z2]
    data = read(filename)
    f = open(filename, 'wb')
    try:
        f.write(header.tostring())
        f.write(data.tostring(order='F'))  # fortran-order array
    finally:
        f.close()
Exemplo n.º 4
0
def write_tilt_angle(filename, tilt_angle):
    emfile = filename.endswith('.em') * 1
    header = read_header(filename)
    if emfile:
        header[42] = int(round(tilt_angle * 1000))
    else:
        header[43] = tilt_angle
    data = read(filename)

    f = open(filename, 'wb')
    try:
        f.write(tostring(header))
        f.write(data.tostring(order='F'))  # fortran-order array
    finally:
        f.close()
Exemplo n.º 5
0
def readSubvolumeFromFourierspaceFile(filename, sizeX, sizeY, sizeZ):
    """
    readSubvolumeFromFourierspaceFile: This function is required when data \
    (in real space) is read in binned mode and a related fourier space file
    like a wedge needs to be read alongside.
    Works only if fourier file is reduced complex without any shift applied.
    @param filename: The fourier space file name
    @param sizeX: X final size of subvolume if it was complete
    (what L{pytom.basic.structures.Wedge.returnWedgeVolume} with
    humanUnderstandable == True returns)
    @param sizeY: Y final size of subvolume if it was complete
    (what L{pytom.basic.structures.Wedge.returnWedgeVolume}
    with humanUnderstandable == True returns)
    @param sizeZ: Z final size of subvolume if it was complete
    (what L{pytom.basic.structures.Wedge.returnWedgeVolume}
    with humanUnderstandable == True returns)
    @return: A subvolume
    @author: Thomas Hrabe
    """
    from pytom.tompy.io import read
    from pytom.basic.fourier import fourierSizeOperation
    [newX, newY, newZ] = fourierSizeOperation(sizeX,
                                              sizeY,
                                              sizeZ,
                                              reducedToFull=False)

    if filename.__class__ == str:
        originalVolume = read(filename)
    elif filename.__class__ == xp.array:
        # open a backdoor for this function to take volumes, but
        # this should be rather an exception -> not fully documented
        originalVolume = filename
    else:
        raise TypeError('Filename must be a string')

    originalVolume = xp.fft.fftshift(originalVolume, axes=(0, 1))
    newVolume = originalVolume[sizeX // 2 - newX // 2:sizeX // 2 + newX // 2,
                               sizeY // 2 - newY // 2:sizeY // 2 +
                               newY // 2, :newZ]
    newVolume = xp.fft.fftshift(newVolume, axes=(0, 1))

    return newVolume
Exemplo n.º 6
0
def determineRotationCenter(particle, binning):
    """
    determineRotationCenter:
    @param particle: The particle
    @type particle: Either L{pytom_volume.vol} or string specifying the particle file name
    @param binning: Binning factor
    @return: [centerX,centerY,centerZ]

    @author: GvdS
    """
    if particle.__class__ == str:
        from pytom.tompy.io import read
        particle = read(particle)

    centerX = particle.shape[0] / 2.0 * (1.0 / float(binning))
    centerY = particle.shape[1] / 2.0 * (1.0 / float(binning))
    centerZ = particle.shape[2] / 2.0 * (1.0 / float(binning))
    #
    # if binning > 1:
    #   centerX = centerX - 0.25*(binning-1)
    #    centerY = centerY - 0.25*(binning-1)
    #    centerZ = centerZ - 0.25*(binning-1)

    return [centerX, centerY, centerZ]
Exemplo n.º 7
0
#!/usr/bin/env pytom

from pytom.tompy.io import read
import sys
import numpy as np

a = read(sys.argv[1])
b = read(sys.argv[2])

a = np.angle(np.fft.fftshift(np.fft.fftn(a)))
b = np.angle(np.fft.fftshift(np.fft.fftn(b)))

d = np.abs(a - b)

print(d.mean(), d.std(), d.min(), d.max())

import matplotlib

matplotlib.use('Qt5Agg')
from pylab import imshow, show

d = d.squeeze()
print(d.shape)
imshow(d)
show()
Exemplo n.º 8
0
#!/usr/bin/env pytom

from pytom.tompy.io import read
import sys
import numpy as np

a = (read(sys.argv[1]).squeeze())
b = (read(sys.argv[2]).squeeze())

if len(sys.argv) > 3 and sys.argv[3] != 'plot' and int(sys.argv[3]) == 1:
    a = np.fft.fftshift(a, axes=(0, 1))
    b = np.fft.fftshift(b, axes=(0, 1))

try:
    mask = read('mask.mrc')
    a = a * mask
    b = b * mask
except Exception as e:
    print(e)
    mask = np.ones_like(a)
#a = np.angle(np.fft.fftshift(np.fft.fftn(a)))
#b = np.angle(np.fft.fftshift(np.fft.fftn(b)))

d = np.abs(a - b)

for e in [d]:
    print(e[mask > 1E-4].mean(), e[mask > 1E-4].std(), e.min(), e.max())

import matplotlib

matplotlib.use('Qt5Agg')
Exemplo n.º 9
0
        options=options)

    if len(sys.argv) == 1:
        print(helper)
        sys.exit()

    try:
        filename, outname, numSTD, smooth, num_cycles, mask, help = parse_script_options(
            sys.argv[1:], helper)
    except Exception as e:
        print(e)
        sys.exit()

    if help is True:
        print(helper)
        sys.exit()

    if os.path.exists(filename):
        data = read(filename)
    else:
        print(helper)
        sys.exit()

    num_cycles = 2 if num_cycles is None else int(num_cycles)
    numStd = 1 if num_cycles is None else float(numSTD)
    smooth = 2 if smooth is None else float(smooth)

    if not mask is None:
        mask = read(mask)

    gen_mask_fsc(data, num_cycles, outname, numSTD, smooth, maskD=mask)
Exemplo n.º 10
0
def CorrectProjection_proxy(fname, new_fname, p, metafile, gs, fs,
                            binning_factor, rotation_angle):
    """
    @param fname: filename
    @type fname: C{str}
    @param new_fname:
    @type new_fname: C{str}
    @param p:
    @param metafile: star-file with metadata (contains defoci (long-/short), astig agnle alpha, 
                     voltage, Cs, Amplitude-contrast, Imdim, PixelSpacing, Magnification
    @type metafile: C{str}
    @param gs: grid spacing [2,4,6,...] is the size of the area which is
               corrected with a constant ctf. The ctf value is taken
               from the center of this area. This area builds up the
               corrected projection.
    @param fs: fieldsize [2,4,6,...] & (fs>=gs) is the size of the area which
               is extracted from the projection and corrected with a
               constant ctf. Fieldsize is also the size of the modelled ctf.
    @param binning_factor: de-magnfication factor
    @type binning_factor: C{float}
    @param rotation_angle:
    @return:

    """
    print('Correct projection:', fname)

    # load the metadata
    #from numpy import loadtxt
    from pytom.gui.guiFunctions import loadstar
    metadata = loadstar(metafile, dtype=datatype)

    # Alignment parameter
    Tiltangles = metadata['TiltAngle']
    Tiltaxis = rotation_angle
    directions = {0: 'horizontal', 1: 'vertical'}
    direction = directions[int(np.around(Tiltaxis / 90)) % 2]

    dz1 = metadata['DefocusU']
    dz2 = metadata['DefocusV']
    alpha = metadata['DefocusAngle']

    Voltage = metadata['Voltage'][p]
    Cs = metadata['SphericalAberration'][p]
    A = metadata['AmplitudeContrast'][p]
    Imdim = metadata['ImageSize'][p]

    if Imdim == 0: Imdim = 3710

    Objectpixelsize = metadata['PixelSpacing'][p] * 0.1 * binning_factor

    from pytom.tompy.io import read, write

    # Load projection
    proj = np.array(read(fname))
    proj = np.squeeze(proj)  # squeeze it to 2D

    # Create defocus plane dz1
    dz1p = CalculateDefocusModel(dz1[p], Objectpixelsize, Imdim, Tiltangles[p],
                                 Tiltaxis)

    # Create defocus plane dz2
    dz2p = CalculateDefocusModel(dz2[p], Objectpixelsize, Imdim, Tiltangles[p],
                                 Tiltaxis)

    # Create astigmatism angle plane
    alphap = (alpha[p] + Tiltaxis) * np.ones((Imdim, Imdim))

    # !!! ADDED Tiltaxis(1,p) to astigmatsm angle to correct for Tiltaxis rotation during CTF determination !!! -- SP 7.7.16
    # originally: alphap = alpha[p]*np.ones((Imdim,Imdim))

    projc = CorrectProjection(proj,
                              dz1p,
                              dz2p,
                              alphap,
                              gs,
                              fs,
                              Objectpixelsize,
                              Voltage,
                              Cs,
                              A,
                              direction=direction)

    # Save projection
    try:
        mrcfile.new(new_fname, projc.T.get(), overwrite=True)
    except:
        mrcfile.new(new_fname, projc.T, overwrite=True)

    #write(new_fname, projc, Tiltangles[p])

    return True
Exemplo n.º 11
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:
Exemplo n.º 12
0
def run_single_tilt_angle(subtomogram, ang, offset, vol_size,
                          particle_position, particle_rotation,
                          particle_filename, particle_number, binning, img,
                          create_graphics, fsc_path, dimz, peak_border):
    """
    To run a single tilt angle to allow for parallel computing

    @param ang: the tilt angle
    @type ang: int
    @param subtomogram: the filename of the subtomogram
    @type subtomogram: str
    @param offset: the offset used (x,y,z)
    @type offset: list(int, int, int)
    @param vol_size: the size of the volume to be reconstructed (in pixels)
    @type vol_size: int
    @param particle_position: the position of the particle in vector format,
               as given by particle.pickPosition().toVector()
    @type particle_position: tuple
    @param particle_rotation: the rotation of the particle (Z1/phi, X/the, Z2/psi)
    @type particle_rotation: tuple
    @param particle_filename: the filename of the particle, as given by particle.getfilename()
    @type particle_filename: str
    @param particle_number: the number of the particle, to allow for unique mapping
    @type particle_number: int
    @param binning: the binning factor used
    @type binning: int
    @param img: the filename of the projection to be used
    @type img: str
    @param create_graphics: to flag if images should be created for human inspection of the work done
    @type create_graphics: bool
    @return: the newly found positions of the particle, as a list  in the LOCAL_ALIGNMENT_RESULTS format
    @returntype: list
    """

    from pytom.reconstruction.reconstructionFunctions import alignImageUsingAlignmentResultFile
    from pytom.tompy.transform import rotate3d, rotate_axis
    from pytom.gui.guiFunctions import datatypeAR, loadstar
    import numpy as np
    from math import cos, sin, pi, sqrt
    from pytom.tompy.tools import create_circle
    from pytom.tompy.transform import cut_from_projection
    from pytom.tompy.filter import applyFourierFilterFull, bandpass_circle
    from pytom.tompy.io import read, write
    from pytom.tompy.correlation import meanUnderMask, stdUnderMask
    import os
    import time

    t = time.time()
    # print(particle_filename, ang)

    # Filter using FSC
    fsc_mask = None
    k = 0.01
    subtomogram = read(subtomogram) * read(
        '/data/gijsvds/ctem/05_Subtomogram_Analysis/Alignment/GLocal/mask_200_75_5.mrc'
    )

    import os
    from pytom.tompy.filter import filter_volume_by_profile, profile2FourierVol
    fsc_path = ''
    if os.path.isfile(fsc_path):
        profile = [line.split()[0] for line in open(fsc_path, 'r').readlines()]
        fsc_mask3d = profile2FourierVol(profile, subtomogram.shape)

        subtomogram = applyFourierFilterFull(subtomogram, fsc_mask3d)

    # Cross correlate the templat
    # e and patch, this should give the pixel shift it is after
    from pytom.tompy.correlation import nXcf

    # Get template
    # First rotate the template towards orientation of the particle, then to the tilt angle

    img = read(img)

    rotated1 = rotate3d(subtomogram,
                        phi=particle_rotation[0],
                        the=particle_rotation[1],
                        psi=particle_rotation[2])
    rotated2 = rotate_axis(
        rotated1, -ang,
        'y')  # SWITCHED TO ROTATE AXIS AND ANGLE *-1 THIS IS AN ATTEMPT
    template = rotated2.sum(axis=2)

    # write('pp1_template.mrc', template)

    # img = read(img)
    try:
        patch, xx, yy = cut_patch(img,
                                  ang,
                                  particle_position,
                                  dimz=dimz,
                                  vol_size=vol_size,
                                  binning=binning)
        mask2d = create_circle(patch.shape, radius=75, sigma=5, num_sigma=2)
        patch *= mask2d
        # write('pp1_patch.mrc', patch)

        if os.path.isfile(fsc_path):
            profile = [
                line.split()[0] for line in open(fsc_path, 'r').readlines()
            ]
            fsc_mask2d = profile2FourierVol(profile, patch.shape)
            patch = applyFourierFilterFull(patch, fsc_mask2d)

        template = normalize_image(template, mask2d, mask2d.sum())
        patch = normalize_image(patch, mask2d, mask2d.sum())

        if 1:
            ff = xp.ones_like(patch)
        else:
            ff = bandpass_circle(patch.squeeze(), 6, 25, 3)

        ccf = normalised_cross_correlation(template, patch, ff)

        points2d1 = find_sub_pixel_max_value_2d(ccf.copy(),
                                                ignore_border=peak_border)
        points2d2 = find_sub_pixel_max_value(ccf.copy(),
                                             ignore_border=peak_border)
        points2d = list(points2d2[:2]) + [points2d1[-1]]

        x_diff = points2d[0] - vol_size / 2
        y_diff = points2d[1] - vol_size / 2

        dist = sqrt(x_diff**2 + y_diff**2)

        #rx, ry, ii, oox, ooy, x2, y2 = find_sub_pixel_max_value_voltools(ccf.copy())

        #print(rx,ry, x_diff, y_diff, x2, y2, oox, ooy, ii.shape)
        #print(f'{particle_number:3d} {ang:5.1f}, {dist:5.2f} {x_diff} {y_diff} {ccf.max()}')
        if create_graphics:
            rx, ry, ii, oox, ooy, x2, y2 = find_sub_pixel_max_value_voltools(
                ccf.copy(), k=k)
            print(rx, ry)
            from scipy.ndimage.filters import gaussian_filter
            # Create an image to display and/or test the inner workings of the algorithm

            points = find_sub_pixel_max_value(ccf, ignore_border=peak_border)

            nx, ny, nz = particle_position
            nx += x_diff
            ny += y_diff

            npatch = cut_from_projection(
                img.squeeze(), [xx + x_diff, yy + y_diff],
                [vol_size, vol_size]
            )  # img.sum(axis=2)[int(xx+x_diff-v):int(xx+x_diff+v), int(yy+y_diff-v):int(yy+y_diff+v)]  #
            npatch = normalize_image(npatch, mask2d, mask2d.sum())

            nccf = normalised_cross_correlation(template, npatch.squeeze(), ff)
            npoints = find_sub_pixel_max_value(nccf, ignore_border=peak_border)
            npoints2d = find_sub_pixel_max_value_2d(nccf,
                                                    ignore_border=peak_border)
            #rx, ry = abs(xp.array(npoints2d[:2]) - 100)

            from scipy.ndimage.filters import gaussian_filter
            # Create an image to display and/or test the inner workings of the algorithm

            points = find_sub_pixel_max_value(ccf, ignore_border=peak_border)

            nx, ny, nz = particle_position
            nx += x_diff
            ny += y_diff

            npatch = cut_from_projection(
                img.squeeze(), [xx + x_diff, yy + y_diff],
                [vol_size, vol_size]
            )  # img.sum(axis=2)[int(xx+x_diff-v):int(xx+x_diff+v), int(yy+y_diff-v):int(yy+y_diff+v)]  #
            npatch = normalize_image(npatch, mask2d, mask2d.sum())

            nccf = normalised_cross_correlation(template, npatch.squeeze(), ff)
            npoints = find_sub_pixel_max_value(nccf, ignore_border=peak_border)
            npoints2d = find_sub_pixel_max_value_2d(nccf,
                                                    ignore_border=peak_border)
            #rx, ry = abs(xp.array(npoints2d[:2]) - 100)

            m_style = dict(color='tab:blue',
                           linestyle=':',
                           marker='o',
                           markersize=5,
                           markerfacecoloralt='tab:red')
            m_style_alt = dict(color='tab:red',
                               linestyle=':',
                               marker='o',
                               markersize=5,
                               markerfacecoloralt='tab:orange')

            grid = pp.GridSpec(3,
                               3,
                               wspace=0,
                               hspace=0.35,
                               left=0.05,
                               right=0.95,
                               top=0.90,
                               bottom=0.05)

            ax_0_0 = pp.subplot(grid[0, 0])
            ax_0_1 = pp.subplot(grid[0, 1])
            ax_0_2 = pp.subplot(grid[0, 2])
            ax_1_0 = pp.subplot(grid[1, 0])
            ax_1_1 = pp.subplot(grid[1, 1])
            ax_1_2 = pp.subplot(grid[1, 2])
            ax_2_0 = pp.subplot(grid[2, 0])
            ax_2_1 = pp.subplot(grid[2, 1])
            ax_2_2 = pp.subplot(grid[2, 2])

            ax_0_0.axis('off')
            ax_0_1.axis('off')
            ax_0_2.axis('off')
            ax_1_0.axis('off')
            ax_1_1.axis('off')
            ax_1_2.axis('off')
            ax_2_0.axis('off')
            ax_2_1.axis('off')
            ax_2_2.axis('off')

            axis_title(ax_0_0, "Cutout")
            ax_0_0.imshow(applyFourierFilterFull(patch, xp.fft.fftshift(ff)))
            axis_title(ax_0_1, "Template")
            ax_0_1.imshow(template)
            axis_title(ax_0_2, "Shifted Cutout\n(based on cross correlation)")
            ax_0_2.imshow(npatch.squeeze())

            axis_title(ax_1_0, u"Cross correlation\ncutout × template")
            ax_1_0.imshow(ccf)
            ax_1_0.plot([points[1]], [points[0]], fillstyle='none', **m_style)
            ax_1_0.plot([points2d[1]], [points2d[0]],
                        fillstyle='none',
                        **m_style_alt)
            ax_1_0.plot([vol_size / 2], [vol_size / 2], ",k")

            ax_1_1.text(
                0.5,
                0.8,
                "Red: 2D spline interpolation\nx: {:f}\ny: {:f}\nBlue: 1D spline interpolation\nx: {:f}\ny: {:f}"
                "\nBlack: center".format(x_diff, y_diff,
                                         points[0] - vol_size / 2,
                                         points[1] - vol_size / 2),
                fontsize=8,
                horizontalalignment='center',
                verticalalignment='center',
                transform=ax_1_1.transAxes)

            axis_title(ax_1_2, u"Cross correlation\nshifted cutout × template")
            ax_1_2.imshow(nccf)
            ax_1_2.plot([npoints[0]], [npoints[1]],
                        fillstyle='none',
                        **m_style)
            ax_1_2.plot([npoints2d[0]], [npoints2d[1]],
                        fillstyle='none',
                        **m_style_alt)
            ax_1_2.plot([vol_size / 2], [vol_size / 2], ",k")

            axis_title(ax_2_0, u"Zoom into red peak\nin CC cutout × template")
            d = 10

            points2d = list(xp.unravel_index(ccf.argmax(),
                                             ccf.shape)) + [points2d[-1]]
            peak = ccf[int(points2d[0]) - d:int(points2d[0]) + d,
                       int(points2d[1]) - d:int(points2d[1] + d)]
            ax_2_0.imshow(peak)

            axis_title(
                ax_2_1,
                u"Zoom into red peak\nin CC cutout × template\ninterpolated")
            ax_2_1.imshow(ii)

            ax_2_1.plot([y2 + (y_diff - ry) / k], [x2 + (x_diff - rx) / k],
                        fillstyle='none',
                        **m_style)
            ax_2_1.plot([y2], [x2], fillstyle='none', **m_style_alt)
            axis_title(ax_2_2, u"Cutout\nGaussian filter σ3")
            import scipy
            ax_2_2.imshow(scipy.ndimage.gaussian_filter(patch, 3))

            pp.savefig(
                "../Images/test/polish_particle_{:04d}_tiltimage_{:05.2f}_shift_{:.1f}.png"
                .format(particle_number, ang, dist))
        del img
    except Exception as e:
        print(e)
        x_diff = y_diff = 0

    print(
        f'{particle_number:3d} {ang:4d} {x_diff:5.2f} {y_diff:5.2f} {points2d1[0]-patch.shape[0]//2:.2f} {points2d1[1]-patch.shape[0]//2:.2f} {time.time()-t:5.3f}'
    )
    return particle_number, x_diff, y_diff, ang, 0, 0, particle_filename
Exemplo n.º 13
0
Arquivo: diff.py Projeto: xmzzaa/PyTom
import sys
from pytom.tompy.io import read

data1 = read(sys.argv[1]).squeeze()
data2 = read(sys.argv[2]).squeeze()

print(data1.shape, data2.shape)

import matplotlib
matplotlib.use('Qt5Agg')

diff = abs(data1 - data2)

from pylab import *

print(allclose(data2, data1, rtol=1E-5))

fig, ax = subplots(1, 3, figsize=(15, 5))

ax[0].imshow(data1)
ax[1].imshow(data2)
ax[2].imshow(abs(data1 - data2))
show()
Exemplo n.º 14
0
def run_single_tilt_angle(ang, subtomogram, offset, vol_size,
                          particle_position, particle_rotation,
                          particle_filename, particle_number, binning, img,
                          create_graphics, fsc_path, dimz, peak_border):
    """
    To run a single tilt angle to allow for parallel computing

    @param ang: the tilt angle
    @type ang: int
    @param subtomogram: the filename of the subtomogram
    @type subtomogram: str
    @param offset: the offset used (x,y,z)
    @type offset: list(int, int, int)
    @param vol_size: the size of the volume to be reconstructed (in pixels)
    @type vol_size: int
    @param particle_position: the position of the particle in vector format,
               as given by particle.pickPosition().toVector()
    @type particle_position: tuple
    @param particle_rotation: the rotation of the particle (Z1/phi, X/the, Z2/psi)
    @type particle_rotation: tuple
    @param particle_filename: the filename of the particle, as given by particle.getfilename()
    @type particle_filename: str
    @param particle_number: the number of the particle, to allow for unique mapping
    @type particle_number: int
    @param binning: the binning factor used
    @type binning: int
    @param img: the filename of the projection to be used
    @type img: str
    @param create_graphics: to flag if images should be created for human inspection of the work done
    @type create_graphics: bool
    @return: the newly found positions of the particle, as a list  in the LOCAL_ALIGNMENT_RESULTS format
    @returntype: list
    """

    print(ang, offset, binning, particle_position)
    from pytom.tompy.transform import rotate3d
    import numpy as np
    from math import cos, sin, pi
    from pytom.tompy.transform import cut_from_projection
    from pytom.tompy.io import read

    subtomogram = read(subtomogram)
    img = read(img)

    # Get the size of the original projection
    dim_x = img.shape[0]
    dim_z = dim_x if dimz is None else dimz
    print(dim_x, dim_z)

    x, y, z = particle_position
    x = (x + offset[0]) * binning
    y = (y + offset[1]) * binning
    z = (z + offset[2]) * binning

    # Get template
    # First rotate towards orientation of the particle, then to the tilt angle
    rotated1 = rotate3d(subtomogram,
                        phi=particle_rotation[0],
                        the=particle_rotation[1],
                        psi=particle_rotation[2])
    rotated2 = rotate3d(rotated1, the=ang)  # 'the' is the rotational axis
    template = rotated2.sum(axis=2)

    # Get coordinates of the paricle adjusted for the tilt angle
    yy = y  # assume the rotation axis is around y
    xx = (cos(ang * pi / 180) * (x - dim_x / 2) - sin(ang * pi / 180) *
          (z - dim_z / 2)) + dim_x / 2

    # Cut the small patch out
    patch = cut_from_projection(img.sum(axis=2), [xx, yy],
                                [vol_size, vol_size])
    patch = patch - patch.mean()

    # Filter using FSC
    fsc_mask = None
    import os
    if os.path.isfile(fsc_path):
        f = open(fsc_path, "r")
        fsc = map(lambda a: float(a), f.readlines())
        f.close()
        fsc_mask = create_fsc_mask(fsc, vol_size)
    elif fsc_path != "":
        print("Not an existing FSC file: " + fsc_path)

    # Cross correlate the template and patch, this should give the pixel shift it is after
    ccf = normalised_cross_correlation(template, patch, fsc_mask)
    points2d = find_sub_pixel_max_value_2d(ccf, ignore_border=peak_border)

    x_diff = points2d[0] - vol_size / 2
    y_diff = points2d[1] - vol_size / 2

    if create_graphics:
        # Create an image to display and/or test the inner workings of the algorithm
        m_style = dict(color='tab:blue',
                       linestyle=':',
                       marker='o',
                       markersize=5,
                       markerfacecoloralt='tab:red')
        m_style_alt = dict(color='tab:red',
                           linestyle=':',
                           marker='o',
                           markersize=5,
                           markerfacecoloralt='tab:blue')

        points = find_sub_pixel_max_value(ccf)

        nx, ny, nz = particle_position
        nx += x_diff
        ny += y_diff

        npatch = cut_from_projection(
            img.sum(axis=2), [xx + x_diff, yy + y_diff], [vol_size, vol_size]
        )  # img.sum(axis=2)[int(xx+x_diff-v):int(xx+x_diff+v), int(yy+y_diff-v):int(yy+y_diff+v)]  #
        npatch = npatch - np.mean(npatch)

        nccf = normalised_cross_correlation(template, npatch.squeeze())
        npoints = find_sub_pixel_max_value(nccf)
        npoints2d = find_sub_pixel_max_value_2d(nccf,
                                                ignore_border=peak_border)

        import pylab as pp

        grid = pp.GridSpec(3,
                           3,
                           wspace=0,
                           hspace=0.35,
                           left=0.05,
                           right=0.95,
                           top=0.90,
                           bottom=0.05)

        ax_0_0 = pp.subplot(grid[0, 0])
        ax_0_1 = pp.subplot(grid[0, 1])
        ax_0_2 = pp.subplot(grid[0, 2])
        ax_1_0 = pp.subplot(grid[1, 0])
        ax_1_1 = pp.subplot(grid[1, 1])
        ax_1_2 = pp.subplot(grid[1, 2])
        ax_2_0 = pp.subplot(grid[2, 0])
        ax_2_1 = pp.subplot(grid[2, 1])
        ax_2_2 = pp.subplot(grid[2, 2])

        ax_0_0.axis('off')
        ax_0_1.axis('off')
        ax_0_2.axis('off')
        ax_1_0.axis('off')
        ax_1_1.axis('off')
        ax_1_2.axis('off')
        ax_2_0.axis('off')
        ax_2_1.axis('off')
        ax_2_2.axis('off')

        axis_title(ax_0_0, "Cutout")
        ax_0_0.imshow(patch)
        axis_title(ax_0_1, "Template")
        ax_0_1.imshow(template)
        axis_title(ax_0_2, "Shifted Cutout\n(based on cross correlation)")
        ax_0_2.imshow(npatch.squeeze())

        axis_title(ax_1_0, u"Cross correlation\ncutout × template")
        ax_1_0.imshow(ccf)
        ax_1_0.plot([p[1] for p in points], [p[0] for p in points],
                    fillstyle='none',
                    **m_style)
        ax_1_0.plot([points2d[1]], [points2d[0]],
                    fillstyle='none',
                    **m_style_alt)
        ax_1_0.plot([vol_size / 2], [vol_size / 2], ",k")

        ax_1_1.text(
            0.5,
            0.8,
            "Red: 2D spline interpolation\nx: {:f}\ny: {:f}\nBlue: 1D spline interpolation\nx: {:f}\ny: {:f}"
            "\nBlack: center".format(x_diff, y_diff,
                                     points[0][0] - vol_size / 2,
                                     points[0][1] - vol_size / 2),
            fontsize=8,
            horizontalalignment='center',
            verticalalignment='center',
            transform=ax_1_1.transAxes)

        axis_title(ax_1_2, u"Cross correlation\nshifted cutout × template")
        ax_1_2.imshow(nccf)
        ax_1_2.plot([p[0] for p in npoints], [p[1] for p in npoints],
                    fillstyle='none',
                    **m_style)
        ax_1_2.plot([npoints2d[0]], [npoints2d[1]],
                    fillstyle='none',
                    **m_style_alt)
        ax_1_2.plot([vol_size / 2], [vol_size / 2], ",k")

        axis_title(ax_2_0, u"Zoom into red peak\nin CC cutout × template")
        d = 10
        peak = ccf[int(points2d[0]) - d:int(points2d[0]) + d,
                   int(points2d[1]) - d:int(points2d[1] + d)]
        ax_2_0.imshow(peak)

        axis_title(
            ax_2_1,
            u"Zoom into red peak\nin CC cutout × template\ninterpolated")
        ax_2_1.imshow(points2d[2])

        axis_title(ax_2_2, u"Cutout\nGaussian filter σ3")
        import scipy
        ax_2_2.imshow(scipy.ndimage.gaussian_filter(patch, 3))

        pp.savefig("polish_particle_{:04d}_tiltimage_{:05.2f}.png".format(
            particle_number, ang))

    return particle_number, x_diff, y_diff, ang, 0, 0, particle_filename
Exemplo n.º 15
0
def CorrectProjection_proxy(fname,
                            new_fname,
                            p,
                            metadata,
                            gs,
                            fs,
                            binning_factor,
                            rotation_angle,
                            def_grad_strip=2.5):
    """
    """
    print('Correct projection:', fname)

    # load the metadata
    #from numpy import loadtxt

    # Alignment parameter
    Tiltaxis = rotation_angle
    Tiltangle = metadata['TiltAngle'][p]
    dz1 = -1 * metadata['DefocusU'][p]
    dz2 = -1 * metadata['DefocusV'][p]
    alpha = metadata['DefocusAngle'][p]
    Voltage = metadata['Voltage'][p]
    Cs = metadata['SphericalAberration'][p]
    A = metadata['AmplitudeContrast'][p]
    Imdim = metadata['ImageSize'][p]
    Objectpixelsize = metadata['PixelSpacing'][p] * 0.1 * binning_factor

    from pytom.tompy.io import read, write
    from pytom.tompy.tools import paste_in_center
    # Load projection

    # Fast options are 3840, 3888, 4096
    size = 3888

    projInit = np.array(read(fname))
    projInit = np.squeeze(projInit)  # squeeze it to 2D
    proj = np.zeros((size, size))
    proj = paste_in_center(projInit, proj)

    B = (proj.shape[0] - projInit.shape[0]) // 2

    sx, sy = proj.shape[0] // 2, proj.shape[1] // 2
    x, y = np.meshgrid(np.arange(-sx, sx), np.arange(-sx, sx))

    import time
    s = time.time()
    projc = CorrectProjection2(proj,
                               dz1,
                               dz2,
                               alpha + Tiltaxis,
                               Tiltangle,
                               Tiltaxis,
                               dfgrad=def_grad_strip,
                               Border=B,
                               ObjectPixelSize=Objectpixelsize,
                               Voltage=Voltage,
                               CS=Cs,
                               AmpContrast=A,
                               x=x,
                               y=y)
    print(time.time() - s)
    # Save projection
    try:
        mrcfile.new(new_fname, projc.T.get().astype('float32'), overwrite=True)
    except:
        mrcfile.new(new_fname, projc.T.astype('float32'), overwrite=True)

    #write(new_fname, projc, Tiltangles[p])

    return True
Exemplo n.º 16
0
        iter = 10
    else:
        iter = int(iter)

    if metafile and os.path.exists(metafile):
        metadata = loadstar(metafile, dtype=datatype)
        tiltAngles = metadata['TiltAngle']
    else:
        tiltAngles = []

    metafile = '' if metafile is None or not os.path.exists(
        metafile) else metafile

    # start reconstruction
    from pytom.tompy.io import read, write
    from nufft.reconstruction import fourier_2d1d_iter_reconstruct
    from pytom.reconstruction.reconstructionStructures import ProjectionList
    projections = ProjectionList()
    projections.loadDirectory(proj_dir, metafile=metafile)
    projections.sort()

    projs = []
    tilt_angles = []
    for p in projections:
        print(p.getTiltAngle(), p.getFilename())
        projs.append(read(p.getFilename()))
        tilt_angles.append(p.getTiltAngle())

    v = fourier_2d1d_iter_reconstruct(projs, tilt_angles, iter)
    write(output_filename, v)
Exemplo n.º 17
0
        print(helper)
        sys.exit()

    try:
        filename, outname, num_cycles, help = parse_script_options(
            sys.argv[1:], helper)
    except Exception as e:
        print(e)
        sys.exit()

    if help is True:
        print(helper)
        sys.exit()

    if os.path.exists(filename):
        data = read(filename)
    else:
        print(helper)
        sys.exit()

    if num_cycles is None:
        num_cycles = 0
    else:
        num_cylces = int(num_cycles)

    data = read(filename)
    mask = zeros_like(data, dtype=int)
    mask[data < data.mean() - data.std()] = 1
    mask = remove_small_objects(mask.astype(bool))
    mask = binary_fill_holes(mask)
Exemplo n.º 18
0
def alignImageUsingAlignmentResultFile(alignmentResultsFile,
                                       indexImage,
                                       weighting=None,
                                       lowpassFilter=0.9,
                                       binning=1,
                                       circleFilter=False):
    import pytom_freqweight
    from pytom_numpy import vol2npy
    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, ellipse_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']

    imdimX = read_size(imageList[0], 'x')
    imdimY = read_size(imageList[0], 'y')

    if binning > 1:
        imdimX = int(float(imdimX) / float(binning) + .5)
        imdimY = int(float(imdimY) / float(binning) + .5)

    sliceWidth = imdimX

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

    if circleFilter:
        circleFilterRadiusX = imdimX // 2
        circleFilterRadiusY = imdimY // 2

        circleSlice = xp.fft.fftshift(
            ellipse_filter(imdimX, imdimY, circleFilterRadiusX,
                           circleFilterRadiusY))
    else:
        circleSlice = xp.ones((imdimX, imdimY))

    # 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((imdimX,imdimY),lowpassFilter*(imdim//2), sigma=0.4*lowpassFilter*(imdim//2)))

    projectionList = ProjectionList()
    for n, image in enumerate(imageList):
        atx = alignmentResults['AlignmentTransX'][n]
        aty = alignmentResults['AlignmentTransY'][n]
        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)

    imdim = min(imdimY, imdimX)

    for (ii, projection) in enumerate(projectionList):
        if not ii == indexImage:
            continue
        from pytom.tompy.transform import resize

        # print(f'read {projection._filename}')
        image = read(str(projection._filename)).squeeze()

        if binning > 1:
            image = resize(image, 1 / binning)

        #write(f'test/image_{ii}.mrc', image, tilt_angle=tilt_angles[ii])

        tiltAngle = projection._tiltAngle

        # 1 -- normalize to contrast - subtract mean and norm to mean
        immean = image.mean()
        image = (image - immean) / immean

        # 2 -- smoothen borders to prevent high contrast oscillations
        image = taper_edges(image, imdim // 30)[0]

        # 3 -- square if needed
        if 0 and imdimY != imdimX:
            newImage = xp.zeros((imdim, imdim, 1), dtype=xp.float32)
            pasteCenter(image, newImage)
            image = newImage

        # 4 -- 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,
            center=[inputImage.shape[0] // 2, inputImage.shape[1] // 2, 0],
            device=device,
            translation=[transX, transY, 0],
            scale=[mag, mag, 1],
            interpolation='filt_bspline')

        del image
        image = outputImage.squeeze()

        # 5 -- Optional Low Pass Filter
        if lowpassFilter:
            from pytom.tompy.filter import bandpass_circle

            image = bandpass_circle(
                image,
                high=lowpassFilter * (min(imdimX, imdimY) // 2),
                sigma=0.4 * lowpassFilter * (min(imdimX, imdimY) // 2))
            # image = xp.abs((xp.fft.ifftn(xp.fft.fftn(image) * lpf)))

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

        # 7 -- 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.T * circleSlice).real

        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).real

        del inputImage, outputImage, circleSlice

        write(f'inputImage_{ii}.mrc', image)

        return image.astype(xp.float32)
Exemplo n.º 19
0
    wedgeAngle = 30
    wedgeFilter = weight(wedgeAngle, 0, end - start, size, size)
    wedgeVolume = wedgeFilter.getWeightVolume(True)

    filterVolume = pytom_volume.reducedToFull(wedgeVolume)
    wedgeV = vol2npy(filterVolume).copy()

    from pytom.tompy.io import read
    import mrcfile

    print(start, end)
    volume = mrcfile.open('tomo.mrc', permissive=True).data.copy()
    volume = volume[start:end, :csize, :csize]
    assert wedgeV.shape == volume.shape

    temp = read('template.em')
    mask = read("mask.em")
    mask2 = paste_in_center(mask, np.zeros_like(volume))

    from pytom.tompy.correlation import meanVolUnderMask, stdVolUnderMask
    import pytom.basic.correlation as corr
    from pytom.basic.files import read as readd
    from pytom.basic.files import write_em
    from pytom_numpy import vol2npy, npy2vol
    from pytom_volume import pasteCenter, vol

    vv = readd('tomo.mrc', subregion=[0, 0, start, 464, 464, end - start])
    mm = readd('mask.em')
    if vv.sizeX() != mm.sizeX() or vv.sizeY() != mm.sizeY() or vv.sizeZ(
    ) != mm.sizeZ():
        maskV = vol(vv.sizeX(), vv.sizeY(), vv.sizeZ())
Exemplo n.º 20
0
def FSC(volume1,
        volume2,
        numberBands=None,
        mask=None,
        verbose=False,
        filename=None,
        num_procs=1):
    """
    FSC - Calculates the Fourier Shell Correlation for two volumes
    @param volume1: volume one
    @type volume1: L{pytom_volume.vol}
    @param volume2: volume two
    @type volume2: L{pytom_volume.vol}
    @param numberBands: number of shells for FSC
    @type numberBands: int
    @param filename: write FSC to ascii file if specified
    @type filename: string

    @return: Returns a list of cc values 
    @author: Thomas Hrabe  
    @rtype: list[floats]
    """

    from pytom.tompy.correlation import bandCC
    from pytom.basic.structures import Mask
    from pytom.tompy.io import read, write
    from pytom.tompy.tools import volumesSameSize
    import time
    t = time.time()

    if not volumesSameSize(volume1, volume2):
        raise RuntimeError('Volumes must have the same size!')

    numberBands = volume1.shape[0] // 2 if numberBands is None else numberBands

    if not mask is None:
        if mask.__class__ == xp.array([0]).__class__:
            volume1 = volume1 * mask
            volume2 = volume2 * mask

        elif mask.__class__ == Mask:
            mask = mask.getVolume()
            volume1 = volume1 * mask
            volume2 = volume2 * mask

        elif mask.__class__ == str:
            mask = read(mask)
            volume1 = volume1 * mask
            volume2 = volume2 * mask

        else:
            raise RuntimeError(
                'FSC: Mask must be a volume OR a Mask object OR a string path to a mask!'
            )

    fscResult = []
    band = [-1, -1]

    increment = int(volume1.shape[0] / 2 * 1 / numberBands)
    import time

    fvolume1 = xp.fft.fftn(volume1)
    fvolume2 = xp.fft.fftn(volume2)

    for n, i in enumerate(range(0, volume1.shape[0] // 2, increment)):

        band[0] = i
        band[1] = i + increment

        if verbose:
            print('Band : ', band)

        res = bandCC(fvolume1, fvolume2, band, verbose)

        if i == 0 and increment == 1:
            #force a 1 for correlation of the zero frequency
            res = 1

        if verbose:
            print('Correlation ', res)

        fscResult.append(res)

    if filename:
        f = open(filename, 'w')
        for item in fscResult:
            f.write("%s\n" % item)
        f.close()

    return fscResult
Exemplo n.º 21
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.º 22
0
    except:
        start, end = 0, csize

    wedgeAngle = 30
    wedgeFilter = weight(wedgeAngle, 0, end - start, size, size)
    wedgeVolume = wedgeFilter.getWeightVolume(True)

    filterVolume = pytom_volume.reducedToFull(wedgeVolume)
    wedgeV = vol2npy(filterVolume).copy()

    from pytom.tompy.io import read
    import mrcfile

    # NDARRAYS
    voluNDA = mrcfile.open('tomo.mrc', permissive=True).data.copy()
    tempNDA = read('template.em')
    maskNDA = read("mask.em")

    sox, soy, soz = tempNDA.shape
    spx, spy, spz = voluNDA.shape

    #GPUARRAYS
    voluGPU = gu.to_gpu(voluNDA.astype(np.float32))
    tempGPU = gu.to_gpu(tempNDA.astype(np.float32))
    maskGPU = gu.to_gpu(maskNDA)

    #Pytom C arrays
    voluVOL = readd('tomo.mrc')
    tempVOL = readd('template.em')
    maskVOL = readd('mask.em')
Exemplo n.º 23
0
def toProjectionStackFromAlignmentResultsFile(alignmentResultsFile,
                                              weighting=None,
                                              lowpassFilter=0.9,
                                              binning=1,
                                              circleFilter=False,
                                              num_procs=1,
                                              outdir='',
                                              prefix='sorted_aligned'):
    """read image and create aligned projection stack, based on the results described in the alignmentResultFile.

       @param alignmentResultsFile: result file generate by the alignment script.
       @type datatypeAR: gui.guiFunction.datatypeAR
       @param weighting: weighting (<0: analytical weighting, >1: exact weighting, 0/None: no weighting )
       @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: GvdS
    """
    print('weighting: ', weighting)
    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, \
        fourierFilterShift_ReducedComplex
    from pytom_volume import complexRealMult, vol, paste
    import pytom_freqweight
    from pytom.basic.transformations import resize, rotate
    from pytom.gui.guiFunctions import fmtAR, headerAlignmentResults, datatype, datatypeAR, loadstar
    from pytom.reconstruction.reconstructionStructures import Projection, ProjectionList
    from pytom_numpy import vol2npy
    import mrcfile
    from pytom.tompy.io import write, read_size
    import os

    print("Create aligned images from alignResults.txt")

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

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

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

    sliceWidth = imdim

    # pre-determine analytical weighting function and lowpass for speedup
    if (weighting != None) and (float(weighting) < -0.001):
        weightSlice = fourierFilterShift(rampFilter(imdim, imdim))

    if circleFilter:
        circleFilterRadius = imdim // 2
        circleSlice = fourierFilterShift_ReducedComplex(
            circleFilter(imdim, imdim, circleFilterRadius))
    else:
        circleSlice = vol(imdim, imdim // 2 + 1, 1)
        circleSlice.setAll(1.0)

    # 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]

    projectionList = ProjectionList()
    imageList = []
    tilt_angles = []
    for n, image in enumerate(alignmentResults['FileName']):
        atx = alignmentResults['AlignmentTransX'][n]
        aty = alignmentResults['AlignmentTransY'][n]
        rot = alignmentResults['InPlaneRotation'][n]
        mag = alignmentResults['Magnification'][n]
        # print(image, alignmentResults['TiltAngle'][n])
        # if abs(alignmentResults['TiltAngle'][n]) > 20:
        #     continue
        tilt_angles.append(alignmentResults['TiltAngle'][n])
        imageList.append(image)
        projection = Projection(imageList[-1],
                                tiltAngle=tilt_angles[-1],
                                alignmentTransX=atx,
                                alignmentTransY=aty,
                                alignmentRotation=rot,
                                alignmentMagnification=mag)
        projectionList.append(projection)

    stack = vol(imdim, imdim, len(imageList))
    stack.setAll(0.0)

    phiStack = vol(1, 1, len(imageList))
    phiStack.setAll(0.0)

    thetaStack = vol(1, 1, len(imageList))
    thetaStack.setAll(0.0)

    offsetStack = vol(1, 2, len(imageList))
    offsetStack.setAll(0.0)

    for (ii, projection) in enumerate(projectionList):
        if projection._filename.split('.')[-1] == 'st':
            from pytom.basic.files import EMHeader, read
            idx = projection._index
            image = read(file=projection._filename,
                         subregion=[0, 0, idx - 1, imdim, 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
            image = read(str(projection._filename))
            # image = rotate(image,180.,0.,0.)
            image = resize(volume=image, factor=1 / float(binning))[0]

        if lowpassFilter:
            filtered = filterFunction(volume=image,
                                      filterObject=lpf,
                                      fourierOnly=False)
            image = filtered[0]

        tiltAngle = projection._tiltAngle

        # normalize to contrast - subtract mean and norm to mean
        immean = vol2npy(image).mean()
        image = (image - immean) / immean

        print(ii, immean, projection._filename)

        # 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)

        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()))
            image = ifft(complexRealMult(
                complexRealMult(fft(image), weightSlice), circleSlice),
                         scaling=True)

        elif (weighting != None) and (weighting > 0):
            weightSlice = fourierFilterShift(
                exactFilter(tilt_angles, tiltAngle, imdim, imdim, sliceWidth))
            # image = (ifft(complexRealMult(fft(image), w_func)) / (image.sizeX() * image.sizeY() * image.sizeZ()))
            image = ifft(complexRealMult(
                complexRealMult(fft(image), weightSlice), circleSlice),
                         scaling=True)

        thetaStack(int(round(projection.getTiltAngle())), 0, 0, ii)
        offsetStack(int(round(projection.getOffsetX())), 0, 0, ii)
        offsetStack(int(round(projection.getOffsetY())), 0, 1, ii)
        paste(image, stack, 0, 0, ii)
        fname = '{}_{:02d}.mrc'.format(
            prefix, int(imageList[ii].split('_')[-1].split('.')[0]))
        if outdir:
            import mrcfile
            # write_em(os.path.join(outdir, fname.replace('mrc', 'em')), image)
            write(os.path.join(outdir, fname),
                  vol2npy(image).copy().astype('float32'))
            print('written file: ', fname)

    return [stack, phiStack, thetaStack, offsetStack]
Exemplo n.º 24
0
def test(index=0):
    from pytom.tompy.io import read
    from pytom.tompy.transform import rotate3d

    path_raw_projection = "/data2/dschulte/BachelorThesis/Data/VPP2/03_Tomographic_Reconstruction/tomogram_000/sorted/sorted_29.em"
    path_aligned_projection = "/data2/dschulte/BachelorThesis/Data/VPP2/03_Tomographic_Reconstruction/tomogram_000/alignment/marker_0001_-60.0,60.0/sorted_aligned_30.em"
    path_template = "/data2/dschulte/BachelorThesis/Data/VPP2/05_Subtomogram_Analysis/combo_reduced.em"
    tilt_angle = -1.9989999533
    raw_projection = read(path_raw_projection)
    aligned_projection = read(path_aligned_projection)
    template = read(path_template)
    dim = aligned_projection.shape[0]

    from pytom.basic.structures import ParticleList

    particlelist1 = ParticleList()
    particlelist1.fromXMLFile(
        "/data2/dschulte/BachelorThesis/Data/VPP2/04_Particle_Picking/Picked_Particles/combined_reduced_extracted/particleList_combined_reduced_tomogram_010_WBP.xml"
    )

    align_results = (-15.3044300079, 3.7495634556, -184.3835906982,
                     1.0000053644)  # -2
    #align_results = (-1.2395387888, 4.9647006989, -184.3754882812, 1.0000000000) # 0
    #pick_position = (278.12318382089245 * 8, 222.6395540890773 * 8, 268.97256780848085 * 8) #0
    #pick_position = (381.21906883806 * 8, 153.61353397521387 * 8, 246.8315433927568 * 8) #74
    #particle_rotation = (26.442828473505173, 44.44149544840194, 58.160958298848676) #0
    #particle_rotation = (85.2456894956599, 30.815061362336394, 9.543300915975514) #74

    pick_position = particlelist1[index].getPickPosition().toVector()
    particle_rotation = (particlelist1[index].getRotation().getZ1(),
                         particlelist1[index].getRotation().getX(),
                         particlelist1[index].getRotation().getZ2())

    print(pick_position, particle_rotation)

    align_transformation, raw_position, aligned_position, template_transformation = combine_trans_projection(
        align_results, pick_position, particle_rotation, tilt_angle, dim, 8,
        template.shape[0])

    d = 100
    raw_patch = raw_projection[int(raw_position[0] - d):int(raw_position[0] +
                                                            d),
                               int(raw_position[1] - d):int(raw_position[1] +
                                                            d)].squeeze()
    raw_patch = raw_patch / np.mean(raw_patch)

    aligned_patch = aligned_projection[int(aligned_position[0] -
                                           d):int(aligned_position[0] + d),
                                       int(aligned_position[1] -
                                           d):int(aligned_position[1] +
                                                  d)].squeeze()
    aligned_patch = aligned_patch / (np.mean(aligned_patch))

    aligned_raw = matrix_apply_to_2d(aligned_projection.squeeze(),
                                     align_transformation)
    aligned_raw_patch = aligned_raw[int(raw_position[0] -
                                        d):int(raw_position[0] + d),
                                    int(raw_position[1] -
                                        d):int(raw_position[1] + d)].squeeze()
    aligned_raw_patch = aligned_raw_patch / (np.mean(aligned_raw_patch))

    transformed_template = matrix_apply_to_3d_3x3(template,
                                                  template_transformation)
    print(np.mean(transformed_template), np.mean(template))
    template_2d = transformed_template.sum(axis=2)
    template_2d = template_2d / np.mean(template_2d)
    print(template_2d.shape)

    template_vol = rotate3d(template,
                            phi=particle_rotation[0],
                            the=particle_rotation[1],
                            psi=particle_rotation[2])
    template_vol = rotate3d(template_vol, the=tilt_angle)
    template_vol_2d = template_vol.sum(axis=2)

    from pytom.reconstruction.reconstruct_local_alignment import normalised_cross_correlation_numpy, find_sub_pixel_max_value_2d

    raw_cc = normalised_cross_correlation_numpy(raw_patch, template_2d)
    rx, ry, _ = find_sub_pixel_max_value_2d(raw_cc)

    aligned_cc = normalised_cross_correlation_numpy(aligned_patch,
                                                    template_vol_2d)
    tx, ty, _ = find_sub_pixel_max_value_2d(aligned_cc)

    import pylab as pl
    import scipy

    f, ax = pl.subplots(2, 3, figsize=(15, 10))

    for i in range(2):
        for j in range(3):
            ax[i][j].axis('off')

    ax[0][0].set_title('Raw Data Particle')
    ax[0][0].imshow(scipy.ndimage.gaussian_filter(raw_patch, 3))
    ax[0][1].set_title('Template Transformed to Raw Data')
    ax[0][1].imshow(template_2d)
    ax[0][2].set_title('Cross Correlation')
    ax[0][2].imshow(raw_cc)
    ax[0][2].text(0.05,
                  0.05,
                  'Peak\nx: {0:.2f}\ny: {0:.2f}'.format(rx, ry),
                  transform=ax[0][2].transAxes,
                  color='white')
    ax[1][0].set_title('Aligned Data Particle')
    ax[1][0].imshow(scipy.ndimage.gaussian_filter(aligned_patch, 3))
    ax[1][1].set_title('Template Aligned to Aligned Data')
    ax[1][1].imshow(template_vol_2d)
    ax[1][2].set_title('Cross Correlation')
    ax[1][2].imshow(aligned_cc)
    ax[1][2].text(0.05,
                  0.05,
                  'Peak\nx: {0:.2f}\ny: {0:.2f}'.format(tx, ty),
                  transform=ax[1][2].transAxes,
                  color='white')
    f.tight_layout()
    pl.show()
Exemplo n.º 25
0
def polish_particles(particle_list_filename,
                     projection_directory,
                     averaged_subtomogram,
                     binning,
                     offset,
                     projections,
                     tilt_angles,
                     fsc_path='',
                     peak_border=75,
                     outputDirectory='./',
                     create_graphics=False,
                     number_of_particles=0,
                     verbose=False,
                     gpuID=-1):
    """
    To polish a particle list based on (an) initial subtomogram(s).

    :param particle_list_filename: the filename of the particlelist
    :type particle_list_filename: str
    :param projection_directory: the directory of the projections
    :type projection_directory: str
    :param averaged_subtomogram: to give a path to an averaged subtomogram to be used instead of subtomograms of all
               particles separately
    :type averaged_subtomogram: str
    :param binning: the binning factor used
    :type binning: int
    :param offset: the offset used (x, y, z)
    :type offset: list(int, int, int)

    :param projections: a list with filenames of projections
    :type projections: list(str)
    :param tilt_angles: the list of tiltangles used
    :type tilt_angles: list(int)
    :param create_graphics: to create plots of major parts of the algorithm, mainly used for debugging
               and initial creation
    :type create_graphics: bool
    :param number_of_particles: to use a subset of the particles for the particle polishing
    :type number_of_particles: int
    :param skip_alignment: skips the alignment phase, does not do particle polishing
    :type skip_alignment: bool

    :return: nothing, it writes everything to disk
    :returntype: void
    """
    assert number_of_particles == -1 or number_of_particles > 0
    assert binning > 0
    assert vol_size > 0
    assert vol_size % 2 == 0
    assert isinstance(projections, list)
    assert isinstance(vol_size, int)
    assert isinstance(binning, int)
    assert isinstance(offset, list) and len(offset) == 3
    assert isinstance(offset[0], int) and isinstance(
        offset[1], int) and isinstance(offset[2], int)
    assert isinstance(tilt_angles, list)
    assert isinstance(particle_list_filename, str)
    assert isinstance(projection_directory, str)
    assert isinstance(create_graphics, bool)
    assert isinstance(averaged_subtomogram, str)
    assert isinstance(number_of_particles, int)
    assert isinstance(skip_alignment, bool)

    import os, time
    from pytom.tompy.io import read_size, read
    from pytom.gui.guiFunctions import fmtLAR, headerLocalAlignmentResults, LOCAL_ALIGNMENT_RESULTS
    import pytom.voltools as vt

    # load particle list
    from pytom.basic.structures import ParticleList

    particlelist = ParticleList()
    particlelist.fromXMLFile(particle_list_filename)
    particle_list_name = os.path.splitext(
        os.path.basename(str(particle_list_filename)))[0]
    if number_of_particles > 0:
        particlelist = particlelist[:number_of_particles]

    if verbose:
        print(len(particlelist))
        print("{:s}> Creating the input array".format(gettime()))

    dimz = read_size(particlelist[0].getPickPosition().getOriginFilename(),
                     'z') * binning
    vol_size = 200
    input_to_processes = []

    data = {}

    for projectioname in projections:
        data[projectioname] = xp.array(read(projectioname))


#
    template1 = read(averaged_subtomogram, order='F')

    #template1 = mrcfile.open(averaged_subtomogram,permissive=True).data.copy().T.copy()

    template = vt.StaticVolume(template1,
                               interpolation='filt_bspline',
                               device=device)

    # output = []
    results_file = os.path.join(outputDirectory,
                                f"resultsPolish_{particle_list_name}.txt")

    results = []

    for particle_number, particle in enumerate(particlelist):

        rot = (particle.getRotation().getZ1(), particle.getRotation().getX(),
               particle.getRotation().getZ2())
        # loop over tiltrange, take patch and cross correlate with reprojected subtomogram
        for img, ang in zip(projections, tilt_angles):
            pick_position = particle.getPickPosition().toVector()

            result = run_single_tilt_angle(template, ang, offset, vol_size,
                                           pick_position, rot,
                                           particle.getFilename(),
                                           particle_number, binning, data,
                                           create_graphics, fsc_path, dimz,
                                           peak_border, img,
                                           averaged_subtomogram)
            results.append(tuple(result))

    try:

        np.savetxt(results_file,
                   np.array(results, dtype=LOCAL_ALIGNMENT_RESULTS),
                   fmt=fmtLAR,
                   header=headerLocalAlignmentResults)
    except Exception as e:
        print(e)
        for res in results:
            print('{:7d} {:15.3f} {:15.3f} {:15.3f} {:15.3f} {:15.10f} {:s}'.
                  format(*res))
            break

    if verbose: print("{:s}> Ran the processes".format(gettime()))