def test_rate_map(): box_size = [1., 1.] rate = 5. bin_size = [.01, .01] n_step=10**4 step_size=.1 sigma=0.1 spacing=0.3 smoothing = .03 rate_map_true, pos_fields, xbins, ybins = make_test_grid_rate_map( sigma=sigma, spacing=spacing, amplitude=rate, box_size=box_size, bin_size=bin_size) x, y, t, spikes = make_test_spike_map( pos_fields=pos_fields, box_size=box_size, rate=rate, n_step=n_step, step_size=step_size, sigma=sigma) smap = SpatialMap( x, y, t, spikes, box_size=box_size, bin_size=bin_size) rate_map = smap.rate_map(smoothing) diff = rate_map_true - rate_map X, Y = np.meshgrid(xbins, ybins) samples = [] for p in pos_fields: mask = np.sqrt((X - p[0])**2 + (Y - p[1])**2) < .1 samples.append(diff[mask]) peak_diff = np.abs(np.mean([s.min() for s in samples if s.size > 0])) assert peak_diff < 0.5
def test_gridness(): box_size = np.array([1., 1.]) rate = 5. bin_size = [.01, .01] spacing_true = 0.3 rate_map, pos_fields, xbins, ybins = make_test_grid_rate_map( sigma=0.05, spacing=spacing_true, amplitude=rate, offset=0, box_size=box_size, bin_size=bin_size) g = gridness(rate_map) assert round(g, 1) == 1.3
def test_spacing_and_orientation_from_true_peaks(): box_size = np.array([1., 1.]) rate = 5. bin_size = [.01, .01] spacing_true = 0.3 rate_map, pos_fields, xbins, ybins = make_test_grid_rate_map( sigma=0.05, spacing=spacing_true, amplitude=rate, offset=0, box_size=box_size, bin_size=bin_size) spacing, orientation = spacing_and_orientation(pos_fields, box_size) assert spacing == spacing_true assert round(orientation * 180 / np.pi) == 30
def test_separate_fields_by_distance(): box_size = [1., 1.] rate = 1. bin_size = [.01, .01] rate_map, pos_true, xbins, ybins = make_test_grid_rate_map( sigma=0.05, spacing=0.3, amplitude=rate, offset=0, box_size=box_size, bin_size=bin_size) peaks, radius = separate_fields_by_distance(rate_map) bump_centers = np.array([xbins[peaks[:, 0]], ybins[peaks[:, 1]]]) # The position of a 2D bin is defined to be its center for p in pos_true: assert np.isclose(p, pos_true).prod(axis=1).any()
def test_separate_fields_by_laplace(): box_size = [1., 1.] rate = 1. bin_size = [.01, .01] sigma = 0.05 spacing = 0.3 rate_map, pos_true, xbins, ybins = make_test_grid_rate_map( sigma=sigma, spacing=spacing, amplitude=rate, offset=0.1, box_size=box_size, bin_size=bin_size, orientation=0.1) labels = separate_fields_by_laplace(rate_map, threshold=0) peaks = calculate_field_centers(rate_map, labels) bump_centers = np.array([xbins[peaks[:, 0]], ybins[peaks[:, 1]]]) # The position of a 2D bin is defined to be its center for p in pos_true: assert np.isclose(p, pos_true).prod(axis=1).any()
def test_find_peaks(): box_size = np.array([1., 1.]) rate = 5. bin_size = [.01, .01] sigma = 0.05 spacing = 0.3 rate_map, pos_fields, xbins, ybins = make_test_grid_rate_map( sigma=sigma, spacing=spacing, amplitude=rate, offset=0, box_size=box_size, bin_size=bin_size, repeat=0) peaks = find_peaks(rate_map) pos_peaks = np.array([xbins[peaks[:, 1]], ybins[peaks[:, 0]]]).T print(pos_peaks) assert all([ np.isclose(p, pos_peaks, rtol=1e-3).prod(axis=1).any() for p in pos_fields ])
def test_spacing_and_orientation_from_autocorr(): box_size = np.array([1., 1.]) rate = 5. bin_size = [.01, .01] spacing_true = 0.3 orientation_true = .3 rate_map, pos_fields, xbins, ybins = make_test_grid_rate_map( sigma=0.05, spacing=spacing_true, amplitude=rate, offset=0, box_size=box_size, bin_size=bin_size, orientation=orientation_true) autocorrelogram = autocorrelation(rate_map) peaks = find_peaks(autocorrelogram) real_peaks = peaks * bin_size autocorrelogram_box_size = box_size * autocorrelogram.shape[ 0] / rate_map.shape[0] spacing, orientation = spacing_and_orientation(real_peaks, autocorrelogram_box_size) assert round(spacing, 1) == spacing_true assert round(orientation, 1) == orientation_true