|
| 1 | +# Author: Nicolas Legrand <nicolas.legrand@cfin.au.dk> |
| 2 | + |
| 3 | +from typing import List, Union |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import pandas as pd |
| 7 | +from bokeh.layouts import column |
| 8 | +from bokeh.models import ColumnDataSource, Panel, RangeTool, Tabs |
| 9 | +from bokeh.plotting import figure |
| 10 | + |
| 11 | + |
| 12 | +def plot_raw( |
| 13 | + signal: Union[np.array, List[np.array]], sfreq: int = 1000, figsize: int = 400 |
| 14 | +) -> Tabs: |
| 15 | + """Plot raw EGG recording. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + signal : list or np.ndarray |
| 20 | + The EGG signal as a Numpy array. If a list is provided, it should |
| 21 | + contain *n* Numpy array of equal length. |
| 22 | + sfreq : int |
| 23 | + Signal sampling frequency. Default is set to 1000 Hz. |
| 24 | + figsize : int |
| 25 | + Figure height. Default is `300`. |
| 26 | +
|
| 27 | + Returns |
| 28 | + ------- |
| 29 | + fig : :class:`bokeh.models.Tabs` |
| 30 | + The bokeh figure containing the plot(s). |
| 31 | +
|
| 32 | + """ |
| 33 | + |
| 34 | + if isinstance(signal, list): |
| 35 | + for i, sig in enumerate(signal): |
| 36 | + if i == 0: |
| 37 | + # Create the time vector |
| 38 | + time = pd.to_datetime( |
| 39 | + np.arange(0, len(sig)) / sfreq, unit="s", origin="unix" |
| 40 | + ) |
| 41 | + data = {"time": time} |
| 42 | + data[f"EGG_{i+1}"] = sig |
| 43 | + elif isinstance(signal, np.ndarray): |
| 44 | + # Create the time vector |
| 45 | + time = pd.to_datetime( |
| 46 | + np.arange(0, len(signal)) / sfreq, unit="s", origin="unix" |
| 47 | + ) |
| 48 | + data = {"time": time} |
| 49 | + data["EGG_1"] = signal |
| 50 | + else: |
| 51 | + raise ValueError( |
| 52 | + "Invalid data format provided. Should be list or 1d Numpy array" |
| 53 | + ) |
| 54 | + |
| 55 | + source = ColumnDataSource(data=data) |
| 56 | + tabs = [] |
| 57 | + for i in range(1, len(data)): |
| 58 | + raw = figure( |
| 59 | + title="Raw data", |
| 60 | + x_axis_type="datetime", |
| 61 | + sizing_mode="stretch_width", |
| 62 | + plot_height=figsize, |
| 63 | + x_axis_label="Time", |
| 64 | + y_axis_label="EGG", |
| 65 | + output_backend="webgl", |
| 66 | + x_range=(time[0], time[-1]), |
| 67 | + ) |
| 68 | + |
| 69 | + raw.line("time", f"EGG_{i}", source=source) |
| 70 | + |
| 71 | + select = figure( |
| 72 | + title="Drag the middle and edges of the selection box to change the range above", |
| 73 | + plot_height=130, |
| 74 | + plot_width=800, |
| 75 | + y_range=raw.y_range, |
| 76 | + x_axis_type="datetime", |
| 77 | + y_axis_type=None, |
| 78 | + tools="", |
| 79 | + toolbar_location=None, |
| 80 | + background_fill_color="#efefef", |
| 81 | + ) |
| 82 | + range_tool = RangeTool(x_range=raw.x_range) |
| 83 | + range_tool.overlay.fill_color = "navy" |
| 84 | + range_tool.overlay.fill_alpha = 0.2 |
| 85 | + |
| 86 | + select.line("time", f"EGG_{i}", source=source) |
| 87 | + select.ygrid.grid_line_color = None |
| 88 | + select.add_tools(range_tool) |
| 89 | + select.toolbar.active_multi = range_tool |
| 90 | + |
| 91 | + tabs.append( |
| 92 | + Panel( |
| 93 | + child=column(*(raw, select), sizing_mode="stretch_width"), |
| 94 | + title=f"EGG_{i}", |
| 95 | + ) |
| 96 | + ) |
| 97 | + |
| 98 | + return Tabs(tabs=tabs) |
0 commit comments