Skip to content

Commit

Permalink
Response_Times
Browse files Browse the repository at this point in the history
  • Loading branch information
altunenes committed Jul 31, 2023
1 parent 13c0148 commit ef3ce49
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 53 deletions.
46 changes: 24 additions & 22 deletions participant_1.csv
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
Trial,Num_Left,Num_Right,Result
1,4,1,Correct
2,1,3,Incorrect
3,2,3,Incorrect
4,4,3,Incorrect
5,3,4,Incorrect
6,1,1,Correct
7,4,3,Correct
8,2,3,Incorrect
9,2,4,Correct
10,2,1,Incorrect
11,4,3,Correct
12,3,4,Incorrect
13,1,1,Incorrect
14,1,1,Correct
15,4,1,Incorrect
16,1,3,Correct
17,2,1,Incorrect
18,3,2,Correct
19,3,2,Incorrect
20,3,2,Correct
Trial,Num_Left,Num_Right,Result,Response_Time
1,3,1,Incorrect,4.332222
2,4,4,Incorrect,0.11030369
3,2,2,Correct,0.0996037
4,4,3,Correct,0.086826
5,2,3,Incorrect,0.0665584
6,2,3,Correct,0.09375831
7,2,1,Incorrect,0.0662386
8,2,3,Correct,0.1006928
9,1,4,Incorrect,0.0593993
10,1,3,Correct,0.08991569
11,1,1,Correct,0.0564854
12,3,1,Correct,0.11422569
13,1,3,Incorrect,0.0590612
14,4,3,Correct,0.1234916
15,2,4,Incorrect,0.0632431
16,3,3,Incorrect,0.1237675
17,4,3,Incorrect,0.0732991
18,2,4,Correct,0.1061892
19,3,4,Incorrect,0.067315295
20,2,3,Correct,0.1028189

Mean Accuracy: 0.5
Mean Correct Response Time: 0.097400725

Mean Accuracy: 0.45
24 changes: 24 additions & 0 deletions participant_2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Trial,Num_Left,Num_Right,Result,Response_Time
1,4,1,Incorrect,3.8359594
2,3,2,Correct,0.0799271
3,4,4,Correct,0.1099855
4,2,4,Correct,0.0765749
5,4,2,Incorrect,0.0769854
6,3,1,Correct,0.0899006
7,1,3,Incorrect,0.079889104
8,2,4,Correct,0.083353
9,4,3,Incorrect,0.0699614
10,2,2,Incorrect,0.097382
11,2,3,Incorrect,0.0759022
12,1,2,Correct,0.089924894
13,1,3,Incorrect,0.070033506
14,3,4,Correct,0.1032448
15,1,3,Incorrect,0.0770115
16,1,1,Incorrect,0.0963092
17,1,2,Incorrect,0.1553647
18,3,3,Incorrect,0.0191919
19,2,2,Correct,0.07604449
20,4,3,Correct,0.1060866

Mean Accuracy: 0.45
Mean Correct Response Time: 0.090560205
70 changes: 39 additions & 31 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,55 +1,63 @@
use bevy::{prelude::*, sprite::MaterialMesh2dBundle};
use rand::{Rng, thread_rng, seq::SliceRandom};
use bevy::input::keyboard::KeyboardInput;
use rand::{Rng, thread_rng};
use rand::distributions::{Distribution, Uniform};
use std::fs::OpenOptions;
use std::io::Write;
use std::process;
use std::time::Instant;
fn main() {
App::new()

.add_plugins(DefaultPlugins)
.insert_resource(ExperimentState::default())
.insert_resource(TrialState::default())
.add_systems(Startup, setup_camera)
.add_systems(Startup, setup)
.add_systems(Update, refresh_ellipses)
.add_systems(Update, update_user_responses)
.run();
}



fn setup_camera(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
}
fn refresh_ellipses(
keys: Res<Input<KeyCode>>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>,
mut experiment_state: ResMut<ExperimentState>,
meshes: ResMut<Assets<Mesh>>,
materials: ResMut<Assets<ColorMaterial>>,
experiment_state: ResMut<ExperimentState>,
mut trial_state: ResMut<TrialState>,
ellipses: Query<Entity, With<Ellipse>>,
) {
if !experiment_state.complete && (keys.just_pressed(KeyCode::S) || keys.just_pressed(KeyCode::D)) {
// Despawn the existing ellipses
for entity in ellipses.iter() {
commands.entity(entity).despawn();
}
// Re-setup the ellipses
trial_state.start_time = Instant::now();

setup(commands, meshes, materials, experiment_state);
}
}
#[derive(Resource)]
struct TrialState {
start_time: Instant,
}
impl Default for TrialState {
fn default() -> Self {
TrialState {
start_time: Instant::now(),
}
}
}
#[derive(Component)]
struct Ellipse;
struct UserResponse(Option<bool>);
#[derive(Default, Resource)]
struct ExperimentState {
final_result: Vec<(usize, usize, bool)>, // true for correct, false for incorrect S is same, D is not same
final_result: Vec<(usize, usize, bool, f32)>, // true for correct, false for incorrect S is same, D is not same
num_ellipses_left: usize,
num_ellipses_right: usize,
num_trials: usize,
complete: bool,

}
fn setup(
mut commands: Commands,
Expand Down Expand Up @@ -88,33 +96,32 @@ fn setup(
fn update_user_responses(
keys: Res<Input<KeyCode>>,
mut experiment_state: ResMut<ExperimentState>,
trial_state: Res<TrialState>,

) {
if keys.just_pressed(KeyCode::S) {
let num_left = experiment_state.num_ellipses_left;
let num_right = experiment_state.num_ellipses_right;

let elapsed = trial_state.start_time.elapsed().as_secs_f32();
if num_left == num_right {
experiment_state.final_result.push((num_left, num_right, true));
experiment_state.final_result.push((num_left, num_right, true,elapsed));
} else {
experiment_state.final_result.push((num_left, num_right, false));
experiment_state.final_result.push((num_left, num_right, false,elapsed));
}

experiment_state.num_trials += 1;
if experiment_state.num_trials == 20 {
print_final_results(&experiment_state.final_result);
}
}

if keys.just_pressed(KeyCode::D) {
let num_left = experiment_state.num_ellipses_left;
let num_right = experiment_state.num_ellipses_right;

let elapsed = trial_state.start_time.elapsed().as_secs_f32();
if num_left != num_right {
experiment_state.final_result.push((num_left, num_right, true));
experiment_state.final_result.push((num_left, num_right, true,elapsed));
} else {
experiment_state.final_result.push((num_left, num_right, false));
experiment_state.final_result.push((num_left, num_right, false,elapsed));
}

experiment_state.num_trials += 1;
if experiment_state.num_trials == 20 {
print_final_results(&experiment_state.final_result);
Expand All @@ -124,27 +131,28 @@ fn update_user_responses(
print_final_results(&experiment_state.final_result);
experiment_state.complete = true;
}

}
fn print_final_results(final_results: &Vec<(usize, usize, bool)>) {
fn print_final_results(final_results: &Vec<(usize, usize, bool, f32)>) {
println!("---Final Results---");
let mut csv_data = String::from("Trial,Num_Left,Num_Right,Result\n");
let mut csv_data = String::from("Trial,Num_Left,Num_Right,Result,Response_Time\n");
let mut correct_count = 0;
for (trial, (num_left, num_right, is_correct)) in final_results.iter().enumerate() {
for (trial, (num_left, num_right, is_correct, response_time)) in final_results.iter().enumerate() {
let correctness = if *is_correct {
correct_count += 1;
"Correct"
} else {
"Incorrect"
};
println!("Trial {}: Left = {}, Right = {}, Result = {}", trial+1, num_left, num_right, correctness);
csv_data += &format!("{},{},{},{}\n", trial+1, num_left, num_right, correctness);
println!("Trial {}: Left = {}, Right = {}, Result = {}, Response Time = {}", trial+1, num_left, num_right, correctness, response_time);
csv_data += &format!("{},{},{},{},{}\n", trial+1, num_left, num_right, correctness, response_time);
}
// Calculate mean accuracy
let mean_accuracy: f32 = correct_count as f32 / final_results.len() as f32;
let mean_correct_rt: f32 = final_results.iter().filter(|(_, _, is_correct, _)| *is_correct).map(|(_, _, _, response_time)| response_time).sum::<f32>() / correct_count as f32;
println!("Mean Accuracy: {}", mean_accuracy);
csv_data += &format!("\nMean Accuracy: {}\n", mean_accuracy);
let file_name = format!("participant_{}.csv", 1); // change 1 with participant number
println!("Mean Correct Response Time: {}", mean_correct_rt);
csv_data += &format!("Mean Correct Response Time: {}\n", mean_correct_rt);
let file_name = format!("participant_{}.csv", 2); // change 1 with participant number
let mut file = OpenOptions::new()
.write(true)
.create(true)
Expand All @@ -153,4 +161,4 @@ fn print_final_results(final_results: &Vec<(usize, usize, bool)>) {
file.write_all(csv_data.as_bytes()).unwrap();
println!("Data saved to {}", &file_name);
process::exit(0);
}
}

0 comments on commit ef3ce49

Please sign in to comment.