예제 #1
0
def test_read_raw_on_test_data():
    import glob
    from rerpy.test import test_data_path
    tested = 0
    for rawp in glob.glob(test_data_path("erpss/*.raw")):
        crwp = rawp[:-3] + "crw"
        print rawp, crwp
        assert_files_match(rawp, crwp)
        tested += 1
    # Cross-check, to make sure is actually finding the files... (bump up this
    # number if you add more test files):
    assert tested == 5
예제 #2
0
파일: erpss.py 프로젝트: rerpy/rerpy
def test_read_raw_on_test_data():
    import glob
    from rerpy.test import test_data_path
    tested = 0
    for rawp in glob.glob(test_data_path("erpss/*.raw")):
        crwp = rawp[:-3] + "crw"
        print rawp, crwp
        assert_files_match(rawp, crwp)
        tested += 1
    # Cross-check, to make sure is actually finding the files... (bump up this
    # number if you add more test files):
    assert tested == 5
예제 #3
0
def test_64bit_channel_names():
    from rerpy.test import test_data_path
    stream = open(test_data_path("erpss/two-chunks-64chan.raw"), "rb")
    (_, hz, channel_names, codes, data, info) = read_raw(stream, int, True)
    # "Correct" channel names as listed by headinfo(1):
    assert (channel_names == [
        "LOPf", "ROPf", "LMPf", "RMPf", "LTPf", "RTPf", "LLPf", "RLPf", "LPrA",
        "RPrA", "LTFr", "RTFr", "LLFr", "RLFr", "LDPf", "RDPf", "LTOc", "RTOc",
        "LTCe", "RTCe", "LLCe", "RLCe", "LDFr", "RDFr", "LMFr", "RMFr", "MiFo",
        "MiPf", "MiFr", "A2", "LHEy", "RHEy", "LIOc", "RIOc", "LLOc", "RLOc",
        "LLPP", "RLPP", "LLPa", "RLPa", "LDCe", "RDCe", "LMCe", "RMCe", "LDOc",
        "RDOc", "LDPP", "RDPP", "LDPa", "RDPa", "LCer", "RCer", "LMOc", "RMOc",
        "LMPP", "RMPP", "LMPa", "RMPa", "MiCe", "MiPa", "MiPP", "MiOc", "LLEy",
        "RLEy"
    ]).all()
예제 #4
0
def test_LazyRecspan():
    from nose.tools import assert_raises
    from rerpy.test import test_data_path
    for suffix in ["crw", "raw"]:
        (fetcher, hz, channames, codes, data, info) = read_raw(
            open(test_data_path("erpss/tiny-complete.%s" % (suffix, )), "rb"),
            "u2", False)
        # This fake recspan is chosen to cover part of the first and last
        # chunks, plus the entire middle chunk. It's exactly 512 samples long.
        lr = LazyRecspan(fetcher, "u2", len(channames), 128, 640)
        assert_raises(IndexError, lr.get_slice, 0, 513)
        for (start, stop) in [(0, 512), (10, 20), (256, 266), (500, 510),
                              (120, 130)]:
            assert np.all(
                lr.get_slice(start, stop) == data[128 + start:128 + stop])
예제 #5
0
파일: erpss.py 프로젝트: rerpy/rerpy
def test_64bit_channel_names():
    from rerpy.test import test_data_path
    stream = open(test_data_path("erpss/two-chunks-64chan.raw"), "rb")
    (_, hz, channel_names, codes, data, info) = read_raw(stream, int, True)
    # "Correct" channel names as listed by headinfo(1):
    assert (channel_names ==
            ["LOPf", "ROPf", "LMPf", "RMPf", "LTPf", "RTPf", "LLPf", "RLPf",
             "LPrA",  "RPrA", "LTFr", "RTFr", "LLFr", "RLFr", "LDPf", "RDPf",
             "LTOc", "RTOc", "LTCe", "RTCe", "LLCe", "RLCe", "LDFr", "RDFr",
             "LMFr", "RMFr", "MiFo", "MiPf", "MiFr", "A2",   "LHEy", "RHEy",
             "LIOc", "RIOc", "LLOc", "RLOc", "LLPP", "RLPP", "LLPa", "RLPa",
             "LDCe", "RDCe", "LMCe", "RMCe", "LDOc", "RDOc", "LDPP", "RDPP",
             "LDPa", "RDPa", "LCer", "RCer", "LMOc", "RMOc", "LMPP", "RMPP",
             "LMPa", "RMPa", "MiCe", "MiPa", "MiPP", "MiOc", "LLEy", "RLEy"]
            ).all()
예제 #6
0
파일: erpss.py 프로젝트: rerpy/rerpy
def test_LazyRecspan():
    from nose.tools import assert_raises
    from rerpy.test import test_data_path
    for suffix in ["crw", "raw"]:
        (fetcher, hz, channames, codes, data, info) = read_raw(
            open(test_data_path("erpss/tiny-complete.%s" % (suffix,)), "rb"),
            "u2", False)
        # This fake recspan is chosen to cover part of the first and last
        # chunks, plus the entire middle chunk. It's exactly 512 samples long.
        lr = LazyRecspan(fetcher, "u2", len(channames), 128, 640)
        assert_raises(IndexError, lr.get_slice, 0, 513)
        for (start, stop) in [(0, 512), (10, 20), (256, 266), (500, 510),
                              (120, 130)]:
            assert np.all(lr.get_slice(start, stop)
                          == data[128 + start:128 + stop])
예제 #7
0
def test_load_erpss():
    from rerpy.test import test_data_path
    # This crw/log file is constructed to have a few features:
    # - it only has 3 records, so it's tiny
    # - the first two records are in one recspan, the last is in a second, so
    #   we test the recspan splitting code
    # - the first recspan ends in a PAUSE event, the second ends in a DELETE
    #   event, so we test the deleted event handling.
    # There are some weird things about it too:
    # - several events in the first recspan have condition 0, to test
    #   calibration pulse stuff. In a normal ERPSS file all events within a
    #   single recspan would have the same condition number.
    # - most of the event codes are >32767. In a normal ERPSS file such events
    #   are supposed to be reserved for special stuff and deleted events, but
    #   it happens the file I was using as a basis violated this rule. Oh
    #   well.
    for lazy in [False, True]:
        dataset = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                             test_data_path("erpss/tiny-complete.log"),
                             lazy=lazy)
        assert len(dataset) == 2
        assert dataset[0].shape == (512, 32)
        assert dataset[1].shape == (256, 32)

        assert dataset.data_format.exact_sample_rate_hz == 250
        assert dataset.data_format.units == "RAW"
        assert list(dataset.data_format.channel_names) == [
            "lle",
            "lhz",
            "MiPf",
            "LLPf",
            "RLPf",
            "LMPf",
            "RMPf",
            "LDFr",
            "RDFr",
            "LLFr",
            "RLFr",
            "LMFr",
            "RMFr",
            "LMCe",
            "RMCe",
            "MiCe",
            "MiPa",
            "LDCe",
            "RDCe",
            "LDPa",
            "RDPa",
            "LMOc",
            "RMOc",
            "LLTe",
            "RLTe",
            "LLOc",
            "RLOc",
            "MiOc",
            "A2",
            "HEOG",
            "rle",
            "rhz",
        ]

        for recspan_info in dataset.recspan_infos:
            assert recspan_info["raw_file"].endswith("tiny-complete.crw")
            assert recspan_info["log_file"].endswith("tiny-complete.log")
            assert recspan_info["experiment"] == "brown-1"
            assert recspan_info["subject"] == "Subject p3 2008-08-20"
            assert recspan_info["odelay"] == 8
            assert len(recspan_info["erpss_raw_header"]) == 512

        assert dataset.recspan_infos[0].ticks == 512
        assert dataset.recspan_infos[1].ticks == 256
        assert dataset.recspan_infos[1]["deleted"]

        assert len(dataset.events()) == 14
        # 2 are calibration events
        assert len(dataset.events("has code")) == 12
        for ev in dataset.events("has code"):
            assert ev["condition"] in (64, 65)
            assert ev["flag"] == 0
            assert not ev["flag_data_error"]
            assert not ev["flag_polinv"]
            assert not ev["flag_rejected"]
        for ev in dataset.events("calibration_pulse"):
            assert dict(ev) == {"calibration_pulse": True}

        def check_ticks(query, recspan_ids, start_ticks):
            events = dataset.events(query)
            assert len(events) == len(recspan_ids) == len(start_ticks)
            for ev, recspan_id, start_tick in zip(events, recspan_ids,
                                                  start_ticks):
                assert ev.recspan_id == recspan_id
                assert ev.start_tick == start_tick
                assert ev.stop_tick == start_tick + 1

        check_ticks("condition == 64", [0] * 8,
                    [21, 221, 304, 329, 379, 458, 483, 511])
        check_ticks("condition == 65", [1] * 4,
                    [533 - 512, 733 - 512, 762 - 512, 767 - 512])
        check_ticks("calibration_pulse", [0, 0], [250, 408])

        # check calibration_events option
        dataset2 = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                              test_data_path("erpss/tiny-complete.log"),
                              lazy=lazy,
                              calibration_events="condition == 65")
        assert len(dataset2.events("condition == 65")) == 0
        assert len(dataset2.events("condition == 0")) == 2
        assert len(dataset2.events("calibration_pulse")) == 4

        # check calibration
        # idea: if calibration works, then the "calibration erp" will have been
        # set to be the same size as whatever we told it to be.
        dataset_cal = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                                 test_data_path("erpss/tiny-complete.log"),
                                 lazy=lazy,
                                 calibration_events="condition == 65",
                                 calibrate=True,
                                 calibrate_half_width_ticks=2,
                                 calibrate_low_cursor_time=-16,
                                 calibrate_high_cursor_time=21,
                                 calibrate_pulse_size=12.34,
                                 calibrate_polarity=-1)
        assert dataset_cal.data_format.units == "uV"
        # -16 ms +/-2 ticks = -24 to -8 ms
        low_cal = dataset_cal.rerp("calibration_pulse",
                                   -24,
                                   -8,
                                   "1",
                                   all_or_nothing=True,
                                   overlap_correction=False)
        # 21 ms rounds to 20 ms, +/-2 ticks for the window = 12 to 28 ms
        high_cal = dataset_cal.rerp("calibration_pulse",
                                    12,
                                    28,
                                    "1",
                                    all_or_nothing=True,
                                    overlap_correction=False)
        low = low_cal.betas["Intercept"].mean(axis=0)
        high = high_cal.betas["Intercept"].mean(axis=0)
        assert np.allclose(high - low, -1 * 12.34)

        # check that we can load from file handles (not sure if anyone cares but
        # hey you never know...)
        crw = open(test_data_path("erpss/tiny-complete.crw"), "rb")
        log = open(test_data_path("erpss/tiny-complete.log"), "rb")
        assert len(load_erpss(crw, log, lazy=lazy)) == 2

        # check that code/raw mismatch is detected
        from nose.tools import assert_raises
        for bad in ["bad-code", "bad-tick", "bad-tick2"]:
            assert_raises(ValueError,
                          load_erpss,
                          test_data_path("erpss/tiny-complete.crw"),
                          test_data_path("erpss/tiny-complete.%s.log" %
                                         (bad, )),
                          lazy=lazy)
        # But if the only mismatch is an event that is "deleted" (sign bit
        # set) in the log file, but not in the raw file, then that is okay:
        load_erpss(test_data_path("erpss/tiny-complete.crw"),
                   test_data_path("erpss/tiny-complete.code-deleted.log"),
                   lazy=lazy)

    # Compare lazy to eager directly
    eager = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                       test_data_path("erpss/tiny-complete.log"),
                       lazy=False)
    lazy = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                      test_data_path("erpss/tiny-complete.log"),
                      lazy=True)
    from pandas.util.testing import assert_frame_equal
    assert len(eager) == len(lazy)
    for eager_recspan, lazy_recspan in zip(eager, lazy):
        assert_frame_equal(eager_recspan, lazy_recspan)
예제 #8
0
파일: erpss.py 프로젝트: rerpy/rerpy
def test_load_erpss():
    from rerpy.test import test_data_path
    # This crw/log file is constructed to have a few features:
    # - it only has 3 records, so it's tiny
    # - the first two records are in one recspan, the last is in a second, so
    #   we test the recspan splitting code
    # - the first recspan ends in a PAUSE event, the second ends in a DELETE
    #   event, so we test the deleted event handling.
    # There are some weird things about it too:
    # - several events in the first recspan have condition 0, to test
    #   calibration pulse stuff. In a normal ERPSS file all events within a
    #   single recspan would have the same condition number.
    # - most of the event codes are >32767. In a normal ERPSS file such events
    #   are supposed to be reserved for special stuff and deleted events, but
    #   it happens the file I was using as a basis violated this rule. Oh
    #   well.
    for lazy in [False, True]:
        dataset = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                             test_data_path("erpss/tiny-complete.log"),
                             lazy=lazy)
        assert len(dataset) == 2
        assert dataset[0].shape == (512, 32)
        assert dataset[1].shape == (256, 32)

        assert dataset.data_format.exact_sample_rate_hz == 250
        assert dataset.data_format.units == "RAW"
        assert list(dataset.data_format.channel_names) == [
            "lle", "lhz", "MiPf", "LLPf", "RLPf", "LMPf", "RMPf", "LDFr", "RDFr",
            "LLFr", "RLFr", "LMFr", "RMFr", "LMCe", "RMCe", "MiCe", "MiPa", "LDCe",
            "RDCe", "LDPa", "RDPa", "LMOc", "RMOc", "LLTe", "RLTe", "LLOc", "RLOc",
            "MiOc", "A2", "HEOG", "rle", "rhz",
            ]

        for recspan_info in dataset.recspan_infos:
            assert recspan_info["raw_file"].endswith("tiny-complete.crw")
            assert recspan_info["log_file"].endswith("tiny-complete.log")
            assert recspan_info["experiment"] == "brown-1"
            assert recspan_info["subject"] == "Subject p3 2008-08-20"
            assert recspan_info["odelay"] == 8
            assert len(recspan_info["erpss_raw_header"]) == 512

        assert dataset.recspan_infos[0].ticks == 512
        assert dataset.recspan_infos[1].ticks == 256
        assert dataset.recspan_infos[1]["deleted"]

        assert len(dataset.events()) == 14
        # 2 are calibration events
        assert len(dataset.events("has code")) == 12
        for ev in dataset.events("has code"):
            assert ev["condition"] in (64, 65)
            assert ev["flag"] == 0
            assert not ev["flag_data_error"]
            assert not ev["flag_polinv"]
            assert not ev["flag_rejected"]
        for ev in dataset.events("calibration_pulse"):
            assert dict(ev) == {"calibration_pulse": True}
        def check_ticks(query, recspan_ids, start_ticks):
            events = dataset.events(query)
            assert len(events) == len(recspan_ids) == len(start_ticks)
            for ev, recspan_id, start_tick in zip(events, recspan_ids, start_ticks):
                assert ev.recspan_id == recspan_id
                assert ev.start_tick == start_tick
                assert ev.stop_tick == start_tick + 1

        check_ticks("condition == 64",
                    [0] * 8, [21, 221, 304, 329, 379, 458, 483, 511])
        check_ticks("condition == 65",
                    [1] * 4,
                    [533 - 512, 733 - 512, 762 - 512, 767 - 512])
        check_ticks("calibration_pulse", [0, 0], [250, 408])

        # check calibration_events option
        dataset2 = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                              test_data_path("erpss/tiny-complete.log"),
                              lazy=lazy,
                              calibration_events="condition == 65")
        assert len(dataset2.events("condition == 65")) == 0
        assert len(dataset2.events("condition == 0")) == 2
        assert len(dataset2.events("calibration_pulse")) == 4

        # check calibration
        # idea: if calibration works, then the "calibration erp" will have been
        # set to be the same size as whatever we told it to be.
        dataset_cal = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                                 test_data_path("erpss/tiny-complete.log"),
                                 lazy=lazy,
                                 calibration_events="condition == 65",
                                 calibrate=True,
                                 calibrate_half_width_ticks=2,
                                 calibrate_low_cursor_time=-16,
                                 calibrate_high_cursor_time=21,
                                 calibrate_pulse_size=12.34,
                                 calibrate_polarity=-1)
        assert dataset_cal.data_format.units == "uV"
        # -16 ms +/-2 ticks = -24 to -8 ms
        low_cal = dataset_cal.rerp("calibration_pulse", -24, -8, "1",
                                   all_or_nothing=True,
                                   overlap_correction=False)
        # 21 ms rounds to 20 ms, +/-2 ticks for the window = 12 to 28 ms
        high_cal = dataset_cal.rerp("calibration_pulse", 12, 28, "1",
                                    all_or_nothing=True,
                                    overlap_correction=False)
        low = low_cal.betas["Intercept"].mean(axis=0)
        high = high_cal.betas["Intercept"].mean(axis=0)
        assert np.allclose(high - low, -1 * 12.34)

        # check that we can load from file handles (not sure if anyone cares but
        # hey you never know...)
        crw = open(test_data_path("erpss/tiny-complete.crw"), "rb")
        log = open(test_data_path("erpss/tiny-complete.log"), "rb")
        assert len(load_erpss(crw, log, lazy=lazy)) == 2

        # check that code/raw mismatch is detected
        from nose.tools import assert_raises
        for bad in ["bad-code", "bad-tick", "bad-tick2"]:
            assert_raises(ValueError,
                          load_erpss,
                          test_data_path("erpss/tiny-complete.crw"),
                          test_data_path("erpss/tiny-complete.%s.log" % (bad,)),
                          lazy=lazy)
        # But if the only mismatch is an event that is "deleted" (sign bit
        # set) in the log file, but not in the raw file, then that is okay:
        load_erpss(test_data_path("erpss/tiny-complete.crw"),
                   test_data_path("erpss/tiny-complete.code-deleted.log"),
                   lazy=lazy)

    # Compare lazy to eager directly
    eager = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                     test_data_path("erpss/tiny-complete.log"),
                     lazy=False)
    lazy = load_erpss(test_data_path("erpss/tiny-complete.crw"),
                      test_data_path("erpss/tiny-complete.log"),
                      lazy=True)
    from pandas.util.testing import assert_frame_equal
    assert len(eager) == len(lazy)
    for eager_recspan, lazy_recspan in zip(eager, lazy):
        assert_frame_equal(eager_recspan, lazy_recspan)