1use med64::{run_analysis, AnalysisOptions};
6use nex5file::FileData;
7use std::path::Path;
8
9#[derive(Debug, Clone)]
11pub struct Med64ConvertOptions {
12 pub analysis: AnalysisOptions,
13 pub timestamp_frequency_hz: f64,
15 pub max_blocks: Option<usize>,
17}
18
19impl Default for Med64ConvertOptions {
20 fn default() -> Self {
21 Self {
22 analysis: AnalysisOptions {
23 max_blocks: Some(2),
24 ..Default::default()
25 },
26 timestamp_frequency_hz: 40_000.0,
27 max_blocks: Some(2),
28 }
29 }
30}
31
32pub fn modat_to_file_data(
34 path: impl AsRef<Path>,
35 options: &Med64ConvertOptions,
36) -> med64::Result<FileData> {
37 let path = path.as_ref();
38 let mut analysis_opts = options.analysis.clone();
39 if let Some(max) = options.max_blocks {
40 analysis_opts.max_blocks = Some(max);
41 }
42 let report = run_analysis(path, &analysis_opts)?;
43
44 let mut data = FileData::new(options.timestamp_frequency_hz, path.to_string_lossy()).map_err(
45 |e| med64::Med64Error::InvalidWrite {
46 message: e.to_string(),
47 },
48 )?;
49
50 let by_channel = report.spikes.by_channel();
51 for (channel, spikes) in by_channel {
52 let name = format!("ch_{channel}");
53 let timestamps: Vec<f64> = spikes
54 .iter()
55 .map(|s| s.within_session_time_ms / 1000.0)
56 .collect();
57 data.add_neuron(&name, timestamps, channel as i32, channel as i32, 0.0, 0.0)
58 .map_err(|e| med64::Med64Error::InvalidWrite {
59 message: e.to_string(),
60 })?;
61 }
62
63 data.end_seconds = data
64 .variables
65 .iter()
66 .map(|v| v.maximum_timestamp())
67 .fold(0.0, f64::max);
68
69 Ok(data)
70}
71
72pub fn modat_to_nex5(
74 modat_path: impl AsRef<Path>,
75 nex5_path: impl AsRef<Path>,
76 options: &Med64ConvertOptions,
77) -> med64::Result<()> {
78 let data = modat_to_file_data(modat_path, options)?;
79 nex5file::write_nex5_file(&data, nex5_path).map_err(|e| med64::Med64Error::InvalidWrite {
80 message: e.to_string(),
81 })
82}