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

rust-edition-update #12

Merged
merged 2 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 1 addition & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: rs-test

on:
push:
branches: [ "main" ]

pull_request:
branches: [ "main" ]

Expand All @@ -15,6 +12,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions-rust-lang/setup-rust-toolchain@v1
- name: Build
run: cargo build --verbose
- name: Run tests
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rs-sim"
version = "0.9.1"
edition = "2021"
version = "0.9.2"
edition = "2024"

[dependencies]
ndarray-rand = "0.15"
Expand Down
45 changes: 20 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,66 +1,61 @@
// Discrete Event Model
// #[allow(dead_code, unused_imports, unused_variables)]
mod tx;
mod pq;
mod sim;
mod tx;

fn main() {
run();
run();
}

fn run() {
let working_dir = "./tmp/sim/test-50yr";

// Survival curve
let probabilities = vec![
1.0, 0.99, 0.97, 0.96, 0.95,
0.94, 0.93, 0.92, 0.91, 0.9,
0.88, 0.85, 0.82, 0.8, 0.75,
0.7, 0.65, 0.6, 0.55, 0.5,
0.4, 0.25, 0.15, 0.1, 0.0
1.0, 0.99, 0.97, 0.96, 0.95, 0.94, 0.93, 0.92, 0.91, 0.9, 0.88, 0.85, 0.82, 0.8, 0.75, 0.7,
0.65, 0.6, 0.55, 0.5, 0.4, 0.25, 0.15, 0.1, 0.0,
];

let init_states = pq::read("data/init_states.parquet")
.expect("failted to read init states...");
let init_states = pq::read("data/init_states.parquet").expect("failted to read init states...");

let uuids = tx::col_to_vec_str(&init_states, "uuid");
let states = tx::col_to_vec_i64(&init_states, "step_0");
let costs = tx::col_to_vec_i64(&init_states, "value");

// Execute
let n_steps:i64 = 50;
let n_sims:i64 = 200;
let n_steps: i64 = 50;
let n_sims: i64 = 200;
let constraints = vec![200_000_000; n_steps as usize];

if !std::path::Path::new(&working_dir).exists() {
std::fs::create_dir(&working_dir).expect("failed to create working dir...");
}

sim::engine(
&working_dir,
n_sims,
n_steps,
&working_dir,
n_sims,
n_steps,
uuids,
states,
probabilities,
0,
Some(&costs),
states,
probabilities,
0,
Some(&costs),
Some(constraints),
);

// Check
// Check
println!("Result Sample :");

let costs = pq::read(&format!("{}/costs.parquet", &working_dir))
.expect("failed to read parquet dir...");

println!("{:?}", costs);
println!();

let costs_constrained = pq::read(&format!("{}/costs_const.parquet", &working_dir))
.expect("failed to read parquet dir...");

println!("{:?}", costs_constrained);
println!();
}

27 changes: 12 additions & 15 deletions src/pq.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// File I/O
use polars::{prelude::*, io::parquet::write::StatisticsOptions};

pub fn read(path:&str) -> PolarsResult<DataFrame> {
Ok(
ParquetReader::new(std::fs::File::open(path)?)
.use_statistics(true)
.finish()?
)
use polars::{io::parquet::write::StatisticsOptions, prelude::*};

pub fn read(path: &str) -> PolarsResult<DataFrame> {
Ok(ParquetReader::new(std::fs::File::open(path)?)
.use_statistics(true)
.finish()?)
}

#[test]
fn test_read(){
// Ensure expected columns are in table
fn test_read() {
// Ensure expected columns are in table
let test = vec!["uuid", "value", "step_0"];
let res:Vec<String> = read("./data/init_states.parquet")
let res: Vec<String> = read("./data/init_states.parquet")
.expect("failed to read file")
.get_column_names()
.iter()
Expand All @@ -32,11 +30,11 @@ pub fn write(mut df: DataFrame, path: &str) -> Result<(), PolarsError> {
.with_compression(ParquetCompression::Snappy)
.finish(&mut df)?;

Ok(())
Ok(())
}

#[test]
fn test_write(){
fn test_write() {
// Create file
if !std::fs::exists("./tmp").unwrap() {
std::fs::create_dir("./tmp").unwrap();
Expand All @@ -49,6 +47,5 @@ fn test_write(){
let res = std::fs::exists(test).unwrap();
std::fs::remove_file(test).unwrap();

assert!(res);

assert!(res);
}
Loading
Loading