1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//! A module providing functionality for performing window-based analysis of Heart Rate
//! Variability (HRV) metrics over non-overlapping windows of RR intervals.
//!
//! The analysis is done over fixed-duration windows, where each window is processed
//! independently, and the results for each window are stored. The user can customize the
//! pipeline for HRV metric calculation by implementing the `AnalysisPipeline` trait, or
//! they can use the default implementation that computes various HRV metrics.
//!
//! ## Key Components:
//!
//! - `WindowsAnalysisBuilder<T>`: A builder that allows you to configure and generate a
//! `WindowsAnalysis` with user-defined data and pipeline.
//! - `AnalysisPipeline<T>`: A trait that allows the user to define custom pipelines to
//! process RR interval data and compute various HRV metrics.
//! - `DefaultPipeline`: A default implementation of the `AnalysisPipeline` trait that computes
//! HRV metrics using the `TimeMetrics`, `FrequencyMetrics`, and `GeometricMetrics` modules.
//! - `WindowsAnalysis<T>`: A struct representing the result of the windowed HRV analysis,
//! containing the computed HRV metrics for each window.
//!
//! ## Example Usage:
//!
//! ### Basic Example (Using Default Pipeline):
//! ```rust
//! use cardio_rs::utils::test_data::RR_INTERVALS;
//! use cardio_rs::windows_analysis::WindowsAnalysisBuilder;
//!
//! let rr_intervals = RR_INTERVALS.to_vec();
//! let window_size = 60_000.0; // 1-minute window size
//!
//! let windows_analysis = WindowsAnalysisBuilder::new(rr_intervals)
//! .with_window_size(window_size)
//! .build();
//!
//! for (i, hrv_metrics) in windows_analysis.metrics.iter().enumerate() {
//! println!("Window {}: HRV Metrics: {:?}", i, hrv_metrics);
//! }
//! ```
//!
//! ### Custom Pipeline Example:
//! You can define your own custom analysis pipeline by implementing the `AnalysisPipeline` trait.
//! This allows you to customize how HRV metrics are computed over the RR intervals.
//!
//! ```rust
//! use cardio_rs::utils::test_data::RR_INTERVALS;
//! use cardio_rs::{windows_analysis::{WindowsAnalysisBuilder}, HrvMetrics, geometric_domain::GeometricMetrics, non_linear::NonLinearMetrics, processing_utils::{EctopicMethod, RRIntervals, DetectOutliers, AnalysisPipeline}, time_domain::TimeMetrics, frequency_domain::FrequencyMetrics};
//!
//! // Define a custom pipeline by implementing the AnalysisPipeline trait
//! struct CustomPipeline;
//!
//! impl AnalysisPipeline<f64> for CustomPipeline
//! {
//! fn process(&self, data: Vec<f64>) -> HrvMetrics<f64> {
//! let mut rr_intervals = RRIntervals::new(data);
//! rr_intervals.detect_ectopics(EctopicMethod::Karlsson);
//! rr_intervals.detect_outliers(&300., &2_000.);
//! rr_intervals.remove_outliers_ectopics();
//!
//! let time = TimeMetrics::compute(rr_intervals.as_slice());
//! let frequency = FrequencyMetrics::compute(rr_intervals.as_slice(), 10.);
//! let geometric = GeometricMetrics::compute(rr_intervals.as_slice());
//! let non_linear = NonLinearMetrics::compute_default(rr_intervals.as_slice());
//!
//! HrvMetrics {
//! time,
//! frequency,
//! geometric,
//! non_linear,
//! }
//! }
//! }
//!
//! let rr_intervals = RR_INTERVALS.to_vec();
//!
//! // Create the window analysis with the custom pipeline and non-overlapping windows
//! let custom_pipeline = Box::new(CustomPipeline);
//! let windows_analysis = WindowsAnalysisBuilder::new(rr_intervals)
//! .with_window_size(60_000.0) // Set the window size (1 minute)
//! .with_pipeline(custom_pipeline) // Use the custom pipeline
//! .build();
//!
//! // Print HRV metrics for each window
//! for (i, hrv_metrics) in windows_analysis.metrics.iter().enumerate() {
//! println!("Window {}: HRV Metrics: {:?}", i, hrv_metrics);
//! }
//! ```
extern crate alloc;
use crate::;
use ;
use Sum;
use Float;
/// A struct that holds the HRV metrics for each window in the analysis.
///
/// `WindowsAnalysis<T>` stores the HRV metrics computed for each window of RR intervals.
/// The `metrics` vector contains the HRV results for all the non-overlapping windows
/// that have been processed. Each window is defined by the specified `window_size`.
/// A builder struct for configuring and constructing a `WindowsAnalysis`.
///
/// The `WindowsAnalysisBuilder` struct is used to configure the sliding window size, the analysis pipeline,
/// and the data for HRV computation. After the builder is configured, the `build()` method is used to generate
/// a `WindowsAnalysis` instance containing HRV metrics for each window.