Esempio n. 1
0
    def encode(self, location, grid_cells=None):
        location = list(location)
        assert (len(location) == 2)
        if grid_cells is None:
            grid_cells = SDR((self.size, ))
        if any(math.isnan(x) for x in location):
            grid_cells.zero()
            return grid_cells

        # Find the distance from the location to each grid cells nearest
        # receptive field center.
        # Convert the units of location to hex grid with angle 0, scale 1, offset 0.
        displacement = location - self.offsets_
        radius = np.empty(self.size)
        for mod_idx in range(len(self.partitions_)):
            start, stop = self.partitions_[mod_idx]
            R = self.rot_mats_[mod_idx]
            displacement[start:stop] = R.dot(displacement[start:stop].T).T
            radius[start:stop] = self.periods[mod_idx] / 2
        # Convert into and out of hexagonal coordinates, which rounds to the
        # nearest hexagons center.
        nearest = hexy.cube_to_pixel(hexy.pixel_to_cube(displacement, radius),
                                     radius)
        # Find the distance between the location and the RF center.
        distances = np.hypot(*(nearest - displacement).T)
        # Activate the closest grid cells in each module.
        index = []
        for start, stop in self.partitions_:
            z = int(round(self.sparsity * (stop - start)))
            index.extend(np.argpartition(distances[start:stop], z)[:z] + start)
        grid_cells.sparse = index
        return grid_cells
Esempio n. 2
0
    def handle_events(self):
        running = True
        for event in pg.event.get():
            if event.type == pg.QUIT:
                running = False
            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    self.clicked_hex = hx.pixel_to_cube(
                        np.array([pg.mouse.get_pos() - self.center]),
                        self.hex_radius)
                    self.clicked_hex = self.clicked_hex[0]
                if event.button == 3:
                    self.selection_type += 1
                if event.button == 4:
                    self.rad += 1
                if event.button == 5:
                    self.rad -= 1

            if event.type == pg.KEYUP:
                if event.key == pg.K_UP:
                    self.rad += 1
                elif event.key == pg.K_DOWN:
                    self.rad -= 1

            if event.type == pg.KEYDOWN:
                if event.key == pg.K_ESCAPE:
                    running = False

        return running
Esempio n. 3
0
def test_cube_to_pixel_conversion():
    axial_coords = hx.cube_to_axial(coords)
    cube_coords = hx.axial_to_cube(axial_coords)
    pixel_coords = hx.cube_to_pixel(cube_coords, radius)
    pixel_to_cube_coords = hx.pixel_to_cube(pixel_coords, radius)
    pixel_to_cube_to_axial_coords = hx.cube_to_axial(pixel_to_cube_coords)

    assert np.array_equal(cube_coords, pixel_to_cube_coords)
Esempio n. 4
0
def test_the_converted_coords_and_dataset_coords_retrieve_the_same_data():
    axial_coords = hx.cube_to_axial(coords)
    cube_coords = hx.axial_to_cube(axial_coords)
    pixel_coords = hx.cube_to_pixel(cube_coords, radius)
    pixel_to_cube_coords = hx.pixel_to_cube(pixel_coords, radius)
    pixel_to_cube_to_axial_coords = hx.cube_to_axial(pixel_to_cube_coords)

    # check that we can correctly retrieve hexes after conversions
    hm[axial_coords] = coords
    retrieved = hm[pixel_to_cube_to_axial_coords]

    assert np.array_equal(retrieved, coords)
Esempio n. 5
0
    def draw(self):
        # show all hexes
        hexagons = list(self.hex_map.values())
        hex_positions = np.array(
            [hexagon.get_draw_position() for hexagon in hexagons])
        sorted_idxs = np.argsort(hex_positions[:, 1])
        for idx in sorted_idxs:
            self.main_surf.blit(hexagons[idx].image,
                                hex_positions[idx] + self.center)

        # draw values of hexes
        for hexagon in list(self.hex_map.values()):
            text = self.font.render(str(hexagon.value), False, (0, 0, 0))
            text.set_alpha(160)
            text_pos = hexagon.get_position() + self.center
            text_pos -= (text.get_width() / 2, text.get_height() / 2)
            self.main_surf.blit(text, text_pos)

        mouse_pos = np.array([np.array(pg.mouse.get_pos()) - self.center])
        cube_mouse = hx.pixel_to_cube(mouse_pos, self.hex_radius)

        # choose either ring or disk
        rad_hex = Selection.get_selection(self.selection_type, cube_mouse,
                                          self.rad, self.clicked_hex)

        rad_hex_axial = hx.cube_to_axial(rad_hex)
        hexes = self.hex_map[rad_hex_axial]

        list(
            map(
                lambda hexagon: self.main_surf.blit(
                    self.selected_hex_image,
                    hexagon.get_draw_position() + self.center), hexes))

        # draw hud
        self.selection_type

        selection_type_text = self.font.render(
            "(Right Click To Change) Selection Type: " +
            Selection.Type.to_string(self.selection_type), True, (50, 50, 50))
        radius_text = self.font.render(
            "(Scroll Mouse Wheel To Change) Radius: " + str(self.rad), True,
            (50, 50, 50))
        fps_text = self.font.render(" FPS: " + str(int(self.clock.get_fps())),
                                    True, (50, 50, 50))
        self.main_surf.blit(fps_text, (5, 0))
        self.main_surf.blit(radius_text, (5, 15))
        self.main_surf.blit(selection_type_text, (5, 30))

        # Update screen
        pg.display.update()
        self.main_surf.fill(COLORS[-1])
        self.clock.tick(30)
Esempio n. 6
0
    def encode(self, location, grid_cells=None):
        """
        Transform a 2-D coordinate into an SDR.

        Argument location: pair of coordinates, such as "[X, Y]"

        Argument grid_cells: Optional, the SDR object to store the results in.
                             Its dimensions must be "[GridCellEncoder.size]"

        Returns grid_cells, an SDR object.  This will be created if not given.
        """
        location = list(location)
        assert (len(location) == 2)
        if grid_cells is None:
            grid_cells = SDR((self.size, ))
        else:
            assert (isinstance(grid_cells, SDR))
            assert (grid_cells.dimensions == [self.size])
        if any(math.isnan(x) for x in location):
            grid_cells.zero()
            return grid_cells

        # Find the distance from the location to each grid cells nearest
        # receptive field center.
        # Convert the units of location to hex grid with angle 0, scale 1, offset 0.
        displacement = location - self.offsets_
        radius = np.empty(self.size)
        for mod_idx in range(len(self.partitions_)):
            start, stop = self.partitions_[mod_idx]
            R = self.rot_mats_[mod_idx]
            displacement[start:stop] = R.dot(displacement[start:stop].T).T
            radius[start:stop] = self.periods[mod_idx] / 2
        # Convert into and out of hexagonal coordinates, which rounds to the
        # nearest hexagons center.
        nearest = hexy.cube_to_pixel(hexy.pixel_to_cube(displacement, radius),
                                     radius)
        # Find the distance between the location and the RF center.
        distances = np.hypot(*(nearest - displacement).T)
        # Activate the closest grid cells in each module.
        index = []
        for start, stop in self.partitions_:
            z = int(round(self.sparsity * (stop - start)))
            index.extend(np.argpartition(distances[start:stop], z)[:z] + start)
        grid_cells.sparse = index
        return grid_cells
 def encode(self, location):
     # Find the distance from the location to each grid cells nearest
     # receptive field center.
     # Convert the units of location to hex grid with angle 0, scale 1, offset 0.
     displacement = location - self.offsets
     radius = np.empty(self.n)
     for mod_idx in range(len(self.module_partitions)):
         start, stop = self.module_partitions[mod_idx]
         R = self.rot_mats[mod_idx]
         displacement[start:stop] = R.dot(displacement[start:stop].T).T
         radius[start:stop] = self.scales[mod_idx] / 2
     # Convert into and out of hexagonal coordinates, which rounds to the
     # nearest hexagons center.
     nearest = hexy.cube_to_pixel(hexy.pixel_to_cube(displacement, radius),
                                  radius)
     # Find the distance between the location and the RF center.
     distances = np.hypot(*(nearest - displacement).T)
     # Activate the closest grid cells in each module.
     index = []
     for start, stop in self.module_partitions:
         z = int(round(self.sparsity * (stop - start)))
         index.extend(np.argpartition(distances[start:stop], z)[:z] + start)
     self.grid_cells.flat_index = np.array(index)
     return self.grid_cells
Esempio n. 8
0
 def __init__(self, radius):
     self.hex_radius = radius
     self.mouse_pos = np.array([pg.mouse.get_pos()])
     self.mouse_cube = hx.pixel_to_cube(self.mouse_pos, self.hex_radius)
     self.mouse_axial = hx.pixel_to_axial(self.mouse_pos, self.hex_radius)
     self.mouse_hex = hx.cube_to_pixel(self.mouse_cube, self.hex_radius)
Esempio n. 9
0
 def update(self, camera):
     self.mouse_pos = np.array([camera.follow_camera(pg.mouse.get_pos())])
     self.mouse_cube = hx.pixel_to_cube(self.mouse_pos, self.hex_radius)
     self.mouse_axial = hx.pixel_to_axial(self.mouse_pos, self.hex_radius)
     self.mouse_hex = hx.cube_to_pixel(self.mouse_cube, self.hex_radius)
Esempio n. 10
0
 def set_position(self, position):
     self.position = position
     self.axial_coordinates = hx.pixel_to_axial(self.position, self.radius)
     self.cube_coordinates = hx.pixel_to_cube(self.position, self.radius)
Esempio n. 11
0
        return True
    return False


radius = 10
coords = hx.get_spiral(np.array((0, 0, 0)), 1, 10)

# check axial <-> cube conversions work
axial_coords = hx.cube_to_axial(coords)
cube_coords = hx.axial_to_cube(axial_coords)

print same_mat(coords, cube_coords)

# check axial <-> pixel conversions work
pixel_coords = hx.axial_to_pixel(axial_coords, radius)
pixel_to_axial_coords = hx.pixel_to_axial(pixel_coords, radius)

print same_mat(axial_coords, pixel_to_axial_coords)

# check cube <-> pixel conversions work
pixel_coords = hx.cube_to_pixel(cube_coords, radius)
pixel_to_cube_coords = hx.pixel_to_cube(pixel_coords, radius)
pixel_to_cube_to_axial_coords = hx.cube_to_axial(pixel_to_cube_coords)

print same_mat(cube_coords, pixel_to_cube_coords)

# check that we can correctly retrieve hexes after conversions
hm[axial_coords] = coords
retrieved = hm[pixel_to_cube_to_axial_coords]
print same_mat(retrieved, cube_coords)