def test_get_peak_parameters_no_peak(self, repeat):
        utils.np.random.seed(1234 * repeat)

        img, p, cutoff, s_noise = utils.create_test_image(peak=False)

        with pytest.raises(utils.LargeNoiseError):
            utils.get_peak_parameters(img)
    def test_get_peak_parameters_images(self, file, params):
        img = imageio.imread(file, as_gray=True)

        h, a, x0, y0, sx, sy, theta, cutoff = params

        p_fit = utils.get_peak_parameters(img)
        h_f, a_f, x0_f, y0_f, sx_f, sy_f, theta_f, cutoff_f = p_fit

        s_noise = 1  # representative for the noise of most images
        rel = max(s_noise / cutoff, 1e-2)

        assert x0_f == approx(x0, rel=1e-3)
        assert y0_f == approx(y0, rel=1e-3)
        assert h_f == approx(h, rel=rel, abs=2 * s_noise)  # rather inaccurate
        assert a_f == approx(a, rel=3 * a / cutoff * rel,
                             abs=2 * s_noise)  # inaccurate if a >> cutoff
        assert sx_f == approx(sx, rel=0.1)
        assert sy_f == approx(sy, rel=0.1)
        if sx == approx(sy, rel=0.05):
            assert theta_f == approx(theta, abs=1)
        elif sx == approx(sy, rel=0.1):
            assert theta_f == approx(theta, abs=0.2)
        else:
            assert theta_f == approx(theta, abs=0.05)
        assert cutoff_f == approx(cutoff, rel=rel,
                                  abs=3 * s_noise)  # inaccurate due to filter
    def test_get_peak_parameters(self, repeat):
        utils.np.random.seed(1234 * repeat)

        # ensure strong enough signal without large eccentricity
        h, a, x0, y0, sx, sy, theta, img_gauss, cutoff, s_noise = [0] * 10
        while (cutoff - h) <= 4 * s_noise or eccentricity(sx, sy) > 0.95:
            img_gauss, p, cutoff, s_noise = utils.create_test_image()
            h, a, x0, y0, sx, sy, theta = p

        p_fit = utils.get_peak_parameters(img_gauss)
        h_f, a_f, x0_f, y0_f, sx_f, sy_f, theta_f, cutoff_f = p_fit

        rel = max(s_noise / cutoff, 1e-2)

        assert x0_f == approx(x0, rel=1e-3)
        assert y0_f == approx(y0, rel=1e-3)
        assert h_f == approx(h, rel=rel, abs=2 * s_noise)  # rather inaccurate
        assert a_f == approx(a, rel=3 * a / cutoff * rel,
                             abs=2 * s_noise)  # inaccurate if a >> cutoff
        assert sx_f == approx(sx, rel=0.1)
        assert sy_f == approx(sy, rel=0.1)
        if sx == approx(sy, rel=0.05):
            assert theta_f == approx(theta, abs=1)
        elif sx == approx(sy, rel=0.1):
            assert theta_f == approx(theta, abs=0.2)
        else:
            assert theta_f == approx(theta, abs=0.05)
        assert cutoff_f == approx(cutoff, rel=rel,
                                  abs=3 * s_noise)  # inaccurate due to filter
Beispiel #4
0
    def __call__(self, data):
        if self.key:
            img = data.value.pop(self.key)
        else:
            img = data.value

        if img is None:
            return data

        img = img.astype(np.float64)

        try:
            p_fit = utils.get_peak_parameters(img)
        except utils.FittingError:
            p_fit = None

        if p_fit:
            self.log_frames(data.timestamp, img, p_fit)
            h, a, x0, y0, sx, sy, theta, cutoff = p_fit
            d = {
                "beam_on": True,
                "mu_x": x0,
                "mu_y": y0,
                "sigma_x": sx,
                "sigma_y": sy,
                "rotation": theta,
                "z_offset": h,
                "amplitude": a,
                "cutoff": cutoff
            }
        else:
            d = {"beam_on": False}

        if self.key:
            data.value.update(d)
        else:
            data.value = d

        return data
    def test_get_peak_parameters_images_no_beam_raw(self, file):
        img = np.load(file)

        with pytest.raises((utils.LargeNoiseError, utils.SmallRegionError)):
            utils.get_peak_parameters(img)
    def test_get_peak_parameters_images_beam_raw(self, file):
        img = np.load(file)

        p_fit = utils.get_peak_parameters(img)
        assert p_fit
    def test_get_peak_parameters_images_no_beam(self, file):
        img = imageio.imread(file, as_gray=True)

        with pytest.raises((utils.LargeNoiseError, utils.SmallRegionError)):
            utils.get_peak_parameters(img)