Complete refactor, now can generate multiple networks, each customizable from a dedicated json file

master
Meliurwen 2 years ago
parent 524bc55fce
commit 6da9d69392
Signed by: meliurwen
GPG Key ID: 818A8B35E9F1CE10
  1. 3
      .gitignore
  2. 35
      README.md
  3. 77
      src/main.rs

3
.gitignore vendored

@ -1,3 +1,6 @@
/target /target
Cargo.lock Cargo.lock
.vscode .vscode
networks.d/
networks-settings.json

@ -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
}
]
```

@ -1,6 +1,11 @@
use reCTBN; use reCTBN;
use json::*; use json::*;
use std::collections::BTreeSet;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
use reCTBN::params; use reCTBN::params;
use reCTBN::params::Params::DiscreteStatesContinousTime; use reCTBN::params::Params::DiscreteStatesContinousTime;
use reCTBN::params::ParamsTrait; use reCTBN::params::ParamsTrait;
@ -12,12 +17,20 @@ use reCTBN::tools::RandomGraphGenerator;
use reCTBN::tools::RandomParametersGenerator; use reCTBN::tools::RandomParametersGenerator;
use reCTBN::tools::UniformGraphGenerator; use reCTBN::tools::UniformGraphGenerator;
use reCTBN::tools::UniformParametersGenerator; 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<u64>,
cg_seed: Option<u64>,
tg_seed: Option<u64>,
) -> (CtbnNetwork, Dataset) {
let mut net = CtbnNetwork::new(); let mut net = CtbnNetwork::new();
let nodes_cardinality = 5;
let domain_cardinality = 3;
for node in 0..nodes_cardinality { for node in 0..nodes_cardinality {
// Create the domain for a discrete node // Create the domain for a discrete node
let mut domain = BTreeSet::new(); 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 // Initialize the Graph Generator using the one with an
// uniform distribution // 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 // Generate the graph directly on the network
structure_generator.generate_graph(&mut net); structure_generator.generate_graph(&mut net);
// Initialize the parameters generator with uniform distributin // 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. // Generate CIMs with uniformly distributed parameters.
cim_generator.generate_parameters(&mut net); 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); return (net, dataset);
} }
fn main() { fn json_gen(net: CtbnNetwork, dataset: Dataset) -> String {
let mut data = json::JsonValue::new_array(); let mut data = json::JsonValue::new_array();
data.push(json::JsonValue::new_object()).unwrap(); data.push(json::JsonValue::new_object()).unwrap();
data[0]["dyn.str"] = json::JsonValue::new_array(); data[0]["dyn.str"] = json::JsonValue::new_array();
data[0]["variables"] = json::JsonValue::new_array(); data[0]["variables"] = json::JsonValue::new_array();
data[0]["dyn.cims"] = object! {}; data[0]["dyn.cims"] = object! {};
data[0]["samples"] = json::JsonValue::new_array(); data[0]["samples"] = json::JsonValue::new_array();
let (net, dataset) = uniform_parameters_generator_right_densities_ctmp();
for node_idx in net.get_node_indices() { for node_idx in net.get_node_indices() {
let mut variable = json::JsonValue::new_object(); let mut variable = json::JsonValue::new_object();
@ -91,5 +103,50 @@ fn main() {
} }
data[0]["samples"].push(trajectory).unwrap(); 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!");
}
} }

Loading…
Cancel
Save