(
initial_train: &[usize],
test_indices: &[usize],
label_spans: &[(NaiveDateTime, NaiveDateTime)],
pct_embargo: f64,
n_samples: usize,
)
| 513 | } |
| 514 | |
| 515 | fn apply_purge_and_embargo( |
| 516 | initial_train: &[usize], |
| 517 | test_indices: &[usize], |
| 518 | label_spans: &[(NaiveDateTime, NaiveDateTime)], |
| 519 | pct_embargo: f64, |
| 520 | n_samples: usize, |
| 521 | ) -> (Vec<usize>, usize, usize) { |
| 522 | let mut train_mask = vec![false; n_samples]; |
| 523 | let mut initial_train_mask = vec![false; n_samples]; |
| 524 | for idx in initial_train { |
| 525 | train_mask[*idx] = true; |
| 526 | initial_train_mask[*idx] = true; |
| 527 | } |
| 528 | |
| 529 | let mut purged_count = 0; |
| 530 | for idx in initial_train { |
| 531 | let mut should_purge = false; |
| 532 | for test_idx in test_indices { |
| 533 | if overlaps(label_spans[*idx], label_spans[*test_idx]) { |
| 534 | should_purge = true; |
| 535 | break; |
| 536 | } |
| 537 | } |
| 538 | if should_purge { |
| 539 | train_mask[*idx] = false; |
| 540 | purged_count += 1; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | let embargo_width = (pct_embargo * n_samples as f64).ceil() as usize; |
| 545 | let mut embargoed = vec![false; n_samples]; |
| 546 | if embargo_width > 0 { |
| 547 | for test_idx in test_indices { |
| 548 | let start = test_idx.saturating_sub(embargo_width); |
| 549 | let stop = (*test_idx + embargo_width + 1).min(n_samples); |
| 550 | for idx in start..stop { |
| 551 | if initial_train_mask[idx] { |
| 552 | embargoed[idx] = true; |
| 553 | } |
| 554 | } |
| 555 | } |
| 556 | for idx in 0..n_samples { |
| 557 | if embargoed[idx] { |
| 558 | train_mask[idx] = false; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | let train_indices: Vec<usize> = train_mask |
| 564 | .iter() |
| 565 | .enumerate() |
| 566 | .filter_map(|(idx, keep)| if *keep { Some(idx) } else { None }) |
| 567 | .collect(); |
| 568 | let embargo_count = embargoed.into_iter().filter(|v| *v).count(); |
| 569 | |
| 570 | (train_indices, purged_count, embargo_count) |
| 571 | } |
| 572 |
no test coverage detected