jspsych / jsPsych

Create behavioral experiments in a browser using JavaScript
http://www.jspsych.org
MIT License
1.02k stars 656 forks source link

add multiple branch option for timelines #314

Open jodeleeuw opened 7 years ago

jodeleeuw commented 7 years ago

A simple use case: Suppose there is a two-alternative trial with a correct and incorrect response. If the subject chooses the correct response then a positive feedback message should display. If the subject chooses the incorrect response a negative feedback message should display.

To accomplish this right now, you would need two conditional_functions. Instead, how about something like this:

var switch_timeline = {
  switch_function: function() {
    // function returns either true or false, but return values could be anything...
    return jsPsych.data.getLastTrialData().select('correct').values()[0]; // uses new unreleased data structures
  },
  switch_timelines: {
    true: timelineA, // Object keys must match up with possible return values, Object values are timelines.
    false: timelineB
  }
}
eweitnauer commented 7 years ago

I like this a lot and might give it a try. Two things I'm wondering about:

1) What do you think about making the API more similar to the if_node? Like so:

var if_node = {
    timeline: [if_trial],
    conditional_function: function() {
        return jsPsych.data.getLastTrialData().correct;
    }
}
var select_node = {
  timeline: [trial0, trial1, ...],
  select_function: function() {
    return jsPsych.data.getLastTrialData().correct ? 0 : 1;
  }
}

2) What is a clean way to make a decision based on several previous trials, e.g., based on the average RT? I guess the switch_function would just use the getLastTimlineData() method and check whatever condition it wants to check. One alternative would be to process the data before, and have the switch_function work on that aggregated data. Hmm, I'm pretty sure I am overthinking this one :)

jodeleeuw commented 7 years ago

@eweitnauer In your select_node is the timeline a set of timelines? For example, if select_function returns 0 does timeline[0] run? I worry that this is confusing because it changes how the timeline parameter operates, but then again adding another parameter as in my original spec is also potentially confusing.

For the switch function, I think the user could grab any data they want. So if you want to make a decision based on the average RT for the last 10 trials, you just do:

select_function: function() {
  var average_rt = jsPsych.data.get().last(10).select('rt').mean();
  if(average_rt < 500) {
    return 0;
  } else {
    return 1;
  }
}
jodeleeuw commented 8 months ago

@jspsych/core maybe this should be added to 8.0 as well?

bjoluc commented 8 months ago

Crazy (and certainly unfinished) thought: Can we allow timeline-returning functions as timelines?