Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue267 (Replace pyNN.utility.assert_arrays_equal by numpy.testing.assert_array_equal) #456

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion pyNN/utility/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,15 @@ def __call__(self, t):
return t + self.interval


#deprecated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If these functions are no longer used, you can delete them completely rather than just marking as deprecated.

def assert_arrays_equal(a, b):
import numpy
assert isinstance(a, numpy.ndarray), "a is a %s" % type(a)
assert isinstance(b, numpy.ndarray), "b is a %s" % type(b)
assert a.shape == b.shape, "%s != %s" % (a, b)
assert (a.flatten() == b.flatten()).all(), "%s != %s" % (a, b)


#deprecated
def assert_arrays_almost_equal(a, b, threshold):
import numpy
assert isinstance(a, numpy.ndarray), "a is a %s" % type(a)
Expand Down
23 changes: 12 additions & 11 deletions test/system/scenarios/test_connectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import numpy
from nose.tools import assert_equal, assert_almost_equal
from pyNN.random import NumpyRNG, RandomDistribution
from pyNN.utility import assert_arrays_equal, assert_arrays_almost_equal, connection_plot, init_logging
from pyNN.utility import connection_plot, init_logging
from numpy.testing import assert_array_equal, assert_array_almost_equal
from .registry import register

#init_logging(None, debug=True)
Expand All @@ -21,7 +22,7 @@ def all_to_all_static_no_self(sim):
print(weights)
delays = prj.get('delay', format='list', gather=False)
i, j, d = numpy.array(delays).T
assert_arrays_almost_equal(d, 0.2 + 0.3 * abs(i - j), 1e-9)
assert_array_almost_equal(d, 0.2 + 0.3 * abs(i - j), decimal=9) # 1e-9)
assert_equal(d.size, p.size * (p.size - 1))
sim.end()

Expand All @@ -34,9 +35,9 @@ def all_to_all_tsodyksmarkram(sim):
synapse_type = sim.TsodyksMarkramSynapse(weight=lambda d: d, delay=0.5, U=lambda d: 0.02 * d + 0.1)
prj = sim.Projection(p1, p2, sim.AllToAllConnector(), synapse_type)
i, j, w, d, u = numpy.array(prj.get(['weight', 'delay', 'U'], format='list', gather=False)).T
assert_arrays_equal(w, abs(i - j))
assert_arrays_equal(d, 0.5 * numpy.ones(p2.size * p1.size))
assert_arrays_equal(u, 0.02 * abs(i - j) + 0.1)
assert_array_equal(w, abs(i - j))
assert_array_equal(d, 0.5 * numpy.ones(p2.size * p1.size))
assert_array_equal(u, 0.02 * abs(i - j) + 0.1)
weights, delays, U = prj.get(['weight', 'delay', 'U'], format='array', gather=False)
print(weights)
print(delays) # should all be 0.5
Expand Down Expand Up @@ -106,9 +107,9 @@ def fixed_number_pre_with_replacement_heterogeneous_parameters(sim):
from pprint import pprint
pprint(x)
i, j, w, d, u = numpy.array(x).T
assert_arrays_equal(w, abs(i - j))
assert_arrays_equal(d, 0.5 * numpy.ones(p2.size * connector1.n))
assert_arrays_equal(u, 0.02 * abs(i - j) + 0.1)
assert_array_equal(w, abs(i - j))
assert_array_equal(d, 0.5 * numpy.ones(p2.size * connector1.n))
assert_array_equal(u, 0.02 * abs(i - j) + 0.1)
sim.end()


Expand Down Expand Up @@ -179,9 +180,9 @@ def fixed_number_post_with_replacement_heterogeneous_parameters(sim):
from pprint import pprint
pprint(x)
i, j, w, d, u = numpy.array(x).T
assert_arrays_equal(w, abs(i - j))
assert_arrays_equal(d, 0.5 * numpy.ones(p1.size * connector1.n))
assert_arrays_equal(u, 0.02 * abs(i - j) + 0.1)
assert_array_equal(w, abs(i - j))
assert_array_equal(d, 0.5 * numpy.ones(p1.size * connector1.n))
assert_array_equal(u, 0.02 * abs(i - j) + 0.1)
sim.end()


Expand Down
23 changes: 11 additions & 12 deletions test/system/scenarios/test_parameter_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from numpy import nan
from numpy.testing import assert_array_equal, assert_array_almost_equal
from nose.tools import assert_equal
from pyNN.utility import assert_arrays_equal, assert_arrays_almost_equal
from .registry import register


Expand All @@ -16,7 +15,7 @@ def issue241(sim):
spike_train3 = sim.Population(1, sim.SpikeSourcePoisson, {'rate': [5], 'start': [1000], 'duration': 1234})
spike_train4 = sim.Population(1, sim.SpikeSourcePoisson, {'rate': [5], 'start': [1000]})
spike_train5 = sim.Population(2, sim.SpikeSourcePoisson, {'rate': [5, 6], 'start': [1000, 1001]})
assert_arrays_equal(spike_train2.get('duration'), numpy.array([1234, 2345]))
assert_array_equal(spike_train2.get('duration'), numpy.array([1234, 2345]))
assert_equal(spike_train3.get(['rate', 'start', 'duration']), [5, 1000, 1234])
sim.end()

Expand Down Expand Up @@ -58,28 +57,28 @@ def test_set_synaptic_parameters_fully_connected(sim):
actual = numpy.array(prj.get(['weight', 'delay', 'U'], format='list'))
if mpi_rank == 0:
ind = numpy.lexsort((actual[:, 1], actual[:, 0]))
assert_arrays_almost_equal(actual[ind], expected, 1e-16)
assert_array_almost_equal(actual[ind], expected, decimal=16)

positional_weights = numpy.array([[0, 1], [2, 3], [4, 5], [6, 7]], dtype=float)
prj.set(weight=positional_weights)
expected = positional_weights
actual = prj.get('weight', format='array')
if mpi_rank == 0:
assert_arrays_equal(actual, expected)
assert_array_equal(actual, expected)

u_list = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2]
prj.set(U=u_list)
expected = numpy.array([[0.9, 0.8], [0.7, 0.6], [0.5, 0.4], [0.3, 0.2]])
actual = prj.get('U', format='array')
if mpi_rank == 0:
assert_arrays_equal(actual, expected)
assert_array_equal(actual, expected)

f_delay = lambda d: 0.5 + d
prj.set(delay=f_delay)
expected = numpy.array([[0.5, 1.5], [1.5, 0.5], [2.5, 1.5], [3.5, 2.5]])
actual = prj.get('delay', format='array')
if mpi_rank == 0:
assert_arrays_equal(actual, expected)
assert_array_equal(actual, expected)

# final sanity check
expected = numpy.array([
Expand All @@ -95,7 +94,7 @@ def test_set_synaptic_parameters_fully_connected(sim):
actual = numpy.array(prj.get(['weight', 'delay', 'U'], format='list'))
if mpi_rank == 0:
ind = numpy.lexsort((actual[:, 1], actual[:, 0]))
assert_arrays_equal(actual[ind], expected)
assert_array_equal(actual[ind], expected)
test_set_synaptic_parameters_fully_connected.__test__ = False


Expand All @@ -118,7 +117,7 @@ def test_set_synaptic_parameters_partially_connected(sim):
actual = numpy.array(prj.get(['weight', 'delay', 'U'], format='list'))
if mpi_rank == 0:
ind = numpy.lexsort((actual[:, 1], actual[:, 0]))
assert_arrays_almost_equal(actual[ind], expected, 1e-16)
assert_array_almost_equal(actual[ind], expected, decimal=16)

positional_weights = numpy.array([[0, nan], [2, 3], [nan, 5], [6, nan]], dtype=float)
prj.set(weight=positional_weights)
Expand Down Expand Up @@ -176,7 +175,7 @@ def test_set_synaptic_parameters_multiply_connected(sim):
actual = numpy.array(prj.get(['weight', 'delay', 'U'], format='list'))
if mpi_rank == 0:
ind = numpy.lexsort((actual[:, 1], actual[:, 0]))
assert_arrays_almost_equal(actual[ind], expected, 1e-16)
assert_array_almost_equal(actual[ind], expected, decimal=16)

positional_weights = numpy.array([[0, nan], [2, 3], [nan, 5], [6, nan]], dtype=float)
prj.set(weight=positional_weights)
Expand All @@ -191,7 +190,7 @@ def test_set_synaptic_parameters_multiply_connected(sim):
actual = numpy.array(prj.get('weight', format='list'))
if mpi_rank == 0:
ind = numpy.lexsort((actual[:, 1], actual[:, 0]))
assert_arrays_almost_equal(actual[ind], expected, 1e-16)
assert_array_almost_equal(actual[ind], expected, decimal=16)

# postponing implementation of this functionality until after 0.8.0
# u_list = [0.9, 0.8, 0.7, 0.6, 0.5, 0.4]
Expand All @@ -207,7 +206,7 @@ def test_set_synaptic_parameters_multiply_connected(sim):
# actual = numpy.array(prj.get('U', format='list'))
# if mpi_rank == 0:
# ind = numpy.lexsort((actual[:, 1], actual[:, 0]))
# assert_arrays_almost_equal(actual[ind], expected, 1e-16)
# assert_array_almost_equal(actual[ind], expected, 1e-16)

f_delay = lambda d: 0.5 + d
prj.set(delay=f_delay)
Expand All @@ -222,7 +221,7 @@ def test_set_synaptic_parameters_multiply_connected(sim):
actual = numpy.array(prj.get('delay', format='list'))
if mpi_rank == 0:
ind = numpy.lexsort((actual[:, 1], actual[:, 0]))
assert_arrays_almost_equal(actual[ind], expected, 1e-16)
assert_array_almost_equal(actual[ind], expected, decimal=16)

# final sanity check
expected = numpy.array([
Expand Down
5 changes: 3 additions & 2 deletions test/system/scenarios/test_procedural_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

import numpy
import quantities as pq
from pyNN.utility import init_logging, assert_arrays_almost_equal
from pyNN.utility import init_logging
from numpy.testing import assert_array_equal, assert_array_almost_equal
from .registry import register


Expand All @@ -20,7 +21,7 @@ def ticket195(sim):
#prj = sim.Projection(pre, post, sim.FromListConnector([(0, 0, 0.01, 0.1)]))
post.record(['spikes', 'v'])
sim.run(100.0)
assert_arrays_almost_equal(post.get_data().segments[0].spiketrains[0], numpy.array([13.4]) * pq.ms, 0.5)
assert_array_almost_equal(post.get_data().segments[0].spiketrains[0], numpy.array([13.4]) * pq.ms, decimal=1)
sim.end()

if __name__ == '__main__':
Expand Down
25 changes: 13 additions & 12 deletions test/system/scenarios/test_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import quantities as pq
from nose.tools import assert_equal
from neo.io import get_io
from pyNN.utility import assert_arrays_equal, assert_arrays_almost_equal, init_logging
from pyNN.utility import init_logging
from numpy.testing import assert_array_equal, assert_array_almost_equal
from .registry import register


Expand All @@ -31,7 +32,7 @@ def test_reset_recording(sim):
data = p.get_data()
sim.end()
ti = lambda i: data.segments[i].analogsignalarrays[0].times
assert_arrays_equal(ti(0), ti(1))
assert_array_equal(ti(0), ti(1))
idx = lambda i: data.segments[i].analogsignalarrays[0].channel_index
assert idx(0) == [3]
assert idx(1) == [4]
Expand Down Expand Up @@ -79,14 +80,14 @@ def test_record_vm_and_gsyn_from_assembly(sim):
assert_equal(gsyn_p1.shape, (n_points, 4))
assert_equal(gsyn_all.shape, (n_points, 7))

assert_arrays_equal(vm_p1[:, 3], vm_all[:, 8])
assert_array_equal(vm_p1[:, 3], vm_all[:, 8])

assert_arrays_equal(vm_p0.channel_index, numpy.arange(5))
assert_arrays_equal(vm_p1.channel_index, numpy.arange(6))
assert_arrays_equal(vm_all.channel_index, numpy.arange(11))
assert_arrays_equal(gsyn_p0.channel_index, numpy.array([2, 3, 4]))
assert_arrays_equal(gsyn_p1.channel_index, numpy.arange(4))
assert_arrays_equal(gsyn_all.channel_index, numpy.arange(2, 9))
assert_array_equal(vm_p0.channel_index, numpy.arange(5))
assert_array_equal(vm_p1.channel_index, numpy.arange(6))
assert_array_equal(vm_all.channel_index, numpy.arange(11))
assert_array_equal(gsyn_p0.channel_index, numpy.array([2, 3, 4]))
assert_array_equal(gsyn_p1.channel_index, numpy.arange(4))
assert_array_equal(gsyn_all.channel_index, numpy.arange(2, 9))

sim.end()
test_record_vm_and_gsyn_from_assembly.__test__ = False
Expand All @@ -111,8 +112,8 @@ def issue259(sim):
print(spiketrains2[0])
sim.end()

assert_arrays_almost_equal(spiketrains0[0], numpy.array([0.075]) * pq.ms, 1e-17)
assert_arrays_almost_equal(spiketrains1[0], numpy.array([10.025, 12.34]) * pq.ms, 1e-14)
assert_array_almost_equal(spiketrains0[0], numpy.array([0.075]) * pq.ms, decimal=17)
assert_array_almost_equal(spiketrains1[0], numpy.array([10.025, 12.34]) * pq.ms, decimal=14)
assert_equal(spiketrains2[0].size, 0)


Expand Down Expand Up @@ -151,7 +152,7 @@ def test_mix_procedural_and_oo(sim):

data_proc = get_io(fn_proc).read()[0]
data_oo = get_io(fn_oo).read()[0]
assert_arrays_equal(data_proc.segments[0].analogsignalarrays[0],
assert_array_equal(data_proc.segments[0].analogsignalarrays[0],
data_oo.segments[0].analogsignalarrays[0])

os.remove(fn_proc)
Expand Down
12 changes: 6 additions & 6 deletions test/system/scenarios/test_simulation_control.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

from nose.tools import assert_almost_equal, assert_raises
from pyNN.utility import assert_arrays_equal, assert_arrays_almost_equal
from numpy.testing import assert_array_equal, assert_array_almost_equal
from .registry import register


Expand All @@ -24,8 +24,8 @@ def test_reset(sim):

assert len(data.segments) == repeats
for segment in data.segments[1:]:
assert_arrays_almost_equal(segment.analogsignalarrays[0],
data.segments[0].analogsignalarrays[0], 1e-11)
assert_array_almost_equal(segment.analogsignalarrays[0],
data.segments[0].analogsignalarrays[0], decimal=11)
test_reset.__test__ = False


Expand All @@ -51,8 +51,8 @@ def test_reset_with_clear(sim):

for rec in data:
assert len(rec.segments) == 1
assert_arrays_almost_equal(rec.segments[0].analogsignalarrays[0],
data[0].segments[0].analogsignalarrays[0], 1e-11)
assert_array_almost_equal(rec.segments[0].analogsignalarrays[0],
data[0].segments[0].analogsignalarrays[0], decimal=11)
test_reset_with_clear.__test__ = False


Expand All @@ -79,7 +79,7 @@ def test_setup(sim):
assert len(block.segments) == 1
signals = block.segments[0].analogsignalarrays
assert len(signals) == 1
assert_arrays_equal(signals[0], data[0].segments[0].analogsignalarrays[0])
assert_array_equal(signals[0], data[0].segments[0].analogsignalarrays[0])
test_setup.__test__ = False


Expand Down
3 changes: 2 additions & 1 deletion test/system/test_nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from .scenarios.registry import registry
from nose.tools import assert_equal, assert_not_equal
from pyNN.utility import init_logging, assert_arrays_equal
from numpy.testing import assert_array_equal, assert_array_almost_equal
import numpy

try:
Expand Down Expand Up @@ -182,7 +183,7 @@ def test_tsodyks_markram_synapse():
sim.run(100.0)
connections = nest.GetConnections(prj._sources.tolist(), synapse_model=prj.nest_synapse_model)
tau_psc = numpy.array(nest.GetStatus(connections, 'tau_psc'))
assert_arrays_equal(tau_psc, numpy.arange(0.2, 0.7, 0.1))
assert_array_equal(tau_psc, numpy.arange(0.2, 0.7, 0.1))


if __name__ == '__main__':
Expand Down
5 changes: 3 additions & 2 deletions test/unittests/test_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import numpy
import os
from pyNN.utility import assert_arrays_equal
from numpy.testing import assert_array_equal, assert_array_almost_equal

builtin_open = open

Expand Down Expand Up @@ -99,7 +100,7 @@ def test_PickleFile():
#
# nbf = files.NumpyBinaryFile("tmp.npz", "r")
# assert_equal(nbf.get_metadata(), metadata)
# assert_arrays_equal(nbf.read().flatten(), numpy.array(data).flatten())
# assert_array_equal(nbf.read().flatten(), numpy.array(data).flatten())
# nbf.close()
#
# os.remove("tmp.npz")
Expand All @@ -115,7 +116,7 @@ def test_HDF5ArrayFile():

h5f = files.HDF5ArrayFile("tmp.h5", "r")
assert_equal(h5f.get_metadata(), metadata)
assert_arrays_equal(numpy.array(h5f.read()).flatten(),
assert_array_equal(numpy.array(h5f.read()).flatten(),
numpy.array(data).flatten())
h5f.close()

Expand Down
6 changes: 4 additions & 2 deletions test/unittests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
have_nrn = True
from pyNN.neuron.random import NativeRNG

from numpy.testing import assert_array_equal, assert_array_almost_equal

def assert_arrays_almost_equal(a, b, threshold):
#deprecated
def assert_arrays_almost_equal(a, b, threshold):
if not (abs(a - b) < threshold).all():
err_msg = "%s != %s" % (a, b)
err_msg += "\nlargest difference = %g" % abs(a - b).max()
Expand Down Expand Up @@ -129,7 +131,7 @@ def test_permutation(self):
A = range(10)
perm0 = rng0.permutation(A)
perm1 = rng1.permutation(A)
assert_arrays_almost_equal(perm0, perm1, 1e-99)
assert_array_almost_equal(perm0, perm1, decimal=99)


class NativeRNGTests(unittest.TestCase):
Expand Down
1 change: 1 addition & 0 deletions test/unittests/test_recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from datetime import datetime
from collections import defaultdict
from pyNN.utility import assert_arrays_equal
from numpy.testing import assert_array_equal

#def test_rename_existing():

Expand Down
Loading