示例#1
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Plots D65 tabulated data"""

import dtmm.color as dc
import matplotlib.pyplot as plt

wavelengths, tcmf = dc.load_tcmf(retx = True)

plt.plot(wavelengths,tcmf[...,0], "r-",label = "X")
plt.plot(wavelengths,tcmf[...,1], "g-",label = "Y")
plt.plot(wavelengths,tcmf[...,2], "b-",label = "Z")

plt.xlabel("Wavelength [nm]")

plt.legend()
plt.show()
示例#2
0
def field_viewer(field_data,
                 cmf=None,
                 bulk_data=False,
                 n=1.,
                 mode=None,
                 window=None,
                 diffraction=True,
                 polarization="normal",
                 betamax=BETAMAX,
                 **parameters):
    """Returns a FieldViewer object for optical microsope simulation
    
    Parameters
    ----------
    field_data : field data tuple
        Input field data
    cmf : str, ndarray or None, optional
        Color matching function (table). If provided as an array, it must match 
        input field wavelengths. If provided as a string, it must match one of 
        available CMF names or be a valid path to tabulated data. See load_tcmf.
    n : float, optional
        Refractive index of the output material.
    mode : [ 't' | 'r' | None], optional
        Viewer mode 't' for transmission mode, 'r' for reflection mode None for
        as is data (no projection calculation - default).
    window : ndarray, optional
        Window function by which the calculated field is multiplied. This can 
        be used for removing artefact from the boundaries.
    diffraction : bool, optional
        Specifies whether field is treated as diffractive field or not (if it
        was calculated by diffraction > 0 algorithm or not). If set to False
        refocusing is disabled.
    polarization : str, optional
        Defines polarization mode. That is, how the polarization of the light is
        treated after passing the analyzer. By default, polarizer is applied
        in real space (`mode`) which is good for normal (or mostly normal) 
        incidence light. You can use `mode` instead of `normal` for more 
        accurate, but slower computation. Here polarizers are applied to 
        mode coefficients in fft space. 
    betamax : float
        Betamax parameter used in the diffraction calculation function. With this
        you can simulate finite NA of the microscope (NA = betamax).
    parameters : kwargs, optional
        Extra parameters passed directly to the :meth:`FieldViewer.set_parameters`
        
    Returns
    -------
    out : viewer
        A :class:`FieldViewer` or :class:`BulkViewer` viewer object 
    
    """
    field, wavelengths, pixelsize = field_data
    wavenumbers = k0(wavelengths, pixelsize)

    if diffraction == False and mode is not None:
        import warnings
        warnings.warn(
            "Diffraction has been enabled because projection mode is set!")
        diffraction = True
    pmodes = ("mode", "normal")
    if polarization not in pmodes:
        raise ValueError(
            "Unknown polarization mode, should be one of {}".format(
                repr(pmodes)))

    if cmf is None:
        cmf = load_tcmf(wavelengths)
    elif isinstance(cmf, str):
        cmf = load_tcmf(wavelengths, cmf=cmf)
    if bulk_data == False:
        if field.ndim < 4:
            raise ValueError("Incompatible field shape")
        viewer = FieldViewer(field,
                             wavenumbers,
                             cmf,
                             mode=mode,
                             n=n,
                             window=window,
                             diffraction=diffraction,
                             polarization=polarization,
                             betamax=betamax)

        viewer.set_parameters(**parameters)
    else:
        if field.ndim < 5:
            raise ValueError("Incompatible field shape")
        parameters.setdefault("focus", 0)
        viewer = BulkViewer(field,
                            wavenumbers,
                            cmf,
                            mode=mode,
                            n=n,
                            window=window,
                            diffraction=diffraction,
                            polarization=polarization,
                            betamax=betamax)
        viewer.set_parameters(**parameters)
    return viewer
示例#3
0
文件: plot.py 项目: Alex-Vasile/dtmm
    def __init__(self,
                 xfield,
                 yfield,
                 refind=1.5,
                 beta=0.,
                 phi=0.,
                 k0=[1],
                 nstep=16,
                 maxfocus=100):
        self.xfield = xfield
        self.yfield = yfield
        self.analizer = (0, 1)
        self.polarizer = (1, 0)
        self.refind = refind
        self.beta = beta
        self.phi = phi
        self.k0 = k0
        self.nstep = nstep
        self.intensity = 1.
        self.cmf = load_tcmf()
        self.n = len(xfield)

        self.fig, self.ax1 = plt.subplots()

        plt.subplots_adjust(bottom=0.25)

        self.ixfield = xfield
        self.iyfield = yfield
        self.xray = field2ray(xfield, pol=self.analizer, phi=self.phi)
        self.yray = field2ray(yfield, pol=self.analizer, phi=self.phi)
        self.ray = [
            self.xray[i] * self.polarizer[0] + self.yray[i] * self.polarizer[1]
            for i in range(self.n)
        ]

        im = specter2color(self.ray, self.cmf, norm=self.intensity)
        self.imax = self.ax1.imshow(im)  #, animated=True)

        self.axfocus = plt.axes([0.25, 0.14, 0.65, 0.03])
        self.axanalizer = plt.axes([0.25, 0.10, 0.65, 0.03])
        self.axpolarizer = plt.axes([0.25, 0.06, 0.65, 0.03])
        self.axintensity = plt.axes([0.25, 0.02, 0.65, 0.03])
        self.sfocus = Slider(self.axfocus,
                             "focus",
                             -maxfocus,
                             maxfocus,
                             valinit=0.,
                             valfmt='%.1f')
        self.sintensity = Slider(self.axintensity,
                                 "light",
                                 0,
                                 10,
                                 valinit=1.,
                                 valfmt='%.1f')
        self.sanalizer = Slider(self.axanalizer,
                                "analizer",
                                -180,
                                180,
                                valinit=0,
                                valfmt='%.1f')
        self.spolarizer = Slider(self.axpolarizer,
                                 "polarizer",
                                 -180,
                                 180,
                                 valinit=0,
                                 valfmt='%.1f')

        def update_focus(d):
            self.ixfield = ipropagate(self.xfield,
                                      d=d,
                                      refind=self.refind,
                                      beta=self.beta,
                                      phi=self.phi,
                                      k0=self.k0,
                                      nstep=self.nstep)
            self.iyfield = ipropagate(self.yfield,
                                      d=d,
                                      refind=self.refind,
                                      beta=self.beta,
                                      phi=self.phi,
                                      k0=self.k0,
                                      nstep=self.nstep)
            field = [
                self.ixfield[i] * self.polarizer[0] +
                self.iyfield[i] * self.polarizer[1] for i in range(self.n)
            ]
            self.ray = field2ray(field, pol=self.analizer, phi=self.phi)

            im = specter2color(self.ray, self.cmf, norm=1. / self.intensity)
            self.imax.set_data(im)
            self.fig.canvas.draw_idle()

        def update_light(d):
            self.intensity = d
            im = specter2color(self.ray, self.cmf, norm=1. / self.intensity)
            self.imax.set_data(im)
            self.fig.canvas.draw_idle()

        def update_analizer(d):
            self.analizer = np.cos(d / 180 * np.pi), np.sin(d / 180 * np.pi)
            field = [
                self.ixfield[i] * self.polarizer[0] +
                self.iyfield[i] * self.polarizer[1] for i in range(self.n)
            ]
            self.ray = field2ray(field, pol=self.analizer, phi=self.phi)

            im = specter2color(self.ray, self.cmf, norm=1. / self.intensity)
            self.imax.set_data(im)
            self.fig.canvas.draw_idle()

        def update_polarizer(d):
            self.polarizer = np.cos(d / 180 * np.pi), np.sin(d / 180 * np.pi)
            field = [
                self.ixfield[i] * self.polarizer[0] +
                self.iyfield[i] * self.polarizer[1] for i in range(self.n)
            ]
            self.ray = field2ray(field, pol=self.analizer, phi=self.phi)

            im = specter2color(self.ray, self.cmf, norm=1. / self.intensity)
            self.imax.set_data(im)
            self.fig.canvas.draw_idle()

        self.ids1 = self.sfocus.on_changed(update_focus)
        self.ids2 = self.sintensity.on_changed(update_light)
        self.ids3 = self.sanalizer.on_changed(update_analizer)
        self.ids4 = self.spolarizer.on_changed(update_polarizer)
示例#4
0
文件: plot.py 项目: Alex-Vasile/dtmm
    def __init__(self,
                 field,
                 refind=1.5,
                 beta=0.,
                 phi=0.,
                 k0=[1],
                 nstep=16,
                 maxfocus=100):
        self.field = field
        self.pol = (1, 0)
        self.refind = refind
        self.beta = beta
        self.phi = phi
        self.k0 = k0
        self.nstep = nstep
        self.intensity = 1.
        self.cmf = load_tcmf()

        self.fig, self.ax1 = plt.subplots()

        plt.subplots_adjust(bottom=0.25)

        self.ifield = field
        self.ray = field2ray2(field, pol=self.pol, phi=self.phi)
        im = specter2color(self.ray, self.cmf, norm=self.intensity)
        self.imax = self.ax1.imshow(im)  #, animated=True)

        self.axfocus = plt.axes([0.25, 0.15, 0.65, 0.03])
        self.axanalizer = plt.axes([0.25, 0.10, 0.65, 0.03])
        self.axintensity = plt.axes([0.25, 0.05, 0.65, 0.03])
        self.sfocus = Slider(self.axfocus,
                             "focus",
                             -maxfocus,
                             maxfocus,
                             valinit=0.,
                             valfmt='%.1f')
        self.sintensity = Slider(self.axintensity,
                                 "light",
                                 0,
                                 10,
                                 valinit=1.,
                                 valfmt='%.1f')
        self.sanalizer = Slider(self.axanalizer,
                                "analizer",
                                -180,
                                180,
                                valinit=0,
                                valfmt='%.1f')

        def update_focus(d):
            self.ifield = ipropagate2(self.field,
                                      d=d,
                                      refind=self.refind,
                                      beta=self.beta,
                                      phi=self.phi,
                                      k0=self.k0,
                                      nstep=self.nstep,
                                      select="forward")
            self.ray = field2ray2(self.ifield, pol=self.pol, phi=self.phi)
            im = specter2color(self.ray, self.cmf, norm=1. / self.intensity)
            self.imax.set_data(im)
            self.fig.canvas.draw_idle()

        def update_light(d):
            self.intensity = d
            im = specter2color(self.ray, self.cmf, norm=1. / self.intensity)
            self.imax.set_data(im)
            self.fig.canvas.draw_idle()

        def update_analizer(d):
            print(d)
            self.pol = np.cos(d / 180 * np.pi), np.sin(d / 180 * np.pi)
            self.ray = field2ray2(self.ifield, pol=self.pol, phi=self.phi)
            im = specter2color(self.ray, self.cmf, norm=1. / self.intensity)
            self.imax.set_data(im)
            self.fig.canvas.draw_idle()

        self.ids1 = self.sfocus.on_changed(update_focus)
        self.ids2 = self.sintensity.on_changed(update_light)
        self.ids3 = self.sanalizer.on_changed(update_analizer)
示例#5
0
# -*- coding: utf-8 -*-
"""Plots colors from a D65 light filtered with a narrow band 
and broad band filters of different central wavelengths. """

import dtmm.color as dc
import matplotlib.pyplot as plt
import numpy as np
import dtmm

nw = 161  #number of wavelengths
ni = 31  #number of intensities

ws = np.linspace(380, 780, nw)

#color matching function
cmf = dc.load_tcmf(ws)

im = np.zeros(shape=(ni, nw, 3))

subplots = [plt.subplot(i) for i in (311, 312, 313)]
intensities = np.linspace(0, 1.5, ni)

for k, delta in enumerate((50, 150, 400)):
    ax = subplots[k]
    ax.set_title("D65 + {} nm bandwidth filter".format(delta))

    for i, w in enumerate(ws):
        for j, intensity in enumerate(intensities):
            s = np.zeros(shape=(nw, ), dtype=dtmm.conf.FDTYPE)
            mask = (ws < w + delta / 2.) & (ws > w - delta / 2.)
            s[mask] = intensity  #make band-pass specter