Skip to content

Commit

Permalink
Merge pull request #83 from meierman1/data_consistency
Browse files Browse the repository at this point in the history
working_data['peaklist'] is always np.array
  • Loading branch information
paulvangentcom committed Dec 16, 2021
2 parents 42534d7 + c71f5e0 commit 7e67402
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions heartpy/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def calc_rr(peaklist, sample_rate, working_data={}):
if len(peaklist) > 0:
if peaklist[0] <= ((sample_rate / 1000.0) * 150):
peaklist = np.delete(peaklist, 0)
working_data['peaklist'] = peaklist
working_data['ybeat'] = np.delete(working_data['ybeat'], 0)
working_data['peaklist'] = peaklist # Make sure, peaklist is always an np.array

rr_list = (np.diff(peaklist) / sample_rate) * 1000.0
rr_indices = [(peaklist[i], peaklist[i+1]) for i in range(len(peaklist) - 1)]
Expand Down Expand Up @@ -132,15 +132,15 @@ def update_rr(working_data={}):
This will have generated a corrected RR_list object in the dictionary:
>>> wd['RR_list_cor']
[800.0, 1250.0, 1140.0]
array([ 800., 1250., 1140.])
As well as updated the lists RR_diff (differences between adjacent peak-peak intervals) and
RR_sqdiff (squared differences between adjacent peak-peak intervals).
'''
rr_source = working_data['RR_list']
b_peaklist = working_data['binary_peaklist']
rr_list = [rr_source[i] for i in range(len(rr_source)) if b_peaklist[i] + b_peaklist[i+1] == 2]
rr_mask = [0 if (b_peaklist[i] + b_peaklist[i+1] == 2) else 1 for i in range(len(rr_source))]
rr_list = np.array([rr_source[i] for i in range(len(rr_source)) if b_peaklist[i] + b_peaklist[i+1] == 2])
rr_mask = np.array([0 if (b_peaklist[i] + b_peaklist[i+1] == 2) else 1 for i in range(len(rr_source))])
rr_masked = np.ma.array(rr_source, mask=rr_mask)
rr_diff = np.abs(np.diff(rr_masked))
rr_diff = rr_diff[~rr_diff.mask]
Expand Down
12 changes: 6 additions & 6 deletions heartpy/peakdetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def detect_peaks(hrdata, rol_mean, ma_perc, sample_rate, update_dict=True, worki
at the first five peak positions:
>>> wd['peaklist'][0:5]
[63, 165, 264, 360, 460]
array([ 63, 165, 264, 360, 460], dtype=int64)
'''
rmean = np.array(rol_mean)

Expand Down Expand Up @@ -272,7 +272,7 @@ def fit_peaks(hrdata, rol_mean, sample_rate, bpmmin=40, bpmmax=180, working_data
To illustrate, these are the first five detected peaks:
>>> wd['peaklist'][0:5]
[63, 165, 264, 360, 460]
array([ 63, 165, 264, 360, 460], dtype=int64)
and the corresponding peak-peak intervals:
Expand Down Expand Up @@ -468,9 +468,9 @@ def interpolate_peaks(data, peaks, sample_rate, desired_sample_rate=1000.0, work
>>> import heartpy as hp
>>> data, _ = hp.load_exampledata(0)
>>> wd, m = hp.process(data, 100.0)
>>> wd, m = process(data, 100.0)
>>> wd['peaklist'][0:5]
[63, 165, 264, 360, 460]
array([ 63, 165, 264, 360, 460], dtype=int64)
Now, the resolution is at max 10ms as that's the distance between data points.
We can use the high precision mode for example to approximate a more precise
Expand All @@ -479,7 +479,7 @@ def interpolate_peaks(data, peaks, sample_rate, desired_sample_rate=1000.0, work
>>> wd = interpolate_peaks(data = data, peaks = wd['peaklist'],
... sample_rate = 100.0, desired_sample_rate = 1000.0, working_data = wd)
>>> wd['peaklist'][0:5]
[63.5, 165.4, 263.6, 360.4, 460.2]
array([ 63.5, 165.4, 263.6, 360.4, 460.2])
As you can see the accuracy of peak positions has increased.
Note that you cannot magically upsample nothing into something. Be reasonable.
Expand All @@ -498,6 +498,6 @@ def interpolate_peaks(data, peaks, sample_rate, desired_sample_rate=1000.0, work
peakpos = np.argmax(resampled)
peaks.append((i[0] + (peakpos * ratio)))

working_data['peaklist'] = peaks
working_data['peaklist'] = np.asarray(peaks)

return working_data

0 comments on commit 7e67402

Please sign in to comment.