pharmsol 0.26.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
//! Row representation of [Data] for flexible parsing
//!
//! # Example
//!
//! ```rust
//! use pharmsol::data::parser::DataRow;
//!
//! // Create a dosing row with ADDL expansion
//! let row = DataRow::builder("subject_1", 0.0)
//!     .evid(1)
//!     .dose(100.0)
//!     .input(1)
//!     .addl(3)   // 3 additional doses
//!     .ii(12.0)  // 12 hours apart
//!     .build();
//!
//! let events = row.into_events().unwrap();
//! assert_eq!(events.len(), 4); // Original + 3 additional doses
//! ```
//!

use crate::data::*;
use std::collections::HashMap;
use thiserror::Error;

/// A format-agnostic representation of a single data row
///
/// This struct represents the canonical fields needed to create pharmsol Events.
/// Consumers construct this from their source data (regardless of column names),
/// then call [`into_events()`](DataRow::into_events) to get properly parsed
/// Events with full ADDL expansion, EVID handling, censoring, etc.
///
/// # Fields
///
/// All fields use Pmetrics conventions:
/// - `input` and `outeq` are **1-indexed** (kept as-is, user must size arrays accordingly)
/// - `evid`: 0=observation, 1=dose, 4=reset/new occasion
/// - `addl`: positive=forward in time, negative=backward in time
///
/// # Example
///
/// ```rust
/// use pharmsol::data::parser::DataRow;
///
/// // Observation row
/// let obs = DataRow::builder("pt1", 1.0)
///     .evid(0)
///     .out(25.5)
///     .outeq(1)
///     .build();
///
/// // Dosing row with negative ADDL (doses before time 0)
/// let dose = DataRow::builder("pt1", 0.0)
///     .evid(1)
///     .dose(100.0)
///     .input(1)
///     .addl(-10)  // 10 doses BEFORE time 0
///     .ii(12.0)
///     .build();
///
/// let events = dose.into_events().unwrap();
/// // Events at times: -120, -108, -96, ..., -12, 0
/// assert_eq!(events.len(), 11);
/// ```
#[derive(Debug, Clone, Default)]
pub struct DataRow {
    /// Subject identifier (required)
    pub id: String,
    /// Event time (required)
    pub time: f64,
    /// Event type: 0=observation, 1=dose, 4=reset/new occasion
    pub evid: i32,
    /// Dose amount (for EVID=1)
    pub dose: Option<f64>,
    /// Infusion duration (if > 0, dose is infusion; otherwise bolus)
    pub dur: Option<f64>,
    /// Additional doses count (positive=forward, negative=backward in time)
    pub addl: Option<i64>,
    /// Interdose interval for ADDL
    pub ii: Option<f64>,
    /// Input compartment
    pub input: Option<usize>,
    /// Observed value (for EVID=0)
    pub out: Option<f64>,
    /// Output equation number
    pub outeq: Option<usize>,
    /// Censoring indicator
    pub cens: Option<Censor>,
    /// Error polynomial coefficients
    pub c0: Option<f64>,
    /// Error polynomial coefficients
    pub c1: Option<f64>,
    /// Error polynomial coefficients
    pub c2: Option<f64>,
    /// Error polynomial coefficients
    pub c3: Option<f64>,
    /// Covariate values at this time point
    pub covariates: HashMap<String, f64>,
}

impl DataRow {
    /// Create a new builder for constructing a DataRow
    ///
    /// # Arguments
    ///
    /// * `id` - Subject identifier
    /// * `time` - Event time
    ///
    /// # Example
    ///
    /// ```rust
    /// use pharmsol::data::parser::DataRow;
    ///
    /// let row = DataRow::builder("patient_001", 0.0)
    ///     .evid(1)
    ///     .dose(100.0)
    ///     .input(1)
    ///     .build();
    /// ```
    pub fn builder(id: impl Into<String>, time: f64) -> DataRowBuilder {
        DataRowBuilder::new(id, time)
    }

    /// Get error polynomial if all coefficients are present
    fn get_errorpoly(&self) -> Option<ErrorPoly> {
        match (self.c0, self.c1, self.c2, self.c3) {
            (Some(c0), Some(c1), Some(c2), Some(c3)) => Some(ErrorPoly::new(c0, c1, c2, c3)),
            _ => None,
        }
    }

    /// Convert this row into pharmsol Events
    ///
    /// This method contains all the complex parsing logic:
    /// - EVID interpretation (0=observation, 1=dose, 4=reset)
    /// - ADDL/II expansion (both positive and negative directions)
    /// - Infusion vs bolus detection based on DUR
    /// - Censoring and error polynomial handling
    ///
    /// # ADDL Expansion
    ///
    /// When `addl` and `ii` are both specified:
    /// - **Positive ADDL**: Additional doses are placed *after* the base time
    ///   - Example: time=0, addl=3, ii=12 → doses at 12, 24, 36, then 0
    /// - **Negative ADDL**: Additional doses are placed *before* the base time
    ///   - Example: time=0, addl=-3, ii=12 → doses at -36, -24, -12, then 0
    ///
    /// # Returns
    ///
    /// A vector of Events. A single row may produce multiple events when ADDL is used.
    ///
    /// # Errors
    ///
    /// Returns [`DataError`] if required fields are missing for the given EVID:
    /// - EVID=0: Requires `outeq`
    /// - EVID=1: Requires `dose` and `input`; if `dur > 0`, it's an infusion
    ///
    /// # Example
    ///
    /// ```rust
    /// use pharmsol::data::parser::DataRow;
    ///
    /// let row = DataRow::builder("pt1", 0.0)
    ///     .evid(1)
    ///     .dose(100.0)
    ///     .input(1)
    ///     .addl(2)
    ///     .ii(24.0)
    ///     .build();
    ///
    /// let events = row.into_events().unwrap();
    /// assert_eq!(events.len(), 3); // doses at 24, 48, and 0
    ///
    /// let times: Vec<f64> = events.iter().map(|e| e.time()).collect();
    /// assert_eq!(times, vec![24.0, 48.0, 0.0]);
    /// ```
    pub fn into_events(self) -> Result<Vec<Event>, DataError> {
        let mut events: Vec<Event> = Vec::new();

        match self.evid {
            0 => {
                // Observation event
                events.push(Event::Observation(Observation::new(
                    self.time,
                    self.out,
                    self.outeq
                        .ok_or_else(|| DataError::MissingObservationOuteq {
                            id: self.id.clone(),
                            time: self.time,
                        })?, // Keep 1-indexed as provided by Pmetrics
                    self.get_errorpoly(),
                    0, // occasion set later
                    self.cens.unwrap_or(Censor::None),
                )));
            }
            1 | 4 => {
                // Dosing event (1) or reset with dose (4)

                let input = self.input.ok_or_else(|| DataError::MissingBolusInput {
                    id: self.id.clone(),
                    time: self.time,
                })?; // Keep 1-indexed as provided by Pmetrics

                let event = if self.dur.unwrap_or(0.0) > 0.0 {
                    // Infusion
                    Event::Infusion(Infusion::new(
                        self.time,
                        self.dose.ok_or_else(|| DataError::MissingInfusionDose {
                            id: self.id.clone(),
                            time: self.time,
                        })?,
                        input,
                        self.dur.ok_or_else(|| DataError::MissingInfusionDur {
                            id: self.id.clone(),
                            time: self.time,
                        })?,
                        0,
                    ))
                } else {
                    // Bolus
                    Event::Bolus(Bolus::new(
                        self.time,
                        self.dose.ok_or_else(|| DataError::MissingBolusDose {
                            id: self.id.clone(),
                            time: self.time,
                        })?,
                        input,
                        0,
                    ))
                };

                // Handle ADDL/II expansion
                if let (Some(addl), Some(ii)) = (self.addl, self.ii) {
                    if addl != 0 && ii > 0.0 {
                        let mut ev = event.clone();
                        let interval = ii.abs();
                        let repetitions = addl.abs();
                        let direction = addl.signum() as f64;

                        for _ in 0..repetitions {
                            ev.inc_time(direction * interval);
                            events.push(ev.clone());
                        }
                    }
                }

                events.push(event);
            }
            _ => {
                return Err(DataError::UnknownEvid {
                    evid: self.evid as isize,
                    id: self.id.clone(),
                    time: self.time,
                });
            }
        }

        Ok(events)
    }

    /// Get the covariate values for this row
    ///
    /// Returns a reference to the HashMap of covariate name → value pairs.
    pub fn covariates(&self) -> &HashMap<String, f64> {
        &self.covariates
    }

    /// Check if this row represents a new occasion (EVID=4)
    pub fn is_occasion_reset(&self) -> bool {
        self.evid == 4
    }

    /// Get the subject ID
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Get the event time
    pub fn time(&self) -> f64 {
        self.time
    }
}

/// Builder for constructing DataRow with a fluent API
///
/// # Example
///
/// ```rust
/// use pharmsol::data::parser::DataRow;
/// use pharmsol::data::Censor;
///
/// let row = DataRow::builder("patient_001", 1.5)
///     .evid(0)
///     .out(25.5)
///     .outeq(1)
///     .cens(Censor::None)
///     .covariate("weight", 70.0)
///     .covariate("age", 45.0)
///     .build();
/// ```
#[derive(Debug, Clone)]
pub struct DataRowBuilder {
    row: DataRow,
}

impl DataRowBuilder {
    /// Create a new builder with required fields
    ///
    /// # Arguments
    ///
    /// * `id` - Subject identifier
    /// * `time` - Event time
    pub fn new(id: impl Into<String>, time: f64) -> Self {
        Self {
            row: DataRow {
                id: id.into(),
                time,
                evid: 0, // Default to observation
                ..Default::default()
            },
        }
    }

    /// Set the event type
    ///
    /// # Arguments
    ///
    /// * `evid` - Event ID: 0=observation, 1=dose, 4=reset/new occasion
    pub fn evid(mut self, evid: i32) -> Self {
        self.row.evid = evid;
        self
    }

    /// Set the dose amount
    ///
    /// Required for EVID=1 (dosing events).
    pub fn dose(mut self, dose: f64) -> Self {
        self.row.dose = Some(dose);
        self
    }

    /// Set the infusion duration
    ///
    /// If > 0, the dose is treated as an infusion rather than a bolus.
    pub fn dur(mut self, dur: f64) -> Self {
        self.row.dur = Some(dur);
        self
    }

    /// Set the additional doses count
    ///
    /// # Arguments
    ///
    /// * `addl` - Number of additional doses
    ///   - Positive: doses placed after the base time
    ///   - Negative: doses placed before the base time
    pub fn addl(mut self, addl: i64) -> Self {
        self.row.addl = Some(addl);
        self
    }

    /// Set the interdose interval
    ///
    /// Used with ADDL to specify time between additional doses.
    pub fn ii(mut self, ii: f64) -> Self {
        self.row.ii = Some(ii);
        self
    }

    /// Set the input compartment (1-indexed)
    ///
    /// Required for EVID=1 (dosing events).
    /// Kept as 1-indexed; user must size state arrays accordingly.
    pub fn input(mut self, input: usize) -> Self {
        self.row.input = Some(input);
        self
    }

    /// Set the observed value
    ///
    /// Used for EVID=0 (observation events).
    pub fn out(mut self, out: f64) -> Self {
        self.row.out = Some(out);
        self
    }

    /// Set the output equation (1-indexed)
    ///
    /// Required for EVID=0 (observation events).
    /// Will be converted to 0-indexed internally.
    pub fn outeq(mut self, outeq: usize) -> Self {
        self.row.outeq = Some(outeq);
        self
    }

    /// Set the censoring type
    pub fn cens(mut self, cens: Censor) -> Self {
        self.row.cens = Some(cens);
        self
    }

    /// Set error polynomial coefficients
    ///
    /// The error polynomial models observation error as:
    /// SD = c0 + c1*Y + c2*Y² + c3*Y³
    pub fn error_poly(mut self, c0: f64, c1: f64, c2: f64, c3: f64) -> Self {
        self.row.c0 = Some(c0);
        self.row.c1 = Some(c1);
        self.row.c2 = Some(c2);
        self.row.c3 = Some(c3);
        self
    }

    /// Add a covariate value
    ///
    /// Can be called multiple times to add multiple covariates.
    ///
    /// # Arguments
    ///
    /// * `name` - Covariate name
    /// * `value` - Covariate value at this time point
    pub fn covariate(mut self, name: impl Into<String>, value: f64) -> Self {
        self.row.covariates.insert(name.into(), value);
        self
    }

    /// Build the DataRow
    pub fn build(self) -> DataRow {
        self.row
    }
}

/// Build a [Data] object from an iterator of [DataRow]s
///
/// This function handles all the complex assembly logic:
/// - Groups rows by subject ID
/// - Splits into occasions at EVID=4 boundaries
/// - Converts rows to events via [`DataRow::into_events()`]
/// - Builds covariates from row covariate data
///
/// # Example
///
/// ```rust
/// use pharmsol::data::parser::{DataRow, build_data};
///
/// let rows = vec![
///     // Subject 1, Occasion 0
///     DataRow::builder("pt1", 0.0)
///         .evid(1).dose(100.0).input(1).build(),
///     DataRow::builder("pt1", 1.0)
///         .evid(0).out(50.0).outeq(1).build(),
///     // Subject 1, Occasion 1 (EVID=4 starts new occasion)
///     DataRow::builder("pt1", 24.0)
///         .evid(4).dose(100.0).input(1).build(),
///     DataRow::builder("pt1", 25.0)
///         .evid(0).out(48.0).outeq(1).build(),
///     // Subject 2
///     DataRow::builder("pt2", 0.0)
///         .evid(1).dose(50.0).input(1).build(),
/// ];
///
/// let data = build_data(rows).unwrap();
/// assert_eq!(data.subjects().len(), 2);
/// ```
pub fn build_data(rows: impl IntoIterator<Item = DataRow>) -> Result<Data, DataError> {
    // Group rows by subject ID
    let mut rows_map: std::collections::HashMap<String, Vec<DataRow>> =
        std::collections::HashMap::new();
    for row in rows {
        rows_map.entry(row.id.clone()).or_default().push(row);
    }

    let mut subjects: Vec<Subject> = Vec::new();

    for (id, rows) in rows_map {
        // Split rows into occasion blocks at EVID=4 boundaries
        let split_indices: Vec<usize> = rows
            .iter()
            .enumerate()
            .filter_map(|(i, row)| if row.evid == 4 { Some(i) } else { None })
            .collect();

        let mut block_rows_vec: Vec<&[DataRow]> = Vec::new();
        let mut start = 0;
        for &split_index in &split_indices {
            if start < split_index {
                block_rows_vec.push(&rows[start..split_index]);
            }
            start = split_index;
        }
        if start < rows.len() {
            block_rows_vec.push(&rows[start..]);
        }

        // Build occasions
        let mut occasions: Vec<Occasion> = Vec::new();
        for (block_index, block) in block_rows_vec.iter().enumerate() {
            let mut events: Vec<Event> = Vec::new();

            // Collect covariate observations for this block
            let mut observed_covariates: std::collections::HashMap<
                String,
                Vec<(f64, Option<f64>)>,
            > = std::collections::HashMap::new();

            for row in *block {
                // Parse events
                let row_events = row.clone().into_events()?;
                events.extend(row_events);

                // Collect covariates
                for (name, value) in &row.covariates {
                    observed_covariates
                        .entry(name.clone())
                        .or_default()
                        .push((row.time, Some(*value)));
                }
            }

            // Set occasion index on all events
            events.iter_mut().for_each(|e| e.set_occasion(block_index));

            // Build covariates
            let covariates = Covariates::from_pmetrics_observations(&observed_covariates);

            // Create occasion
            let mut occasion = Occasion::new(block_index);
            occasion.events = events;
            occasion.covariates = covariates;
            occasion.sort();
            occasions.push(occasion);
        }

        subjects.push(Subject::new(id, occasions));
    }

    // Sort subjects alphabetically by ID for consistent ordering
    subjects.sort_by(|a, b| a.id().cmp(b.id()));

    Ok(Data::new(subjects))
}

/// Custom error type for the module
#[allow(private_interfaces)]
#[derive(Error, Debug, Clone)]
pub enum DataError {
    /// Error encountered when reading CSV data
    #[error("CSV error: {0}")]
    CSVError(String),
    /// Error during data deserialization
    #[error("Parse error: {0}")]
    SerdeError(String),
    /// Encountered an unknown EVID value
    #[error("Unknown EVID: {evid} for ID {id} at time {time}")]
    UnknownEvid { evid: isize, id: String, time: f64 },
    /// Required observation value (OUT) is missing
    #[error("Observation OUT is missing for {id} at time {time}")]
    MissingObservationOut { id: String, time: f64 },
    /// Required observation output equation (OUTEQ) is missing
    #[error("Observation OUTEQ is missing in for {id} at time {time}")]
    MissingObservationOuteq { id: String, time: f64 },
    /// Required infusion dose amount is missing
    #[error("Infusion amount (DOSE) is missing for {id} at time {time}")]
    MissingInfusionDose { id: String, time: f64 },
    /// Required infusion input compartment is missing
    #[error("Infusion compartment (INPUT) is missing for {id} at time {time}")]
    MissingInfusionInput { id: String, time: f64 },
    /// Required infusion duration is missing
    #[error("Infusion duration (DUR) is missing for {id} at time {time}")]
    MissingInfusionDur { id: String, time: f64 },
    /// Required bolus dose amount is missing
    #[error("Bolus amount (DOSE) is missing for {id} at time {time}")]
    MissingBolusDose { id: String, time: f64 },
    /// Required bolus input compartment is missing
    #[error("Bolus compartment (INPUT) is missing for {id} at time {time}")]
    MissingBolusInput { id: String, time: f64 },
}

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

    #[test]
    fn test_observation_row() {
        let row = DataRow::builder("pt1", 1.0)
            .evid(0)
            .out(25.5)
            .outeq(1)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 1);

        match &events[0] {
            Event::Observation(obs) => {
                assert_eq!(obs.time(), 1.0);
                assert_eq!(obs.value(), Some(25.5));
                assert_eq!(obs.outeq(), 1); // Kept as 1-indexed
            }
            _ => panic!("Expected observation event"),
        }
    }

    #[test]
    fn test_bolus_row() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .input(1)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 1);

        match &events[0] {
            Event::Bolus(bolus) => {
                assert_eq!(bolus.time(), 0.0);
                assert_eq!(bolus.amount(), 100.0);
                assert_eq!(bolus.input(), 1); // Kept as 1-indexed
            }
            _ => panic!("Expected bolus event"),
        }
    }

    #[test]
    fn test_infusion_row() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .dur(2.0)
            .input(1)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 1);

        match &events[0] {
            Event::Infusion(inf) => {
                assert_eq!(inf.time(), 0.0);
                assert_eq!(inf.amount(), 100.0);
                assert_eq!(inf.duration(), 2.0);
                assert_eq!(inf.input(), 1); // Kept as 1-indexed
            }
            _ => panic!("Expected infusion event"),
        }
    }

    #[test]
    fn test_positive_addl() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .input(1)
            .addl(3)
            .ii(12.0)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 4); // Original + 3 additional

        let times: Vec<f64> = events.iter().map(|e| e.time()).collect();
        // Additional doses come first, then original
        assert_eq!(times, vec![12.0, 24.0, 36.0, 0.0]);
    }

    #[test]
    fn test_negative_addl() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .input(1)
            .addl(-3)
            .ii(12.0)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 4); // Original + 3 additional

        let times: Vec<f64> = events.iter().map(|e| e.time()).collect();
        // Negative ADDL: doses go backward in time
        assert_eq!(times, vec![-12.0, -24.0, -36.0, 0.0]);
    }

    #[test]
    fn test_large_negative_addl() {
        // Match the pharmsol pmetrics test case
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .input(1)
            .addl(-10)
            .ii(12.0)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 11); // Original + 10 additional

        let times: Vec<f64> = events.iter().map(|e| e.time()).collect();
        assert_eq!(
            times,
            vec![-12.0, -24.0, -36.0, -48.0, -60.0, -72.0, -84.0, -96.0, -108.0, -120.0, 0.0]
        );
    }

    #[test]
    fn test_infusion_with_addl() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .dur(1.0)
            .input(1)
            .addl(2)
            .ii(24.0)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 3);

        // All events should be infusions
        for event in &events {
            match event {
                Event::Infusion(inf) => {
                    assert_eq!(inf.amount(), 100.0);
                    assert_eq!(inf.duration(), 1.0);
                }
                _ => panic!("Expected infusion event"),
            }
        }
    }

    #[test]
    fn test_covariates() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(0)
            .out(25.0)
            .outeq(1)
            .covariate("weight", 70.0)
            .covariate("age", 45.0)
            .build();

        assert_eq!(row.covariates().len(), 2);
        assert_eq!(row.covariates().get("weight"), Some(&70.0));
        assert_eq!(row.covariates().get("age"), Some(&45.0));
    }

    #[test]
    fn test_error_poly() {
        let row = DataRow::builder("pt1", 1.0)
            .evid(0)
            .out(25.0)
            .outeq(1)
            .error_poly(0.1, 0.2, 0.0, 0.0)
            .build();

        let events = row.into_events().unwrap();
        match &events[0] {
            Event::Observation(obs) => {
                let ep = obs.errorpoly().unwrap();
                assert_eq!(ep.coefficients(), (0.1, 0.2, 0.0, 0.0));
            }
            _ => panic!("Expected observation"),
        }
    }

    #[test]
    fn test_censoring() {
        let row = DataRow::builder("pt1", 1.0)
            .evid(0)
            .out(0.5)
            .outeq(1)
            .cens(Censor::BLOQ)
            .build();

        let events = row.into_events().unwrap();
        match &events[0] {
            Event::Observation(obs) => {
                assert!(obs.censored());
                assert_eq!(obs.censoring(), Censor::BLOQ);
            }
            _ => panic!("Expected observation"),
        }
    }

    #[test]
    fn test_missing_outeq_error() {
        let row = DataRow::builder("pt1", 1.0)
            .evid(0)
            .out(25.0)
            // Missing outeq
            .build();

        let result = row.into_events();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            DataError::MissingObservationOuteq { .. }
        ));
    }

    #[test]
    fn test_missing_dose_error() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .input(1)
            // Missing dose
            .build();

        let result = row.into_events();
        assert!(result.is_err());
    }

    #[test]
    fn test_missing_input_error() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            // Missing input
            .build();

        let result = row.into_events();
        assert!(result.is_err());
    }

    #[test]
    fn test_unknown_evid_error() {
        let row = DataRow::builder("pt1", 0.0).evid(99).build();

        let result = row.into_events();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            DataError::UnknownEvid { evid: 99, .. }
        ));
    }

    #[test]
    fn test_addl_zero_has_no_effect() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .input(1)
            .addl(0)
            .ii(12.0)
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 1); // Only original dose
    }

    #[test]
    fn test_addl_without_ii_has_no_effect() {
        let row = DataRow::builder("pt1", 0.0)
            .evid(1)
            .dose(100.0)
            .input(1)
            .addl(5)
            // Missing ii
            .build();

        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 1); // Only original dose
    }

    #[test]
    fn test_evid_4_reset() {
        let row = DataRow::builder("pt1", 24.0)
            .evid(4)
            .dose(100.0)
            .input(1)
            .build();

        assert!(row.is_occasion_reset());
        let events = row.into_events().unwrap();
        assert_eq!(events.len(), 1);
    }
}