use std::process::ExitCode;
use gresil::nir::Thousandths;
use gresil::{NirImport, NirImportOptions};
const GRAPH: &str = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/nir_fixtures/snntorch_two_layer_sim.json"
);
fn main() -> ExitCode {
let path = std::env::args().nth(1).unwrap_or(GRAPH.into());
match run(&path) {
Ok(trains) => {
let counts: Vec<usize> = trains.iter().map(Vec::len).collect();
println!("spikes per neuron over 100 steps: {counts:?}");
ExitCode::SUCCESS
}
Err(e) => {
eprintln!("{path}: {e}");
ExitCode::FAILURE
}
}
}
fn run(path: &str) -> Result<Vec<Vec<u32>>, Box<dyn std::error::Error>> {
let json = std::fs::read(path)?;
let graph = NirImport::from_json(&json, NirImportOptions::sim_units(100))?;
let (mut net, encoder, _report) = graph.build_network()?;
let mut trains = vec![Vec::new(); usize::from(net.neuron_count())];
for step in 0..100u32 {
let input = Thousandths(i32::from(matches!(step, 0..=9 | 50..=59)) * 1_000);
let currents = encoder.encode(&[&[input]]);
for spike in net.step(¤ts)? {
trains[usize::from(spike.neuron_id)].push(step);
}
}
Ok(trains)
}
#[cfg(test)]
mod tests {
#[test]
fn it_spikes_where_the_pinned_trace_spikes() {
let trains = super::run(super::GRAPH).expect("the fixture imports and steps");
assert_eq!(
trains,
[
vec![1, 3, 5, 7, 9, 51, 53, 55, 57, 59],
vec![4, 8, 52, 56, 60]
]
);
}
}