Пример #1
0
def poop():
    global sources

    meas_focus = get_measurements(sources=sources,
                                  mode='circular',
                                  psfs=psfs_focus,
                                  real=True)
    meas_mid = get_measurements(sources=sources,
                                mode='circular',
                                psfs=psfs_mid,
                                real=True)
    meas_outer = get_measurements(sources=sources,
                                  mode='circular',
                                  psfs=psfs_outer,
                                  real=True)
    meas_csbs = get_measurements(sources=sources,
                                 mode='circular',
                                 psfs=psfs_csbs,
                                 real=True)

    meas_focus = add_noise(meas_focus, model='gaussian', dbsnr=15)
    meas_mid = add_noise(meas_mid, model='gaussian', dbsnr=15)
    meas_outer = add_noise(meas_outer, model='gaussian', dbsnr=15)
    meas_csbs = add_noise(meas_csbs, model='gaussian', dbsnr=15)
    # meas_focus = add_noise(meas_focus, model='poisson', max_count=20)
    # meas_mid = add_noise(meas_mid, model='poisson', max_count=20)
    # meas_outer = add_noise(meas_outer, model='poisson', max_count=20)
    # meas_csbs = add_noise(meas_csbs, model='poisson', max_count=20)

    recon_focus = tikhonov(psfs=psfs_focus,
                           measurements=meas_focus,
                           tikhonov_lam=1e-3)
    recon_mid = tikhonov(psfs=psfs_mid,
                         measurements=meas_mid,
                         tikhonov_lam=1e-3)
    recon_outer = tikhonov(psfs=psfs_outer,
                           measurements=meas_outer,
                           tikhonov_lam=1e-3)
    recon_csbs = tikhonov(psfs=psfs_csbs,
                          measurements=meas_csbs,
                          tikhonov_lam=1e-3)

    recon_focus -= np.mean(recon_focus, axis=(1, 2))[:, np.newaxis, np.newaxis]
    recon_mid -= np.mean(recon_mid, axis=(1, 2))[:, np.newaxis, np.newaxis]
    recon_outer -= np.mean(recon_outer, axis=(1, 2))[:, np.newaxis, np.newaxis]
    recon_csbs -= np.mean(recon_csbs, axis=(1, 2))[:, np.newaxis, np.newaxis]
    sources_comp = sources - np.mean(sources, axis=(1, 2))[:, np.newaxis,
                                                           np.newaxis]

    result_focus = np.sum(compare_ssim(sources_comp, recon_focus))
    result_mid = np.sum(compare_ssim(sources_comp, recon_mid))
    result_outer = np.sum(compare_ssim(sources_comp, recon_outer))
    result_csbs = np.sum(compare_ssim(sources_comp, recon_csbs))

    print('focused SSIM sum:', result_focus)
    print('middle SSIM sum:', result_mid)
    print('outer SSIM sum:', result_outer)
    print('csbs SSIM sum:', result_csbs)

    return [result_focus, result_mid, result_outer, result_csbs]
Пример #2
0
        def measure_reconstruct():

            noise = 15  # dB SNR

            meas_focus = get_measurements(sources=sources,
                                          mode='circular',
                                          psfs=psfs_focus,
                                          real=True)
            meas_csbs = get_measurements(sources=sources,
                                         mode='circular',
                                         psfs=psfs_csbs,
                                         real=True)

            meas_focus = add_noise(meas_focus, model='gaussian', dbsnr=noise)
            meas_csbs = add_noise(meas_csbs, model='gaussian', dbsnr=noise)

            recon_focus = tikhonov(psfs=psfs_focus,
                                   measurements=meas_focus,
                                   tikhonov_lam=1e-3)
            recon_csbs = tikhonov(psfs=psfs_csbs,
                                  measurements=meas_csbs,
                                  tikhonov_lam=1e-3)
            result_focus = np.sum(compare_ssim(sources, recon_focus))
            result_csbs = np.sum(compare_ssim(sources, recon_csbs))

            ratio = result_csbs / result_focus

            return dict(**locals())
Пример #3
0
 def setUp(self):
     self.sources = np.ones((2, 4, 4))
     self.ps = PhotonSieve()
     wavelengths = np.array([33.4e-9, 33.5e-9])
     self.psfs = PSFs(self.ps,
                      source_wavelengths=wavelengths,
                      measurement_wavelengths=wavelengths)
     measured = get_measurements(sources=self.sources,
                                 psfs=self.psfs,
                                 real=True)
     self.measured_noisy = add_noise(measured,
                                     max_count=10,
                                     model='poisson')
Пример #4
0
def cost(tikhonov_lam_exp, csbs_lam_exp):

    # csbs_grid
    psfs.copies = np.repeat(12, 12)
    # csbs_focus
    # psfs.copies = np.repeat(12, 2)
    csbs(psfs, sse_cost, 12, lam=10**csbs_lam_exp, order=1)
    measured = get_measurements(psfs=psfs, sources=truth, real=True)
    measured_noisy = add_noise(measured, model='poisson', max_count=500)
    recon = tikhonov(
            sources=measured,
            psfs=psfs,
            measurements=measured_noisy,
            tikhonov_lam=10**tikhonov_lam_exp
    )

    plt.imshow(recon[0])
    plt.show()
    plt.pause(.05)
    return compare_ssim(truth[0], recon[0]) + compare_ssim(truth[1], recon[1])
Пример #5
0
def default_forward(x, psfs):
    im = radon_adjoint(x)
    return get_measurements(sources=im, psfs=psfs, real=True)
Пример #6
0
# ############## GAUSSIAN CONVOLUTION ##############
# measured = get_measurements(
#     sources=sources,
#     mode='circular',
#     meas_size=meas_size,
#     psfs=psfs,
#     blur_sigma=5
# )
# nu=10**-1.5
# lam=[10**-3.8]*num_sources

############## DRIFT ##############
measured = get_measurements(
    sources=sources,
    mode='circular',
    meas_size=meas_size,
    psfs=psfs,
    # blur_sigma=1,
    drift_amount=15
)

# ############## CONVOLUTION AND DRIFT ##############
# measured = get_measurements(
#     sources=sources,
#     mode='circular',
#     meas_size=meas_size,
#     psfs=psfs,
#     blur_sigma=1,
#     drift_amount=5
# )

measured_noisy = add_noise(measured, max_count=100, model='Poisson')
Пример #7
0
original = strands[0]
offset = (25, 25)
# shifted = np.fft.ifft2(fourier_shift(np.fft.fft2(original), offset))
shifted = shift(original, shift=offset, mode='wrap')

# %% psfs -----

ps = PhotonSieve(diameter=10e-2)
wavelengths = np.array([33.4e-9])
psfs = PSFs(
    sieve=ps,
    source_wavelengths=wavelengths,
    measurement_wavelengths=wavelengths,
    num_copies=1
)

# %% measure -----

measured_original = get_measurements(
    sources=original[np.newaxis, :, :],
    psfs=psfs,
    real=True
)[0]

measured_shifted = get_measurements(
    sources=shifted[np.newaxis, :, :],
    psfs=psfs,
    real=True
)[0]
Пример #8
0
                  image_width=303)

psfs_csbs = PSFs(ps,
                 source_wavelengths=wavelengths,
                 num_copies=10,
                 image_width=303)
csbs(psfs_csbs, sse_cost, 12, lam=10**-4.5, order=1)

sources = strands[:2]

# %% measure

noise = 15  # dB SNR

meas_focus = get_measurements(sources=sources,
                              mode='circular',
                              psfs=psfs_focus,
                              real=True)
meas_csbs = get_measurements(sources=sources,
                             mode='circular',
                             psfs=psfs_csbs,
                             real=True)

meas_focus = add_noise(meas_focus, model='gaussian', dbsnr=noise)
meas_csbs = add_noise(meas_csbs, model='gaussian', dbsnr=noise)

recon_focus = tikhonov(psfs=psfs_focus,
                       measurements=meas_focus,
                       tikhonov_lam=1e-3)
recon_csbs = tikhonov(psfs=psfs_csbs,
                      measurements=meas_csbs,
                      tikhonov_lam=1e-3)
Пример #9
0
wavelengths = np.array([33.4e-9])
psfs = PSFs(
    sieve=ps,
    source_wavelengths=wavelengths,
    measurement_wavelengths=wavelengths,
    num_copies=1
)

# %% measure -----

measured_nanoflare_list = []
for nanoflare in nanoflare_list:
    measured_nanoflare_list.append(
        get_measurements(
            sources=nanoflare[np.newaxis, np.newaxis, :, :],
            psfs=psfs,
            real=True
        )[0, 0]
    )

# %% noise -----

max_photon_count_list = [10, 50, 100, 500, 1000]

noisy_nanoflares = []
for max_photon_count in max_photon_count_list:

    noisy_nanoflare_list = []
    for nanoflare in measured_nanoflare_list:
        noisy_nanoflare = np.random.poisson(
            max_photon_count * nanoflare / np.max(nanoflare)
Пример #10
0
sources_ref = size_equalizer(sources, ref_size=meas_size)

ps = PhotonSieve(diameter=16e-2, smallest_hole_diameter=7e-6)

# generate psfs
psfs = PSFs(
    ps,
    sampling_interval=3.5e-6,
    measurement_wavelengths=source_wavelengths,
    source_wavelengths=source_wavelengths,
    psf_generator=circ_incoherent_psf,
    # image_width=psf_width,
    num_copies=1)

measured = get_measurements(sources=sources,
                            mode='valid',
                            meas_size=meas_size,
                            psfs=psfs)
measured_noisy = add_noise(measured, max_count=10, model='Poisson')

# %% recon ------------------------------------

recon = tikhonov(sources=sources_ref,
                 measurements=measured_noisy,
                 psfs=psfs,
                 tikhonov_lam=2e-1,
                 tikhonov_order=1)
if deconvolver == admm:
    recon = admm(
        sources=sources_ref,
        measurements=measured_noisy,
        psfs=psfs,
Пример #11
0
    x = np.repeat(x[np.newaxis], noise_copies, axis=0)
    return add_noise(x, **kwargs)


# %% csbs_grid -----
print('csbs_grid')

psfs[0] = PSFs(sieve=ps,
               source_wavelengths=wavelengths,
               measurement_wavelengths=10,
               num_copies=12)

# csbs(psfs[0], sse_cost, 12, lam=10**-3.75, order=1) # 10 counts
csbs(psfs[0], sse_cost, 12, lam=10**-3.5, order=1)  # 500 counts

measured = get_measurements(sources=sources, real=True, psfs=psfs[0])
measured_noisy = vec_add_noise(measured, max_count=max_count, model='Poisson')
recons[0] = vec_tikhonov(
    sources=sources,
    measurements=measured_noisy,
    psfs=psfs[0],
    # tikhonov_lam=10**-0.75, # 10 counts
    tikhonov_lam=10**-1.75,  # 500 counts
    tikhonov_order=1)

# %% csbs_focus -----
print('csbs_focus')

psfs[1] = PSFs(sieve=ps,
               source_wavelengths=wavelengths,
               measurement_wavelengths=wavelengths,
Пример #12
0
from mas.deconvolution import ista
import numpy as np
from matplotlib import pyplot as plt

# %% problem -----

truth = strands[0:1]
ps = PhotonSieve()

wavelengths = np.array([33.4e-9, 40e-9])

psfs = PSFs(ps,
            source_wavelengths=wavelengths,
            measurement_wavelengths=wavelengths,
            num_copies=10)
measured = get_measurements(psfs=psfs, sources=truth, real=True)
measured = add_noise(measured, model='poisson', max_count=10)


def cost(lam_exp, time_step_exp):

    recon = ista(psfs=psfs,
                 measurements=measured,
                 lam=10**lam_exp,
                 time_step=10**time_step_exp,
                 iterations=100)[0]

    cost = compare_ssim(truth[0], recon)

    plt.subplot(1, 3, 3)
    plt.title('Reconstruction - SSIM {:.3f}'.format(cost))
Пример #13
0
psfs_focus = PSFs(
    ps,
    sampling_interval=3.5e-6,
    measurement_wavelengths=source_wavelengths,
    source_wavelengths=source_wavelengths,
    psf_generator=circ_incoherent_psf,
    image_width=501,
    # cropped_width=psfs_csbs.psfs.shape[-1],
    num_copies=6)

psfs_csbs = csbs(psfs_csbs, sse_cost, 12, lam=csbs_lam, order=order)

# %% measure
psnr_focus, psnr_csbs, ssim_focus, ssim_csbs = [], [], [], []
measured_focus = get_measurements(sources=sources, psfs=psfs_focus)
measured_csbs = get_measurements(sources=sources, psfs=psfs_csbs)
for i in range(50):
    measured_noisy_focus = add_noise(measured_focus,
                                     dbsnr=15,
                                     model='Gaussian')
    measured_noisy_csbs = add_noise(measured_csbs, dbsnr=15, model='Gaussian')

    recon_focus = tikhonov(psfs=psfs_focus,
                           measurements=measured_noisy_focus,
                           tikhonov_lam=1e-3,
                           tikhonov_order=order)
    recon_csbs = tikhonov(psfs=psfs_csbs,
                          measurements=measured_noisy_csbs,
                          tikhonov_lam=1e-3,
                          tikhonov_order=order)
Пример #14
0
    def __init__(
            self,
            # experiment parameters
            exp_time=10,  # s
            drift_angle=-45,  # degrees
            drift_velocity=0.2e-3,  # meters / s
            angle_velocity=0,  # deg / s
            max_count=20,
            noise_model=get_visors_noise(),
            wavelengths=np.array([30.4e-9]),
            # CCD parameters
            frame_rate=4,  # Hz
            ccd_size=np.array((750, 750)),
            start=(1500, 1500),
            pixel_size=14e-6,  # meters
            # simulation subpixel parameters
        resolution_ratio=2,  # CCD pixel_size / simulation pixel_size
            fov_ratio=2,  # simulated FOV / CCD FOV
            # strand parameters
        scene=None,  # pregenerated scene
            num_strands=100,  # num strands per CCD FOV
            # sieve parameters
        diameter=75e-3,  # meters
            smallest_hole_diameter=17e-6,  # meters
    ):
        from mas.psf_generator import PSFs, PhotonSieve
        from mas.forward_model import get_measurements
        from mas.tracking import video

        self.ps = PhotonSieve(diameter=diameter,
                              smallest_hole_diameter=smallest_hole_diameter)
        self.psfs = PSFs(self.ps,
                         sampling_interval=pixel_size / resolution_ratio,
                         source_wavelengths=wavelengths,
                         measurement_wavelengths=wavelengths)

        # load common high resolution scene from file
        if scene is not None:
            scene = np.copy(scene)
        elif (num_strands, fov_ratio, resolution_ratio,
              ccd_size[0]) == (100, 2, 5, 160):
            from mas.data import strand_highres
            self.scene = strand_highres
        elif (num_strands, fov_ratio, resolution_ratio,
              ccd_size[0]) == (100, 2, 2, 750):
            from mas.data import strand_highres2
            self.scene = strand_highres2
        else:
            self.scene = strands(
                num_strands=int(num_strands * fov_ratio * ccd_size[0] / 160),
                thickness=22 * resolution_ratio,
                image_width=ccd_size[0] * resolution_ratio * fov_ratio,
                initial_width=3 * ccd_size[0] * resolution_ratio * fov_ratio)

        self.scene = get_measurements(sources=self.scene[np.newaxis, :, :],
                                      psfs=self.psfs)[0]

        self.scene *= max_count / np.max(self.scene)

        self.frames_clean, self.midpoint_coords = video(
            scene=self.scene,
            frame_rate=frame_rate,
            exp_time=exp_time,
            drift_angle=drift_angle,
            drift_velocity=drift_velocity,
            angle_velocity=angle_velocity,
            ccd_size=ccd_size,
            resolution_ratio=resolution_ratio,
            pixel_size=pixel_size,
            start=start)

        # add noise to the frames
        if noise_model is not None:
            self.frames = noise_model(self.frames_clean, frame_rate)
        else:
            self.frames = self.frames_clean

        self.true_drift = drift_velocity / frame_rate * np.array([
            -np.sin(np.deg2rad(drift_angle)),  # use image coordinate system
            np.cos(np.deg2rad(drift_angle))
        ]) / pixel_size
Пример #15
0
# sources = sources / sources.max()

cmap = 'gist_heat'

ps = PhotonSieve(diameter=16e-2)
# generate psfs
psfs = PSFs(ps,
            sampling_interval=3.5e-6,
            measurement_wavelengths=source_wavelengths,
            source_wavelengths=source_wavelengths,
            psf_generator=circ_incoherent_psf,
            image_width=psf_width,
            num_copies=1)

measured = get_measurements(real=True,
                            sources=sources,
                            psfs=psfs,
                            meas_size=meas_size)
sources = size_equalizer(sources, meas_size)
# take multiple measurements with different noise
measured_noisy_instances = np.zeros((num_instances, ) + measured.shape)
for i in range(num_instances):
    for j in range(measured.shape[0]):
        measured_noisy_instances[i, j, 0] = np.fft.fftshift(
            add_noise(measured[j, 0],
                      dbsnr=10,
                      max_count=500,
                      model='Poisson'
                      # dbsnr=20, no_noise=no_noise, model='Gaussian'
                      ))
if len(measured_noisy_instances.shape) == 4:
    measured_noisy_instances = measured_noisy_instances[np.newaxis]