Skip to content

Commit

Permalink
Fix correct choices generating bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jonahweissman committed Oct 25, 2021
1 parent d2f28f3 commit c1aa1b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "decide-config"
version = "0.1.1"
version = "0.1.2"
authors = ["Jonah Weissman <jonahrweissman@gmail.com>"]
description = "Generate config files for chorus noise 2 alternative choice experiment"
edition = "2018"
Expand Down
34 changes: 21 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand::{seq::SliceRandom, thread_rng};
use serde::{Deserialize, Serialize};
use serde_value::Value;
use serde_with::skip_serializing_none;
use std::{collections::HashMap, fs::File, path::Path};
use std::{collections::HashMap, fs::File, iter, path::Path};
use strum::{EnumIter, IntoEnumIterator};
use thiserror::Error as ThisError;

Expand Down Expand Up @@ -178,21 +178,29 @@ impl CorrectChoices {
}
pub fn random(experiment: &Experiment) -> Result<Self, Error> {
let mut rng = thread_rng();
let mut choices = experiment.config.choices.clone();
choices.shuffle(&mut rng);
if choices.is_empty() {
return Err(Error::EmptyChoices);
}
let stimuli_per_response = experiment.stimuli.len() / choices.len();
let remainder = experiment.stimuli.len() % choices.len();
// we create a vector with one response per stimulus,
// with evenly divided assignment as much as possible
let mut matched_choices: Vec<Response> = choices
.iter()
.map(|&c| iter::repeat(c).take(stimuli_per_response))
.flatten()
.chain(choices.iter().take(remainder).copied())
.collect();
matched_choices.shuffle(&mut rng);
Ok(CorrectChoices(
experiment
.stimuli
.iter()
.map(|s| {
Ok((
s.name.clone(),
*experiment
.config
.choices
.choose(&mut rng)
.ok_or(Error::EmptyChoices)?,
))
})
.collect::<Result<_, _>>()?,
.map(|s| s.name.clone())
.zip(matched_choices)
.collect(),
))
}
}
Expand All @@ -207,7 +215,7 @@ impl StimulusName {
}

#[skip_serializing_none]
#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
struct StimulusWithGroup {
name: StimulusName,
group: Option<u32>,
Expand Down

0 comments on commit c1aa1b9

Please sign in to comment.