| Reference | Downloads | Github

Syntax error when selecting rows

Hi, everyone!

I’m trying to do something similar to the OP using @lindeloev’s “select rows dynamically” method in PsychoPy 1.84.2, except instead of randomizing within sequentially increasing blocks of conditions (random within 1:12, random within 13:24, etc.), I’m trying to randomize within alternating blocks of conditions. That is to say, I have a conditions file with 100 rows, and I’d like odd numbered trials to be chosen at random from rows 0:49 and even numbered trials chosen from rows 50:99.

To try to implement this, I currently have a single routine with a single loop around it. I’ve inserted a code component into my routine, with

thisLoop = 1

in the Begin Experiment tab, and

if thisLoop = 1: ....thisLoop = 2 else: ....thisLoop = 1

in the begin routine tab.

In the loop settings dialog, I set “nReps $” to 1, load my conditions file, and set “Selected rows $” to ((thisLoop-1)*50):((thisLoop*50)-1). If I close and reopen the loop dialog, there’s an error message at the bottom of it that says “Python syntax error in field ‘Selected rows’:” and the OK button is greyed out. Here’s a screenshot in case I haven’t described this clearly:

When I try to run the experiment, I get the following error.

line 829
trialList=data.importConditions(u'[path].csv', selection=((thisLoop-1)*50):((thisLoop*50)-1))

For some reason there’s an extra ) on the end of the selection value!

I’ve also tried different combinations of including extra ( at the beginning and leaving off extra ) at the end of the “Selected rows $” dialog to try to balance out the total number of parens, but that hasn’t worked. Here’s a list of things I input, and the errors they generate:

Input (thisLoop-1)*50:(thisLoop*50)-1
Error (thisLoop-1)*50:(thisLoop*50)-1)

Input (thisLoop-1)*50:((thisLoop*50)-1
Error (thisLoop-1)*50:((thisLoop*50)-1)

Input ((thisLoop-1)*50):((thisLoop*50)-1
Error ((thisLoop-1)*50):((thisLoop*50)-1)

Input ((thisLoop-1)*50:(thisLoop*50)-1
Error ((thisLoop-1)*50:(thisLoop*50)-1)

So it seems even when there’s an even number of open and close parens, PsychoPy doesn’t like something about my “Selected rows $” code. What am I doing that python doesn’t like?

Thank you for your help!

– You haven’t actually given us the error message, just a line of code where it occurs, so it is hard to help you here.

– Please don’t stress about the ‘extra’ parethesis issue: PsychoPy has wrapped your expression in parentheses because your expression is part of a function call: .importConditions(), so the whole thing gets wrapped in those opening and closing parentheses (note that that all left and right parentheses are matched, as they should be).

– When checking for equality, use two == signs. When assigning a value, use just one. i.e.

if thisLoop = 1:
    thisLoop = 2

should look like:

if thisLoop == 1: # testing for equality
    thisLoop = 2 # assigning a value

– this post really should probably be in its own thread.

Sorry, I thought I’d copied it in with the line of code. Here’s the full output of the PsychoPy Output window:

Running: /[path]/ File "/[path]/", line 829 trialList=data.importConditions(u'[path]/prospectus_IAT conditions_master.csv', selection=(thisLoop-1)*50:(thisLoop*50)-1), ^ SyntaxError: invalid syntax

Please let me know if this still isn’t enough information. I’m very new to programming.


I’ve fixed this, but I’m still getting the same error about invalid syntax in line 829.

Can I move it or something? Sorry, this is the first time I’ve ever posted to a forum like this.

This is where things get a bit confusing, as this is different from the message in your first post, where the parentheses did balance?

It seems to me that adding expression in the “selected rows” field can be a bit fragile. Once the loop dialog box gets convinced it contains a syntax error, it is hard to convince it otherwise. Perhaps it might be easiest to delete the loop and try re-inserting it. You might have better luck if you wrap your expression in an eval() statement like this:


i.e. this means that the expression is hidden in a string and won’t get interpreted until run-time.

I’m not sure if I did something wrong, but using eval('((thisLoop-1)*50):((thisLoop*50)-1)') in the Selected rows field didn’t fix it, I still got a syntax error, just later when the experiment was actually running instead of before the experiment started.

I managed to get python to stop throwing errors by using the code str((thisLoop-1)*50) + ‘:’ + str((thisLoop*50)-1), though in the end it didn’t behave the way I expected it to. I did eventually get it to do what I wanted, though. In the segment of code headed # Initialize components for Routine "X", I used the following code:

from itertools import chain, izip from random import shuffle Type1Stims = data.importConditions('[path]/conditions.csv', selection=u'0:49') Type2Stims = data.importConditions('[path]/conditions.csv', selection=u'50:99')

Then, I added shuffle(Type1Stims) and shuffle(Type2Stims) to the top of the section of the code headed # set up handler to look after randomisation of conditions etc and defined trialList with the following code: trialList=list(chain.from_iterable(izip(Type1Stims, Type2Stims))).

Here’s the post that inspired my solution: