use std::path::Path;
use crate::analysis::AnalysisReport;
use crate::error::Result;
use crate::svg::{
write_bar_svg, write_line_svg, write_raster_svg as write_svg_raster, SvgChartOptions, SvgSeries,
};
fn default_opts(title: &str, xlabel: &str, ylabel: &str, w: u32, h: u32) -> SvgChartOptions {
SvgChartOptions {
width: w,
height: h,
title: title.into(),
xlabel: xlabel.into(),
ylabel: ylabel.into(),
..Default::default()
}
}
pub fn write_asdr_svg(
report: &AnalysisReport,
path: impl AsRef<Path>,
w: u32,
h: u32,
) -> Result<()> {
let points: Vec<(f64, f64)> =
report.asdr.points.iter().map(|p| (p.time_ms, p.smoothed_hz)).collect();
write_line_svg(
&[SvgSeries { label: "ASDR".into(), points, color: "#2563eb".into(), fill: false }],
&default_opts("ASDR", "Time (ms)", "Rate (Hz)", w, h),
path,
)
}
#[cfg(feature = "correlation")]
pub fn write_botcc_svg(
report: &AnalysisReport,
path: impl AsRef<Path>,
w: u32,
h: u32,
) -> Result<()> {
let points: Vec<(f64, f64)> = report
.correlogram
.lag_ms
.iter()
.zip(&report.correlogram.counts)
.map(|(&lag, &c)| (lag, c as f64))
.collect();
write_line_svg(
&[SvgSeries { label: "BOTCC".into(), points, color: "#0891b2".into(), fill: true }],
&default_opts("Burst onset correlogram", "Lag (ms)", "Count", w, h),
path,
)
}
#[cfg(not(feature = "correlation"))]
pub fn write_botcc_svg(
_report: &AnalysisReport,
_path: impl AsRef<Path>,
_w: u32,
_h: u32,
) -> Result<()> {
Err(crate::error::Med64Error::InvalidWrite {
message: "BOTCC SVG requires correlation feature".into(),
})
}
pub fn write_raster_svg(
report: &AnalysisReport,
path: impl AsRef<Path>,
w: u32,
h: u32,
) -> Result<()> {
let mut channels: Vec<usize> = report.spikes.spikes.iter().map(|s| s.channel).collect();
channels.sort_unstable();
channels.dedup();
if channels.is_empty() {
channels = report.header.active_channel_indices();
}
let mut by_ch: std::collections::BTreeMap<usize, Vec<f64>> = std::collections::BTreeMap::new();
for spike in &report.spikes.spikes {
by_ch.entry(spike.channel).or_default().push(spike.within_trace_time_ms);
}
let rows: Vec<(usize, Vec<f64>)> =
channels.iter().map(|&ch| (ch, by_ch.remove(&ch).unwrap_or_default())).collect();
let t_max = report.spikes.spikes.iter().map(|s| s.within_trace_time_ms).fold(1.0_f64, f64::max);
write_svg_raster(
&rows,
&default_opts("Spike raster", "Time (ms)", "Channel", w, h),
0.0,
t_max,
path,
)
}
#[cfg(feature = "correlation")]
pub fn write_synchrony_svg(
report: &AnalysisReport,
path: impl AsRef<Path>,
w: u32,
h: u32,
) -> Result<()> {
let bars: Vec<(String, f64)> = report
.synchrony
.iter()
.take(64)
.map(|r| (format!("{}-{}", r.channel_a + 1, r.channel_b + 1), r.synchrony_index))
.collect();
let mut opts = default_opts("Synchrony index", "Channel pair", "Index", w, h);
opts.margin_bottom = 56.0;
write_bar_svg(&bars, &opts, path)
}
#[cfg(not(feature = "correlation"))]
pub fn write_synchrony_svg(
_report: &AnalysisReport,
_path: impl AsRef<Path>,
_w: u32,
_h: u32,
) -> Result<()> {
Err(crate::error::Med64Error::InvalidWrite {
message: "synchrony SVG requires correlation feature".into(),
})
}