{ "cells": [ { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "from pyedflib import highlevel\n", "from portiloop.src.demo.utils import xdf2array\n", "import numpy as np\n", "\n", "filename_edf = '/home/ubuntu/portiloop-software/BSP_L22_Portiloop_EDF.edf'\n", "filename_xdf = '/home/ubuntu/portiloop-software/BSP_L22_Portiloop_XDF.xdf'" ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1147000,)" ] }, "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ "edf_read = highlevel.read_edf(filename_edf)\n", "signal_edf = edf_read[0][1, :]\n", "signal_edf.shape" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "data_whole, columns = xdf2array(filename_xdf, 2)" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1142166,)" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "signal_xdf = data_whole[:, columns.index(\"online_filtered_signal_portiloop\")]\n", "signal_xdf.shape" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4834" ] }, "execution_count": 101, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(signal_edf) - len(signal_xdf)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.isin(signal_edf, signal_xdf).sum()" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.020828564888990615 != -0.3275071084499359\n" ] } ], "source": [ "print(f\"{signal_edf[10000]} != {signal_xdf[10000]}\")" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(array([], dtype=int64),)" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.where(signal_edf == signal_xdf[100000])" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "# signal_xdf = np.concatenate([signal_xdf, np.zeros(len(signal_edf) - len(signal_xdf))])" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [], "source": [ "# signals = np.concatenate((np.expand_dims(signal_edf, 0), np.expand_dims(signal_xdf, 0)), axis = 0)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(2, 1147000)" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# signals.shape" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# # Create and edf file with both signals:\n", "# channel_names = ['EDF_Data', \"XDF_Data\"]\n", "# signal_headers = highlevel.make_signal_headers(channel_names, sample_frequency=250)\n", "# headers = highlevel.make_header(patientname='L22', gender='Male')\n", "\n", "\n", "# highlevel.write_edf('edf_file.edf', signals, signal_headers, headers)" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [], "source": [ "from portiloop.src.demo.utils import OfflineSleepSpindleRealTimeStimulator\n", "from portiloop.src.detection import SleepSpindleRealTimeDetector\n", "from portiloop.src.processing import FilterPipeline\n", "\n", "\n", "filter = FilterPipeline(nb_channels=1, sampling_rate=250)\n", "detector = SleepSpindleRealTimeDetector(threshold=0.82, channel=1) # always 1 because we have only one channel\n", "stimulator = OfflineSleepSpindleRealTimeStimulator()" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running online filtering and detection...\n" ] } ], "source": [ "print(\"Running online filtering and detection...\")\n", "\n", "points = []\n", "online_activations = []\n", "delayed_stims = []\n", "\n", "# Go through the data\n", "for index, point in enumerate(signal_xdf):\n", " # Filter the data\n", " filtered_point = filter.filter(np.array([point]))\n", "\n", " filtered_point = filtered_point.tolist()\n", " points.append(filtered_point[0])\n", " # Detect the spindles\n", " result = detector.detect([[point]])\n", "\n", " # if stimulation_phase != \"Fast\":\n", " # delayed_stim = stimulation_delayer.step_timesteps(filtered_point[0])\n", " # if delayed_stim:\n", " # delayed_stims.append(1)\n", " # else:\n", " # delayed_stims.append(0)\n", "\n", " # Stimulate if necessary\n", " stim = stimulator.stimulate(result)\n", " if stim:\n", " online_activations.append(1)\n", " else:\n", " online_activations.append(0)" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1147000" ] }, "execution_count": 144, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(online_activations)" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "31" ] }, "execution_count": 141, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(online_activations)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "dd88f1663b1efd7dd128096061ae4c3f92be53565689be8013239d96443491e7" } } }, "nbformat": 4, "nbformat_minor": 2 }