def test_output_creation(): """ verify that the output array is the correct shape and size, can be indexed correctly """ out, inc = func(times, offset_lim, iterations, t_start, t_stop) assert out.shape == (iterations, times.size) assert out[0].shape == times.shape return
def test_2d_bin_edges(): arena_size = np.array((80, 120)) pos = (np.random.rand(1000, 2) * arena_size).transpose() limits = (0, 80.001, 0, 120.001) bin_edges = [np.arange(arena_size[i] + 1) for i in range(2)] hist, edges = func(pos, arena_size=arena_size, limits=limits, bin_edges=bin_edges) for i in range(2): assert np.array_equal(edges[i], bin_edges[i]) # Also test that passing in an array instead of a list works: bin_edges = np.array(bin_edges) hist, edges = func(pos, arena_size=arena_size, limits=limits, bin_edges=bin_edges) for i in range(2): assert np.array_equal(edges[i], bin_edges[i])
def test_smoothing_simple(): data = spio.loadmat(th.test_data_square) ds = np.arange(th.get_data_size(data)) for key in ds: bnt, tr = get_time_map_bnt(data, key) ope = func(tr, sigma=2, mask_fill=0) abs_error = np.abs(bnt - ope) rel_error = abs_error / np.nanmax(bnt) assert (np.ma.less_equal(rel_error, 1e-4).all())
def test_1d_input(): arena_size = 80 pos = np.random.rand(1000) * arena_size bin_width = 2.32 limits = (np.nanmin(pos), np.nanmax(pos) * 1.0001) hist, edges = func(pos, arena_size=arena_size, limits=limits, bin_width=bin_width) assert hist.ndim == 1 assert hist.size == opexebo.general.bin_width_to_bin_number(arena_size, bin_width) assert edges.size == hist.size + 1 assert pos.size == np.sum(hist)
def test_integer_scaling_2d(): upscale = int(2) ndarray = np.random.rand(50, 50) maarray = ndarray.copy() maarray[25:28, 10:16] = np.nan maarray = np.ma.masked_invalid(maarray) # Test behavior on masked array new_array = func(maarray, upscale) assert np.array_equal( np.array(maarray.shape) * upscale, np.array(new_array.shape)) assert np.sum(maarray.mask) * (upscale**2) == np.sum(new_array.mask) # Test behavior on ndarray new_array = func(ndarray, upscale) assert np.array_equal( np.array(ndarray.shape) * upscale, np.array(new_array.shape)) print("test_integer_scaling_2d passed") return True
def test_2d_bin_edges(): arena_size = np.array((80, 120)) pos = (np.random.rand(1000, 2) * arena_size).transpose() limits = (0, 80.001, 0, 120.001) bin_edges = [np.arange(arena_size[i] + 1) for i in range(2)] hist, edges = func(pos, arena_size=arena_size, limits=limits, bin_edges=bin_edges) for i in range(2): assert np.array_equal(edges[i], bin_edges[i])
def test_edge_cases(): """ Test some obvious edge cases """ # Basic arguments t_start = 1 t_stop = 13 offset_lim = 2.3 iterations = 1000 times = np.arange(t_start + 0.5, t_stop - 0.5) # Few spikes compared to large time range out, _ = func(times, 1e3, iterations, t_start, 1e4) assert np.array_equal(np.diff(times), np.diff(out[0])) # One spike spk1 = np.array([5.3]) out, _ = func(spk1, offset_lim, iterations, t_start, t_stop) assert out.shape == (iterations, spk1.size)
def test_2d_bin_number(): arena_size = np.array((80, 120)) pos = (np.random.rand(1000, 2) * arena_size).transpose() limits = (0, 80.001, 0, 120.001) bin_number = (8, 12) hist, (edge_x, edge_y) = func(pos, arena_size=arena_size, limits=limits, bin_number=bin_number) assert edge_x.size == bin_number[0] + 1 assert edge_y.size == bin_number[1] + 1 assert pos.shape[1] == np.sum(hist) bin_number = 8 hist, (edge_x, edge_y) = func(pos, arena_size=arena_size, limits=limits, bin_number=bin_number) assert edge_x.size == edge_y.size == bin_number + 1 assert pos.shape[1] == np.sum(hist)
def test_output_logic(): """ Verify that the output is a shuffled copy of the input """ out, inc = func(times, offset_lim, iterations, t_start, t_stop) assert np.min(out) >= t_start assert np.max(out) <= t_stop for row in out: assert np.array_equal(row, np.array(sorted(row))) return
def test_increment_creation(): """ Verify that the increments are correctly created between (t_start + offset_lim) and (t_stop - offset_lim) """ out, inc = func(times, offset_lim, iterations, t_start, t_stop) assert inc.size == iterations assert min(inc) >= t_start + offset_lim assert (min(inc) <= t_start + offset_lim + 0.5 ) # this is a random test, so it _could_ fail... assert max(inc) <= t_stop - offset_lim assert max(inc) >= t_stop - offset_lim - 0.5 return
def test_output_structure(): """Verify that the function correctly returns an array where each row is a time-shifted copy of the input, with num_shuffle rows""" times = np.random.randint(0, 100, size=1000) offset_lim = 13 iterations = 274 tr = np.arange(111) output, _ = func(times, offset_lim, iterations, tracking_range=tr) # output.shape = rows, cols assert output.shape[0] == iterations assert output.shape[1] == times.size print("test_output_structure() passed")
def test_1d2d_input(): """ It is possible to give 1d data as a 2d array, i.e. with shape (1, n) """ arena_size = 80 pos = np.random.rand(1000) * arena_size pos = np.expand_dims(pos, 0) bin_width = 2.32 limits = (np.nanmin(pos), np.nanmax(pos) * 1.0001) hist, edges = func(pos, arena_size=arena_size, limits=limits, bin_width=bin_width) assert hist.ndim == 1 assert hist.size == opexebo.general.bin_width_to_bin_number(arena_size, bin_width) assert edges.size == hist.size + 1 assert pos.size == np.sum(hist)
def test_fractional_scaling_2d(): upscale = float(1.45) ndarray = np.random.rand(50, 75) new_array = func(ndarray, upscale) assert np.array_equal(np.round(np.array(ndarray.shape) * upscale), new_array.shape) print("test_fractional_scaling_2d passed") return True #if __name__ == '__main__': # test_invalid_inputs() # test_integer_scaling_2d() # test_fractional_scaling_2d()
def test_large_circle(): axes = [np.linspace(-100, 100, 501) for i in range(2)] diameter = 175 mask, distance_map, angular_map = func(axes, diameter) plt.close("all") fig, ax = plt.subplots(1,3) ax[0].imshow(mask) ax[0].invert_yaxis() ax[1].imshow(distance_map) ax[1].invert_yaxis() im2 = ax[2].imshow(angular_map) ax[2].invert_yaxis() fig.colorbar(im2, orientation='horizontal') #if __name__ == '__main__': # test_large_circle()
def test_invalid_inputs(): # No `arena_size` keyword with pytest.raises(TypeError): pos = np.random.rand(100) func(pos) # Misdefined bins with pytest.raises(KeyError): pos = np.random.rand(100) func(pos, arena_size=1, bin_number=10, bin_width=2.5) # Misdefined `limit` keyword with pytest.raises(ValueError): post = np.random.rand(100) func(post, arena_size=1, limits="abc")
def test_trivial_inputs_2d(): num_bins = (10, 11) '''Correlation of all-zeros should be NaN''' arr0 = np.zeros(num_bins) arr1 = np.zeros(num_bins) p = func(arr0, arr1) assert (np.isnan(p[0])) assert (np.isnan(p[1]).all()) '''correlation of equal arange''' arr0 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins) arr1 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins) p = func(arr0, arr1, row_major=True) assert (np.isclose(p[0], 1)) assert (np.isclose(p[1], 1).all()) p = func(arr0, arr1, row_major=False) assert (np.isclose(p[1], 1).all()) '''correlation of opposite arange''' arr0 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins) arr1 = np.flip(np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins)) p = func(arr0, arr1, row_major=True) assert (np.isclose(p[0], -1)) assert (np.isclose(p[1], -1).all()) p = func(arr0, arr1, row_major=False) assert (np.isclose(p[1], -1).all()) '''Handling occasional NaNs''' arr0 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins).astype(float) arr1 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins).astype(float) arr0[2, 3] = np.nan arr1[4, 8] = np.nan p = func(arr0, arr1, row_major=True) assert (np.isclose(p[0], 1)) assert (np.isclose(p[1], 1).all()) assert (np.sum(np.isnan(p[1])) == 0) p = func(arr0, arr1, row_major=False) assert (np.isclose(p[1], 1).all()) assert (np.sum(np.isnan(p[1])) == 0) print("test_trivial_inputs_2d passed") #if __name__ == "__main__": # test_trivial_inputs_1d() # test_trivial_inputs_2d()
def single(): key = 10 bnt, tr = get_time_map_bnt(data, key) ope = func(tr, sigma=2, mask_fill=0) ratio = (bnt - ope) / np.nanmax(bnt) plt.figure() plt.subplot(1, 3, 1) plt.title("BNT") plt.imshow(bnt) plt.colorbar() plt.subplot(1, 3, 2) plt.title("Opexebo") plt.imshow(ope) plt.colorbar() plt.subplot(1, 3, 3) plt.title("Ratio") plt.imshow(ratio) plt.colorbar() plt.show()
def test_invalid_inputs(): with pytest.raises(ValueError): # 1d array, mismatched sizes arr0 = np.zeros(5) arr1 = np.zeros(6) func(arr0, arr1) with pytest.raises(ValueError): # 2d array, mismatched sizes arr0 = np.zeros((3, 3)) arr1 = np.zeros((3, 4)) func(arr0, arr1) with pytest.raises(ValueError): # Random arguments arr0 = np.zeros(5) arr1 = "b" func(arr0, arr1) print("test_invalid_inputs passed") return True
def test_2d_input(): arena_size = np.array((80, 120)) pos = (np.random.rand(1000, 2) * arena_size).transpose() limits = (0, 80.001, 0, 120.001) bin_width = 4.3 hist, (edge_x, edge_y) = func(pos, arena_size=arena_size, limits=limits, bin_width=bin_width) assert edge_x[0] == limits[0] assert hist.ndim == 2 for i in range(hist.ndim): # Note: the array is transposed, so the shape swaps order # print(hist.shape) # print(opexebo.general.bin_width_to_bin_number(arena_size, bin_width)) # print(edge_x[0], edge_x[1]) # print(np.min(pos[0]), np.max(pos[0])) assert (hist.shape[i] == opexebo.general.bin_width_to_bin_number( arena_size, bin_width)[i - 1]) assert pos.shape[1] == np.sum(hist)
def test_stupid_values(): times = np.random.randint(0, 100, size=1000) offset_lim = 13 iterations = 274 tr = np.arange(111) with pytest.raises(ValueError): stupid_times = np.random.randint(0, 100, size=(100, 100)) # wrong shape func(stupid_times, offset_lim, iterations, tracking_range=tr) with pytest.raises(ValueError): stupid_times = times.copy().astype(float) stupid_times[813] = np.nan # Contains NaN func(stupid_times, offset_lim, iterations, tracking_range=tr) with pytest.raises(ValueError): stupid_offset = 120 func(times, stupid_offset, iterations, tracking_range=tr) print("test_stupid_values() passed")
def test_trivial_inputs_2d(): num_bins = (10, 11) """Correlation of all-zeros should be NaN""" arr0 = np.zeros(num_bins) arr1 = np.zeros(num_bins) p = func(arr0, arr1) assert np.isnan(p[0]) assert np.isnan(p[1]).all() """correlation of equal arange""" arr0 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins) arr1 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins) p = func(arr0, arr1, row_major=True) assert np.isclose(p[0], 1) assert np.isclose(p[1], 1).all() p = func(arr0, arr1, row_major=False) assert np.isclose(p[1], 1).all() """correlation of opposite arange""" arr0 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins) arr1 = np.flip(np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins)) p = func(arr0, arr1, row_major=True) assert np.isclose(p[0], -1) assert np.isclose(p[1], -1).all() p = func(arr0, arr1, row_major=False) assert np.isclose(p[1], -1).all() """Handling occasional NaNs""" arr0 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins).astype(float) arr1 = np.arange(num_bins[0] * num_bins[1]).reshape(*num_bins).astype(float) arr0[2, 3] = np.nan arr1[4, 8] = np.nan p = func(arr0, arr1, row_major=True) assert np.isclose(p[0], 1) assert np.isclose(p[1], 1).all() assert np.sum(np.isnan(p[1])) == 0 p = func(arr0, arr1, row_major=False) assert np.isclose(p[1], 1).all() assert np.sum(np.isnan(p[1])) == 0 print("test_trivial_inputs_2d passed")
def test_invalid_inputs(): # >2D dimensional data with pytest.raises(ValueError): # Negative length, 1d kwv = -1 dim = 1 func(kwv, dim) with pytest.raises(ValueError): # Negative length, 2d kwv = -1 dim = 2 func(kwv, dim) with pytest.raises(ValueError): # zero length kwv = 0 dim = 1 func(kwv, dim) with pytest.raises(NotImplementedError): # Invalid # dimensions kwv = 80 dim = 3 func(kwv, dim) with pytest.raises(NotImplementedError): # Invalid # dimensions kwv = 80 dim = 0 func(kwv, dim) with pytest.raises(IndexError): # Invalid # dimensions kwv = (80, 80) dim = 1 func(kwv, dim) print("test_invalid_inputs passed") return True
def test_invalid_inputs(): # Invalid array format with pytest.raises(NotImplementedError): upscale = 2 func(3, upscale) func("abc", upscale) func({2: 2}, upscale) # Invalid integerr upscaling # Force integer upsclaing with masked array inputs with pytest.raises(NotImplementedError): masked_array = np.ma.ones((4, 6)) high_dim_ma = np.ma.ones((4, 6, 7)) fractional_upscale = 2.5 shrinking_upscale = 0 valid_upscale = 2 func(masked_array, fractional_upscale) func(masked_array, shrinking_upscale) func(high_dim_ma, valid_upscale) # Invalid fractional upscaling # force fractional upscaling with floating point upscale with pytest.raises(ValueError): ndarray = np.ones((4, 6)) zero_upscale = float(0) neg_upscale = -0.5 func(ndarray, zero_upscale) func(ndarray, neg_upscale) print("test_invalid_inputs passed") return True
def test_invalid_t_stop(invalid_data): with pytest.raises(errors.ArgumentError): func(times, offset_lim, iterations, t_start, invalid_data)
def test_large_circle(): axes = [np.linspace(-100, 100, 501) for i in range(2)] diameter = 175 mask, distance_map, angular_map = func(axes, diameter)
def test_filter_other_arrays(): positions = np.linspace(-1000, 1000, speeds.size) fspeed, out = func(speeds, speed_cutoff, positions, fmt="remove") assert fspeed.shape == out.shape
def test_mask(): out = func(speeds, speed_cutoff, fmt="mask") assert type(out) == np.ma.MaskedArray assert np.sum(out.mask) == min_out assert np.min(out) == min_out
def test_remove(): out = func(speeds, speed_cutoff, fmt="remove") assert out.size == remaining assert np.min(out) == min_out