Пример #1
0
    def __init__(self, input_port, lowcut, highcut, order=4):
        Node.__init__(self, input_port)

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency
        )

        fs = self.input.sampling_frequency
        nyq = 0.5 * fs
        low = lowcut / nyq
        high = highcut / nyq

        # calculate a and b, properties of the Butter filter
        self._b, self._a = signal.butter(
            order,
            [low, high],
            analog=False,
            btype='band',
            output='ba')

        # initial condition zi
        len_to_conserve = max(len(self._a), len(self._b)) - 1
        self._zi = np.zeros((len(self.input.channels), len_to_conserve))

        Node.log_instance(self, {
            'lowcut': lowcut,
            'highcut': highcut,
            'order': order
        })
Пример #2
0
    def __init__(self, file):
        Node.__init__(self, None)
        try:
            config = Config(file)
        except (ConfigFileNotInAccordance, FileNotFound, InvalidXml) as err:
            print(err)
            exit()

        self._scenario = config.create_a_new_scenario()

        self.output.set_parameters(data_type='marker',
                                   channels=['marker'],
                                   sampling_frequency=0,
                                   meta='')

        # create the plot process
        self.plot_pipe, plotter_pipe = mp.Pipe()
        self.plotter = ProcessStim(len(self._scenario))
        self.plot_process = mp.Process(target=self.plotter,
                                       args=(plotter_pipe, ),
                                       daemon=True)
        self.plot_process.start()

        Node.log_instance(
            self, {
                'file': file,
                'name': config.name,
                'author': config.author,
                'session': config.session,
                'nb of trials': config.number_of_trials,
                'classes': config.classes,
                'random': config.random,
                'type': config.type
            })
        self._start_time = None
Пример #3
0
    def __init__(self, input_port, marker_input_port, stimulation, offset, duration):
        Node.__init__(self, input_port)

        self.marker_input = marker_input_port

        self.output.set_parameters(
            data_type='epoch',
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=0
        )

        self.stimulation = stimulation
        self.duration = duration
        self.offset = offset

        # persitent is data of a non-complete epoch
        self.persistent = pd.DataFrame([], [], self.input.channels)

        self.markers = []

        Node.log_instance(self, {
            'marker port': self.marker_input.id,
            'stimulation': self.stimulation,
            'type stimulation': type(self.stimulation),
            'offset': self.offset,
            'duration': self.duration})
Пример #4
0
    def __init__(self, input_port, frequency_to_remove, quality_factor):
        Node.__init__(self, input_port)

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency
        )

        # calculate a and b, properties of the Butter filter
        self._b, self._a = signal.iirnotch(
            w0=frequency_to_remove,
            Q=quality_factor,
            fs=self.input.sampling_frequency)

        # initial condition zi
        len_to_conserve = max(len(self._a), len(self._b)) - 1
        self._zi = np.zeros((len(self.input.channels), len_to_conserve))

        Node.log_instance(self, {
            'frequency to remove': frequency_to_remove,
            'quality factor': quality_factor
        })
Пример #5
0
    def __init__(self, input_port, duration=10, channels='all', way='index'):
        Node.__init__(self, input_port, None)

        if channels == 'all':
            self._channels = self.input.channels
        else:
            if way == 'index':
                self._channels = [self.input.channels[i - 1] for i in channels]
            elif way == 'name':
                self._channels = channels

        self._duration = float(duration)

        # create the plot process
        self.plot_pipe, plotter_pipe = mp.Pipe()
        self.plotter = ProcessPlotter(self._duration, self._channels,
                                      self.input.sampling_frequency)
        self.plot_process = mp.Process(target=self.plotter,
                                       args=(plotter_pipe, ),
                                       daemon=True)
        self.plot_process.start()

        # Log new instance
        Node.log_instance(self, {
            'duration': self._duration,
            'channels': self._channels
        })
Пример #6
0
    def __init__(self, input_port, model_file, output):
        Node.__init__(self, input_port)
        # load model from save
        self._loaded_model = joblib.load(model_file)

        assert output in ['class', 'probability']
        self._output = output

        # verify the input signal type
        if self._output == 'class':
            self._columns = ['class']
        elif self._output == 'probability':
            self._columns = self.input.channels

        # set the ouput Port parameters
        self.output.set_parameters(
            data_type='signal',
            channels=self._columns,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta
        )
        # log the new instance
        Node.log_instance(self, {
            'path to model': model_file,
            'model': self._loaded_model
        })
Пример #7
0
    def __init__(self, input_port):
        Node.__init__(self, input_port)

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency)

        Node.log_instance(self, {})
Пример #8
0
    def __init__(self, input_port):
        Node.__init__(self, input_port, None)

        # create the plot process
        self.plot_pipe, plotter_pipe = mp.Pipe()
        self.plotter = ProcessGraz()
        self.plot_process = mp.Process(target=self.plotter,
                                       args=(plotter_pipe, ),
                                       daemon=True)
        self.plot_process.start()

        # Log new instance
        Node.log_instance(self, {})
Пример #9
0
    def __init__(self, input_port, file, key):
        Node.__init__(self, input_port, None)
        filename, file_extension = os.path.splitext(file)
        self._file = filename + '.h5'

        self._key = key
        chan = self.input.channels
        pd.DataFrame([] * len(chan), [], chan).to_hdf(self._file,
                                                      key=self._key,
                                                      mode='w',
                                                      format='table')

        Node.log_instance(self, {'file': self._file})
Пример #10
0
    def __init__(self, input_port):
        Node.__init__(self, input_port)

        assert self.input.data_type in ['epoch', 'signal']

        self.output.set_parameters(
            data_type='spectrum',
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency)

        Node.log_instance(self, {})
Пример #11
0
    def __init__(self, input_port, file, sep=';', decimal=','):
        Node.__init__(self, input_port, None)
        filename, file_extension = os.path.splitext(file)
        self._file = filename + '.csv'
        self._sep = sep
        self._decimal = decimal
        self._first_iter = True
        chan = self.input.channels
        pd.DataFrame([] * len(chan), [], chan).to_csv(self._file,
                                                      mode='w',
                                                      sep=self._sep,
                                                      decimal=self._decimal)

        Node.log_instance(self, {'file': self._file})
Пример #12
0
    def __init__(self, input_port, matrix):
        Node.__init__(self, input_port)

        if isinstance(matrix, str):
            filename, file_extension = os.path.splitext(matrix)
            if file_extension == '.yaml':
                logging.debug(f'Got matrix from file {matrix}')
                with open(matrix, 'r') as file:
                    matrix = yaml.load(file, Loader=yaml.FullLoader)
                logging.debug(f'{matrix}')
            elif file_extension in ['.xml', '.cfg']:
                file = minidom.parse(matrix)
                coefs = file.getElementsByTagName(
                    'SettingValue')[0].firstChild.data
                coefs = [float(coef) for coef in coefs.split()]
                output_ch_nb = int(
                    file.getElementsByTagName('SettingValue')
                    [1].firstChild.data)
                input_ch_nb = int(
                    file.getElementsByTagName('SettingValue')
                    [2].firstChild.data)
                assert len(coefs) == input_ch_nb * output_ch_nb
                matrix = {
                    f'Ch{i + 1}':
                    [coefs[i * input_ch_nb + j] for j in range(input_ch_nb)]
                    for i in range(output_ch_nb)
                }

        self._matrix = matrix
        self._channels = [*self._matrix.keys()]

        str_matrix = f''
        for chan in self._channels:
            # verify that the size of _matrix is correct
            assert len(self._matrix[chan]) == len(self.input.channels)
            # convert to float (for format '8e-4')
            self._matrix[chan] = [float(i) for i in self._matrix[chan]]
            str_matrix += f'\n      {chan}: {self._matrix[chan]}'

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self._channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency)

        Node.log_instance(self, {'matrix': str_matrix})
Пример #13
0
    def __init__(self, input_port, file):
        Node.__init__(self, input_port, None)
        filename, file_extension = os.path.splitext(file)
        self._file = filename + '.mat'

        letters = string.ascii_lowercase
        self._key = ''.join(choice(letters) for i in range(3))

        self._save_file = 'temporary_file_' + self._key + '.h5'

        chan = self.input.channels
        pd.DataFrame([] * len(chan), [], chan).to_hdf(self._save_file,
                                                      key=self._key,
                                                      mode='w',
                                                      format='table')

        Node.log_instance(self, {'file': self._file})
Пример #14
0
    def __init__(self, input_port, function):
        Node.__init__(self, input_port)

        assert self.input.data_type in ['epoch', 'signal']

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency)

        self.function = function

        Node.log_instance(self, {
            'function': self.function
        })
Пример #15
0
    def __init__(self, input_port, window):
        Node.__init__(self, input_port)

        assert self.input.data_type == 'epoch'

        assert window in ['blackman', 'hanning', 'hamming', 'triang']
        self._window = window

        self.output.set_parameters(
            data_type='epoch',
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency)

        self._buffered_windows = {}

        Node.log_instance(self, {'window': self._window})
Пример #16
0
def run(pipeline):
    """Function to run pipeline"""
    dir_path = os.path.dirname(os.path.realpath(pipeline))
    sys.path.append(dir_path)

    th = threading.Thread(target=lis)
    th.start()

    exec(open(pipeline).read())

    ports = Port.get_instances()
    logging.debug(f'Name  freq  data_type epochfreq channels')
    for port in ports:
        port.log_parameters()

    logging.info('Run pipeline')

    # run the pipeline
    flag = True
    while flag:
        # logging.debug('New iteration')
        # print('CPU ', py.cpu_percent(interval=.0001), '%')
        # print('RAM ', int(100 * py.memory_percent()) / 100, '%')
        '''
        p = py.parents()
        for p1 in p:
            print(p1.cpu_percent(interval=.001))
            print(p1.memory_percent())'''

        for port in Port.get_instances():
            port.clear()
        for nods in Node.get_instances():
            nods.update()
            nods.update_to_log()
        try:
            if q.get_nowait() == 'end':
                flag = False
        except queue.Empty:
            pass

    print('Stop processing')
    for nods in Node.get_instances():
        nods.terminate()
    exit()
Пример #17
0
    def __init__(self, input_port, marker_input_port):
        Node.__init__(self, input_port)

        self.marker_input = marker_input_port

        self.output.set_parameters(
            data_type='epoch',
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=0)

        # persitent is data of a non-complete epoch
        self.persistent = pd.DataFrame([], [], self.input.channels)
        self.markers_received = pd.DataFrame([], [], self.marker_input.channels)
        # current_name is the name to add for current epoch
        self.current_name = 'first epoch'

        Node.log_instance(self, {'marker port': self.marker_input.id})
Пример #18
0
    def __init__(self, input_port, class_tag=None):
        Node.__init__(self, input_port)
        self._tag = class_tag
        if self._tag:
            self._channels = ['class'] + self.input.channels
        else:
            self._channels = self.input.channels

        self.output.set_parameters(
            data_type='vector',
            channels=self._channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta)

        Node.log_instance(self, {
            'tag': self._tag,
            'coordinates': self._channels
        })
        self._i = 0
Пример #19
0
    def __init__(self, input_port, mode, selected):
        Node.__init__(self, input_port)

        assert mode in ['index', 'name']

        # get channels
        if mode == 'index':
            self._channels = [self.input.channels[i - 1] for i in selected]
        elif mode == 'name':
            self._channels = selected

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self._channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency)

        Node.log_instance(self, {'selected channels': self._channels})
Пример #20
0
    def __init__(self, input_port, downsampling_factor):
        Node.__init__(self, input_port)

        assert self.input.data_type == 'signal'

        self._downsampling_factor = int(downsampling_factor)

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency / self._downsampling_factor,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency
        )

        Node.log_instance(self, {
            'downsampling factor': self._downsampling_factor
        })
        self.persistent = pd.DataFrame([], [], self.input.channels)
Пример #21
0
    def __init__(self,
                 input_port,
                 stat,
                 quantile=None,
                 iqr_quantile=[None, None]):
        Node.__init__(self, input_port)

        assert self.input.data_type == 'epoch'

        assert stat in [
            'mean', 'min', 'max', 'range', 'std', 'median', 'quantile', 'iqr'
        ]
        self._stat = stat

        self.output.set_parameters(
            data_type='signal',
            channels=self.input.channels,
            sampling_frequency=self.input.epoching_frequency,
            meta=self.input.meta)

        # value attribute in initialized at 0
        self.value = np.array([0] * len(self.input.channels))

        if self._stat == 'quantile':
            assert 0 <= quantile and quantile <= 1
            self._q = quantile
            Node.log_instance(
                self, {
                    'output frequency': self.input.epoching_frequency,
                    'stat': self._stat,
                    'quantile': self._q
                })

        elif self._stat == 'iqr':
            q1 = iqr_quantile[0]
            q2 = iqr_quantile[1]
            assert 0 <= q1 and q1 <= q2 and q2 <= 1
            self._q1 = q1
            self._q2 = q2
            Node.log_instance(
                self, {
                    'output frequency': self.input.epoching_frequency,
                    'stat': self._stat,
                    'quantile1': self._q1,
                    'quantile2': self._q2
                })
        else:
            Node.log_instance(
                self, {
                    'output frequency': self.input.epoching_frequency,
                    'stat': self._stat
                })
Пример #22
0
    def __init__(self, input_port, arg1, arg2=4):
        '''Initialize the node before running the pipeline'''

        # create self.input and self.output
        Node.__init__(self, input_port)

        # make sure you get the right input.data_type
        # data_type is either 'epoch', 'signal', 'marker' or 'spectrum'
        assert self.input.data_type in ['epoch', 'signal']

        # update self.output parameters
        self.output.set_parameters(
            # self.input.data_type if the data_type of output is the same as
            # in input or among ['epoch', 'signal', 'marker', 'spectrum']
            data_type=self.input.data_type,
            # self.input.channels if the output channels are the same as
            # in input else list of output channels (['Ch1', 'Ch2'] for example)
            channels=self.input.channels,
            # self.input.sampling_frequency if the output sampling frequency is the same as
            # in input or specify the new output sampling frequency
            sampling_frequency=self.input.sampling_frequency,
            # self.input.meta and/or add every details you want to add in meta
            meta=self.input.meta,
            # if the epoching frequency is unchanged (None or float), specify
            # self.input.epoching_frequency else specify the new epoching frequency
            epoching_frequency=self.input.epoching_frequency)

        # initialize parameters that will be useful for calculation:
        # ex:
        self._arg1 = arg1 / 10  # _arg means a protected arg
        self._channels = self.input.channels

        # log all the parameters you think it is useful to log
        Node.log_instance(
            self,
            {
                # specify the name of the parameter, and its value
                'my arg1': self._arg1
            })
Пример #23
0
    def __init__(self, input_port, mode, ref):
        Node.__init__(self, input_port)

        assert mode in ['index', 'name']

        # get reference channel name
        if mode == 'index':
            self._ref = self.input.channels[ref - 1]
        elif mode == 'name':
            self._ref = ref

        self._channels = self.input.channels.copy()
        self._channels.remove(self._ref)

        self.output.set_parameters(
            data_type=self.input.data_type,
            channels=self._channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=self.input.epoching_frequency)

        Node.log_instance(self, {'reference': self._ref})
Пример #24
0
    def __init__(self, input_port, duration, interval):
        Node.__init__(self, input_port)

        self.duration = duration
        self.interval = interval

        self.output.set_parameters(
            data_type='epoch',
            channels=self.input.channels,
            sampling_frequency=self.input.sampling_frequency,
            meta=self.input.meta,
            epoching_frequency=1 / interval
        )

        self.persistent = pd.DataFrame([], [], self.input.channels)
        self.trigger = None
        self.markers = []

        Node.log_instance(self, {
            'duration': self.duration,
            'interval': self.interval
        })
Пример #25
0
    def __init__(self, input_port, channels='all', way='index'):
        Node.__init__(self, input_port, None)

        assert self.input.data_type == 'spectrum'

        if channels == 'all':
            self._channels = self.input.channels
        else:
            if way == 'index':
                self._channels = [self.input.channels[i - 1] for i in channels]
            elif way == 'name':
                self._channels = channels

        # create the plot process
        self.plot_pipe, plotter_pipe = mp.Pipe()
        self.plotter = ProcessSpectralPlotter(self._channels)
        self.plot_process = mp.Process(target=self.plotter,
                                       args=(plotter_pipe, ),
                                       daemon=True)
        self.plot_process.start()

        # Log new instance
        Node.log_instance(self, {'channels': self._channels})
Пример #26
0
    def __init__(self, file, min_chunk_size=4):
        Node.__init__(self, None)

        filename, self._file_extension = os.path.splitext(file)
        self._events = []
        if self._file_extension in ['.gdf', '.set', '.vhdr']:
            if self._file_extension == '.gdf':
                self._raw = read_raw_gdf(file)
            elif self._file_extension == '.set':
                self._raw = read_raw_eeglab(file)
            elif self._file_extension == '.vhdr':
                self._raw = read_raw_brainvision(file)
            self._sampling_frequency = self._raw.info['sfreq']
            self._channels = self._raw.info.ch_names
            try:
                # to test
                events = find_events(self._raw)
                logging.debug('Get from find_events')
            except ValueError:
                events = events_from_annotations(self._raw)
                logging.debug('Get from events_from_annotations')
            nb_to_event = {events[1][key]: key for key in events[1]}
            for h in events[0]:
                try:
                    value = float(nb_to_event[h[2]])
                except ValueError:
                    value = nb_to_event[h[2]]
                self._events.append((h[0] / 1000, value))
            self._end_record = self._raw.times[-1]
            self._start_record = self._raw.times[0]
        elif self._file_extension == '.xdf':
            streams, header = pyxdf.load_xdf(file,
                                             synchronize_clocks=False,
                                             verbose=False)
            logging.info(f'Found {len(streams)} streams in xdf file')
            for ix, stream in enumerate(streams):
                sampling_frequency = float(stream['info']['nominal_srate'][0])
                if sampling_frequency == 0:
                    logging.debug(f'Get marker from stream {ix}')
                    for timestamp, event in zip(stream['time_stamps'],
                                                stream['time_series']):
                        self._events.append((timestamp, float(event)))
                else:
                    logging.debug(f'Get data from stream {ix}')
                    self._sampling_frequency = sampling_frequency
                    nb_chan = int(stream['info']['channel_count'][0])
                    self._channels = [(stream['info']['desc'][0]['channels'][0]
                                       ['channel'][i]['label'][0])
                                      for i in range(nb_chan)]
                    self._df = pd.DataFrame(stream['time_series'],
                                            stream['time_stamps'],
                                            self._channels)
                    self._start_record = stream['time_stamps'][0]
                    self._end_record = stream['time_stamps'][-1]

        self.marker_output = Port()

        self.marker_output.set_parameters(data_type='marker',
                                          channels=['marker'],
                                          sampling_frequency=0,
                                          meta='')

        self.output.set_parameters(data_type='signal',
                                   channels=self._channels,
                                   sampling_frequency=self._sampling_frequency,
                                   meta='')

        Node.log_instance(
            self, {
                'marquers output': self.marker_output.id,
                'sampling frequency': self._sampling_frequency,
                'channels': self._channels,
                'min chunk size': min_chunk_size,
                'from file': file
            })

        self._last_t = None
        self._min_period = min_chunk_size / self._sampling_frequency
        self._start_time = None
        self._flag = True