HRV: changed wrapper calcFeat method with specialized one.

sociality-task
= 2022-04-14 11:51:53 +00:00
parent 3c058e4463
commit 075c64d1e5
4 changed files with 104 additions and 20 deletions

View File

@ -510,18 +510,18 @@ EMPATICA_TEMPERATURE:
CONTAINER: TEMP CONTAINER: TEMP
PROVIDERS: PROVIDERS:
DBDP: DBDP:
COMPUTE: True COMPUTE: False
FEATURES: ["maxtemp", "mintemp", "avgtemp", "mediantemp", "modetemp", "stdtemp", "diffmaxmodetemp", "diffminmodetemp", "entropytemp"] FEATURES: ["maxtemp", "mintemp", "avgtemp", "mediantemp", "modetemp", "stdtemp", "diffmaxmodetemp", "diffminmodetemp", "entropytemp"]
SRC_SCRIPT: src/features/empatica_temperature/dbdp/main.py SRC_SCRIPT: src/features/empatica_temperature/dbdp/main.py
CR: CR:
COMPUTE: True COMPUTE: False
FEATURES: ["autocorrelations", "countAboveMean", "countBelowMean", "maximum", "minimum", "meanAbsChange", "longestStrikeAboveMean", FEATURES: ["autocorrelations", "countAboveMean", "countBelowMean", "maximum", "minimum", "meanAbsChange", "longestStrikeAboveMean",
"longestStrikeBelowMean", "stdDev", "median", "meanChange", "numberOfZeroCrossings", "absEnergy", "linearTrendSlope", "longestStrikeBelowMean", "stdDev", "median", "meanChange", "numberOfZeroCrossings", "absEnergy", "linearTrendSlope",
"ratioBeyondRSigma", "binnedEntropy", "numOfPeaksAutocorr", "numberOfZeroCrossingsAutocorr", "areaAutocorr", "ratioBeyondRSigma", "binnedEntropy", "numOfPeaksAutocorr", "numberOfZeroCrossingsAutocorr", "areaAutocorr",
"calcMeanCrossingRateAutocorr", "countAboveMeanAutocorr", "sumPer", "sumSquared", "squareSumOfComponent", "calcMeanCrossingRateAutocorr", "countAboveMeanAutocorr", "sumPer", "sumSquared", "squareSumOfComponent",
"sumOfSquareComponents"] "sumOfSquareComponents"]
WINDOWS: WINDOWS:
COMPUTE: True COMPUTE: False
WINDOW_LENGTH: 90 # specify window length in seconds WINDOW_LENGTH: 90 # specify window length in seconds
SRC_SCRIPT: src/features/empatica_temperature/cr/main.py SRC_SCRIPT: src/features/empatica_temperature/cr/main.py
@ -530,18 +530,18 @@ EMPATICA_ELECTRODERMAL_ACTIVITY:
CONTAINER: EDA CONTAINER: EDA
PROVIDERS: PROVIDERS:
DBDP: DBDP:
COMPUTE: True COMPUTE: False
FEATURES: ["maxeda", "mineda", "avgeda", "medianeda", "modeeda", "stdeda", "diffmaxmodeeda", "diffminmodeeda", "entropyeda"] FEATURES: ["maxeda", "mineda", "avgeda", "medianeda", "modeeda", "stdeda", "diffmaxmodeeda", "diffminmodeeda", "entropyeda"]
SRC_SCRIPT: src/features/empatica_electrodermal_activity/dbdp/main.py SRC_SCRIPT: src/features/empatica_electrodermal_activity/dbdp/main.py
CR: CR:
COMPUTE: True COMPUTE: False
FEATURES: ['mean', 'std', 'q25', 'q75', 'qd', 'deriv', 'power', 'numPeaks', 'ratePeaks', 'powerPeaks', 'sumPosDeriv', 'propPosDeriv', 'derivTonic', FEATURES: ['mean', 'std', 'q25', 'q75', 'qd', 'deriv', 'power', 'numPeaks', 'ratePeaks', 'powerPeaks', 'sumPosDeriv', 'propPosDeriv', 'derivTonic',
'sigTonicDifference', 'freqFeats','maxPeakAmplitudeChangeBefore', 'maxPeakAmplitudeChangeAfter', 'avgPeakAmplitudeChangeBefore', 'sigTonicDifference', 'freqFeats','maxPeakAmplitudeChangeBefore', 'maxPeakAmplitudeChangeAfter', 'avgPeakAmplitudeChangeBefore',
'avgPeakAmplitudeChangeAfter', 'avgPeakChangeRatio', 'maxPeakIncreaseTime', 'maxPeakDecreaseTime', 'maxPeakDuration', 'maxPeakChangeRatio', 'avgPeakAmplitudeChangeAfter', 'avgPeakChangeRatio', 'maxPeakIncreaseTime', 'maxPeakDecreaseTime', 'maxPeakDuration', 'maxPeakChangeRatio',
'avgPeakIncreaseTime', 'avgPeakDecreaseTime', 'avgPeakDuration', 'maxPeakResponseSlopeBefore', 'maxPeakResponseSlopeAfter', 'avgPeakIncreaseTime', 'avgPeakDecreaseTime', 'avgPeakDuration', 'maxPeakResponseSlopeBefore', 'maxPeakResponseSlopeAfter',
'signalOverallChange', 'changeDuration', 'changeRate', 'significantIncrease', 'significantDecrease'] 'signalOverallChange', 'changeDuration', 'changeRate', 'significantIncrease', 'significantDecrease']
WINDOWS: WINDOWS:
COMPUTE: True COMPUTE: False
WINDOW_LENGTH: 80 # specify window length in seconds WINDOW_LENGTH: 80 # specify window length in seconds
SRC_SCRIPT: src/features/empatica_electrodermal_activity/cr/main.py SRC_SCRIPT: src/features/empatica_electrodermal_activity/cr/main.py
@ -551,15 +551,15 @@ EMPATICA_BLOOD_VOLUME_PULSE:
PROVIDERS: PROVIDERS:
DBDP: DBDP:
COMPUTE: True COMPUTE: True
FEATURES: ["fqHighestPeakFreqs", "fqHighestPeaks", "fqEnergyFeat", "fqEntropyFeat", "fqHistogramBins","fqAbsMean", "fqSkewness", "fqKurtosis", "fqInterquart", # Freq features FEATURES: ["maxbvp", "minbvp", "avgbvp", "medianbvp", "modebvp", "stdbvp", "diffmaxmodebvp", "diffminmodebvp", "entropybvp"]
"maxbvp", "minbvp", "avgbvp", "medianbvp", "modebvp", "stdbvp", "diffmaxmodebvp", "diffminmodebvp", "entropybvp"] # HRV features
SRC_SCRIPT: src/features/empatica_blood_volume_pulse/dbdp/main.py SRC_SCRIPT: src/features/empatica_blood_volume_pulse/dbdp/main.py
CR: CR:
COMPUTE: True COMPUTE: True
FEATURES: ['meanHr', 'ibi', 'sdnn', 'sdsd', 'rmssd', 'pnn20', 'pnn50', 'sd', 'sd2', 'sd1/sd2', 'numRR'] FEATURES: ['meanHr', 'ibi', 'sdnn', 'sdsd', 'rmssd', 'pnn20', 'pnn50', 'sd', 'sd2', 'sd1/sd2', 'numRR', # Time features
'VLF', 'LF', 'LFnorm', 'HF', 'HFnorm', 'LF/HF', 'fullIntegral'] # Freq features
WINDOWS: WINDOWS:
COMPUTE: True COMPUTE: True
WINDOW_LENGTH: 4 # specify window length in seconds WINDOW_LENGTH: 10 # specify window length in seconds
SRC_SCRIPT: src/features/empatica_blood_volume_pulse/cr/main.py SRC_SCRIPT: src/features/empatica_blood_volume_pulse/cr/main.py
# See https://www.rapids.science/latest/features/empatica-inter-beat-interval/ # See https://www.rapids.science/latest/features/empatica-inter-beat-interval/
@ -570,6 +570,14 @@ EMPATICA_INTER_BEAT_INTERVAL:
COMPUTE: False COMPUTE: False
FEATURES: ["maxibi", "minibi", "avgibi", "medianibi", "modeibi", "stdibi", "diffmaxmodeibi", "diffminmodeibi", "entropyibi"] FEATURES: ["maxibi", "minibi", "avgibi", "medianibi", "modeibi", "stdibi", "diffmaxmodeibi", "diffminmodeibi", "entropyibi"]
SRC_SCRIPT: src/features/empatica_inter_beat_interval/dbdp/main.py SRC_SCRIPT: src/features/empatica_inter_beat_interval/dbdp/main.py
CR:
COMPUTE: False
FEATURES: ['meanHr', 'ibi', 'sdnn', 'sdsd', 'rmssd', 'pnn20', 'pnn50', 'sd', 'sd2', 'sd1/sd2', 'numRR', # Time features
'VLF', 'LF', 'LFnorm', 'HF', 'HFnorm', 'LF/HF', 'fullIntegral'] # Freq features
WINDOWS:
COMPUTE: True
WINDOW_LENGTH: 4 # specify window length in seconds
SRC_SCRIPT: src/features/inter_beat_interval/cr/main.py
# See https://www.rapids.science/latest/features/empatica-tags/ # See https://www.rapids.science/latest/features/empatica-tags/
EMPATICA_TAGS: EMPATICA_TAGS:

View File

@ -1,8 +1,8 @@
import pandas as pd import pandas as pd
from scipy.stats import entropy from scipy.stats import entropy
from CalculatingFeatures.helper_functions import convertInputInto2d, hrvFeatureNames, frequencyFeatureNames from CalculatingFeatures.helper_functions import convertInputInto2d, hrvFeatureNames, hrvFreqFeatureNames
from CalculatingFeatures.calculate_features import calculateFeatures from CalculatingFeatures.hrv import extractHrvFeatures, extractHrvFeatures2D, extractHrvFeatures2DWrapper
import sys import sys
@ -13,13 +13,15 @@ def getSampleRate(data):
except: except:
raise Exception("Error occured while trying to get the mean sample rate from the data.") raise Exception("Error occured while trying to get the mean sample rate from the data.")
return 1000/timestamps_diff return int(1000/timestamps_diff)
def extractBVPFeaturesFromIntradayData(bvp_intraday_data, features, window_length, time_segment, filter_data_by_segment): def extractBVPFeaturesFromIntradayData(bvp_intraday_data, features, window_length, time_segment, filter_data_by_segment):
bvp_intraday_features = pd.DataFrame(columns=["local_segment"] + features) bvp_intraday_features = pd.DataFrame(columns=["local_segment"] + features)
if not bvp_intraday_data.empty: if not bvp_intraday_data.empty:
sample_rate = getSampleRate(bvp_intraday_data) sample_rate = getSampleRate(bvp_intraday_data)
print(bvp_intraday_data.shape)
bvp_intraday_data = filter_data_by_segment(bvp_intraday_data, time_segment) bvp_intraday_data = filter_data_by_segment(bvp_intraday_data, time_segment)
@ -31,12 +33,20 @@ def extractBVPFeaturesFromIntradayData(bvp_intraday_data, features, window_lengt
if window_length is None: if window_length is None:
bvp_intraday_features = \ bvp_intraday_features = \
bvp_intraday_data.groupby('local_segment').apply(\ bvp_intraday_data.groupby('local_segment').apply(\
lambda x: calculateFeatures(convertInputInto2d(x['blood_volume_pulse'], x.shape[0]), fs=int(sample_rate), featureNames=features)) lambda x:
extractHrvFeatures2DWrapper(
convertInputInto2d(x['blood_volume_pulse'], x.shape[0]),
sampling=sample_rate, hampel_fiter=False, median_filter=False, mod_z_score_filter=True, featureNames=features))
else: else:
bvp_intraday_features = \ bvp_intraday_features = \
bvp_intraday_data.groupby('local_segment').apply(\ bvp_intraday_data.groupby('local_segment').apply(\
lambda x: calculateFeatures(convertInputInto2d(x['blood_volume_pulse'], window_length*int(sample_rate)), fs=int(sample_rate), featureNames=features)) lambda x:
extractHrvFeatures2DWrapper(
convertInputInto2d(x['blood_volume_pulse'], window_length*sample_rate),
sampling=sample_rate, hampel_fiter=False, median_filter=False, mod_z_score_filter=True, featureNames=features))
print(sample_rate)
print(bvp_intraday_features)
bvp_intraday_features.reset_index(inplace=True) bvp_intraday_features.reset_index(inplace=True)
return bvp_intraday_features return bvp_intraday_features
@ -55,7 +65,7 @@ def cr_features(sensor_data_files, time_segment, provider, filter_data_by_segmen
requested_window_length = None requested_window_length = None
# name of the features this function can compute # name of the features this function can compute
base_intraday_features_names = hrvFeatureNames + frequencyFeatureNames base_intraday_features_names = hrvFeatureNames + hrvFreqFeatureNames
# the subset of requested features this function can compute # the subset of requested features this function can compute
intraday_features_to_compute = list(set(requested_intraday_features) & set(base_intraday_features_names)) intraday_features_to_compute = list(set(requested_intraday_features) & set(base_intraday_features_names))

View File

@ -0,0 +1,66 @@
import pandas as pd
from scipy.stats import entropy
from CalculatingFeatures.helper_functions import convertInputInto2dTime, convert2DEmpaticaToArray hrvFeatureNames, hrvFreqFeatureNames
from CalculatingFeatures.calculate_features import calculateFeatures
import sys
def getSampleRate(data):
try:
timestamps_diff = data['timestamp'].diff().dropna().mean()
except:
raise Exception("Error occured while trying to get the mean sample rate from the data.")
return 1000/timestamps_diff
def extractIBIFeaturesFromIntradayData(ibi_intraday_data, features, window_length, time_segment, filter_data_by_segment):
ibi_intraday_features = pd.DataFrame(columns=["local_segment"] + features)
if not ibi_intraday_data.empty:
sample_rate = getSampleRate(ibi_intraday_data)
ibi_intraday_data = filter_data_by_segment(ibi_intraday_data, time_segment)
if not ibi_intraday_data.empty:
ibi_intraday_features = pd.DataFrame()
# apply methods from calculate features module
# if window_length is None:
# ibi_intraday_features = \
# ibi_intraday_data.groupby('local_segment').apply(\
# lambda x: calculateFeatures(convertInputInto2d(x['blood_volume_pulse'], x.shape[0]), fs=int(sample_rate), featureNames=features))
# else:
# ibi_intraday_features = \
# ibi_intraday_data.groupby('local_segment').apply(\
# lambda x: calculateFeatures(convertInputInto2d(x['blood_volume_pulse'], window_length*int(sample_rate)), fs=int(sample_rate), featureNames=features))
ibi_intraday_features.reset_index(inplace=True)
return ibi_intraday_features
def cr_features(sensor_data_files, time_segment, provider, filter_data_by_segment, *args, **kwargs):
ibi_intraday_data = pd.read_csv(sensor_data_files["sensor_data"])
requested_intraday_features = provider["FEATURES"]
calc_windows = kwargs.get('calc_windows', False)
if provider["WINDOWS"]["COMPUTE"] and calc_windows:
requested_window_length = provider["WINDOWS"]["WINDOW_LENGTH"]
else:
requested_window_length = None
# name of the features this function can compute
base_intraday_features_names = hrvFeatureNames + hrvFreqFeatureNames
# the subset of requested features this function can compute
intraday_features_to_compute = list(set(requested_intraday_features) & set(base_intraday_features_names))
# extract features from intraday data
ibi_intraday_features = extractBVPFeaturesFromIntradayData(ibi_intraday_data, intraday_features_to_compute,
requested_window_length, time_segment, filter_data_by_segment)
return ibi_intraday_features

View File

@ -27,7 +27,7 @@ else:
if calc_windows: if calc_windows:
sensor_features.to_csv(snakemake.output[1], index=False) sensor_features.to_csv(snakemake.output[1], index=False)
sensor_features = fetch_provider_features(provider, provider_key, sensor_key, sensor_data_files, time_segments_file, calc_windows=False) sensor_features = fetch_provider_features(provider, provider_key, sensor_key, sensor_data_files, time_segments_file, calc_windows=False)
elif "empatica" in sensor_key and provider_key == "dbdp": elif "empatica" in sensor_key:
pd.DataFrame().to_csv(snakemake.output[1], index=False) pd.DataFrame().to_csv(snakemake.output[1], index=False)