Skip to content

Commit

Permalink
Merge pull request #9 from MarcoDiFrancesco/main
Browse files Browse the repository at this point in the history
Implement print of DataStream
  • Loading branch information
AdilZouitine authored Apr 5, 2024
2 parents beb90c4 + 55608f9 commit 270e4c7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ Cargo.lock
target/
*.csv
*.zip

# Local configuration
.cargo/config.toml
File renamed without changes.
1 change: 1 addition & 0 deletions src/anomaly/half_space_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl<F: Float + FromPrimitive + AddAssign + SubAssign + MulAssign + DivAssign> T
fn new(n_trees: u32, height: u32, features: &Vec<String>, rng: &mut ThreadRng) -> Self {
// #nodes = 2 ^ height - 1
let n_nodes: usize = usize::try_from(n_trees * (u32::pow(2, height) - 1)).unwrap();
// Branches are "non-leaf nodes"
// #branches = 2 ^ (height - 1) - 1
let n_branches = usize::try_from(n_trees * (u32::pow(2, height - 1) - 1)).unwrap();

Expand Down
37 changes: 37 additions & 0 deletions src/stream/data_stream.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::fmt;

use crate::common::{ClassifierTarget, Observation};
use num::Float;
Expand Down Expand Up @@ -47,6 +48,18 @@ pub enum Data<F: Float + std::str::FromStr> {
Bool(bool),
String(String),
}

impl<F: Float + fmt::Display + std::str::FromStr> fmt::Display for Data<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Data::Scalar(v) => write!(f, "{}", v),
Data::Int(v) => write!(f, "{}", v),
Data::Bool(v) => write!(f, "{}", v),
Data::String(v) => write!(f, "{}", v),
}
}
}

impl<F: Float + std::fmt::Display + std::str::FromStr> Data<F> {
pub fn to_float(&self) -> Result<F, &str> {
match self {
Expand All @@ -72,6 +85,30 @@ pub enum DataStream<F: Float + std::str::FromStr> {
XY(HashMap<String, Data<F>>, HashMap<String, Data<F>>),
}

impl<F: Float + fmt::Display + std::str::FromStr> fmt::Display for DataStream<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fn fmt_hashmap<F: Float + fmt::Display + std::str::FromStr>(
f: &mut fmt::Formatter<'_>,
hm: &HashMap<String, Data<F>>,
hm_name: &str,
) -> fmt::Result {
write!(f, "{hm_name}: [")?;
for (key, value) in hm {
write!(f, " {}: {},", key, value)?;
}
write!(f, " ] ")
}

match self {
DataStream::X(x) => fmt_hashmap(f, x, "X"),
DataStream::XY(x, y) => {
fmt_hashmap(f, x, "X")?;
fmt_hashmap(f, y, "Y")
}
}
}
}

impl<F: Float + std::str::FromStr + std::fmt::Display> DataStream<F> {
pub fn get_x(&self) -> &HashMap<String, Data<F>> {
match self {
Expand Down

0 comments on commit 270e4c7

Please sign in to comment.