def test_sparse_neighborhood_padded(self): in_shape = np.array((4, 5), dtype=np.int64) kernel_shape = np.array((3, 3), dtype=np.int64) strides = np.array((2, 2), dtype=np.int64) padding = np.array((1, 1), dtype=np.int64) p, indices, splits, out_shape = grid.sparse_neighborhood( in_shape, kernel_shape, strides, padding=padding) np.testing.assert_equal(out_shape, (2, 3)) np.testing.assert_equal( p, (4, 5, 7, 8, 3, 4, 5, 6, 7, 8, 3, 4, 6, 7, 1, 2, 4, 5, 7, 8) + tuple(range(9)) + (0, 1, 3, 4, 6, 7), ) np.testing.assert_equal( indices, [ 0, 1, 5, 6, 1, 2, 3, 6, 7, 8, 3, 4, 8, 9, 5, 6, 10, 11, 15, 16, 6, 7, 8, 11, 12, 13, 16, 17, 18, 8, 9, 13, 14, 18, 19, ], ) np.testing.assert_equal(splits, [0, 4, 10, 14, 20, 29, 35])
def test_sparse_neighborhood(self): in_shape = np.array((4, 5), dtype=np.int64) kernel_shape = np.array((3, 3), dtype=np.int64) strides = np.array((2, 2), dtype=np.int64) padding = np.array((0, 0), dtype=np.int64) p, indices, splits, out_shape = grid.sparse_neighborhood( in_shape, kernel_shape, strides, padding=padding) np.testing.assert_equal(out_shape, (1, 2)) np.testing.assert_equal(p, tuple(range(9)) * 2) np.testing.assert_equal( indices, [0, 1, 2, 5, 6, 7, 10, 11, 12, 2, 3, 4, 7, 8, 9, 12, 13, 14]) np.testing.assert_equal(splits, [0, 9, 18])
def test_sparse_neighborhood_1d(self): in_shape = np.array((7, )) kernel_shape = np.array((3, )) strides = np.array((2, )) padding = np.array((0, )) p, indices, splits, out_shape = grid.sparse_neighborhood( in_shape, kernel_shape, strides, padding=padding) np.testing.assert_equal(out_shape, (3, )) np.testing.assert_equal(p, tuple(range(3)) * 3) np.testing.assert_equal(indices, [0, 1, 2, 2, 3, 4, 4, 5, 6]) np.testing.assert_equal(splits, [0, 3, 6, 9]) in_shape = np.array((7, )) kernel_shape = np.array((2, )) strides = np.array((2, )) padding = np.array((0, )) p, indices, splits, out_shape = grid.sparse_neighborhood( in_shape, kernel_shape, strides, padding=padding) np.testing.assert_equal(out_shape, (3, )) np.testing.assert_equal(p, tuple(range(2)) * 3) np.testing.assert_equal(indices, [0, 1, 2, 3, 4, 5]) np.testing.assert_equal(splits, [0, 2, 4, 6])
def test_pointwise_neighbors(self): in_size = 1024 out_size = 256 grid_size = 10 t_end = 10000 in_times = np.random.uniform(high=t_end, size=in_size).astype(np.int64) in_coords = np.random.uniform(high=grid_size, size=in_size).astype(np.int64) in_times.sort() out_times = np.random.uniform(high=t_end, size=out_size).astype(np.int64) out_coords = np.random.uniform(high=grid_size, size=out_size).astype(np.int64) out_times.sort() partitions, indices, splits, out_shape = grid.sparse_neighborhood( np.array([grid_size]), np.array([1]), np.array([1]), np.array([0])) np.testing.assert_equal(out_shape, [grid_size]) np.testing.assert_equal(partitions, 0) np.testing.assert_equal(indices, np.arange(grid_size)) np.testing.assert_equal(splits, np.arange(grid_size + 1)) spatial_buffer_size = 32 actual_indices, actual_splits = neigh.compute_pointwise_neighbors( in_times, in_coords, out_times, out_coords, spatial_buffer_size) part, expected_indices, expected_splits = neigh.compute_neighbors( in_times, in_coords, out_times, out_coords, partitions, indices, splits, spatial_buffer_size, ) np.testing.assert_equal(part, 0) np.testing.assert_equal(actual_indices, expected_indices) np.testing.assert_equal(actual_splits, expected_splits) assert len(actual_indices) > 0
def test_compute_neighbors_2d_finite(self): # in_coords = np.array([ # [0, 0], # [2, 3], # [1, 1], # ], dtype=np.int64) # out_coords = np.array([ # [0, 0], # [2, 2], # ], dtype=np.int64) neigh_parts, neigh_coords, splits, out_shape = grid.sparse_neighborhood( np.array((3, 4)), np.array((3, 3)), strides=np.array((1, 1)), padding=np.array((1, 1)), ) np.testing.assert_equal(out_shape, (3, 4)) in_coords = np.array((0, 11, 5)) out_coords = np.array((0, 11)) in_times = np.array([0, 2, 4], dtype=np.int64) out_times = np.array([3, 5]) event_duration = None spatial_buffer_size = 4 partitions, indices, splits = neigh.compute_neighbors( in_times=in_times, in_coords=in_coords, out_times=out_times, out_coords=out_coords, grid_partitions=neigh_parts, grid_indices=neigh_coords, grid_splits=splits, event_duration=event_duration, spatial_buffer_size=spatial_buffer_size, ) np.testing.assert_equal(partitions, [4, 4]) np.testing.assert_equal(indices, [0, 1]) np.testing.assert_equal(splits, [0, 1, 2])
frame_kwargs = dict(num_frames=60) for events, label in dataset: coords = events["coords"].numpy() time = events["time"].numpy() polarity = events["polarity"].numpy() img_data = [as_frames(coords, time, polarity, **frame_kwargs)] coords = grid.ravel_multi_index_transpose(coords, dims) sizes = [time.size] curr_shape = dims for _ in range(3): partitions, indices, splits, curr_shape = grid.sparse_neighborhood( in_shape=curr_shape, kernel_shape=np.array((kernel_size, kernel_size), dtype=np.int64), strides=np.array((stride, stride), dtype=np.int64), padding=np.array((padding, padding), dtype=np.int64), ) # spatial_leaky_integrate_and_fire needs transposed grid indices, splits, partitions = transpose_csr(indices, splits, partitions) time, coords = spatial_leaky_integrate_and_fire( times=time, coords=coords, grid_indices=indices, grid_splits=splits, decay_time=decay_time, ) sizes.append(time.size)
def create_sparse_neighborhood(): return grid.sparse_neighborhood(in_shape, kernel_shape, strides, top_left)
import matplotlib.pyplot as plt import numpy as np from scipy.sparse import csr_matrix from numba_stream import grid in_shape = np.array((4, 5), dtype=np.int64) kernel_shape = np.array((3, 3), dtype=np.int64) strides = np.array((2, 2), dtype=np.int64) padding = np.array((1, 1), dtype=np.int64) p, indices, splits, shape = grid.sparse_neighborhood( in_shape, kernel_shape, strides, padding ) print(shape) print(indices) print(splits) print(indices.shape) print(splits.shape) values = np.ones_like(indices) mat = csr_matrix((values, indices, splits)) plt.spy(mat) plt.show()