Skip to content

feat: Write the full NPresult to file#242

Open
mhovd wants to merge 3 commits intomainfrom
npresult-json
Open

feat: Write the full NPresult to file#242
mhovd wants to merge 3 commits intomainfrom
npresult-json

Conversation

@mhovd
Copy link
Collaborator

@mhovd mhovd commented Jan 29, 2026

For single-file parsing by Pmetrics and other software.

Work in progress.

@mhovd
Copy link
Collaborator Author

mhovd commented Jan 30, 2026

Note to self: consider using polars to write a .parquetfile, which is a binary format that can be read with R using arrow.

@mhovd mhovd marked this pull request as ready for review February 1, 2026 09:12
Copilot AI review requested due to automatic review settings February 1, 2026 09:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds functionality to serialize and write the complete NPResult structure to a JSON file for consumption by Pmetrics and other external tools. The PR is marked as "Work in progress."

Changes:

  • Added a new write_json() method to NPResult that serializes the entire structure to a JSON file
  • Updated the bimodal_ke example to demonstrate usage of the new method
  • Added result.json to .gitignore to exclude generated output files

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/routines/output/mod.rs Added write_json() method that serializes NPResult to JSON using serde_json
examples/bimodal_ke/main.rs Added call to write_json() after writing standard outputs
.gitignore Added result.json to ignored files list

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

/// Writes the entire NPResult to a JSON file
///
/// This will not calculate predictions automatically, to do so, call [NPResult::calculate_predictions] first
pub fn write_json(&mut self) -> Result<()> {
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method signature uses &mut self, but the method doesn't mutate any fields of the struct - it only serializes the current state. Based on the documentation comment stating this method will not calculate predictions automatically, and the pattern used by other non-mutating write methods like write_theta and write_covs, this should use &self instead of &mut self.

Suggested change
pub fn write_json(&mut self) -> Result<()> {
pub fn write_json(&self) -> Result<()> {

Copilot uses AI. Check for mistakes.
@mnneely
Copy link

mnneely commented Feb 1, 2026

Hi @mhovd , does this change how Pmetrics needs to parse the output from PMcore?

@mhovd
Copy link
Collaborator Author

mhovd commented Feb 1, 2026

Hi @mhovd , does this change how Pmetrics needs to parse the output from PMcore?

Possibly - for now it will be written in addition to the "regular" files.
There are some advantages of a single file:

  • Only need to parse one file
  • We can stuff more information into it, e.g. the support points for each cycle, metadata, all settings, etc. Maybe even the model if we can write it in a manner that can be read back in.

Disadvantages:

  • Not human readable, so needs to be parsed from JSON.
  • Possibly larger footprint, depending on what we include in it.

Open to suggestions and feedback!

@mnneely
Copy link

mnneely commented Feb 1, 2026

OK. Where’s the best place to grab/understand the schema? Reading json into R is easy enough. Lack of human readability isn’t a problem, as long as we provide an easy way for humans to read it, either through R or the GUI.

My motivation for it would be to increase speed, stability, and ease of extracting additional information in the future, although I have not heard any requests for more information from users. If it would help unify output from parametric vs. nonparametric algorithms into a single structure, that would be another advantage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants