use std::path::Path;
use crate::analysis::LowPassKind;
use crate::error::{Med64Error, Result};
use crate::toolkit::beta::BetaOscillationOptions;
#[derive(Debug, Clone)]
pub struct BetaPrefs {
pub channel_sele: String,
pub combohpf: String,
pub combolpf: String,
pub text_channel_sele: String,
}
impl Default for BetaPrefs {
fn default() -> Self {
Self {
channel_sele: String::new(),
combohpf: "Butterworth highpass".into(),
combolpf: "Butterworth lowpass".into(),
text_channel_sele: "1".into(),
}
}
}
impl BetaPrefs {
pub fn apply_to(&self, options: &mut BetaOscillationOptions) {
if let Some(ch) = first_selected_channel(&self.channel_sele, &self.text_channel_sele) {
options.channel = ch;
}
options.highpass_hz =
map_filter_hz(&self.combohpf, true, options.highpass_hz.or(Some(0.1)));
options.lowpass_hz =
map_filter_hz(&self.combolpf, false, options.lowpass_hz.or(Some(20.0)));
if self.combolpf.to_ascii_lowercase().contains("butterworth") {
options.lowpass_kind = LowPassKind::Butterworth;
} else if self.combolpf.to_ascii_lowercase().contains("bessel") {
options.lowpass_kind = LowPassKind::Bessel;
}
}
}
fn map_filter_hz(combo: &str, _is_hp: bool, default_hz: Option<f64>) -> Option<f64> {
let s = combo.to_ascii_lowercase();
if s.contains("none") || s.is_empty() {
None
} else if s.contains("highpass") || s.contains("lowpass") || s.contains("butterworth") {
default_hz
} else {
default_hz
}
}
pub fn first_selected_channel(channel_sele: &str, text_channel_sele: &str) -> Option<usize> {
let flags: Vec<&str> = channel_sele.split(',').map(|s| s.trim()).collect();
if flags.iter().any(|f| !f.is_empty()) {
for (i, f) in flags.iter().enumerate() {
if *f == "1" || f.eq_ignore_ascii_case("true") {
return Some(i);
}
}
}
let t = text_channel_sele.trim();
if t.is_empty() {
return None;
}
let first = t.split(['-', ',', ' ']).find(|s| !s.is_empty())?;
let n: usize = first.parse().ok()?;
Some(n.saturating_sub(1))
}
pub fn load_beta_prefs(path: impl AsRef<Path>) -> Result<BetaPrefs> {
let path = path.as_ref();
let text = std::fs::read_to_string(path)
.map_err(|source| Med64Error::Io { path: path.to_path_buf(), source })?;
let mut prefs = BetaPrefs::default();
for line in text.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let Some((k, v)) = line.split_once('=') else {
continue;
};
match k.trim() {
"ChannelSele" => prefs.channel_sele = v.trim().to_string(),
"combohpf" => prefs.combohpf = v.trim().to_string(),
"combolpf" => prefs.combolpf = v.trim().to_string(),
"textChannelSele" => prefs.text_channel_sele = v.trim().to_string(),
_ => {}
}
}
Ok(prefs)
}
pub fn write_beta_prefs(prefs: &BetaPrefs, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();
let body = format!(
"# med64 beta oscillation prefs\n\
ChannelSele={}\n\
combohpf={}\n\
combolpf={}\n\
textChannelSele={}\n",
prefs.channel_sele, prefs.combohpf, prefs.combolpf, prefs.text_channel_sele,
);
std::fs::write(path, body)
.map_err(|source| Med64Error::WriteIo { path: path.to_path_buf(), source })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn beta_prefs_roundtrip() {
let dir = std::env::temp_dir().join(format!("med64-betapref-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let path = dir.join("conf_betaoscillation.properties");
let prefs = BetaPrefs {
channel_sele: "0,1,0,".into(),
combohpf: "Butterworth highpass".into(),
combolpf: "None".into(),
text_channel_sele: "2".into(),
};
write_beta_prefs(&prefs, &path).unwrap();
let loaded = load_beta_prefs(&path).unwrap();
assert_eq!(loaded.combolpf, "None");
let mut opts = BetaOscillationOptions::default();
loaded.apply_to(&mut opts);
assert_eq!(opts.channel, 1);
assert!(opts.highpass_hz.is_some());
assert!(opts.lowpass_hz.is_none());
let _ = std::fs::remove_dir_all(dir);
}
}