Skip to content

Commit 4f05c40

Browse files
authored
Entropy HRV correction (#63)
* Fix sphinx Scipy modules added to conf.py * Update hrv.py Add condition for when entropy is nan or inf
1 parent b09ca33 commit 4f05c40

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

biosppy/signals/hrv.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,14 @@ def rri_correction(rri=None, threshold=250):
311311
artifacts = np.abs(rri - rri_filt) > threshold
312312

313313
# before interpolating, check if the artifacts are at the beginning or end of the sequence
314-
if min(np.where(artifacts)[0]) < min(np.where(~artifacts)[0]):
315-
rri = rri[min(np.where(~artifacts)[0]):]
316-
artifacts = artifacts[min(np.where(~artifacts)[0]):]
317-
318-
if max(np.where(artifacts)[0]) > max(np.where(~artifacts)[0]):
319-
rri = rri[:max(np.where(~artifacts)[0])]
320-
artifacts = artifacts[:max(np.where(~artifacts)[0])]
314+
if len(np.argwhere(artifacts)) >0:
315+
if min(np.where(artifacts)[0]) < min(np.where(~artifacts)[0]):
316+
rri = rri[min(np.where(~artifacts)[0]):]
317+
artifacts = artifacts[min(np.where(~artifacts)[0]):]
318+
319+
if max(np.where(artifacts)[0]) > max(np.where(~artifacts)[0]):
320+
rri = rri[:max(np.where(~artifacts)[0])]
321+
artifacts = artifacts[:max(np.where(~artifacts)[0])]
321322

322323
# replace artifacts with cubic spline interpolation
323324
rri[artifacts] = interp1d(np.where(~artifacts)[0], rri[~artifacts],
@@ -956,9 +957,21 @@ def sample_entropy(rri, m=2, r=0.2):
956957
xm = np.array([rri[i: i + m] for i in range(n - m + 1)])
957958

958959
a = np.sum([np.sum(np.abs(xmi - xm).max(axis=1) <= r) - 1 for xmi in xm])
960+
if a > 0 and b > 0:
961+
sampen = -np.log(a / b)
962+
else:
963+
if a == 0 and b==0:
964+
# both a and b are zero => cannot determine saen
965+
sampen = np.nan
966+
elif a == 0:
967+
# a is zero => log would be infinite or undefined => cannot determine saen
968+
sampen = -np.inf
969+
else:
970+
# b is zero => a is not zero, but b is zero =>
971+
sampen = np.inf
959972

960973
# Return SampEn
961-
return -np.log(a / b)
974+
return sampen
962975

963976

964977
def approximate_entropy(rri, m=2, r=0.2):

0 commit comments

Comments
 (0)