Fixing bugs

Bug in parsing single set in .experiment and bug in printing frequency
This commit is contained in:
elvis
2025-07-03 11:09:58 +02:00
parent c3a84cff2b
commit e0a7c61baa
3 changed files with 37 additions and 29 deletions

View File

@ -21,7 +21,7 @@ fn main() -> std::io::Result<()> {
// examples::run()?;
examples::limit_freq()?;
// examples::limit_freq()?;
examples::fast_freq()?;

View File

@ -37,6 +37,11 @@ Separated<T, C>: Vec<T> = {
}
};
Separated_Or<T, C>: Vec<T> = {
<v: T> => vec![v],
<v: Separated<T, C>> => v
}
// -----------------------------------------------------------------------------
// SetParser
// -----------------------------------------------------------------------------
@ -46,8 +51,7 @@ pub Set: RSset = {
Set_of_entities: RSset = {
"{" "}" => RSset::from(vec![]),
"{" <t: Literal> "}" => RSset::from(vec![translator.encode(t)]),
"{" <t: Separated<Literal, ",">> "}" =>
"{" <t: Separated_Or<Literal, ",">> "}" =>
RSset::from(t.into_iter().map(|t| translator.encode(t)).collect::<Vec<_>>())
};
@ -58,8 +62,7 @@ Set_of_entities: RSset = {
pub Reactions: Vec<RSreaction> = {
"(" ")" => vec![],
"(" <r: Reaction> ")" => vec![r],
"(" <s: Separated<Reaction, ";">> ")" => s
"(" <s: Separated_Or<Reaction, ";">> ")" => s
}
Reaction: RSreaction = {
@ -73,9 +76,7 @@ Reaction: RSreaction = {
// -----------------------------------------------------------------------------
pub Context: RSprocess = {
"[" "]" => RSprocess::NondeterministicChoice{ children: vec![] },
"[" <t: CTX_process> "]" =>
RSprocess::NondeterministicChoice{children: vec![Rc::new(t)]},
"[" <t: Separated<Boxed_CTX_process, ",">> "]" =>
"[" <t: Separated_Or<Boxed_CTX_process, ",">> "]" =>
RSprocess::NondeterministicChoice{ children: t }
};
@ -107,8 +108,7 @@ CTX_process: RSprocess = {
// -----------------------------------------------------------------------------
pub Environment: Box<RSenvironment> = {
"[" "]" => Box::new(RSenvironment::new()),
"[" <t:Env_term> "]" => Box::new(RSenvironment::from(vec![t])),
"[" <t: Separated<Env_term, ",">> "]" => Box::new(RSenvironment::from(t))
"[" <t: Separated_Or<Env_term, ",">> "]" => Box::new(RSenvironment::from(t))
};
Env_term: (IdType, RSprocess) = {
@ -181,7 +181,7 @@ pub System: RSsystem = {
// entities of equal length.
pub Experiment: (Vec<u32>, Vec<RSset>) = {
"Weights:" <w: Separated<Num, ",">>
"Sets:" <s: Separated<Set_of_entities, ",">>
=> (w.into_iter().map(|x| x as u32).collect::<Vec<_>>(), s)
"Weights:" <w: Separated_Or<Num, ",">>
"Sets:" <s: Separated_Or<Set_of_entities, ",">>
=> (w.into_iter().map(|x| x as u32).collect::<Vec<_>>(), s),
}

View File

@ -1,5 +1,7 @@
// translate and keeps track of strings
use std::collections::HashMap;
use std::{cmp::max, collections::HashMap};
static PRECISION: &usize = &2;
pub type IdType = u32;
@ -403,30 +405,36 @@ fn print_frequency(
write!(f, "[")?;
let mut freq_it = frequency.frequency_map.iter().peekable();
let totals = &frequency.totals;
let weights = &frequency.weights;
while let Some((e, freq)) = freq_it.next() {
write!(f, "{} -> ", translator.decode(*e).unwrap_or("Missing".into()))?;
let mut iter = freq
.iter()
.zip(frequency.totals.iter().zip(frequency.weights.iter()))
.peekable();
let mut total_freq = 0.;
while let Some((freq_e, (total, weight))) = iter.next() {
let weighted_freq = (*freq_e as f32 * *weight as f32 * 100.) / (*total as f32);
let end = max(freq.len(), max(totals.len(), weights.len()));
if iter.peek().is_none() {
write!(f, "{weighted_freq:.2}")?;
for pos in 0..end {
let freq_e = freq.get(pos).copied().unwrap_or(0) as f32;
let weight = weights.get(pos).copied().unwrap_or(1) as f32;
let total = totals.get(pos).copied().unwrap_or(1) as f32;
let weighted_freq = (freq_e * weight * 100.) / (total);
if pos == end-1 {
#[allow(clippy::uninlined_format_args)]
write!(f, "{weighted_freq:.*}", PRECISION)?;
} else {
write!(f, "{weighted_freq:.2}, ")?;
#[allow(clippy::uninlined_format_args)]
write!(f, "{weighted_freq:.*}, ", PRECISION)?;
}
total_freq += weighted_freq;
}
total_freq /= frequency.total_weights() as f32;
write!(f, "(total: {total_freq:.2})")?;
#[allow(clippy::uninlined_format_args)]
write!(f, " (total: {total_freq:.*})", PRECISION)?;
if freq_it.peek().is_some() {
writeln!(f, ",")?;