def pytestcase_cycle_consistency_read_write(tmpdir, dataset_dir):
    """Tests reading and writing DAT files and that the read write read cycle is consistent"""
    filename = os.path.join(dataset_dir,
                            "metavision_core", "event_io", "recording_td.dat")
    record = EventDatReader(filename)
    # height, width = record.get_size()
    # it is a gen1 recording with no height or width
    tmp_filename = str(tmpdir.join("tmp_td.dat"))

    writer = DatWriter(tmp_filename, height=480, width=640)

    event_buffers = []

    for i in range(20):
        events = record.load_n_events(1000)
        event_buffers.append(events)
        writer.write(events)
    writer.close()

    new_event_buffers = []
    new_record = EventDatReader(tmp_filename)
    assert new_record.ev_type == 0
    assert new_record.get_size() == [480, 640]
    assert record.current_time >= new_record.duration_s * 1e6
    assert events['t'][-1] == new_record.duration_s * 1e6
    assert new_record.event_count() == 20 * 1000

    for i, events in enumerate(event_buffers):
        new_events = new_record.load_n_events(1000)
        assert all([np.allclose(events[name], new_events[name]) for name in events.dtype.names])

    assert record.current_event_index() == new_record.current_event_index()
def pytestcase_init(dataset_dir):
    """Tests initialization of all member variables after creation of EventDatReader object from a file"""
    filename = os.path.join(dataset_dir,
                            "metavision_core", "event_io", "recording_td.dat")
    record = EventDatReader(filename)
    # check that the representation strings Works
    print(record)
    assert record.ev_type == 12
    assert record._ev_size == 8
    assert record.get_size() == [480, 640]
    assert EV_TYPES[record.ev_type] == [('t', 'u4'), ('_', 'i4')]

    assert record.event_count() == 667855
    assert record.done is False
    assert record.current_time == 0
    assert record.current_event_index() == 0
    assert record.duration_s == 7.702845
    assert record.load_n_events(1).dtype == np.dtype({'names': ['x', 'y', 'p', 't'],
                                                      'formats': ['<u2', '<u2', '<i2', '<i8'],
                                                      'offsets': [0, 2, 4, 8], 'itemsize': 16})