Skip to content

Commit 8f32463

Browse files
committed
update and use less emojis
1 parent b99b3f4 commit 8f32463

File tree

2 files changed

+73
-53
lines changed

2 files changed

+73
-53
lines changed

examples/sample_callback.rs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ fn main() -> Result<()> {
179179
// Print progress information periodically
180180
if *count <= 10 {
181181
println!(
182-
"📊 Progress callback #{}: Elapsed: {:.1}s, {} chains",
182+
"Progress callback #{}: Elapsed: {:.1}s, {} chains",
183183
count,
184184
elapsed.as_secs_f64(),
185185
chains.len()
@@ -188,26 +188,45 @@ fn main() -> Result<()> {
188188
for chain_progress in chains.iter() {
189189
// Access the latest sample data if available
190190
if let Some(sample_data) = &chain_progress.latest_sample {
191+
// Demonstrate accessing optional fields with proper handling
192+
let energy_str = sample_data
193+
.draw_energy
194+
.map(|e| format!("{:.3}", e))
195+
.unwrap_or_else(|| "N/A".to_string());
196+
let diverging_str = sample_data
197+
.diverging
198+
.map(|d| d.to_string())
199+
.unwrap_or_else(|| "N/A".to_string());
200+
let tree_depth_str = sample_data
201+
.tree_depth
202+
.map(|d| d.to_string())
203+
.unwrap_or_else(|| "N/A".to_string());
204+
191205
println!(
192-
" Chain {}: Draw {}/{}, Energy: {:.3}, Diverging: {}, Tree depth: {}",
206+
" Chain {}: Draw {}/{}, Energy: {}, Diverging: {}, Tree depth: {}",
193207
sample_data.chain_id,
194208
chain_progress.finished_draws,
195209
chain_progress.total_draws,
196-
sample_data.energy,
197-
sample_data.diverging,
198-
sample_data.tree_depth
199-
);
200-
println!(
201-
" Position: [{:.4}, {:.4}]",
202-
sample_data.position[0], sample_data.position[1]
203-
);
204-
println!(
205-
" Step size: {:.6}, Tuning: {}",
206-
sample_data.step_size, sample_data.is_tuning
210+
energy_str,
211+
diverging_str,
212+
tree_depth_str
207213
);
208214

215+
if let Some(step_size) = sample_data.step_size {
216+
println!(
217+
" Step size: {:.6}, Tuning: {}",
218+
step_size, sample_data.is_tuning
219+
);
220+
}
221+
222+
if let Some(max_depth) = sample_data.reached_max_treedepth {
223+
if max_depth {
224+
println!(" ⚠ Maximum tree depth reached!");
225+
}
226+
}
227+
209228
// Track divergences
210-
if sample_data.diverging {
229+
if sample_data.diverging.unwrap_or(false) {
211230
let mut div_count = divergence_count_clone.lock().unwrap();
212231
*div_count += 1;
213232
}
@@ -276,11 +295,14 @@ fn main() -> Result<()> {
276295
}
277296
}
278297

279-
println!("\n Example completed successfully!");
298+
println!("\n Example completed successfully!");
280299
println!("\nKey features demonstrated:");
281300
println!(" - ProgressCallback provides both chain progress and latest sample data");
282301
println!(" - Time-based rate limiting (10ms) prevents excessive overhead");
283-
println!(" - latest_sample includes rich data (position, energy, divergence, etc.)");
302+
println!(
303+
" - latest_sample includes rich optional data (energy, divergence, tree depth, etc.)"
304+
);
305+
println!(" - All sampler-specific stats are Option<T> for compatibility with other samplers");
284306
println!(" - Works seamlessly with multi-chain sampling");
285307
println!(" - Single callback mechanism for all monitoring needs");
286308

tests/sample_callback_test.rs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ fn test_sample_data_type_exists() {
1515
chain_id: 0,
1616
draw: 42,
1717
is_tuning: true,
18-
position: vec![1.0, 2.0, 3.0],
19-
energy: -10.5,
20-
diverging: false,
21-
tree_depth: 5,
22-
step_size: 0.1,
18+
tree_depth: Some(5),
19+
reached_max_treedepth: Some(false),
20+
diverging: Some(false),
21+
initial_energy: None,
22+
draw_energy: Some(-10.5),
23+
step_size: Some(0.1),
2324
};
24-
25-
println!("✅ SampleData struct exists and can be constructed");
2625
}
2726

2827
#[test]
@@ -49,18 +48,17 @@ fn test_progress_callback_creation() {
4948
let _ = sample_data.chain_id;
5049
let _ = sample_data.draw;
5150
let _ = sample_data.is_tuning;
52-
let _ = &sample_data.position;
53-
let _ = sample_data.energy;
54-
let _ = sample_data.diverging;
5551
let _ = sample_data.tree_depth;
52+
let _ = sample_data.reached_max_treedepth;
53+
let _ = sample_data.diverging;
54+
let _ = sample_data.initial_energy;
55+
let _ = sample_data.draw_energy;
5656
let _ = sample_data.step_size;
5757
}
5858
}
5959
}),
6060
rate: Duration::from_millis(100),
6161
};
62-
63-
println!("✅ ProgressCallback can be created with closure that accesses sample data");
6462
}
6563

6664
#[test]
@@ -70,26 +68,24 @@ fn test_chain_progress_with_sample_data() {
7068
chain_id: 1,
7169
draw: 10,
7270
is_tuning: true,
73-
position: vec![0.5, -0.3],
74-
energy: -5.2,
75-
diverging: false,
76-
tree_depth: 3,
77-
step_size: 0.05,
71+
tree_depth: Some(3),
72+
reached_max_treedepth: Some(false),
73+
diverging: Some(false),
74+
initial_energy: None,
75+
draw_energy: Some(-5.2),
76+
step_size: Some(0.05),
7877
};
7978

8079
// Verify we can access sample data fields
8180
assert_eq!(sample_data.chain_id, 1);
8281
assert_eq!(sample_data.draw, 10);
8382
assert!(sample_data.is_tuning);
84-
assert_eq!(sample_data.position.len(), 2);
85-
assert_eq!(sample_data.position[0], 0.5);
86-
assert_eq!(sample_data.position[1], -0.3);
87-
assert_eq!(sample_data.energy, -5.2);
88-
assert!(!sample_data.diverging);
89-
assert_eq!(sample_data.tree_depth, 3);
90-
assert_eq!(sample_data.step_size, 0.05);
91-
92-
println!("✅ Sample data can be created and accessed");
83+
assert_eq!(sample_data.tree_depth, Some(3));
84+
assert_eq!(sample_data.reached_max_treedepth, Some(false));
85+
assert_eq!(sample_data.diverging, Some(false));
86+
assert_eq!(sample_data.initial_energy, None);
87+
assert_eq!(sample_data.draw_energy, Some(-5.2));
88+
assert_eq!(sample_data.step_size, Some(0.05));
9389
}
9490

9591
#[test]
@@ -99,22 +95,25 @@ fn test_sample_data_clone() {
9995
chain_id: 0,
10096
draw: 1,
10197
is_tuning: false,
102-
position: vec![1.0, 2.0],
103-
energy: -3.0,
104-
diverging: true,
105-
tree_depth: 4,
106-
step_size: 0.1,
98+
tree_depth: Some(4),
99+
reached_max_treedepth: Some(true),
100+
diverging: Some(true),
101+
initial_energy: Some(-2.5),
102+
draw_energy: Some(-3.0),
103+
step_size: Some(0.1),
107104
};
108105

109106
let data2 = data1.clone();
110107

111108
assert_eq!(data1.chain_id, data2.chain_id);
112109
assert_eq!(data1.draw, data2.draw);
113-
assert_eq!(data1.position, data2.position);
114-
assert_eq!(data1.energy, data2.energy);
110+
assert_eq!(data1.is_tuning, data2.is_tuning);
111+
assert_eq!(data1.tree_depth, data2.tree_depth);
112+
assert_eq!(data1.reached_max_treedepth, data2.reached_max_treedepth);
115113
assert_eq!(data1.diverging, data2.diverging);
116-
117-
println!("✅ SampleData can be cloned");
114+
assert_eq!(data1.initial_energy, data2.initial_energy);
115+
assert_eq!(data1.draw_energy, data2.draw_energy);
116+
assert_eq!(data1.step_size, data2.step_size);
118117
}
119118

120119
#[test]
@@ -139,7 +138,7 @@ fn test_progress_callback_invocation() {
139138

140139
// Verify we can access the data
141140
let _ = sample_data.chain_id;
142-
let _ = sample_data.position.len();
141+
let _ = sample_data.draw_energy;
143142
}
144143
}
145144
}),
@@ -151,5 +150,4 @@ fn test_progress_callback_invocation() {
151150
(callback.callback)(Duration::from_secs(1), chains.into());
152151

153152
assert_eq!(*callback_count.lock().unwrap(), 1);
154-
println!("✅ ProgressCallback can be invoked");
155153
}

0 commit comments

Comments
 (0)