Ejemplo n.º 1
0
        view_spectra(
            focal_plane[sp.numframes - 1],
            title=f"Intensity per Spectral Bin at Timestep {tstep} \n"
            f" AO={tp.use_ao}",
            # f"Beam Ratio = {sp.beam_ratio:.4f}",#  sampling = {sampling*1e6:.4f} [um/gridpt]",
            logZ=True,
            subplt_cols=sp.spectra_cols,
            vlim=(1e-4, 1e-1),
            dx=fp_sampling)

    # Plotting Timeseries in White Light
    if sp.show_tseries:
        img_tseries = np.sum(focal_plane, axis=1)  # sum over wavelength
        view_timeseries(img_tseries,
                        title=f"White Light Timeseries\n"
                        f"AO={tp.use_ao}",
                        subplt_cols=sp.tseries_cols,
                        logZ=True,
                        vlim=(1e-6, 1e-3))
        # dx=fp_sampling[0])

    # Plotting Selected Plane
    if sp.show_planes:
        vlim = [(None, None), (None, None), (None, None), (None, None),
                (None, None)]
        # vlim = [(None,None), (None,None), (None,None), (1e-2,1e-1)]  # (7e-4, 6e-4)
        logZ = [True, False, False, True]
        if sp.save_list:
            plot_planes(cpx_sequence,
                        title=f"White Light through Optical System",
                        subplt_cols=2,
                        vlim=vlim,
Ejemplo n.º 2
0
            f"CDI={cdi.use_cdi}, Atmos={tp.use_atmos}, Aberrations={tp.use_aber}",
            # f"Beam Ratio = {sp.beam_ratio:.4f}",#  sampling = {sampling*1e6:.4f} [um/gridpt]",
            logZ=True,
            subplt_cols=sp.spectra_cols,
            #vlim=(1e-7, 1e-3),
            dx=fp_sampling,
            show=False)

    # Plotting Timeseries in White Light
    if sp.show_tseries:
        img_tseries = np.sum(focal_plane, axis=1)  # sum over wavelength
        view_timeseries(
            img_tseries,
            cdi,
            title=f"White Light Timeseries\n"
            f"CDI={cdi.use_cdi}, Atmos={tp.use_atmos}, Aberrations={tp.use_aber}",
            subplt_cols=sp.tseries_cols,
            logZ=True,
            vlim=(1e-7, 1e-4),
            dx=fp_sampling[0],
        )

    # Plotting Selected Plane
    if sp.show_planes:
        vlim = [(None, None), (1e-7, 1e-4), (None, None), (1e-7, 1e-3),
                (1e-7, 1e-3), (1e-7, 1e-3)]
        # vlim = [(None,None), (None,None), (None,None), (None,None)]  # (1e-2,1e-1) (7e-4, 6e-4)
        logZ = [True, True, True, True, True, True]
        if sp.save_list:
            plot_planes(cpx_sequence,
                        title=f"White Light through Optical System",
                        subplt_cols=2,
Ejemplo n.º 3
0
        view_spectra(
            focal_plane[sp.numframes - 1],
            title=f"Intensity per Spectral Bin at Timestep {tstep} \n"
            f" AO={tp.use_ao}, CDI={cdip.use_cdi}",
            # f"Beam Ratio = {sp.beam_ratio:.4f}",#  sampling = {sampling*1e6:.4f} [um/gridpt]",
            logZ=True,
            subplt_cols=sp.spectra_cols,
            vlim=(1e-10, 1e-4),
            dx=fp_sampling)

    # Plotting Timeseries in White Light
    if sp.show_tseries:
        img_tseries = np.sum(focal_plane, axis=1)  # sum over wavelength
        view_timeseries(img_tseries,
                        title=f"White Light Timeseries\n"
                        f"AO={tp.use_ao}. CDI={cdip.use_cdi}",
                        subplt_cols=sp.tseries_cols,
                        logZ=True,
                        vlim=(1e-10, 1e-4))
        # dx=fp_sampling[0])

    # Plotting Selected Plane
    if sp.show_planes:
        # vlim = ((None, None), (None, None), (None, None), (None, None), (None, None))
        vlim = [(None, None), (None, None), (None, None),
                (None, None)]  # (1e-2,1e-1) (7e-4, 6e-4)
        logZ = [True, False, False, True]
        if sp.save_list:
            plot_planes(cpx_sequence,
                        title=f"White Light through Optical System",
                        subplt_cols=2,
                        vlim=vlim,
Ejemplo n.º 4
0
Archivo: CDI.py Proyecto: jessmos/MEDIS
def cdi_postprocess(cpx_sequence, sampling, plot=False):
    """
    this is the function that accepts the timeseries of intensity images from the simulation and returns the processed
    single image. This function calculates the speckle amplitude phase, and then corrects for it to create the dark
    hole over the specified region of the image.

    From Give'on et al 2011, we have in eq 10 two different values: DeltaP-the change in the focal plane due to the
    probe, and delta, the intensity difference measurements between pairs of phase probes. (It is unfortunate that
    both these terms use delta, so I have done my best to distinguish them in the variable names)

    Here I note that in the CDI phase stream generation, for n_probes there are n_pairs = n_probes/2 pairs of probes.
    These get applied to the DM in a series such that the two probes that form the conjugate pair are separated by
    n_pairs of probes. In other words, for n_probes = 6, the 0th and 3rd probes are a pair, the 1st and 4th are a pair,
    and so on. This is a choice made when creating cdi.phase_series.

    :param cpx_sequence: #timestream of 2D images (complex) from the focal plane complex field
    :param sampling: focal plane sampling
    :return:
    """
    ##
    tic = time.time()
    focal_plane = extract_plane(cpx_sequence, 'detector')  # eliminates astro_body axis [tsteps,wvl,obj,x,y]
    fp_seq = np.sum(focal_plane, axis=(1,2))  # sum over wavelength,object

    n_pairs = cdi.n_probes//2  # number of deltas (probe differentials)
    n_nulls = sp.numframes - cdi.n_probes
    delta = np.zeros((n_pairs, sp.grid_size, sp.grid_size), dtype=float)
    # absDelta = np.zeros((n_nulls, sp.grid_size, sp.grid_size))
    # phsDelta = np.zeros((n_pairs, sp.grid_size, sp.grid_size), dtype=float)
    E_est = np.zeros((n_nulls, sp.grid_size, sp.grid_size), dtype=complex)
    I_processed = np.zeros((n_nulls, sp.grid_size, sp.grid_size))
    H = np.zeros((n_pairs, 2), dtype=float)
    b = np.zeros((n_pairs, 1))

    # Get Masked Data
    mask2D, imsk, jmsk, irng, jrng, imx, imn, jmx, jmn = get_fp_mask(cdi, thresh=1e-6)

    if sp.debug:
        fig, ax = plt.subplots(1,1)
        fig.suptitle(f'Masked FP in CDI probe Region')
        im = ax.imshow(cpx_to_intensity(fp_seq[0,:,:]*mask2D))

    for ip in range(n_pairs):
        # Compute deltas (I_ip+ - I_ip-)/2
        delta[ip] = (np.abs(fp_seq[ip])**2 - np.abs(fp_seq[ip + n_pairs])**2) / 4

    # for i,j in zip(imsk,jmsk):
    # for i,j in zip(irng,jrng):
    for i in irng:
        for j in jrng:
            for xn in range(n_nulls):
                for ip in range(n_pairs):
                    # Amplitude DeltaP
                    Ip = np.abs(fp_seq[ip, i, j]) ** 2
                    Im = np.abs(fp_seq[ip + n_pairs, i, j]) ** 2
                    Io = np.abs(fp_seq[cdi.n_probes + xn, i, j]) ** 2
                    abs = (Ip + Im) / 2 - Io
                    if abs < 0:
                        abs = 0
                    absDeltaP = np.sqrt(abs)
                    # absDeltaP = np.sqrt(np.abs((Ip + Im) / 2 - Io))

                    # phsDeltaP = phsDelta[ip, i, j]
                    # Phase DeltaP
                    # The phase of the change in the focal plane of the probe applied to the DM
                    # First subtract Eo vector from each probe phase to make new field vectors dEa, dEb,
                    # then take the angle between the two
                    dEp = fp_seq[ip, i, j] - fp_seq[cdi.n_probes + xn, i, j]
                    dEm = fp_seq[ip + n_pairs, i, j] - fp_seq[cdi.n_probes + xn, i, j]
                    phsDeltaP = np.arctan2(dEp.imag - dEm.imag, dEp.real - dEm.real)

                    cpxDeltaP = absDeltaP * np.exp(1j * phsDeltaP)
                    # cpxDeltaP = absDeltaP * np.array((np.cos(phsDeltaP), np.sin(phsDeltaP)))

                    H[ip, :] = [-cpxDeltaP.imag, cpxDeltaP.real]  # [n_pairs, 2]
                    # H[ip,:] = [cpxDeltaP[0], cpxDeltaP[1]]
                    b[ip] = delta[ip, i, j]  # [n_pairs, 1]

                a = 2 * H
                Exy = linalg.lstsq(a, b)[0]  # returns tuple, not array
                E_est[xn, i, j] = Exy[0] + (1j * Exy[1])

    toc = time.time()
    dprint(f'CDI post-processing took {(toc-tic)/60:.2} minutes\n')

    ## ===========================
    # Contrast Ratios
    # ===========================
    intensity_probe        = np.zeros(n_nulls)
    intensity_DM_FFT       = np.zeros(n_nulls)
    intensity_pre_process  = np.zeros(n_nulls)
    intensity_post_process = np.zeros(n_nulls)
    
    for xn in range(n_nulls):
        I_processed[xn] = np.abs(fp_seq[n_pairs+xn])**2 - np.abs(np.conj(E_est[xn])*mask2D)**2
        # I_processed[xn] = np.sqrt(np.abs(np.abs(fp_seq[n_pairs+xn])**2 - np.abs(E_est[xn]*mask2D)**2))**2
        # I_processed[xn] = np.abs(fp_seq[n_pairs+xn] - np.conj(E_est[xn]*mask2D))**2

        # Contrast
        intensity_probe[xn] = np.sum(np.abs(fp_seq[xn]*mask2D)**2)
        intensity_pre_process[xn] = np.sum(np.abs(fp_seq[n_pairs + xn]*mask2D)**2)
        intensity_post_process[xn] = np.sum(I_processed[xn]*mask2D)  #np.sum(np.abs(E_processed[xn]*mask2D)**2)

        print(f'\nIntensity in probed region for null step {xn} is '
              f'\nprobe {intensity_probe[xn]}'
              f'\npre-processed {intensity_pre_process[xn]} '
              f'\npost-processed {intensity_post_process[xn]}'
              f'\n difference = {intensity_post_process[xn] - intensity_pre_process[xn]}'
              f'\n')

 ##   if plot:
        # ==================
        # FFT of Tweeter Plane
        # ==================
        # fig, subplot = plt.subplots(1, n_pairs, figsize=(14, 5))
        # fig.subplots_adjust(wspace=0.5, right=0.85)
        # fig.suptitle('Tweeter DM Plane')
        #
        # tweet = extract_plane(cpx_sequence, 'tweeter')  # eliminates astro_body axis [tsteps,wvl,obj,x,y]
        # tweeter = np.sum(tweet, axis=(1, 2))
        # tweeter_intensity = np.abs(tweeter) ** 2
        # for ax, ix in zip(subplot.flatten(), range(n_pairs)):
        #     im = ax.imshow(extract_center(tweeter_intensity[ix], new_size=sp.grid_size*sp.beam_ratio+10),
        #                    interpolation='none', norm=LogNorm(),
        #                    vmin=5e-3, vmax=1e-2)
        #     ax.set_title(f'Probe Phase ' r'$\theta$' f'={cdi.phase_cycle[ix] / np.pi:.2f}' r'$\pi$')
        #
        # cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        # cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        # cb.set_label('Intensity')
        #
        # # =================================================================================
        # fig, subplot = plt.subplots(1, n_pairs, figsize=(14, 5))
        # fig.subplots_adjust(wspace=0.5, right=0.85)
        # fig.suptitle('FFT of Tweeter DM Plane')
        #
        # for ax, ix in zip(subplot.flatten(), range(n_pairs)):
        #     fft_tweeter = (1 / np.sqrt(2 * np.pi) *
        #                    np.fft.fftshift(np.fft.fft2(np.fft.ifftshift(tweeter[ix]))))
        #     im = ax.imshow(np.abs(fft_tweeter) ** 2,
        #                    interpolation='none', norm=LogNorm(),
        #                    vmin=1e-2, vmax=10)
        #     ax.set_title(f'Probe Phase ' r'$\theta$' f'={cdi.phase_cycle[ix] / np.pi:.2f}' r'$\pi$')
        #
        # cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        # cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        # cb.set_label('Intensity')

        # ==================
        # Deltas
        # ==================
        # fig, subplot = plt.subplots(1, n_pairs, figsize=(14,5))
        # fig.subplots_adjust(wspace=0.5, right=0.85)
        # fig.suptitle('Deltas for CDI Probes')
        #
        # for ax, ix in zip(subplot.flatten(), range(n_pairs)):
        #     im = ax.imshow(delta[ix]*1e6*mask2D, interpolation='none',
        #                    norm=SymLogNorm(linthresh=1),
        #                    # vmin=-1, vmax=1
        #                    ) #, norm=SymLogNorm(linthresh=1e-5))
        #     ax.set_title(f"Diff Probe\n" + r'$\theta$' + f'={cdi.phase_series[ix]/np.pi:.3f}' +
        #                  r'$\pi$ -$\theta$' + f'={cdi.phase_series[ix+n_pairs]/np.pi:.3f}' + r'$\pi$')
        #
        # cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        # cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        # cb.set_label('Intensity')

        # ==================
        # Null Probe E-Field
        # ==================
        fig, subplot = plt.subplots(1, n_nulls, figsize=(14, 5))
        fig.subplots_adjust(wspace=0.5, right=0.85)
        fig.suptitle('Original (Null-Probe) Image Plane Intensity')

        for ax, ix in zip(subplot.flatten(), range(n_nulls)):
            im = ax.imshow(np.abs(fp_seq[cdi.n_probes + ix, irng[0]:irng[-1], jrng[0]:jrng[-1]]) ** 2,  # , 250:270, 150:170  *mask2D , irng[0]:irng[-1], jrng[0]:jrng[-1]
                           interpolation='none', norm=LogNorm(),
                           vmin=1e-7, vmax=1e-4)
            ax.set_title(f'Null Step {ix}')

        cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        cb.set_label('Intensity')

        # ===============================
        # E-filed Estimate
        # ===============================
        fig, subplot = plt.subplots(1, n_nulls, figsize=(14, 5))
        fig.subplots_adjust(wspace=0.5, right=0.85)
        fig.suptitle('Pairwise Probe Estimated Image Plane intensity')

        for ax, ix in zip(subplot.flatten(), range(n_nulls)):
            im = ax.imshow(np.abs(E_est[ix, irng[0]:irng[-1], jrng[0]:jrng[-1]])**2,  # , 250:270, 150:170  *mask2D , irng[0]:irng[-1], jrng[0]:jrng[-1]
                           interpolation='none',
                           norm=LogNorm(),
                           vmin=1e-7, vmax=1e-4
                           )  # , norm=SymLogNorm(linthresh=1e-5))
            ax.set_title(f'Null Step {ix}')

        cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        cb.set_label('Intensity')

        # ==================================
        # Processed E-field (CDI Subtracted)
        # ==================================
        fig, subplot = plt.subplots(1, n_nulls, figsize=(14, 5))
        fig.subplots_adjust(wspace=0.5, right=0.85)
        fig.suptitle('CDI Subtracted Image Plane Intensity')

        for ax, ix in zip(subplot.flatten(), range(n_nulls)):
            im = ax.imshow(I_processed[ix],  # , 250:270, 150:170  *mask2D , irng[0]:irng[-1], jrng[0]:jrng[-1]
                           interpolation='none',
                           norm=LogNorm(),
                           vmin=1e-7, vmax=1e-4
                           )  # , norm=SymLogNorm(linthresh=1e-5))
            ax.set_title(f'Null Step {ix}')

        cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        cb.set_label('Intensity')

        # =======================================
        # Compare Unprobed to Estimated E-fields
        # ======================================
        fig, subplot = plt.subplots(2, n_nulls, figsize=(16, 12))
        fig.subplots_adjust(wspace=0.5, right=0.85)
        fig.suptitle('Compare Null Probed to Pairwise Probe Estimated Image Plane Intensity')
        ax1, ax2, ax3, ax4 = subplot.flatten()

        ax1.imshow(np.abs(fp_seq[-2, irng[0]:irng[-1], jrng[0]:jrng[-1]]) ** 2,  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax1.set_title(f'Original Null Step 0')

        ax2.imshow(np.abs(fp_seq[-1, irng[0]:irng[-1], jrng[0]:jrng[-1]]) ** 2,  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax2.set_title(f'Original Null Step 1')

        ax3.imshow(np.abs(E_est[0, irng[0]:irng[-1], jrng[0]:jrng[-1]]) ** 2,  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax3.set_title(f'PP Estimated Null Step 0')

        ax4.imshow(np.abs(E_est[1, irng[0]:irng[-1], jrng[0]:jrng[-1]]) ** 2,  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax4.set_title(f'PP Estimated Null Step 1')

        cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        cb.set_label('Intensity')

        # =======================================
        # Compare Unprobed to Estimated E-fields
        # ======================================
        fig, subplot = plt.subplots(2, n_nulls, figsize=(16, 12))
        fig.subplots_adjust(wspace=0.5, right=0.85)
        fig.suptitle('Compare Null Probed to Pairwise Probe Estimated Image Plane Intensity')
        ax1, ax2, ax3, ax4 = subplot.flatten()

        ax1.imshow(np.abs(fp_seq[-2]) ** 2,  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax1.set_title(f'Original Null Step 0')

        ax2.imshow(np.abs(fp_seq[-1]) ** 2,  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax2.set_title(f'Original Null Step 1')

        ax3.imshow(I_processed[0],  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax3.set_title(f'CDI Processed Null Step 0')

        ax4.imshow(I_processed[1],  # , 250:270, 150:170  *mask2D
                   interpolation='none',
                   norm=LogNorm(),
                   # vmin=1e-8, vmax=1e-2
                   )
        ax4.set_title(f'CDI Processed Null Step 1')

        cax = fig.add_axes([0.9, 0.2, 0.03, 0.6])  # Add axes for colorbar @ position [left,bottom,width,height]
        cb = fig.colorbar(im, orientation='vertical', cax=cax)  #
        cb.set_label('Intensity')

        # ==================
        # View Time Series
        # ==================
        view_timeseries(cpx_to_intensity(fp_seq[:, 100:300, 300:500]), cdi, title=f"White Light Timeseries",
                        subplt_cols=sp.tseries_cols,
                        logZ=False,
                        vlim=(1e-7, 1e-4),
                        )

        plt.show()
Ejemplo n.º 5
0
        view_spectra(
            focal_plane[sp.numframes - 1],
            title=f"Intensity per Spectral Bin at Timestep {tstep} \n"
            f" AO={tp.use_ao}, CDI={cdi.use_cdi}",
            # f"Beam Ratio = {sp.beam_ratio:.4f}",#  sampling = {sampling*1e6:.4f} [um/gridpt]",
            logZ=True,
            subplt_cols=sp.spectra_cols,
            vlim=(1e-7, 1e-4),
            dx=fp_sampling)

    # Plotting Timeseries in White Light
    if sp.show_tseries:
        view_timeseries(img_tseries, cdi, title=f"White Light Timeseries\n"  #f"AO={tp.use_ao}. CDI={cdi.use_cdi}"
                                                f'n_probes={cdi.n_probes}, phase intervals = {cdi.phs_intervals/np.pi:.2f}'\
                                                + r'$\pi$',
                        subplt_cols=sp.tseries_cols,
                        logZ=True,
                        vlim=(1e-7, 1e-4),
                        dx=fp_sampling[0])

    # Plotting Selected Plane
    if sp.show_planes:
        # vlim = [(None, None), (None, None), (None, None), (1e-7,1e-3), (1e-7,1e-3), (1e-7,1e-3)]
        vlim = [(None, None), (1e-7, 1e-4), (1e-8, 1e-4),
                (None, None)]  # (1e-2,1e-1) (7e-4, 6e-4)
        logZ = [False, True, True, True, True, True]
        if sp.save_list:
            plot_planes(cpx_sequence,
                        title=f"White Light through Optical System"
                        f"\nFirst Timestep",
                        subplt_cols=2,