# microsig

"""
Author: Maximilliano Rossi
More detail about the MicroSIG can be found at:
Website:
    https://gitlab.com/defocustracking/microsig-python
Publication:
    Rossi M, Synthetic image generator for defocusing and astigmatic PIV/PTV, Meas. Sci. Technol., 31, 017003 (2020)
    DOI:10.1088/1361-6501/ab42bb.
"""

import numpy as np
import imageio
import tkinter as tk
import os
from os import listdir
from os.path import isfile, basename, join, isdir
import sys
import glob
# import time as tm

from tkinter import filedialog

# ----- code adapted by Sean MacKenzie ------

# 2.0 define class
class CurlypivMicrosigCollection(object):

    def __init__(self, testSetup, synCol, use_gui=False,
                 use_internal_setting=False, setting_file=None,
                 use_internal_data=False, data_files=None,
                 to_internal_sequence=False, destination_folder=None,
                 output_dtype='np.uint16'):

        if not isinstance(testSetup, object):
            raise ValueError("{} must be a CurlypivTestSetup class object".format(testSetup))

        if not isinstance(synCol, object):
            raise ValueError("{} must be a CurlypivSyntheticCollection class object".format(synCol))

        valid_output_dtype = ['np.uint16', 'np.uint8']
        if output_dtype not in valid_output_dtype:
            raise ValueError("{} must be one of {}".format(output_dtype, valid_output_dtype))

        self.testSetup = testSetup
        self.synCol = synCol
        self.use_gui = use_gui
        self.output_dtype = output_dtype

        if self.use_gui:
            run()
        else:
            if use_internal_setting:
                self.setting_file = self.synCol.microsigSetup
            else:
                if not isinstance(setting_file, str):
                    raise ValueError("{} must be a filepath to microsig settings text file".format(setting_file))
                self.setting_file = os.path.abspath(setting_file)

            if use_internal_data:
                raise ValueError("script to use internal data still in development")
            else:
                if not isinstance(data_files, str):
                    raise ValueError("{} must be a filepath to particle location text files".format(data_files))

                all_files = glob.glob(data_files + '/*.txt')
                save_files = []
                for ff in [f for f in all_files if f.endswith('.txt')]:
                    save_files.append(ff)
                save_files.sort()
                self.data_files = save_files

            if to_internal_sequence:
                raise ValueError("script to use internal data still in development")
            else:
                if not isinstance(destination_folder, str):
                    raise ValueError("{} must be a filepath to write output images".format(destination_folder))
                self.destination_folder = os.path.abspath(destination_folder)

            self.generate()

    def generate(self):
        # %%
        mic = {}
        f = open(self.setting_file)
        for x in f:
            words = x.split()
            mic[words[0]] = float(words[2])
        mic['pixel_dim_x'] = int(mic['pixel_dim_x'])
        mic['pixel_dim_y'] = int(mic['pixel_dim_y'])
        mic['n_rays'] = int(mic['n_rays'])

        # %%
        ii = 0;
        ii_tot = len(self.data_files)
        for data in self.data_files:
            ii = ii + 1
            print('creating image {0} of {1} ...'.format(ii, ii_tot))
            P = np.genfromtxt(data)
            if len(P.shape) == 1:
                P = np.array([P])

            head, tail = os.path.split(data)
            I = take_image(mic, P)

            if self.output_dtype == 'np.uint16':
                imageio.imwrite(os.path.join(self.destination_folder, (tail[:-3] + 'tif')),
                            np.uint16(I))
            elif self.output_dtype == 'np.uint8':
                imageio.imwrite(os.path.join(self.destination_folder, (tail[:-3] + 'tif')),
                            np.uint8(I))

        print('done!')


    # %%

def sorter(f):
    sorting = int(f[:-4])
    return sorting

def run():
    # %%
    root = tk.Tk()
    root.attributes('-topmost', True)
    root.withdraw()

    setting_file = filedialog.askopenfilenames(
        title="Select settings file", parent=root,
        filetypes=(("txt files", "*.txt"), ("all files", "*.*")))

    if not setting_file:
        sys.exit('input file not valid')

    data_files = filedialog.askopenfilenames(
        title="Select data file(s)", parent=root,
        filetypes=(("txt files", "*.txt"), ("all files", "*.*")))

    if not setting_file:
        sys.exit('input file not valid')

    destination_folder = filedialog.askdirectory(
        title="Select destination file", parent=root)

    if not setting_file:
        sys.exit('input file not valid')

    # %%
    mic = {}
    f = open(setting_file[0])
    for x in f:
        words = x.split()
        mic[words[0]] = float(words[2])
    mic['pixel_dim_x'] = int(mic['pixel_dim_x'])
    mic['pixel_dim_y'] = int(mic['pixel_dim_y'])
    mic['n_rays'] = int(mic['n_rays'])

    # %%
    ii = 0;
    ii_tot = len(data_files)
    for data in data_files:
        ii = ii + 1
        print('creating image {0} of {1} ...'.format(ii, ii_tot))
        P = np.genfromtxt(data)
        if len(P.shape) == 1:
            P = np.array([P])

        head, tail = os.path.split(data)
        I = take_image(mic, P)

    print('done!')


# %%

def take_image(mic, P):
    # NOTE: x and xp represent here light fields and should not be confused$
    # with particle image coordinates which are represented by P

    I = np.zeros((mic['pixel_dim_y'], mic['pixel_dim_x']));

    dp_s = np.unique(P[:, 3])
    if P.shape[1] == 5 or P.shape[1] == 8:
        k_id = P[:, -1]
    else:
        k_id = np.ones(P.shape[0])

    if P.shape[1] <= 5 and dp_s.size == 1:

        n_points = int(np.round(mic['points_per_pixel'] * 2 * np.pi *
                                (dp_s * mic['magnification'] / mic['pixel_size']) ** 2))
        xp = create_particle(dp_s, n_points, mic['n_rays'])

        for ii in range(0, P.shape[0]):
            Id = image_spherical(mic, xp, P[ii, 0:3])
            I = I + Id * k_id[ii]

    elif P.shape[1] <= 5 and dp_s.size != 1:

        for ii in range(0, P.shape[0]):
            n_points = int(np.round(mic['points_per_pixel'] * 2 * np.pi *
                                    (P[ii, 3] * mic['magnification'] / mic['pixel_size']) ** 2))
            xp = create_particle(P[ii, 3], n_points, mic['n_rays'])

            Id = image_spherical(mic, xp, P[ii, 0:3])
            I = I + Id * k_id[ii]


    elif P.shape[1] >= 7:

        for ii in range(0, P.shape[0]):
            n_points = int(np.round(mic['points_per_pixel'] * 2 * np.pi *
                                    (P[ii, 3] * mic['magnification'] / mic['pixel_size']) ** 2))

            ecc = P[ii, 4]
            if ecc > 1:
                # area elipsoid/area sphere
                fact = 1 / 2 * (1 + ecc / np.sqrt(1 - 1 / ecc ** 2)
                                * np.arcsin(np.sqrt(1 - 1 / ecc ** 2)))
                n_points = int(np.round(fact * n_points))
            elif ecc < 1:
                # area elipsoid/area sphere
                fact = 1 / 2 * (1 + ecc ** 2 / np.sqrt(1 - ecc ** 2)
                                * np.arctan(np.sqrt(1 - ecc ** 2)))
                n_points = int(np.round(fact * n_points))

            xp = create_ellipsoid(P[ii, 3:7], n_points, mic['n_rays'])
            Id = image_spherical(mic, xp, P[ii, 0:3]);
            I = I + Id * k_id[ii]

    I = I * mic['gain']

    if mic['background_mean'] != 0:
        I = I + mic['background_mean']

    if mic['background_noise'] != 0:
        Irand = np.random.normal(0, mic['background_noise'],
                                 (mic['pixel_dim_y'], mic['pixel_dim_x']))
        I = I + np.round(Irand)
    #        I = np.round(I+random('norm',0,mic.background_noise,...
    #            mic.pixel_dim_y,mic.pixel_dim_x));

    return I


# %%
def image_spherical(mic, xp, P1):
    # take image of a particle with a spherical lens
    # NOTE: x and xp represent here light fields and should not be confused$
    # with particle image coordinates which are represented by P1

    lens_radius = (np.tan(np.arcsin(mic['numerical_aperture']))
                   * (1 + 1 / mic['magnification']) * mic['focal_length'])
    # distance lens-ccd
    dCCD = -mic['focal_length'] * (mic['magnification'] + 1);
    # distance particle-lens
    dPART = P1[2] + mic['focal_length'] * (1 / mic['magnification'] + 1);

    # linear transformation from the object plane to the lens plane
    T2 = np.array([[1, 0, dPART, 0],
                   [0, 1, 0, dPART],
                   [0, 0, 1, 0],
                   [0, 0, 0, 1]])

    # light field right before the lens
    x = np.linalg.inv(T2) @ xp

    # remove rays outside of the lens aperture
    ind = x[0, :] ** 2 + x[1, :] ** 2 <= lens_radius ** 2
    x = x[:, ind]

    # transformation of the light field with spherical lens
    a = x[0, :];
    b = x[1, :]
    c = x[2, :];
    d = x[3, :]
    # radius of curvature of the lens
    rk = mic['focal_length'] * (mic['ri_lens'] / mic['ri_medium'] - 1) * 2
    dum = a * 0
    # refraction medium-lens
    # ray-vector befor lens
    Vr = np.vstack((1 + dum, c, d))
    Vr = (Vr / np.tile(np.sqrt(sum(Vr ** 2)), (3, 1)))
    # normal-vector to the lens surface
    Vl = np.vstack((rk + dum, a, b))
    Vl = (Vl / np.tile(np.sqrt(sum(Vl ** 2)), (3, 1)))
    # tangent-vector to the lens surface
    Vrot = np.cross(Vr, Vl, axisa=0, axisb=0)
    Vrot = np.cross(Vrot, Vl, axisa=1, axisb=0).transpose()
    Vrot = Vrot / np.tile(np.sqrt(sum(Vrot ** 2)), (3, 1))
    # angle after snell-law correction
    vx = np.sum(Vr * Vl, axis=0)  # dot product!
    vy = np.sum(Vr * Vrot, axis=0)  # dot product!
    th11 = np.arcsin(mic['ri_medium'] / mic['ri_lens'] *
                     np.sin(np.arctan(vy / vx)))
    # new ray-vector inside the lens
    Vr11 = (Vl * np.tile(np.cos(th11), (3, 1)) +
            Vrot * np.tile(np.sin(th11), (3, 1)))
    Vr = Vr11 / np.tile(Vr11[0, :], (3, 1))
    # refraction lens-medium
    # normal-vector to the lens surface
    Vl2 = np.vstack((Vl[0, :], -Vl[1:, :]))
    # tangent-vector to the lens surface
    Vrot = np.cross(Vr, Vl2, axisa=0, axisb=0)
    Vrot = np.cross(Vrot, Vl2, axisa=1, axisb=0).transpose()
    Vrot = Vrot / np.tile(np.sqrt(sum(Vrot ** 2)), (3, 1))
    # angle after snell-law correction
    vx = np.sum(Vr * Vl2, axis=0)  # dot product!
    vy = np.sum(Vr * Vrot, axis=0)  # dot product!

    th11 = np.arcsin(mic['ri_lens'] / mic['ri_medium'] *
                     np.sin(np.arctan(vy / vx)))
    # new ray-vector outside the lens
    Vr11 = (Vl2 * np.tile(np.cos(th11), (3, 1)) +
            Vrot * np.tile(np.sin(th11), (3, 1)))
    Vr = Vr11 / np.tile(Vr11[0, :], (3, 1))
    # light field after the spherical lens
    x[2, :] = Vr[1, :]
    x[3, :] = Vr[2, :]

    if mic['cyl_focal_length'] == 0:
        #     linear transformation from the lens plane to the ccd plane
        T1 = np.array([[1, 0, -dCCD, 0],
                       [0, 1, 0, -dCCD],
                       [0, 0, 1, 0],
                       [0, 0, 0, 1]])
        #     light field at the ccd plane
        xs = np.linalg.inv(T1) @ x
    else:
        #    #     linear transformation from the lens plane to the cyl_lens plane
        T1c = np.array([[1, 0, -dCCD * 1 / 3, 0],
                        [0, 1, 0, -dCCD * 1 / 3],
                        [0, 0, 1, 0],
                        [0, 0, 0, 1]])
        #    #     light field at the cylindrical lens plane
        xc = np.linalg.inv(T1c) @ x
        #    #     light field after the cylindrical lens plane
        Tc = np.array([[1, 0, 0, 0],
                       [0, 1, 0, 0],
                       [-1 / mic['cyl_focal_length'], 0, 1, 0],
                       [0, 0, 0, 1]])
        xc_a = np.linalg.inv(Tc) @ xc
        #    #     light field at the ccd plane
        T1 = np.array([[1, 0, -dCCD * 2 / 3, 0],
                       [0, 1, 0, -dCCD * 2 / 3],
                       [0, 0, 1, 0],
                       [0, 0, 0, 1]]);
        #    #     light field at the ccd plane
        xs = np.linalg.inv(T1) @ xc_a

    # transform the position in pixel units
    X = np.round(xs[0, :] / mic['pixel_size'] + P1[0])
    Y = np.round(xs[1, :] / mic['pixel_size'] + P1[1])

    # remove rays outside the CCD
    ind = np.all([X > 0, X <= mic['pixel_dim_x'], Y > 0, Y <= mic['pixel_dim_y'],
                  X.imag == 0, Y.imag == 0], axis=0)

    # count number of rays in each pixel
    countXY = np.sort(Y[ind] + (X[ind] - 1) * mic['pixel_dim_y'])
    indi, ia = np.unique(countXY, return_index=True)
    nCounts = np.hstack((ia[1:], countXY.size + 1)) - ia

    # prepare image
    I = np.zeros((mic['pixel_dim_y'], mic['pixel_dim_x']))
    Ifr = I.flatten('F')
    Ifr[indi.astype(int) - 1] = nCounts
    I = Ifr.reshape(mic['pixel_dim_y'], mic['pixel_dim_x'], order='F')

    return I


# %%
def create_particle(D, Ns, Nr):
    R = D / 2

    V = spiral_sphere(Ns)
    V[0:2, V[0, :] > 0] = -V[0:2, V[0, :] > 0]
    x = R * V[0, :]
    y = R * V[1, :]
    z = R * V[2, :]

    V0 = spiral_sphere(Nr + 2)
    V0 = V0[:, 1:-1]
    u = np.tile(x, (Nr, 1))
    v = np.tile(y, (Nr, 1))
    s = u * 0
    t = u * 0

    phs = np.random.uniform(-np.pi, np.pi, z.size)
    cs = np.cos(phs)
    sn = np.sin(phs)
    for k in range(0, Ns):
        Rot = np.array([[cs[k], -sn[k], 0],
                        [sn[k], cs[k], 0], [0, 0, 1]])
        Vr = Rot @ V0
        Vr[0, :] = -abs(Vr[0, :])
        s[:, k] = Vr[1, :] / Vr[0, :]
        t[:, k] = Vr[2, :] / Vr[0, :]
        u[:, k] = y[k] - s[:, k] * x[k]
        v[:, k] = z[k] - t[:, k] * x[k]

    xp = np.vstack((u.flatten('F'), v.flatten('F'),
                    s.flatten('F'), t.flatten('F')))

    return xp


# %%
def create_ellipsoid(Deab, Ns, Nr):
    D = Deab[0];
    ecc = Deab[1]
    alpha = Deab[2];
    beta = Deab[3]

    R = D / 2

    V = spiral_sphere(Ns)
    V = R * V
    V[2, :] = V[2, :] * ecc

    R_beta = np.array([[np.cos(beta), 0, np.sin(beta)],
                       [0, 1, 0],
                       [-np.sin(beta), 0, np.cos(beta)]])
    R_alpha = np.array([[np.cos(alpha), -np.sin(alpha), 0],
                        [np.sin(alpha), np.cos(alpha), 0],
                        [0, 0, 1]])

    Vf = R_alpha @ (R_beta @ V)

    ii1 = (Vf[1, :] == np.min(Vf[1, :])).nonzero()[0][0]
    ii2 = (Vf[1, :] == np.max(Vf[1, :])).nonzero()[0][0]
    ii3 = (Vf[2, :] == np.min(Vf[2, :])).nonzero()[0][0]
    ii4 = (Vf[2, :] == np.max(Vf[2, :])).nonzero()[0][0]
    Vdum = Vf[:, [ii1, ii2, ii3, ii4]]

    A = np.c_[Vdum[1, :], Vdum[2, :], np.ones(Vdum.shape[1])]
    C, _, _, _ = np.linalg.lstsq(A, Vdum[0, :], rcond=None)
    V1dum = C[0] * Vf[1, :] + C[1] * Vf[2, :] + C[2]
    ind = (Vf[0, :] - V1dum) < 0
    x = Vf[0, ind]
    y = Vf[1, ind]
    z = Vf[2, ind]
    Ns = z.size

    V0 = spiral_sphere(Nr + 2)
    V0 = V0[:, 1:-1]
    u = np.tile(x, (Nr, 1))
    v = np.tile(y, (Nr, 1))
    s = u * 0
    t = u * 0

    phs = np.random.uniform(-np.pi, np.pi, z.size)
    cs = np.cos(phs)
    sn = np.sin(phs)
    for k in range(0, Ns):
        Rot = np.array([[cs[k], -sn[k], 0],
                        [sn[k], cs[k], 0], [0, 0, 1]])
        Vr = Rot @ V0
        Vr[0, :] = -abs(Vr[0, :])
        s[:, k] = Vr[1, :] / Vr[0, :]
        t[:, k] = Vr[2, :] / Vr[0, :]
        u[:, k] = y[k] - s[:, k] * x[k]
        v[:, k] = z[k] - t[:, k] * x[k]

    xp = np.vstack((u.flatten('F'), v.flatten('F'),
                    s.flatten('F'), t.flatten('F')))

    return xp


# %%
def spiral_sphere(N):
    gr = (1 + np.sqrt(5)) / 2  # golden ratio
    ga = 2 * np.pi * (1 - 1 / gr)  # golden angle

    ind_p = np.arange(0, N)  # particle (i.e., point sample) index
    lat = np.arccos(1 - 2 * ind_p / (
                N - 1))  # latitude is defined so that particle index is proportional to surface area between 0 and lat
    lon = ind_p * ga  # position particles at even intervals along longitude

    # Convert from spherical to Cartesian co-ordinates
    x = np.sin(lat) * np.cos(lon)
    y = np.sin(lat) * np.sin(lon)
    z = np.cos(lat)
    V = np.vstack((x, y, z))

    return V


# %%


if __name__ == '__main__':
    run()




[((4197, 4204), 'tkinter.Tk', 'tk.Tk', ([], {}), '()\n', (4202, 4204), True, 'import tkinter as tk\n'), ((4283, 4415), 'tkinter.filedialog.askopenfilenames', 'filedialog.askopenfilenames', ([], {'title': '"""Select settings file"""', 'parent': 'root', 'filetypes': "(('txt files', '*.txt'), ('all files', '*.*'))"}), "(title='Select settings file', parent=root,\n    filetypes=(('txt files', '*.txt'), ('all files', '*.*')))\n", (4310, 4415), False, 'from tkinter import filedialog\n'), ((4514, 4645), 'tkinter.filedialog.askopenfilenames', 'filedialog.askopenfilenames', ([], {'title': '"""Select data file(s)"""', 'parent': 'root', 'filetypes': "(('txt files', '*.txt'), ('all files', '*.*'))"}), "(title='Select data file(s)', parent=root,\n    filetypes=(('txt files', '*.txt'), ('all files', '*.*')))\n", (4541, 4645), False, 'from tkinter import filedialog\n'), ((4752, 4821), 'tkinter.filedialog.askdirectory', 'filedialog.askdirectory', ([], {'title': '"""Select destination file"""', 'parent': 'root'}), "(title='Select destination file', parent=root)\n", (4775, 4821), False, 'from tkinter import filedialog\n'), ((5703, 5753), 'numpy.zeros', 'np.zeros', (["(mic['pixel_dim_y'], mic['pixel_dim_x'])"], {}), "((mic['pixel_dim_y'], mic['pixel_dim_x']))\n", (5711, 5753), True, 'import numpy as np\n'), ((5767, 5787), 'numpy.unique', 'np.unique', (['P[:, (3)]'], {}), '(P[:, (3)])\n', (5776, 5787), True, 'import numpy as np\n'), ((8752, 8826), 'numpy.array', 'np.array', (['[[1, 0, dPART, 0], [0, 1, 0, dPART], [0, 0, 1, 0], [0, 0, 0, 1]]'], {}), '([[1, 0, dPART, 0], [0, 1, 0, dPART], [0, 0, 1, 0], [0, 0, 0, 1]])\n', (8760, 8826), True, 'import numpy as np\n'), ((9402, 9428), 'numpy.vstack', 'np.vstack', (['(1 + dum, c, d)'], {}), '((1 + dum, c, d))\n', (9411, 9428), True, 'import numpy as np\n'), ((9533, 9560), 'numpy.vstack', 'np.vstack', (['(rk + dum, a, b)'], {}), '((rk + dum, a, b))\n', (9542, 9560), True, 'import numpy as np\n'), ((9668, 9702), 'numpy.cross', 'np.cross', (['Vr', 'Vl'], {'axisa': '(0)', 'axisb': '(0)'}), '(Vr, Vl, axisa=0, axisb=0)\n', (9676, 9702), True, 'import numpy as np\n'), ((9870, 9893), 'numpy.sum', 'np.sum', (['(Vr * Vl)'], {'axis': '(0)'}), '(Vr * Vl, axis=0)\n', (9876, 9893), True, 'import numpy as np\n'), ((9919, 9944), 'numpy.sum', 'np.sum', (['(Vr * Vrot)'], {'axis': '(0)'}), '(Vr * Vrot, axis=0)\n', (9925, 9944), True, 'import numpy as np\n'), ((10326, 10361), 'numpy.vstack', 'np.vstack', (['(Vl[(0), :], -Vl[1:, :])'], {}), '((Vl[(0), :], -Vl[1:, :]))\n', (10335, 10361), True, 'import numpy as np\n'), ((10412, 10447), 'numpy.cross', 'np.cross', (['Vr', 'Vl2'], {'axisa': '(0)', 'axisb': '(0)'}), '(Vr, Vl2, axisa=0, axisb=0)\n', (10420, 10447), True, 'import numpy as np\n'), ((10616, 10640), 'numpy.sum', 'np.sum', (['(Vr * Vl2)'], {'axis': '(0)'}), '(Vr * Vl2, axis=0)\n', (10622, 10640), True, 'import numpy as np\n'), ((10666, 10691), 'numpy.sum', 'np.sum', (['(Vr * Vrot)'], {'axis': '(0)'}), '(Vr * Vrot, axis=0)\n', (10672, 10691), True, 'import numpy as np\n'), ((12438, 12486), 'numpy.round', 'np.round', (["(xs[(0), :] / mic['pixel_size'] + P1[0])"], {}), "(xs[(0), :] / mic['pixel_size'] + P1[0])\n", (12446, 12486), True, 'import numpy as np\n'), ((12493, 12541), 'numpy.round', 'np.round', (["(xs[(1), :] / mic['pixel_size'] + P1[1])"], {}), "(xs[(1), :] / mic['pixel_size'] + P1[1])\n", (12501, 12541), True, 'import numpy as np\n'), ((12585, 12696), 'numpy.all', 'np.all', (["[X > 0, X <= mic['pixel_dim_x'], Y > 0, Y <= mic['pixel_dim_y'], X.imag == \n    0, Y.imag == 0]"], {'axis': '(0)'}), "([X > 0, X <= mic['pixel_dim_x'], Y > 0, Y <= mic['pixel_dim_y'], X.\n    imag == 0, Y.imag == 0], axis=0)\n", (12591, 12696), True, 'import numpy as np\n'), ((12766, 12817), 'numpy.sort', 'np.sort', (["(Y[ind] + (X[ind] - 1) * mic['pixel_dim_y'])"], {}), "(Y[ind] + (X[ind] - 1) * mic['pixel_dim_y'])\n", (12773, 12817), True, 'import numpy as np\n'), ((12833, 12870), 'numpy.unique', 'np.unique', (['countXY'], {'return_index': '(True)'}), '(countXY, return_index=True)\n', (12842, 12870), True, 'import numpy as np\n'), ((12957, 13007), 'numpy.zeros', 'np.zeros', (["(mic['pixel_dim_y'], mic['pixel_dim_x'])"], {}), "((mic['pixel_dim_y'], mic['pixel_dim_x']))\n", (12965, 13007), True, 'import numpy as np\n'), ((13406, 13425), 'numpy.tile', 'np.tile', (['x', '(Nr, 1)'], {}), '(x, (Nr, 1))\n', (13413, 13425), True, 'import numpy as np\n'), ((13434, 13453), 'numpy.tile', 'np.tile', (['y', '(Nr, 1)'], {}), '(y, (Nr, 1))\n', (13441, 13453), True, 'import numpy as np\n'), ((13493, 13533), 'numpy.random.uniform', 'np.random.uniform', (['(-np.pi)', 'np.pi', 'z.size'], {}), '(-np.pi, np.pi, z.size)\n', (13510, 13533), True, 'import numpy as np\n'), ((13543, 13554), 'numpy.cos', 'np.cos', (['phs'], {}), '(phs)\n', (13549, 13554), True, 'import numpy as np\n'), ((13564, 13575), 'numpy.sin', 'np.sin', (['phs'], {}), '(phs)\n', (13570, 13575), True, 'import numpy as np\n'), ((14922, 14966), 'numpy.linalg.lstsq', 'np.linalg.lstsq', (['A', 'Vdum[(0), :]'], {'rcond': 'None'}), '(A, Vdum[(0), :], rcond=None)\n', (14937, 14966), True, 'import numpy as np\n'), ((15185, 15204), 'numpy.tile', 'np.tile', (['x', '(Nr, 1)'], {}), '(x, (Nr, 1))\n', (15192, 15204), True, 'import numpy as np\n'), ((15213, 15232), 'numpy.tile', 'np.tile', (['y', '(Nr, 1)'], {}), '(y, (Nr, 1))\n', (15220, 15232), True, 'import numpy as np\n'), ((15272, 15312), 'numpy.random.uniform', 'np.random.uniform', (['(-np.pi)', 'np.pi', 'z.size'], {}), '(-np.pi, np.pi, z.size)\n', (15289, 15312), True, 'import numpy as np\n'), ((15322, 15333), 'numpy.cos', 'np.cos', (['phs'], {}), '(phs)\n', (15328, 15333), True, 'import numpy as np\n'), ((15343, 15354), 'numpy.sin', 'np.sin', (['phs'], {}), '(phs)\n', (15349, 15354), True, 'import numpy as np\n'), ((15952, 15967), 'numpy.arange', 'np.arange', (['(0)', 'N'], {}), '(0, N)\n', (15961, 15967), True, 'import numpy as np\n'), ((16017, 16051), 'numpy.arccos', 'np.arccos', (['(1 - 2 * ind_p / (N - 1))'], {}), '(1 - 2 * ind_p / (N - 1))\n', (16026, 16051), True, 'import numpy as np\n'), ((16374, 16385), 'numpy.cos', 'np.cos', (['lat'], {}), '(lat)\n', (16380, 16385), True, 'import numpy as np\n'), ((16394, 16414), 'numpy.vstack', 'np.vstack', (['(x, y, z)'], {}), '((x, y, z))\n', (16403, 16414), True, 'import numpy as np\n'), ((4463, 4495), 'sys.exit', 'sys.exit', (['"""input file not valid"""'], {}), "('input file not valid')\n", (4471, 4495), False, 'import sys\n'), ((4693, 4725), 'sys.exit', 'sys.exit', (['"""input file not valid"""'], {}), "('input file not valid')\n", (4701, 4725), False, 'import sys\n'), ((4865, 4897), 'sys.exit', 'sys.exit', (['"""input file not valid"""'], {}), "('input file not valid')\n", (4873, 4897), False, 'import sys\n'), ((5347, 5366), 'numpy.genfromtxt', 'np.genfromtxt', (['data'], {}), '(data)\n', (5360, 5366), True, 'import numpy as np\n'), ((5449, 5468), 'os.path.split', 'os.path.split', (['data'], {}), '(data)\n', (5462, 5468), False, 'import os\n'), ((5878, 5897), 'numpy.ones', 'np.ones', (['P.shape[0]'], {}), '(P.shape[0])\n', (5885, 5897), True, 'import numpy as np\n'), ((7821, 7912), 'numpy.random.normal', 'np.random.normal', (['(0)', "mic['background_noise']", "(mic['pixel_dim_y'], mic['pixel_dim_x'])"], {}), "(0, mic['background_noise'], (mic['pixel_dim_y'], mic[\n    'pixel_dim_x']))\n", (7837, 7912), True, 'import numpy as np\n'), ((8933, 8950), 'numpy.linalg.inv', 'np.linalg.inv', (['T2'], {}), '(T2)\n', (8946, 8950), True, 'import numpy as np\n'), ((10219, 10248), 'numpy.tile', 'np.tile', (['Vr11[(0), :]', '(3, 1)'], {}), '(Vr11[(0), :], (3, 1))\n', (10226, 10248), True, 'import numpy as np\n'), ((10969, 10998), 'numpy.tile', 'np.tile', (['Vr11[(0), :]', '(3, 1)'], {}), '(Vr11[(0), :], (3, 1))\n', (10976, 10998), True, 'import numpy as np\n'), ((11210, 11284), 'numpy.array', 'np.array', (['[[1, 0, -dCCD, 0], [0, 1, 0, -dCCD], [0, 0, 1, 0], [0, 0, 0, 1]]'], {}), '([[1, 0, -dCCD, 0], [0, 1, 0, -dCCD], [0, 0, 1, 0], [0, 0, 0, 1]])\n', (11218, 11284), True, 'import numpy as np\n'), ((11539, 11633), 'numpy.array', 'np.array', (['[[1, 0, -dCCD * 1 / 3, 0], [0, 1, 0, -dCCD * 1 / 3], [0, 0, 1, 0], [0, 0, 0, 1]\n    ]'], {}), '([[1, 0, -dCCD * 1 / 3, 0], [0, 1, 0, -dCCD * 1 / 3], [0, 0, 1, 0],\n    [0, 0, 0, 1]])\n', (11547, 11633), True, 'import numpy as np\n'), ((11876, 11974), 'numpy.array', 'np.array', (["[[1, 0, 0, 0], [0, 1, 0, 0], [-1 / mic['cyl_focal_length'], 0, 1, 0], [0, 0,\n    0, 1]]"], {}), "([[1, 0, 0, 0], [0, 1, 0, 0], [-1 / mic['cyl_focal_length'], 0, 1, \n    0], [0, 0, 0, 1]])\n", (11884, 11974), True, 'import numpy as np\n'), ((12138, 12232), 'numpy.array', 'np.array', (['[[1, 0, -dCCD * 2 / 3, 0], [0, 1, 0, -dCCD * 2 / 3], [0, 0, 1, 0], [0, 0, 0, 1]\n    ]'], {}), '([[1, 0, -dCCD * 2 / 3, 0], [0, 1, 0, -dCCD * 2 / 3], [0, 0, 1, 0],\n    [0, 0, 0, 1]])\n', (12146, 12232), True, 'import numpy as np\n'), ((12885, 12922), 'numpy.hstack', 'np.hstack', (['(ia[1:], countXY.size + 1)'], {}), '((ia[1:], countXY.size + 1))\n', (12894, 12922), True, 'import numpy as np\n'), ((13617, 13677), 'numpy.array', 'np.array', (['[[cs[k], -sn[k], 0], [sn[k], cs[k], 0], [0, 0, 1]]'], {}), '([[cs[k], -sn[k], 0], [sn[k], cs[k], 0], [0, 0, 1]])\n', (13625, 13677), True, 'import numpy as np\n'), ((15396, 15456), 'numpy.array', 'np.array', (['[[cs[k], -sn[k], 0], [sn[k], cs[k], 0], [0, 0, 1]]'], {}), '([[cs[k], -sn[k], 0], [sn[k], cs[k], 0], [0, 0, 1]])\n', (15404, 15456), True, 'import numpy as np\n'), ((16306, 16317), 'numpy.sin', 'np.sin', (['lat'], {}), '(lat)\n', (16312, 16317), True, 'import numpy as np\n'), ((16320, 16331), 'numpy.cos', 'np.cos', (['lon'], {}), '(lon)\n', (16326, 16331), True, 'import numpy as np\n'), ((16340, 16351), 'numpy.sin', 'np.sin', (['lat'], {}), '(lat)\n', (16346, 16351), True, 'import numpy as np\n'), ((16354, 16365), 'numpy.sin', 'np.sin', (['lon'], {}), '(lon)\n', (16360, 16365), True, 'import numpy as np\n'), ((3533, 3552), 'numpy.genfromtxt', 'np.genfromtxt', (['data'], {}), '(data)\n', (3546, 3552), True, 'import numpy as np\n'), ((3647, 3666), 'os.path.split', 'os.path.split', (['data'], {}), '(data)\n', (3660, 3666), False, 'import os\n'), ((5413, 5426), 'numpy.array', 'np.array', (['[P]'], {}), '([P])\n', (5421, 5426), True, 'import numpy as np\n'), ((5966, 6072), 'numpy.round', 'np.round', (["(mic['points_per_pixel'] * 2 * np.pi * (dp_s * mic['magnification'] / mic[\n    'pixel_size']) ** 2)"], {}), "(mic['points_per_pixel'] * 2 * np.pi * (dp_s * mic['magnification'] /\n    mic['pixel_size']) ** 2)\n", (5974, 6072), True, 'import numpy as np\n'), ((7957, 7972), 'numpy.round', 'np.round', (['Irand'], {}), '(Irand)\n', (7965, 7972), True, 'import numpy as np\n'), ((9714, 9750), 'numpy.cross', 'np.cross', (['Vrot', 'Vl'], {'axisa': '(1)', 'axisb': '(0)'}), '(Vrot, Vl, axisa=1, axisb=0)\n', (9722, 9750), True, 'import numpy as np\n'), ((10459, 10496), 'numpy.cross', 'np.cross', (['Vrot', 'Vl2'], {'axisa': '(1)', 'axisb': '(0)'}), '(Vrot, Vl2, axisa=1, axisb=0)\n', (10467, 10496), True, 'import numpy as np\n'), ((11410, 11427), 'numpy.linalg.inv', 'np.linalg.inv', (['T1'], {}), '(T1)\n', (11423, 11427), True, 'import numpy as np\n'), ((11776, 11794), 'numpy.linalg.inv', 'np.linalg.inv', (['T1c'], {}), '(T1c)\n', (11789, 11794), True, 'import numpy as np\n'), ((12054, 12071), 'numpy.linalg.inv', 'np.linalg.inv', (['Tc'], {}), '(Tc)\n', (12067, 12071), True, 'import numpy as np\n'), ((12360, 12377), 'numpy.linalg.inv', 'np.linalg.inv', (['T1'], {}), '(T1)\n', (12373, 12377), True, 'import numpy as np\n'), ((15857, 15867), 'numpy.sqrt', 'np.sqrt', (['(5)'], {}), '(5)\n', (15864, 15867), True, 'import numpy as np\n'), ((1944, 1973), 'os.path.abspath', 'os.path.abspath', (['setting_file'], {}), '(setting_file)\n', (1959, 1973), False, 'import os\n'), ((2306, 2338), 'glob.glob', 'glob.glob', (["(data_files + '/*.txt')"], {}), "(data_files + '/*.txt')\n", (2315, 2338), False, 'import glob\n'), ((2919, 2954), 'os.path.abspath', 'os.path.abspath', (['destination_folder'], {}), '(destination_folder)\n', (2934, 2954), False, 'import os\n'), ((3607, 3620), 'numpy.array', 'np.array', (['[P]'], {}), '([P])\n', (3615, 3620), True, 'import numpy as np\n'), ((8372, 8408), 'numpy.arcsin', 'np.arcsin', (["mic['numerical_aperture']"], {}), "(mic['numerical_aperture'])\n", (8381, 8408), True, 'import numpy as np\n'), ((10046, 10064), 'numpy.arctan', 'np.arctan', (['(vy / vx)'], {}), '(vy / vx)\n', (10055, 10064), True, 'import numpy as np\n'), ((10129, 10141), 'numpy.cos', 'np.cos', (['th11'], {}), '(th11)\n', (10135, 10141), True, 'import numpy as np\n'), ((10180, 10192), 'numpy.sin', 'np.sin', (['th11'], {}), '(th11)\n', (10186, 10192), True, 'import numpy as np\n'), ((10794, 10812), 'numpy.arctan', 'np.arctan', (['(vy / vx)'], {}), '(vy / vx)\n', (10803, 10812), True, 'import numpy as np\n'), ((10879, 10891), 'numpy.cos', 'np.cos', (['th11'], {}), '(th11)\n', (10885, 10891), True, 'import numpy as np\n'), ((10930, 10942), 'numpy.sin', 'np.sin', (['th11'], {}), '(th11)\n', (10936, 10942), True, 'import numpy as np\n'), ((14262, 14274), 'numpy.cos', 'np.cos', (['beta'], {}), '(beta)\n', (14268, 14274), True, 'import numpy as np\n'), ((14279, 14291), 'numpy.sin', 'np.sin', (['beta'], {}), '(beta)\n', (14285, 14291), True, 'import numpy as np\n'), ((14370, 14382), 'numpy.cos', 'np.cos', (['beta'], {}), '(beta)\n', (14376, 14382), True, 'import numpy as np\n'), ((14411, 14424), 'numpy.cos', 'np.cos', (['alpha'], {}), '(alpha)\n', (14417, 14424), True, 'import numpy as np\n'), ((14471, 14484), 'numpy.sin', 'np.sin', (['alpha'], {}), '(alpha)\n', (14477, 14484), True, 'import numpy as np\n'), ((14486, 14499), 'numpy.cos', 'np.cos', (['alpha'], {}), '(alpha)\n', (14492, 14499), True, 'import numpy as np\n'), ((14881, 14903), 'numpy.ones', 'np.ones', (['Vdum.shape[1]'], {}), '(Vdum.shape[1])\n', (14888, 14903), True, 'import numpy as np\n'), ((3784, 3840), 'os.path.join', 'os.path.join', (['self.destination_folder', "(tail[:-3] + 'tif')"], {}), "(self.destination_folder, tail[:-3] + 'tif')\n", (3796, 3840), False, 'import os\n'), ((3872, 3884), 'numpy.uint16', 'np.uint16', (['I'], {}), '(I)\n', (3881, 3884), True, 'import numpy as np\n'), ((6405, 6516), 'numpy.round', 'np.round', (["(mic['points_per_pixel'] * 2 * np.pi * (P[ii, 3] * mic['magnification'] /\n    mic['pixel_size']) ** 2)"], {}), "(mic['points_per_pixel'] * 2 * np.pi * (P[ii, 3] * mic[\n    'magnification'] / mic['pixel_size']) ** 2)\n", (6413, 6516), True, 'import numpy as np\n'), ((14353, 14365), 'numpy.sin', 'np.sin', (['beta'], {}), '(beta)\n', (14359, 14365), True, 'import numpy as np\n'), ((14427, 14440), 'numpy.sin', 'np.sin', (['alpha'], {}), '(alpha)\n', (14433, 14440), True, 'import numpy as np\n'), ((3968, 4024), 'os.path.join', 'os.path.join', (['self.destination_folder', "(tail[:-3] + 'tif')"], {}), "(self.destination_folder, tail[:-3] + 'tif')\n", (3980, 4024), False, 'import os\n'), ((4056, 4067), 'numpy.uint8', 'np.uint8', (['I'], {}), '(I)\n', (4064, 4067), True, 'import numpy as np\n'), ((6802, 6913), 'numpy.round', 'np.round', (["(mic['points_per_pixel'] * 2 * np.pi * (P[ii, 3] * mic['magnification'] /\n    mic['pixel_size']) ** 2)"], {}), "(mic['points_per_pixel'] * 2 * np.pi * (P[ii, 3] * mic[\n    'magnification'] / mic['pixel_size']) ** 2)\n", (6810, 6913), True, 'import numpy as np\n'), ((14598, 14616), 'numpy.min', 'np.min', (['Vf[(1), :]'], {}), '(Vf[(1), :])\n', (14604, 14616), True, 'import numpy as np\n'), ((14655, 14673), 'numpy.max', 'np.max', (['Vf[(1), :]'], {}), '(Vf[(1), :])\n', (14661, 14673), True, 'import numpy as np\n'), ((14712, 14730), 'numpy.min', 'np.min', (['Vf[(2), :]'], {}), '(Vf[(2), :])\n', (14718, 14730), True, 'import numpy as np\n'), ((14769, 14787), 'numpy.max', 'np.max', (['Vf[(2), :]'], {}), '(Vf[(2), :])\n', (14775, 14787), True, 'import numpy as np\n'), ((7213, 7238), 'numpy.round', 'np.round', (['(fact * n_points)'], {}), '(fact * n_points)\n', (7221, 7238), True, 'import numpy as np\n'), ((7478, 7503), 'numpy.round', 'np.round', (['(fact * n_points)'], {}), '(fact * n_points)\n', (7486, 7503), True, 'import numpy as np\n'), ((7084, 7109), 'numpy.sqrt', 'np.sqrt', (['(1 - 1 / ecc ** 2)'], {}), '(1 - 1 / ecc ** 2)\n', (7091, 7109), True, 'import numpy as np\n'), ((7154, 7179), 'numpy.sqrt', 'np.sqrt', (['(1 - 1 / ecc ** 2)'], {}), '(1 - 1 / ecc ** 2)\n', (7161, 7179), True, 'import numpy as np\n'), ((7357, 7378), 'numpy.sqrt', 'np.sqrt', (['(1 - ecc ** 2)'], {}), '(1 - ecc ** 2)\n', (7364, 7378), True, 'import numpy as np\n'), ((7423, 7444), 'numpy.sqrt', 'np.sqrt', (['(1 - ecc ** 2)'], {}), '(1 - ecc ** 2)\n', (7430, 7444), True, 'import numpy as np\n')]