def cut_and_check_infos(input_raw, start, end, expected_output_info=None):
    """"Runs metavision_raw_cutter on input file and checks the output

    Args:
        input_raw (str): path of the input file
        start, end : cut range [s]
        expected_output_info : expected output on running metavision_raw_info on the output file
                               If none is provided, we assume that we have to get the same info as
                               the input file (which is the case when the range covers all the input file)
    """

    # Before launching the app, check the dataset file exists
    assert os.path.exists(input_raw)

    # Create temporary directory, where we'll put the output
    tmp_dir = os_tools.TemporaryDirectoryHandler()
    output_file_name = "raw_cut_{}_{}.raw".format(start, end)
    output_file_path = os.path.join(tmp_dir.temporary_directory(), output_file_name)

    cmd = "./metavision_raw_cutter -i \"{}\" --start {} --end {} -o {}".format(input_raw, start, end,
                                                                               output_file_path)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check output file has been written
    assert os.path.exists(output_file_path)

    # Now, with the app metavision_raw_info we check the information
    cmd = "./metavision_raw_info -i {}".format(output_file_path)
    info_cut_file, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0

    # Now check output
    if not expected_output_info:
        # Then run metavision_raw_info on input file, since we expect the same output
        cmd = "./metavision_raw_info -i \"{}\"".format(input_raw)
        info_input_file, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

        # Check no error occurred
        assert error_code == 0

        # Need to format the output
        expected_output_info = info_input_file.replace(input_raw, "{}").replace(os.path.basename(input_raw), "{}")

    expected_output_info_formatted = expected_output_info.format(output_file_name, os.path.realpath(output_file_path))

    output_strip = "\n".join([line.strip() for line in info_cut_file.splitlines()])
    expected_output_strip = "\n".join([line.strip() for line in expected_output_info_formatted.splitlines()])
    assert output_strip.find(expected_output_strip) >= 0
def cut_and_check_infos(dataset_dir, start, end, expected_output_info):
    """"Runs metavision_raw_cutter on sample_plugin_recording.raw and checks the output

    Args:
        dataset_dir (str): path of the directory with the datasets
        start, end : cut range [s]
        expected_output_info : expected output on running metavision_raw_info on the output file
                               If none is provided, we assume that we have to get the same info as
                               the input file (which is the case when the range covers all the input file)
    """

    filename = "sample_plugin_recording.raw"
    filename_full = os.path.join(dataset_dir, filename)

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    # Create temporary directory, where we'll put the output
    tmp_dir = os_tools.TemporaryDirectoryHandler()
    output_file_name = "sample_plugin_raw_cut_{}_{}.raw".format(start, end)
    output_file_path = os.path.join(tmp_dir.temporary_directory(), output_file_name)

    cmd = "./metavision_raw_cutter -i \"{}\" --start {} --end {} -o {}".format(filename_full, start, end,
                                                                               output_file_path)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check output file has been written
    assert os.path.exists(output_file_path)

    # Now, with the app metavision_raw_info we check the information
    cmd = "./metavision_raw_info -i {}".format(output_file_path)
    info_cut_file, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0

    # Now check output
    expected_output_info_formatted = expected_output_info.format(output_file_name, os.path.realpath(output_file_path))

    output_strip = "\n".join([line.strip() for line in info_cut_file.splitlines()])
    expected_output_strip = "\n".join([line.strip() for line in expected_output_info_formatted.splitlines()])

    print(output_strip)
    print(expected_output_strip)
    assert output_strip.find(expected_output_strip) >= 0
def generate_video(filename_full, fps, expected_video_infos):

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    tmp_dir = os_tools.TemporaryDirectoryHandler()
    output_video = os.path.join(tmp_dir.temporary_directory(), "out.avi")

    cmd = "./metavision_raw_to_video -i \"{}\" --fps {} -o {} --fourcc MJPG".format(
        filename_full, fps, output_video)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(
        cmd, output)

    # Before opening the video, verify it has been written :
    assert os.path.exists(output_video)

    # Now check basic information on the video
    cap = cv2.VideoCapture(output_video)

    # Check size :
    assert int(cap.get(
        cv2.CAP_PROP_FRAME_WIDTH)) == expected_video_infos['width']
    assert int(cap.get(
        cv2.CAP_PROP_FRAME_HEIGHT)) == expected_video_infos['height']

    # Check frame count and frame rate
    assert int(cap.get(
        cv2.CAP_PROP_FRAME_COUNT)) == expected_video_infos['frame-count']
    assert int(cap.get(cv2.CAP_PROP_FPS)) == fps

    # Check codec
    assert decode_fourcc(cap.get(cv2.CAP_PROP_FOURCC)) == "MJPG"
def pytestcase_test_metavision_platform_info_with_sample_plugin_system():
    '''
    Checks output of metavision_platform_info application when using sample plugin
    '''

    cmd = "./metavision_platform_info --system"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0

    # Check expected output
    expected_output_contains = """# FOUND SampleIntegratorName GEN 1.0 600x500 #

Connection                    USB
Integrator                    SampleIntegratorName
Raw Formats                   SAMPLE-FORMAT-1.0
Sensor Info                   1.0
Serial                        000000
System Version                0.0.1
SystemID                      42
"""
    output_strip = "\n".join([line.strip() for line in output.splitlines()])
    expected_output_strip = "\n".join(
        [line.strip() for line in expected_output_contains.splitlines()])
    assert output_strip.find(expected_output_strip) >= 0
def pytestcase_test_metavision_raw_cutter_show_help():
    '''
    Checks output of metavision_raw_cutter when displaying help message
    '''

    cmd = "./metavision_raw_cutter --help"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check that the options shows in the output
    assert "Options:" in output, "******\nMissing options display in output :{}\n******".format(output)
def pytestcase_test_metavision_raw_cutter_missing_input_args():
    '''
    Checks that metavision_raw_to_dat returns an error when not passing required input args
    '''

    cmd = "./metavision_raw_cutter"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited with error
    assert error_code != 0

    # And now check that the error came from the fact that the input file arg is missing
    assert re.search("Parsing error: the option (.+) is required but missing", output)
def pytestcase_test_metavision_raw_to_csv_missing_input_args():
    """
    Checks that metavision_raw_to_csv returns an error when not passing required input args
    """

    cmd = "./metavision_raw_to_csv"
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Assert app returned error
    assert error_code != 0

    # And now check that the error came from the fact that the input file arg is missing
    assert re.search("Parsing error: the option (.+) is required but missing", output)
def run_raw_to_csv_and_check_result(filename_full, number_events_expected, first_10_events_expected,
                                    middle_10_events_expected, last_10_events_expected):

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    # Since the application metavision_raw_to_csv writes the output file in the same directory
    # the app is launched from, create a tmp directory from where we can run the app
    tmp_dir = os_tools.TemporaryDirectoryHandler()

    # The pytest is run from the build/bin dir (cf CMakeLists.txt), but since we'll run the command
    # from the temporary directory created above, we need to get the full path to the application
    application_full_path = os.path.join(os.getcwd(), "metavision_raw_to_csv")

    cmd = "\"{}\" -i \"{}\"".format(application_full_path, filename_full)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd, working_directory=tmp_dir.temporary_directory())

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check CSV file has been written
    expected_generated_file = os.path.join(tmp_dir.temporary_directory(), "cd.csv")
    assert os.path.exists(expected_generated_file)

    # Now open the file and check for its contents
    with open(expected_generated_file, 'r') as f:
        lines = f.readlines()

        # Check number of events
        nr_events = len(lines)
        assert nr_events == number_events_expected

        # Check first 10 events :
        for idx in range(0, 10):
            ev = get_ev_from_line(lines[idx])
            assert ev == first_10_events_expected[idx], "Error on event nr {}".format(idx)

        # Check the 10 events in the middle:
        idx_ev = nr_events // 2 - 5
        for idx in range(0, 10):
            ev = get_ev_from_line(lines[idx_ev])
            assert ev == middle_10_events_expected[idx], "Error on event nr {}".format(idx_ev)
            idx_ev += 1

        # Check last 10 events :
        for idx in range(0, 10):
            idx_ev = -(10 - idx)
            ev = get_ev_from_line(lines[idx_ev])
            assert ev == last_10_events_expected[idx], "Error on event nr {}".format(idx_ev)
Esempio n. 9
0
def check_file_informations(filename_full, expected_output):

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    cmd = "./metavision_raw_info -i \"{}\"".format(filename_full)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0

    # Now check output ,after stripping them for trailing whitespaces
    output_strip = "\n".join([line.strip() for line in output.splitlines()])
    expected_output_strip = "\n".join([line.strip() for line in expected_output.splitlines()])
    assert output_strip.find(expected_output_strip) >= 0
def pytestcase_test_metavision_raw_to_video_non_existing_input_file():
    """
    Checks that metavision_raw_to_video returns an error when passing an input file that doesn't exist
    """

    # Create a filepath that we are sure it does not exist
    tmp_dir = os_tools.TemporaryDirectoryHandler()
    input_rawfile = os.path.join(tmp_dir.temporary_directory(), "data_in.raw")

    cmd = "./metavision_raw_to_video -i {}".format(input_rawfile)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Assert app returned error
    assert error_code != 0

    # And now check that the error came from the fact that the input file could not be read
    assert "not an existing file" in output
Esempio n. 11
0
def pytestcase_test_metavision_raw_cutter_non_existing_input_file():
    '''
    Checks that metavision_raw_cutter returns an error when passing an input file that doesn't exist
    '''

    # Create temporary directory for non existing RAW file
    tmp_dir = os_tools.TemporaryDirectoryHandler()
    input_rawfile = os.path.join(tmp_dir.temporary_directory(), "data_in.raw")
    output_rawfile = os.path.join(tmp_dir.temporary_directory(), "data_out.raw")

    cmd = "./metavision_raw_cutter -i {} --start {} --end {} -o {}".format(input_rawfile, 0, 2, output_rawfile)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited with error
    assert error_code != 0

    # And now check that the error came from the fact that the input file could not be read
    assert "Unable to open RAW file" in output
Esempio n. 12
0
def run_standalone_decoder_and_compare_to_hal_implementation(
        filename_full, format_event):
    """"Run standalone apps and compare its output with the app implemented with hal

    Args:
        filename_full (str): path of the file
        format_event (int): format event of the file (2 or 3)
    """

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    # Create a temporary directory for the output files that will be generated
    tmp_dir = os_tools.TemporaryDirectoryHandler()

    # Run the standalone decoder application
    standalone_output_file = os.path.join(tmp_dir.temporary_directory(),
                                          "standalone_output_file.csv")
    cmd = "./metavision_evt{}_raw_file_decoder \"{}\" \"{}\"".format(
        format_event, filename_full, standalone_output_file)
    output, error, err_code = shell_tools.execute_cmd(cmd)
    assert err_code == 0, "******\nError while executing cmd '{}':{}\n{}\n******".format(
        cmd, output, error)
    assert os.path.exists(standalone_output_file)

    # Now run the application implemented with hal
    hal_output_file = os.path.join(tmp_dir.temporary_directory(),
                                   "hal_output_file.csv")
    cmd = "./raw_file_decoder_with_hal \"{}\" \"{}\"".format(
        filename_full, hal_output_file)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(
        cmd, output)
    assert os.path.exists(hal_output_file)

    # Now open the files and store their content :
    with open(standalone_output_file, 'r') as f:
        standalone_output = f.read()

    with open(hal_output_file, 'r') as f:
        hal_output = f.read()

    assert standalone_output == hal_output
def pytestcase_test_metavision_raw_info_with_sample_plugin(dataset_dir):
    '''
    Checks output of metavision_raw_info application when using sample plugin
    '''

    filename = "sample_plugin_recording.raw"
    filename_full = os.path.join(dataset_dir, filename)

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    cmd = "./metavision_raw_info -i \"{}\"".format(filename_full)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(
        cmd, output)

    expected_output = """
====================================================================================================

Name                {}
Path                {}
Duration            4s 805ms 980us
Integrator          SampleIntegratorName
Plugin name         hal_sample_plugin
Event encoding      SAMPLE-FORMAT-1.0
Camera generation   1.0
Camera systemID     42
Camera serial       000000

====================================================================================================

Type of event       Number of events    First timestamp     Last timestamp      Average event rate
----------------------------------------------------------------------------------------------------
CD                  790840              0                   4805980             164.6 Kev/s
""".format(filename, filename_full)

    # Now check output ,after stripping them for trailing white spaces
    output_strip = "\n".join([line.strip() for line in output.splitlines()])
    expected_output_strip = "\n".join(
        [line.strip() for line in expected_output.splitlines()])
    assert output_strip.find(expected_output_strip) >= 0
Esempio n. 14
0
def pytestcase_test_metavision_raw_cutter_invalid_range(dataset_dir):
    '''
    Checks that metavision_raw_cutter returns an error when passing inconsistent values for start and stop
    '''

    # To be sure the error isn't thrown because the input file doens't exist, use one of the datasets
    input_rawfile = os.path.join(dataset_dir, "gen31_timer.raw")
    assert os.path.exists(input_rawfile)

    # Create temporary directory for non existing RAW file
    tmp_dir = os_tools.TemporaryDirectoryHandler()
    output_rawfile = os.path.join(tmp_dir.temporary_directory(), "data_out.raw")

    start = 4
    end = 2
    cmd = "./metavision_raw_cutter -i {} --start {} --end {} -o {}".format(input_rawfile, start, end, output_rawfile)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited with error
    assert error_code != 0

    # And now check that the error came from the fact that the input range was invalid
    assert re.search("end time {} is less than or equal to start {}".format(end, start), output)
Esempio n. 15
0
def pytestcase_test_metavision_raw_to_dat_with_sample_plugin(dataset_dir):
    '''
    Checks output of metavision_raw_to_dat application when using sample plugin
    '''

    filename = "sample_plugin_recording.raw"
    filename_full = os.path.join(dataset_dir, filename)

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    # Since the application metavision_raw_to_dat writes the output file in the same directory
    # as the input file, in order not to pollute the git status of the repository (input dataset
    # is committed), copy input file in temporary directory and run the app on that

    tmp_dir = os_tools.TemporaryDirectoryHandler()
    input_file = tmp_dir.copy_file_in_tmp_dir(filename_full)
    assert input_file  # i.e. assert input_file != None, to verify the copy was successful

    expected_generated_file = input_file.replace(".raw", "_cd.dat")
    # Just to be sure, check that the DAT file does not already exist, otherwise the test could be misleading
    assert not os.path.exists(expected_generated_file)

    cmd = "./metavision_raw_to_dat -i {}".format(input_file)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(
        cmd, output)

    # Check DAT file has been written
    assert os.path.exists(expected_generated_file)

    # Checking all events would take long, so we just test a subset
    idx_to_check = [
        1, 2114, 20185, 50001, 104618, 116783, 130045, 159141, 171864, 177868,
        181003, 198623, 207518, 213796, 216581, 245937, 250340, 280508, 285551,
        370256, 373888, 380633, 381277, 389611, 396206, 406399, 409008, 411388,
        417972, 472989, 474184, 491985, 505700, 527520, 531732, 535567, 577006,
        582677, 587597, 619476, 632649, 640135, 641796, 662353, 669284, 684812,
        701566, 709096, 757240, 767908, 773760, 790839
    ]
    events_expected = [{
        'x': 294,
        'y': 284,
        'p': 1,
        't': 0
    }, {
        'x': 285,
        'y': 270,
        'p': 0,
        't': 14030
    }, {
        'x': 350,
        'y': 364,
        'p': 1,
        't': 122305
    }, {
        'x': 446,
        'y': 436,
        'p': 1,
        't': 304755
    }, {
        'x': 587,
        'y': 366,
        'p': 0,
        't': 635585
    }, {
        'x': 533,
        'y': 301,
        'p': 1,
        't': 709770
    }, {
        'x': 475,
        'y': 301,
        'p': 0,
        't': 789970
    }, {
        'x': 382,
        'y': 194,
        'p': 1,
        't': 966410
    }, {
        'x': 343,
        'y': 168,
        'p': 1,
        't': 1044600
    }, {
        'x': 365,
        'y': 134,
        'p': 0,
        't': 1080695
    }, {
        'x': 355,
        'y': 119,
        'p': 0,
        't': 1100740
    }, {
        'x': 285,
        'y': 53,
        'p': 1,
        't': 1207010
    }, {
        'x': 243,
        'y': 26,
        'p': 1,
        't': 1261145
    }, {
        'x': 232,
        'y': 7,
        'p': 1,
        't': 1299240
    }, {
        'x': 111,
        'y': 143,
        'p': 0,
        't': 1315680
    }, {
        'x': 119,
        'y': 117,
        'p': 1,
        't': 1493725
    }, {
        'x': 115,
        'y': 144,
        'p': 1,
        't': 1521790
    }, {
        'x': 54,
        'y': 223,
        'p': 0,
        't': 1704250
    }, {
        'x': 563,
        'y': 473,
        'p': 0,
        't': 1734725
    }, {
        'x': 298,
        'y': 467,
        'p': 1,
        't': 2249610
    }, {
        'x': 309,
        'y': 458,
        'p': 1,
        't': 2271665
    }, {
        'x': 323,
        'y': 419,
        'p': 1,
        't': 2313765
    }, {
        'x': 309,
        'y': 417,
        'p': 1,
        't': 2317775
    }, {
        'x': 338,
        'y': 432,
        'p': 0,
        't': 2367900
    }, {
        'x': 353,
        'y': 412,
        'p': 0,
        't': 2408000
    }, {
        'x': 408,
        'y': 360,
        'p': 1,
        't': 2470155
    }, {
        'x': 404,
        'y': 333,
        'p': 1,
        't': 2486195
    }, {
        'x': 401,
        'y': 366,
        'p': 0,
        't': 2500230
    }, {
        'x': 405,
        'y': 346,
        'p': 0,
        't': 2540330
    }, {
        'x': 549,
        'y': 179,
        'p': 1,
        't': 2873165
    }, {
        'x': 579,
        'y': 136,
        'p': 1,
        't': 2881185
    }, {
        'x': 506,
        'y': 82,
        'p': 1,
        't': 2989455
    }, {
        'x': 479,
        'y': 40,
        'p': 1,
        't': 3073660
    }, {
        'x': 423,
        'y': 56,
        'p': 0,
        't': 3205990
    }, {
        'x': 580,
        'y': 217,
        'p': 1,
        't': 3230655
    }, {
        'x': 396,
        'y': 50,
        'p': 0,
        't': 3254115
    }, {
        'x': 273,
        'y': 192,
        'p': 0,
        't': 3506740
    }, {
        'x': 253,
        'y': 233,
        'p': 1,
        't': 3540825
    }, {
        'x': 208,
        'y': 248,
        'p': 1,
        't': 3570900
    }, {
        'x': 105,
        'y': 310,
        'p': 1,
        't': 3763385
    }, {
        'x': 84,
        'y': 384,
        'p': 1,
        't': 3843585
    }, {
        'x': 42,
        'y': 402,
        'p': 1,
        't': 3889695
    }, {
        'x': 43,
        'y': 372,
        'p': 0,
        't': 3899725
    }, {
        'x': 48,
        'y': 474,
        'p': 1,
        't': 4024035
    }, {
        'x': 70,
        'y': 495,
        'p': 1,
        't': 4066140
    }, {
        'x': 93,
        'y': 418,
        'p': 0,
        't': 4160375
    }, {
        'x': 184,
        'y': 371,
        'p': 1,
        't': 4262630
    }, {
        'x': 167,
        'y': 368,
        'p': 0,
        't': 4308740
    }, {
        'x': 343,
        'y': 236,
        'p': 0,
        't': 4601470
    }, {
        'x': 385,
        'y': 182,
        'p': 1,
        't': 4665635
    }, {
        'x': 363,
        'y': 156,
        'p': 0,
        't': 4701720
    }, {
        'x': 455,
        'y': 103,
        'p': 1,
        't': 4805980
    }]
    # Now open the file and check for its contents
    with open(expected_generated_file, 'rb') as f:
        # Parse header
        width = -1
        height = -1
        begin_events_pos = 0
        ev_type = -1
        ev_size = -1
        while (True):
            begin_events_pos = f.tell()
            line = f.readline().decode("latin-1")
            first_char = line[0]
            if first_char == '%':
                # Look for width and height :
                res_width = re.match(r"% Width (\d+)", line)
                if res_width:
                    width = int(res_width.group(1))
                else:
                    res_height = re.match(r"% Height (\d+)", line)
                    if res_height:
                        height = int(res_height.group(1))
            else:
                # Position cursor after header and exit loop
                f.seek(begin_events_pos, os.SEEK_SET)
                # Read event type
                ev_type = np.frombuffer(f.read(1), dtype=np.uint8)[0]
                # Read event size
                ev_size = np.frombuffer(f.read(1), dtype=np.uint8)[0]
                break

        # Verify expected size
        assert width == 600
        assert height == 500

        # Assert event type and size
        assert ev_type == 12  # CD
        assert ev_size == 8

        # Now check total number of CD events and time of first and last
        data = np.fromfile(f, dtype=[('t', 'u4'), ('xyp', 'i4')])

        x = np.bitwise_and(data["xyp"], CD_X_MASK)
        y = np.right_shift(np.bitwise_and(data["xyp"], CD_Y_MASK), 14)
        p = np.right_shift(np.bitwise_and(data["xyp"], CD_P_MASK), 28)

        nr_cd = len(data)
        assert nr_cd == 790840

        # Check events :
        for idx in range(0, len(idx_to_check)):
            event_number = idx_to_check[idx]
            ev = {
                'x': x[event_number],
                'y': y[event_number],
                'p': p[event_number],
                't': data["t"][event_number]
            }
            assert ev == events_expected[idx], "Error on event nr {}".format(
                event_number)
Esempio n. 16
0
def pytestcase_test_metavision_raw_to_csv_with_sample_plugin(dataset_dir):
    '''
    Checks output of metavision_raw_to_csv application when using sample plugin
    '''

    filename = "sample_plugin_recording.raw"
    filename_full = os.path.join(dataset_dir, filename)

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    # Since the application metavision_raw_to_csv writes the output file in the same directory
    # the app is launched from, create a tmp directory from where we can run the app
    tmp_dir = os_tools.TemporaryDirectoryHandler()

    # The pytest is run from the build/bin dir (cf CMakeLists.txt), but since we'll run the command
    # from the temporary directory created above, we need to get the full path to the application
    application_full_path = os.path.join(os.getcwd(), "metavision_raw_to_csv")

    cmd = "\"{}\" -i \"{}\"".format(application_full_path, filename_full)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd, working_directory=tmp_dir.temporary_directory())

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(cmd, output)

    # Check CSV file has been written
    expected_generated_file = os.path.join(tmp_dir.temporary_directory(), "cd.csv")
    assert os.path.exists(expected_generated_file)

    # Checking all events would take long, so we just test a subset
    idx_to_check = [1, 2647, 15098, 31393, 55056, 60392, 126838, 160953, 161756, 239542, 255380, 263120, 286172,
                    305025, 306674, 313755, 337452, 340242, 348143, 362732, 367017, 367516, 411226, 411721, 414272,
                    469949, 478421, 487793, 494067, 502675, 510576, 518535, 519037, 541733, 542226, 595092, 595579,
                    599828, 615304, 653587, 655153, 663832, 665051, 665486, 671977, 676311, 689468, 697722, 700298,
                    745706, 760479, 790839]
    events_expected = [{'x': 294, 'y': 284, 'p': 1, 't': 0}, {'x': 302, 'y': 298, 'p': 1, 't': 16040},
                       {'x': 300, 'y': 327, 'p': 0, 't': 92225}, {'x': 372, 'y': 398, 'p': 1, 't': 190475},
                       {'x': 427, 'y': 430, 'p': 0, 't': 334835}, {'x': 477, 'y': 468, 'p': 1, 't': 366915},
                       {'x': 487, 'y': 270, 'p': 1, 't': 771920}, {'x': 376, 'y': 180, 'p': 1, 't': 978435},
                       {'x': 410, 'y': 205, 'p': 0, 't': 982450}, {'x': 160, 'y': 71, 'p': 0, 't': 1455630},
                       {'x': 110, 'y': 119, 'p': 0, 't': 1551870}, {'x': 66, 'y': 143, 'p': 0, 't': 1599985},
                       {'x': 3, 'y': 214, 'p': 0, 't': 1738335}, {'x': 474, 'y': 293, 'p': 1, 't': 1853820},
                       {'x': 400, 'y': 497, 'p': 0, 't': 1863645}, {'x': 87, 'y': 321, 'p': 0, 't': 1906750},
                       {'x': 161, 'y': 408, 'p': 1, 't': 2051110}, {'x': 207, 'y': 408, 'p': 1, 't': 2067155},
                       {'x': 231, 'y': 413, 'p': 1, 't': 2115275}, {'x': 235, 'y': 446, 'p': 0, 't': 2203495},
                       {'x': 285, 'y': 497, 'p': 1, 't': 2229560}, {'x': 286, 'y': 499, 'p': 1, 't': 2233565},
                       {'x': 408, 'y': 367, 'p': 0, 't': 2498230}, {'x': 405, 'y': 365, 'p': 0, 't': 2502235},
                       {'x': 404, 'y': 317, 'p': 1, 't': 2518275}, {'x': 567, 'y': 189, 'p': 0, 't': 2855120},
                       {'x': 532, 'y': 144, 'p': 1, 't': 2907245}, {'x': 537, 'y': 135, 'p': 0, 't': 2963390},
                       {'x': 522, 'y': 116, 'p': 0, 't': 3001485}, {'x': 459, 'y': 75, 'p': 1, 't': 3053615},
                       {'x': 435, 'y': 32, 'p': 1, 't': 3101735}, {'x': 74, 'y': 192, 'p': 0, 't': 3151055},
                       {'x': 416, 'y': 0, 'p': 1, 't': 3153865}, {'x': 353, 'y': 109, 'p': 1, 't': 3292205},
                       {'x': 355, 'y': 110, 'p': 1, 't': 3294215}, {'x': 31, 'y': 307, 'p': 1, 't': 3615615},
                       {'x': 177, 'y': 261, 'p': 1, 't': 3619020}, {'x': 192, 'y': 285, 'p': 1, 't': 3645085},
                       {'x': 157, 'y': 326, 'p': 0, 't': 3739320}, {'x': 41, 'y': 445, 'p': 0, 't': 3971900},
                       {'x': 357, 'y': 491, 'p': 0, 't': 3980725}, {'x': 42, 'y': 479, 'p': 1, 't': 4034055},
                       {'x': 76, 'y': 216, 'p': 0, 't': 4040475}, {'x': 51, 'y': 484, 'p': 1, 't': 4044080},
                       {'x': 54, 'y': 492, 'p': 0, 't': 4082180}, {'x': 99, 'y': 441, 'p': 1, 't': 4110245},
                       {'x': 107, 'y': 440, 'p': 0, 't': 4188445}, {'x': 119, 'y': 398, 'p': 0, 't': 4239170},
                       {'x': 158, 'y': 409, 'p': 0, 't': 4254610}, {'x': 278, 'y': 237, 'p': 0, 't': 4531295},
                       {'x': 363, 'y': 195, 'p': 1, 't': 4621520}, {'x': 455, 'y': 103, 'p': 1, 't': 4805980}]

    # Now open the file and check for its contents
    with open(expected_generated_file, 'r') as f:
        lines = f.readlines()

        # Check number of events
        nr_events = len(lines)
        assert nr_events == 790840

        # Check events :
        for idx in range(0, len(idx_to_check)):
            ev = get_ev_from_line(lines[idx_to_check[idx]])
            assert ev == events_expected[idx], "Error on event nr {}".format(idx_to_check[idx])
Esempio n. 17
0
def run_raw_to_dat_on_cd_recording_and_check_result(
        filename_full, width_expected, height_expected, number_cd_expected,
        first_10_events_expected, middle_10_events_expected,
        last_10_events_expected):

    # Before launching the app, check the dataset file exists
    assert os.path.exists(filename_full)

    # Since the application metavision_raw_to_dat writes the output file in the same directory
    # as the input file, in order not to pollute the git status of the repository (input dataset
    # is committed), copy input file in temporary directory and run the app on that

    tmp_dir = os_tools.TemporaryDirectoryHandler()
    input_file = tmp_dir.copy_file_in_tmp_dir(filename_full)
    assert input_file  # i.e. assert input_file != None, to verify the copy was successful

    expected_generated_file = input_file.replace(".raw", "_cd.dat")
    # Just to be sure, check that the DAT file does not already exist, otherwise the test could be misleading
    assert not os.path.exists(expected_generated_file)

    cmd = "./metavision_raw_to_dat -i {}".format(input_file)
    output, error_code = pytest_tools.run_cmd_setting_mv_log_file(cmd)

    # Check app exited without error
    assert error_code == 0, "******\nError while executing cmd '{}':{}\n******".format(
        cmd, output)

    # Check DAT file has been written
    assert os.path.exists(expected_generated_file)

    # Now open the file and check for its contents
    with open(expected_generated_file, 'rb') as f:
        # Parse header
        width = -1
        height = -1
        begin_events_pos = 0
        ev_type = -1
        ev_size = -1
        while (True):
            begin_events_pos = f.tell()
            line = f.readline().decode("latin-1")
            first_char = line[0]
            if first_char == '%':
                # Look for width and height :
                res_width = re.match(r"% Width (\d+)", line)
                if res_width:
                    width = int(res_width.group(1))
                else:
                    res_height = re.match(r"% Height (\d+)", line)
                    if res_height:
                        height = int(res_height.group(1))
            else:
                # Position cursor after header and exit loop
                f.seek(begin_events_pos, os.SEEK_SET)
                # Read event type
                ev_type = np.frombuffer(f.read(1), dtype=np.uint8)[0]
                # Read event size
                ev_size = np.frombuffer(f.read(1), dtype=np.uint8)[0]
                break

        # Verify expected size
        assert width == width_expected
        assert height == height_expected

        # Assert event type and size
        assert ev_type == 12  # CD
        assert ev_size == 8

        # Now check total number of CD events and time of first and last
        data = np.fromfile(f, dtype=[('t', 'u4'), ('xyp', 'i4')])

        x = np.bitwise_and(data["xyp"], CD_X_MASK)
        y = np.right_shift(np.bitwise_and(data["xyp"], CD_Y_MASK), 14)
        p = np.right_shift(np.bitwise_and(data["xyp"], CD_P_MASK), 28)

        nr_cd = len(data)
        assert nr_cd == number_cd_expected

        # Check first 10 events :
        for idx in range(0, 10):
            ev = {'x': x[idx], 'y': y[idx], 'p': p[idx], 't': data["t"][idx]}
            assert ev == first_10_events_expected[
                idx], "Error on event nr {}".format(idx)

        # Check the 10 events in the middle:
        idx_ev = nr_cd // 2 - 5
        for idx in range(0, 10):
            ev = {
                'x': x[idx_ev],
                'y': y[idx_ev],
                'p': p[idx_ev],
                't': data["t"][idx_ev]
            }
            assert ev == middle_10_events_expected[
                idx], "Error on event nr {}".format(idx_ev)
            idx_ev += 1

        # Check last 10 events :
        for idx in range(0, 10):
            idx_ev = -(10 - idx)
            ev = {
                'x': x[idx_ev],
                'y': y[idx_ev],
                'p': p[idx_ev],
                't': data["t"][idx_ev]
            }
            assert ev == last_10_events_expected[
                idx], "Error on event nr {}".format(idx_ev)