Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
HRV_MEG
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Ryan Christopher Godwin
HRV_MEG
Commits
a02f53e2
Commit
a02f53e2
authored
2 years ago
by
Ryan Godwin
Browse files
Options
Downloads
Patches
Plain Diff
ECG Preprocessing is working correctly now... using neurokit2
parent
4f59bb0d
No related branches found
Branches containing commit
No related tags found
1 merge request
!1
Progress toward getting the evaluation integrated
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
docs/dev_requirements.txt
+1
-0
1 addition, 0 deletions
docs/dev_requirements.txt
src/hrvmeg/main.py
+4
-4
4 additions, 4 deletions
src/hrvmeg/main.py
src/hrvmeg/plotter.py
+15
-14
15 additions, 14 deletions
src/hrvmeg/plotter.py
src/hrvmeg/preprocess_ecg.py
+22
-30
22 additions, 30 deletions
src/hrvmeg/preprocess_ecg.py
with
42 additions
and
48 deletions
docs/dev_requirements.txt
+
1
−
0
View file @
a02f53e2
...
...
@@ -24,3 +24,4 @@ mne-icalabel
scipy
tensorflow
tensorflow-addons
neurokit2
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/hrvmeg/main.py
+
4
−
4
View file @
a02f53e2
...
...
@@ -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
()
...
...
This diff is collapsed.
Click to expand it.
src/hrvmeg/plotter.py
+
15
−
14
View file @
a02f53e2
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
This diff is collapsed.
Click to expand it.
src/hrvmeg/preprocess_ecg.py
+
22
−
30
View file @
a02f53e2
...
...
@@ -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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment