示例#1
0
def test_write_sonata_append(st_cls, write_fnc):
    # Check that we can append spikes data to an existing sonata file. Currently it only works if /spikes/<pop_name>
    # does not already exists, since append to an h5 can be prohibitive. iI the future may want ot implement.
    tmpfile = tempfile.NamedTemporaryFile(suffix='.h5')
    with h5py.File(tmpfile.name, 'w') as h5:
        h5.create_group('/spikes/V1')

    st = create_st_buffer(st_cls)
    st.add_spikes(population='V2',
                  node_ids=0,
                  timestamps=np.linspace(0, 1.0, 100))

    write_fnc(tmpfile.name, st, mode='a', sort_order=sort_order.by_id)
    with h5py.File(tmpfile.name) as h5:
        assert (check_magic(h5))
        assert (get_version(h5) is not None)
        assert ('/spikes/V1' in h5)
        assert ('/spikes/V2' in h5)
        assert (len(h5['/spikes/V2/node_ids']) == 100)
        assert (len(h5['/spikes/V2/timestamps']) == 100)

    # Throw error if same pop_name alrady exists
    tmpfile2 = tempfile.NamedTemporaryFile(suffix='.h5')
    with h5py.File(tmpfile2.name, 'w') as h5:
        h5.create_group('/spikes/V2')

    with pytest.raises(ValueError):
        write_fnc(tmpfile2.name, st, mode='a', sort_order=sort_order.by_id)
示例#2
0
def test_write_sonata(st_cls, write_fnc):
    st = create_st_buffer(st_cls)
    st.add_spikes(population='V1',
                  node_ids=0,
                  timestamps=np.linspace(0, 1.0, 100))
    st.add_spikes(population='V1',
                  node_ids=2,
                  timestamps=np.linspace(2.0, 1.0, 10))
    st.add_spike(population='V1', node_id=1, timestamp=3.0)
    st.add_spikes(population='V2',
                  node_ids=[3, 3, 3],
                  timestamps=[0.25, 0.5, 0.75])

    tmpfile = tempfile.NamedTemporaryFile(suffix='.h5')
    write_fnc(tmpfile.name, st)

    with h5py.File(tmpfile.name) as h5:
        assert (check_magic(h5))
        assert (get_version(h5) is not None)
        assert ('/spikes/V1' in h5)
        node_ids = h5['/spikes/V1/node_ids'][()]
        assert (len(node_ids) == 111)
        assert (set(np.unique(node_ids)) == {0, 1, 2})
        assert (len(h5['/spikes/V1/timestamps'][()]) == 111)

        assert ('/spikes/V2' in h5)
        assert (np.all(h5['/spikes/V2/node_ids'][()] == [3, 3, 3]))
        # WARNING: Not all adaptor guarentee order of spikes
        assert (np.allclose(np.sort(h5['/spikes/V2/timestamps'][()]),
                            [0.25, 0.50, 0.75]))
示例#3
0
def test_write_sonata(st_cls, write_fnc):
    st = create_st_buffer_mpi(st_cls)
    st.add_spikes(population='V1',
                  node_ids=MPI_rank,
                  timestamps=[MPI_rank] * 5)
    st.add_spike(population='V2', node_id=MPI_size, timestamp=float(MPI_rank))
    st.add_spikes(population='R{}'.format(MPI_rank),
                  node_ids=0,
                  timestamps=[0.1, 0.2, 0.3, 0.4])

    tmp_h5 = tmpfile()
    write_fnc(tmp_h5, st)

    if MPI_rank == 0:
        # Warnings: some systems creates lock even for reading an hdf5 file
        with h5py.File(tmp_h5, 'r') as h5:
            assert (check_magic(h5))
            assert (get_version(h5) is not None)
            assert (set(h5['/spikes'].keys()) >=
                    {'R{}'.format(r)
                     for r in range(MPI_size)} | {'V1', 'V2'})
            assert (set(h5['/spikes/V1']['node_ids'][()]) == {
                i
                for i in range(MPI_size)
            })
            assert (set(h5['/spikes/V2']['timestamps'][()]) == {
                float(i)
                for i in range(MPI_size)
            })
            for r in range(MPI_size):
                grp = h5['/spikes/R{}'.format(r)]
                assert (np.all(grp['node_ids'][()] == [0, 0, 0, 0]))
                assert (np.allclose(grp['timestamps'][()],
                                    [0.1, 0.2, 0.3, 0.4]))
示例#4
0
def test_write_sonata_empty(st_cls, write_fnc):
    st = create_st_buffer_mpi(st_cls)

    tmp_h5 = tmpfile()
    write_fnc(tmp_h5, st)

    if MPI_rank == 0:
        with h5py.File(tmp_h5, 'r') as h5:
            assert(check_magic(h5))
            assert(get_version(h5) is not None)
            assert('/spikes' in h5)
示例#5
0
def test_write_sonata_empty(st_cls, write_fnc):
    # Important use case, a valid simulation may run for a long time but not produce any spikes, make sure it doesn't
    # fail trying to write any empty set of spike-trains to h5
    st = create_st_buffer(st_cls)
    tmpfile = tempfile.NamedTemporaryFile(suffix='.h5')
    write_fnc(tmpfile.name, st)

    with h5py.File(tmpfile.name) as h5:
        assert (check_magic(h5))
        assert (get_version(h5) is not None)
        assert ('/spikes' in h5)
        assert (len(h5['/spikes']) == 0)
示例#6
0
def test_write_sonata_byid(st_cls, write_fnc):
    # Check we can sort by node_ids
    st = create_st_buffer(st_cls)
    st.add_spikes(population='V1', node_ids=[2, 4, 2, 1, 3, 3, 6, 0], timestamps=[0.1]*8)

    tmpfile = tempfile.NamedTemporaryFile(suffix='.h5')
    write_fnc(tmpfile.name, st, sort_order=sort_order.by_id)
    with h5py.File(tmpfile.name, 'r') as h5:
        assert(check_magic(h5))
        assert(get_version(h5) is not None)
        assert(h5['/spikes/V1'].attrs['sorting'] == 'by_id')
        assert(np.all(h5['/spikes/V1/node_ids'][()] == [0, 1, 2, 2, 3, 3, 4, 6]))
        assert(np.all(h5['/spikes/V1/timestamps'][()] == [0.1]*8))
示例#7
0
def test_write_sonata_bytime(st_cls, write_fnc):
    # Check we can sort by timestamps
    st = create_st_buffer(st_cls)
    st.add_spikes(population='V1', node_ids=0, timestamps=[0.5, 0.3, 0.1, 0.2, 0.4])

    tmpfile = tempfile.NamedTemporaryFile(suffix='.h5')
    write_fnc(tmpfile.name, st, sort_order=sort_order.by_time)
    with h5py.File(tmpfile.name, 'r') as h5:
        assert(check_magic(h5))
        assert(get_version(h5) is not None)
        assert(h5['/spikes/V1'].attrs['sorting'] == 'by_time')
        assert(np.all(h5['/spikes/V1/node_ids'][()] == [0, 0, 0, 0, 0]))
        assert(np.all(h5['/spikes/V1/timestamps'][()] == [0.1, 0.2, 0.3, 0.4, 0.5]))
示例#8
0
def test_write_sonata_byid(st_cls, write_fnc):
    st = create_st_buffer_mpi(st_cls)
    st.add_spikes(population='V1', node_ids=[MPI_size + MPI_rank, MPI_rank], timestamps=[0.5, 1.0])

    tmp_h5 = tmpfile()
    write_fnc(tmp_h5, st, sort_order=sort_order.by_id)

    if MPI_rank == 0:
        with h5py.File(tmp_h5, 'r') as h5:
            assert(check_magic(h5))
            assert(get_version(h5) is not None)
            assert(np.all(h5['/spikes/V1']['node_ids'][()] == list(range(MPI_size*2))))
            assert(len(h5['/spikes/V1']['timestamps'][()]) == MPI_size * 2)
示例#9
0
def test_write_sonata_bytime(st_cls, write_fnc):
    st = create_st_buffer_mpi(st_cls)
    st.add_spikes(population='V1', node_ids=[MPI_rank, MPI_rank],
                  timestamps=np.array([MPI_rank/10.0, (MPI_size + MPI_rank)/10.0], dtype=float))

    tmp_h5 = tmpfile()
    write_fnc(tmp_h5, st, sort_order=sort_order.by_time)

    if MPI_rank == 0:
        with h5py.File(tmp_h5, 'r') as h5:
            assert(check_magic(h5))
            assert(get_version(h5) is not None)
            assert(len(h5['/spikes/V1']['node_ids'][()]) == MPI_size*2)
            assert(np.all(np.diff(h5['/spikes/V1']['timestamps'][()]) > 0))
示例#10
0
def _check_edges(h5, n_edges):
    assert(check_magic(h5))
    assert(get_version(h5))

    assert(len(h5['/edges/a_to_b/source_node_id']) == n_edges)
    assert(h5['/edges/a_to_b/source_node_id'].attrs['node_population'] == 'a')
    assert(len(h5['/edges/a_to_b/target_node_id']) == n_edges)
    assert(h5['/edges/a_to_b/target_node_id'].attrs['node_population'] == 'b')
    assert(len(h5['/edges/a_to_b/edge_type_id']) == n_edges)
    assert(len(h5['/edges/a_to_b/edge_group_id']) == n_edges)
    assert(len(h5['/edges/a_to_b/edge_group_index']) == n_edges)
    for i in range(n_edges):
        grp_id = h5['/edges/a_to_b/edge_group_id'][i]
        grp_indx = h5['/edges/a_to_b/edge_group_index'][i]
        assert (h5['/edges/a_to_b/source_node_id'][i] == h5['/edges/a_to_b'][str(grp_id)]['src_id'][grp_indx])
        assert (h5['/edges/a_to_b/target_node_id'][i] == h5['/edges/a_to_b'][str(grp_id)]['trg_id'][grp_indx])
        assert (h5['/edges/a_to_b/edge_type_id'][i] == h5['/edges/a_to_b'][str(grp_id)]['et_id'][grp_indx])