def test_position_combine_wrong_dimension2(): position = nept.Position([[1, 1, 1]], [0, 1, 2]) pos = nept.Position([[8, 3, 4], [6, 8, 4]], [0.5, 1, 2.5]) with pytest.raises(ValueError) as excinfo: combined = position.combine(pos) assert str(excinfo.value) == "'pos' must be 1 dimensions"
def test_position_combine_same_times(): position = nept.Position([[1, 1, 1], [2, 2, 2]], [0, 1, 2]) pos = nept.Position([[8, 3, 4], [6, 8, 4]], [0, 1, 2]) combined = position.combine(pos) assert np.allclose(combined.time, np.array([0., 0., 1., 1., 2., 2.])) assert np.allclose(combined.x, np.array([1., 8., 1., 3., 1., 4.])) assert np.allclose(combined.y, np.array([2., 6., 2., 8., 2., 4.]))
def test_get_xyedges_one_full(): times = np.array([1.0, 2.0, 3.0]) data = np.array([[1.0, 1.1], [5.0, 5.1], [10.0, 10.1]]) position = nept.Position(data, times) position = nept.Position(data, times) xedges, yedges = nept.get_xyedges(position, binsize=10) assert np.allclose(xedges, np.array([1., 11.])) assert np.allclose(yedges, np.array([1.1, 11.1]))
def test_position_distance_1d(): times = np.array([1.0, 2.0, 3.0]) x = np.array([1.1, 0.9, 2.3]) y = np.array([3.1, 2.0, 1.4]) pos = nept.Position(x, times) other = nept.Position(y, times) distance_pos = pos.distance(other) distance_other = other.distance(pos) assert np.allclose(distance_pos, np.array([2.0, 1.1, 0.9])) assert np.all(distance_other == distance_pos)
def test_position_distance_dimensions(): times = np.array([1.0, 2.0, 3.0]) data = np.array([[1.0, 3.1], [2.0, 2.1], [3.0, 1.1]]) x = np.array([1.1, 0.9, 2.3]) pos = nept.Position(data, times) other = nept.Position(x, times) with pytest.raises(ValueError) as excinfo: dist = pos.distance(other) assert str(excinfo.value) == "'pos' must be 2 dimensions"
def test_position_distance_2d(): times = np.array([1.0, 2.0, 3.0]) data = np.array([[1.0, 3.1], [2.0, 2.1], [3.0, 1.1]]) times2 = np.array([1.0, 2.0, 3.0]) data2 = np.array([[1.2, 3.3], [2.0, 2.6], [3.0, 1.1]]) pos = nept.Position(data, times) other = nept.Position(data2, times2) dist = pos.distance(other) assert np.allclose(dist, np.array([0.28284271, 0.5, 0.0]))
def decode_location(likelihood, pos_centers, time_centers): """Finds the decoded location based on the centers of the position bins. Parameters ---------- likelihood : np.array With shape(n_timebins, n_positionbins) pos_centers : np.array time_centers : np.array Returns ------- decoded : nept.Position Estimate of decoded position. """ keep_idx = np.sum(np.isnan(likelihood), axis=1) < likelihood.shape[1] likelihood = likelihood[keep_idx] max_decoded_idx = np.nanargmax(likelihood, axis=1) decoded_data = pos_centers[max_decoded_idx] decoded_time = time_centers[keep_idx] return nept.Position(decoded_data, decoded_time)
def test_get_heatmaps(): position = np.vstack([ np.arange(0, 10, 1), np.hstack((np.arange(0, 10, 2), np.arange(10, 0, -2))) ]), position = nept.Position(position, np.arange(0, 30, 3)) neuron_list = [0, 2, 3] spikes = [ nept.SpikeTrain(np.array([19.9, 20., 20.1]), 'test'), nept.SpikeTrain(np.array([8.]), 'test'), nept.SpikeTrain(np.array([0., 15., 27.]), 'test'), nept.SpikeTrain(np.array([9., 10., 11., 15., 16.]), 'test') ] heatmaps = nept.get_heatmaps(neuron_list, spikes, position, num_bins=5) assert np.allclose(len(heatmaps), 3) assert np.allclose(np.max(heatmaps[0]), 3.) assert np.allclose(np.mean(heatmaps[0]), 0.12) assert np.allclose(np.max(heatmaps[2]), 1.) assert np.allclose(np.mean(heatmaps[2]), 0.12) assert np.allclose(np.max(heatmaps[3]), 2.) assert np.allclose(np.mean(heatmaps[3]), 0.2)
def test_position_idx_in_pos(): position = nept.Position([[0, 1, 2], [9, 7, 5]], [10, 11, 12]) pos = position[1] assert np.allclose(pos.x, 1) assert np.allclose(pos.y, 7) assert np.allclose(pos.time, 11)
def test_position_speed_simple_rest(): times = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]) data = np.array([0.0, 1.0, 1.0, 1.0, 0.0, 0.0]) pos = nept.Position(data, times) speed = pos.speed(t_smooth=None) assert np.allclose(speed.data, np.array([[0.0], [1.0], [0.0], [0.0], [1.0], [0.0]]))
def test_positon_speed_simple(): times = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) data = np.array([0.0, 0.5, 1.0, 0.7, 1.7]) pos = nept.Position(data, times) speed = pos.speed() assert np.allclose(speed.data, np.array([[0.0], [0.5], [0.5], [0.3], [1.0]]))
def test_position_distance_diff_size(): times = np.array([1.0, 2.0, 3.0, 4.0]) data = np.array([[1.0, 3.1], [2.0, 2.1], [3.0, 1.1], [4.0, 0.1]]) times2 = np.array([1.0, 2.0, 3.0]) data2 = np.array([[1.2, 3.3], [2.0, 2.6], [3.0, 1.1]]) pos = nept.Position(data, times) other = nept.Position(data2, times2) with pytest.raises(ValueError) as excinfo: dist = pos.distance(other) assert str(excinfo.value) == "'pos' must have 4 samples"
def test_position_1d_y(): times = np.array([1.0, 2.0, 3.0]) x = np.array([1.1, 0.9, 2.3]) pos = nept.Position(x, times) with pytest.raises(ValueError) as excinfo: y_val = pos.y assert str(excinfo.value) == "can't get 'y' of one-dimensional position"
def test_positon_speed_simple_false_smooth(): times = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) data = np.array([0.0, 0.5, 1.0, 0.7, 1.7]) pos = nept.Position(data, times) # No smoothing occurs when t_smooth > dt speed = pos.speed(t_smooth=0.1) assert np.allclose(speed.data, np.array([[0.0], [0.5], [0.5], [0.3], [1.0]]))
def test_position_y_setter_array(): times = np.array([1.0, 2.0, 3.0, 4.0]) data = np.array([[1.0, 3.1], [2.0, 2.1], [3.0, 1.1], [4.0, 0.1]]) position = nept.Position(data, times) position.y = np.array([0.0, 1.0, 2.0, 3.0]) assert np.allclose(position.y, np.array([0.0, 1.0, 2.0, 3.0]))
def test_position_x_setter_value(): times = np.array([1.0, 2.0, 3.0, 4.0]) data = np.array([[1.0, 3.1], [2.0, 2.1], [3.0, 1.1], [4.0, 0.1]]) position = nept.Position(data, times) position.x = 3.3 assert np.allclose(position.x, np.array([3.3, 3.3, 3.3, 3.3]))
def test_get_xyedges_1d_position(): times = np.array([1.0, 2.0, 3.0]) data = np.array([1.0, 5.0, 10.0]) position = nept.Position(data, times) with pytest.raises(ValueError) as excinfo: xedges, yedges = nept.get_xyedges(position, binsize=3) assert str(excinfo.value) == "position must be 2-dimensional"
def test_position_xy_reshaped(): times = np.array([1.0, 2.0, 3.0]) x = np.array([1.1, 0.9, 2.3]) y = np.array([3.1, 2.0, 1.4]) pos = nept.Position(np.array([x, y]), times) assert np.allclose(pos.x, np.array([1.1, 0.9, 2.3])) assert np.allclose(pos.y, np.array([3.1, 2.0, 1.4])) assert np.allclose(pos.time, np.array([1.0, 2.0, 3.0]))
def test_position_speed_unequal_time(): time = np.hstack((np.linspace(0, 10, 10), np.linspace(11, 101, 10))) data = np.arange(0, 20) position = nept.Position(data, time) speed = position.speed() run_idx = np.squeeze(speed.data) >= 0.7 run_position = position[run_idx] assert np.allclose(len(run_position.x), 10)
def test_run_threshold_simple(): times = np.array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0]) data = np.array([0.0, 0.5, 1.0, 0.7, 1.7, 2.0]) position = nept.Position(data, times) run_epoch = nept.run_threshold(position, thresh=0.4, t_smooth=None) assert np.allclose(run_epoch.starts, np.array([1., 4.])) assert np.allclose(run_epoch.stops, np.array([3., 5.]))
def test_filter_jumps_empty(): decoded = nept.Position(np.array([10., 20., 30., 40.]), np.array([0., 1., 2., 3.])) decoded_sequences = nept.remove_teleports(decoded, speed_thresh=9, min_length=3) assert np.allclose(decoded_sequences.starts, np.array([])) assert np.allclose(decoded_sequences.stops, np.array([]))
def test_get_xyedges_mult(): times = np.array([1.0, 2.0, 3.0]) data = np.array([[1.0, 1.1], [5.0, 5.1], [10.0, 10.1]]) position = nept.Position(data, times) xedges, yedges = nept.get_xyedges(position, binsize=3) assert np.allclose(xedges, np.array([1., 4., 7., 10.])) assert np.allclose(yedges, np.array([1.1, 4.1, 7.1, 10.1]))
def test_position_xy(): times = np.array([1.0, 2.0, 3.0]) data = np.array([[1.1, 3.1], [0.9, 2.0], [2.3, 1.4]]) pos = nept.Position(data, times) assert np.allclose(pos.x, np.array([1.1, 0.9, 2.3])) assert np.allclose(pos.y, np.array([3.1, 2.0, 1.4])) assert np.allclose(pos.time, np.array([1.0, 2.0, 3.0]))
def test_binned_position_2d_position(): position = nept.Position( np.hstack([ np.array([2, 4, 6, 8])[..., np.newaxis], np.array([7, 5, 3, 1])[..., np.newaxis] ]), np.array([0., 1., 2., 3.])) binsize = 2 with pytest.raises(ValueError) as excinfo: nept.binned_position(position, binsize=binsize) assert str(excinfo.value) == 'position must be linear'
def test_position_speed_simple_smooth(): times = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) data = np.array([0.0, 0.5, 1.0, 0.7, 1.7]) pos = nept.Position(data, times) speed = pos.speed(t_smooth=0.3) assert np.allclose(speed.data, np.array([[0.00191813], [0.49808187], [0.49923275], [0.30345263], [0.99347836]]))
def test_position_noy_setter(): times = np.array([1.0, 2.0, 3.0, 4.0]) data = np.array([[1.0], [2.0], [3.0], [4.0]]) position = nept.Position(data, times) with pytest.raises(ValueError) as excinfo: position.y = np.array([0.0, 1.0, 2.0, 3.0]) assert str(excinfo.value) == "can't set 'y' of one-dimensional position"
def test_position_empty_epoch_slice(): times = np.array([1.0, 2.0, 3.0, 4.0]) data = np.array([[1.0, 3.1], [2.0, 2.1], [3.0, 1.1], [4.0, 0.1]]) position = nept.Position(data, times) epochs = nept.Epoch([], []) sliced_position = position[epochs] assert sliced_position.time.size == 0
def test_sort_idx(): linear = nept.Position(np.linspace(0, 10, 4), np.linspace(0, 3, 4)) spikes = [ nept.SpikeTrain(np.array([1.5]), 'test'), nept.SpikeTrain(np.array([0.5]), 'test'), nept.SpikeTrain(np.array([2.5]), 'test') ] tuning = nept.tuning_curve_1d(linear, spikes, binsize=3, gaussian_std=None) sort_idx = nept.get_sort_idx(tuning) assert np.allclose(sort_idx, [1, 0, 2])
def test_simple_tc(): linear = nept.Position(np.linspace(0, 10, 4), np.linspace(0, 3, 4)) spikes = [ nept.SpikeTrain(np.array([0.5]), 'test'), nept.SpikeTrain(np.array([1.5]), 'test'), nept.SpikeTrain(np.array([2.5]), 'test') ] tuning = nept.tuning_curve_1d(linear, spikes, binsize=3, gaussian_std=None) assert np.allclose(tuning, ([1., 0., 0., 0.], [0., 1., 0., 0.], [0., 0., 1., 0.]))
def test_position_epoch_slice(): times = np.array([1.0, 2.0, 3.0, 4.0]) data = np.array([[1.0, 3.1], [2.0, 2.1], [3.0, 1.1], [4.0, 0.1]]) position = nept.Position(data, times) epochs = nept.Epoch([1.8], [3.2]) sliced_position = position[epochs] assert np.allclose(sliced_position.time, np.array([2., 3.])) assert np.allclose(sliced_position.data, np.array([[2., 2.1], [3., 1.1]]))