Skip to content

Commit 6e92b3f

Browse files
committed
Linting and formatting.
Signed-off-by: Marvin Hansen <[email protected]>
1 parent cc60832 commit 6e92b3f

File tree

3 files changed

+96
-41
lines changed

3 files changed

+96
-41
lines changed

examples/epp_scm/src/rung1_association.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,38 @@ use deep_causality::*;
77

88
// Define Causaloids
99
fn get_smoking_causaloid() -> BaseCausaloid {
10-
Causaloid::new(1, |effect| {
11-
let nicotine_level = effect.as_numerical().unwrap_or(0.0);
12-
Ok(PropagatingEffect::Deterministic(nicotine_level > 0.6))
13-
}, "Smoking Status")
10+
Causaloid::new(
11+
1,
12+
|effect| {
13+
let nicotine_level = effect.as_numerical().unwrap_or(0.0);
14+
Ok(PropagatingEffect::Deterministic(nicotine_level > 0.6))
15+
},
16+
"Smoking Status",
17+
)
1418
}
1519

1620
fn get_tar_causaloid() -> BaseCausaloid {
17-
Causaloid::new(2, |effect| {
18-
// Tar is present if the preceding cause (smoking) is active.
19-
let is_smoking = effect.as_bool().unwrap_or(false);
20-
Ok(PropagatingEffect::Deterministic(is_smoking))
21-
}, "Tar in Lungs")
21+
Causaloid::new(
22+
2,
23+
|effect| {
24+
// Tar is present if the preceding cause (smoking) is active.
25+
let is_smoking = effect.as_bool().unwrap_or(false);
26+
Ok(PropagatingEffect::Deterministic(is_smoking))
27+
},
28+
"Tar in Lungs",
29+
)
2230
}
2331

2432
fn get_cancer_risk_causaloid() -> BaseCausaloid {
25-
Causaloid::new(3, |effect| {
26-
// Cancer risk is high if tar is present.
27-
let has_tar = effect.as_bool().unwrap_or(false);
28-
Ok(PropagatingEffect::Deterministic(has_tar))
29-
}, "Cancer Risk")
33+
Causaloid::new(
34+
3,
35+
|effect| {
36+
// Cancer risk is high if tar is present.
37+
let has_tar = effect.as_bool().unwrap_or(false);
38+
Ok(PropagatingEffect::Deterministic(has_tar))
39+
},
40+
"Cancer Risk",
41+
)
3042
}
3143

3244
pub fn run_rung1_association() {
@@ -46,9 +58,11 @@ pub fn run_rung1_association() {
4658
// 2. Execute and Observe
4759
// Represents observing a high nicotine level.
4860
let initial_effect = PropagatingEffect::Numerical(0.8);
49-
61+
5062
// Evaluate the full chain of events starting from the first cause.
51-
let final_effect = graph.evaluate_shortest_path_between_causes(smoke_idx, cancer_idx, &initial_effect).unwrap();
63+
let final_effect = graph
64+
.evaluate_shortest_path_between_causes(smoke_idx, cancer_idx, &initial_effect)
65+
.unwrap();
5266

5367
// 3. Assert and Explain
5468
assert_eq!(final_effect, PropagatingEffect::Deterministic(true));

examples/epp_scm/src/rung2_intervention.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,36 @@
66
use deep_causality::*;
77

88
fn get_smoking_causaloid() -> BaseCausaloid {
9-
Causaloid::new(1, |effect| {
10-
let nicotine_level = effect.as_numerical().unwrap_or(0.0);
11-
Ok(PropagatingEffect::Deterministic(nicotine_level > 0.6))
12-
}, "Smoking Status")
9+
Causaloid::new(
10+
1,
11+
|effect| {
12+
let nicotine_level = effect.as_numerical().unwrap_or(0.0);
13+
Ok(PropagatingEffect::Deterministic(nicotine_level > 0.6))
14+
},
15+
"Smoking Status",
16+
)
1317
}
1418

1519
fn get_tar_causaloid() -> BaseCausaloid {
16-
Causaloid::new(2, |effect| {
17-
let is_smoking = effect.as_bool().unwrap_or(false);
18-
Ok(PropagatingEffect::Deterministic(is_smoking))
19-
}, "Tar in Lungs")
20+
Causaloid::new(
21+
2,
22+
|effect| {
23+
let is_smoking = effect.as_bool().unwrap_or(false);
24+
Ok(PropagatingEffect::Deterministic(is_smoking))
25+
},
26+
"Tar in Lungs",
27+
)
2028
}
2129

2230
fn get_cancer_risk_causaloid() -> BaseCausaloid {
23-
Causaloid::new(3, |effect| {
24-
let has_tar = effect.as_bool().unwrap_or(false);
25-
Ok(PropagatingEffect::Deterministic(has_tar))
26-
}, "Cancer Risk")
31+
Causaloid::new(
32+
3,
33+
|effect| {
34+
let has_tar = effect.as_bool().unwrap_or(false);
35+
Ok(PropagatingEffect::Deterministic(has_tar))
36+
},
37+
"Cancer Risk",
38+
)
2739
}
2840

2941
pub fn run_rung2_intervention() {
@@ -44,8 +56,8 @@ pub fn run_rung2_intervention() {
4456

4557
// 2. Define State and Action
4658
let high_cancer_risk_state = CausalState::new(
47-
10, // state ID
48-
1, // version
59+
10, // state ID
60+
1, // version
4961
PropagatingEffect::Deterministic(true), // The data to evaluate against the causaloid
5062
final_risk_causaloid,
5163
);
@@ -66,7 +78,9 @@ pub fn run_rung2_intervention() {
6678
// 4. Execute and Intervene
6779
// We need to evaluate the full causal chain first to get the final effect.
6880
let initial_effect = PropagatingEffect::Numerical(0.8);
69-
let final_effect = graph.evaluate_shortest_path_between_causes(smoke_idx, cancer_idx, &initial_effect).unwrap();
81+
let final_effect = graph
82+
.evaluate_shortest_path_between_causes(smoke_idx, cancer_idx, &initial_effect)
83+
.unwrap();
7084

7185
// Now, use the final effect as input to the CSM.
7286
// The CSM will check if the `high_cancer_risk_state` is met by this effect.

examples/epp_scm/src/rung3_counterfactual.rs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ fn contextual_cancer_risk_logic(
4646

4747
pub fn run_rung3_counterfactual() {
4848
println!("--- Rung 3: Counterfactual ---");
49-
println!("Query: Given a smoker with high tar, what would their cancer risk be if they hadn't smoked?");
49+
println!(
50+
"Query: Given a smoker with high tar, what would their cancer risk be if they hadn't smoked?"
51+
);
5052

5153
// 1. Define the Causaloid with our contextual logic
5254
let cancer_risk_causaloid = Causaloid::new_with_context(
@@ -58,15 +60,28 @@ pub fn run_rung3_counterfactual() {
5860

5961
// 2. Create Factual Context: A person who smokes and has high tar.
6062
let mut factual_context = BaseContext::with_capacity(1, "Factual", 5);
61-
factual_context.add_node(Contextoid::new(1, ContextoidType::Datoid(Data::new(NICOTINE_ID, 0.8)))).unwrap();
62-
factual_context.add_node(Contextoid::new(2, ContextoidType::Datoid(Data::new(TAR_ID, 0.8)))).unwrap();
63+
factual_context
64+
.add_node(Contextoid::new(
65+
1,
66+
ContextoidType::Datoid(Data::new(NICOTINE_ID, 0.8)),
67+
))
68+
.unwrap();
69+
factual_context
70+
.add_node(Contextoid::new(
71+
2,
72+
ContextoidType::Datoid(Data::new(TAR_ID, 0.8)),
73+
))
74+
.unwrap();
6375

6476
// 3. Create Counterfactual Context: Same person, but we hypothetically set smoking to zero.
6577
let mut counterfactual_context = factual_context.clone();
6678
// To update, we need to know the index. In this simple case, it's 0.
6779
// A real implementation might use a HashMap<ID, Index> for lookup.
68-
let new_nicotine_datoid = Contextoid::new(1, ContextoidType::Datoid(Data::new(NICOTINE_ID, 0.1)));
69-
counterfactual_context.update_node(1, new_nicotine_datoid).unwrap();
80+
let new_nicotine_datoid =
81+
Contextoid::new(1, ContextoidType::Datoid(Data::new(NICOTINE_ID, 0.1)));
82+
counterfactual_context
83+
.update_node(1, new_nicotine_datoid)
84+
.unwrap();
7085

7186
// 4. Evaluate Both Scenarios
7287
let mut factual_causaloid = cancer_risk_causaloid.clone();
@@ -75,16 +90,28 @@ pub fn run_rung3_counterfactual() {
7590
let mut counterfactual_causaloid = cancer_risk_causaloid.clone();
7691
counterfactual_causaloid.set_context(Some(Arc::new(counterfactual_context)));
7792

78-
let factual_risk = factual_causaloid.evaluate(&PropagatingEffect::None).unwrap();
79-
let counterfactual_risk = counterfactual_causaloid.evaluate(&PropagatingEffect::None).unwrap();
93+
let factual_risk = factual_causaloid
94+
.evaluate(&PropagatingEffect::None)
95+
.unwrap();
96+
let counterfactual_risk = counterfactual_causaloid
97+
.evaluate(&PropagatingEffect::None)
98+
.unwrap();
8099

81100
// 5. Assert and Explain
82-
println!("Factual Result (smoker with high tar): Cancer risk is high -> {}", factual_risk.as_bool().unwrap());
83-
println!("Counterfactual Result (non-smoker with high tar): Cancer risk is high -> {}", counterfactual_risk.as_bool().unwrap());
101+
println!(
102+
"Factual Result (smoker with high tar): Cancer risk is high -> {}",
103+
factual_risk.as_bool().unwrap()
104+
);
105+
println!(
106+
"Counterfactual Result (non-smoker with high tar): Cancer risk is high -> {}",
107+
counterfactual_risk.as_bool().unwrap()
108+
);
84109

85110
assert_eq!(factual_risk, PropagatingEffect::Deterministic(true));
86111
assert_eq!(counterfactual_risk, PropagatingEffect::Deterministic(true));
87112

88-
println!("Conclusion: The cancer risk remains high in the counterfactual world because the direct cause (tar) was not undone.");
113+
println!(
114+
"Conclusion: The cancer risk remains high in the counterfactual world because the direct cause (tar) was not undone."
115+
);
89116
println!("\n");
90117
}

0 commit comments

Comments
 (0)