jspsychrexamples icon indicating copy to clipboard operation
jspsychrexamples copied to clipboard

Stimulus generation function

Open CrumpLab opened this issue 6 years ago • 1 comments

It would be nice to have helper functions for generating stimuli. For example, a set of stimuli in jsPsych for a Stroop experiment can be defined in an object as follows:

/*defining stimuli*/
var test_stimuli = [
  {
    stimulus: "<p style='color:red;font-size:60pt;'>RED</p>",
    data: { stim_type: 'congruent', response: 'r'}
  },
  {
    stimulus: "<p style='color:blue;font-size:60pt;'>RED</p>",
    data: { stim_type: 'incongruent', response: 'b'}
  },
  {
    stimulus: "<p style='color:green;font-size:60pt;'>RED</p>",
    data: { stim_type: 'incongruent', response: 'g'}
  },
  {
    stimulus: "<p style='color:yellow;font-size:60pt;'>RED</p>",
    data: { stim_type: 'incongruent', response: 'y'}
  }

The above defines 4 individual items, and it takes a bit of copying and pasting to define the remaining combinations.

GOAL

It would be nice to have the stimuli described in table, say tidy format like a dataframe:

html word color congruency
red red red con
green red green inc
blue blue yellow inc

Then, write a function that converts the data.frame to the JS object format.

CrumpLab avatar Aug 06 '19 11:08 CrumpLab

Deciding to use this thread to think about what I'm doing while doing it...

Something like this defines a data.frame in R that codes aspects of the stimulus set:

stroop_stim <- data.frame( stimuli = length(16),
                           word = rep(c("red","green","blue","yellow"), each=4),
                           color = rep(c("red","green","blue","yellow"), 4),
                           response = rep(c("r","g","b","y"), 4),
                           congruency = length(16)) %>%
  mutate(congruency = as.numeric(word==color)) %>%
  mutate(congruency = recode(congruency, `1` = "Con", `0` = "Inc"))

The stimulus column is empty and needs the appropriate html to define each stimulus. After that, the idea would be to write a function that translates this data frame to a JS object for jsPsych, with stimuli and data attributes.

But, maybe it would be helpful to also write a function that could take a dataframe like this one and generate the necessary html to describe the stimuli. The constructor would do something like, create

snippets and assign styles based on entries in the table...

CrumpLab avatar Aug 06 '19 11:08 CrumpLab