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
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)
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)
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
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)
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)
import hexy as hx import numpy as np import matplotlib.pyplot as plt import grids as grids radius = 1000 # [m] radius = 1291.472 # [m] area = hx.get_area(radius) * 3 cube0 = np.array([ [0, 0, 0], [1, 0, -1], [0, 1, -1], ]) pos = hx.cube_to_pixel(cube0, radius) corners = hx.get_corners(pos, radius) sh = np.array(corners).shape corners = corners.transpose(0, 2, 1) corners = np.array(corners).reshape((sh[0] * sh[2], sh[1])) corners_x, corners_y = grids.remove_redundant_point(corners[:, 0], corners[:, 1]) pos_gp13 = np.array([corners_x, corners_y]).transpose() plt.figure(1) plt.clf() plt.plot(pos[:, 0], pos[:, 1], 'k.', label='hex. center') plt.plot(corners_x, corners_y, 'r.', label='antenna position')
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)