use std::collections::BTreeMap;
use std::path::Path;
use serde_json::{Map, Value, json};
pub mod recording;
pub mod table;
pub mod timing;
pub const FORMAT: &str = "openfrenz-session/1";
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("recording: {0}")]
Sqlite(#[from] rusqlite::Error),
#[error("io: {0}")]
Io(#[from] std::io::Error),
#[error("arrow: {0}")]
Arrow(#[from] arrow_schema::ArrowError),
#[error("parquet: {0}")]
Parquet(#[from] parquet::errors::ParquetError),
#[error("json: {0}")]
Json(#[from] serde_json::Error),
#[error("invalid input: {0}")]
Invalid(String),
}
#[derive(Debug, Clone)]
pub struct Session {
pub meta: Map<String, Value>,
pub tables: BTreeMap<&'static str, table::Table>,
}
impl Session {
fn new(source: Value) -> Self {
let mut meta = Map::new();
meta.insert("format".into(), json!(FORMAT));
meta.insert(
"exporter".into(),
json!(format!("openfrenz frenz-export {}", env!("CARGO_PKG_VERSION"))),
);
meta.insert("source".into(), source);
Self {
meta,
tables: BTreeMap::new(),
}
}
pub fn write(&self, dir: &Path) -> Result<(), Error> {
std::fs::create_dir_all(dir)?;
let mut files = Map::new();
for (name, table) in &self.tables {
table.write_parquet(&dir.join(format!("{name}.parquet")))?;
files.insert(
(*name).to_owned(),
json!({"file": format!("{name}.parquet"), "rows": table.rows()}),
);
}
let mut meta = self.meta.clone();
meta.insert("tables".into(), Value::Object(files));
std::fs::write(
dir.join("session.json"),
serde_json::to_string_pretty(&Value::Object(meta))?,
)?;
Ok(())
}
}
pub fn read(input: &Path) -> Result<Session, Error> {
if input.extension().is_some_and(|e| e == "sqlite") {
recording::from_recording(input)
} else {
Err(Error::Invalid(format!("{}: not a .sqlite recording", input.display())))
}
}
pub(crate) fn eeg_channel_json() -> Value {
json!({
"forehead_right": "slot 0: right forehead pad, beside the optical sensor",
"comb_left": "slot 1: left side comb (shows eyes-closed alpha)",
"forehead_left": "slot 3: left forehead pad (strongest blink/EMG)",
"comb_right": "slot 4: right side comb (shows eyes-closed alpha)",
"reference": "centre forehead pad (inferred)",
})
}
pub(crate) fn motion_json(accel: bool) -> Value {
json!({
"nominal_rate_hz": 50.0,
"units": "raw signed 16-bit counts",
"frame": "+x down (toward chin when worn), +y wearer's left, +z forward; y/z rotated ~30° about x",
"scale": if accel { json!({"counts_per_g_nominal": 16384, "note": "per-unit calibration: see docs/HARDWARE.md"}) } else { json!("unknown") },
})
}