def test_get_rgb_image_rois(self):
        s = load(KIKUCHIPY_FILE)
        vbse_gen = VirtualBSEGenerator(s)

        # Get channels by ROIs
        rois1 = [
            vbse_gen.roi_from_grid(i) for i in np.ndindex(vbse_gen.grid_shape)
        ][:3]
        vbse_rgb_img1 = vbse_gen.get_rgb_image(r=rois1[0],
                                               g=rois1[1],
                                               b=rois1[2])

        # Get channels from grid tile indices
        rois2 = [(0, 0), (0, 1), (0, 2)]
        vbse_rgb_img2 = vbse_gen.get_rgb_image(r=rois2[0],
                                               g=rois2[1],
                                               b=rois2[2])

        assert isinstance(vbse_rgb_img1, VirtualBSEImage)
        assert vbse_rgb_img1.data.dtype == np.dtype([("R", "u1"), ("G", "u1"),
                                                     ("B", "u1")])

        vbse_rgb_img1.change_dtype(np.uint8)
        vbse_rgb_img2.change_dtype(np.uint8)
        assert np.allclose(vbse_rgb_img1.data, vbse_rgb_img2.data)
    def test_get_rgb_1d(self):
        s = EBSD(np.random.random(9 * 3600).reshape((9, 60, 60)))
        vbse_gen = VirtualBSEGenerator(s)

        with pytest.raises(ValueError,
                           match="The signal dimension cannot be "):
            _ = vbse_gen.get_rgb_image(r=(0, 0), g=(0, 1), b=(0, 2))
    def test_get_rgb_alpha_signal(self):
        s = load(KIKUCHIPY_FILE)
        vbse_gen = VirtualBSEGenerator(s)

        vbse_img = s.get_virtual_bse_intensity(roi=RectangularROI(0, 0, 10, 10))

        vbse_rgb_img1 = vbse_gen.get_rgb_image(
            r=(0, 1), g=(0, 2), b=(0, 3), alpha=vbse_img
        )
        vbse_rgb_img2 = vbse_gen.get_rgb_image(
            r=(0, 1), g=(0, 2), b=(0, 3), alpha=vbse_img.data,
        )
        vbse_rgb_img1.change_dtype(np.uint8)
        vbse_rgb_img2.change_dtype(np.uint8)

        assert np.allclose(vbse_rgb_img1.data, vbse_rgb_img2.data)
    def test_get_rgb_multiple_rois_per_channel(self, r, g, b,
                                               desired_mean_intensity):
        s = load(KIKUCHIPY_FILE)
        vbse_gen = VirtualBSEGenerator(s)

        vbse_rgb_img1 = vbse_gen.get_rgb_image(r=r, g=g, b=b)
        vbse_rgb_img1.change_dtype(np.uint8)

        assert np.allclose(vbse_rgb_img1.data.mean(), desired_mean_intensity)

        roi_r = vbse_gen.roi_from_grid(r)
        roi_g = vbse_gen.roi_from_grid(g)
        roi_b = vbse_gen.roi_from_grid(b)
        vbse_rgb_img2 = vbse_gen.get_rgb_image(r=roi_r, g=roi_g, b=roi_b)
        vbse_rgb_img2.change_dtype(np.uint8)

        assert np.allclose(vbse_rgb_img1.data, vbse_rgb_img2.data)
    def test_get_rgb_image_lazy(self):
        s = load(KIKUCHIPY_FILE, lazy=True)
        vbse_gen = VirtualBSEGenerator(s)

        assert isinstance(vbse_gen.signal, LazyEBSD)

        vbse_rgb_img = vbse_gen.get_rgb_image(r=(0, 0), g=(0, 1), b=(0, 2))

        assert isinstance(vbse_rgb_img.data, np.ndarray)
    def test_get_rgb_image_dtype(self):
        s = load(KIKUCHIPY_FILE)
        vbse_gen = VirtualBSEGenerator(s)
        vbse_rgb_img = vbse_gen.get_rgb_image(r=(0, 0),
                                              g=(0, 1),
                                              b=(0, 2),
                                              dtype_out=np.uint16)

        assert vbse_rgb_img.data.dtype == np.dtype([("R", "u2"), ("G", "u2"),
                                                    ("B", "u2")])
    def test_get_rgb_image_contrast_stretching(self, percentile,
                                               desired_mean_intensity):
        s = load(KIKUCHIPY_FILE)
        vbse_gen = VirtualBSEGenerator(s)
        vbse_rgb_img = vbse_gen.get_rgb_image(r=(0, 0),
                                              g=(0, 1),
                                              b=(0, 2),
                                              percentiles=percentile)
        vbse_rgb_img.change_dtype(np.uint8)

        assert np.allclose(vbse_rgb_img.data.mean(), desired_mean_intensity)
    def test_get_rgb_alpha(self, alpha_add, desired_mean_intensity):
        s = load(KIKUCHIPY_FILE)
        vbse_gen = VirtualBSEGenerator(s)

        alpha = np.arange(9).reshape((3, 3))
        alpha[0] += alpha_add

        vbse_rgb_img = vbse_gen.get_rgb_image(
            r=(0, 0), g=(0, 1), b=(0, 2), alpha=alpha
        )
        vbse_rgb_img.change_dtype(np.uint8)

        assert np.allclose(vbse_rgb_img.data.mean(), desired_mean_intensity)
 def test_normalize_rgb_raises(self, dummy_signal):
     vbse_gen = VirtualBSEGenerator(dummy_signal)
     vbse_gen.grid_shape = (3, 3)
     rgb = vbse_gen.get_rgb_image(r=(0, 0), g=(0, 1), b=(1, 0))
     with pytest.raises(NotImplementedError):
         rgb.normalize_intensity()