med64 0.0.2

Headless MED64 electrophysiology toolkit: .modat I/O, spike/burst analysis, assays, spike-rate maps, and CSV/HTML/SVG/HDF5 export
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//! Native analysis-log directory layout (JSON sidecars + spike CSVs).
//!
//! Extensionless UTF-8 JSON sidecars (`information`, `config`, `burst`, `results`,
//! `wave_data`, `spike_thresholds`) plus `ts+N.csv` spike timestamp files.

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;

/// Options for native log directory layout.
#[derive(Debug, Clone, Default)]
pub struct NativeLogOptions {
    /// Empty = write at `dir` root only. Non-empty = write a full log set under each name.
    pub well_names: Vec<String>,
    /// When true, split spikes by `Spike.trace` into `ts+1..ts+N` and set `information.n`.
    pub split_traces: bool,
}

/// Contents restored from a native log directory.
#[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>,
}

/// Write a `log I/O`-compatible log folder (root, single-pass defaults).
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())
}

/// Write a native log with well nesting and/or multi-trace `ts+N` splitting.
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(())
}

/// Validate a native log directory and return restore instructions.
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(())
}

/// Group spikes into `ts+K` trains. Keys are 1-based file indices matching `ts+N.csv`.
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)
}

/// Read a native log directory (extensionless JSON + `ts+*.csv`).
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);
        }
    }
    // Fallback: legacy tooling name if no ts+ files present.
    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,
    })
}

/// `ThresholdType.getName` — only `SD` / `Median`.
fn threshold_type_name(method: ThresholdMethod) -> &'static str {
    match method {
        ThresholdMethod::StandardDeviation => "SD",
        ThresholdMethod::Median | ThresholdMethod::MedianMad | ThresholdMethod::Fixed => "Median",
    }
}

/// `ThresholdDirection.getName` uses fullwidth plus/minus glyphs.
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)
        }
    };

    // ParametersIOService / fromMap require JSON *strings* for several fields
    // (Jackson Map cast to String) and enum `getName()` spellings.
    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;
    // ThresholdParameter.validate requires durationSec > 0 and multiple of 0.5.
    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();

    // Jackson casts lt/ut to Double — write as floats (not bare ints).
    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,
    });

    // Profile `pl` is a list of Y-value arrays (one series per analysis).
    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);
            // CorrelationFreqMap.fromMap: `cf` is lag→count map; tf/sf are ints.
            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));
    }
    // Empty change map — ChangeValueMap.fromMap is strict about StatType labels.
    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);
    }
}