From 6da9d6939292c65fb188e2eb1aa5cb63e7c8e99d Mon Sep 17 00:00:00 2001 From: meliurwen Date: Thu, 9 Feb 2023 16:04:10 +0100 Subject: [PATCH] Complete refactor, now can generate multiple networks, each customizable from a dedicated json file --- .gitignore | 3 +++ README.md | 35 ++++++++++++++++++++++++ src/main.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 README.md diff --git a/.gitignore b/.gitignore index c640ca5..11492d7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /target Cargo.lock .vscode + +networks.d/ +networks-settings.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..7727498 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# reCTBN Exporter + +You must create a file named `networks-settings.json` in the same folder you +are executing this command. + +The json must be something like this: + +```json +[ + { + "nodes_cardinality": 5, + "domain_cardinality": 2, + "density": 0.1, + "interval_s": 3.0, + "interval_f": 7.0, + "n_trajectories": 300, + "t_end": 200.0, + "sg_seed": 123456, + "cg_seed": 12345, + "tg_seed": 1234 + }, + { + "nodes_cardinality": 4, + "domain_cardinality": 2, + "density": 0.3333, + "interval_s": 3.0, + "interval_f": 7.0, + "n_trajectories": 300, + "t_end": 200.0, + "sg_seed": 1234563232, + "cg_seed": 1234522, + "tg_seed": 123432 + } +] +``` diff --git a/src/main.rs b/src/main.rs index 325dd80..1aabfa0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,11 @@ use reCTBN; use json::*; +use std::collections::BTreeSet; +use std::fs; +use std::fs::File; +use std::io::prelude::*; + use reCTBN::params; use reCTBN::params::Params::DiscreteStatesContinousTime; use reCTBN::params::ParamsTrait; @@ -12,12 +17,20 @@ use reCTBN::tools::RandomGraphGenerator; use reCTBN::tools::RandomParametersGenerator; use reCTBN::tools::UniformGraphGenerator; use reCTBN::tools::UniformParametersGenerator; -use std::collections::BTreeSet; -fn uniform_parameters_generator_right_densities_ctmp() -> (CtbnNetwork, Dataset) { +fn uniform_parameters_generator_right_densities_ctmp( + nodes_cardinality: usize, + domain_cardinality: usize, + density: f64, + interval_s: f64, + interval_f: f64, + n_trajectories: u64, + t_end: f64, + sg_seed: Option, + cg_seed: Option, + tg_seed: Option, +) -> (CtbnNetwork, Dataset) { let mut net = CtbnNetwork::new(); - let nodes_cardinality = 5; - let domain_cardinality = 3; for node in 0..nodes_cardinality { // Create the domain for a discrete node let mut domain = BTreeSet::new(); @@ -34,30 +47,29 @@ fn uniform_parameters_generator_right_densities_ctmp() -> (CtbnNetwork, Dataset) // Initialize the Graph Generator using the one with an // uniform distribution - let mut structure_generator = UniformGraphGenerator::new(1.0 / 3.0, Some(7641630759785120)); + let mut structure_generator = UniformGraphGenerator::new(density, sg_seed); // Generate the graph directly on the network structure_generator.generate_graph(&mut net); // Initialize the parameters generator with uniform distributin - let mut cim_generator = UniformParametersGenerator::new(3.0..7.0, Some(7641630759785120)); + let mut cim_generator = UniformParametersGenerator::new(interval_s..interval_f, cg_seed); // Generate CIMs with uniformly distributed parameters. cim_generator.generate_parameters(&mut net); - let dataset = trajectory_generator(&net, 300, 200.0, Some(30230423)); + let dataset = trajectory_generator(&net, n_trajectories, t_end, tg_seed); return (net, dataset); } -fn main() { +fn json_gen(net: CtbnNetwork, dataset: Dataset) -> String { let mut data = json::JsonValue::new_array(); data.push(json::JsonValue::new_object()).unwrap(); data[0]["dyn.str"] = json::JsonValue::new_array(); data[0]["variables"] = json::JsonValue::new_array(); data[0]["dyn.cims"] = object! {}; data[0]["samples"] = json::JsonValue::new_array(); - let (net, dataset) = uniform_parameters_generator_right_densities_ctmp(); for node_idx in net.get_node_indices() { let mut variable = json::JsonValue::new_object(); @@ -91,5 +103,50 @@ fn main() { } data[0]["samples"].push(trajectory).unwrap(); } - println!("{}", data.dump()); + data.dump() +} + +fn create_dir(path: String) -> std::io::Result<()> { + fs::create_dir_all(path)?; + Ok(()) +} + +fn write_file(file_name: String, file_content: &[u8]) -> std::io::Result<()> { + let mut file = File::create(file_name)?; + file.write_all(file_content)?; + Ok(()) +} + +fn main() { + let file_path = "./networks-settings.json"; + eprintln!("Opening file {}...", file_path); + let file_content = + fs::read_to_string(file_path).expect("File not found! Check the README for instructions!"); + let parsed_json = json::parse(&file_content).unwrap(); + + let networks_dir = "./networks.d/"; + create_dir(networks_dir.to_string()).unwrap(); + + for idx in 0..parsed_json.len() { + let (net, dataset) = uniform_parameters_generator_right_densities_ctmp( + parsed_json[idx]["nodes_cardinality"].as_usize().unwrap(), + parsed_json[idx]["domain_cardinality"].as_usize().unwrap(), + parsed_json[idx]["density"].as_f64().unwrap(), + parsed_json[idx]["interval_s"].as_f64().unwrap(), + parsed_json[idx]["interval_f"].as_f64().unwrap(), + parsed_json[idx]["n_trajectories"].as_u64().unwrap(), + parsed_json[idx]["t_end"].as_f64().unwrap(), + parsed_json[idx]["sg_seed"].as_u64(), + parsed_json[idx]["cg_seed"].as_u64(), + parsed_json[idx]["tg_seed"].as_u64(), + ); + + let data = json_gen(net, dataset); + let file_content = data.as_bytes(); + let file_name = format!("{}{}{}", idx, "network", ".json"); + let file_name = format!("{}{}", networks_dir, file_name); + write_file(file_name.to_owned(), &file_content).unwrap(); + + eprintln!("Networks generation complete!"); + } }