Biomedical
TTFields electrode optimization for a glioblastoma volume
An autodiff gradient-ascent optimizer ascends the exact efficacy gradient through a CausalFlow chain; a non-finite gradient short-circuits the error channel.
This example lives at tumor_treatment. It models the Tumor Treating Fields (TTFields) optimization problem on a glioblastoma volume: given a 3D grid of tumor voxels with directional cell-division axes, find the electrode angle that maximizes disruption.
The optimizer ascends the exact gradient. Each electrode orientation (theta, phi) has an efficacy ⟨|E(θ,φ)·axis|⟩, and because that objective is a DifferentiableField, its gradient comes straight from autodiff rather than from random perturbations. The loop starts just off the degenerate θ = 0 pole and steps along ∇efficacy for a fixed number of iterations.
The interesting move is the wrapper: the whole ascent runs inside a CausalFlow. A single bind runs the gradient-ascent step function, and a non-finite gradient drops the flow into its error channel instead of returning a bad orientation.
The chain
use deep_causality_core::{CausalFlow, PropagatingEffect};
// Start just off the degenerate θ = 0 pole, then ascend the exact gradient.
let start = [ft(0.1), ft(0.1)];
let pipeline = CausalFlow::value(start)
.bind(move |p, _, _| match p.into_value() {
Some(s) => ascend(&efficacy, s, learning_rate, ASCENT_STEPS),
None => fail("no starting orientation"),
})
.into_effect();
match pipeline.value.into_value() {
Some(r) => println!("θ={:.3}, φ={:.3} over {} steps", r.theta, r.phi, r.steps),
None => eprintln!("Optimization failed: {:?}", pipeline.error),
}
The bind runs ascend, which walks the gradient for ASCENT_STEPS iterations and returns a PropagatingEffect<Report> carrying the final orientation. into_effect() drops back to the concrete carrier so the result block can read the report off the value channel, or print the error if the gradient ever left the finite range.
What to look at in the source
model.rs:Efficacy, itsDifferentiableFieldgradient,build_mock_tumor, and the physics that turns an electrode angle into a disruption score.main.rs: theascendgradient-ascent loop and theCausalFlowchain that wraps it.
The crate ships sibling examples for protein folding, tissue classification, aneurysm hemodynamics, decompression, and epilepsy. They live next to this one under examples/medicine_examples/.
Run it
git clone https://github.com/deepcausality-rs/deep_causality
cd deep_causality
cargo run --release -p medicine_examples --example tumor_treatment
The output prints one line per ascent step with the current efficacy, the orientation, and the gradient.
Why this is a good fit
The ascent is glue; the physics is heavy. Wrapping it in a CausalFlow keeps the result handling indifferent to the implementation. Swap the efficacy field for a different model, the chain does not change. A non-finite gradient short-circuits cleanly through the error channel instead of propagating a bad orientation.