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

Progress toward getting the evaluation integrated

This will compare the MEG and ECG directly
parent 89e76d93
No related branches found
No related tags found
1 merge request!1Progress toward getting the evaluation integrated
......@@ -3,10 +3,11 @@ import mpld3
import numpy as np
import pandas as pd
import scipy.signal as signal
from data import GetRPeaks
class CompareMEGICAtoECG:
def __init__(self, output_sub_dir, subject_id, subject_path, ICA_value):
def __init__(self, output_sub_dir, subject_id, subject_path):
"""
The function takes in the subject_id, subject_path, output_sub_dir, and ICA_value and assigns
them to the class.
......@@ -20,130 +21,9 @@ class CompareMEGICAtoECG:
self.subject_path = subject_path
self.subject_id = subject_id
self.output_sub_dir = output_sub_dir
self.ICA_value = ICA_value
def get_ICA_RR_intervals(self):
"""
The function takes in a .CSV file with two columns: time and a component value. It then uses
scipy's peak detection function to find peaks in the component value. It then plots the peaks
and saves the plot as a .HTML file
"""
#############################################################
df = pd.DataFrame()
# get time and component from .CSV file
arr_Time_Series = pd.read_csv(self.subject_path).iloc[:, 0].T.to_numpy()
component_value = pd.read_csv(self.subject_path).iloc[:, 1].T.to_numpy()
df["Time"] = arr_Time_Series
df[f"ICA_Values_{self.ICA_value}"] = component_value
hr_arr = df.to_numpy()
#############################################################
# Assign paths
stats_component = f"{self.output_sub_dir}/{self.subject_id}_HR_component.csv"
graph_component = f"{self.output_sub_dir}/{self.subject_id}_Plot_{self.ICA_value}.png"
#############################################################
component_time = hr_arr[:, 0]
ICA_component_value = hr_arr[:, 1]
# attempt to flip values if signal is inverted
if np.median(ICA_component_value) > 0.00:
ICA_component_value = -ICA_component_value
df[f"ICA_Values_{self.ICA_value}"] = ICA_component_value
# SCIPY PEAK DETECTION
###################################################################################################
distance = 150
# filter height to peaks within the top 5% of largest values
num_to_filter_component = round(len(ICA_component_value) * 0.045)
height_component = (
sum(sorted(ICA_component_value)[-num_to_filter_component:])
) / num_to_filter_component
# Load sample ECG signal & extract R-peaks
component_rpeaks, _ = signal.find_peaks(
ICA_component_value, height=height_component, distance=distance, prominence=3.5
)
# PYHRV PEAK DETECTION
########################################################################################################
# t_component, filtered_signal, component_rpeaks = biosppy.signals.ecg.ecg(ICA_component_value, sampling_rate=500)[:3]
# Plots Component Peaks
################################################################################################
length_of_component_time_series = np.amax(component_time)
to_divide = 1
mini2 = 0
maxi2 = len(component_time) / to_divide
peaks_range = [
idx for idx in range(len(component_rpeaks)) if mini2 < component_rpeaks[idx] < maxi2
]
peaks1 = component_rpeaks[peaks_range]
fig, ax = plt.subplots(1, 1, figsize=(12, 5))
plt.plot(component_time, ICA_component_value, linewidth=0.2)
plt.plot(component_time[component_rpeaks], ICA_component_value[component_rpeaks], "x")
plt.xlim([10, 480 // to_divide])
fig = plt.gcf()
fig.set_size_inches(12, 5)
beat_cnt = len(peaks1)
avg_hr = round((beat_cnt / (length_of_component_time_series)) * 60, 2)
plt.title(
f"{self.subject_id} RR Interval Component {self.ICA_value} \nNumber of Beats: {beat_cnt} || Average Heart Rate: {avg_hr}"
)
plt.savefig(graph_component, dpi=100)
# save figure as HTML file
html_str = mpld3.fig_to_html(fig)
Html_file = open(graph_component[:-4] + ".html", "w")
Html_file.write(html_str)
Html_file.close()
# Writes Component RR Intervals to CSV
################################################################################################################
# peak_vals = component_time[component_rpeaks]
# rr_int = []
# size = len(peak_vals) - 1
# for i in range(1, size):
# print(peak_vals[i])
# delta = peak_vals[i] - peak_vals[i - 1]
# rr_int = np.append(rr_int, delta)
# df = pd.DataFrame()
# df['Time'] = peak_vals[2:]
# df = pd.DataFrame()
# df['Signal'] = ICA_component_value
# df = df.set_index('Signal')
# df = df.dropna()
df.to_csv(stats_component, index=False)
def get_ica_rr(self):
ica_rr = GetRPeaks(output_sub_dir = self.output_sub_dir,
subject_id = self.subject_id,
subject_path = self.subject_path
)
import json
import warnings
from argparse import Namespace
from pathlib import Path
from typing import Dict
......@@ -10,8 +8,8 @@ import typer
from config import config
from config.config import logger
from hrvmeg import config_utils, evaluate, predict
from py_utils import get_files, print_info
from hrvmeg import predict
from py_utils import config_utils, get_files, print_info
from utils import meg_to_ica
# Initialize Typer CLI app
......@@ -64,7 +62,6 @@ def extract_ica_from_meg(
#TODO add plotting here
# fig, ax = MNE_proc.plot()
logger.info("✅ ICA successfully calculated on MEG data with MNE")
with mlflow.start_run(run_name=run_name) as run:
print_info.print_run_info(run)
......@@ -87,7 +84,7 @@ def extract_ica_from_meg(
@app.command()
def label_cardiac_ic(
args_fp: str = "config/args.json",
args_fp: str = "config/args_preproc.json",
experiment_name: str = "label_cardiac_signals",
run_name: str = "sgd",
test_run: bool = False,
......@@ -133,7 +130,12 @@ def label_cardiac_ic(
@app.command
def compare_ica_and_ecg():
def compare_ica_and_ecg(
args_rr: str="config/args_rr.json",
experiment_name: str= "compare ECG + MEG",
run_name: str="comparison 1"
test_run: bool = True
):
raise (NotImplementedError)
......
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