コード例 #1
0
def stream_test(mocker, channels, read_data=[], segs=1):
    """Main function to test the stream with several channels and data"""
    # Creates the mock read_data
    if (not isinstance(read_data, np.ndarray)):
        if (read_data == []):
            read_data = [[0, 0, 0, 0, 0] for i in range(10)]
            for i in channels:
                sample = random.sample(range(1024), len(read_data))
                for j in range(len(read_data)):
                    read_data[j].append(sample[j])
            read_data = np.array(read_data)

    # Init device
    mocker.patch.object(bitalino, 'BITalino')
    pytest.device = bitalino_lsl.BitalinoLSL(pytest.mac_address)
    pytest.device.create_lsl_EEG(channels)
    mocker.patch.object(pytest.device._bitalino, 'start')
    mocker.patch.object(pytest.device._bitalino, 'read')
    pytest.device._bitalino.read.return_value = read_data
    sampling_rate = pytest.device.get_sampling_rate()
    if (isinstance(read_data, np.ndarray)):
        pytest.device._set_n_samples(len(read_data))
        stream_data = list(map(lambda x: x[5:], read_data))
        # transpose
        stream_data = [list(i) for i in zip(*stream_data)]

    # Start streaming
    pytest.device.start()
    streams = resolve_stream(1.0, 'type', 'EEG')
    if (streams != []):
        inlet = StreamInlet(streams[0])
        first_data = inlet.pull_sample(timeout=_TIMEOUT)
        old_sample, old_timestamp = (first_data[0], first_data[1])
    else:
        segs = 0

    # Getting the stream and asserting
    for i in range(sampling_rate * segs):
        if (pytest.device.threads_alive()):
            sample, timestamp = inlet.pull_sample(timeout=_TIMEOUT)
        else:
            break
        if (sample == None or old_sample == None):
            break
        for j in range(len(sample)):
            assert sample[j] in stream_data[j]
            #index = stream_data[j].index(sample[j])
            #assert stream_data[j][index - 1] == old_sample[j]
            #print("old = {old:6.4f}".format(old = old_timestamp))
            assert timestamp == old_timestamp + 1.0 / sampling_rate
        old_sample = sample
        old_timestamp = timestamp
    pytest.device.stop()
コード例 #2
0
def connect_my_bitalino(data, request):
    pytest.device = bitalino_lsl.BitalinoLSL(pytest.mac_address)
    def end():
        pytest.device.close()
        time.sleep(1)
    request.addfinalizer(end)
コード例 #3
0
def test_my_bitalino(mocker, data):
    mocker.patch.object(bitalino, 'BITalino')
    pytest.device = bitalino_lsl.BitalinoLSL(pytest.mac_address)
    device = bitalino_lsl.BitalinoLSL(pytest.mac_address)
    mocker.patch.object(pytest.device._bitalino, 'close')
    device.close()
コード例 #4
0
def test_bitalino_no_mac():
    with pytest.raises(Exception) as excinfo:
        device = bitalino_lsl.BitalinoLSL("wrong_mac")
        device.close()
    assert "The specified address is invalid." in str(excinfo.value)
コード例 #5
0
def test_bitalino_no_device():
    with pytest.raises(Exception) as excinfo:
        device = bitalino_lsl.BitalinoLSL("FF:FF:FF:FF:FF:FF")
        device.close()
    assert "Host is down" in str(excinfo.value)
コード例 #6
0
def test_my_bitalino(data):
    device = bitalino_lsl.BitalinoLSL(pytest.mac_address)
    device.close()
コード例 #7
0
def run_around_tests(data, capsys):
    pytest.device = bitalino_lsl.BitalinoLSL(pytest.mac_address)
    yield
    time.sleep(1)