Ejemplo n.º 1
0
def second_field_diffraction_matrix(shape,
                                    ks,
                                    beta,
                                    phi,
                                    d=1.,
                                    epsv=(1, 1, 1),
                                    epsa=(0, 0, 0.),
                                    betamax=BETAMAX,
                                    out=None):
    dmat = field_diffraction_matrix(shape, ks, d, epsv, epsa, betamax=betamax)
    cmat = field_correction_matrix(beta, phi, ks, d, epsv, epsa)
    return dotmm(cmat, dmat, out=None)
Ejemplo n.º 2
0
    def _calculate_specter_normal(self, recalc=False, **params):
        self.set_parameters(**params)
        if self.ofield is None:
            recalc = True  #first time only trigger calculation
        if recalc or "focus" in self._updated_parameters:
            if self.mode is None:
                self.ofield = self.ifield[self.focus]
            else:
                dmat = field_diffraction_matrix(self.ifield.shape[-2:],
                                                self.ks,
                                                d=0,
                                                epsv=self.epsv,
                                                epsa=self.epsa,
                                                mode=self.mode,
                                                betamax=self.betamax)
                self.ofield = diffract(self.ifield[self.focus],
                                       dmat,
                                       window=self.window,
                                       out=self.ofield)
            recalc = True

        if recalc or "polarizer" in self._updated_parameters or "analyzer" in self._updated_parameters or "sample" in self._updated_parameters:
            sample = self.sample
            if sample is None:
                sample = 0.
            if self.polarizer is None:
                tmp = _redim(self.ofield, ndim=5)
                out = np.empty_like(tmp[0])
            else:
                angle = -np.pi / 180 * (self.polarizer - sample)
                c, s = np.cos(angle), np.sin(angle)
                tmp = _redim(self.ofield, ndim=6)
                out = np.empty_like(tmp[0, 0])
            if self.analyzer is not None:
                angle = -np.pi / 180 * (self.analyzer - sample)
                #pmat = linear_polarizer(angle)
                pmat = ray_polarizer((np.cos(angle), np.sin(angle)),
                                     epsv=self.epsv,
                                     epsa=self.epsa)

            for i, data in enumerate(tmp):
                if self.polarizer is not None:
                    x = data[0] * c
                    y = np.multiply(data[1], s, out=out)
                    ffield = np.add(
                        x, y, out=out)  #numexpr.evaluate("x*c+y*s", out = out)
                else:
                    ffield = data

                if self.analyzer is not None:
                    pfield = dotmf(pmat, ffield, out=out)
                else:
                    pfield = ffield
                if i == 0:
                    self.specter = field2specter(pfield)
                else:
                    self.specter += field2specter(pfield)
            recalc = True

        if recalc or "intensity" in self._updated_parameters:
            self._updated_parameters.clear()
            self._updated_parameters.add(
                "intensity")  #trigger calculate_image call
        else:
            self._updated_parameters.clear()
        return self.specter
Ejemplo n.º 3
0
    def _calculate_specter_mode(self, recalc=False, **params):
        self.set_parameters(**params)

        if self.ofield is None:
            recalc = True  #first time only trigger calculation

        if recalc or self._has_parameter_updated("analyzer", "sample"):
            sample = self.sample if self.sample is not None else 0.
            if self.analyzer is not None:
                angle = -np.pi / 180 * (self.analyzer - sample)
                c, s = np.cos(angle), np.sin(angle)
                self.pmat = mode_polarizer(self.ifield.shape[-2:],
                                           self.ks,
                                           jones=(c, s),
                                           epsv=self.epsv,
                                           epsa=self.epsa,
                                           betamax=self.betamax)
                if self.pmode != "mode":
                    self.pmat = self.pmat[..., 0:1, 0:1, :, :]

        if recalc or self._has_parameter_updated("focus"):
            if self.mode is None:
                self.ffield = fft2(self.ifield[self.focus])
            else:
                self.dmat = field_diffraction_matrix(self.ifield.shape[-2:],
                                                     self.ks,
                                                     d=0,
                                                     epsv=self.epsv,
                                                     epsa=self.epsa,
                                                     mode=self.mode,
                                                     betamax=self.betamax)
                self.ffield = fft2(self.ifield[self.focus])

            recalc = True  #trigger update of self.data

        if recalc or self._has_parameter_updated("sample", "polarizer"):
            sample = self.sample if self.sample is not None else 0.
            if self.polarizer is not None:
                angle = -np.pi / 180 * (self.polarizer - sample)
                c, s = np.cos(angle), np.sin(angle)

                self.data = _redim(self.ffield, ndim=6)
                x = c * self.data[:, 0]
                y = s * self.data[:, 1]
                self.data = x + y
            else:
                self.data = _redim(self.ffield, ndim=5)

        if recalc or self._has_parameter_updated(
                "analyzer", "sample", "polarizer", "focus", "intensity"):
            if self.dmat is not None:
                pmat = dotmm(self.pmat, self.dmat)
            else:
                pmat = self.pmat
            self.ofield = dotmf(pmat, self.data, out=self.ofield)
            self.ofield = ifft2(self.ofield, out=self.ofield)

            for i, data in enumerate(self.ofield):
                if i == 0:
                    self.specter = field2specter(data)
                else:
                    self.specter += field2specter(data)
            recalc = True

        if recalc or "intensity" in self._updated_parameters:
            self._updated_parameters.clear()
            self._updated_parameters.add(
                "intensity")  #trigger calculate_image call
        else:
            self._updated_parameters.clear()

        return self.specter
Ejemplo n.º 4
0
    def _calculate_specter_mode(self, recalc=False, **params):
        self.set_parameters(**params)
        if self.ofield is None:
            recalc = True  #first time only trigger calculation
        if recalc or self._has_parameter_updated("sample", "polarizer"):
            sample = self.sample if self.sample is not None else 0.
            if self.polarizer is not None:
                if self.ffield is None:
                    self.ffield = fft2(self.ifield)
                angle = -np.pi / 180 * (self.polarizer - sample)
                c, s = np.cos(angle), np.sin(angle)

                self.data = _redim(self.ffield, ndim=6)
                x = c * self.data[:, 0]
                y = s * self.data[:, 1]
                self.data = x + y
            else:
                self.data = _redim(self.ffield, ndim=5)

        if recalc or self._has_parameter_updated("focus"):
            if self.diffraction == True or self.mode is not None:
                #if mode is selected, we need to project the field using diffraction
                d = 0 if self.focus is None else self.focus
                self.dmat = field_diffraction_matrix(self.ifield.shape[-2:],
                                                     self.ks,
                                                     d=d,
                                                     epsv=self.epsv,
                                                     epsa=self.epsa,
                                                     mode=self.mode,
                                                     betamax=self.betamax)
            else:
                self.dmat = np.asarray(np.diag((1, 1, 1, 1)), CDTYPE)
        if recalc or self._has_parameter_updated("analyzer", "sample"):
            sample = self.sample if self.sample is not None else 0.
            if self.analyzer is not None:
                angle = -np.pi / 180 * (self.analyzer - sample)
                c, s = np.cos(angle), np.sin(angle)
                self.pmat = mode_polarizer(self.ifield.shape[-2:],
                                           self.ks,
                                           jones=(c, s),
                                           epsv=self.epsv,
                                           epsa=self.epsa,
                                           betamax=self.betamax)
            else:
                self.pmat = None

        if recalc or self._has_parameter_updated(
                "analyzer", "sample", "polarizer", "focus", "intensity"):
            tmat = None
            if self.pmat is not None and self.dmat is not None:
                tmat = dotmm(self.pmat, self.dmat)
            if self.pmat is None and self.dmat is not None:
                tmat = self.dmat
            if self.pmat is not None and self.dmat is None:
                tmat = self.pmat
            if tmat is not None:
                self.ofield = dotmf(tmat, self.data, out=self.ofield)
            self.ofield = ifft2(self.ofield, out=self.ofield)

            for i, data in enumerate(self.ofield):
                if i == 0:
                    self.specter = field2specter(data)
                else:
                    self.specter += field2specter(data)

            recalc = True

        if recalc or "intensity" in self._updated_parameters:
            self._updated_parameters.clear()
            self._updated_parameters.add(
                "intensity")  #trigger calculate_image call
        else:
            self._updated_parameters.clear()

        return self.specter
Ejemplo n.º 5
0
    def _calculate_specter_normal(self, recalc=False, **params):
        """Calculates field specter.
        
        Parameters
        ----------
        recalc : bool, optional
            If specified, it forces recalculation. Otherwise, result is calculated
            only if calculation parameters have changed.
        params: kwargs, optional
            Any additional keyword arguments that are passed dirrectly to 
            set_parameters method.
        """
        self.set_parameters(**params)
        if self.ofield is None:
            recalc = True  #first time only trigger calculation
        if recalc or "focus" in self._updated_parameters:
            if self.diffraction == True or self.mode is not None:
                #if mode is selected, we need to project the filed using diffraction
                d = 0 if self.focus is None else self.focus
                dmat = field_diffraction_matrix(self.ifield.shape[-2:],
                                                self.ks,
                                                d=d,
                                                epsv=self.epsv,
                                                epsa=self.epsa,
                                                mode=self.mode,
                                                betamax=self.betamax)

                self.ofield = diffract(self.ifield,
                                       dmat,
                                       window=self.window,
                                       out=self.ofield)
            else:
                #no diffraction at all..
                if self.window is not None:
                    self.ofield = self.ifield * self.window
                else:
                    self.ofield = self.ifield.copy()
            recalc = True
        if recalc or "polarizer" in self._updated_parameters or "analyzer" in self._updated_parameters or "sample" in self._updated_parameters:
            sample = self.sample
            if sample is None:
                sample = 0.
            if self.polarizer is None:
                tmp = _redim(self.ofield, ndim=5)
                out = np.empty_like(tmp[0])
            else:
                angle = -np.pi / 180 * (self.polarizer - sample)
                c, s = np.cos(angle), np.sin(angle)
                tmp = _redim(self.ofield, ndim=6)
                out = np.empty_like(tmp[0, 0])
            if self.analyzer is not None:

                angle = -np.pi / 180 * (self.analyzer - sample)
                #pmat = linear_polarizer(angle)
                pmat = normal_polarizer((np.cos(angle), np.sin(angle)))
                #pmat = ray_polarizer((np.cos(angle),np.sin(angle)),epsv = self.epsv, epsa = self.epsa)

            for i, data in enumerate(tmp):
                if self.polarizer is not None:
                    x = data[0] * c
                    y = np.multiply(data[1], s, out=out)
                    ffield = np.add(
                        x, y, out=out)  #numexpr.evaluate("x*c+y*s", out = out)
                else:
                    ffield = data

                if self.analyzer is not None:
                    #pfield = apply_jones_matrix(pmat, ffield, out = out)
                    pfield = dotmf(pmat, ffield, out=out)
                else:
                    pfield = ffield
                if i == 0:
                    self.specter = field2specter(pfield)
                else:
                    self.specter += field2specter(pfield)
            recalc = True

        if recalc or "intensity" in self._updated_parameters:
            self._updated_parameters.clear()
            self._updated_parameters.add(
                "intensity")  #trigger calculate_image call
        else:
            self._updated_parameters.clear()
        return self.specter