Subsetting Rows from Conditions File Is Not Working

OS (e.g. Win10): Mac OS10
PsychoPy version (e.g. 1.84.x): 2020.1.2
**Standard Standalone? Yes

What are you trying to achieve?:
I have a practice trial part of my experiment. In builder, I set this up as a loop that references a .csv file with trial information for 48 possible trials, in 48 rows. However, I want to randomly select only 5 of those 48.

What did you try to make it work?:
I tried the advice from the main psychopy site under “flow”, which says to set Selected Rows to random(desired number of trials)*total rows in csv.

I have also tried suggestions from from two different threads on here: one, two. I also tried a few different configurations of loopType and Selected Rows, but nothing has worked.

Images of things I’ve tried are below.

What specifically went wrong when you tried that?:
When I use the advice from the main psychopy site, I get this error: " for n in selection:
TypeError: ‘NoneType’ object is not iterable"

When I use the advice from the forum posts (the added code snippet), I do not get an error, but the experiment runs through all the rows in my practrials.csv file.

Thanks for any guidance! After trying all the things I could find, I’m really not sure why this isn’t working for me.

  • Natalie

Hi There,

So I think that if you were going to do it as in post 1 you wouldn’t need any input to selected rows. (i.e. remove random(4)*47).

What this will mean is you are essentially feeding in all trials, in a random order, but once you hit the 5th trial the practrials loop should end.

Good luck!
Becca

Hi Becca,

Thanks for your reply! I wasn’t clear enough - I’ve followed both methods separately (e.g. just the code snippets without the random() setting, and just the random() setting without the code snippets). I haven’t been combining the two methods, since they’re separate recommendations.

When I have just the random() code in Selected Rows (per the main site), I get an error: “for n in selection: TypeError: ‘NoneType’ object is not iterable”

When I have just the code snippet added to the routine (per the forum recommendation), the loop doesn’t stop after 5 trials. Instead, it goes through all 48 possible practice trials.

Thanks in advance for any other guidance!

Best,
Nat

Hi Nat,

Ok so for method 1: ““for n in selection: TypeError: ‘NoneType’ object is not iterable”” means that you are not getting a list to iterate through - have you tried it with a $ in front of it? alternatively to debug that error in future try printing the output by adding print(random(4)*47) to a code snippet at the start.

For the second method: I think I might see the issue you might have to change practrials.thisN to practrials.thisTrialN (check the variable name in the header of your .csv data file).

Let me know how you get on,
Becca

Hi

Just to check. Are you testing this locally or online?

Best wishes

Wakefield

Adding a $ to the random command did it! Thanks so much Becca!

Wakefield, this was running on a local computer.

Best,
Nat

Woo good to hear! Could you tick the solution for future users?!

Thanks :blush:

Done! I didn’t realize that was an option, first time on this forum :sweat_smile:

No worries! Good luck with the rest of your study!

TypeError: ‘NoneType’ object is not iterable

This error means that python is trying to iterate over a None object. With Python, you can only iterate over an object if that object has a value. This is because iterable objects only have a next item which can be accessed if their value is not equal to None. If you try to iterate over a None object, you encounter the TypeError: ‘NoneType’ object is not iterable error.

For example, list.sort() only change the list but return None.

Python’s interpreter converted your code to pyc bytecode. The Python virtual machine processed the bytecode, it encountered a looping construct which said iterate over a variable containing None. The operation was performed by invoking the __iter__ method on the None. None has no __iter__ method defined, so Python’s virtual machine tells you what it sees: that NoneType has no __iter__ method.

Technically, you can avoid the NoneType exception by checking if a value is equal to None before you iterate over that value.