Skip to content
Snippets Groups Projects
Commit a02f53e2 authored by Ryan Godwin's avatar Ryan Godwin
Browse files

ECG Preprocessing is working correctly now... using neurokit2

parent 4f59bb0d
No related branches found
No related tags found
1 merge request!1Progress toward getting the evaluation integrated
......@@ -24,3 +24,4 @@ mne-icalabel
scipy
tensorflow
tensorflow-addons
neurokit2
\ No newline at end of file
......@@ -175,11 +175,11 @@ def preprocess_ecgs(
for file in ecg_to_proc:
fp = Path(file)
output_folder = Path(config.INTERMEDIATE_DIR, fp.parent.name)
output_file = Path(output_folder, "clean_ecg.csv")
clean_output_file = Path(output_folder, "clean_ecg.csv")
Path(output_folder).mkdir(parents=True, exist_ok=True)
data_to_write= preprocess_ecg.preproc_ecg(fp, fs, args["downsample_freq"],output_folder)
np.savetxt(output_file, data_to_write, delimiter=',')
data_to_write = preprocess_ecg.preproc_ecg(fp, fs, args["downsample_freq"],output_folder)
np.savetxt(clean_output_file, data_to_write, delimiter=',')
@app.command()
......
from pathlib import Path
import neurokit2 as nk
import matplotlib.pyplot as plt
import mpld3
def make_html(fig):
def make_html(fig, output_file):
# save figure as HTML file
html_str = mpld3.fig_to_html(fig)
output_html = self._output_file.rename(self._output_file.with_suffix(".html"))
output_html = Path(output_file).rename(output_file.with_suffix(".html"))
HTML_file = open(output_html, "w", encoding="utf8")
HTML_file.write(html_str)
HTML_file.close()
def plot_signals_comp(meg_values, ecg_values, output_file):
fig, ax = plt.subplot(211)
......@@ -21,9 +20,7 @@ def plot_signals_comp(meg_values, ecg_values, output_file):
ax.ylabel("Cardiac Component")
ax.title("Selected Cardiac Component")
fig.savefig(Path(output_file), dpi=330)
# output_html = super().make_html(plt.gcf())
fig.subplot(212)
print('Plotting ECG...')
fig.plot(ecg_values.iloc[:,0],ecg_values.iloc[:,1], linewidth=0.2)
......@@ -33,18 +30,22 @@ def plot_signals_comp(meg_values, ecg_values, output_file):
fig.savefig(str(output_file), dpi=330)
print("outfile - ", Path(output_file))
make_html(fig)
#make_html(fig, output_file)
plt.close()
def plot_ecg(ecg_values, output_file):
plt.close()
print('Plotting ECG...')
plt.plot(ecg_values.iloc[:,0],ecg_values.iloc[:,1], linewidth=0.2)
plt.plot(ecg_values[15000:25000], linewidth=1)
plt.xlabel("Time (s)")
plt.ylabel("ECG Signal")
plt.title("ECG Signal")
plt.legend(['Raw', 'Cleaned'])
plt.title("Cleaning ECG Signal")
plt.savefig(str(output_file), dpi=330)
print("outfile - ", Path(output_file))
make_html(plt.gcf())
plt.close()
\ No newline at end of file
#make_html(plt.gcf(), output_file)
plt.close()
def nk_plotter(signals, output_file):
nk.plot_ecgs(signals)
\ No newline at end of file
......@@ -4,40 +4,32 @@ from pathlib import Path
from sklearn.preprocessing import StandardScaler
import pandas as pd
import numpy as np
from py_utils import baseline_wander_removal
import neurokit2 as nk
import plotter
def nk_preproc(ecg_signal, fs):
# Do processing
ecg_cleaned = nk.ecg_clean(ecg_signal, sampling_rate=fs, method="biosppy")
# Prepare output
signals = pd.DataFrame({"ECG_Raw": ecg_signal,
"ECG_Clean": ecg_cleaned
})
return signals
def preproc_ecg(file_path, fs, downsample_freq, output_folder):
ecg_data = pd.read_csv(file_path)
#Determing the time domain of the raw signal from the length of data and sampling freq
time_s = np.arange(0, len(ecg_data)/fs, 1/fs)
signals = nk_preproc(ecg_data.iloc[:,0], fs)
for signal in signals:
downsamp_signals = nk.signal_resample(signals[signal],sampling_rate = fs,
desired_sampling_rate=downsample_freq
)
#spot checking
output_file = Path(output_folder, 'raw_data.png')
plotter.plot_ecg(ecg_data,output_file)
#Basline Drift Removal
baseline, ecg_out = baseline_wander_removal.bwr(ecg_data)
#spot check 2
output_file = Path(output_folder, 'bwr_data.png')
plotter.plot_ecg(ecg_out,output_file)
#Perfrom a standard scaling of the data (x-mu/sigma)
scaler = StandardScaler()
scaled_ecg = scaler.fit_transform(ecg_out)
#spot check 3
output_file = Path(output_folder, 'scaled_data.png')
plotter.plot_ecg(ecg_data,scaled_ecg)
#Need to downsample to match the MEG data
secs = len(scaled_ecg)/fs
sampls = secs*downsample_freq
print('int - ', int(sampls))
scaled_down_ecg = signal.resample(scaled_ecg, int(sampls), t = time_s)
#spot check 4
output_file = Path(output_folder, 'scaled_downsampled_data.png')
plotter.plot_ecg(ecg_data,scaled_down_ecg)
#Combining time data with signal data
print(scaled_down_ecg[0])
scaled_down_ecg_wtime = np.insert(scaled_down_ecg[0], 0, scaled_down_ecg[1], axis=1)
output_file = Path(output_folder, 'ecg_cleaned_data_part_biosppy.png')
plotter.plot_ecg(signals,output_file)
return downsamp_signals
return scaled_down_ecg_wtime
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment