Ejemplo n.º 1
0
    def h(self):
        if self.illuminate_all:
            return tf.TFCoeffs([[1.0, 0, 0, 0, 0, 0], 6 * [0], 6 * [0]])

        n0 = [
            1 + (self.alpha**2) / 4, 0, 0,
            (-1 + (self.alpha**2) / 2) / np.sqrt(5), 0, 0
        ]
        n_2 = 6 * [0]
        n2 = 6 * [0]
        if self.polarizer:
            n_2 = [0, -np.sqrt(3 / 5), 0, 0, 0, 0]
            n2 = [0, 0, 0, 0, 0, np.sqrt(3 / 5)]
        if self.optical_axis == [1, 0, 0]:  # x-illumination
            n0 = sh.SHCoeffs(n0).rotate().coeffs
            n_2 = sh.SHCoeffs(n_2).rotate().coeffs
            n2 = sh.SHCoeffs(n2).rotate().coeffs

        return tf.TFCoeffs([n0, n_2, n2])
Ejemplo n.º 2
0
    def H(self, x, y, z):
        if self.detect_all:
            return tf.TFCoeffs([[1.0, 0, 0, 0, 0, 0], 6 * [0], 6 * [0]])

        # Find cylindrical coordinates based on view
        if self.optical_axis == [0, 0, 1]:  # z-detection
            nu = np.sqrt(x**2 + y**2)
            phi_nu = np.arctan2(y, x)
            z_ax = z
        elif self.optical_axis == [1, 0, 0]:  # x-detection
            nu = np.sqrt(y**2 + z**2)
            phi_nu = np.arctan2(z, y)
            z_ax = x

        A1 = self.A1
        A2 = self.A2
        n0 = [
            A1(nu) + (self.alpha**2 / 4) * A2(nu), 0, 0,
            (-A1(nu) + (self.alpha**2 / 2) * A2(nu)) / np.sqrt(5), 0, 0
        ]
        n_2 = 6 * [0]
        n2 = 6 * [0]
        if self.polarizer:
            # TODO: Update these (unused for now)
            n_2 = [
                2 * C(nu) * np.sin(2 * phi_nu), -np.sqrt(0.6) * A(nu), 0,
                (4.0 / np.sqrt(5)) * C(nu) * np.sin(2 * phi_nu), 0, 0
            ]
            n2 = [
                2 * C(nu) * np.cos(2 * phi_nu), 0, 0,
                (4.0 / np.sqrt(5)) * C(nu) * np.cos(2 * phi_nu), 0,
                np.sqrt(0.6) * A(nu)
            ]
        if self.optical_axis == [1, 0, 0]:  # x-detection
            n0 = sh.SHCoeffs(n0).rotate().coeffs
            n_2 = sh.SHCoeffs(n_2).rotate().coeffs
            n2 = sh.SHCoeffs(n2).rotate().coeffs

        ax_weight = np.exp(-(z_ax**2) / (2 * (self.sigma_ax**2)))
        print(ax_weight)
        return tf.TFCoeffs([n0, n_2, n2]) * ax_weight
Ejemplo n.º 3
0
    def h(self, x, y, z):
        if self.detect_all:
            return tf.TFCoeffs([[1.0, 0, 0, 0, 0, 0], 6 * [0], 6 * [0]])

        # Find cylindrical coordinates based on view
        if self.optical_axis == [0, 0, 1]:  # z-detection
            r = np.sqrt(x**2 + y**2)
            phi = np.arctan2(y, x)
            z_ax = z
        elif self.optical_axis == [1, 0, 0]:  # x-detection
            r = np.sqrt(y**2 + z**2)
            phi = np.arctan2(z, y)
            z_ax = x

        a1 = self.a1
        a2 = self.a2
        n0 = [
            a1(r) + (self.alpha**2 / 4) * a2(r), 0, 0,
            (-a1(r) + (self.alpha**2 / 2) * a2(r)) / np.sqrt(5), 0, 0
        ]
        n_2 = 6 * [0]
        n2 = 6 * [0]
        if self.polarizer:
            n_2 = [
                2 * a2(r) * np.sin(2 * phi), -np.sqrt(0.6) * a1(r), 0,
                (4.0 / np.sqrt(5)) * a2(r) * np.sin(2 * phi), 0, 0
            ]
            n2 = [
                2 * a2(r) * np.cos(2 * phi), 0, 0,
                (4.0 / np.sqrt(5)) * a2(r) * np.cos(2 * phi), 0,
                np.sqrt(0.6) * a1(r)
            ]
        if self.optical_axis == [1, 0, 0]:  # x-detection
            n0 = sh.SHCoeffs(n0).rotate().coeffs
            n_2 = sh.SHCoeffs(n_2).rotate().coeffs
            n2 = sh.SHCoeffs(n2).rotate().coeffs

        return tf.TFCoeffs([n0, n_2, n2])
Ejemplo n.º 4
0
    def plot_SVS(self,
                 filename='svs.pdf',
                 n_px=2**6,
                 marks=[[1e-5, 0], [0.5, 0], [1.0, 0], [1.5, 0]]):
        print('Plotting: ' + filename)

        # Layout windows
        inches = 1.5
        rows = self.sigma.shape[-1] + 1
        cols = len(marks) + 1
        f, axs = plt.subplots(rows,
                              cols,
                              figsize=(inches * cols, inches * (rows - 0.75)),
                              gridspec_kw={
                                  'hspace': 0.05,
                                  'wspace': 0.10,
                                  'height_ratios': [1, 1, 1, 0.05]
                              })

        # Label top row with arrow
        axs[0, 0].annotate(
            '',
            xy=(-0.03, 1.1),
            xytext=(0.55, 1.1),
            textcoords='axes fraction',
            xycoords='axes fraction',
            ha='center',
            va='center',
            arrowprops=dict(arrowstyle='<->, head_width=0.05, head_length=0.1',
                            connectionstyle="arc3",
                            linewidth=0.5),
        )
        axs[0, 0].annotate(r'$2\textrm{NA}/\lambda$',
                           xy=(0, 0),
                           xytext=(0.25, 1.2),
                           textcoords='axes fraction',
                           xycoords='axes fraction',
                           ha='center',
                           va='center',
                           fontsize=7)

        # Top row singular values
        for j, ax in enumerate(axs[:-1, 0]):
            ax.axis('off')
            ax.annotate('$j=' + str(j) + '$',
                        xy=(1, 1),
                        xytext=(-0.15, 0.5),
                        textcoords='axes fraction',
                        ha='center',
                        va='center',
                        rotation=90)
            extent = [-2, 2, -2, 2]
            origin = 'lower'
            ax.imshow(self.sigma[:, :, j] / self.sigma_max,
                      cmap="bwr",
                      vmin=-1,
                      vmax=1,
                      interpolation='none',
                      extent=extent,
                      origin=origin)
            ax.set_xlim([-2.05, 2.05])
            ax.set_ylim([-2.05, 2.05])
            levels = [-0.1, -1e-5, 1e-5, 0.1]
            ct = ax.contour(self.sigma[:, :, j] / self.sigma_max,
                            levels,
                            colors='k',
                            linewidths=0.5,
                            extent=extent,
                            origin=origin)
            for mark in marks:
                ax.plot(mark[0] * np.cos(mark[1]),
                        mark[0] * np.sin(mark[1]),
                        'xk',
                        ms=2.5,
                        mew=0.5)

        # Colorbars
        X, Y = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 1, 100))
        axs[-1, 0].imshow(X,
                          cmap="bwr",
                          vmin=-1,
                          vmax=1,
                          interpolation='none',
                          extent=[0, 1, 0, 1],
                          origin='lower',
                          aspect='auto')
        axs[-1, 0].contour(
            X,
            levels,
            colors='k',
            linewidths=0.5,
            extent=[0, 1, 0, 1],
            origin='lower',
        )
        axs[-1, 0].set_xlim([0, 1])
        axs[-1, 0].set_ylim([0, 1])
        axs[-1, 0].tick_params(direction='out', bottom=True, top=False)
        axs[-1, 0].xaxis.set_ticks([0, 0.5, 1.0])
        axs[-1, 0].yaxis.set_ticks([])
        for j, ax in enumerate(axs[-1, 1:]):
            ax.axis('off')

        # For saving singular function pngs
        folder = 'singular'
        if not os.path.exists(folder):
            os.makedirs(folder)

        # Object space singular functions
        for j, ax in np.ndenumerate(axs[:-1, 1:]):
            if self.det.optical_axis == [0, 0, 1]:  # z-detection
                u, s, v = self.calc_point_SVD(marks[j[1]][0], marks[j[1]][1],
                                              0)
            elif self.det.optical_axis == [1, 0, 0]:  # x-detection
                u, s, v = self.calc_point_SVD(0, marks[j[1]][0],
                                              marks[j[1]][1])

            # Labels
            if j[0] == 0:
                rho_label = str(marks[j[1]][0])
                if marks[j[1]][0] < 1e-3:
                    rho_label = '0'
                ax.annotate(r'$\rho = ' + rho_label + '$',
                            xy=(1, 1),
                            xytext=(0.5, 1.15),
                            textcoords='axes fraction',
                            ha='center',
                            va='center')
                ax.annotate(r'$\phi_{\rho} = ' + str(marks[j[1]][1]) + '$',
                            xy=(1, 1),
                            xytext=(0.5, 0.95),
                            textcoords='axes fraction',
                            ha='center',
                            va='center')
            ax.axis('off')
            ax.annotate('$\sigma=' +
                        '{:.2f}'.format(s[j[0]] / self.sigma_max) + '$',
                        xy=(1, 1),
                        xytext=(0.5, 0),
                        textcoords='axes fraction',
                        ha='center',
                        va='center')

            # Create singular function plots
            sh = shcoeffs.SHCoeffs(u[:, j[0]])
            shfilename = folder + '/' + str(j[0]) + str(j[1]) + '.png'
            sh.plot_dist(filename=folder + '/' + str(j[0]) + str(j[1]) +
                         '.png',
                         r=1.1)

            from PIL import Image
            im1 = np.asarray(Image.open(shfilename))
            ax.imshow(im1, interpolation='none')

        f.savefig(filename, bbox_inches='tight')
Ejemplo n.º 5
0
    def plot_SVS_3D(self,
                    filename='svs.pdf',
                    n_px=2**6,
                    marks=np.array([[0, 0, 0], [0.5, 0, 0], [1, 0, 0],
                                    [1.5, 0, 0]])):
        print('Plotting: ' + filename)

        # Layout windows
        inches = 1.5
        rows = self.sigma.shape[-1] + 1
        cols = marks.shape[0] + 1
        f = plt.figure(figsize=(inches * cols, inches * (rows - 0.75)))
        widths = cols * [1]
        heights = [1] * (rows - 1) + [0.05]
        spec = gridspec.GridSpec(ncols=cols,
                                 nrows=rows,
                                 width_ratios=widths,
                                 height_ratios=heights)

        # For saving singular function pngs
        folder = 'singular'
        if not os.path.exists(folder):
            os.makedirs(folder)

        # For each branch
        # self.sigma[0,0,0,:] = 10 # Marker for testing
        # self.sigma[-1,0,0,:] = 10 # Marker for testing
        for row in range(rows):
            for col in range(cols):
                if col == 0 and row != rows - 1:
                    mini_spec = gridspec.GridSpecFromSubplotSpec(
                        2, 2, subplot_spec=spec[row, col])
                    for a in range(2):
                        for b in range(2):
                            # Annotate
                            ax = f.add_subplot(mini_spec[a, b])
                            ax.axis('off')
                            sigma = self.sigma[:, :, :, row] / self.sigma_max
                            sigma_plot = np.zeros(sigma.shape[0:2])

                            if a == 0 and b == 1:
                                ax.annotate('',
                                            xy=(-0.1, -0.1),
                                            xytext=(-0.6, -0.1),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            va='center',
                                            arrowprops=dict(arrowstyle="<-",
                                                            shrinkB=0,
                                                            lw=0.5))
                                ax.annotate('',
                                            xy=(-0.1, -0.1),
                                            xytext=(-0.1, 0.4),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            ha='center',
                                            arrowprops=dict(arrowstyle="<-",
                                                            shrinkB=0,
                                                            lw=0.5))
                                ax.annotate('',
                                            xy=(-0.1, -0.1),
                                            xytext=(-0.1, -0.6),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            ha='center',
                                            arrowprops=dict(arrowstyle="<-",
                                                            shrinkB=0,
                                                            lw=0.5))
                                ax.annotate('',
                                            xy=(-0.1, -0.1),
                                            xytext=(0.4, -0.1),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            va='center',
                                            arrowprops=dict(arrowstyle="<-",
                                                            shrinkB=0,
                                                            lw=0.5))
                                ax.annotate('$z$',
                                            xy=(0, 0),
                                            xytext=(-0.7, -0.1),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            va='center',
                                            ha='center',
                                            fontsize=8)
                                ax.annotate('$y$',
                                            xy=(0, 0),
                                            xytext=(-0.1, 0.5),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            va='center',
                                            ha='center',
                                            fontsize=8)
                                ax.annotate('$z$',
                                            xy=(0, 0),
                                            xytext=(-0.1, -0.7),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            va='center',
                                            ha='center',
                                            fontsize=8)
                                ax.annotate('$x$',
                                            xy=(0, 0),
                                            xytext=(0.5, -0.1),
                                            xycoords='axes fraction',
                                            textcoords='axes fraction',
                                            va='center',
                                            ha='center',
                                            fontsize=8)
                                sigma_plot = np.max(sigma, axis=2).T
                                ax.plot(marks[:, 0],
                                        marks[:, 1],
                                        'xk',
                                        ms=1.5,
                                        mew=0.2)
                            if a == 1 and b == 1:
                                sigma_plot = np.max(sigma, axis=1)[:, ::-1].T
                                ax.plot(marks[:, 0],
                                        marks[:, 2],
                                        'xk',
                                        ms=1.5,
                                        mew=0.2)
                            if a == 0 and b == 0:
                                sigma_plot = np.max(sigma, axis=0)[:, ::-1]
                                ax.plot(marks[:, 2],
                                        marks[:, 1],
                                        'xk',
                                        ms=1.5,
                                        mew=0.2)
                            ax.imshow(sigma_plot,
                                      cmap="bwr",
                                      vmin=-1,
                                      vmax=1,
                                      interpolation='none',
                                      extent=[-2, 2, -2, 2],
                                      origin='lower')
                            ax.set_xlim([-2.05, 2.05])
                            ax.set_ylim([-2.05, 2.05])
                elif row != rows - 1:
                    ax = f.add_subplot(spec[row, col])
                    cl = col - 1
                    print(row, cl)
                    u, s, v = self.calc_point_SVD(marks[cl, 0], marks[cl, 1],
                                                  marks[cl, 2])
                    if np.max(u[:, row]) > 0:
                        sh = shcoeffs.SHCoeffs(u[:, row])
                        shfilename = folder + '/' + str(row) + str(cl) + '.png'
                        sh.plot_dist(filename=shfilename, r=1.1)
                        from PIL import Image
                        im1 = np.asarray(Image.open(shfilename))
                        ax.imshow(im1, interpolation='none')
                        ax.annotate('$\sigma=' +
                                    '{:.2f}'.format(s[row] / self.sigma_max) +
                                    '$',
                                    xy=(1, 1),
                                    xytext=(0.5, 0),
                                    textcoords='axes fraction',
                                    ha='center',
                                    va='center')

                    ax.axis('off')
                elif row == rows - 1 and col == 0:
                    ax = f.add_subplot(spec[row, col])
                    # Colorbars
                    X, Y = np.meshgrid(np.linspace(0, 1, 100),
                                       np.linspace(0, 1, 100))
                    ax.imshow(X,
                              cmap="bwr",
                              vmin=-1,
                              vmax=1,
                              interpolation='none',
                              extent=[0, 1, 0, 1],
                              origin='lower',
                              aspect='auto')
                    # ax.contour(X, levels, colors='k',linewidths=0.5, extent=[0,1,0,1], origin='lower',)
                    ax.set_xlim([0, 1])
                    ax.set_ylim([0, 1])
                    ax.tick_params(direction='out', bottom=True, top=False)
                    ax.xaxis.set_ticks([0, 0.5, 1.0])
                    ax.yaxis.set_ticks([])

        # f, axs = plt.subplots(rows, cols,
        #                       figsize=(inches*cols, inches*(rows - 0.75)),
        #                       gridspec_kw={'hspace':0.05, 'wspace':0.10,
        #                                    'height_ratios':[1]*(rows - 1) + [0.05]})

        # # Label top row with arrow
        # axs[0,0].annotate('', xy=(-0.03, 1.1), xytext=(0.55, 1.1),
        #                   textcoords='axes fraction', xycoords='axes fraction', ha='center', va='center',
        #                   arrowprops=dict(arrowstyle='<->, head_width=0.05, head_length=0.1',
        #                                   connectionstyle="arc3", linewidth=0.5),)
        # axs[0,0].annotate(r'$2\textrm{NA}/\lambda$', xy=(0,0), xytext=(0.25, 1.2),
        #                   textcoords='axes fraction', xycoords='axes fraction', ha='center', va='center', fontsize=7)

        # # Top row singular values
        # for j, ax in enumerate(axs[:-1,0]):
        #     ax.axis('off')
        #     ax.annotate('$j='+str(j)+'$', xy=(1, 1), xytext=(-0.15, 0.5),
        #                 textcoords='axes fraction', ha='center', va='center', rotation=90)
        #     extent= [-2,2,-2,2]
        #     origin = 'lower'
        #     ax.imshow(self.sigma[:,:,j]/self.sigma_max, cmap="bwr", vmin=-1, vmax=1, interpolation='none', extent=extent, origin=origin)
        #     ax.set_xlim([-2.05,2.05])
        #     ax.set_ylim([-2.05,2.05])
        #     levels = [-0.1, -1e-5, 1e-5, 0.1]
        #     ct = ax.contour(self.sigma[:,:,j]/self.sigma_max, levels, colors='k',linewidths=0.5, extent=extent, origin=origin)
        #     for mark in marks:
        #         ax.plot(mark[0]*np.cos(mark[1]), mark[0]*np.sin(mark[1]), 'xk', ms=2.5, mew=0.5)

        # # Colorbars
        # X, Y = np.meshgrid(np.linspace(0, 1, 100),
        #                      np.linspace(0, 1, 100))
        # axs[-1,0].imshow(X, cmap="bwr", vmin=-1, vmax=1, interpolation='none', extent=[0,1,0,1], origin='lower', aspect='auto')
        # axs[-1,0].contour(X, levels, colors='k',linewidths=0.5, extent=[0,1,0,1], origin='lower',)
        # axs[-1,0].set_xlim([0,1])
        # axs[-1,0].set_ylim([0,1])
        # axs[-1,0].tick_params(direction='out', bottom=True, top=False)
        # axs[-1,0].xaxis.set_ticks([0, 0.5, 1.0])
        # axs[-1,0].yaxis.set_ticks([])
        # for j, ax in enumerate(axs[-1, 1:]):
        #     ax.axis('off')

        # # For saving singular function pngs
        # folder = 'singular'
        # if not os.path.exists(folder):
        #     os.makedirs(folder)

        # # Object space singular functions
        # for j, ax in np.ndenumerate(axs[:-1,1:]):
        #     if self.det.optical_axis == [0,0,1]: # z-detection
        #         u, s, v = self.calc_point_SVD(marks[j[1]][0], marks[j[1]][1], 0)
        #     elif self.det.optical_axis == [1,0,0]: # x-detection
        #         u, s, v = self.calc_point_SVD(0, marks[j[1]][0], marks[j[1]][1])

        #     # Labels
        #     if j[0] == 0:
        #         rho_label = str(marks[j[1]][0])
        #         if marks[j[1]][0] < 1e-3:
        #             rho_label = '0'
        #         ax.annotate(r'$\rho = '+rho_label+'$', xy=(1, 1), xytext=(0.5, 1.15),
        #                     textcoords='axes fraction', ha='center', va='center')
        #         ax.annotate(r'$\phi_{\rho} = '+str(marks[j[1]][1])+'$', xy=(1, 1), xytext=(0.5, 0.95),
        #                     textcoords='axes fraction', ha='center', va='center')
        #     ax.axis('off')
        #     ax.annotate('$\sigma='+'{:.2f}'.format(s[j[0]]/self.sigma_max)+'$', xy=(1, 1), xytext=(0.5, 0),
        #                 textcoords='axes fraction', ha='center', va='center')

        #     # Create singular function plots
        #     sh = shcoeffs.SHCoeffs(u[:,j[0]])
        #     shfilename = folder+'/'+str(j[0])+str(j[1])+'.png'
        #     sh.plot_dist(filename=folder+'/'+str(j[0])+str(j[1])+'.png', r=1.1)

        #     from PIL import Image
        #     im1 = np.asarray(Image.open(shfilename))
        #     ax.imshow(im1, interpolation='none')

        f.savefig(filename, bbox_inches='tight')
Ejemplo n.º 6
0
from polharmonic import det, ill, micro, shcoeffs

a = shcoeffs.SHCoeffs([0, 0, 1, 0, 0, 0])
a.plot_dist('normal.png', force_positive=False)
print(a)

b = a.rotate()
b.plot_dist('rotated.png', force_positive=False)
print(b)
Ejemplo n.º 7
0
from polharmonic import det, shcoeffs
import numpy as np

x = shcoeffs.SHCoeffs([1,0,0,1,0,0])
y = shcoeffs.SHCoeffs([1,1,0,1,0,1])
xy = x*y
x.plot(folder='mult/x')
y.plot(folder='mult/y')
xy.plot(folder='mult/xy')