Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new starter example #103

Merged
merged 7 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ In your project folder, just run in a terminal:
cargo add deep_causality
```

## 🚀 Getting Started

See the [starter example](deep_causality/examples/starter/src/main.rs).


## How to run the example code

```bash
Expand All @@ -109,6 +114,9 @@ cargo run --release --bin example-ctx

# Smoking inference
cargo run --release --bin example-smoking

# Getting started example
cargo run --release --bin starter
```

## 📦 Sub-Crates
Expand Down
14 changes: 14 additions & 0 deletions deep_causality/examples/starter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "starter"
version = "0.1.0"
edition = "2021"
rust-version = "1.65"
publish = false


# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
# For testing with latest commit in main
#deep_causality = { git = "https://github.com/deepcausality-rs/deep_causality", branch = "main" }
deep_causality = "0.6"
108 changes: 108 additions & 0 deletions deep_causality/examples/starter/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
use deep_causality::prelude::*;

fn main() {
println!();
println!("Build new causality graph");
let g = get_multi_cause_graph();
println!();

println!("Verify that the graph is fully inactive");
let percent_active = g.percent_active();
assert_eq!(percent_active, 0.0);
assert_eq!(g.number_active(), 0.0);
assert!(!g.all_active());
println!();

println!("Full reasoning over the entire graph");
let data = [0.99; 5]; // sample data
let res = g
.reason_all_causes(&data, None)
.expect("Failed to reason over the entire graph");
assert!(res);
println!();

println!("Verify that the graph is fully active");
assert_eq!(g.percent_active(), 100.0);
assert!(g.all_active());
println!();

println!("Partial reasoning over shortest path through the graph");
let start_index = 2;
let stop_index = 3;
let res = g
.reason_shortest_path_between_causes(start_index, stop_index, &data, None)
.unwrap();
assert!(res);
println!();

println!("Explain partial reasoning");
let expl = g
.explain_shortest_path_between_causes(start_index, stop_index)
.unwrap();

println!("{}", expl);
}

fn get_test_causaloid<'l>(id: IdentificationValue) -> BaseCausaloid<'l> {
let description = "tests whether data exceeds threshold of 0.55";
fn causal_fn(obs: NumericalValue) -> Result<bool, CausalityError> {
if obs.is_sign_negative() {
return Err(CausalityError("Observation is negative".into()));
}

let threshold: NumericalValue = 0.55;
if !obs.ge(&threshold) {
Ok(false)
} else {
Ok(true)
}
}

Causaloid::new(id, causal_fn, description)
}

fn get_multi_cause_graph<'l>() -> BaseCausalGraph<'l> {
// Builds a multi cause graph:
// root
// / \
// A B
// \ /
// C

// Create a new causaloid graph
let mut g = CausaloidGraph::new();

// Add root causaloid
let root_causaloid = get_test_causaloid(0);
let root_index = g.add_root_causaloid(root_causaloid);

// Add causaloid A
let causaloid = get_test_causaloid(1);
let idx_a = g.add_causaloid(causaloid);

// Link causaloid A to root causaloid
g.add_edge(root_index, idx_a)
.expect("Failed to add edge between root and A");

// Add causaloid B
let causaloid = get_test_causaloid(2);
let idx_b = g.add_causaloid(causaloid);

// Link causaloid B to root causaloid
g.add_edge(root_index, idx_b)
.expect("Failed to add edge between root and B");

// Add causaloid C
let causaloid = get_test_causaloid(3);
let idx_c = g.add_causaloid(causaloid);

// Link causaloid C to A
g.add_edge(idx_a, idx_c)
.expect("Failed to add edge between A and C");

// Link causaloid C to B
g.add_edge(idx_b, idx_c)
.expect("Failed to add edge between C and B");

g
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ fn test_explain_subgraph_from_cause() {
let all_true = g.all_active();
assert!(!all_true);

let res = g.reason_all_causes(&data, None).expect("`");
let res = g
.reason_all_causes(&data, None)
.expect("Failed to reason over the entire graph");
assert!(res);

// Verify that the graph is fully active.
Expand Down
1 change: 0 additions & 1 deletion deep_causality/tests/utils/test_utils_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Copyright (c) "2023" . The DeepCausality Authors. All Rights Reserved.

use deep_causality::prelude::*;
use deep_causality::protocols::causable_graph::graph::CausableGraph;

const SMALL: usize = 9;
// const MEDIUM: usize = 1_00;
Expand Down
14 changes: 13 additions & 1 deletion scripts/example.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,44 @@ echo "Select example to run: "
echo "--------------------------------"
echo "csm: Causal state machine"
echo "ctx: Causal model with base (static) context"
echo "starter: Simplest getting started example"
echo "smoking: Simple causal model without Context"
echo "--------------------------------"
echo ""

select opt in csm ctx smoking quit;
select opt in csm ctx starter smoking quit;
do
case $opt in

csm)
echo "Selected example: CSM (Causal State Machine)"
command cargo run --release --bin example-csm
break
;;

ctx)
echo "Selected example: CTX (Context)"
command cargo run --release --bin example-ctx
break
;;

starter)
echo "Selected example: Starter (Starter)"
command cargo run --release --bin starter
break
;;

smoking)
echo "Selected example: SMOKING (Smoking)"
command cargo run --release --bin example-smoking
break
;;

quit)
echo "Exiting!"
exit 0
;;

*)
echo "Invalid option $REPLY"
;;
Expand Down
Loading