Skip to content

Commit 785d420

Browse files
authored
Merge pull request #98 from ClojureCivitas/hrv-for-macroexpand-noj-1
Hrv for macroexpand noj 1
2 parents 30fc196 + 1e04cb9 commit 785d420

2 files changed

Lines changed: 148 additions & 113 deletions

File tree

site/data_analysis/heart_rate_variability/exploring_heart_rate_variability.qmd

Lines changed: 110 additions & 93 deletions
Large diffs are not rendered by default.

src/data_analysis/heart_rate_variability/exploring_heart_rate_variability.clj

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
(:require [tablecloth.api :as tc]
1111
[scicloj.tableplot.v1.plotly :as plotly]
1212
[tech.v3.datatype :as dtype]
13+
[tech.v3.datatype.rolling :as rolling]
1314
[tech.v3.datatype.functional :as dfn]
1415
[tech.v3.tensor :as tensor]
1516
[tech.v3.datatype.datetime :as dt-datetime]
@@ -95,23 +96,37 @@
9596
hop-size 8
9697
n-windows (int (/ (- n window-size)
9798
hop-size))
98-
windows (->> (range n-windows)
99-
(map (fn [w]
100-
[w (let [start-idx (* w hop-size)]
101-
(-> resampled-ppi
102-
:ppi
103-
(dtype/sub-buffer start-idx window-size)))])))
104-
rmssds (->> windows
105-
(map (fn [[w window]]
106-
(-> window
107-
(tcc/shift 1)
108-
(tcc/- window)
109-
tcc/sq
110-
tcc/mean
111-
tcc/sqrt))))
112-
spectrograms (->> windows
113-
(pfor/pmap (fn [[w window]]
114-
(let [window-standardized (stats/standardize window)
99+
ranges (-> ppi-ds
100+
:t
101+
(rolling/variable-rolling-window-ranges
102+
30
103+
{:relative-window-position :left}))
104+
range-datasets (->> ranges
105+
(map (fn [r]
106+
(tc/select-rows ppi-ds r))))
107+
reasonable-ppi? #(< 500 % 900)
108+
rmssds (->> range-datasets
109+
(map (fn [{:keys [ppi]}]
110+
(->> ppi
111+
(partition-by reasonable-ppi?)
112+
(map (fn [part]
113+
(when (-> part first reasonable-ppi?)
114+
(map -
115+
(rest part)
116+
part))))
117+
(remove nil?)
118+
(apply concat)
119+
double-array
120+
tcc/sq
121+
tcc/mean
122+
math/sqrt))))
123+
spectrograms (->> (range n-windows)
124+
(pfor/pmap (fn [w]
125+
(let [start-idx (* w hop-size)
126+
window (-> resampled-ppi
127+
:ppi
128+
(dtype/sub-buffer start-idx window-size))
129+
window-standardized (stats/standardize window)
115130
window-filtered (.bandPassFilter bw
116131
(double-array window-standardized)
117132
4
@@ -179,7 +194,7 @@
179194
(plotly/layer-line {:=x :t
180195
:=y :rmssd})
181196
plotly/plot
182-
(assoc-in [:layout :yaxis :range] [0 0.1]))
197+
(assoc-in [:layout :yaxis :range] [0 100]))
183198
:power-spectrum (kind/plotly
184199
{:data [{:x times
185200
:y freqs
@@ -339,8 +354,9 @@
339354
(-> {:t peak-times}
340355
tc/dataset
341356
;; Calculate peak-to-peak intervals
342-
(tc/add-column :ppi #(tcc/- (:t %)
343-
(tcc/shift (:t %) 1)))
357+
(tc/add-column :ppi #(tcc/* 1000
358+
(tcc/- (:t %)
359+
(tcc/shift (:t %) 1))))
344360
(tc/drop-rows [0]))))
345361

346362
;; ### Plotting the PPI
@@ -376,6 +392,8 @@
376392

377393
;; ## A subject's journey
378394

395+
;; The various phases of the WESAD experiments:
396+
379397
(def id->label
380398
[:transient, :baseline,
381399
:stress, :amusement, :meditation,

0 commit comments

Comments
 (0)