def test_nonzero_minimum_box():
    test = cle.push(np.asarray([
        [0, 0, 0, 0, 0],
        [0, 1, 1, 2, 0],
        [0, 2, 2, 3, 0],
        [0, 3, 3, 4, 0],
        [0, 0, 0, 0, 0]
    ]))

    reference = cle.push(np.asarray([
        [0, 0, 0, 0, 0],
        [0, 1, 1, 1, 0],
        [0, 1, 1, 1, 0],
        [0, 2, 2, 2, 0],
        [0, 0, 0, 0, 0]
    ]))

    result = cle.create(test)
    flag = cle.create((1, 1, 1))

    # as nonzero filters don't touch zero values, we need to initialize the result in advance
    cle.set(result, 0);

    cle.nonzero_minimum_box(test, flag, result)

    print(result)

    a = cle.pull(result)
    b = cle.pull(reference)
    assert (np.allclose(a, b, atol=0.00001))
def test_write_values_to_positions_2d():
    positions_and_values = cle.push(np.asarray([
        [0, 0, 2, 3, 5],
        [0, 1, 3, 2, 6],
        [8, 7, 6, 5, 4]
    ]))


    reference = cle.push(np.asarray([
        [8, 0, 0, 0, 0, 0],
        [7, 0, 0, 0, 0, 0],
        [0, 0, 0, 5, 0, 0],
        [0, 0, 6, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 4]
    ]))

    result = cle.create_like(reference)
    cle.set(result, 0)
    result = cle.write_values_to_positions(positions_and_values, result)

    a = cle.pull(result)
    b = cle.pull(reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
예제 #3
0
 def draw_positions(self, new_positions, value):
     """bring position lists in the right format and draw the list of coordinates in a given color on the playground.
     """
     positions = cle.push(np.asarray(new_positions))
     values_and_positions = cle.create(
         [positions.shape[0] + 1, positions.shape[1]])
     cle.set(values_and_positions, value)
     cle.paste(positions, values_and_positions, 0, 0)
     cle.write_values_to_positions(values_and_positions, self.temp)
def test_maximum_sphere_2():
    gpu_a = cle.push(np.asarray([[1, 1, 1], [1, 2, 1], [1, 1, 1]]))
    gpu_b = cle.create(gpu_a)
    cle.maximum_sphere(gpu_a, gpu_b, 1, 1, 1)

    a = cle.pull(gpu_b)
    assert np.min(a) == 1
    assert np.max(a) == 2

    cle.set(gpu_a, 5)
    assert np.all(cle.pull(gpu_a) == 5)
def test_copy_slice_to_3d():
    test1 = cle.push(np.asarray([[3, 4], [4, 5]]))

    test2 = cle.create((2, 2, 2))
    cle.set(test2, 0)
    cle.copy_slice(test1, test2, 0)

    print(test2)
    a = cle.pull(test2)
    assert (np.min(a) == 0)
    assert (np.max(a) == 5)
    assert (np.mean(a) == 2)
예제 #6
0
def test_set():
    result = cle.create((5, 5))

    reference = cle.push(
        np.asarray([[3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 3, 3, 3],
                    [3, 3, 3, 3, 3], [3, 3, 3, 3, 3]]))

    cle.set(result, 3)

    print(result)

    a = cle.pull(result)
    b = cle.pull(reference)
    assert (np.allclose(a, b, 0.001))
def test_draw_line():
    reference = cle.push_zyx(
        np.asarray([[2, 2, 0, 0, 0], [2, 2, 2, 0, 0], [0, 2, 2, 2, 0],
                    [0, 0, 2, 2, 2], [0, 0, 0, 2, 2]]))

    result = cle.create((5, 5))
    cle.set(result, 0)
    cle.draw_line(result, 1, 1, 0, 4, 4, 0, 1, 2)

    print(result)

    a = cle.pull(result)
    b = cle.pull(reference)

    assert (np.array_equal(a, b))
def test_draw_box():
    reference = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 2, 2, 2, 0], [0, 2, 2, 2, 0],
                    [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]))

    result = cle.create((5, 5))
    cle.set(result, 0)
    cle.draw_box(result, 1, 1, 0, 2, 1, 1, 2)

    print(result)

    a = cle.pull(result)
    b = cle.pull(reference)

    assert (np.array_equal(a, b))
def test_maximum_sphere_1():
    test = cle.push(np.asarray([
        [1, 1, 1],
        [1, 2, 1],
        [1, 1, 1]
    ]))

    test2 = cle.create(test)
    cle.maximum_sphere(test, test2, 1, 1, 1)

    a = cle.pull(test2)
    assert (np.min(a) == 1)
    assert (np.max(a) == 2)

    cle.set(test, 5)
    # print(test)
    # print(cle.pull(test))

    a = cle.pull(test)
    assert (np.min(a) == 5)
    assert (np.max(a) == 5)
    assert (np.mean(a) == 5)
def test_touch_matrix_to_mesh():

    gpu_touch_matrix = cle.push(np.asarray([[0, 0, 0], [0, 0, 0], [0, 1, 0]]))

    gpu_point_list = cle.push(np.asarray([[1, 4], [2, 5]]))

    gpu_output = cle.create([5, 5])
    cle.set(gpu_output, 0)

    gpu_reference = cle.push(
        np.asarray([[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0],
                    [0, 0, 0, 0, 1], [0, 0, 0, 0, 0]]).T)

    cle.touch_matrix_to_mesh(gpu_point_list, gpu_touch_matrix, gpu_output)

    a = cle.pull(gpu_output)
    b = cle.pull(gpu_reference)

    print(a)
    print(b)

    assert (np.array_equal(a, b))
    def game_step(self):
        """Forwards the game by one step and computes the new playground

        Returns
        -------
            an image with the current state of the game
        """

        # read camera image
        _, picture = self._video_source.read()

        # push to GPU and bring in right format
        current_image = self._push_and_format(picture)

        # determine
        difference = cle.sum_z_projection(
            cle.squared_difference(current_image, self._image_at_beginning))
        crop_left = cle.crop(difference,
                             start_x=0,
                             start_y=0,
                             width=self.sensitive_area_width,
                             height=self.height)
        crop_right = cle.crop(difference,
                              start_x=self.width - self.sensitive_area_width,
                              start_y=0,
                              width=self.sensitive_area_width,
                              height=self.height)

        # check player positions
        self.player1_position = self._determine_player_position(
            self.player1_position, crop_left)
        self.player2_position = self._determine_player_position(
            self.player2_position, crop_right)
        squared_difference_picture = cle.pull(difference)

        # move puck
        self.puck_x = self.puck_x + self.puck_delta_x
        self.puck_y = self.puck_y + self.puck_delta_y

        # check puck_position
        if self.puck_y < 0 or self.puck_y > self.height:
            self.puck_delta_y = -self.puck_delta_y
            self.puck_y = self.puck_y + self.puck_delta_y

        # puck at player 1
        if self.puck_x <= self.player1_x:
            self.puck_delta_x = -self.puck_delta_x
            self.puck_x = self.puck_x + self.puck_delta_x

            if abs(self.puck_y - self.player1_position) > self.bar_radius:
                # player 2 scores
                self.player2_score = self.player2_score + 1
                self._level_up()
            else:
                self.puck_delta_y = (
                    self.puck_y - self.player1_position) / self.bar_radius * 5

        # puck at player 2
        if self.puck_x >= self.player2_x:
            self.puck_delta_x = -self.puck_delta_x
            self.puck_x = self.puck_x + self.puck_delta_x

            if abs(self.puck_y - self.player2_position) > self.bar_radius:
                # player 1 scores
                self.player1_score = self.player1_score + 1
                self._level_up()
            else:
                self.puck_delta_y = (
                    self.puck_y - self.player2_position) / self.bar_radius * 5

        # show game status
        self.result_label.setText(
            str(self.player1_score) + ":" + str(self.player2_score))

        # draw playground
        cle.set(self.playground, 0.1)

        # draw player 1
        cle.draw_box(self.playground, self.player1_x,
                     self.player1_position - self.bar_radius, 0, 3,
                     self.bar_radius * 2, 0)

        # draw player 2
        cle.draw_box(self.playground, self.player2_x,
                     self.player2_position - self.bar_radius, 0, 3,
                     self.bar_radius * 2, 0)

        # draw puck
        cle.draw_sphere(self.playground, self.puck_x - self.puck_width / 2,
                        self.puck_y - self.puck_height / 2, 0, self.puck_width,
                        self.puck_height, 1)

        # binary playground image
        game_state = cle.pull(self.playground)

        # turn camera view in single channel grey-value image
        grey = cle.mean_z_projection(current_image)

        # warp the image
        gradient_radius = 25
        position_delta = 25000
        blurred_playground = cle.gaussian_blur(self.playground,
                                               sigma_x=gradient_radius,
                                               sigma_y=gradient_radius)
        gradient = cle.laplace_box(blurred_playground)
        vector_x = cle.multiply_image_and_scalar(gradient,
                                                 scalar=position_delta)
        vector_y = cle.multiply_image_and_scalar(gradient,
                                                 scalar=position_delta)
        warped_playground = cle.apply_vector_field(grey, vector_x, vector_y)

        return [
            # images to add in napari
            [
                cle.pull(grey), squared_difference_picture, game_state,
                cle.pull(vector_x),
                cle.pull(vector_x),
                cle.pull(warped_playground)
            ],
            # names
            [
                'camera input', 'squared difference', 'game state', 'vector x',
                'vector y', 'warped view'
            ],
            # visibility
            [False, False, False, False, False, True]
        ]
예제 #12
0
    def game_loop(self):
        """
        This function is called at every game iteration. It checks if bullets hit nuclei and redraws the playground
        """
        bullet_radius = 5

        # empty playground
        cle.set(self.playground, 0)

        # pull nuclei from GPU so that we can access individual pixels
        nuclei = np.asarray(self.nuclei)
        # prepare all existing labels in a list. We set them to 0 in case a nucleus was hit
        labels_to_keep = list(range(0, int(np.max(self.nuclei) + 1)))

        # future bullets to keep
        new_bullets = []

        for bullet in self.bullets:
            bullet[1] += 10

            # check if a bullet has hit a nucleus
            try:
                label = nuclei[int(self.playground.shape[0] - bullet[1]),
                               int(bullet[0] + self.fov_x)]
            except IndexError:
                label = 0
            if label != 0:  # bullet has hit a nucleus
                labels_to_keep[label] = 0
            elif bullet[1] > self.playground.shape[1]:
                pass  # bullet has left the playground
            else:
                new_bullets.append(bullet)
                # draw bullets
                cle.draw_sphere(self.playground, bullet[0],
                                self.playground.shape[0] - bullet[1], 0,
                                bullet_radius, bullet_radius, 1, 1)
        self.bullets = new_bullets

        # only keep cells where the nuclei weren't hit
        new_label_ids = np.asarray(labels_to_keep)
        cle.replace_intensities(self.nuclei, new_label_ids, self.nuclei)
        cle.replace_intensities(self.cells, new_label_ids, self.cells)

        # make a binary image of areas to keep
        binary = self.cells > 0

        # draw player
        cle.draw_box(self.playground, self.player_position - 5,
                     self.playground.shape[0] - 20, 0, 10, 20, 1, 2)
        cle.draw_box(self.playground, self.player_position - 15,
                     self.playground.shape[0] - 10, 0, 30, 10, 1, 2)

        # collect all layers in a dictionary
        result = {}
        for i, image in enumerate(self.images):
            result["channel" + str(i)] = self.crop_fov(
                cle.multiply_images(image, binary))

        # add segmentation (invisble) and playground
        result['nuclei'] = self.crop_fov(self.nuclei, self.fov_nuclei)
        result['cells'] = self.crop_fov(self.cells, self.fov_cells)
        result['playground'] = cle.copy(self.playground)

        self.fov_x += self.fov_delta_x
        if self.fov_x <= 0:
            self.fov_x = 0
            self.fov_delta_x = 1
        elif self.fov_x >= self.fov_max_x:
            self.fov_x = self.fov_max_x
            self.fov_delta_x = -1

        return result
예제 #13
0
    def game_step(self):
        """Forwards the game by one step and computes the new playground

        Returns
        -------
            an image with the current state of the game
        """

        # check player positions
        self.player1_y, self.player1_z = self._check_player_position(
            self.player1_y, self.player1_z)
        self.player2_y, self.player2_z = self._check_player_position(
            self.player2_y, self.player2_z)

        # move puck
        self.puck_x = self.puck_x + self.puck_delta_x
        self.puck_y = self.puck_y + self.puck_delta_y
        self.puck_z = self.puck_z + self.puck_delta_z

        # check puck_position
        if self.puck_y < 0 or self.puck_y > self.height:
            self.puck_delta_y = -self.puck_delta_y
            self.puck_y = self.puck_y + self.puck_delta_y
        if self.puck_z < 0 or self.puck_z > self.depth:
            self.puck_delta_z = -self.puck_delta_z
            self.puck_z = self.puck_z + self.puck_delta_z

        # puck at player 1
        if self.puck_x <= self.player1_x:
            self.puck_delta_x = -self.puck_delta_x
            self.puck_x = self.puck_x + self.puck_delta_x

            if abs(self.puck_y - self.player1_y) > self.bar_radius or abs(
                    self.puck_z - self.player1_z) > self.bar_radius:
                # player 2 scores
                self.player2_score = self.player2_score + 1
                self._level_up()
            else:
                self.puck_delta_y = (self.puck_y -
                                     self.player1_y) / self.bar_radius * 5
                self.puck_delta_z = (self.puck_z -
                                     self.player1_z) / self.bar_radius * 5

        # puck at player 2
        if self.puck_x >= self.player2_x:
            self.puck_delta_x = -self.puck_delta_x
            self.puck_x = self.puck_x + self.puck_delta_x

            if abs(self.puck_y - self.player2_y) > self.bar_radius or abs(
                    self.puck_z - self.player2_z) > self.bar_radius:
                # player 1 scores
                self.player1_score = self.player1_score + 1
                self._level_up()
            else:
                self.puck_delta_y = (self.puck_y -
                                     self.player2_y) / self.bar_radius * 5
                self.puck_delta_z = (self.puck_z -
                                     self.player2_z) / self.bar_radius * 5

        # show game status
        self.result_label.setText(
            str(self.player1_score) + ":" + str(self.player2_score))

        # draw playground
        cle.set(self.playground, 0.0)

        # draw player 1
        cle.draw_box(self.playground, self.player1_x,
                     self.player1_y - self.bar_radius,
                     self.player1_z - self.bar_radius, 3, self.bar_radius * 2,
                     self.bar_radius * 2)

        # draw player 2
        cle.draw_box(self.playground, self.player2_x,
                     self.player2_y - self.bar_radius,
                     self.player2_z - self.bar_radius, 3, self.bar_radius * 2,
                     self.bar_radius * 2)

        # draw puck
        cle.draw_sphere(self.playground, self.puck_x, self.puck_y, self.puck_z,
                        5, 3, 3, 1)

        # put z-color coding on playground
        cle.multiply_images(self.playground, self.gradient, self.view)

        # return playground
        image = cle.pull_zyx(self.view)

        return image
예제 #14
0
    def game_step(self):
        """Forwards the game by one step and computes the new playground

        Returns
        -------
            an image with the current state of the game
        """

        # check player positions
        self.player1_position = self._check_player_position(self.player1_position)
        self.player2_position = self._check_player_position(self.player2_position)

        # move puck
        self.puck_x = self.puck_x + self.puck_delta_x
        self.puck_y = self.puck_y + self.puck_delta_y

        # check puck_position
        if self.puck_y < 0 or self.puck_y > self.height:
            self.puck_delta_y = -self.puck_delta_y
            self.puck_y = self.puck_y + self.puck_delta_y

        # puck at player 1
        if self.puck_x <= self.player1_x:
            self.puck_delta_x = -self.puck_delta_x
            self.puck_x = self.puck_x + self.puck_delta_x

            if abs(self.puck_y - self.player1_position) > self.bar_radius:
                # player 2 scores
                self.player2_score = self.player2_score + 1
                self._level_up()
            else:
                self.puck_delta_y = (self.puck_y - self.player1_position) / self.bar_radius * 5

        # puck at player 2
        if self.puck_x >= self.player2_x:
            self.puck_delta_x = -self.puck_delta_x
            self.puck_x = self.puck_x + self.puck_delta_x

            if abs(self.puck_y - self.player2_position) > self.bar_radius:
                # player 1 scores
                self.player1_score = self.player1_score + 1
                self._level_up()
            else:
                self.puck_delta_y = (self.puck_y - self.player2_position) / self.bar_radius * 5

        # show game status
        self.result_label.setText(str(self.player1_score) + ":" + str(self.player2_score))

        # draw playground
        cle.set(self.playground, 0.1)

        # draw player 1
        cle.draw_box(self.playground, self.player1_x, self.player1_position - self.bar_radius, 0, 3, self.bar_radius * 2, 0)

        # draw player 2
        cle.draw_box(self.playground, self.player2_x, self.player2_position - self.bar_radius, 0, 3, self.bar_radius * 2, 0)

        # draw puck
        cle.draw_sphere(self.playground, self.puck_x, self.puck_y, 0, 5, 3, 1)

        # return playground
        image = cle.pull_zyx(self.playground)

        return image