Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build settings using a typestate pattern #68

Open
wants to merge 31 commits into
base: hash
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
3fe21b9
Parameters is a new struct to hold and add parameters
mhovd Dec 15, 2024
fb8b28c
Documentation
mhovd Dec 15, 2024
7166398
Improved builder pattern
mhovd Dec 16, 2024
0cc0dae
Update BKE
mhovd Dec 21, 2024
b7b6138
Merge pull request #66 from LAPKB/hash
mhovd Dec 21, 2024
44b8f7c
Merge branch 'dev' into hash_settings_restructure
mhovd Dec 21, 2024
d6d3268
Refactoring
mhovd Dec 22, 2024
bc5d4f2
Error handling in examples
mhovd Dec 23, 2024
382d178
Setters and getters
mhovd Dec 23, 2024
ee3c9ed
Update settings.rs
mhovd Dec 23, 2024
a45e45b
Setters takes reference to mut self
mhovd Dec 23, 2024
1a32d99
Use BTreeMap instead of vector for parameters
mhovd Dec 23, 2024
2545fcc
Minor typos
mhovd Dec 23, 2024
d56b501
Update config.toml
mhovd Dec 23, 2024
7c01ccf
More getters/setters
mhovd Dec 23, 2024
8b3dca1
Rename POSTPROB to MAP
mhovd Dec 23, 2024
b51bdd4
Update examples
mhovd Dec 23, 2024
a55639a
Merge pull request #65 from LAPKB/hash_settings_restructure
mhovd Dec 23, 2024
a89c1d1
Structure name change
mhovd Dec 23, 2024
c578733
clippy
mhovd Dec 23, 2024
88b5aac
clippy
mhovd Dec 23, 2024
9abaa71
Clean up NPAG
mhovd Dec 23, 2024
aeff351
Clean up NPOD
mhovd Dec 23, 2024
419cad4
Typo
mhovd Dec 24, 2024
2cabcf8
Support typestate builder for settings
mhovd Dec 24, 2024
9b0d809
Remove include/exclude from settings
mhovd Dec 24, 2024
96e84f7
Experimenting with algorithm choice and dispatch
mhovd Dec 24, 2024
4f807b4
Merge pull request #67 from LAPKB/typestate_settings_extended
mhovd Dec 24, 2024
dbfe41c
Clean up arguments to MAP
mhovd Dec 25, 2024
09b9b34
Minor changes to logger helpers
mhovd Dec 25, 2024
b683b72
Minor docs update
mhovd Dec 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ faer = "0.19.3"
faer-ext = { version = "0.2.0", features = ["nalgebra", "ndarray"] }
# pharmsol = "0.7.1"
pharmsol = {path="../pharmsol"}
# pharmsol = { git = "https://github.com/LAPKB/pharmsol.git", branch = "dev"}
toml = "0.8.14" #REMOVE
rand = "0.8.5"
anyhow = "1.0.86"
nalgebra = "0.33.0"
Expand Down
178 changes: 55 additions & 123 deletions benches/compare.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use pmcore::prelude::*;

use diol::prelude::*;
use settings::{Log, *};
use toml::Table;
use settings::*;

fn main() -> std::io::Result<()> {
let mut bench = Bench::new(BenchConfig::from_args()?);
Expand Down Expand Up @@ -125,132 +124,65 @@ fn ode_tel(bencher: Bencher, len: usize) {
}

fn tel_settings() -> Settings {
let settings = Settings {
config: Config {
cycles: 1000,
algorithm: "NPAG".to_string(),
cache: true,
..Default::default()
},
predictions: settings::Predictions::default(),
log: Log {
level: "warn".to_string(),
file: "".to_string(),
write: false,
},
prior: Prior {
file: None,
sampler: "sobol".to_string(),
points: 2129,
seed: 347,
},
output: Output {
write: false,
..Default::default()
},
convergence: Default::default(),
advanced: Default::default(),
random: Random {
parameters: Table::from(
[
(
"Ka".to_string(),
toml::Value::Array(vec![toml::Value::Float(0.1), toml::Value::Float(0.9)]),
),
(
"Ke".to_string(),
toml::Value::Array(vec![
toml::Value::Float(0.001),
toml::Value::Float(0.1),
]),
),
(
"Tlag1".to_string(),
toml::Value::Array(vec![toml::Value::Float(0.0), toml::Value::Float(4.0)]),
),
(
"V".to_string(),
toml::Value::Array(vec![
toml::Value::Float(30.0),
toml::Value::Float(120.0),
]),
),
]
.iter()
.cloned()
.collect(),
),
},
fixed: None,
constant: None,
error: Error {
value: 5.0,
class: "proportional".to_string(),
poly: (0.02, 0.05, -2e-04, 0.0),
},
};
let mut settings = Settings::new();

let parameters = Parameters::new()
.add("Ka", 0.1, 0.3, false)
.unwrap()
.add("Ke", 0.001, 0.1, false)
.unwrap()
.add("Tlag1", 0.0, 4.00, false)
.unwrap()
.add("V", 30.0, 120.0, false)
.unwrap()
.to_owned();

settings.set_parameters(parameters);
settings.set_config(Config {
cycles: 1000,
..Default::default()
});
settings.set_prior(Prior {
file: None,
sampler: "sobol".to_string(),
points: 2129,
seed: 347,
});
settings.set_error(Error {
value: 5.0,
class: ErrorType::Proportional,
poly: (0.02, 0.05, -2e-04, 0.0),
});

settings.validate().unwrap();
settings
}

fn bke_settings() -> Settings {
let settings = Settings {
config: Config {
cycles: 1024,
algorithm: "NPAG".to_string(),
cache: true,
include: None,
exclude: None,
},
predictions: settings::Predictions::default(),
log: Log {
level: "warn".to_string(),
file: "".to_string(),
write: false,
},
prior: Prior {
file: None,
points: settings::Prior::default().points,
sampler: "sobol".to_string(),
..Default::default()
},
output: Output {
write: false,
path: "output".to_string(),
},
convergence: Convergence::default(),
advanced: Advanced::default(),
random: Random {
parameters: Table::from(
[
(
"Ke".to_string(),
toml::Value::Array(vec![
toml::Value::Float(0.001),
toml::Value::Float(3.0),
]),
),
(
"V".to_string(),
toml::Value::Array(vec![
toml::Value::Float(25.0),
toml::Value::Float(250.0),
]),
),
]
.iter()
.cloned()
.collect(),
),
},
fixed: None,
constant: None,
error: Error {
value: 0.0,
class: "additive".to_string(),
poly: (0.0, 0.05, 0.0, 0.0),
},
};
let mut settings = Settings::new();

let parameters = Parameters::new()
.add("ke", 0.001, 3.0, true)
.unwrap()
.add("v", 25.0, 250.0, true)
.unwrap()
.to_owned();

settings.set_parameters(parameters);
settings.set_config(Config {
cycles: 1000,
..Default::default()
});
settings.set_output(Output {
write: false,
path: "".to_string(),
});
settings.set_error(Error {
value: 0.0,
class: ErrorType::Additive,
poly: (0.00, 0.05, 0.0, 0.0),
});

settings.validate().unwrap();
settings
}
6 changes: 2 additions & 4 deletions examples/bimodal_ke/config.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Currently not in use - settings are defined in main.rs

[config]
cycles = 1024
algorithm = "NPAG"
cache = true

[random]
ke = [0.001, 3.0]
v = [25.0, 250.0]

[error]
value = 0.0
class = "additive"
Expand Down
34 changes: 22 additions & 12 deletions examples/bimodal_ke/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anyhow::Result;
use logger::setup_log;
use pmcore::prelude::*;
fn main() {
use settings::{Parameters, Settings};
fn main() -> Result<()> {
let eq = equation::ODE::new(
|x, p, _t, dx, rateiv, _cov| {
// fetch_cov!(cov, t, wt);
Expand Down Expand Up @@ -39,15 +41,23 @@ fn main() {
// (1, 1),
// );

let settings = settings::read("examples/bimodal_ke/config.toml").unwrap();
setup_log(&settings).unwrap();
let data = data::read_pmetrics("examples/bimodal_ke/bimodal_ke.csv").unwrap();
let mut algorithm = dispatch_algorithm(settings, eq, data).unwrap();
let result = algorithm.fit().unwrap();
// algorithm.initialize().unwrap();
// while !algorithm.next_cycle().unwrap() {}
// let result = algorithm.into_npresult();
result.write_outputs().unwrap();
// println!("{:?}", result);
// let _result = fit(eq, data, settings);
let mut settings = Settings::new();

let params = Parameters::new()
.add("ke", 0.001, 3.0, true)?
.add("v", 25.0, 250.0, true)?;

settings.set_parameters(params);
settings.set_cycles(1000);
settings.set_error_poly((0.0, 0.5, 0.0, 0.0));
settings.set_error_type(ErrorType::Additive);
settings.set_output_path("examples/bimodal_ke/output");

setup_log(&settings)?;
let data = data::read_pmetrics("examples/bimodal_ke/bimodal_ke.csv")?;
let mut algorithm = dispatch_algorithm(settings, eq, data)?;
let result = algorithm.fit()?;
result.write_outputs()?;

Ok(())
}
2 changes: 1 addition & 1 deletion examples/debug.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use algorithms::{npag::NPAG, Algorithm};
use algorithms::{npag::NPAG, NonParametricAlgorithm};
use ipm::burke;
use logger::setup_log;
use pmcore::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion examples/drusano/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn find_m0(ufinal: f64, v: f64, alpha: f64, h1: f64, h2: f64) -> f64 {
let b3 = alpha * v * u * hh / xm.powf(hh + 1.0);
let xmp = top / (b1 + b2 + b3);

xm = xm + xmp * delu;
xm += xmp * delu;

if xm <= 0.0 {
return -1.0; // Greco equation is not solvable
Expand Down
11 changes: 0 additions & 11 deletions examples/toml.rs

This file was deleted.

7 changes: 0 additions & 7 deletions examples/two_eq_lag/config.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
[config]
cycles = 1000
algorithm = "NPAG"

cache = true

[random]
Ka = [0.1, 0.9]
Ke = [0.001, 0.1]
Tlag1 = [0.0, 4.0]
V = [30.0, 120.0]

[error]
value = 5
class = "proportional"
Expand Down
29 changes: 25 additions & 4 deletions examples/two_eq_lag/main.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
#![allow(dead_code)]
#![allow(unused_variables)]
#![allow(unused_imports)]
use core::panic;
use std::path::Path;

use data::read_pmetrics;
use logger::setup_log;
use ndarray::Array2;
use pmcore::prelude::{models::one_compartment_with_absorption, simulator::Equation, *};
use settings::Parameters;
use settings::Settings;

fn main() {
let eq = equation::ODE::new(
|x, p, _t, dx, rateiv, _cov| {
fetch_cov!(cov, t,);
fetch_params!(p, ka, ke, _tlag, _v);
fetch_params!(p, ka, ke, tlag, v);
dx[0] = -ka * x[0];
dx[1] = ka * x[0] - ke * x[1];
},
|p| {
fetch_params!(p, _ka, _ke, tlag, _v);
fetch_params!(p, ka, ke, tlag, v);
lag! {0=>tlag}
},
|_p| fa! {},
|_p, _t, _cov, _x| {},
|x, p, _t, _cov, y| {
fetch_params!(p, _ka, _ke, _tlag, v);
fetch_params!(p, ka, ke, tlag, v);
y[0] = x[1] / v;
},
(2, 1),
Expand Down Expand Up @@ -71,7 +74,25 @@ fn main() {
// (2, 1),
// );

let settings = settings::read("examples/two_eq_lag/config.toml").unwrap();
let mut settings = Settings::new();

let parameters = Parameters::new()
.add("ka", 0.1, 0.3, false)
.unwrap()
.add("ke", 0.001, 0.1, false)
.unwrap()
.add("tlag", 0.0, 4.00, false)
.unwrap()
.add("v", 30.0, 120.0, false)
.unwrap();

settings.set_parameters(parameters);

settings.set_gamlam(5.0);
settings.set_error_poly((0.02, 0.05, -2e-04, 0.0));
settings.set_prior_points(2129);
settings.set_cycles(1000);

setup_log(&settings).unwrap();
let data = data::read_pmetrics("examples/two_eq_lag/two_eq_lag.csv").unwrap();
let mut algorithm = dispatch_algorithm(settings, eq, data).unwrap();
Expand Down
Loading