Ejemplo n.º 1
0
    def test_filewriter(self):
        """Test FileWriter functionality"""

        data = [[i + j for j in range(3)] for i in range(3)]
        expected_csv_rows = ['0,1,2\r\n', '1,2,3\r\n', '2,3,4\r\n']

        filewriter = FileWriter('foo.csv')
        filewriter.set_device_info(DeviceInfo(name='foo-device', fs=100,
                                              channels=['c1', 'c2', 'c3']))

        mockopen = mock_open()
        with patch('bcipy.acquisition.processor.open', mockopen):
            with filewriter:
                mockopen.assert_called_once_with('foo.csv', 'w', newline='')

                handle = mockopen()
                handle.write.assert_called_with('timestamp,c1,c2,c3\r\n')

                for i, row in enumerate(data):
                    timestamp = float(i)
                    filewriter.process(row, timestamp)
                    handle.write.assert_called_with(
                        str(timestamp) + "," + str(expected_csv_rows[i]))

            mockopen().close.assert_called_once()
Ejemplo n.º 2
0
def file_data(path: str):
    """Constructs a QueueDataSource from the contents of the file at the given
    path. Data is written to the datasource at the rate specified in the
    raw_data.csv metadata, so it acts as a live stream.

    Parameters
    ----------
        path - path to the raw_data.csv file
    Returns
    -------
        (QueueDataSource, DeviceInfo, FileStreamer)
            - QueueDataSource can be provided to the EEGFrame
            - DeviceInfo provides the metadata read from the raw_data.csv
            - data is not written to the QueueDataSource until the
                FileStreamer (StoppableProcess) is started.
    """

    from bcipy.gui.viewer.data_source.file_streamer import FileStreamer
    # read metadata
    with open(path) as csvfile:
        r1 = next(csvfile)
        name = r1.strip().split(",")[1]
        r2 = next(csvfile)
        freq = float(r2.strip().split(",")[1])

        reader = csv.reader(csvfile)
        channels = next(reader)
    queue = Queue()
    streamer = FileStreamer(path, queue)
    data_source = QueueDataSource(queue)
    device_info = DeviceInfo(fs=freq, channels=channels, name=name)
    streamer.start()

    return (data_source, device_info, streamer)
Ejemplo n.º 3
0
 def device_info(self) -> DeviceInfo:
     """Information about the acquisition parameters. Should be called after
     acquisition_init for those devices which set this information. Note that
     DeviceInfo may differ from the DeviceSpec if additional information is
     added by the Connector."""
     device_name = self.name if not callable(self.name) else self.name()
     return DeviceInfo(fs=self.fs, channels=self.channels, name=device_name)
Ejemplo n.º 4
0
def file_data(path):
    """Reads raw_data; returns raw_data and device_info."""
    with open(path) as csvfile:
        # read metadata
        r1 = next(csvfile)
        name = r1.strip().split(",")[1]
        r2 = next(csvfile)
        freq = float(r2.strip().split(",")[1])

        reader = csv.reader(csvfile)
        channels = next(reader)

        # read the rest of the lines into a list
        data = []
        for line in reader:
            data.append(line)

    device_info = DeviceInfo(fs=freq, channels=channels, name=name)
    return (data, device_info)
Ejemplo n.º 5
0
    def __init__(self, stream_type: str = 'EEG'):
        super(LslDataSource, self).__init__()

        print('Waiting for LSL EEG data stream...')
        self.stream_type = stream_type
        streams = pylsl.resolve_stream('type', self.stream_type)
        inlet = pylsl.StreamInlet(streams[0])
        info = inlet.info()

        fs = float(info.nominal_srate())
        self.sample_rate = fs
        print(f'Sample rate: {fs}')
        name = info.name()
        channel_names = []
        ch = info.desc().child("channels").child("channel")
        for k in range(info.channel_count()):
            channel_names.append(ch.child_value("label"))
            ch = ch.next_sibling()

        self.device_info = DeviceInfo(fs=fs, channels=channel_names, name=name)
        self.inlet = inlet
Ejemplo n.º 6
0
    def test_set_device_info(self):
        """Test MultiProcessor functionality"""

        proc1 = mock(Processor)
        proc2 = mock(Processor)
        proc3 = mock(Processor)

        when(proc1).set_device_info(any()).thenReturn(None)
        when(proc2).set_device_info(any()).thenReturn(None)
        when(proc3).set_device_info(any()).thenReturn(None)

        multi = DispatchProcessor(proc1, proc2)

        device_info = DeviceInfo(name='foo-device', fs=100,
                                 channels=['c1', 'c2', 'c3'])

        multi.set_device_info(device_info)
        verify(proc1, times=1).set_device_info(device_info)
        verify(proc2, times=1).set_device_info(device_info)

        multi.add(proc3)
        verify(proc3, times=1).set_device_info(device_info)
Ejemplo n.º 7
0
 def device_info(self):
     """Information about the acquisition parameters. Should be called after
     acquisition_init for those devices which set this information."""
     device_name = self.name if not callable(self.name) else self.name()
     return DeviceInfo(fs=self.fs, channels=self.channels, name=device_name)