Skip to content

Commit

Permalink
async run function gets now called but websocket connection needs to …
Browse files Browse the repository at this point in the history
…be setup with frontend.
  • Loading branch information
timonmerk committed Sep 22, 2024
1 parent 1296a81 commit b770784
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
2 changes: 2 additions & 0 deletions py_neuromodulation/gui/backend/app_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
4 changes: 3 additions & 1 deletion py_neuromodulation/gui/backend/app_pynm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
import numpy as np
from multiprocessing import Process, Queue
Expand Down Expand Up @@ -49,7 +50,7 @@ async def start_run_function(
# else "",
# },
# )

#asyncio.run(
await self.stream.run(
out_dir=out_dir,
experiment_name=experiment_name,
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion py_neuromodulation/gui/backend/app_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand Down
2 changes: 2 additions & 0 deletions py_neuromodulation/stream/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
71 changes: 41 additions & 30 deletions start_LSL_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit b770784

Please sign in to comment.