|
10 | 10 | (:require [tablecloth.api :as tc] |
11 | 11 | [scicloj.tableplot.v1.plotly :as plotly] |
12 | 12 | [tech.v3.datatype :as dtype] |
| 13 | + [tech.v3.datatype.rolling :as rolling] |
13 | 14 | [tech.v3.datatype.functional :as dfn] |
14 | 15 | [tech.v3.tensor :as tensor] |
15 | 16 | [tech.v3.datatype.datetime :as dt-datetime] |
|
95 | 96 | hop-size 8 |
96 | 97 | n-windows (int (/ (- n window-size) |
97 | 98 | 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) |
115 | 130 | window-filtered (.bandPassFilter bw |
116 | 131 | (double-array window-standardized) |
117 | 132 | 4 |
|
179 | 194 | (plotly/layer-line {:=x :t |
180 | 195 | :=y :rmssd}) |
181 | 196 | plotly/plot |
182 | | - (assoc-in [:layout :yaxis :range] [0 0.1])) |
| 197 | + (assoc-in [:layout :yaxis :range] [0 100])) |
183 | 198 | :power-spectrum (kind/plotly |
184 | 199 | {:data [{:x times |
185 | 200 | :y freqs |
|
339 | 354 | (-> {:t peak-times} |
340 | 355 | tc/dataset |
341 | 356 | ;; 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)))) |
344 | 360 | (tc/drop-rows [0])))) |
345 | 361 |
|
346 | 362 | ;; ### Plotting the PPI |
|
376 | 392 |
|
377 | 393 | ;; ## A subject's journey |
378 | 394 |
|
| 395 | +;; The various phases of the WESAD experiments: |
| 396 | + |
379 | 397 | (def id->label |
380 | 398 | [:transient, :baseline, |
381 | 399 | :stress, :amusement, :meditation, |
|
0 commit comments