jsPsych icon indicating copy to clipboard operation
jsPsych copied to clipboard

add multiple branch option for timelines

Open jodeleeuw opened this issue 9 years ago • 4 comments

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
  }
}

jodeleeuw avatar Dec 31 '16 18:12 jodeleeuw

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;
  }
}
  1. 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 :)

eweitnauer avatar Apr 04 '17 16:04 eweitnauer

@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 avatar Apr 05 '17 02:04 jodeleeuw

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

jodeleeuw avatar Oct 27 '23 18:10 jodeleeuw

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

bjoluc avatar Nov 08 '23 16:11 bjoluc