-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from the-virtual-brain/WID-69
WID-69. Add support for EDF files
- Loading branch information
Showing
7 changed files
with
4,501 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "bae5e88c-1828-4349-b67a-01082e41aaf7", | ||
"metadata": {}, | ||
"source": [ | ||
"## EDF TimeSeries Widget" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "866da714-9a4c-4242-b469-8643ba5a8573", | ||
"metadata": {}, | ||
"source": [ | ||
"---" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "a28b8eb4-4709-4056-9e6e-4530a3735011", | ||
"metadata": {}, | ||
"source": [ | ||
"### This notebook is dedicated to showcasing the TimeSeries widget using data from an EDF file" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "56d5c4b6-7804-46d6-931f-fcc5593977d1", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"%matplotlib widget" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "28420169-2274-4e0e-a73d-c1ab54e6da9c", | ||
"metadata": { | ||
"scrolled": true, | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"17-04-2024 05:02:37 - DEBUG - tvbwidgets - Package is not fully installed\n", | ||
"17-04-2024 05:02:37 - DEBUG - tvbwidgets - Version read from the internal package.json file\n", | ||
"17-04-2024 05:02:37 - INFO - tvbwidgets - Version: 2.0.0\n", | ||
"Using matplotlib as 2D backend.\n", | ||
" INFO Cannot import syncrypto library.\n", | ||
"17-04-2024 05:02:49 - INFO - tvbwidgets.core.pse.parameters - ImportError: Dask dependency is not included, so this functionality won't be available\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import numpy as np\n", | ||
"from tvbwidgets.api import plot_timeseries\n", | ||
"from tvbwidgets.readers import read_edf_file" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "59594729-3afd-4537-87c2-7413dcb66606", | ||
"metadata": {}, | ||
"source": [ | ||
"#### Reading the data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "94687d23-0222-4e9f-887e-e97169737304", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Extracting EDF parameters from C:\\Work\\TVB\\tvb-widgets\\tvbwidgets\\tests\\data\\test_file.edf...\n", | ||
"EDF file detected\n", | ||
"Setting channel info structure...\n", | ||
"Creating raw.info structure...\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"edf_file_path = '../tvbwidgets/tests/data/test_file.edf' # replace path with your actual EDF file path\n", | ||
"data, freq, index = read_edf_file(edf_file_path)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "12da93b3-f96f-44cf-8555-52eee8aaecfd", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"(92000, 32)\n", | ||
"400.0\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"print(data.shape)\n", | ||
"print(freq)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "94d8b0ec-654f-47a7-a0c8-ce3b28651e22", | ||
"metadata": {}, | ||
"source": [ | ||
"#### TS Viewer" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "cac83fe3-b4fe-486a-8955-70203dda1004", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"backend = 'plotly' # change to 'matplotlib' to see the other TS widget" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "20c3eae2-49dd-4d01-af9a-06a0dba15888", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"17-04-2024 05:02:49 - INFO - tvbwidgets.ui.ts.plotly_ts_widget - TimeSeries Widget with Plotly initialized\n", | ||
"Creating RawArray with float64 data, n_channels=32, n_times=92000\n", | ||
" Range : 0 ... 91999 = 0.000 ... 229.998 secs\n", | ||
"Ready.\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"application/vnd.jupyter.widget-view+json": { | ||
"model_id": "28d8ffb85be94f28b59e85b521e4aeeb", | ||
"version_major": 2, | ||
"version_minor": 0 | ||
}, | ||
"text/plain": [ | ||
"TimeSeriesWidgetPlotly(children=(HBox(children=(Output(),)), VBox(children=(Dropdown(description='Colormap:', …" | ||
] | ||
}, | ||
"metadata": {}, | ||
"output_type": "display_data" | ||
} | ||
], | ||
"source": [ | ||
"tsw = plot_timeseries(data=data, sample_freq=freq, ch_idx=index, backend=backend)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "83da35ea-9c05-4f4b-90e8-93ee7002554c", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"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.9.18" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# "TheVirtualBrain - Widgets" package | ||
# | ||
# (c) 2022-2024, TVB Widgets Team | ||
# | ||
|
||
import mne | ||
import numpy as np | ||
|
||
|
||
def read_edf_file(filepath): | ||
# type: (str) -> (numpy.ndarray, float, int) | ||
""" | ||
Reads an EDF file located at filepath and returns the data array, the sample frequency and the channel index, | ||
all necessary for the `api.plot_timeseries` function | ||
""" | ||
raw = mne.io.read_raw_edf(filepath) | ||
data, _ = raw[:] | ||
data = np.transpose(data) | ||
ch_idx = len(data.shape) - 1 | ||
sample_freq = raw.info['sfreq'] | ||
|
||
return data, sample_freq, ch_idx |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# "TheVirtualBrain - Widgets" package | ||
# | ||
# (c) 2022-2024, TVB Widgets Team | ||
# | ||
|
||
import numpy as np | ||
import os | ||
import tvbwidgets.readers as readers | ||
|
||
TEST_DATA = os.path.join(os.path.dirname(__file__), 'data') | ||
|
||
|
||
def test_read_edf_file(): | ||
edf_test_file = 'test_file.edf' | ||
edf_path = os.path.join(TEST_DATA, edf_test_file) | ||
|
||
data, freq, idx = readers.read_edf_file(edf_path) | ||
assert data is not None | ||
assert freq is not None | ||
assert idx is not None | ||
|
||
assert isinstance(data, np.ndarray) | ||
assert data.shape == (92000, 32) | ||
|
||
assert isinstance(freq, float) | ||
assert freq == 400 | ||
|
||
assert isinstance(idx, int) | ||
assert idx == 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters