edfarray 1.2.0__tar.gz → 1.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. {edfarray-1.2.0 → edfarray-1.2.2}/Cargo.lock +2 -2
  2. {edfarray-1.2.0 → edfarray-1.2.2}/PKG-INFO +1 -1
  3. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/Cargo.toml +1 -1
  4. edfarray-1.2.2/crates/edfarray-core/src/alloc.rs +33 -0
  5. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/edit.rs +66 -17
  6. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/epoch.rs +68 -2
  7. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/error.rs +9 -0
  8. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/file.rs +264 -55
  9. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/lib.rs +1 -0
  10. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/mmap.rs +7 -0
  11. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/proxy.rs +57 -6
  12. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/proxy_2d.rs +17 -1
  13. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/proxy_3d.rs +5 -4
  14. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/signal.rs +35 -3
  15. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/writer.rs +699 -38
  16. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/Cargo.toml +1 -1
  17. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/aio.rs +67 -47
  18. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/epoch.rs +12 -5
  19. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/errors.rs +4 -0
  20. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/file.rs +58 -50
  21. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/indexing.rs +12 -0
  22. edfarray-1.2.2/crates/edfarray-python/src/numpy_util.rs +41 -0
  23. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/proxy_2d.rs +4 -25
  24. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/proxy_3d.rs +3 -17
  25. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/writer.rs +22 -2
  26. {edfarray-1.2.0 → edfarray-1.2.2}/edfarray/_core/__init__.pyi +21 -6
  27. {edfarray-1.2.0 → edfarray-1.2.2}/edfarray/aio/__init__.pyi +25 -4
  28. {edfarray-1.2.0 → edfarray-1.2.2}/pyproject.toml +10 -0
  29. edfarray-1.2.0/crates/edfarray-python/src/numpy_util.rs +0 -6
  30. {edfarray-1.2.0 → edfarray-1.2.2}/Cargo.toml +0 -0
  31. {edfarray-1.2.0 → edfarray-1.2.2}/LICENSE +0 -0
  32. {edfarray-1.2.0 → edfarray-1.2.2}/README.md +0 -0
  33. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/README.md +0 -0
  34. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/annotation.rs +0 -0
  35. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/grid.rs +0 -0
  36. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/group.rs +0 -0
  37. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/header.rs +0 -0
  38. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-core/src/record.rs +0 -0
  39. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/README.md +0 -0
  40. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/annotations.rs +0 -0
  41. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/bin/gen_stubs.rs +0 -0
  42. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/edit.rs +0 -0
  43. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/group.rs +0 -0
  44. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/lib.rs +0 -0
  45. {edfarray-1.2.0 → edfarray-1.2.2}/crates/edfarray-python/src/signal.rs +0 -0
  46. {edfarray-1.2.0 → edfarray-1.2.2}/edfarray/__init__.py +0 -0
  47. {edfarray-1.2.0 → edfarray-1.2.2}/edfarray/aio/__init__.py +0 -0
  48. {edfarray-1.2.0 → edfarray-1.2.2}/edfarray/py.typed +0 -0
@@ -196,7 +196,7 @@ dependencies = [
196
196
 
197
197
  [[package]]
198
198
  name = "edfarray"
199
- version = "1.2.0"
199
+ version = "1.2.2"
200
200
  dependencies = [
201
201
  "chrono",
202
202
  "edfarray-core",
@@ -209,7 +209,7 @@ dependencies = [
209
209
 
210
210
  [[package]]
211
211
  name = "edfarray-core"
212
- version = "1.2.0"
212
+ version = "1.2.2"
213
213
  dependencies = [
214
214
  "chrono",
215
215
  "hmac",
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: edfarray
3
- Version: 1.2.0
3
+ Version: 1.2.2
4
4
  Requires-Dist: numpy>=2.0
5
5
  License-File: LICENSE
6
6
  Summary: A Rust-backed EDF/EDF+ file parser with numpy-like Python bindings
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "edfarray-core"
3
- version = "1.2.0"
3
+ version = "1.2.2"
4
4
  edition = "2024"
5
5
  description = "EDF/EDF+/BDF file parsing with memory-mapped and streaming reads"
6
6
  license = "MIT"
@@ -0,0 +1,33 @@
1
+ use crate::error::{EdfError, Result};
2
+
3
+ /// Fill a `Vec` with `len` copies of `value`, or return [`EdfError::MemoryRequest`] naming
4
+ /// `name` if the allocator refuses.
5
+ pub(crate) fn filled_vec<T: Clone>(len: usize, value: T, name: &'static str) -> Result<Vec<T>> {
6
+ let bytes = len as u128 * std::mem::size_of::<T>() as u128;
7
+ let mut buf: Vec<T> = Vec::new();
8
+ if buf.try_reserve_exact(len).is_err() {
9
+ return Err(EdfError::MemoryRequest {
10
+ name,
11
+ elements: len,
12
+ bytes,
13
+ });
14
+ }
15
+ buf.resize(len, value);
16
+ Ok(buf)
17
+ }
18
+
19
+ #[cfg(test)]
20
+ mod tests {
21
+ use super::*;
22
+
23
+ #[test]
24
+ fn small_allocation_succeeds() {
25
+ let buf = filled_vec(4, 1.0f64, "test").unwrap();
26
+ assert_eq!(buf, vec![1.0; 4]);
27
+ }
28
+
29
+ #[test]
30
+ fn empty_allocation_succeeds() {
31
+ assert!(filled_vec(0, 1.0f64, "test").unwrap().is_empty());
32
+ }
33
+ }
@@ -413,25 +413,29 @@ impl AuditReport {
413
413
 
414
414
  /// Derive identity terms (lowercased) from the patient name, patient code, technician, and
415
415
  /// admin code subfields.
416
+ /// Fall back to the tokens of the raw patient field when no subfield gives a term.
416
417
  fn identity_terms(header: &EdfHeader) -> Vec<String> {
417
418
  let mut terms: Vec<String> = Vec::new();
418
- let mut add_tokens = |value: Option<&str>, min_len: usize| {
419
- if let Some(value) = value {
420
- for token in value.split([' ', '_']) {
421
- let t = token.trim().to_lowercase();
422
- if t.len() >= min_len && t != "x" && !terms.contains(&t) {
423
- terms.push(t);
424
- }
425
- }
426
- }
427
- };
428
- add_tokens(header.patient.name.as_deref(), 3);
429
- add_tokens(header.patient.code.as_deref(), 4);
430
- add_tokens(header.recording.technician.as_deref(), 3);
431
- add_tokens(header.recording.admin_code.as_deref(), 4);
419
+ add_identity_tokens(&mut terms, header.patient.name.as_deref(), 3);
420
+ add_identity_tokens(&mut terms, header.patient.code.as_deref(), 4);
421
+ add_identity_tokens(&mut terms, header.recording.technician.as_deref(), 3);
422
+ add_identity_tokens(&mut terms, header.recording.admin_code.as_deref(), 4);
423
+ if terms.is_empty() {
424
+ add_identity_tokens(&mut terms, Some(&header.patient_id), 3);
425
+ }
432
426
  terms
433
427
  }
434
428
 
429
+ fn add_identity_tokens(terms: &mut Vec<String>, value: Option<&str>, min_len: usize) {
430
+ let Some(value) = value else { return };
431
+ for token in value.split([' ', '_']) {
432
+ let t = token.trim().to_lowercase();
433
+ if t.len() >= min_len && t != "x" && !terms.contains(&t) {
434
+ terms.push(t);
435
+ }
436
+ }
437
+ }
438
+
435
439
  /// Scan signal labels, transducers, prefilters, and annotation text for strings that repeat
436
440
  /// the patient identity currently stored in the header fields. This function does not write
437
441
  /// to the file.
@@ -731,8 +735,8 @@ fn generate_pseudonym(seed: &str, subject: &str) -> String {
731
735
  }
732
736
 
733
737
  /// Date shift in days, taken from `seed`. The hash is not stretched, on purpose. The shift has
734
- /// only `span` possible values, so an attacker can try all of them, whatever the seed hash is.
735
- /// A stretch will give no protection here and will double the cost of every call.
738
+ /// only `2 * span - 2` possible values, so an attacker can try all of them, whatever the seed
739
+ /// hash is. A stretch will give no protection here and will double the cost of every call.
736
740
  fn derive_shift_days(seed: &str) -> i32 {
737
741
  let digest = hmac_sha256(seed.as_bytes(), b"dates", b"");
738
742
  // +/-10 years, skipping 0 (a zero shift leaves dates untouched).
@@ -786,7 +790,7 @@ mod tests {
786
790
  ),
787
791
  record_duration_secs: 1.0,
788
792
  signals: vec![WriterSignal::new(
789
- "EEG Haagansen Fpz",
793
+ "EEG Haagansen",
790
794
  "uV",
791
795
  -3200.0,
792
796
  3200.0,
@@ -1079,6 +1083,51 @@ mod tests {
1079
1083
  assert_eq!(residual.hits.len(), 2);
1080
1084
  }
1081
1085
 
1086
+ #[test]
1087
+ fn audit_falls_back_to_raw_patient_field_tokens() {
1088
+ let dir = tempdir().unwrap();
1089
+ let path = dir.path().join("free.edf");
1090
+ let spec = WriterSpec {
1091
+ variant: EdfVariant::EdfPlusC,
1092
+ patient_id: "John Smith".into(),
1093
+ recording_id: "Startdate 01-JAN-2026 X X X".into(),
1094
+ start_datetime: NaiveDateTime::new(
1095
+ NaiveDate::from_ymd_opt(2026, 1, 1).unwrap(),
1096
+ NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
1097
+ ),
1098
+ record_duration_secs: 1.0,
1099
+ signals: vec![WriterSignal::new(
1100
+ "EEG Fpz", "uV", -3200.0, 3200.0, -32768, 32767, 32,
1101
+ )],
1102
+ annotation_bytes_per_record: None,
1103
+ record_onsets: None,
1104
+ };
1105
+ let anns = vec![Annotation {
1106
+ onset: 0.5,
1107
+ duration: None,
1108
+ text: "John Smith sat up".into(),
1109
+ }];
1110
+ write_edf(&path, spec, &[&vec![0.0f64; 64]], &anns).unwrap();
1111
+
1112
+ let report = audit(&path).unwrap();
1113
+ assert!(report.terms.contains(&"john".to_string()));
1114
+ assert!(report.terms.contains(&"smith".to_string()));
1115
+ assert!(!report.is_clean(), "unexpected clean: {:?}", report.hits);
1116
+
1117
+ let ann_hits: Vec<_> = report
1118
+ .hits
1119
+ .iter()
1120
+ .filter(|h| h.signal_index.is_none())
1121
+ .collect();
1122
+ assert_eq!(ann_hits.len(), 2);
1123
+ assert_eq!(ann_hits[0].term, "john");
1124
+ assert_eq!(ann_hits[1].term, "smith");
1125
+
1126
+ let result = anonymize(&path, &AnonymizeOptions::default()).unwrap();
1127
+ assert!(result.scrubbed_terms.contains(&"john".to_string()));
1128
+ assert!(result.scrubbed_terms.contains(&"smith".to_string()));
1129
+ }
1130
+
1082
1131
  #[test]
1083
1132
  fn audit_clean_on_scrubbed_file() {
1084
1133
  let dir = tempdir().unwrap();
@@ -166,6 +166,22 @@ pub fn plan_epochs(
166
166
  // a row is always grid index `first + j`, where `first` is the first sample at or after
167
167
  // the window start.
168
168
  let n_samples = first_sample_at_or_after((pre + post) * sample_rate).max(1) as usize;
169
+ // a row that cannot fit in the address space is a units typo, not a request for more RAM
170
+ let row_bytes = group
171
+ .indices()
172
+ .len()
173
+ .checked_mul(n_samples)
174
+ .and_then(|row| row.checked_mul(std::mem::size_of::<f64>()));
175
+ if row_bytes.is_none_or(|bytes| bytes > isize::MAX as usize) {
176
+ return Err(epoch_argument(
177
+ "pre/post",
178
+ format!(
179
+ "window width {n_samples} samples x {} channels does not fit in one output \
180
+ row; pre + post is too large",
181
+ group.indices().len()
182
+ ),
183
+ ));
184
+ }
169
185
  let n = n_samples as i64;
170
186
  let spr_i = spr as i64;
171
187
 
@@ -311,8 +327,22 @@ pub fn extract_epochs(
311
327
  _ => valid.clone(),
312
328
  };
313
329
 
314
- let row_size = nc * n;
315
- let mut data = vec![fill; kept.len() * row_size];
330
+ let row_size = nc.checked_mul(n).ok_or_else(|| {
331
+ epoch_argument(
332
+ "n_samples",
333
+ format!("{n} samples x {nc} channels overflows the row size"),
334
+ )
335
+ })?;
336
+ let total = kept.len().checked_mul(row_size).ok_or_else(|| {
337
+ epoch_argument(
338
+ "epochs",
339
+ format!(
340
+ "{} epochs x {row_size} samples per row overflows the output size",
341
+ kept.len()
342
+ ),
343
+ )
344
+ })?;
345
+ let mut data = crate::alloc::filled_vec(total, fill, "epochs")?;
316
346
 
317
347
  // Each task owns one row block (disjoint mutable slice), so the parallel closure never
318
348
  // shares `data`. Nothing to fill when no rows survived or the row width collapsed to zero.
@@ -487,6 +517,42 @@ mod tests {
487
517
  SignalGroup::from_indices(file.header(), indices).unwrap()
488
518
  }
489
519
 
520
+ #[test]
521
+ fn plan_rejects_absurd_window_width() {
522
+ let f = NamedTempFile::new().unwrap();
523
+ write_contig(f.path().to_str().unwrap(), 10, 10.0, 2);
524
+ let file = crate::file::EdfFile::open(f.path()).unwrap();
525
+ let group = group_of(&file, &[0, 1]);
526
+ let err = plan_epochs(&file, &group, &[1.0], 1e300, 1e300).unwrap_err();
527
+ assert!(
528
+ matches!(
529
+ err,
530
+ EdfError::InvalidArgument {
531
+ name: "pre/post",
532
+ ..
533
+ }
534
+ ),
535
+ "{err:?}"
536
+ );
537
+ }
538
+
539
+ #[test]
540
+ fn extract_epochs_absurd_width_raises_instead_of_aborting() {
541
+ let f = NamedTempFile::new().unwrap();
542
+ // 1000 events x 2 channels x 2e8 samples is about 3.2 TB
543
+ write_contig(f.path().to_str().unwrap(), 10, 10.0, 2);
544
+ let file = crate::file::EdfFile::open(f.path()).unwrap();
545
+ let group = group_of(&file, &[0, 1]);
546
+ let plan = plan_epochs(&file, &group, &vec![0.0; 1000], 1e7, 1e7).unwrap();
547
+ assert_eq!(plan.n_samples, 200_000_000);
548
+ let err = extract_epochs(&file, &group, &plan, EpochPad::Fill(PadMode::Nan)).unwrap_err();
549
+ assert!(matches!(err, EdfError::MemoryRequest { .. }), "{err:?}");
550
+
551
+ let (data, _, dropped) = extract_epochs(&file, &group, &plan, EpochPad::Drop).unwrap();
552
+ assert!(data.is_empty());
553
+ assert_eq!(dropped.len(), 1000);
554
+ }
555
+
490
556
  #[test]
491
557
  fn contiguous_window_math() {
492
558
  let f = NamedTempFile::new().unwrap();
@@ -74,6 +74,15 @@ pub enum EdfError {
74
74
 
75
75
  #[error("{name}: {reason}")]
76
76
  InvalidArgument { name: &'static str, reason: String },
77
+
78
+ /// The allocator refused a read or epoch buffer. Python raises it as `MemoryError`.
79
+ #[error("cannot allocate {bytes} bytes for {name} ({elements} elements)")]
80
+ MemoryRequest {
81
+ name: &'static str,
82
+ elements: usize,
83
+ // u128 because elements * size_of can exceed usize
84
+ bytes: u128,
85
+ },
77
86
  }
78
87
 
79
88
  pub type Result<T> = std::result::Result<T, EdfError>;