Skip to content

Commit

Permalink
Merge pull request #61 from LAPKB/net
Browse files Browse the repository at this point in the history
Net
  • Loading branch information
Siel authored Oct 17, 2024
2 parents d1ed931 + 2576d7b commit 8bdc4de
Show file tree
Hide file tree
Showing 47 changed files with 2,122 additions and 4,810 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ settings.json
/benches/*.json
log.txt
op.csv
*results.txt
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pmcore"
version = "0.6.0"
version = "0.7.0"
edition = "2021"
authors = [
"Julián D. Otálvaro <juliandavid347@gmail.com>",
Expand All @@ -23,21 +23,23 @@ serde_json = "1.0.66"
sobol_burley = "0.5.0"
ndarray-stats = "0.5.1"
linfa-linalg = "0.1.0"
ratatui = { version = "0.28.0", features = ["crossterm"] }
tokio = { version = "1.32.0", features = ["sync", "rt"] }
argmin = { version = "0.10.0", features = [] }
argmin-math = { version = "0.4.0", features = ["ndarray_v0_15-nolinalg"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "fmt", "time"] }
chrono = "0.4"
config = {version = "0.14", features=["preserve_order"]}
faer = "0.19.0"
faer = "0.19.3"
faer-ext = {version = "0.2.0", features = ["nalgebra", "ndarray"]}
pharmsol = "0.6.0"
pharmsol = "0.7.0"
# pharmsol = {path = "../pharmsol"}
# pharmsol = {git ="https://github.com/LAPKB/pharmsol", branch="net"}
# pharmsol = {git ="https://github.com/LAPKB/pharmsol", branch="main"}
toml = "0.8.14" #REMOVE
rand = "0.8.5"
anyhow = "1.0.86"
nalgebra = "0.33.0"


[profile.release]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Rust library with the building blocks to create and implement new non-parametric
* Powerful simulation engine
* Informative Terminal User Interface (TUI)

## Available algoritms
## Available algorithms

This project aims to implement several algorithms for non-parametric population pharmacokinetic modelling.

Expand Down
32 changes: 17 additions & 15 deletions benches/compare.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pmcore::prelude::*;

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

fn main() -> std::io::Result<()> {
Expand All @@ -26,11 +26,12 @@ pub fn analytical_bke(bencher: Bencher, len: usize) {
);
let settings = bke_settings();
let data = data::read_pmetrics("examples/bimodal_ke/bimodal_ke.csv").unwrap();
let mut algorithm = dispatch_algorithm(settings, eq, data).unwrap();
bencher.bench(|| {
for _ in 0..len {
let result = black_box(fit(eq.clone(), data.clone(), settings.clone()).unwrap());
assert!(result.cycles == 96);
assert!(result.objf == -344.64028277953844);
let result = black_box(algorithm.fit().unwrap());
assert!(result.cycles() == 96);
assert!(result.objf() == -344.64028277953844);
}
});
}
Expand All @@ -53,11 +54,12 @@ pub fn ode_bke(bencher: Bencher, len: usize) {
);
let settings = bke_settings();
let data = data::read_pmetrics("examples/bimodal_ke/bimodal_ke.csv").unwrap();
let mut algorithm = dispatch_algorithm(settings, eq, data).unwrap();
bencher.bench(|| {
for _ in 0..len {
let result = black_box(fit(eq.clone(), data.clone(), settings.clone()).unwrap());
assert!(result.cycles == 104);
assert!(result.objf == -348.69505647385495);
let result = black_box(algorithm.fit().unwrap());
assert!(result.cycles() == 104);
assert!(result.objf() == -348.69505647385495);
}
});
}
Expand All @@ -80,11 +82,12 @@ fn analytical_tel(bencher: Bencher, len: usize) {
);
let settings = tel_settings();
let data = data::read_pmetrics("examples/two_eq_lag/two_eq_lag.csv").unwrap();
let mut algorithm = dispatch_algorithm(settings, eq, data).unwrap();
bencher.bench(|| {
for _ in 0..len {
let result = black_box(fit(eq.clone(), data.clone(), settings.clone()).unwrap());
assert!(result.cycles == 686);
assert!(result.objf == 432.95499351489167);
let result = black_box(algorithm.fit().unwrap());
assert!(result.cycles() == 686);
assert!(result.objf() == 432.95499351489167);
}
});
}
Expand All @@ -111,11 +114,12 @@ fn ode_tel(bencher: Bencher, len: usize) {
);
let settings = tel_settings();
let data = data::read_pmetrics("examples/two_eq_lag/two_eq_lag.csv").unwrap();
let mut algorithm = dispatch_algorithm(settings, eq, data).unwrap();
bencher.bench(|| {
for _ in 0..len {
let result = black_box(fit(eq.clone(), data.clone(), settings.clone()).unwrap());
assert!(result.cycles == 707);
assert!(result.objf == 432.9542531584738);
let result = black_box(algorithm.fit().unwrap());
assert!(result.cycles() == 707);
assert!(result.objf() == 432.9542531584738);
}
});
}
Expand All @@ -125,7 +129,6 @@ fn tel_settings() -> Settings {
config: Config {
cycles: 1000,
algorithm: "NPAG".to_string(),
tui: false,
cache: true,
..Default::default()
},
Expand Down Expand Up @@ -195,7 +198,6 @@ fn bke_settings() -> Settings {
config: Config {
cycles: 1024,
algorithm: "NPAG".to_string(),
tui: false,
cache: true,
include: None,
exclude: None,
Expand Down
82 changes: 82 additions & 0 deletions compare.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash

# Check if enough arguments are supplied
if [ "$#" -ne 4 ]; then
echo "Usage: ./time_compare.sh <example_name> <N> <commit-hash-1> <commit-hash-2>"
exit 1
fi

# Input arguments
EXAMPLE_NAME=$1
N=$2
COMMIT1=$3
COMMIT2=$4

# Check if bc is installed
if ! command -v bc &> /dev/null
then
echo "'bc' is required but it's not installed. Please install it using your package manager."
exit 1
fi

# Output file based on example name
OUTPUT_FILE="${EXAMPLE_NAME}_time_comparison_results.txt"
echo "Time comparison results" > $OUTPUT_FILE
echo "Example: $EXAMPLE_NAME" >> $OUTPUT_FILE
echo "===================================" >> $OUTPUT_FILE

# Function to run the example N times and log the average time
function time_commit() {
COMMIT=$1
EXAMPLE_NAME=$2
N=$3
TOTAL_TIME=0

echo "Checking out commit $COMMIT"
git checkout $COMMIT

# Running the example N times
echo "Running $EXAMPLE_NAME in release mode for $N times"

# # Makes sure the target directory is clean
# cargo clean -q

#compile the example
echo "Compilating $COMMIT..."
cargo build --release -q --example $EXAMPLE_NAME
echo "Done compilating $COMMIT..."
for i in $(seq 1 $N); do


echo "Run $i: executing 'cargo run' for commit $COMMIT..."

# Capture both stdout and stderr to handle errors from cargo run
RUN_TIME=$( { /usr/bin/time -f "%e" cargo run --release --example $EXAMPLE_NAME 2>&1 | grep -Eo '^[0-9]+\.[0-9]+$'; } 2>&1 )

# Validate that RUN_TIME is a valid float
if [[ $RUN_TIME =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
echo "Run $i: ${RUN_TIME}s"
TOTAL_TIME=$(echo "$TOTAL_TIME + $RUN_TIME" | bc)
else
echo "Error: Unable to capture timing for run $i. Got: $RUN_TIME"
exit 1
fi
done

# Calculate average time
AVERAGE_TIME=$(echo "scale=3; $TOTAL_TIME / $N" | bc)
echo "Average time for commit $COMMIT: ${AVERAGE_TIME}s" >> $OUTPUT_FILE
echo "-----------------------------------" >> $OUTPUT_FILE
}

# Time the first commit
time_commit $COMMIT1 $EXAMPLE_NAME $N

# Time the second commit
time_commit $COMMIT2 $EXAMPLE_NAME $N

# Switch back to the current branch
git checkout -

# Output completion message
echo "Timing complete. Results saved to $OUTPUT_FILE."
5 changes: 2 additions & 3 deletions examples/bimodal_ke/config.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[config]
cycles = 1024
algorithm = "NPAG"
tui = false
cache = true

[random]
Expand All @@ -18,8 +17,8 @@ level = "info"

[prior]
sampler = "sobol"
file = "examples/bimodal_ke/prior.csv"
# file = "examples/bimodal_ke/prior.csv"

[output]
write = true
path = "examples/bimodal_ke/output"
path = "examples/bimodal_ke/output"
22 changes: 20 additions & 2 deletions examples/bimodal_ke/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use logger::setup_log;
use pmcore::prelude::*;

fn main() {
let eq = equation::ODE::new(
|x, p, _t, dx, rateiv, _cov| {
Expand Down Expand Up @@ -28,8 +28,26 @@ fn main() {
// },
// (1, 1),
// );
// let eq = equation::ODENet::new(
// vec![dmatrix![-1.0], dmatrix![0.0]],
// vec![],
// vec![],
// vec![],
// vec![],
// vec![],
// vec![OutEq::new(0, Div(X(0), P(1)))],
// (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 _result = fit(eq, data, settings);
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);
}
Loading

0 comments on commit 8bdc4de

Please sign in to comment.