Signal Quality Indices

Amplitude Variability SQI

# Configure Plotly for ReadTheDocs
import plotly.io as pio
pio.renderers.default = "sphinx_gallery"
from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
import plotly.io as pio
pio.renderers.default = "sphinx_gallery"
# pio.renderers.default = "plotly_mimetype"  # or "plotly_mimetype"
# from IPython.display import display, HTML
# display(HTML('<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>'))
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace

fs = 128
duration = 60
step_size = fs * 20
signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.amplitude_variability_sqi(
    window_size=fs * duration, step_size=step_size, threshold=2,threshold_type='above', aggregate=False
)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[2], line 10
      8 from plotly import graph_objects as go
      9 import os
---> 10 from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
     12 fs = 128
     13 duration = 60

File ~/checkouts/readthedocs.org/user_builds/vital-dsp/envs/stable/lib/python3.9/site-packages/vitalDSP/notebooks/__init__.py:27
     25 import ast
     26 import datetime as dt
---> 27 import pkg_resources
     30 # Init functions to handle the sample data
     31 def load_sample_ecg():
     32     # Get the path to the file in the package resources

ModuleNotFoundError: No module named 'pkg_resources'
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Baseline Wander SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.baseline_wander_sqi(
    window_size=fs * duration, step_size=step_size, threshold=2,threshold_type='above'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Zero-Crossing SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20
signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.zero_crossing_sqi(
    window_size=fs * duration, step_size=step_size, threshold=2,threshold_type='above'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Entropy SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.signal_entropy_sqi(
    window_size=fs * duration, step_size=step_size, threshold=-2,threshold_type='below'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Skewness

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.skewness_sqi(
    window_size=fs * duration, step_size=step_size, threshold=-2,threshold_type='below'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)
fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Kurtosis SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.kurtosis_sqi(
    window_size=fs * duration, step_size=step_size, threshold=2,threshold_type='above'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))

# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Peak-to-Peak Amplitude SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.peak_to_peak_amplitude_sqi(
    window_size=fs * duration, step_size=step_size, threshold=2,threshold_type='above'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Signal-to-Noise Ratio (SNR) SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.snr_sqi(
    window_size=fs * duration, step_size=step_size, threshold=-1,threshold_type='below'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Energy SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.energy_sqi(
    window_size=fs * duration, step_size=step_size, threshold=2,threshold_type='above'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()

Waveform Similarity SQI

from vitalDSP.signal_quality_assessment.signal_quality_index import SignalQualityIndex
import numpy as np
from vitalDSP.utils.data_processing.synthesize_data import generate_ecg_signal
from plotly import graph_objects as go
import os
from vitalDSP.notebooks import load_sample_ecg_small, plot_trace
pio.renderers.default = "sphinx_gallery"

fs = 128
duration = 60
step_size = fs * 20

signal_col, date_col = load_sample_ecg_small()
signal_col = np.array(signal_col)

offset = 1000
target = generate_ecg_signal(sfecg=fs, duration=duration, 
                            Anoise=0.01, hrmean=70, sfint=512)
reference_signal = target[offset:(fs*duration+offset)]

sqi = SignalQualityIndex(signal_col)
sqi_values, normal_segments, abnormal_segments = sqi.waveform_similarity_sqi(
    window_size=fs * duration, step_size=step_size, 
    reference_waveform=reference_signal,
    threshold=-2,threshold_type='below'
, aggregate=False)

# Prepare the normal and abnormal segments along with their corresponding date_col
date_col_normal = []
date_col_abnormal = []
normal_signal = []
abnormal_signal = []

for start, end in normal_segments:
    date_col_normal.extend(date_col[start:end])
    normal_signal.extend(signal_col[start:end])

for start, end in abnormal_segments:
    date_col_abnormal.extend(date_col[start:end])
    abnormal_signal.extend(signal_col[start:end])

# Convert lists to numpy arrays
normal_signal = np.array(normal_signal)
abnormal_signal = np.array(abnormal_signal)
date_col_normal = np.array(date_col_normal)
date_col_abnormal = np.array(date_col_abnormal)

# Plot the original signal and segments
fig = go.Figure()
fig.add_trace(go.Scatter(x=date_col, y=signal_col, mode='lines', name='Original Signal'))

fig.add_trace(go.Scatter(x=date_col_normal, y=normal_signal,
                        mode='lines', line=dict(color='green', width=2),
                        name='Normal Segments'))

fig.add_trace(go.Scatter(x=date_col_abnormal, y=abnormal_signal,
                        mode='lines', line=dict(color='red', width=2),
                        name='Abnormal Segments'))


# Update layout
fig.update_layout(
    title="Signal with Combined Normal and Abnormal Segments",
    xaxis_title="Time",
    yaxis_title="Amplitude",
    legend_title="Segments"
)

fig.show()
# Plot the histogram of SQI values
# Plot the histogram of SQI values using Plotly
fig_hist = go.Figure()
fig_hist.add_trace(go.Histogram(
    x=sqi_values,
    nbinsx=20,
    marker_color="blue",
    marker_line=dict(color="black", width=1),
    name="SQI Values"
))

fig_hist.update_layout(
    title="Histogram of SQI Values",
    xaxis_title="SQI Value",
    yaxis_title="Frequency",
    showlegend=False
)

fig_hist.show()