|
|
@ -1,3 +1,5 @@ |
|
|
|
|
|
|
|
//! Module containing methods to define different types of nodes.
|
|
|
|
|
|
|
|
|
|
|
|
use std::collections::BTreeSet; |
|
|
|
use std::collections::BTreeSet; |
|
|
|
|
|
|
|
|
|
|
|
use enum_dispatch::enum_dispatch; |
|
|
|
use enum_dispatch::enum_dispatch; |
|
|
@ -23,9 +25,8 @@ pub enum StateType { |
|
|
|
Discrete(usize), |
|
|
|
Discrete(usize), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Parameters
|
|
|
|
/// This is a core element for building different types of nodes; the goal is to define the set of
|
|
|
|
/// The Params trait is the core element for building different types of nodes. The goal is to
|
|
|
|
/// methods required to describes a generic node.
|
|
|
|
/// define the set of method required to describes a generic node.
|
|
|
|
|
|
|
|
#[enum_dispatch(Params)] |
|
|
|
#[enum_dispatch(Params)] |
|
|
|
pub trait ParamsTrait { |
|
|
|
pub trait ParamsTrait { |
|
|
|
fn reset_params(&mut self); |
|
|
|
fn reset_params(&mut self); |
|
|
@ -65,25 +66,27 @@ pub trait ParamsTrait { |
|
|
|
fn get_label(&self) -> &String; |
|
|
|
fn get_label(&self) -> &String; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// The Params enum is the core element for building different types of nodes. The goal is to
|
|
|
|
/// Is a core element for building different types of nodes; the goal is to define all the
|
|
|
|
/// define all the supported type of Parameters
|
|
|
|
/// supported type of Parameters
|
|
|
|
#[derive(Clone)] |
|
|
|
#[derive(Clone)] |
|
|
|
#[enum_dispatch] |
|
|
|
#[enum_dispatch] |
|
|
|
pub enum Params { |
|
|
|
pub enum Params { |
|
|
|
DiscreteStatesContinousTime(DiscreteStatesContinousTimeParams), |
|
|
|
DiscreteStatesContinousTime(DiscreteStatesContinousTimeParams), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// DiscreteStatesContinousTime.
|
|
|
|
|
|
|
|
/// This represents the parameters of a classical discrete node for ctbn and it's composed by the
|
|
|
|
/// This represents the parameters of a classical discrete node for ctbn and it's composed by the
|
|
|
|
/// following elements:
|
|
|
|
/// following elements.
|
|
|
|
/// - **domain**: an ordered and exhaustive set of possible states
|
|
|
|
///
|
|
|
|
/// - **cim**: Conditional Intensity Matrix
|
|
|
|
/// # Arguments
|
|
|
|
/// - **Sufficient Statistics**: the sufficient statistics are mainly used during the parameter
|
|
|
|
///
|
|
|
|
/// learning task and are composed by:
|
|
|
|
/// * `label` - node's variable name.
|
|
|
|
/// - **transitions**: number of transitions from one state to another given a specific
|
|
|
|
/// * `domain` - an ordered and exhaustive set of possible states.
|
|
|
|
/// realization of the parent set
|
|
|
|
/// * `cim` - Conditional Intensity Matrix.
|
|
|
|
/// - **residence_time**: permanence time in each possible states given a specific
|
|
|
|
/// * `transitions` - number of transitions from one state to another given a specific realization
|
|
|
|
/// realization of the parent set
|
|
|
|
/// of the parent set; is a sufficient statistics are mainly used during the parameter learning
|
|
|
|
|
|
|
|
/// task.
|
|
|
|
|
|
|
|
/// * `residence_time` - residence time in each possible state, given a specific realization of the
|
|
|
|
|
|
|
|
/// parent set; is a sufficient statistics are mainly used during the parameter learning task.
|
|
|
|
#[derive(Clone)] |
|
|
|
#[derive(Clone)] |
|
|
|
pub struct DiscreteStatesContinousTimeParams { |
|
|
|
pub struct DiscreteStatesContinousTimeParams { |
|
|
|
label: String, |
|
|
|
label: String, |
|
|
@ -104,15 +107,17 @@ impl DiscreteStatesContinousTimeParams { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///Getter function for CIM
|
|
|
|
/// Getter function for CIM
|
|
|
|
pub fn get_cim(&self) -> &Option<Array3<f64>> { |
|
|
|
pub fn get_cim(&self) -> &Option<Array3<f64>> { |
|
|
|
&self.cim |
|
|
|
&self.cim |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///Setter function for CIM.\\
|
|
|
|
/// Setter function for CIM.
|
|
|
|
///This function check if the cim is valid using the validate_params method.
|
|
|
|
///
|
|
|
|
///- **Valid cim inserted**: it substitute the CIM in self.cim and return Ok(())
|
|
|
|
/// This function checks if the CIM is valid using the [`validate_params`](self::ParamsTrait::validate_params) method:
|
|
|
|
///- **Invalid cim inserted**: it replace the self.cim value with None and it retu ParamsError
|
|
|
|
/// * **Valid CIM inserted** - it substitutes the CIM in `self.cim` and returns `Ok(())`.
|
|
|
|
|
|
|
|
/// * **Invalid CIM inserted** - it replaces the `self.cim` value with `None` and it returns
|
|
|
|
|
|
|
|
/// `ParamsError`.
|
|
|
|
pub fn set_cim(&mut self, cim: Array3<f64>) -> Result<(), ParamsError> { |
|
|
|
pub fn set_cim(&mut self, cim: Array3<f64>) -> Result<(), ParamsError> { |
|
|
|
self.cim = Some(cim); |
|
|
|
self.cim = Some(cim); |
|
|
|
match self.validate_params() { |
|
|
|
match self.validate_params() { |
|
|
@ -124,27 +129,27 @@ impl DiscreteStatesContinousTimeParams { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///Unchecked version of the setter function for CIM.
|
|
|
|
/// Unchecked version of the setter function for CIM.
|
|
|
|
pub fn set_cim_unchecked(&mut self, cim: Array3<f64>) { |
|
|
|
pub fn set_cim_unchecked(&mut self, cim: Array3<f64>) { |
|
|
|
self.cim = Some(cim); |
|
|
|
self.cim = Some(cim); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///Getter function for transitions
|
|
|
|
/// Getter function for transitions.
|
|
|
|
pub fn get_transitions(&self) -> &Option<Array3<usize>> { |
|
|
|
pub fn get_transitions(&self) -> &Option<Array3<usize>> { |
|
|
|
&self.transitions |
|
|
|
&self.transitions |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///Setter function for transitions
|
|
|
|
/// Setter function for transitions.
|
|
|
|
pub fn set_transitions(&mut self, transitions: Array3<usize>) { |
|
|
|
pub fn set_transitions(&mut self, transitions: Array3<usize>) { |
|
|
|
self.transitions = Some(transitions); |
|
|
|
self.transitions = Some(transitions); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///Getter function for residence_time
|
|
|
|
/// Getter function for residence_time.
|
|
|
|
pub fn get_residence_time(&self) -> &Option<Array2<f64>> { |
|
|
|
pub fn get_residence_time(&self) -> &Option<Array2<f64>> { |
|
|
|
&self.residence_time |
|
|
|
&self.residence_time |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
///Setter function for residence_time
|
|
|
|
/// Setter function for residence_time.
|
|
|
|
pub fn set_residence_time(&mut self, residence_time: Array2<f64>) { |
|
|
|
pub fn set_residence_time(&mut self, residence_time: Array2<f64>) { |
|
|
|
self.residence_time = Some(residence_time); |
|
|
|
self.residence_time = Some(residence_time); |
|
|
|
} |
|
|
|
} |
|
|
|