Skip to content

Commit

Permalink
Write covariates at all event times
Browse files Browse the repository at this point in the history
  • Loading branch information
mhovd committed Oct 17, 2024
1 parent f08c1d2 commit 949bb7f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ settings.json
/benches/*.json
log.txt
op.csv
*results.txt
*results.txt
covs.csv
53 changes: 53 additions & 0 deletions src/routines/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl<E: Equation> NPResult<E> {
.context("Failed to write observed-predicted file")?;
self.write_pred(idelta, tad)
.context("Failed to write predictions")?;
self.write_covs().context("Failed to write covariates")?;
if self.w.len() > 0 {
//TODO: find a better way to indicate that the run failed
self.write_posterior()
Expand Down Expand Up @@ -397,6 +398,58 @@ impl<E: Equation> NPResult<E> {
);
Ok(())
}

/// Writes the covariates
pub fn write_covs(&self) -> Result<()> {
tracing::debug!("Writing covariates...");
let outputfile = OutputFile::new(&self.settings.output.path, "covs.csv")?;
let mut writer = WriterBuilder::new()
.has_headers(true)
.from_writer(&outputfile.file);

#[derive(Debug, Serialize)]
struct Row {
id: String,
time: f64,
block: usize,
cov: String,
value: f64,
}

for subject in self.data.get_subjects() {
for occasion in subject.occasions() {
if let Some(cov) = occasion.get_covariates() {
let covmap = cov.covariates();

for event in occasion.get_events(&None, &None, false) {
let time = match event {
Event::Bolus(bolus) => bolus.time(),
Event::Infusion(infusion) => infusion.time(),
Event::Observation(observation) => observation.time(),
};

for cov in &covmap {
let value = cov.1.interpolate(time).unwrap();
let row = Row {
id: subject.id().clone(),
time,
block: occasion.index(),
cov: cov.0.clone(),
value,
};
writer.serialize(row)?;
}
}
}
}
}
writer.flush()?;
tracing::info!(
"Covariates written to {:?}",
&outputfile.get_relative_path()
);
Ok(())
}
}

/// An [NPCycle] object contains the summary of a cycle
Expand Down

0 comments on commit 949bb7f

Please sign in to comment.