Skip to content

Commit 270e4c7

Browse files
authored
Merge pull request #9 from MarcoDiFrancesco/main
Implement print of DataStream
2 parents beb90c4 + 55608f9 commit 270e4c7

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ Cargo.lock
33
target/
44
*.csv
55
*.zip
6+
7+
# Local configuration
8+
.cargo/config.toml
File renamed without changes.

src/anomaly/half_space_tree.rs

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl<F: Float + FromPrimitive + AddAssign + SubAssign + MulAssign + DivAssign> T
3434
fn new(n_trees: u32, height: u32, features: &Vec<String>, rng: &mut ThreadRng) -> Self {
3535
// #nodes = 2 ^ height - 1
3636
let n_nodes: usize = usize::try_from(n_trees * (u32::pow(2, height) - 1)).unwrap();
37+
// Branches are "non-leaf nodes"
3738
// #branches = 2 ^ (height - 1) - 1
3839
let n_branches = usize::try_from(n_trees * (u32::pow(2, height - 1) - 1)).unwrap();
3940

src/stream/data_stream.rs

+37
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::{HashMap, HashSet};
2+
use std::fmt;
23

34
use crate::common::{ClassifierTarget, Observation};
45
use num::Float;
@@ -47,6 +48,18 @@ pub enum Data<F: Float + std::str::FromStr> {
4748
Bool(bool),
4849
String(String),
4950
}
51+
52+
impl<F: Float + fmt::Display + std::str::FromStr> fmt::Display for Data<F> {
53+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
match self {
55+
Data::Scalar(v) => write!(f, "{}", v),
56+
Data::Int(v) => write!(f, "{}", v),
57+
Data::Bool(v) => write!(f, "{}", v),
58+
Data::String(v) => write!(f, "{}", v),
59+
}
60+
}
61+
}
62+
5063
impl<F: Float + std::fmt::Display + std::str::FromStr> Data<F> {
5164
pub fn to_float(&self) -> Result<F, &str> {
5265
match self {
@@ -72,6 +85,30 @@ pub enum DataStream<F: Float + std::str::FromStr> {
7285
XY(HashMap<String, Data<F>>, HashMap<String, Data<F>>),
7386
}
7487

88+
impl<F: Float + fmt::Display + std::str::FromStr> fmt::Display for DataStream<F> {
89+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90+
fn fmt_hashmap<F: Float + fmt::Display + std::str::FromStr>(
91+
f: &mut fmt::Formatter<'_>,
92+
hm: &HashMap<String, Data<F>>,
93+
hm_name: &str,
94+
) -> fmt::Result {
95+
write!(f, "{hm_name}: [")?;
96+
for (key, value) in hm {
97+
write!(f, " {}: {},", key, value)?;
98+
}
99+
write!(f, " ] ")
100+
}
101+
102+
match self {
103+
DataStream::X(x) => fmt_hashmap(f, x, "X"),
104+
DataStream::XY(x, y) => {
105+
fmt_hashmap(f, x, "X")?;
106+
fmt_hashmap(f, y, "Y")
107+
}
108+
}
109+
}
110+
}
111+
75112
impl<F: Float + std::str::FromStr + std::fmt::Display> DataStream<F> {
76113
pub fn get_x(&self) -> &HashMap<String, Data<F>> {
77114
match self {

0 commit comments

Comments
 (0)