Skip to content

Commit

Permalink
Re-add test diles
Browse files Browse the repository at this point in the history
  • Loading branch information
alejoe91 committed Nov 21, 2024
1 parent 7d2076b commit 9a9d846
Show file tree
Hide file tree
Showing 5 changed files with 293 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_bordercells.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import pytest
import numpy as np


def test_border_score():
from spatial_maps.bordercells import border_score
from spatial_maps.tools import make_test_border_map
from spatial_maps.fields import separate_fields_by_laplace

box_size = [1.0, 1.0]
rate = 1.0
bin_size = [0.01, 0.01]

rate_map, pos_true, xbins, ybins = make_test_border_map(
sigma=0.05, amplitude=rate, offset=0, box_size=box_size, bin_size=bin_size
)

labels = separate_fields_by_laplace(rate_map, threshold=0)
bs = border_score(rate_map, labels)
assert round(bs, 2) == 0.32
90 changes: 90 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import numpy as np
import pytest
import quantities as pq
from spatial_maps.tools import make_test_grid_rate_map, make_test_border_map
from spatial_maps.fields import (
separate_fields_by_laplace,
find_peaks,
calculate_field_centers,
distance_to_edge_function,
map_pass_to_unit_circle,
)


def test_find_peaks():
box_size = np.array([1.0, 1.0])
rate = 5.0
bin_size = [0.01, 0.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_separate_fields_by_laplace():
box_size = [1.0, 1.0]
rate = 1.0
bin_size = [0.01, 0.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_distance_to_edge_function():
n_bins = 10
box_size = [1, 1]
bin_size = box_size[0] / n_bins

field = np.zeros((n_bins, n_bins))
field[2:8, 2:8] = 1
d = distance_to_edge_function(0.5, 0.5, field, box_size, interpolation="linear")

# assert edges 3/10 of the box size from center
for a in [i * np.pi / 2 for i in range(4)]:
assert np.isclose(0.3, d(a))

# assert area within 5 % of expected result
angles = np.linspace(0, 2 * np.pi, 10000)
dist = d(angles)
x = dist * np.cos(angles)
y = dist * np.sin(angles)

dx = np.gradient(x)
dy = np.gradient(y)

# Greens theorem
area = 0.5 * np.sum(x * dy - y * dx)
exact_area = np.sum(field) / np.size(field) * box_size[0] ** 2

assert np.abs(area - exact_area) / exact_area < 0.05


def test_map_pass_to_unit_circle():

dist_func = lambda theta: 1
x_c, y_c = (0.5, 0.5)
theta = np.linspace(np.pi, 2 * np.pi, 100)
t = theta
x = x_c + np.cos(theta)
y = y_c + np.sin(theta)

r, angle, _, _ = map_pass_to_unit_circle(x, y, t, x_c, y_c, dist_func=dist_func)

assert np.all(np.isclose(angle, theta % (2 * np.pi)))
assert np.all(np.isclose(1, r))
103 changes: 103 additions & 0 deletions tests/test_gridcells.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import numpy as np
import pytest
from spatial_maps import SpatialMap
import quantities as pq
from spatial_maps.tools import make_test_grid_rate_map, make_test_spike_map, autocorrelation
from spatial_maps.fields import find_peaks
from spatial_maps.gridcells import gridness, spacing_and_orientation, separate_fields_by_distance


def test_gridness():
box_size = np.array([1.0, 1.0])
rate = 5.0
bin_size = [0.01, 0.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.0, 1.0])
rate = 5.0
bin_size = [0.01, 0.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_spacing_and_orientation_from_autocorr():
box_size = np.array([1.0, 1.0])
rate = 5.0
bin_size = [0.01, 0.01]
spacing_true = 0.3
orientation_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,
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


def test_separate_fields_by_distance():
box_size = [1.0, 1.0]
rate = 1.0
bin_size = [0.01, 0.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_distance_2():
Y, X = np.mgrid[0:100, 0:100]
fx, fy = np.mgrid[5:95:20, 5:95:20]
fields = np.array([fx.ravel(), fy.ravel()]).T

rate_map = np.zeros((100, 100))

for field in fields:
dY = Y - field[0]
dX = X - field[1]
rate_map += np.exp(-1 / 2 * (dY**2 + dX**2) / 10) # Gaussian-ish

# should be removed by the algorithm because they are lower and close to existing fields
noise_fields = [[60, 52], [45, 35]]

for field in noise_fields:
dY = Y - field[0]
dX = X - field[1]
rate_map += 0.5 * np.exp(-1 / 2 * (dY**2 + dX**2) / 10) # Gaussian-ish

found_fields, radius = separate_fields_by_distance(rate_map)

for field in found_fields:
assert np.isclose(field, fields).prod(axis=1).any()
67 changes: 67 additions & 0 deletions tests/test_maps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import numpy as np
import pytest
from spatial_maps.maps import SpatialMap
import quantities as pq
from spatial_maps.tools import make_test_grid_rate_map, make_test_spike_map


def test_rate_map():
box_size = [1.0, 1.0]
rate = 5.0
bin_size = [0.01, 0.01]
n_step = 10**4
step_size = 0.1
sigma = 0.1
spacing = 0.3
smoothing = 0.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(smoothing=smoothing, box_size=box_size, bin_size=bin_size)
rate_map = smap.rate_map(x, y, t, spikes)

rate_map[np.isnan(rate_map)] = 0
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) < 0.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_spatial_rate_map_diag():
N = 10
bin_size = 1
box_size = 1.0
x = np.linspace(0.0, box_size, N)
y = np.linspace(0.0, box_size, N)
t = np.linspace(0.1, 10.1, N)
spike_times = np.arange(0.1, 10.1, 0.5)
sm = SpatialMap(box_size=box_size, bin_size=bin_size)
ratemap = sm.rate_map(x, y, t, spike_times)
print(ratemap)
assert all(np.diff(np.diag(ratemap)) < 1e-10)
assert ratemap.shape == (int(box_size / bin_size), int(box_size / bin_size))


def test_occupancy_map_diag():
N = 3
bin_size = 0.5
box_size = 1.5
x = np.linspace(0.0, box_size, N)
y = np.linspace(0.0, box_size, N)
t = np.linspace(0, 10.0, N)

sm = SpatialMap(box_size=box_size, bin_size=bin_size)
occmap_expected = np.array([[5, 0, 0], [0, 5, 0], [0, 0, 5]])
occmap = sm.occupancy_map(x, y, t)
occmap[np.isnan(occmap)] = 0
assert np.array_equal(occmap, occmap_expected)
13 changes: 13 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import numpy as np
import pytest


def test_calc_population_vector_correlation():
from spatial_maps.stats import population_vector_correlation as pvcorr

rmaps1 = np.array([[[1, 0.1], [0.1, 4]], [[6, 0.1], [0.1, 2]], [[2, 0.1], [0.1, 3]]])
rmaps2 = np.array([[[2, 0.2], [0.2, 8]], [[12, 0.2], [0.2, 4]], [[4, 0.2], [0.2, 6]]])
rmaps2 += 10e-5
pv = pvcorr(rmaps1, rmaps2)
err = pv - 1
assert err < 10e-5

0 comments on commit 9a9d846

Please sign in to comment.