Skip to content

Commit

Permalink
adding the task
Browse files Browse the repository at this point in the history
  • Loading branch information
kianooshhosseini committed Feb 27, 2025
1 parent 623a9a4 commit 8fb8896
Show file tree
Hide file tree
Showing 81 changed files with 17,997 additions and 23 deletions.
146 changes: 146 additions & 0 deletions code/stats/mfe_d_face_measure_computations_v2_mini.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
# This script will load, and organize the pavlovia data. Then, computes measures of interest.
# For each participant, a single, new, organized csv file that has all the necessary information will be generated.
# Author: Kianoosh Hosseini at NDCLab @FIU (https://Kianoosh.info; https://NDClab.com)
# Last Update: 2024-10-17 (YYYY-MM-DD)
# This version computes hit rates by binning the face delay.
### This version just computes number of errors for all participants without any exclusion.

library(tidyverse)
library(dplyr)
library(stringr)
library(psycho) # to compute d' measures, etc.

#Working directory should be the Psychopy experiment directory.
proje_wd <- "/Users/kihossei/Library/CloudStorage/[email protected]/My Drive/My Digital Life/Professional/Github_Repos/mfe-d-dataset"
setwd(proje_wd)


input_raw_path <- paste(proje_wd, "sourcedata", "checked", "psychopy", sep ="/", collapse = NULL) # input data directory
input_organized_path <- paste(proje_wd, "derivatives", "psychopy", "csv_output", sep ="/", collapse = NULL) # input directory for data files generated by the organizer script!
output_path <- paste(proje_wd, "derivatives", "psychopy", "stat_output", sep ="/", collapse = NULL) # output directory
proc_fileName <- "processed_data_mfe_d_Proj_v3.csv" # output filename
flanker_csv_fileName <- "_mfe_d_flankerDat_v1.csv" # each output csv file will have this on its filename
surprise_csv_fileName <- "_mfe_d_surpriseDat_v1.csv" # each output csv file will have this on its filename


## creating a list of all raw data csv files in the input folder.
raw_datafiles_list <- c() # an empty list that will be filled in the next "for" loop!
csvSelect <- list.files(input_raw_path, pattern = ".csv") # listing only csv files
for (i in 1:length(csvSelect)){
temp_for_file <- ifelse (str_detect(csvSelect[i], "mfe_d", negate = FALSE), 1, 0)
if (temp_for_file == 1){
temp_list <- csvSelect[i]
raw_datafiles_list <- c(raw_datafiles_list, temp_list)
}
}
# Creating the main empty dataframe that will be filled with the data from the loop below:
main_df <- setNames(data.frame(matrix(ncol = 25, nrow = 0)), c("participant_id", "congAcc", "incongAcc",
"incongruent_dat_meanRT", "errorDat_meanRT", "congruent_dat_meanRT", "corrDat_meanRT",
"congCorr_meanRT", "incongCorr_meanRT", "congCorr_logMeanRT",
"congErr_meanRT", "incongErr_meanRT", "congErr_logMeanRT", "incongErr_logMeanRT",
"incongCorr_logMeanRT", "flankEff_meanACC", "flankEff_meanRT", "flankEff_logMeanRT",
"reported_errors", "committed_errors", "memoryBias_score", "num_incong_errorDat",
"faceDuration_avg", "faceDuration_min", "faceDuration_max"))


# Looping over all participants
for (subject in 1:length(raw_datafiles_list)){
#for this participant, find the raw csv file
psychopy_file <- paste(input_raw_path,raw_datafiles_list[subject], sep = "/", collapse = NULL)

#read in the data for this participant, establish id, and remove extraneous variables
psychopyDat <- read.csv(file = psychopy_file, stringsAsFactors = FALSE, na.strings=c("", "NA"))
participant_id <- psychopyDat$id[1]

# Load this participant's flanker and surprise data frames
flanker_name <- paste0(participant_id, flanker_csv_fileName, sep = "", collapse = NULL)
surprise_name <- paste0(participant_id, surprise_csv_fileName, sep = "", collapse = NULL)
flanker_df <- read.csv(file = paste(input_organized_path, flanker_name, sep = "/", collapse = NULL), stringsAsFactors = FALSE, na.strings=c("", "NA"))
surprise_df <- read.csv(file = paste(input_organized_path, surprise_name, sep = "/", collapse = NULL), stringsAsFactors = FALSE, na.strings=c("", "NA"))


incong_flankerDat <- filter(flanker_df, current_trial_congruency == 0)
cong_flankerDat <- filter(flanker_df, current_trial_congruency == 1)

incongAcc <- mean(as.numeric(incong_flankerDat$current_trial_accuracy))
congAcc <- mean(as.numeric(cong_flankerDat$current_trial_accuracy))

# subset the data for correct and error trials, separately for congruent and incongruent trials, creating new data frames for each
corrDat <- flanker_df[flanker_df$current_trial_accuracy == 1,]
corrDat_meanRT <- mean(corrDat$current_trial_rt, na.rm = TRUE)

congruent_dat <- flanker_df[flanker_df$current_trial_congruency == 1,]
congruent_dat_meanRT <- mean(congruent_dat$current_trial_rt, na.rm = TRUE)

cong_corrDat <- corrDat[corrDat$current_trial_congruency == 1,]
incong_corrDat <- corrDat[corrDat$current_trial_congruency == 0,]

errorDat <- flanker_df[flanker_df$current_trial_accuracy == 0,]
errorDat_meanRT <- mean(errorDat$current_trial_rt, na.rm = TRUE)

incongruent_dat <- flanker_df[flanker_df$current_trial_congruency == 0,]
incongruent_dat_meanRT <- mean(incongruent_dat$current_trial_rt, na.rm = TRUE)


cong_errorDat <- errorDat[errorDat$current_trial_congruency == 1,]
incong_errorDat <- errorDat[errorDat$current_trial_congruency == 0,]
#for correct trials, compute mean RT (raw and log-corrected)
congCorr_meanRT <- mean(cong_corrDat$current_trial_rt, na.rm = TRUE)
incongCorr_meanRT <- mean(incong_corrDat$current_trial_rt, na.rm = TRUE)

congErr_meanRT <- mean(cong_errorDat$current_trial_rt, na.rm = TRUE)
incongErr_meanRT <- mean(incong_errorDat$current_trial_rt, na.rm = TRUE)

congCorr_logMeanRT <- mean(log((1+cong_corrDat$current_trial_rt)), na.rm = TRUE)
incongCorr_logMeanRT <- mean(log((1+incong_corrDat$current_trial_rt)), na.rm = TRUE)

congErr_logMeanRT <- mean(log((1+cong_errorDat$current_trial_rt)), na.rm = TRUE)
incongErr_logMeanRT <- mean(log((1+incong_errorDat$current_trial_rt)), na.rm = TRUE)

# compute flanker-effect scores for accuracy, RT, log-RT
flankEff_meanACC <- incongAcc - congAcc
flankEff_meanRT <- incongCorr_meanRT - congCorr_meanRT
flankEff_logMeanRT <- incongCorr_logMeanRT - congCorr_logMeanRT

#
# number of committed errors in the flanker task
committed_errors <- nrow(errorDat)
# number of reported errors
psychopyDatTrim <- psychopyDat[("errorNum_text_box.text")] # stores the number of reported errors by subjects
reported_errors <- subset(psychopyDatTrim, complete.cases(psychopyDatTrim$errorNum_text_box.text))
reported_errors <- reported_errors$errorNum_text_box.text # number of reported errors by participants
reported_errors <- str_extract_all(reported_errors, '\\d+\\.?\\d*') # to extract all sequences of digits from the input string
# There was a participant who had reported 70/100. values 70 and 100. To solve this issue, I replace this kind of values with NAs!
if (length(reported_errors) == 0){ # there is no reported errors
reported_errors <- NA
memoryBias_score <- NA
} else {
reported_errors <- parse_number(reported_errors[[1]]) # in cases like 70/100, we will have 70 100 at this stage. So, length(reported_errors) will
# higher than 1.
if (length(reported_errors) > 1){ # In case they have reported sth like 70/100
reported_errors <- NA
memoryBias_score <- NA
} else if (length(reported_errors) == 1){
reported_errors <- reported_errors[1]
memoryBias_score <- ((reported_errors - committed_errors)/ reported_errors) # percent bias score calculation
}
}


num_incong_errorDat <- nrow(incong_errorDat)
faceDuration_avg <- mean(flanker_df$current_trial_faceDuration)
faceDuration_min <- min(flanker_df$current_trial_faceDuration)
faceDuration_max <- max(flanker_df$current_trial_faceDuration)
#### filling the main data frame
main_df[nrow(main_df) + 1,] <-c(participant_id, congAcc, incongAcc,
incongruent_dat_meanRT, errorDat_meanRT, congruent_dat_meanRT, corrDat_meanRT,
congCorr_meanRT, incongCorr_meanRT, congCorr_logMeanRT,
congErr_meanRT, incongErr_meanRT, congErr_logMeanRT, incongErr_logMeanRT,
incongCorr_logMeanRT, flankEff_meanACC, flankEff_meanRT, flankEff_logMeanRT,
reported_errors, committed_errors, memoryBias_score, num_incong_errorDat,
faceDuration_avg, faceDuration_min, faceDuration_max)
} # Closing the loop for each participant




Loading

0 comments on commit 8fb8896

Please sign in to comment.