easy-rs 0.0.2

A Rust library for reading and processing EEG and accelerometer data from .easy files.
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use anyhow::Result;
use chrono::{DateTime, MappedLocalTime, TimeZone, Utc};
use std::collections::HashMap;
use std::fs::File;
use std::io::{self, BufRead};

/// Struct holding device information for EEG data.
#[derive(Debug)]
pub struct DeviceInfo {
    pub version: String,
    pub start_date: Option<DateTime<Utc>>,
    pub device_class: String,
    pub communication_type: String,
    pub device_id: String,
    pub software_version: String,
    pub firmware_version: String,
    pub os: String,
    pub sdcard_filename: String,
    pub additional_channel: String,
}

/// Struct for EEG settings including sampling rate, filters, and montage.
#[derive(Debug)]
pub struct EEGSettings {
    pub total_channels: usize,
    pub eeg_channels: usize,
    pub records: usize,
    pub sampling_rate: f32,
    pub configured_duration: u32,
    pub packets_lost: usize,
    pub line_filter: bool,
    pub fir_filter: bool,
    pub eog_correction: bool,
    pub reference_filter: bool,
    pub eeg_units: String,
    pub montage: HashMap<usize, String>,
    pub accelerometer: Option<AccelerometerData>,
}

/// Struct for accelerometer data.
#[derive(Debug)]
pub struct AccelerometerData {
    pub channels: usize,
    pub sampling_rate: f32,
    pub units: String,
}

/// Struct for trigger information in EEG data.
#[derive(Debug)]
pub struct TriggerInfo {
    pub triggers: HashMap<u32, String>,
}

/// Main struct representing EEG data, including device, settings, and trigger info.
#[derive(Debug)]
pub struct EEGData {
    pub device_info: DeviceInfo,
    pub eeg_settings: EEGSettings,
    pub trigger_info: TriggerInfo,
}

impl EEGData {
    /// Creates a new, empty EEGData struct.
    pub fn new() -> Self {
        EEGData {
            device_info: DeviceInfo {
                version: String::new(),
                start_date: None,
                device_class: String::new(),
                communication_type: String::new(),
                device_id: String::new(),
                software_version: String::new(),
                firmware_version: String::new(),
                os: String::new(),
                sdcard_filename: String::new(),
                additional_channel: String::new(),
            },
            eeg_settings: EEGSettings {
                total_channels: 0,
                eeg_channels: 0,
                records: 0,
                sampling_rate: 0.0,
                configured_duration: 0,
                packets_lost: 0,
                line_filter: false,
                fir_filter: false,
                eog_correction: false,
                reference_filter: false,
                eeg_units: String::new(),
                montage: HashMap::new(),
                accelerometer: None,
            },
            trigger_info: TriggerInfo {
                triggers: HashMap::new(),
            },
        }
    }

    /// Parses an EEG data file and returns an EEGData struct.
    pub fn parse_file(filename: &str) -> Result<Self> {
        let file = File::open(filename)?;
        let reader = io::BufReader::new(file);
        let mut data = EEGData::new();
        let mut current_section = None;

        for line in reader.lines() {
            let line = line?;

            if line.contains("Step Details") {
                current_section = Some("Step Details");
            } else if line.contains("EEG Settings") {
                current_section = Some("EEG Settings");
            } else if line.contains("Trigger information") {
                current_section = Some("Trigger information");
            }

            match current_section.as_deref() {
                Some("Step Details") => Self::parse_step_details(&line, &mut data),
                Some("EEG Settings") => Self::parse_eeg_settings(&line, &mut data),
                Some("Trigger information") => Self::parse_trigger_info(&line, &mut data),
                _ => continue,
            }
        }
        Ok(data)
    }

    /// Parses the 'Step Details' section of the file.
    fn parse_step_details(line: &str, data: &mut EEGData) {
        if line.contains("Info Version") {
            data.device_info.version = line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("StartDate") {
            let timestamp: i64 = line
                .split(':')
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0);

            data.device_info.start_date = match Utc.timestamp_millis_opt(timestamp) {
                MappedLocalTime::Single(dt) => Some(dt),
                MappedLocalTime::Ambiguous(early, _late) => Some(early),
                MappedLocalTime::None => None,
            }
        } else if line.contains("Device class") {
            data.device_info.device_class = line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("Communication type") {
            data.device_info.communication_type =
                line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("Device ID") {
            data.device_info.device_id = line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("Software's version") {
            data.device_info.software_version =
                line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("Firmware's version") {
            data.device_info.firmware_version =
                line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("Operative system") {
            data.device_info.os = line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("SDCard Filename") {
            data.device_info.sdcard_filename =
                line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("Additional channel") {
            data.device_info.additional_channel =
                line.split(':').nth(1).unwrap_or("").trim().to_string();
        }
    }

    /// Parses the 'EEG Settings' section of the file.
    fn parse_eeg_settings(line: &str, data: &mut EEGData) {
        if line.contains("Total number of channels") {
            data.eeg_settings.total_channels = line
                .split(':')
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0);
        } else if line.contains("Number of EEG channels") {
            data.eeg_settings.eeg_channels = line
                .split(':')
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0);
        } else if line.contains("Number of records of EEG") {
            data.eeg_settings.records = line
                .split(':')
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0);
        } else if line.contains("EEG sampling rate") {
            data.eeg_settings.sampling_rate = line
                .split(':')
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0.0);
        } else if line.contains("EEG recording configured duration") {
            data.eeg_settings.configured_duration = line
                .split(':')
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0);
        } else if line.contains("Number of packets lost") {
            data.eeg_settings.packets_lost = line
                .split(':')
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0);
        } else if line.contains("Line filter status") {
            data.eeg_settings.line_filter = line.contains("ON");
        } else if line.contains("FIR filter status") {
            data.eeg_settings.fir_filter = line.contains("ON");
        } else if line.contains("EOG correction filter status") {
            data.eeg_settings.eog_correction = line.contains("ON");
        } else if line.contains("Reference filter status") {
            data.eeg_settings.reference_filter = line.contains("ON");
        } else if line.contains("EEG units") {
            data.eeg_settings.eeg_units = line.split(':').nth(1).unwrap_or("").trim().to_string();
        } else if line.contains("Accelerometer data") {
            if line.contains("ON") {
                let accelerometer = AccelerometerData {
                    channels: 3,
                    sampling_rate: 100.0,
                    units: "mm/s^2".to_string(),
                };
                data.eeg_settings.accelerometer = Some(accelerometer);
            }
        } else if line.contains("Channel") {
            let parts: Vec<&str> = line.split(':').collect();
            let channel_number = parts[0]
                .split_whitespace()
                .nth(1)
                .unwrap_or("")
                .trim()
                .parse()
                .unwrap_or(0);
            let electrode = parts[1].trim().to_string();
            data.eeg_settings.montage.insert(channel_number, electrode);
        }
    }

    /// Parses the 'Trigger information' section of the file.
    fn parse_trigger_info(line: &str, data: &mut EEGData) {
        // Skip header if found.
        if line.contains("Code") && line.contains("Description") {
            return;
        }

        // Parse trigger code and description.
        let parts: Vec<&str> = line.split_whitespace().collect();

        if parts.len() >= 2 {
            if let Ok(code) = parts[0].parse::<u32>() {
                let description = parts[1..].join(" ");
                data.trigger_info.triggers.insert(code, description);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;

    // Helper function to create a sample EEG file.
    fn create_sample_file() -> String {
        let file_content = r#"
        Step Details
        Info Version: 1.0
        StartDate: 1609459200000
        Device class: EEG
        Communication type: Bluetooth
        Device ID: 123456
        Software's version: 1.0.0
        Firmware's version: 1.0.1
        Operative system: Linux
        SDCard Filename: eegd_data.txt
        Additional channel: Channel_Extra

        EEG Settings
        Total number of channels: 8
        Number of EEG channels: 4
        Number of records of EEG: 1000
        EEG sampling rate: 250.0
        EEG recording configured duration: 3600
        Number of packets lost: 0
        Line filter status: ON
        FIR filter status: OFF
        EOG correction filter status: ON
        Reference filter status: OFF
        EEG units: µV
        Accelerometer data: ON
        Channel 1: Fp1
        Channel 2: Fp2
        Channel 3: F3
        Channel 4: F4

        Trigger information
        Code Description
        1 Start of EEG
        2 End of EEG
        "#;

        let filename = "sample_eeg_data.txt";
        std::fs::write(filename, file_content).unwrap();
        filename.to_string()
    }

    // Test for parsing EEG data file
    #[test]
    fn test_parse_file() {
        let filename = create_sample_file();
        let eeg_data = EEGData::parse_file(&filename).unwrap();

        // Test Device Info parsing
        assert_eq!(eeg_data.device_info.version, "1.0");
        assert_eq!(eeg_data.device_info.device_class, "EEG");
        assert_eq!(eeg_data.device_info.communication_type, "Bluetooth");
        assert_eq!(eeg_data.device_info.device_id, "123456");
        assert_eq!(eeg_data.device_info.software_version, "1.0.0");
        assert_eq!(eeg_data.device_info.firmware_version, "1.0.1");
        assert_eq!(eeg_data.device_info.os, "Linux");
        assert_eq!(eeg_data.device_info.sdcard_filename, "eegd_data.txt");
        assert_eq!(eeg_data.device_info.additional_channel, "Channel_Extra");

        // Test EEG Settings parsing
        assert_eq!(eeg_data.eeg_settings.total_channels, 8);
        assert_eq!(eeg_data.eeg_settings.eeg_channels, 4);
        assert_eq!(eeg_data.eeg_settings.records, 1000);
        assert_eq!(eeg_data.eeg_settings.sampling_rate, 250.0);
        assert_eq!(eeg_data.eeg_settings.configured_duration, 3600);
        assert_eq!(eeg_data.eeg_settings.packets_lost, 0);
        assert!(eeg_data.eeg_settings.line_filter);
        assert!(!eeg_data.eeg_settings.fir_filter);
        assert!(eeg_data.eeg_settings.eog_correction);
        assert!(!eeg_data.eeg_settings.reference_filter);
        assert_eq!(eeg_data.eeg_settings.eeg_units, "µV");

        // Test Accelerometer data
        assert!(eeg_data.eeg_settings.accelerometer.is_some());
        let accelerometer = eeg_data.eeg_settings.accelerometer.as_ref().unwrap();
        assert_eq!(accelerometer.channels, 3);
        assert_eq!(accelerometer.sampling_rate, 100.0);
        assert_eq!(accelerometer.units, "mm/s^2");

        // Test Montage parsing
        assert_eq!(
            eeg_data.eeg_settings.montage.get(&1),
            Some(&"Fp1".to_string())
        );
        assert_eq!(
            eeg_data.eeg_settings.montage.get(&2),
            Some(&"Fp2".to_string())
        );
        assert_eq!(
            eeg_data.eeg_settings.montage.get(&3),
            Some(&"F3".to_string())
        );
        assert_eq!(
            eeg_data.eeg_settings.montage.get(&4),
            Some(&"F4".to_string())
        );

        // Test Trigger Information parsing
        assert_eq!(eeg_data.trigger_info.triggers.len(), 2);
        assert_eq!(
            eeg_data.trigger_info.triggers.get(&1),
            Some(&"Start of EEG".to_string())
        );
        assert_eq!(
            eeg_data.trigger_info.triggers.get(&2),
            Some(&"End of EEG".to_string())
        );

        // Clean up the sample file
        std::fs::remove_file(filename).unwrap();
    }

    // Test parsing when the file is empty
    #[test]
    fn test_parse_empty_file() {
        let filename = "empty_file.txt";
        std::fs::write(filename, "").unwrap();

        let eeg_data = EEGData::parse_file(filename).unwrap();
        assert_eq!(eeg_data.device_info.version, "");
        assert_eq!(eeg_data.eeg_settings.total_channels, 0);
        assert_eq!(eeg_data.trigger_info.triggers.len(), 0);

        std::fs::remove_file(filename).unwrap();
    }

    // Test parsing when a field is missing (e.g., missing "StartDate" in the Step Details section)
    #[test]
    fn test_parse_missing_field() {
        let file_content = r#"
        Step Details
        Info Version: 1.0
        Device class: EEG
        Communication type: Bluetooth
        Device ID: 123456
        Software's version: 1.0.0
        Firmware's version: 1.0.1
        Operative system: Linux
        SDCard Filename: eegd_data.txt
        Additional channel: Channel_Extra

        EEG Settings
        Total number of channels: 8
        Number of EEG channels: 4
        Number of records of EEG: 1000
        EEG sampling rate: 250.0
        EEG recording configured duration: 3600
        Number of packets lost: 0
        Line filter status: ON
        FIR filter status: OFF
        EOG correction filter status: ON
        Reference filter status: OFF
        EEG units: µV
        Accelerometer data: ON

        Trigger information
        Code Description
        1 Start of EEG
        2 End of EEG
        "#;

        let filename = "missing_start_date.txt";
        std::fs::write(filename, file_content).unwrap();

        let eeg_data = EEGData::parse_file(filename).unwrap();
        assert!(eeg_data.device_info.start_date.is_none());

        std::fs::remove_file(filename).unwrap();
    }
}