Georg Simon Willer commited on
Commit
d438052
·
1 Parent(s): 4548c26

Change Parameter name from frequency to sampling_rate

Browse files
src/ecg2hrv.py CHANGED
@@ -8,30 +8,30 @@ class ECG2HRV(FeatureExtractor):
8
  def __init__(self):
9
  super().__init__()
10
 
11
- def extract_features(self, ecg, frequency, baseline=None, normalization_method=None):
12
  # Ensure the numpy has at least one dimension (i.e. is not a scalar)
13
  if ecg.ndim < 1:
14
  raise ValueError("Array must have at least one dimension")
15
 
16
  # Preprocess the ecg signal
17
- ecg = nk.ecg_clean(ecg_signal=ecg, sampling_rate=frequency, method="pantompkins1985")
18
 
19
  # Compute the HRV features
20
- features = self.get_hrv_features(ecg, frequency)
21
 
22
  # Normalize if baseline is available and method is set - TBD
23
  if baseline is not None and normalization_method is not None:
24
- baseline_features = self.get_hrv_features(baseline, frequency)
25
  features = self.normalize_features(features, baseline_features, normalization_method)
26
 
27
  return features
28
 
29
- def get_hrv_features(self, ecg, frequency):
30
  # Find peaks
31
- peaks, info = nk.ecg_peaks(ecg, sampling_rate=frequency, method="pantompkins1985")
32
 
33
  # Compute time domain features
34
- hrv_time_features = nk.hrv_time(peaks, sampling_rate=frequency)
35
  # Compute frequency domain features
36
  # hrv_frequency_features = nk.hrv_frequency(peaks, sampling_rate=fs, method="welch", show=False)
37
 
 
8
  def __init__(self):
9
  super().__init__()
10
 
11
+ def extract_features(self, ecg, sampling_rate, baseline=None, normalization_method=None):
12
  # Ensure the numpy has at least one dimension (i.e. is not a scalar)
13
  if ecg.ndim < 1:
14
  raise ValueError("Array must have at least one dimension")
15
 
16
  # Preprocess the ecg signal
17
+ ecg = nk.ecg_clean(ecg_signal=ecg, sampling_rate=sampling_rate, method="pantompkins1985")
18
 
19
  # Compute the HRV features
20
+ features = self.get_hrv_features(ecg, sampling_rate)
21
 
22
  # Normalize if baseline is available and method is set - TBD
23
  if baseline is not None and normalization_method is not None:
24
+ baseline_features = self.get_hrv_features(baseline, sampling_rate)
25
  features = self.normalize_features(features, baseline_features, normalization_method)
26
 
27
  return features
28
 
29
+ def get_hrv_features(self, ecg, sampling_rate):
30
  # Find peaks
31
+ peaks, info = nk.ecg_peaks(ecg, sampling_rate=sampling_rate, method="pantompkins1985")
32
 
33
  # Compute time domain features
34
+ hrv_time_features = nk.hrv_time(peaks, sampling_rate=sampling_rate)
35
  # Compute frequency domain features
36
  # hrv_frequency_features = nk.hrv_frequency(peaks, sampling_rate=fs, method="welch", show=False)
37
 
src/ecg_2_hrv_pipeline.py CHANGED
@@ -6,16 +6,16 @@ class Ecg2HrvPipeline(Pipeline):
6
 
7
  def _sanitize_parameters(self, **kwargs):
8
  preprocess_kwargs = {}
9
- if "frequency" in kwargs:
10
- preprocess_kwargs["frequency"] = kwargs["frequency"]
11
  if "baseline" in kwargs:
12
  preprocess_kwargs["baseline"] = kwargs["baseline"]
13
  if "normalization_method" in kwargs:
14
  preprocess_kwargs["normalization_method"] = kwargs["normalization_method"]
15
  return preprocess_kwargs, {}, {}
16
 
17
- def preprocess(self, inputs, frequency = 100, baseline = None, normalization_method = None):
18
- return self.ecg2HrvExtractor.extract_features(inputs, frequency, baseline, normalization_method)
19
 
20
  def _forward(self, model_inputs):
21
  # currently empty as all preprocessing steps are performed by preprocess function
 
6
 
7
  def _sanitize_parameters(self, **kwargs):
8
  preprocess_kwargs = {}
9
+ if "sampling_rate" in kwargs:
10
+ preprocess_kwargs["sampling_rate"] = kwargs["sampling_rate"]
11
  if "baseline" in kwargs:
12
  preprocess_kwargs["baseline"] = kwargs["baseline"]
13
  if "normalization_method" in kwargs:
14
  preprocess_kwargs["normalization_method"] = kwargs["normalization_method"]
15
  return preprocess_kwargs, {}, {}
16
 
17
+ def preprocess(self, inputs, sampling_rate = 1000, baseline = None, normalization_method = None):
18
+ return self.ecg2HrvExtractor.extract_features(inputs, sampling_rate, baseline, normalization_method)
19
 
20
  def _forward(self, model_inputs):
21
  # currently empty as all preprocessing steps are performed by preprocess function
src/rpeaks2hrv.py CHANGED
@@ -10,21 +10,21 @@ class WindowingMethod(Enum):
10
 
11
  class RPeak2HRV():
12
 
13
- def get_hrv_features(self, input, windowing_method:str = None, time_header = "SystemTime", rri_header = "interbeat_interval", window_size = "60s", frequency = 100):
14
  data = self._load_data(input)
15
- refined_data = self._refine_dataframe(input=data, frequency=frequency, time_header=time_header, rri_header=rri_header)
16
  if (windowing_method != None):
17
  windows = self._apply_windowing(data=refined_data, method=windowing_method, window_size= window_size)
18
  # TODO: Implement hrv_time on windows
19
  hrv_values = pd.DataFrame(columns=['window_start', 'window_end', 'HRV_MeanNN', 'HRV_SDNN', 'HRV_SDANN1', 'HRV_SDNNI1', 'HRV_SDANN2', 'HRV_SDNNI2', 'HRV_SDANN5', 'HRV_SDNNI5', 'HRV_RMSSD', 'HRV_SDSD', 'HRV_CVNN', 'HRV_CVSD', 'HRV_MedianNN', 'HRV_MadNN', 'HRV_MCVNN', 'HRV_IQRNN', 'HRV_SDRMSSD', 'HRV_Prc20NN', 'HRV_Prc80NN', 'HRV_pNN50', 'HRV_pNN20', 'HRV_MinNN', 'HRV_MaxNN', 'HRV_HTI', 'HRV_TINN'])
20
  for window in windows:
21
- hrv_time = nk.hrv_time(self._convert_format(window), frequency)
22
  hrv_time['window_start'] = window.index[0]
23
  hrv_time['window_end'] = window.index[-1]
24
  hrv_values = pd.concat([hrv_values, hrv_time], ignore_index=True)
25
  return hrv_values
26
  else:
27
- return nk.hrv_time(self._convert_format(refined_data), frequency)
28
 
29
  def _load_data(self, input):
30
  if isinstance(input, str):
@@ -46,11 +46,11 @@ class RPeak2HRV():
46
  else:
47
  raise ValueError('File format not supported. Please provide a csv or txt file.')
48
 
49
- def _refine_dataframe(self, input:pd.DataFrame, time_header:str, rri_header:str, frequency = 100):
50
  input = self.__clean_data(input)
51
  if 'ECG_R_Peaks' in input.columns:
52
  # Schritt 1: Berechne die Timestamps für alle Datenpunkte
53
- timestamps = pd.to_datetime(input.index / frequency, unit='s') # Indizes durch Sampling-Rate teilen
54
  data = pd.DataFrame(input["ECG_R_Peaks"], columns=["ECG_R_Peaks"])
55
  data['Timestamp'] = timestamps
56
  data.set_index("Timestamp", inplace=True)
 
10
 
11
  class RPeak2HRV():
12
 
13
+ def get_hrv_features(self, input, windowing_method:str = None, time_header = "SystemTime", rri_header = "interbeat_interval", window_size = "60s", sampling_rate = 1000):
14
  data = self._load_data(input)
15
+ refined_data = self._refine_dataframe(input=data, sampling_rate=sampling_rate, time_header=time_header, rri_header=rri_header)
16
  if (windowing_method != None):
17
  windows = self._apply_windowing(data=refined_data, method=windowing_method, window_size= window_size)
18
  # TODO: Implement hrv_time on windows
19
  hrv_values = pd.DataFrame(columns=['window_start', 'window_end', 'HRV_MeanNN', 'HRV_SDNN', 'HRV_SDANN1', 'HRV_SDNNI1', 'HRV_SDANN2', 'HRV_SDNNI2', 'HRV_SDANN5', 'HRV_SDNNI5', 'HRV_RMSSD', 'HRV_SDSD', 'HRV_CVNN', 'HRV_CVSD', 'HRV_MedianNN', 'HRV_MadNN', 'HRV_MCVNN', 'HRV_IQRNN', 'HRV_SDRMSSD', 'HRV_Prc20NN', 'HRV_Prc80NN', 'HRV_pNN50', 'HRV_pNN20', 'HRV_MinNN', 'HRV_MaxNN', 'HRV_HTI', 'HRV_TINN'])
20
  for window in windows:
21
+ hrv_time = nk.hrv_time(self._convert_format(window), sampling_rate)
22
  hrv_time['window_start'] = window.index[0]
23
  hrv_time['window_end'] = window.index[-1]
24
  hrv_values = pd.concat([hrv_values, hrv_time], ignore_index=True)
25
  return hrv_values
26
  else:
27
+ return nk.hrv_time(self._convert_format(refined_data), sampling_rate)
28
 
29
  def _load_data(self, input):
30
  if isinstance(input, str):
 
46
  else:
47
  raise ValueError('File format not supported. Please provide a csv or txt file.')
48
 
49
+ def _refine_dataframe(self, input:pd.DataFrame, time_header:str, rri_header:str, sampling_rate = 1000):
50
  input = self.__clean_data(input)
51
  if 'ECG_R_Peaks' in input.columns:
52
  # Schritt 1: Berechne die Timestamps für alle Datenpunkte
53
+ timestamps = pd.to_datetime(input.index / sampling_rate, unit='s') # Indizes durch Sampling-Rate teilen
54
  data = pd.DataFrame(input["ECG_R_Peaks"], columns=["ECG_R_Peaks"])
55
  data['Timestamp'] = timestamps
56
  data.set_index("Timestamp", inplace=True)
src/rpeaks_2_hrv_pipeline.py CHANGED
@@ -6,8 +6,8 @@ class RPeak2HRVPipeline(Pipeline):
6
 
7
  def _sanitize_parameters(self, **kwargs):
8
  preprocess_kwargs = {}
9
- if "frequency" in kwargs:
10
- preprocess_kwargs["frequency"] = kwargs["frequency"]
11
  if "windowing_method" in kwargs:
12
  preprocess_kwargs["windowing_method"] = kwargs["windowing_method"]
13
  if "time_header" in kwargs:
@@ -18,8 +18,8 @@ class RPeak2HRVPipeline(Pipeline):
18
  preprocess_kwargs["window_size"] = kwargs["window_size"]
19
  return preprocess_kwargs, {}, {}
20
 
21
- def preprocess(self, inputs, windowing_method:str = None, time_header = "SystemTime", rri_header = "interbeat_interval", window_size = "60s", frequency = 100):
22
- return self.rpeak2HRVExtractor.get_hrv_features(inputs, windowing_method=windowing_method, time_header=time_header, rri_header=rri_header, window_size=window_size, frequency=frequency)
23
 
24
  def _forward(self, model_inputs):
25
  # currently empty as all preprocessing steps are performed by preprocess function
 
6
 
7
  def _sanitize_parameters(self, **kwargs):
8
  preprocess_kwargs = {}
9
+ if "sampling_rate" in kwargs:
10
+ preprocess_kwargs["sampling_rate"] = kwargs["sampling_rate"]
11
  if "windowing_method" in kwargs:
12
  preprocess_kwargs["windowing_method"] = kwargs["windowing_method"]
13
  if "time_header" in kwargs:
 
18
  preprocess_kwargs["window_size"] = kwargs["window_size"]
19
  return preprocess_kwargs, {}, {}
20
 
21
+ def preprocess(self, inputs, windowing_method:str = None, time_header = "SystemTime", rri_header = "interbeat_interval", window_size = "60s", sampling_rate = 1000):
22
+ return self.rpeak2HRVExtractor.get_hrv_features(inputs, windowing_method=windowing_method, time_header=time_header, rri_header=rri_header, window_size=window_size, sampling_rate=sampling_rate)
23
 
24
  def _forward(self, model_inputs):
25
  # currently empty as all preprocessing steps are performed by preprocess function