use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use serde_json::{json, Value};
use crate::analysis::log_io::AnalysisLogManifest;
use crate::analysis::{
average_spike_waveforms, compute_burst_result_table, import_spike_csv, AnalysisOptions,
AnalysisReport, ResultTableOptions, SpikeTrain, ThresholdMethod,
};
use crate::error::{Med64Error, Result};
use crate::export::write_spike_timestamps_csv_from_train;
#[derive(Debug, Clone, Default)]
pub struct NativeLogOptions {
pub well_names: Vec<String>,
pub split_traces: bool,
}
#[derive(Debug, Clone)]
pub struct NativeAnalysisLog {
pub dir: PathBuf,
pub spikes: SpikeTrain,
pub information: Value,
pub config: Value,
pub burst: Value,
pub results: Option<Value>,
pub wave_data: Option<Value>,
pub spike_thresholds: Option<Value>,
pub summary: Option<Value>,
}
pub fn write_native_analysis_log(
report: &AnalysisReport,
options: &AnalysisOptions,
dir: impl AsRef<Path>,
) -> Result<AnalysisLogManifest> {
write_native_analysis_log_with_options(report, options, dir, &NativeLogOptions::default())
}
pub fn write_native_analysis_log_with_options(
report: &AnalysisReport,
options: &AnalysisOptions,
dir: impl AsRef<Path>,
log_opts: &NativeLogOptions,
) -> Result<AnalysisLogManifest> {
let dir = dir.as_ref();
std::fs::create_dir_all(dir)
.map_err(|source| Med64Error::WriteIo { path: dir.to_path_buf(), source })?;
let mut files = Vec::new();
if log_opts.well_names.is_empty() {
write_native_log_set(report, options, dir, log_opts, "", &mut files)?;
} else {
for well in &log_opts.well_names {
let well_dir = dir.join(well);
std::fs::create_dir_all(&well_dir)
.map_err(|source| Med64Error::WriteIo { path: well_dir.clone(), source })?;
let prefix = format!("{well}/");
write_native_log_set(report, options, &well_dir, log_opts, &prefix, &mut files)?;
}
}
write_restore_sidecars(dir, &mut files)?;
Ok(AnalysisLogManifest { version: 1, source_path: report.path.clone(), files })
}
fn write_restore_sidecars(dir: &Path, files: &mut Vec<String>) -> Result<()> {
let restore_txt = dir.join("RESTORE.txt");
let body = format!(
"med64 native analysis log layout\n\
================================================\n\
Required extensionless JSON sidecars:\n\
information — n analyses, active channels (a), interval (i)\n\
config — thresholds, filters, burst params\n\
burst — single/multi burst + ASDR\n\
results — ResultTable metrics\n\
wave_data — average waveforms\n\
spike_thresholds — per-channel thresholds\n\
Spike timestamps:\n\
ts+1.csv … ts+N.csv (N = information.n)\n\
Optional:\n\
summary\n\
\n\
Restore Log: select this folder in the restore dialog.\n\
CLI: med64 log prepare-restore {}\n",
dir.display()
);
std::fs::write(&restore_txt, body)
.map_err(|source| Med64Error::WriteIo { path: restore_txt.clone(), source })?;
files.push("RESTORE.txt".into());
let restore_meta = dir.join("restore_layout.json");
let layout = json!({
"layout": "native_analysis_log",
"required": ["information", "config", "burst", "ts+1.csv"],
"optional": ["results", "wave_data", "spike_thresholds", "summary"],
"session_start_time_field": "Session Start Time",
"restore_hint": "Select this directory in a log restore UI",
});
std::fs::write(
&restore_meta,
serde_json::to_string_pretty(&layout).unwrap_or_else(|_| "{}".into()),
)
.map_err(|source| Med64Error::WriteIo { path: restore_meta.clone(), source })?;
files.push("restore_layout.json".into());
Ok(())
}
pub fn prepare_native_log_restore(dir: impl AsRef<Path>) -> Result<Vec<String>> {
let dir = dir.as_ref();
let required = ["information", "config", "burst"];
let mut missing = Vec::new();
let mut present = Vec::new();
for name in required {
let p = dir.join(name);
if p.is_file() {
present.push(name.to_string());
} else {
missing.push(name.to_string());
}
}
let mut has_ts = false;
if let Ok(rd) = std::fs::read_dir(dir) {
for e in rd.flatten() {
let n = e.file_name().to_string_lossy().into_owned();
if n.starts_with("ts+") && n.ends_with(".csv") {
has_ts = true;
present.push(n);
}
}
}
if !has_ts {
missing.push("ts+*.csv".into());
}
if !missing.is_empty() {
return Err(Med64Error::InvalidWrite {
message: format!(
"native log incomplete under {}: missing {}",
dir.display(),
missing.join(", ")
),
});
}
if !dir.join("RESTORE.txt").is_file() {
let mut files = Vec::new();
write_restore_sidecars(dir, &mut files)?;
present.extend(files);
}
Ok(present)
}
fn write_native_log_set(
report: &AnalysisReport,
options: &AnalysisOptions,
dir: &Path,
log_opts: &NativeLogOptions,
path_prefix: &str,
files: &mut Vec<String>,
) -> Result<()> {
let (trace_trains, n_analyses) = split_spike_trains(report, log_opts.split_traces);
let information = build_information(report, n_analyses);
write_json_file(dir, "information", &information)?;
files.push(format!("{path_prefix}information"));
let config = build_config(report, options);
write_json_file(dir, "config", &config)?;
files.push(format!("{path_prefix}config"));
for (k, train) in &trace_trains {
let name = format!("ts+{k}.csv");
let ts_path = dir.join(&name);
write_spike_timestamps_csv_from_train(&report.header, train, &ts_path)?;
files.push(format!("{path_prefix}{name}"));
}
let burst = build_burst(report, options);
write_json_file(dir, "burst", &burst)?;
files.push(format!("{path_prefix}burst"));
let results = build_results(report);
write_json_file(dir, "results", &results)?;
files.push(format!("{path_prefix}results"));
let wave_data = build_wave_data(report);
write_json_file(dir, "wave_data", &wave_data)?;
files.push(format!("{path_prefix}wave_data"));
let spike_thresholds = build_spike_thresholds(report, options);
write_json_file(dir, "spike_thresholds", &spike_thresholds)?;
files.push(format!("{path_prefix}spike_thresholds"));
Ok(())
}
fn split_spike_trains(
report: &AnalysisReport,
split_traces: bool,
) -> (BTreeMap<usize, SpikeTrain>, usize) {
if !split_traces {
let mut map = BTreeMap::new();
map.insert(1, report.spikes.clone());
return (map, 1);
}
let mut by_trace: BTreeMap<u32, SpikeTrain> = BTreeMap::new();
for spike in &report.spikes.spikes {
by_trace.entry(spike.trace).or_default().push(spike.clone());
}
if by_trace.is_empty() {
let mut map = BTreeMap::new();
map.insert(1, SpikeTrain::default());
return (map, 1);
}
let mut out = BTreeMap::new();
for (trace0, train) in by_trace {
out.insert((trace0 as usize) + 1, train);
}
let n = out.len();
(out, n)
}
pub fn read_native_analysis_log(dir: impl AsRef<Path>) -> Result<NativeAnalysisLog> {
let dir = dir.as_ref();
let information = read_json_file(dir, "information")?;
let config = read_json_file(dir, "config")?;
let burst = read_json_file(dir, "burst")?;
let results = read_json_optional(dir, "results")?;
let wave_data = read_json_optional(dir, "wave_data")?;
let spike_thresholds = read_json_optional(dir, "spike_thresholds")?;
let summary = read_json_optional(dir, "summary")?;
let n = information.get("n").and_then(|v| v.as_u64()).unwrap_or(1).max(1) as usize;
let mut spikes = SpikeTrain::default();
for i in 1..=n {
let path = dir.join(format!("ts+{i}.csv"));
if path.exists() {
let imported = import_spike_csv(&path)?;
spikes.spikes.extend(imported.spikes.spikes);
}
}
if spikes.is_empty() {
let legacy = dir.join("spike_time_stamp.csv");
if legacy.exists() {
spikes = import_spike_csv(&legacy)?.spikes;
}
}
Ok(NativeAnalysisLog {
dir: dir.to_path_buf(),
spikes,
information,
config,
burst,
results,
wave_data,
spike_thresholds,
summary,
})
}
fn build_information(report: &AnalysisReport, n_analyses: usize) -> Value {
let active: Vec<Value> =
report.header.active_channel_indices().into_iter().map(|c| json!(c)).collect();
let n = n_analyses.max(1);
let a: Vec<Value> = (0..n).map(|_| Value::Array(active.clone())).collect();
let interval = report.header.interval_ms();
let i: Vec<Value> = (0..n).map(|_| json!(interval)).collect();
json!({
"n": n,
"a": a,
"i": i,
})
}
fn threshold_type_name(method: ThresholdMethod) -> &'static str {
match method {
ThresholdMethod::StandardDeviation => "SD",
ThresholdMethod::Median | ThresholdMethod::MedianMad | ThresholdMethod::Fixed => "Median",
}
}
fn threshold_direction(options: &AnalysisOptions) -> &'static str {
match (options.detect_plus, options.detect_minus) {
(true, true) => "+/−",
(true, false) => "+",
(false, true) => "−",
(false, false) => "+/−",
}
}
fn build_config(report: &AnalysisReport, options: &AnalysisOptions) -> Value {
let active: Vec<Value> =
report.header.active_channel_indices().into_iter().map(|c| json!(c)).collect();
let (corr_bin, corr_max, corr_sync) = {
#[cfg(feature = "correlation")]
{
(
options.correlation.bin_ms.round() as i64,
options.correlation.max_distance_ms.round() as i64,
options.correlation.synchrony_ms.round() as i64,
)
}
#[cfg(not(feature = "correlation"))]
{
(5_i64, 500_i64, 20_i64)
}
};
let hpf = options.highpass_hz.unwrap_or(100.0);
let lpf = options.lowpass_hz.unwrap_or(5000.0);
let mag = options.threshold_percent.round().max(1.0) as i64;
let dur_sec = {
let d = options.threshold_window_sec;
if d > 0.0 {
((d * 2.0).round() / 2.0).max(0.5)
} else {
1.0
}
};
let bin_ms = options.multi_burst.bin_width_ms.round().max(1.0) as i64;
let smooth = options.multi_burst.smoothing_bins as i64;
let auto_pct = options.multi_burst.auto_threshold_percent.round() as i64;
let auto_pct_lo = options.multi_burst.auto_threshold_percent_low.round() as i64;
let burst_site = options.multi_burst.min_bursting_channels.max(1) as i64;
json!({
"inputFile": {
"fileFormat": "modat",
"fileTraceType": "single trace",
"fileList": [report.path.display().to_string()],
"labelList": ["#1"],
"compoundName": "",
},
"durationRange": {
"durationRange": "all",
"lastMin": "0",
"fromMin": "0",
"toMin": "0",
"firstMin": "0",
},
"channelSelector": {
"channelIndexList": active,
},
"filter": {
"hpfType": "Butterworth",
"hpfHz": format!("{hpf}"),
"lpfType": "Butterworth",
"lpfHz": format!("{lpf}"),
},
"threshold": {
"thresholdType": threshold_type_name(options.threshold_method),
"thresholdMagnification": format!("{mag}"),
"fileTraceIndex": 0,
"thresholdDuration": "all",
"thresholdDirection": threshold_direction(options),
"thresholdDurationSec": format!("{dur_sec}"),
"recalcAll": true,
"fileTraceType": "single trace",
"channelThresholdMap": {},
},
"spikeRange": {
"amplitudeAvailable": false,
"durationAvailable": false,
"amplitudeRange": null,
"durationRange": null,
},
"channelCriteria": {
"fileTraceIndex": 0,
"numOfBursts": 0.0,
"numOfBursts2": 1.0e300,
"fileTraceType": "single trace",
},
"burstSingle": {
"spikeTrainSingleChBurst": true,
"burstSingleDetectionMethod": "Max interval method",
"maxIntervalStartMSec": options.single_burst.max_interval_start_ms.round() as i64,
"maxIntervalEndMSec": options.single_burst.max_interval_end_ms.round() as i64,
"minSpikes": options.single_burst.min_spikes as i64,
"minDurationMSec": options.single_burst.min_duration_ms.round() as i64,
"minIntervalMSec": options.single_burst.min_interburst_ms.round().max(1.0) as i64,
"useActiveChannels": true,
},
"burstMulti": {
"spikeTrainMultiChBurst": true,
"burstMultiDetectionMethod": "ASDR",
"binMSec": bin_ms,
"smoothingBin": smooth,
"upperThreshold": 0,
"lowerThreshold": 0,
"autoThreshold": true,
"fileTraceIndex": 0,
"autoThresholdPercent": auto_pct,
"burstProfileChartType": "mean",
"burstProfileAlignment": "start time",
"fileTraceType": "single trace",
"autoThresholdPercentlow": auto_pct_lo,
"burstingsite": burst_site,
"upperthreshmethod": "ASDR",
"mincheck": false,
"upperThreshold2": 0,
"lowerThreshold2": 0,
"useActiveChannels": true,
},
"correlation": {
"spikeTrainBurstCorrelation": true,
"binMSec": corr_bin,
"maxDistanceMSec": corr_max,
"syncMSec": corr_sync,
},
})
}
fn asdr_bin_ms(report: &AnalysisReport) -> f64 {
if report.asdr.points.len() >= 2 {
(report.asdr.points[1].time_ms - report.asdr.points[0].time_ms).abs()
} else {
50.0
}
.max(1e-9)
}
fn asdr_row_6(
report: &AnalysisReport,
bin_ms: f64,
rate: f64,
smooth: f64,
time_ms: f64,
nspikes: usize,
) -> Value {
let lo = time_ms;
let hi = time_ms + bin_ms;
let mid = time_ms + bin_ms * 0.5;
let is_burst = report.multi_bursts.iter().any(|b| mid >= b.start_ms && mid <= b.end_ms);
json!([rate, smooth, lo, hi, if is_burst { 1.0 } else { 0.0 }, nspikes as f64])
}
fn build_burst(report: &AnalysisReport, options: &AnalysisOptions) -> Value {
let mut by_ch: BTreeMap<usize, Vec<Value>> = BTreeMap::new();
for b in &report.single_bursts {
by_ch.entry(b.channel).or_default().push(json!([
b.start_ms,
b.end_ms,
b.num_spikes as f64
]));
}
let c_map: serde_json::Map<String, Value> =
by_ch.into_iter().map(|(ch, list)| (ch.to_string(), Value::Array(list))).collect();
let bin_ms = asdr_bin_ms(report);
let bin_int = bin_ms.round() as i64;
let bl: Vec<Value> = report
.multi_bursts
.iter()
.map(|b| {
let start_i = (b.start_ms / bin_ms).round();
let end_i = (b.end_ms / bin_ms).round();
let top_i = ((b.start_ms + b.end_ms) * 0.5 / bin_ms).round();
json!([start_i, end_i, top_i, b.peak_series, b.num_spikes as f64])
})
.collect();
let bf: Vec<Value> = report
.asdr
.points
.iter()
.map(|p| asdr_row_6(report, bin_ms, p.rate_hz, p.smoothed_hz, p.time_ms, p.spike_count))
.collect();
let bfasdr: Vec<Value> = report
.asdr
.points
.iter()
.map(|p| asdr_row_6(report, bin_ms, p.rate_hz, p.smoothed_hz, p.time_ms, p.spike_count))
.collect();
let lt = report.asdr.lower_threshold;
let ut = report.asdr.threshold_hz;
let m = json!({
"bm": bin_int,
"bf": bf,
"bfasdr": bfasdr,
"lt": lt,
"ut": ut,
"bl": bl,
});
let f = build_correlation_freq_map(report, options, bin_int);
let burst_data = json!({
"m": m,
"c": c_map,
"f": f,
});
let pl_rates: Vec<Value> =
report.burst_profile.points.iter().map(|p| json!(p.mean_rate_hz)).collect();
let pr_hi = pl_rates.len() as i64;
let profile = json!({
"pl": [pl_rates],
"bm": bin_int,
"pr": [0, pr_hi],
});
json!({
"l": [burst_data],
"b": profile,
})
}
fn build_correlation_freq_map(
report: &AnalysisReport,
options: &AnalysisOptions,
bin_int: i64,
) -> Value {
#[cfg(feature = "correlation")]
{
let _ = options;
let mut m = serde_json::Map::new();
for s in &report.synchrony {
let key = format!("{},{}", s.channel_a, s.channel_b);
m.insert(
key,
json!({
"bm": bin_int,
"cf": {},
"si": s.synchrony_index,
"sa": s.synchrony_index,
"tf": s.bursts_a as i64,
"sf": s.coincident_bursts as i64,
}),
);
}
json!({ "b": bin_int, "m": m })
}
#[cfg(not(feature = "correlation"))]
{
let _ = (report, options);
json!({ "b": bin_int, "m": {} })
}
}
fn stat_cell(v: Option<f64>) -> Value {
match v {
Some(x) if x.is_finite() => json!({
"mn": x,
"md": x,
"sd": "NaN",
"cv": "NaN",
}),
_ => json!({
"mn": "NaN",
"md": "NaN",
"sd": "NaN",
"cv": "NaN",
}),
}
}
fn build_results(report: &AnalysisReport) -> Value {
let table = compute_burst_result_table(report, &ResultTableOptions::default());
let mut sm = serde_json::Map::new();
for row in &table.rows {
sm.insert(row.map_label.clone(), stat_cell(row.value));
}
json!([{
"l": "#1",
"sm": sm,
"cm": {},
}])
}
fn build_wave_data(report: &AnalysisReport) -> Value {
let avgs = average_spike_waveforms(&report.spikes, false);
let mut ch_map = serde_json::Map::new();
for avg in &avgs {
let samples: Vec<Value> = avg.mean_mv.iter().map(|v| json!(*v as f32)).collect();
let wave = json!({
"p": samples,
"s": 0,
});
let key = avg.channel.to_string();
ch_map
.entry(key)
.or_insert_with(|| Value::Array(Vec::new()))
.as_array_mut()
.unwrap()
.push(wave);
}
json!([ch_map])
}
fn build_spike_thresholds(report: &AnalysisReport, options: &AnalysisOptions) -> Value {
let n = report.header.num_channel_slots().max(1);
let mut thresholds = Vec::new();
for ch in 0..n {
let pt = options.plus_thresholds_mv.as_ref().and_then(|v| v.get(ch)).copied();
let mt = options.minus_thresholds_mv.as_ref().and_then(|v| v.get(ch)).copied();
thresholds.push(json!({
"ci": ch,
"pt": opt_nan(pt),
"mt": opt_nan(mt),
"rm": 1.0,
"om": 1.0,
"sr": "Calc",
}));
}
json!([{
"ft": [0, 0],
"st": thresholds,
}])
}
fn opt_nan(v: Option<f64>) -> Value {
match v {
Some(x) if x.is_finite() => json!(x),
_ => json!("NaN"),
}
}
fn write_json_file(dir: &Path, name: &str, value: &Value) -> Result<()> {
let path = dir.join(name);
let text = serde_json::to_string(value)
.map_err(|e| Med64Error::InvalidWrite { message: e.to_string() })?;
std::fs::write(&path, text).map_err(|source| Med64Error::WriteIo { path: path.clone(), source })
}
fn read_json_file(dir: &Path, name: &str) -> Result<Value> {
let path = dir.join(name);
let text = std::fs::read_to_string(&path)
.map_err(|source| Med64Error::Io { path: path.clone(), source })?;
serde_json::from_str(&text)
.map_err(|e| Med64Error::InvalidMoflo { path, message: e.to_string() })
}
fn read_json_optional(dir: &Path, name: &str) -> Result<Option<Value>> {
let path = dir.join(name);
if !path.exists() {
return Ok(None);
}
Ok(Some(read_json_file(dir, name)?))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::analysis::{run_analysis, AnalysisOptions};
#[test]
fn save_native_writes_restore_sidecars() {
let report = run_analysis(
crate::fixture_paths::sample_data1(),
&AnalysisOptions { max_blocks: Some(1), ..AnalysisOptions::reference() },
)
.unwrap();
let dir = std::env::temp_dir().join(format!("med64_native_restore_{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let opts = AnalysisOptions::reference();
write_native_analysis_log(&report, &opts, &dir).unwrap();
assert!(dir.join("RESTORE.txt").is_file());
assert!(dir.join("restore_layout.json").is_file());
assert!(dir.join("information").is_file());
assert!(dir.join("ts+1.csv").is_file());
let present = prepare_native_log_restore(&dir).unwrap();
assert!(present.iter().any(|s| s.contains("information")));
let _ = std::fs::remove_dir_all(&dir);
}
}