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

Made new args file and updating data and meg to ica to make a general rr...

Made new args file and updating data and meg to ica to make a general rr function that is called as needed
parent 8732e1d0
No related branches found
No related tags found
1 merge request!1Progress toward getting the evaluation integrated
{
"ica_method": "fastica",
"n_components": 20,
"notch_filter_freq": 60,
"bandpass_lowfreq_cutoff": 0.5,
"bandpass_highfreq_cutoff": 100,
......
{
"peak_finding_dist":120,
"peak_finging_height_pct":10,
"smoothing":20
}
......@@ -8,7 +8,7 @@ import scipy.signal as signal
from mne.preprocessing import ICA
class GetRRDurations:
class GetRpeaks:
def __init__(self, output_sub_dir, subject_id, subject_path, ICA_value):
"""
The function takes in the subject_id, subject_path, output_sub_dir, and ICA_value and assigns
......@@ -27,7 +27,7 @@ class GetRRDurations:
def get_RR_intervals(self):
"""
The function takes in a .CSV file containing a time series and a component value, and then uses
The function takes in a .CSV file containing a time and a trace, and then uses
the scipy peak detection function to find peaks in the component value.
The function then plots the peaks and saves the plot as a .PNG file.
......@@ -43,10 +43,10 @@ class GetRRDurations:
# 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()
trace_raw = 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
df[f"ICA_Values_{self.ICA_value}"] = trace_raw
hr_arr = df.to_numpy()
......@@ -58,14 +58,14 @@ class GetRRDurations:
#############################################################
component_time = hr_arr[:, 0]
ICA_component_value = hr_arr[:, 1]
time = hr_arr[:, 0]
trace = 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
if np.median(trace) > 0.00:
trace = -trace
df[f"ICA_Values_{self.ICA_value}"] = ICA_component_value
df[f"ICA_Values_{self.ICA_value}"] = trace
# SCIPY PEAK DETECTION
......@@ -74,30 +74,30 @@ class GetRRDurations:
distance = 150
# filter height to peaks within the top 5% of largest values
num_to_filter_component = round(len(ICA_component_value) * 0.045)
num_to_filter_component = round(len(trace) * 0.045)
height_component = (
sum(sorted(ICA_component_value)[-num_to_filter_component:])
sum(sorted(trace)[-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
trace, 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]
# t_component, filtered_signal, component_rpeaks = biosppy.signals.ecg.ecg(trace, sampling_rate=500)[:3]
# Plots Component Peaks
################################################################################################
length_of_component_time_series = np.amax(component_time)
length_of_time_series = np.amax(time)
to_divide = 1
mini2 = 0
maxi2 = len(component_time) / to_divide
maxi2 = len(time) / to_divide
peaks_range = [
idx for idx in range(len(component_rpeaks)) if mini2 < component_rpeaks[idx] < maxi2
......@@ -107,8 +107,8 @@ class GetRRDurations:
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.plot(time, trace, linewidth=0.2)
plt.plot(time[component_rpeaks], trace[component_rpeaks], "x")
plt.xlim([10, 480 // to_divide])
......@@ -116,7 +116,7 @@ class GetRRDurations:
fig.set_size_inches(12, 5)
beat_cnt = len(peaks1)
avg_hr = round((beat_cnt / (length_of_component_time_series)) * 60, 2)
avg_hr = round((beat_cnt / (length_of_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}"
......@@ -133,7 +133,7 @@ class GetRRDurations:
# Writes Component RR Intervals to CSV
################################################################################################################
# peak_vals = component_time[component_rpeaks]
# peak_vals = time[component_rpeaks]
# rr_int = []
# size = len(peak_vals) - 1
......@@ -149,9 +149,10 @@ class GetRRDurations:
# df = pd.DataFrame()
# df['Signal'] = ICA_component_value
# df['Signal'] = trace
# df = df.set_index('Signal')
# df = df.dropna()
df.to_csv(stats_component, index=False)
return time[component_rpeaks], trace[component_rpeaks]
\ No newline at end of file
......@@ -76,12 +76,12 @@ class MNE_Processor:
# reject = dict(mag=5e-12, grad=4000e-13)
ica = ICA(
n_components=20, method=self.args["ica_method"], fit_params=fit_params, random_state=0
n_components=self.args["n_components"], method=self.args["ica_method"], fit_params=fit_params, random_state=0
)
ica.fit(bandpass_filtered_resampled, picks="data")
ecg_index = list(range(20))
ecg_index = list(range(self.args["n_components"]))
ica_sources = ica.get_sources(bandpass_filtered_resampled)
......
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