Survey tool, code for negative scoring

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x): 3
Standard Standalone? (y/n) If not then what?: y
**What are you trying to achieve?:use survey tool for PANAS questionnaire. The questions have a mixture of positve and negative scoring. Is there a way of coding this into survey tool (as one can do in ratingsscales component) so that the output data reflects this?

What did you try to make it work?:
My excel file has a column ‘score’ which is either ‘+’ or ‘-’ I then put a code component at the end of the routine :
if score ==’+’:
thisExp.addData(‘scoring’, rating.getRating())
elif score == ‘-’:
thisExp.addData(‘scoring’, 5-rating.getRating())

**What specifically went wrong when you tried that?:the error: … \", line 314, in
if score ==’+’:
NameError: name ‘score’ is not defined
Include pasted full error message if possible. “That didn’t work” is not enough information.

Hi @esandhu , you will need to create a column called “score” in your excel conditions file, and feed that file into the loop.

Thank you for your, as always, swift response. I have already created a column called “score” in my excel conditions file. The headings for my condition file are: questionText options score layout type questionWidth responseWidth questionColor responseColor
Is the survey tool set up to allow this?

I see, and you are using the Form component. This works different from your usual loop and simply adding a score column to the form will not work, it will just be ignored. Having negative scoring options is something that can be added to the form, so thanks for highlighting this issue. In the mean time, you need to change the scores yourself at the end of the routine. Here is some code to help:

# End Routine
import pandas as pd
newData = pd.DataFrame.from_dict(form.getData())
newData = newData.sort_values(by=["itemIndex"])

answers = pd.read_csv("PANASFILE.csv")
newData['ratings'] = np.where(answers['score']=='-', 5 - newData['ratings'], newData['ratings'])

Thank you, that is now working. I added:
newData[‘participant’] = expInfo[‘participant’]
so that the partcipant number is saved alongside the reverse scores.
The only issue is that the file name is constant and not unique. Is pandas able to draw data from psychopy so the file name can incorporate the participant number?
Best wishes

#Sure, you could use:


This will create a file name, such as the following,if your participant is labelled “1”


That’s great. Thank you

Thank you for the neat code solution, @dvbridges!

In case anybody just wants to calculate and report a total score from the forward and reversed ratings, as opposed to reporting the individual forward and reversed ratings themselves, it is easy to write the score to the main results file, instead of to a new csv file.

# Read in responses
newData = pd.DataFrame.from_dict(form.getData())
# Read in values from conditions file
answers = pd.read_csv("PANASFILE.csv")
# Apply forward and reverse scoring to responses
newData['ratings'] = np.where(answers['score']=='-', 5 - newData['response'], newData['response'])
# Calculate total score
scoreTotal = newData['ratings'].sum()
# Save score to data file
thisExp.addData("TotalScore", scoreTotal)

The score is written to the first row of form data in the results file. I don’t think the ‘thisExp.addData’ method gives write access to subsequent rows of form data in the results file, so this approach is not useful if you want to report the individual forward and reversed ratings in a tidy way. Better to create a new csv file, as proposed by @dvbridges.