pharmsol 0.27.1

Rust library for solving analytic and ode-defined pharmacometric models.
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
//! Comprehensive tests for NCA module
//!
//! Tests cover all major NCA parameters and edge cases.
//! All tests use Subject::builder() as the single entry point.

use super::observation::ObservationProfile;
use crate::data::Subject;
use crate::nca::calc::lambda_z_candidates;
use crate::nca::*;
use crate::Data;
use crate::SubjectBuilderExt;

// ============================================================================
// Test subject builders
// ============================================================================

/// Create a typical single-dose oral PK subject
fn single_dose_oral() -> Subject {
    Subject::builder("test")
        .bolus(0.0, 100.0, 0) // 100 mg to depot (extravascular)
        .observation(0.0, 0.0, 0)
        .observation(0.5, 5.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .observation(4.0, 4.0, 0)
        .observation(8.0, 2.0, 0)
        .observation(12.0, 1.0, 0)
        .observation(24.0, 0.25, 0)
        .build()
}

/// Create an IV bolus subject (high C0, dose to central)
fn iv_bolus_subject() -> Subject {
    Subject::builder("test")
        .bolus(0.0, 500.0, 1) // 500 mg to central (IV)
        .observation(0.0, 100.0, 0)
        .observation(0.25, 75.0, 0)
        .observation(0.5, 56.0, 0)
        .observation(1.0, 32.0, 0)
        .observation(2.0, 10.0, 0)
        .observation(4.0, 3.0, 0)
        .observation(8.0, 0.9, 0)
        .observation(12.0, 0.3, 0)
        .build()
}

/// Create an IV infusion subject
fn iv_infusion_subject() -> Subject {
    Subject::builder("test")
        .infusion(0.0, 100.0, 1, 0.5) // 100 mg over 0.5h to central
        .observation(0.0, 0.0, 0)
        .observation(0.5, 5.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .observation(4.0, 4.0, 0)
        .observation(8.0, 2.0, 0)
        .observation(12.0, 1.0, 0)
        .observation(24.0, 0.25, 0)
        .build()
}

/// Create a steady-state profile subject
fn steady_state_subject() -> Subject {
    Subject::builder("test")
        .bolus(0.0, 100.0, 0) // 100 mg oral
        .observation(0.0, 5.0, 0)
        .observation(1.0, 15.0, 0)
        .observation(2.0, 12.0, 0)
        .observation(4.0, 8.0, 0)
        .observation(6.0, 6.0, 0)
        .observation(8.0, 5.5, 0)
        .observation(12.0, 5.0, 0)
        .build()
}

/// Create a subject with BLQ values
fn blq_subject() -> Subject {
    use crate::Censor;

    Subject::builder("test")
        .bolus(0.0, 100.0, 0)
        .observation(0.0, 0.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .observation(4.0, 4.0, 0)
        .observation(8.0, 2.0, 0)
        .observation(12.0, 0.5, 0)
        .censored_observation(24.0, 0.1, 0, Censor::BLOQ) // BLQ with LOQ=0.1
        .build()
}

/// Create a minimal subject (no dose)
fn no_dose_subject() -> Subject {
    Subject::builder("test")
        .observation(0.0, 0.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .observation(4.0, 4.0, 0)
        .build()
}

// ============================================================================
// Basic NCA parameter tests
// ============================================================================

#[test]
fn test_nca_basic_exposure() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Check Cmax/Tmax
    assert_eq!(result.exposure.cmax, 10.0, "Cmax should be 10.0");
    assert_eq!(result.exposure.tmax, 1.0, "Tmax should be 1.0");

    // Check Clast/Tlast
    assert_eq!(result.exposure.clast, 0.25, "Clast should be 0.25");
    assert_eq!(result.exposure.tlast, 24.0, "Tlast should be 24.0");

    // AUClast should be positive
    assert!(result.exposure.auc_last > 0.0, "AUClast should be positive");
}

#[test]
fn test_nca_with_dose() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Should have clearance parameters if lambda-z was estimated
    if let Some(ref cl) = result.clearance {
        assert!(cl.cl_f > 0.0, "CL/F should be positive");
        assert!(cl.vz_f > 0.0, "Vz/F should be positive");
    }
}

#[test]
fn test_nca_without_dose() {
    let subject = no_dose_subject();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Exposure should still be computed
    assert!(result.exposure.cmax > 0.0);
    // But clearance should be None (no dose)
    assert!(result.clearance.is_none());
}

#[test]
fn test_nca_terminal_phase() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Check terminal phase was estimated
    assert!(
        result.terminal.is_some(),
        "Terminal phase should be estimated"
    );

    if let Some(ref term) = result.terminal {
        assert!(term.lambda_z > 0.0, "Lambda-z should be positive");
        assert!(term.half_life > 0.0, "Half-life should be positive");

        // Half-life relationship
        let expected_hl = std::f64::consts::LN_2 / term.lambda_z;
        assert!(
            (term.half_life - expected_hl).abs() < 1e-10,
            "Half-life = ln(2)/lambda_z"
        );
    }
}

// ============================================================================
// AUC calculation tests
// ============================================================================

#[test]
fn test_auc_linear_method() {
    let subject = single_dose_oral();
    let options = NCAOptions::default().with_auc_method(AUCMethod::Linear);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    assert!(result.exposure.auc_last > 0.0);
}

#[test]
fn test_auc_linuplogdown_method() {
    let subject = single_dose_oral();
    let options = NCAOptions::default().with_auc_method(AUCMethod::LinUpLogDown);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    assert!(result.exposure.auc_last > 0.0);
}

#[test]
fn test_auc_methods_differ() {
    let subject = single_dose_oral();

    let linear = NCAOptions::default().with_auc_method(AUCMethod::Linear);
    let logdown = NCAOptions::default().with_auc_method(AUCMethod::LinUpLogDown);

    let result_linear = subject.nca_all(&linear)[0]
        .as_ref()
        .unwrap()
        .exposure
        .auc_last;
    let result_logdown = subject.nca_all(&logdown)[0]
        .as_ref()
        .unwrap()
        .exposure
        .auc_last;

    // Methods should give slightly different results
    assert!(
        result_linear != result_logdown,
        "Different AUC methods should give different results"
    );
}

// ============================================================================
// Route-specific tests
// ============================================================================

#[test]
fn test_iv_bolus_route() {
    let subject = iv_bolus_subject();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Should have IV bolus parameters
    assert!(
        matches!(result.route_params, Some(RouteParams::IVBolus(_))),
        "IV bolus parameters should be present"
    );

    if let Some(RouteParams::IVBolus(ref bolus)) = result.route_params {
        assert!(bolus.c0 > 0.0, "C0 should be positive");
        assert!(bolus.vd > 0.0, "Vd should be positive");
    }
}

#[test]
fn test_iv_infusion_route() {
    let subject = iv_infusion_subject();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Should have IV infusion parameters
    assert!(
        matches!(result.route_params, Some(RouteParams::IVInfusion(_))),
        "IV infusion parameters should be present"
    );

    if let Some(RouteParams::IVInfusion(ref infusion)) = result.route_params {
        assert_eq!(
            infusion.infusion_duration, 0.5,
            "Infusion duration should be 0.5"
        );
    }
}

#[test]
fn test_extravascular_route() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Tlag should be in exposure params (may be None if no lag detected)
    // For extravascular, should have Extravascular route params
    assert!(
        matches!(result.route_params, Some(RouteParams::Extravascular)),
        "Extravascular route should not have IV-specific params"
    );
}

// ============================================================================
// Steady-state tests
// ============================================================================

#[test]
fn test_steady_state_parameters() {
    let subject = steady_state_subject();
    let options = NCAOptions::default().with_tau(12.0);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Should have steady-state parameters
    assert!(
        result.steady_state.is_some(),
        "Steady-state parameters should be present"
    );

    if let Some(ref ss) = result.steady_state {
        assert_eq!(ss.tau, 12.0, "Tau should be 12.0");
        assert!(ss.auc_tau > 0.0, "AUCtau should be positive");
        assert!(ss.cmin > 0.0, "Cmin should be positive");
        assert!(ss.cavg > 0.0, "Cavg should be positive");
        assert!(ss.fluctuation > 0.0, "Fluctuation should be positive");
    }
}

// ============================================================================
// BLQ handling tests
// ============================================================================

#[test]
fn test_blq_exclude() {
    let subject = blq_subject();
    let options = NCAOptions::default().with_blq_rule(BLQRule::Exclude);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Tlast should be at t=12 (last non-BLQ point)
    assert_eq!(result.exposure.tlast, 12.0, "Tlast should exclude BLQ");
}

#[test]
fn test_blq_zero() {
    let subject = blq_subject();
    let options = NCAOptions::default().with_blq_rule(BLQRule::Zero);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Should include the BLQ points as zeros
    assert!(result.exposure.auc_last > 0.0);
}

#[test]
fn test_blq_loq_over_2() {
    let subject = blq_subject();
    let options = NCAOptions::default().with_blq_rule(BLQRule::LoqOver2);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Should include the BLQ points as LOQ/2 (0.1 / 2 = 0.05)
    assert!(result.exposure.auc_last > 0.0);
}

// ============================================================================
// Lambda-z estimation tests
// ============================================================================

#[test]
fn test_lambda_z_auto_selection() {
    let subject = single_dose_oral();
    let options = NCAOptions::default().with_lambda_z(LambdaZOptions {
        method: LambdaZMethod::AdjR2,
        ..Default::default()
    });
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Should have terminal phase
    assert!(result.terminal.is_some());

    if let Some(ref term) = result.terminal {
        assert!(term.regression.is_some());
        if let Some(ref reg) = term.regression {
            assert!(reg.r_squared > 0.9, "R² should be high for good fit");
            assert!(reg.n_points >= 3, "Should use at least 3 points");
        }
    }
}

#[test]
fn test_lambda_z_manual_points() {
    let subject = single_dose_oral();
    let options = NCAOptions::default().with_lambda_z(LambdaZOptions {
        method: LambdaZMethod::Manual(4),
        ..Default::default()
    });
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    if let Some(ref term) = result.terminal {
        if let Some(ref reg) = term.regression {
            assert_eq!(reg.n_points, 4, "Should use exactly 4 points");
        }
    }
}

// ============================================================================
// Edge case tests
// ============================================================================

#[test]
fn test_insufficient_observations() {
    let subject = Subject::builder("test")
        .bolus(0.0, 100.0, 0)
        .observation(1.0, 10.0, 0)
        .build();

    let results = subject.nca_all(&NCAOptions::default());
    // Should fail with insufficient data
    assert!(
        results[0].is_err(),
        "Single observation should return error"
    );
}

#[test]
fn test_all_zero_concentrations() {
    let subject = Subject::builder("test")
        .bolus(0.0, 100.0, 0)
        .observation(0.0, 0.0, 0)
        .observation(1.0, 0.0, 0)
        .observation(2.0, 0.0, 0)
        .observation(4.0, 0.0, 0)
        .build();

    let results = subject.nca_all(&NCAOptions::default());
    assert!(results[0].is_err(), "All zero concentrations should fail");
}

// ============================================================================
// Quality/Warning tests
// ============================================================================

#[test]
fn test_quality_warnings_lambda_z() {
    // Profile with too few points for lambda-z
    let subject = Subject::builder("test")
        .bolus(0.0, 100.0, 0)
        .observation(0.0, 0.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .build();

    let results = subject.nca_all(&NCAOptions::default());
    let result = results[0].as_ref().unwrap();

    // Should have lambda-z warning
    assert!(
        result
            .quality
            .warnings
            .iter()
            .any(|w| matches!(w, Warning::LambdaZNotEstimable)),
        "Should warn about lambda-z"
    );
}

// ============================================================================
// Result conversion tests
// ============================================================================

#[test]
fn test_result_to_params() {
    let subject = single_dose_oral();
    let results = subject.nca_all(&NCAOptions::default());
    let result = results[0].as_ref().unwrap();

    let params = result.to_params();

    // Check key parameters are present
    assert!(params.contains_key("cmax"));
    assert!(params.contains_key("tmax"));
    assert!(params.contains_key("auc_last"));
}

#[test]
fn test_result_display() {
    let subject = single_dose_oral();
    let results = subject.nca_all(&NCAOptions::default());
    let result = results[0].as_ref().unwrap();

    let display = format!("{}", result);
    assert!(display.contains("Cmax"), "Display should contain Cmax");
    assert!(display.contains("AUC"), "Display should contain AUC");
}

// ============================================================================
// Subject/Occasion identification tests
// ============================================================================

#[test]
fn test_result_subject_id() {
    let subject = Subject::builder("patient_001")
        .bolus(0.0, 100.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .observation(4.0, 4.0, 0)
        .observation(8.0, 2.0, 0)
        .build();

    let results = subject.nca_all(&NCAOptions::default());
    let result = results[0].as_ref().unwrap();

    assert_eq!(result.subject_id.as_deref(), Some("patient_001"));
    assert_eq!(result.occasion, Some(0));
}

// ============================================================================
// Presets tests
// ============================================================================

#[test]
fn test_bioequivalence_preset() {
    let options = NCAOptions::bioequivalence();
    assert_eq!(options.lambda_z.min_r_squared, 0.90);
    assert_eq!(options.max_auc_extrap_pct, 20.0);
}

#[test]
fn test_sparse_preset() {
    let options = NCAOptions::sparse();
    assert_eq!(options.lambda_z.min_r_squared, 0.80);
    assert_eq!(options.max_auc_extrap_pct, 30.0);
}

// ============================================================================
// Partial AUC tests
// ============================================================================

#[test]
fn test_partial_auc_interval() {
    let subject = single_dose_oral();
    let options = NCAOptions::default().with_auc_interval(0.0, 4.0);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Partial AUC should be calculated
    assert!(
        result.exposure.auc_partial.is_some(),
        "Partial AUC should be computed when interval specified"
    );

    let auc_partial = result.exposure.auc_partial.unwrap();
    assert!(auc_partial > 0.0, "Partial AUC should be positive");

    // Partial AUC (0-4h) should be less than AUClast (0-24h)
    assert!(
        auc_partial < result.exposure.auc_last,
        "Partial AUC should be less than AUClast"
    );
}

#[test]
fn test_positional_blq_rule() {
    use crate::Censor;

    // Create subject with BLQ at start, middle, and end
    let subject = Subject::builder("test")
        .bolus(0.0, 100.0, 0)
        .censored_observation(0.0, 0.1, 0, Censor::BLOQ) // First - keep as 0
        .observation(1.0, 10.0, 0)
        .censored_observation(2.0, 0.1, 0, Censor::BLOQ) // Middle - drop
        .observation(4.0, 4.0, 0)
        .observation(8.0, 2.0, 0)
        .censored_observation(12.0, 0.1, 0, Censor::BLOQ) // Last - keep as 0
        .build();

    // With positional BLQ handling
    let options = NCAOptions::default().with_blq_rule(BLQRule::Positional);
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();

    // Middle BLQ at t=2 should be dropped, but first and last kept as 0 (PKNCA behavior)
    // With last BLQ kept as 0 (not LOQ), tlast remains at 8.0 (last positive conc)
    assert_eq!(result.exposure.cmax, 10.0, "Cmax should be 10.0");
    // tlast is the last time with positive concentration (8.0), the BLQ at 12 is 0
    assert_eq!(
        result.exposure.tlast, 8.0,
        "Tlast should be 8.0 (last positive concentration)"
    );
    assert_eq!(
        result.exposure.clast, 2.0,
        "Clast should be 2.0 (last positive value)"
    );
}

// ============================================================================
// Lambda-z Candidates API tests
// ============================================================================

#[test]
fn test_lambda_z_candidates_returns_multiple() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();
    let auc_last = result.exposure.auc_last;

    // Get ObservationProfile for the first occasion
    let occasion = &subject.occasions()[0];
    let profile = ObservationProfile::from_occasion(occasion, 0, &options.blq_rule).unwrap();

    let candidates = lambda_z_candidates(&profile, &options.lambda_z, auc_last);
    assert!(
        candidates.len() >= 2,
        "Should produce multiple candidates, got {}",
        candidates.len()
    );

    // Exactly one should be selected
    let selected_count = candidates.iter().filter(|c| c.is_selected).count();
    assert_eq!(
        selected_count, 1,
        "Exactly one candidate should be selected"
    );
}

#[test]
fn test_lambda_z_candidates_selected_matches_nca_result() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let result = results[0].as_ref().unwrap();
    let auc_last = result.exposure.auc_last;

    let occasion = &subject.occasions()[0];
    let profile = ObservationProfile::from_occasion(occasion, 0, &options.blq_rule).unwrap();

    let candidates = lambda_z_candidates(&profile, &options.lambda_z, auc_last);
    let selected = candidates.iter().find(|c| c.is_selected).unwrap();

    // Selected candidate's lambda_z should match what NCA computed
    let terminal = result.terminal.as_ref().unwrap();
    let rel_diff = (selected.lambda_z - terminal.lambda_z).abs() / terminal.lambda_z;
    assert!(
        rel_diff < 1e-10,
        "Selected λz ({}) should match NCA result ({})",
        selected.lambda_z,
        terminal.lambda_z
    );

    // Half-life should also match
    let hl_diff = (selected.half_life - terminal.half_life).abs() / terminal.half_life;
    assert!(
        hl_diff < 1e-10,
        "Selected t½ ({}) should match NCA result ({})",
        selected.half_life,
        terminal.half_life
    );
}

#[test]
fn test_lambda_z_candidates_all_have_positive_lambda_z() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let auc_last = results[0].as_ref().unwrap().exposure.auc_last;

    let occasion = &subject.occasions()[0];
    let profile = ObservationProfile::from_occasion(occasion, 0, &options.blq_rule).unwrap();

    let candidates = lambda_z_candidates(&profile, &options.lambda_z, auc_last);
    for c in &candidates {
        assert!(c.lambda_z > 0.0, "λz must be positive, got {}", c.lambda_z);
        assert!(
            c.half_life > 0.0,
            "t½ must be positive, got {}",
            c.half_life
        );
        assert!(c.n_points >= 3, "Must have at least 3 points");
        assert!(c.r_squared >= 0.0 && c.r_squared <= 1.0, "R² out of range");
    }
}

#[test]
fn test_lambda_z_candidates_empty_for_insufficient_points() {
    // Subject with too few observations for terminal regression
    let subject = Subject::builder("short")
        .bolus(0.0, 100.0, 0)
        .observation(0.0, 0.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 5.0, 0)
        .build();

    let options = NCAOptions::default();
    let occasion = &subject.occasions()[0];

    if let Ok(profile) = ObservationProfile::from_occasion(occasion, 0, &options.blq_rule) {
        let candidates = lambda_z_candidates(&profile, &options.lambda_z, 10.0);
        // Either empty or no selected candidate (not enough points after Cmax)
        let selected = candidates.iter().filter(|c| c.is_selected).count();
        assert!(
            candidates.is_empty() || selected == 0,
            "Should have no selected candidate with insufficient terminal points"
        );
    }
}

#[test]
fn test_lambda_z_candidates_span_ratio_and_extrap() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let results = subject.nca_all(&options);
    let auc_last = results[0].as_ref().unwrap().exposure.auc_last;

    let occasion = &subject.occasions()[0];
    let profile = ObservationProfile::from_occasion(occasion, 0, &options.blq_rule).unwrap();

    let candidates = lambda_z_candidates(&profile, &options.lambda_z, auc_last);
    for c in &candidates {
        // span_ratio = (end_time - start_time) / half_life
        let expected_span = (c.end_time - c.start_time) / c.half_life;
        let diff = (c.span_ratio - expected_span).abs();
        assert!(
            diff < 1e-10,
            "Span ratio mismatch: {} vs expected {}",
            c.span_ratio,
            expected_span
        );

        // auc_inf should be >= auc_last
        assert!(
            c.auc_inf >= auc_last,
            "AUC∞ ({}) should be >= AUClast ({})",
            c.auc_inf,
            auc_last
        );

        // extrap pct should be 0..100
        assert!(
            c.auc_pct_extrap >= 0.0 && c.auc_pct_extrap <= 100.0,
            "Extrap % ({}) out of range",
            c.auc_pct_extrap
        );
    }
}

// ============================================================================
// Phase 8: nca() / nca_all() and to_row() tests
// ============================================================================

#[test]
fn test_nca_returns_single_result() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let result = subject.nca(&options);
    assert!(result.is_ok(), "nca() should succeed for a valid subject");
    let r = result.unwrap();
    assert!(r.exposure.cmax > 0.0);
    assert_eq!(r.subject_id.as_deref(), Some("test"));
}

#[test]
fn test_nca_matches_nca_all_vec() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();

    let first = subject.nca(&options).unwrap();
    let vec_result = subject.nca_all(&options);
    let vec_first = vec_result[0].as_ref().unwrap();

    assert!((first.exposure.cmax - vec_first.exposure.cmax).abs() < 1e-10);
    assert!((first.exposure.auc_last - vec_first.exposure.auc_last).abs() < 1e-10);
}

#[test]
fn test_nca_error_on_empty_outeq() {
    // A subject with no observations for outeq=99
    let subject = Subject::builder("empty")
        .bolus(0.0, 100.0, 0)
        .observation(1.0, 10.0, 0)
        .build();
    let options = NCAOptions::default().with_outeq(99);
    let result = subject.nca(&options);
    assert!(result.is_err(), "nca() should fail for missing outeq");
}

#[test]
fn test_to_row_contains_expected_keys() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let result = subject.nca(&options).unwrap();
    let row = result.to_row();

    let keys: Vec<&str> = row.iter().map(|(k, _)| *k).collect();
    assert!(keys.contains(&"cmax"), "to_row should contain cmax");
    assert!(keys.contains(&"tmax"), "to_row should contain tmax");
    assert!(keys.contains(&"auc_last"), "to_row should contain auc_last");
    assert!(keys.contains(&"clast"), "to_row should contain clast");
    assert!(keys.contains(&"tlast"), "to_row should contain tlast");
}

#[test]
fn test_to_row_values_match_result() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let result = subject.nca(&options).unwrap();
    let row = result.to_row();

    let find =
        |key: &str| -> Option<f64> { row.iter().find(|(k, _)| *k == key).and_then(|(_, v)| *v) };

    assert!((find("cmax").unwrap() - result.exposure.cmax).abs() < 1e-10);
    assert!((find("tmax").unwrap() - result.exposure.tmax).abs() < 1e-10);
    assert!((find("auc_last").unwrap() - result.exposure.auc_last).abs() < 1e-10);
}

#[test]
fn test_to_row_terminal_params_present_when_lambda_z_succeeds() {
    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let result = subject.nca(&options).unwrap();

    // Verify terminal phase succeeded
    assert!(
        result.terminal.is_some(),
        "Expected terminal phase to succeed"
    );

    let row = result.to_row();
    let find =
        |key: &str| -> Option<f64> { row.iter().find(|(k, _)| *k == key).and_then(|(_, v)| *v) };

    assert!(
        find("lambda_z").is_some(),
        "to_row should have lambda_z when terminal succeeds"
    );
    assert!(
        find("half_life").is_some(),
        "to_row should have half_life when terminal succeeds"
    );
}

// ============================================================================
// Phase 9: ObservationProfile NCA tests
// ============================================================================

#[test]
fn test_nca_with_dose_matches_subject() {
    use crate::data::Route;

    let subject = single_dose_oral();
    let options = NCAOptions::default();
    let subject_result = subject.nca(&options).unwrap();

    // Build a profile using the same data as single_dose_oral() but without embedded dose
    let profile = Subject::builder("profile")
        .observation(0.0, 0.0, 0)
        .observation(0.5, 5.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .observation(4.0, 4.0, 0)
        .observation(8.0, 2.0, 0)
        .observation(12.0, 1.0, 0)
        .observation(24.0, 0.25, 0)
        .build();
    let profile_result = profile
        .nca_with_dose(100.0, Route::Extravascular, None, &options)
        .unwrap();

    // Cmax and tmax should match exactly (same data, same filtering)
    assert!(
        (subject_result.exposure.cmax - profile_result.exposure.cmax).abs() < 1e-10,
        "Cmax should match"
    );
    assert!(
        (subject_result.exposure.tmax - profile_result.exposure.tmax).abs() < 1e-10,
        "Tmax should match"
    );
    // AUClast should be very close (tlag may differ slightly)
    assert!(
        (subject_result.exposure.auc_last - profile_result.exposure.auc_last).abs()
            / subject_result.exposure.auc_last
            < 0.01,
        "AUClast should be within 1%"
    );
}

#[test]
fn test_nca_with_dose_no_dose() {
    use crate::data::Route;

    let profile = Subject::builder("profile")
        .observation(0.0, 0.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(4.0, 5.0, 0)
        .observation(8.0, 1.0, 0)
        .build();
    let options = NCAOptions::default().with_route(Route::Extravascular);
    let result = profile.nca(&options).unwrap();

    // Should work but dose-normalized params should be None
    assert!(result.exposure.cmax > 0.0);
    assert!(result.exposure.cmax_dn.is_none());
}

// ============================================================================
// Phase 10: Population error isolation (Task 4.5)
// ============================================================================

#[test]
fn test_population_error_isolation() {
    // Create a population: one good subject, one with no observations (will fail)
    let good = Subject::builder("good")
        .bolus(0.0, 100.0, 0)
        .observation(1.0, 10.0, 0)
        .observation(2.0, 8.0, 0)
        .observation(4.0, 4.0, 0)
        .observation(8.0, 2.0, 0)
        .build();

    let bad = Subject::builder("bad")
        .bolus(0.0, 100.0, 0)
        // No observations → will fail
        .build();

    let data = Data::new(vec![good, bad]);
    let opts = NCAOptions::default();
    let grouped = data.nca_grouped(&opts);

    assert_eq!(grouped.len(), 2);

    // Good subject
    let good_result = grouped.iter().find(|r| r.subject_id == "good").unwrap();
    assert_eq!(good_result.successes().len(), 1);
    assert_eq!(good_result.errors().len(), 0);

    // Bad subject
    let bad_result = grouped.iter().find(|r| r.subject_id == "bad").unwrap();
    assert_eq!(bad_result.successes().len(), 0);
    assert_eq!(bad_result.errors().len(), 1);

    // nca_all() should have both success and failure
    let all = data.nca_all(&opts);
    let ok_count = all.iter().filter(|r| r.is_ok()).count();
    let err_count = all.iter().filter(|r| r.is_err()).count();
    assert_eq!(ok_count, 1);
    assert_eq!(err_count, 1);
}