Skip to content

Commit

Permalink
Added smoke tests and refactored test code
Browse files Browse the repository at this point in the history
  • Loading branch information
raj1701 committed Jun 21, 2023
1 parent 391c9f0 commit 3783e72
Showing 1 changed file with 41 additions and 12 deletions.
53 changes: 41 additions & 12 deletions hnn_core/tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,39 @@
@pytest.mark.parametrize("network_model",
["law_2021", "calcium", "jones_2009"])
def test_network_io(tmpdir, network_model):
# Instantiating network along with drives
if network_model == "jones_2009":
net = jones_2009_model(add_drives_from_params=True)
elif network_model == "law_2021":
net = law_2021_model(add_drives_from_params=True)
elif network_model == "calcium":
net = calcium_model(add_drives_from_params=True)
# add_erp_drives_to_model(net)

# Adding bias
net.add_tonic_bias(cell_type='L2_pyramidal', amplitude=1.0)

# Test __eq__ method
net_copy = net.copy()
assert net_copy == net

# Adding electrode arrays
electrode_pos = (1, 2, 3)
net.add_electrode_array('el1', electrode_pos)
electrode_pos = [(1, 2, 3), (-1, -2, -3)]
net.add_electrode_array('arr1', electrode_pos)

# Writing network
net.write(tmpdir.join('net.hdf5'))

# Testing when overwrite is False and same filename is used
with pytest.raises(FileExistsError,
match="File already exists at path "):
net.write(tmpdir.join('net.hdf5'), overwrite=False)

# Reading network
net_read = read_network(tmpdir.join('net.hdf5'))
assert net == net_read

# Add test to check weights are equal in connections and drives (todo)
# Run simulation and simulation output should be same (dipole) (Done)

# Simulating network
dpls1 = simulate_dipole(net, tstop=2, n_trials=1)
dpls2 = simulate_dipole(net_read, tstop=2, n_trials=1)
Expand All @@ -57,29 +62,50 @@ def test_network_io(tmpdir, network_model):
for dpl_key in dpl1.data.keys():
assert_allclose(dpl1.data[dpl_key],
dpl2.data[dpl_key], rtol=0.000051, atol=0)
# Writing network
# Writing simulated network and reading it
net.write(tmpdir.join('net_sim.hdf5'))
# Reading network
net_sim = read_network(tmpdir.join('net_sim.hdf5'))
assert net == net_sim
# For cell response vsec isec bug

# For cell response vsec isec bug (Not checked by __eq__)
assert net.cell_response.vsec == net_sim.cell_response.vsec

# Smoke test
net_sim.plot_cells(show=False)

# Checking Saving unsimulated network
net.write(tmpdir.join('net_unsim.hdf5'), save_unsimulated=True)
net_unsim = read_network(tmpdir.join('net_unsim.hdf5'))
net_unsim_read = read_network(tmpdir.join('net_unsim.hdf5'))
net_unsim = net.copy()
net_unsim.cell_response = None
assert net_unsim == net_unsim
# Run simulation on the read unsimulated network and check it against the
assert net_unsim_read == net_unsim

# Running simulation on the read unsimulated network and check it against
# previous simulation
dpls3 = simulate_dipole(net_unsim_read, tstop=2, n_trials=1)
for dpl1, dpl3 in zip(dpls1, dpls3):
assert_allclose(dpl1.times, dpl3.times, rtol=0.00051, atol=0)
for dpl_key in dpl1.data.keys():
assert_allclose(dpl1.data[dpl_key],
dpl3.data[dpl_key], rtol=0.000051, atol=0)

# Smoke test
net_unsim_read.plot_cells(show=False)

# Checking reading of raw network
net_raw = read_network(tmpdir.join('net_sim.hdf5'),
read_raw=True)
assert net_raw == net_unsim
# Add smoke tests for plotting
# Use pytest.parametrize to run through different networks
# Checking simulation correctness of read raw network
dpls4 = simulate_dipole(net_raw, tstop=2, n_trials=1)
for dpl1, dpl4 in zip(dpls1, dpls4):
assert_allclose(dpl1.times, dpl4.times, rtol=0.00051, atol=0)
for dpl_key in dpl1.data.keys():
assert_allclose(dpl1.data[dpl_key],
dpl4.data[dpl_key], rtol=0.000051, atol=0)

# Smoke test
net_raw.plot_cells(show=False)

# Checking object type field not exists error
dummy_data = dict()
Expand All @@ -88,13 +114,16 @@ def test_network_io(tmpdir, network_model):
with pytest.raises(NameError,
match="The given file is not compatible."):
read_network(tmpdir.join('not_net.hdf5'))

# Checking wrong object type error
dummy_data['object_type'] = "net"
write_hdf5(tmpdir.join('not_net.hdf5'), dummy_data, overwrite=True)
with pytest.raises(ValueError,
match="The object should be of type Network."):
read_network(tmpdir.join('not_net.hdf5'))

# Add test to check weights are equal in connections and drives (todo)


def test_network_models():
""""Test instantiations of the network object"""
Expand Down

0 comments on commit 3783e72

Please sign in to comment.