Skip to content

Commit

Permalink
Revise spatial tests to pytest style; avoids numercial issues due to …
Browse files Browse the repository at this point in the history
…precise float comparions
  • Loading branch information
heplesser committed Apr 10, 2023
1 parent b3ba799 commit 535dce5
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 185 deletions.
203 changes: 98 additions & 105 deletions testsuite/pytests/test_spatial/test_SynapseCollection_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,159 +23,152 @@
Tests distance between sources and targets of SynapseCollection
"""

import unittest
import pytest
import math
import nest


class SynapseCollectionDistance(unittest.TestCase):
@pytest.fixture(autouse=True)
def _reset_kernel():
nest.ResetKernel()

def setUp(self):
nest.ResetKernel()

def calculate_distance(self, conns, s_nodes, t_nodes):
"""Calculate a reference distance between source and target nodes"""
def _calculate_distance(conns, s_nodes, t_nodes):
"""Calculate a reference distance between source and target nodes"""

s_pos = nest.GetPosition(s_nodes)
t_pos = nest.GetPosition(t_nodes)
s_pos = nest.GetPosition(s_nodes)
t_pos = nest.GetPosition(t_nodes)

src = conns.source
trgt = conns.target
src = conns.source
trgt = conns.target

in_3d = len(s_pos[0]) == 3
in_3d = len(s_pos[0]) == 3

ref_distance = []
for s, t in zip(src, trgt):
x_ref = t_pos[t_nodes.index(t)][0] - s_pos[s_nodes.index(s)][0]
y_ref = t_pos[t_nodes.index(t)][1] - s_pos[s_nodes.index(s)][1]
z_ref = 0.0
if in_3d:
z_ref = t_pos[t_nodes.index(t)][2] - s_pos[s_nodes.index(s)][2]
ref_distance = []
for s, t in zip(src, trgt):
x_ref = t_pos[t_nodes.index(t)][0] - s_pos[s_nodes.index(s)][0]
y_ref = t_pos[t_nodes.index(t)][1] - s_pos[s_nodes.index(s)][1]
z_ref = 0.0
if in_3d:
z_ref = t_pos[t_nodes.index(t)][2] - s_pos[s_nodes.index(s)][2]

ref_dist = math.sqrt(x_ref * x_ref + y_ref * y_ref + z_ref * z_ref)
ref_distance.append(ref_dist)
ref_dist = math.sqrt(x_ref * x_ref + y_ref * y_ref + z_ref * z_ref)
ref_distance.append(ref_dist)

return tuple(ref_distance)
return tuple(ref_distance)

def test_SynapseCollection_distance_simple(self):
"""Test distance on SynapseCollection where source and target are equal"""

s_nodes = nest.Create('iaf_psc_alpha', positions=nest.spatial.grid(shape=[3, 3]))
def test_SynapseCollection_distance_simple():
"""Test distance on SynapseCollection where source and target are equal"""

nest.Connect(s_nodes, s_nodes, {'rule': 'one_to_one'})
conns = nest.GetConnections()
dist = conns.distance
s_nodes = nest.Create('iaf_psc_alpha', positions=nest.spatial.grid(shape=[3, 3]))

self.assertTrue(all([dd == 0. for dd in dist]))
nest.Connect(s_nodes, s_nodes, {'rule': 'one_to_one'})
conns = nest.GetConnections()
dist = conns.distance

def test_SynapseCollection_distance(self):
"""Test SynapseCollection distance function for grid positions"""
assert all(d == 0 for d in dist)

s_nodes = nest.Create('iaf_psc_alpha', positions=nest.spatial.grid(shape=[3, 1]))
t_nodes = nest.Create('iaf_psc_alpha', positions=nest.spatial.grid(shape=[1, 3]))

nest.Connect(s_nodes, t_nodes)
conns = nest.GetConnections()
dist = conns.distance
def test_SynapseCollection_distance():
"""Test SynapseCollection distance function for grid positions"""

ref_distance = self.calculate_distance(conns, s_nodes, t_nodes)
s_nodes = nest.Create('iaf_psc_alpha', positions=nest.spatial.grid(shape=[3, 1]))
t_nodes = nest.Create('iaf_psc_alpha', positions=nest.spatial.grid(shape=[1, 3]))

self.assertEqual(ref_distance, dist)
nest.Connect(s_nodes, t_nodes)
conns = nest.GetConnections()
dist = conns.distance

def test_SynapseCollection_distance_free(self):
"""Test SynapseCollection distance function for positions placed freely in space"""
ref_distance = _calculate_distance(conns, s_nodes, t_nodes)

positions = nest.spatial.free(nest.random.uniform(), num_dimensions=2)
s_nodes = nest.Create('iaf_psc_alpha', n=5, positions=positions)
t_nodes = nest.Create('iaf_psc_alpha', n=7, positions=positions)
assert dist == pytest.approx(ref_distance)

nest.Connect(s_nodes, t_nodes, {'rule': 'pairwise_bernoulli', 'p': 0.7})
conns = nest.GetConnections()
dist = conns.distance

ref_distance = self.calculate_distance(conns, s_nodes, t_nodes)
def test_SynapseCollection_distance_free():
"""Test SynapseCollection distance function for positions placed freely in space"""

self.assertEqual(ref_distance, dist)
positions = nest.spatial.free(nest.random.uniform(), num_dimensions=2)
s_nodes = nest.Create('iaf_psc_alpha', n=5, positions=positions)
t_nodes = nest.Create('iaf_psc_alpha', n=7, positions=positions)

def test_SynapseCollection_distance_3D(self):
"""Test SynapseCollection distance function for spatial nodes in 3D"""
nest.Connect(s_nodes, t_nodes, {'rule': 'pairwise_bernoulli', 'p': 0.7})
conns = nest.GetConnections()
dist = conns.distance

positions = nest.spatial.free(nest.random.uniform(), num_dimensions=3)
s_nodes = nest.Create('iaf_psc_alpha', n=8, positions=positions)
t_nodes = nest.Create('iaf_psc_alpha', n=11, positions=positions)
ref_distance = _calculate_distance(conns, s_nodes, t_nodes)

nest.Connect(s_nodes, t_nodes)
conns = nest.GetConnections()
dist = conns.distance
assert dist == pytest.approx(ref_distance)

ref_distance = self.calculate_distance(conns, s_nodes, t_nodes)

self.assertEqual(ref_distance, dist)
def test_SynapseCollection_distance_3D():
"""Test SynapseCollection distance function for spatial nodes in 3D"""

def test_SynapseCollection_distance_non_spatial(self):
"""Test SynapseCollection distance function on non-spatial nodes"""
positions = nest.spatial.free(nest.random.uniform(), num_dimensions=3)
s_nodes = nest.Create('iaf_psc_alpha', n=8, positions=positions)
t_nodes = nest.Create('iaf_psc_alpha', n=11, positions=positions)

s_nodes = nest.Create('iaf_psc_alpha', 3)
t_nodes = nest.Create('iaf_psc_alpha', 2)
nest.Connect(s_nodes, t_nodes)
conns = nest.GetConnections()
dist = conns.distance

nest.Connect(s_nodes, t_nodes)
conns = nest.GetConnections()
dist = conns.distance
ref_distance = _calculate_distance(conns, s_nodes, t_nodes)

dist_is_nan = [math.isnan(d) for d in dist]
assert dist == pytest.approx(ref_distance)

self.assertTrue(dist_is_nan)

def test_SynapseCollection_distance_mixed(self):
"""Test SynapseCollection distance function on non-spatial and spatial nodes"""
def test_SynapseCollection_distance_non_spatial():
"""Test SynapseCollection distance function on non-spatial nodes"""

num_snodes_nonspatial = 3
num_tnodes_nonspatial = 2
num_conns_nonsparial = num_snodes_nonspatial * num_tnodes_nonspatial
s_nodes_nonspatial = nest.Create('iaf_psc_alpha', num_snodes_nonspatial)
t_nodes_nonspatial = nest.Create('iaf_psc_alpha', num_tnodes_nonspatial)
s_nodes = nest.Create('iaf_psc_alpha', 3)
t_nodes = nest.Create('iaf_psc_alpha', 2)

positions = nest.spatial.free(nest.random.uniform(), num_dimensions=2)
s_nodes_spatial = nest.Create('iaf_psc_alpha', n=6, positions=positions)
t_nodes_spatial = nest.Create('iaf_psc_alpha', n=7, positions=positions)
nest.Connect(s_nodes, t_nodes)
conns = nest.GetConnections()
dist = conns.distance

nest.Connect(s_nodes_nonspatial, t_nodes_nonspatial)
nest.Connect(s_nodes_spatial, t_nodes_spatial)
conns = nest.GetConnections()
dist = conns.distance
assert all(math.isnan(d) for d in dist)

# Check part that is spatial
ref_distance = self.calculate_distance(conns[num_conns_nonsparial:], s_nodes_spatial, t_nodes_spatial)
self.assertEqual(ref_distance, dist[num_conns_nonsparial:])

# Check part that is non-spatial
dist_is_nan = [math.isnan(d) for d in dist[:num_conns_nonsparial]]
self.assertTrue(dist_is_nan)
def test_SynapseCollection_distance_mixed():
"""Test SynapseCollection distance function on non-spatial and spatial nodes"""

def test_SynapseCollection_distance_spatial_nonspatial_connected(self):
"""Test SynapseCollection distance function on non-spatial and spatial nodes that are connected"""
num_snodes_nonspatial = 3
num_tnodes_nonspatial = 2
num_conns_nonspatial = num_snodes_nonspatial * num_tnodes_nonspatial
s_nodes_nonspatial = nest.Create('iaf_psc_alpha', num_snodes_nonspatial)
t_nodes_nonspatial = nest.Create('iaf_psc_alpha', num_tnodes_nonspatial)

num_snodes = 5
num_tnodes = 11
s_nodes_nonspatial = nest.Create('iaf_psc_alpha', num_snodes)
positions = nest.spatial.free(nest.random.uniform(), num_dimensions=2)
s_nodes_spatial = nest.Create('iaf_psc_alpha', n=6, positions=positions)
t_nodes_spatial = nest.Create('iaf_psc_alpha', n=7, positions=positions)

positions = nest.spatial.free(nest.random.uniform(), num_dimensions=2)
t_nodes_spatial = nest.Create('iaf_psc_alpha', n=num_tnodes, positions=positions)
nest.Connect(s_nodes_nonspatial, t_nodes_nonspatial)
nest.Connect(s_nodes_spatial, t_nodes_spatial)
conns = nest.GetConnections()
dist = conns.distance

nest.Connect(s_nodes_nonspatial, t_nodes_spatial)
conns = nest.GetConnections()
dist = conns.distance
# Check part that is spatial
ref_distance = _calculate_distance(conns[num_conns_nonspatial:], s_nodes_spatial, t_nodes_spatial)
assert dist[num_conns_nonspatial:] == pytest.approx(ref_distance)

# All should be nan
dist_is_nan = [math.isnan(d) for d in dist]
self.assertTrue(dist_is_nan)
# Check part that is non-spatial
assert all(math.isnan(d) for d in dist[:num_conns_nonspatial])


def suite():
suite = unittest.makeSuite(SynapseCollectionDistance, 'test')
return suite
def test_SynapseCollection_distance_spatial_nonspatial_connected():
"""Test SynapseCollection distance function on non-spatial and spatial nodes that are connected"""

num_snodes = 5
num_tnodes = 11
s_nodes_nonspatial = nest.Create('iaf_psc_alpha', num_snodes)

if __name__ == "__main__":
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite())
positions = nest.spatial.free(nest.random.uniform(), num_dimensions=2)
t_nodes_spatial = nest.Create('iaf_psc_alpha', n=num_tnodes, positions=positions)

nest.Connect(s_nodes_nonspatial, t_nodes_spatial)
conns = nest.GetConnections()
dist = conns.distance

# All should be nan
assert all(math.isnan(d) for d in dist)
Loading

0 comments on commit 535dce5

Please sign in to comment.