Esempio n. 1
0
def get_psfs(*, separation):
    ps = PhotonSieve(diameter=diameter)
    wavelengths = np.array([33.4e-9, 33.4e-9 + separation])

    psfs_focus = PSFs(ps,
                      source_wavelengths=wavelengths,
                      measurement_wavelengths=wavelengths,
                      num_copies=6,
                      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)

    return psfs_focus, psfs_csbs
Esempio n. 2
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')
Esempio n. 3
0
# %% meas ------------------------

source_wavelengths = np.array([33.4e-9, 33.5e-9])
num_sources = len(source_wavelengths)
meas_size = [160,160]
sources = strands_ext[0:num_sources]
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
)

# ############## INVERSE CRIME ##############
# measured = get_measurements(
#     sources=sources,
#     mode='circular',
#     meas_size=meas_size,
#     psfs=psfs
# )
#
# nu=10**-1.5
# lam=[10**-3.8]*num_sources
Esempio n. 4
0
# dfts2 = np.fft.fftshift(dfts2, axes=(2, 3))

# dfts = np.concatenate((dfts1, dfts2), axis=1)

# psfs.psf_dfts = dfts
# psfs.psfs = dfts

# csbs(psfs, sse_cost, 10, lam=1e-2, order=0)
# fourier_slices(psfs)

# %% ps -----

wavelengths = np.array([33.4e-9])
ps = PhotonSieve()
psfs_ps = PSFs(ps,
               source_wavelengths=wavelengths,
               measurement_wavelengths=49,
               num_copies=5)

plotter4d(np.fft.fftshift(
    np.abs(psfs.psf_dfts.reshape(7, 7, 301, 301)),
    axes=(2, 3),
),
          figsize=(5, 5),
          scale=True)
plt.savefig('photonsieve_dfts.png', dpi=300)

# %% sq -----

wavelengths = np.array([33.4e-9])
ps = PhotonSieve()
psfs_sq = PSFs(ps,
Esempio n. 5
0
# plot final csbs result for a range of lambdas
from mas.csbs import csbs
from mas import sse_cost
from mas.psf_generator import PSFs, PhotonSieve, circ_incoherent_psf
import numpy as np
from matplotlib import pyplot as plt
import copy

# generate photon sieve with default parameters
ps = PhotonSieve()

# run csbs algorithm on a range of lambdas
lambdas = np.logspace(-10, 2, 20)
copies = []
orig_psfs = PSFs(ps,
                 source_wavelengths=np.array([33.4e-9, 33.7e-9, 33.8e-9]),
                 psf_generator=circ_incoherent_psf,
                 image_width=51)
for lam in lambdas:
    print('----------------------', lam)
    psfs = copy.deepcopy(orig_psfs)
    csbs(psfs, sse_cost, 10, lam=lam)
    copies.append(psfs.copies)

# 2D plot of (plane_locations, lambda) vs copies
plt.figure(constrained_layout=True)
plt.imshow(np.abs(copies), cmap='magma', aspect=1)
plt.ylabel('lambda')
plt.xlabel('plane locations')
plt.yticks(np.arange(len(lambdas)), np.round(lambdas, 3))
cbar = plt.colorbar()
cbar.set_label('Copies')
Esempio n. 6
0
from mas.psf_generator import PSFs, PhotonSieve
from mas.forward_model import add_noise, get_measurements
from mas.deconvolution import tikhonov
from mas.data import strands
import numpy as np
from matplotlib import pyplot as plt
from mas.plotting import plotter4d
from mas.measure import compare_ssim
from mas import sse_cost
from mas.csbs import csbs

ps = PhotonSieve()
wavelengths = np.array([33.4e-9, 33.402e-9])

psfs_focus = PSFs(ps,
                  source_wavelengths=wavelengths,
                  measurement_wavelengths=wavelengths,
                  num_copies=6)

psfs_mid = PSFs(ps,
                source_wavelengths=wavelengths,
                measurement_wavelengths=np.array(
                    [33.4e-9, 33.401e-9, 33.402e-9]),
                num_copies=6)
psfs_mid.copies = np.array([6, 1, 5])

psfs_outer = PSFs(ps,
                  source_wavelengths=wavelengths,
                  measurement_wavelengths=np.array(
                      [33.4e-9, 33.402e-9, 33.41e-9]),
                  num_copies=6)
psfs_outer.copies = np.array([6, 5, 1])
Esempio n. 7
0
from mas.data import strands
from matplotlib import pyplot as plt
from mas.plotting import plotter4d
from mas.measure import compare_ssim
from mas import sse_cost
from mas.csbs import csbs
import numpy as np

separation = .04e-9  # m

ps = PhotonSieve()
wavelengths = np.array([33.4e-9, 33.4e-9 + separation])

psfs_focus = PSFs(ps,
                  source_wavelengths=wavelengths,
                  measurement_wavelengths=wavelengths,
                  num_copies=6,
                  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
Esempio n. 8
0
#!/bin/env python3
# Comparison of in focus and out of focus PSFs for 9nm and 33.4nm

import matplotlib.pyplot as plt
from mas.psf_generator import PSFs, PhotonSieve, circ_incoherent_psf, sieve_incoherent_psf
from mas.plotting import plotter4d
import numpy as np

wavelengths = np.array([9, 33.5]) * 1e-9
ps = PhotonSieve(diameter=8e-2)
psfs = PSFs(sieve=ps,
            source_wavelengths=wavelengths,
            measurement_wavelengths=wavelengths,
            image_width=301,
            psf_generator=sieve_incoherent_psf)

# focused 9nm
plt.imshow(np.abs(psfs.psfs[0, 0]))
plt.axis([140, 160, 140, 160])
plt.title('9 nm, focused')
plt.savefig('9nm.png')
# focused 33.5nm
plt.imshow(np.abs(psfs.psfs[1, 1]))
plt.axis([140, 160, 140, 160])
plt.title('33.4 nm, focused')
plt.savefig('33.4nm.png')
plt.show()
Esempio n. 9
0
from mas.psf_generator import PSFs, PhotonSieve
from mas.forward_model import add_noise, get_measurements
from mas.deconvolution import tikhonov
from mas.data import strands
import numpy as np
from matplotlib import pyplot as plt
from mas.plotting import plotter4d
from mas.measure import compare_ssim
from mas import sse_cost
from mas.csbs import csbs

ps = PhotonSieve()
wavelengths = np.array([33.4e-9, 33.402e-9])

psfs_focus = PSFs(ps,
                  source_wavelengths=wavelengths,
                  measurement_wavelengths=wavelengths,
                  num_copies=6)

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

# %% measure

sources = strands[:2]

meas_focus = get_measurements(sources=sources,
                              mode='circular',
                              psfs=psfs_focus,
                              real=True)
meas_csbs = get_measurements(sources=sources,
                             mode='circular',
Esempio n. 10
0
max_count = 500

vec_tikhonov = _vectorize(signature='(a,b,c)->(d,e,f)',
                          included=['measurements'])(tikhonov)


def vec_add_noise(x, **kwargs):
    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)
Esempio n. 11
0
import numpy as np
from matplotlib import pyplot as plt

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

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

psfs = PSFs(
    ps,
    source_wavelengths=wavelengths,
    # naive_focus
    # measurement_wavelengths=wavelengths,
    # num_copies=6,
    # csbs_grid
    measurement_wavelengths=10,
    num_copies=12
    # csbs_focus
    # measurement_wavelengths=wavelengths,
    # num_copies=12
    # naive_grid
    # measurement_wavelengths=10,
    # num_copies=1
)


# Bounded region of parameter space
pbounds = {'tikhonov_lam_exp': (-4, 1), 'csbs_lam_exp': (-4, 1)}

# def cost(tikhonov_lam_exp):
def cost(tikhonov_lam_exp, csbs_lam_exp):
Esempio n. 12
0
diameter = 16e-2
smallest_hole_diameter = 7e-6
print('DOF_separation = {}'.format(
    diameter * (source_wavelengths[1] - source_wavelengths[0]) /
    (2 * smallest_hole_diameter * source_wavelengths[0])))
sources = np.load('sources.npy')
order = 1
csbs_lam = 5e-4

ps = PhotonSieve(diameter=diameter,
                 smallest_hole_diameter=smallest_hole_diameter)
psfs_csbs = PSFs(
    ps,
    sampling_interval=3.5e-6,
    measurement_wavelengths=30,
    source_wavelengths=source_wavelengths,
    psf_generator=circ_incoherent_psf,
    image_width=501,
    # cropped_width=51,
    num_copies=10)

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)
Esempio n. 13
0
#!/bin/env python3

from mas.data import strands as sources
from mas.forward_model import add_noise, get_measurements, get_contributions
from mas.psf_generator import PhotonSieve, PSFs
import numpy as np
import matplotlib.pyplot as plt

# %% generate -----

ps = PhotonSieve()
wavelengths = np.array([33.4, 33.5]) * 1e-9
sources = sources[0:-1:7].reshape((3, 1, 160, 160))
psfs = PSFs(sieve=ps,
            source_wavelengths=wavelengths,
            measurement_wavelengths=300,
            num_copies=1,
            image_width=301)

# %% plot -----

# fig = plt.figure(figsize=(12, 12))
# ax = fig.add_subplot(111, projection='3d')
plt.figure(figsize=(10, 5))

dft_mag1 = np.fft.fftshift(np.abs(psfs.psf_dfts[:, 0, 0, :]), axes=1).T
dft_mag2 = np.fft.fftshift(np.abs(psfs.psf_dfts[:, 1, 0, :]), axes=1).T

spatial_extent = (-psfs.psfs.shape[-1] // 2, psfs.psfs.shape[-1] // 2)
wavelength_extent = (psfs.measurement_wavelengths[0],
                     psfs.measurement_wavelengths[-1])
Esempio n. 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
Esempio n. 15
0
from mas.deconvolution import tikhonov
from mas.data import strands
import numpy as np
from matplotlib import pyplot as plt
from mas.plotting import plotter4d
from mas.measure import compare_ssim
from mas import sse_cost
from mas.csbs import csbs
from itertools import product

# %% psfs

ps = PhotonSieve()
wavelengths = np.array([33.4e-9, 33.402e-9])

psfs = PSFs(ps, source_wavelengths=wavelengths)

# %% plot

plt.plot(
    psfs.measurement_wavelengths,
    1 / np.sum((psfs.psfs[:, 0] * psfs.psfs[:, 1]), axis=(1,2))
)

plt.axvline(wavelengths[0], color='red')
plt.axvline(wavelengths[1], color='red')
plt.title('Incoherence of PSF pairs')
plt.ylabel('Incoherence')
plt.xlabel('Meas. wavelength')

plt.show()
Esempio n. 16
0
from mas.psf_generator import PSFs, PhotonSieve, circ_incoherent_psf
import numpy as np
import h5py

# generate photon sieve
ps = PhotonSieve(diameter=80e-3,
                 smallest_hole_diameter=7e-6,
                 hole_diameter_to_zone_width=7 / 6)

# generate psfs for 632.8nm at [0, 1, 2, 3, 5, 7] DOF away from focus
source_wavelengths = np.array([632.8e-9])
dof_array = np.array([0, 1, 2, 3, 5, 7])
dof = 2 * ps.smallest_hole_diameter**2 / source_wavelengths
focal_length = ps.diameter * ps.smallest_hole_diameter / source_wavelengths

psfs = PSFs(
    ps,
    source_wavelengths=source_wavelengths,
    measurement_wavelengths=ps.diameter * ps.smallest_hole_diameter /
    (focal_length + dof_array * dof),
    image_width=3001,
    sampling_interval=2.2e-6,
    psf_generator=circ_incoherent_psf,
)

# save to file
with h5py.File('/tmp/psfs.h5', 'w') as f:
    for psf, dof in zip(psfs.psfs[:, 0, :, :], dof_array):
        f.create_dataset("dof={}".format(dof), data=psf)
Esempio n. 17
0
from mas.forward_model import add_noise, get_measurements, get_contributions
from mas.psf_generator import PhotonSieve, PSFs
from mas.plotting import plotter4d
from mas.csbs import csbs
from mas import sse_cost
import numpy as np
import matplotlib.pyplot as plt

# %% psfs -----

ps = PhotonSieve()
wavelengths = np.array([33.4, 33.5]) * 1e-9
sources = sources[np.array((0, 1))].reshape((2, 1, 160, 160))
psfs = PSFs(
    sieve=ps,
    source_wavelengths=wavelengths,
    measurement_wavelengths=wavelengths,
    num_copies=1
)

# %% contributions -----

contributions = get_contributions(sources=sources[:, 0, :, :], psfs=psfs, real=True)
plt = plotter4d(
    contributions,
    title='measurement contributions',
    column_labels=wavelengths,
    sup_xlabel='source wavelengths',
    row_labels=wavelengths,
    sup_ylabel='measurement wavelengths',
    scale=True
)
Esempio n. 18
0
from mas.deconvolution import tikhonov
from mas.data import strands
import numpy as np
from matplotlib import pyplot as plt
from mas.plotting import plotter4d
from mas.measure import compare_ssim
from mas import sse_cost
from mas.csbs import csbs
from itertools import product

# %% psfs

ps = PhotonSieve()

psfs_separated = PSFs(
    ps,
    source_wavelengths=np.array([33.4e-9, 33.5e-9]),
)
psfs_close = PSFs(
    ps,
    source_wavelengths=np.array([33.4e-9, 33.402e-9]),
)

# %% compute

prod_separated = np.array(
    list(product(psfs_separated.psfs, psfs_separated.psfs)))
energy_separated = np.sum(np.linalg.norm(prod_separated, axis=(3, 4))**2,
                          axis=(1, 2)).reshape((32, 32))
separability_separated = np.linalg.norm(
    prod_separated[:, 0, 0] * prod_separated[:, 1, 1] -
    prod_separated[:, 0, 1] * prod_separated[:, 1, 0],