Many things

main
AlessandroBregoli 3 years ago
parent c2a86cfccd
commit 67f77c3361
  1. 2
      Cargo.toml
  2. 46
      src/ctbn.rs
  3. 1
      src/lib.rs
  4. 7
      src/network.rs
  5. 13
      src/node.rs
  6. 38
      src/params.rs
  7. 30
      src/tools.rs

@ -10,3 +10,5 @@ edition = "2021"
petgraph = "*" petgraph = "*"
ndarray = "*" ndarray = "*"
thiserror = "*" thiserror = "*"
rand = "*"
bimap = "*"

@ -1,4 +1,4 @@
use std::collections::HashMap; use std::collections::{HashMap, BTreeSet};
use petgraph::prelude::*; use petgraph::prelude::*;
use crate::node; use crate::node;
@ -7,39 +7,39 @@ use crate::network;
pub struct CtbnParams {
cim: Option<params::CIM>,
transitions: Option<params::M>,
residence_time: Option<params::T>
}
impl CtbnParams {
fn init() -> CtbnParams {
CtbnParams {
cim: Option::None,
transitions: Option::None,
residence_time: Option::None
}
}
}
pub struct CtbnNetwork { pub struct CtbnNetwork {
network: petgraph::stable_graph::StableGraph<node::Node, u64>, network: petgraph::stable_graph::StableGraph<node::Node, ()>,
params: HashMap<petgraph::graph::NodeIndex,CtbnParams>,
} }
impl network::Network for CtbnNetwork { impl network::Network for CtbnNetwork {
fn add_node(&mut self, n: node::Node) -> Result<petgraph::graph::NodeIndex, network::NetworkError> { fn add_node(&mut self, n: node::Node) -> Result<petgraph::graph::NodeIndex, network::NetworkError> {
match &n.domain { match &n.params {
node::DomainType::Discrete(_) => { node::ParamsType::DiscreteStatesContinousTime(_) => {
if self.network.node_weights().any(|x| x.label == n.label) {
//TODO: Insert a better error description
return Err(network::NetworkError::NodeInsertionError(String::from("Label already used")));
}
let idx = self.network.add_node(n); let idx = self.network.add_node(n);
self.params.insert(idx, CtbnParams::init());
Ok(idx) Ok(idx)
}, },
_ => Err(network::NetworkError::InsertionError(String::from("unsupported node"))) //TODO: Insert a better error description
_ => Err(network::NetworkError::NodeInsertionError(String::from("unsupported node")))
} }
}
fn add_edge(&mut self, parent: &petgraph::stable_graph::NodeIndex, child: &petgraph::graph::NodeIndex) {
self.network.add_edge(parent.clone(), child.clone(), {});
let mut p = self.network.node_weight(child.clone());
match p.
} }
}
fn get_node_indices(&self) -> petgraph::stable_graph::NodeIndices<node::Node>{
self.network.node_indices()
}
fn get_node_weight(&self, node_idx: &petgraph::stable_graph::NodeIndex) -> &node::Node{
self.network.node_weight(node_idx.clone()).unwrap()
}
}

@ -2,6 +2,7 @@ mod node;
mod params; mod params;
mod network; mod network;
mod ctbn; mod ctbn;
mod tools;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {

@ -5,11 +5,12 @@ use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum NetworkError { pub enum NetworkError {
#[error("Error during node insertion")] #[error("Error during node insertion")]
InsertionError(String) NodeInsertionError(String)
} }
pub trait Network { pub trait Network {
fn add_node(&mut self, n: node::Node) -> Result<petgraph::graph::NodeIndex, NetworkError>; fn add_node(&mut self, n: node::Node) -> Result<petgraph::graph::NodeIndex, NetworkError>;
fn add_edge(&mut self, parent: &petgraph::stable_graph::NodeIndex, child: &petgraph::graph::NodeIndex);
fn get_node_indices(&self) -> petgraph::stable_graph::NodeIndices<node::Node>;
fn get_node_weight(&self, node_idx: &petgraph::stable_graph::NodeIndex) -> &node::Node;
} }

@ -1,11 +1,16 @@
use std::collections::BTreeSet;
use crate::params;
pub enum DomainType {
Discrete(Vec<String>)
}
pub struct Node { pub struct Node {
pub domain: DomainType, pub params: Box<dyn params::Params>,
pub label: String pub label: String
} }
impl PartialEq for Node {
fn eq(&self, other: &Node) -> bool{
self.label == other.label
}
}

@ -1,15 +1,39 @@
use ndarray::prelude::*; use ndarray::prelude::*;
use std::collections::{HashMap, BTreeSet};
use petgraph::prelude::*;
pub struct CIM {
cim: Array3<f64>,
}
pub struct M {
transitions: Array3<u64>, pub trait Params {
fn add_parent(&mut self, p: &petgraph::stable_graph::NodeIndex);
} }
pub struct T { pub struct DiscreteStatesContinousTimeParams {
residence_time: Array2<f64>, domain: BTreeSet<String>,
parents: BTreeSet<petgraph::stable_graph::NodeIndex>,
cim: Option<Array3<f64>>,
transitions: Option<Array3<u64>>,
residence_time: Option<Array2<f64>>
} }
impl DiscreteStatesContinousTimeParams {
fn init(domain: BTreeSet<String>) -> DiscreteStatesContinousTimeParams {
DiscreteStatesContinousTimeParams {
domain: domain,
parents: BTreeSet::new(),
cim: Option::None,
transitions: Option::None,
residence_time: Option::None
}
}
}
impl Params for DiscreteStatesContinousTimeParams {
fn add_parent(&mut self, p: &petgraph::stable_graph::NodeIndex) {
self.parents.insert(p.clone());
self.cim = Option::None;
self.transitions = Option::None;
self.residence_time = Option::None;
}
}

@ -0,0 +1,30 @@
use ndarray::prelude::*;
use crate::network;
use petgraph::prelude::*;
use rand::Rng;
pub struct Trajectory {
time: Array1<f64>,
events: Array2<u32>
}
pub struct Dataset {
trajectories: Vec<Trajectory>
}
fn trajectory_generator(net: &Box<dyn network::Network>, n_trajectories: u64, t_end: f64) -> Dataset {
let mut dataset = Dataset{
trajectories: Vec::new()
};
for _ in 0..n_trajectories {
let mut rng = rand::thread_rng();
let t = 0.0;
let mut time: Vec<f64> = Vec::new();
let mut events: Vec<Vec<u32>> = Vec::new();
let current_state: Vec<u32> = net.get_node_indices().map(|x| rng.gen_range(0..2)).collect();
}
dataset
}
Loading…
Cancel
Save