How can I save internally generated variables?

OS: Windows, presumably latest version
PsychoPy version (e.g. 1.84.x): 1.90.3
**What are you trying to achieve?: A two-alternative forced choice learning task with four conditions and probabilistic outcomes. I need to save those outcomes.

**What did you try to make it work?: I wrote the component below to generate and record the feedback. It generates feedback just fine. Now I need to save the content of the variable “outcome” in each trial. The text in the variable “msg” shows up in the log file. If I had enough time, or knew how to write R scripts, I might recover the data I need from there. I don’t know how to write R scripts, and I need to know the sum of outcomes as soon as the experiment is over, because people win money and will need to be paid. Is there any way I can tell PsychoPy in the _Builder to save this information?

In case the solution is to write Python code, I don’t know Python either, I wrote the code component by extrapolating from page 87 in the book. So I am hoping for a solution that can be implemented in Builder, so the student who needs that software doesn’t have to wait for me to learn Python.

if condition == '90pos':
   if key_resp_2.corr:
      if random() > 0.1:
         msg = "Win!"
         outcome = 1
      else:
         msg = "No change"
         outcome = 0
   else: 
      if random() > 0.9:
         msg = "Win!"
         outcome = 1
      else:
         msg = "No change"
         outcome = 0
elif condition == '80pos':
   if key_resp_2.corr:
      if random() > 0.2:
         msg = "Win!"
         outcome = 1
      else:
         msg = "No change"
         outcome = 0
   else: 
      if random() > 0.8:
         msg = "Win!"
         outcome = 1
      else:
         msg = "No change"
         outcome = 0
elif condition == '90neg':
   if key_resp_2.corr:
      if random() > 0.1:
         msg = "No change"
         outcome = 0
      else:
         msg = "Loss!"
         outcome = -1
   else: 
      if random() > 0.9:
         msg = "No change"
         outcome = 0
      else:
         msg = "Loss"
elif condition == '80neg':
   if key_resp_2.corr:
      if random() > 0.2:
         msg = "No change"
         outcome = 0
      else:
         msg = "Loss!"
         outcome = -1
   else: 
      if random() > 0.8:
         msg = "No change"
         outcome = 0
      else:
         msg = "Loss"
         outcome = -1
else:
   msg = "Error: PsychoPy doesn't know this condition.

Check the Excel file that lists the reward sensitivity pictures and conditions."

1 Like

Hi Robert,

you can store the outcome variable for each trial by adding this line in the “End Routine” section of your code component:

thisExp.addData('outcome', outcome)

There will be a new column named “outcome” in your CSV data file.

Concerning the sum of outcomes, you could add the following line in the “Begin Experiment” section of your code component to create the variable:

outcomeTotal = 0

… and the following line in the “End Routine” section:

outcomeTotal = outcomeTotal + outcome

This way, the outcomeTotal variable will be updated each trial.

At the end of your experiment, you could return it to your participant with a text component with the following text:

$u'All in all, you have an outcome of %i points. Congratulations!' % (outcomeTotal)

Then you’ll know right away how much they won, too.

Let us know if it works.

Best
Torge

3 Likes

Edit: First, just the one line using addData stopped the programme from running. Then I tried again, typing the line instead of cutting and pasting, and now it does work. Perhaps my pasting I included an invisible character. With the line typed, the programme runs, and saves as you said. Perfect. I’ll try the rest now.

Hi Robert,
I’m glad it worked. The problems were because I accidentally used the quote instead of the code function in the forum, which mixed up ', ` and ´. I edited the post and copying the code should now be fine, too.

I have tried this solution and it works perfectly in Psychopy, but seems to throw an error when the experiment is run using Pavlovia. Is that a bug or is it simply not supported?

John

1 Like

I think I have solved this one. In JS, the function name is different; one has to have psychoJS.experiment rather than thisExp. In order to have different function names in the Psychopy and JS code, the “Code Type” box for has to be set to “Both” rather than the default “Auto-JS”.

1 Like

I highly recommend you refer to Wakefield’s Python to Javascript Cribsheet. It provides some code that you can put in the ‘begin experiment’ tab of a code component to prevent compatibility issues, including the issue you experienced.

The javascript solution for this is: thisExp=psychoJS.experiment;

1 Like

Thanks! That looks really useful.
It does make me wonder why these straightforward string replacements are not handled automatically in the first place.

John

1 Like