From b770784cace057148b1ef89a7de5b78d65fd8842 Mon Sep 17 00:00:00 2001 From: timonmerk Date: Sun, 22 Sep 2024 14:52:38 +0200 Subject: [PATCH] async run function gets now called but websocket connection needs to be setup with frontend. --- py_neuromodulation/gui/backend/app_backend.py | 2 + py_neuromodulation/gui/backend/app_pynm.py | 4 +- py_neuromodulation/gui/backend/app_socket.py | 3 +- py_neuromodulation/stream/stream.py | 2 + start_LSL_stream.py | 71 +++++++++++-------- 5 files changed, 50 insertions(+), 32 deletions(-) diff --git a/py_neuromodulation/gui/backend/app_backend.py b/py_neuromodulation/gui/backend/app_backend.py index b173ced7..a265ba59 100644 --- a/py_neuromodulation/gui/backend/app_backend.py +++ b/py_neuromodulation/gui/backend/app_backend.py @@ -117,6 +117,8 @@ async def handle_stream_control(data: dict): action = data["action"] if action == "start": # TODO: create out_dir and experiment_name text filds in frontend + self.logger.info("websocket:") + self.logger.info(self.websocket_manager_features) await self.pynm_state.start_run_function( out_dir=data["out_dir"], experiment_name=data["experiment_name"], diff --git a/py_neuromodulation/gui/backend/app_pynm.py b/py_neuromodulation/gui/backend/app_pynm.py index 282f3252..f60c51c0 100644 --- a/py_neuromodulation/gui/backend/app_pynm.py +++ b/py_neuromodulation/gui/backend/app_pynm.py @@ -1,3 +1,4 @@ +import asyncio import logging import numpy as np from multiprocessing import Process, Queue @@ -49,7 +50,7 @@ async def start_run_function( # else "", # }, # ) - + #asyncio.run( await self.stream.run( out_dir=out_dir, experiment_name=experiment_name, @@ -138,6 +139,7 @@ def setup_offline_stream( bads=bads, reference=None, used_types=["eeg", "ecog", "dbs", "seeg"], + target_keywords=None, ) self.settings: NMSettings = NMSettings( diff --git a/py_neuromodulation/gui/backend/app_socket.py b/py_neuromodulation/gui/backend/app_socket.py index d964f73a..b9e8d861 100644 --- a/py_neuromodulation/gui/backend/app_socket.py +++ b/py_neuromodulation/gui/backend/app_socket.py @@ -58,13 +58,14 @@ async def send_bytes(self, header: dict, payload: bytes | None = None): await connection.send_bytes(message) async def send_message(self, message: str | dict): + self.logger.info(f"Sending message within app_socket: {message.keys()}") if self.active_connections: for connection in self.active_connections: if type(message) is dict: await connection.send_json(json.dump(message)) elif type(message) is str: await connection.send_text(message) - self.logger.info(f"Message sent: {message}") + self.logger.info(f"Message sent") else: self.logger.warning("No active connection to send message.") diff --git a/py_neuromodulation/stream/stream.py b/py_neuromodulation/stream/stream.py index 2f22aac4..fd2813fa 100644 --- a/py_neuromodulation/stream/stream.py +++ b/py_neuromodulation/stream/stream.py @@ -330,6 +330,8 @@ async def run( # self.feature_queue.put(feature_dict) if websocket_featues is not None: + nm.logger.info("Sending message to Websocket") + #nm.logger.info(feature_dict) await websocket_featues.send_message(feature_dict) self.batch_count += 1 if self.batch_count % self.save_interval == 0: diff --git a/start_LSL_stream.py b/start_LSL_stream.py index 1024d2b1..fbd6ed85 100644 --- a/start_LSL_stream.py +++ b/start_LSL_stream.py @@ -32,36 +32,47 @@ ) = io.read_BIDS_data(PATH_RUN=PATH_RUN) if __name__ == "__main__": - PATH_VHDR = "/Users/Timon/Documents/py-neurmodulation_merge/py_neuromodulation/py_neuromodulation/data/sub-testsub/ses-EphysMedOff/ieeg/sub-testsub_ses-EphysMedOff_task-gripforce_run-0_ieeg.vhdr" - # data, sfreq, ch_names, ch_types, bads = io.read_mne_data(PATH_VHDR) - ( - raw_arr, - data, - sfreq, - line_noise, - coord_list, - coord_names, - ) = io.read_BIDS_data(PATH_RUN=PATH_RUN) - - channels = set_channels( - ch_names=raw_arr.ch_names, - ch_types=raw_arr.get_channel_types(), - bads=None, - reference=None, - used_types=["eeg", "ecog", "dbs", "seeg"], - ) - - settings = nm.NMSettings.get_fast_compute() - - stream = nm.Stream( - settings=settings, - channels=channels, - verbose=True, - sfreq=sfreq, - line_noise=50, - ) - - features = asyncio.run(stream.run(data, save_csv=True)) + # PATH_VHDR = "/Users/Timon/Documents/py-neurmodulation_merge/py_neuromodulation/py_neuromodulation/data/sub-testsub/ses-EphysMedOff/ieeg/sub-testsub_ses-EphysMedOff_task-gripforce_run-0_ieeg.vhdr" + + # data, sfreq, ch_names, ch_types, bads = io.read_mne_data(PATH_VHDR) + + # channels = set_channels( + # ch_names=ch_names, + # ch_types=ch_types, + # bads=bads, + # reference=None, + # used_types=["eeg", "ecog", "dbs", "seeg"], + # target_keywords=None, + # ) + + # ( + # raw_arr, + # data, + # sfreq, + # line_noise, + # coord_list, + # coord_names, + # ) = io.read_BIDS_data(PATH_RUN=PATH_RUN) + + # channels = set_channels( + # ch_names=raw_arr.ch_names, + # ch_types=raw_arr.get_channel_types(), + # bads=None, + # reference=None, + # used_types=["eeg", "ecog", "dbs", "seeg"], + # ) + + # settings = nm.NMSettings.get_fast_compute() + + # stream = nm.Stream( + # settings=settings, + # channels=channels, + # verbose=True, + # sfreq=sfreq, + # line_noise=50, + # ) + + # features = asyncio.run(stream.run(data, save_csv=True)) player = LSLOfflinePlayer(raw=raw, stream_name="example_stream")