From 8aa8d2b56edfb48a5f9293a079016ba86d32e4f7 Mon Sep 17 00:00:00 2001 From: Marvin Hansen Date: Wed, 2 Aug 2023 13:02:25 +0800 Subject: [PATCH] Completed ctx code example --- Cargo.toml | 2 +- deep_causality/examples/ctx/Cargo.toml | 2 +- deep_causality/examples/ctx/src/main.rs | 1 + deep_causality/examples/ctx/src/model/mod.rs | 3 ++- deep_causality/examples/ctx/src/run.rs | 15 +++++---------- .../examples/ctx/src/types/date_time_bar.rs | 3 --- deep_causality/examples/ctx/src/utils.rs | 9 +++++++++ 7 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 deep_causality/examples/ctx/src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index d2529166..d0c7e3e3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ resolver = "2" members = [ "deep_causality/examples/csm", -# "deep_causality/examples/ctx", + "deep_causality/examples/ctx", "deep_causality/examples/smoking", "dcl_data_structures", "deep_causality", diff --git a/deep_causality/examples/ctx/Cargo.toml b/deep_causality/examples/ctx/Cargo.toml index 8e135e6f..8ac497fb 100644 --- a/deep_causality/examples/ctx/Cargo.toml +++ b/deep_causality/examples/ctx/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] #deep_causality = "0.2.8" -deep_causality = { git = "https://github.com/deepcausality-rs/deep_causality.git", rev = "c2c316246b840ed4c9f279fb5ff95c437c8a84b1"} +deep_causality = { git = "https://github.com/deepcausality-rs/deep_causality.git", rev = "e3e121f99e177c25de18ffd2e3ab319bae9faa01"} # chrono default-features = false mitigates "RUSTSEC-2020-0071". # See https://rustsec.org/advisories/RUSTSEC-2020-0071.html diff --git a/deep_causality/examples/ctx/src/main.rs b/deep_causality/examples/ctx/src/main.rs index 23fa7654..421d28dc 100644 --- a/deep_causality/examples/ctx/src/main.rs +++ b/deep_causality/examples/ctx/src/main.rs @@ -7,6 +7,7 @@ mod workflow; mod io; mod config; mod model; +mod utils; use deep_causality::prelude::time_execution; diff --git a/deep_causality/examples/ctx/src/model/mod.rs b/deep_causality/examples/ctx/src/model/mod.rs index 3655c854..1d57bd13 100644 --- a/deep_causality/examples/ctx/src/model/mod.rs +++ b/deep_causality/examples/ctx/src/model/mod.rs @@ -36,9 +36,10 @@ fn build_causaloid<'l>( let _ = g.add_edge(root_index, month_index); + // Here we wrap the causal graph into a causaloid Causaloid::from_causal_graph_with_context( 0, - &g, + g, Option::from(context), "Causaloid main graph" ) diff --git a/deep_causality/examples/ctx/src/run.rs b/deep_causality/examples/ctx/src/run.rs index b63f474d..2dce2a81 100644 --- a/deep_causality/examples/ctx/src/run.rs +++ b/deep_causality/examples/ctx/src/run.rs @@ -1,8 +1,9 @@ // Copyright (c) "2023" . Marvin Hansen All rights reserved. -use std::time::{Duration, Instant}; +use std::time::Instant; use deep_causality::prelude::{Contextuable, Identifiable, TimeScale}; use crate::model::get_main_causaloid; +use crate::utils; use crate::workflow::{build_time_data_context, load_data}; use crate::workflow::build_model::build_model; @@ -34,7 +35,7 @@ pub fn run() // Reading parquet files is at least 10x faster than reading CSV files. let elapsed = &lap.elapsed(); - print_duration("Load Data", elapsed); + utils::print_duration("Load Data", elapsed); // This context hypergraph is low resolution (Day), relatively small (<1k nodes), // and thus takes only a few milliseconds to generate. In practice, @@ -50,7 +51,7 @@ pub fn run() }; let elapsed = &lap.elapsed(); - print_duration("Build Context HyperGraph", elapsed); + utils::print_duration("Build Context HyperGraph", elapsed); // Print out some key metrics of the context graph. println!("Context HyperGraph Metrics:"); @@ -67,16 +68,10 @@ pub fn run() Err(e) => panic!("{}", e), }; let elapsed = &lap.elapsed(); - print_duration("Build Causal Model", elapsed); + utils::print_duration("Build Causal Model", elapsed); println!("Causal Model:"); println!("Model ID: {}", model.id()); println!("Model Description: {}", model.description()); println!(); } - -fn print_duration(msg: &str, elapsed: &Duration) -{ - println!("{} took: {:?} ", msg, elapsed); - println!(); -} diff --git a/deep_causality/examples/ctx/src/types/date_time_bar.rs b/deep_causality/examples/ctx/src/types/date_time_bar.rs index 85bc060e..11490866 100644 --- a/deep_causality/examples/ctx/src/types/date_time_bar.rs +++ b/deep_causality/examples/ctx/src/types/date_time_bar.rs @@ -59,9 +59,6 @@ impl DateTimeBar pub fn high(&self) -> Decimal { self.high } - pub fn low(&self) -> Decimal { - self.low - } pub fn close(&self) -> Decimal { self.close } diff --git a/deep_causality/examples/ctx/src/utils.rs b/deep_causality/examples/ctx/src/utils.rs new file mode 100644 index 00000000..26e7cc5a --- /dev/null +++ b/deep_causality/examples/ctx/src/utils.rs @@ -0,0 +1,9 @@ +// Copyright (c) "2023" . Marvin Hansen All rights reserved. + +use std::time::Duration; + +pub fn print_duration(msg: &str, elapsed: &Duration) +{ + println!("{} took: {:?} ", msg, elapsed); + println!(); +}