Randomize without repeating stimuli

    print(expInfo['seed'])
NameError: name 'expInfo' is not defined

Is that code in the “Begin experiment” tab (i.e. it shouldn’t be in the “Before experiment” tab, as the expInfo dialog has not opened at that stage)?

1 Like

Yes it is

But other code in that tab is referring to expInfo though, right, and isn’t throwing an error? If so, it must be defined at that stage.

Please show all of the code you have in that tab. I wonder if it is somehow getting deleted.

1 Like

Yes. Just a simple table of numbers somewhere, linked to the participant id.

1 Like
CB1 = 1
CB2 = 1
errormessage = ''

error_num = 0
hugeloop_num = 1
hugeloop2_num = 1

#importing randomization stuff
import numpy as np
import xlrd

#more importations
from numpy.random import random, randint, normal, shuffle, choice as randchoice

if expInfo['session'] == '001':
    hugeloop_num = 1
    hugeloop2_num = 0
elif expInfo ['session'] == '002':
    hugeloop2_num = 1
    hugeloop_num = 0
else:
    errormessage = 'Incorrect session! Try 001 or 002.'
    error_num = 1
    hugeloop_num = 0
    hugeloop2_num = 0

#fixed order per subject
np.random.seed(expInfo['seed']) 
row_numbers = list(range(40))
shuffle(row_numbers)

#define words to be presented per session
first_session_rows = row_numbers[0:20]
second_session_rows = row_numbers[21:40]

#sets the words to be presented per session
if expInfo['session'] == '001':
    selected_rows = first_session_rows
elif expInfo['session'] == '002':
    selected_rows = second_session_rows
else:
    errormessage = 'Incorrect session! Try 001 or 002.'
    error_num = 1
    hugeloop_num = 0
    hugeloop2_num = 0

[quote]

The lines that are causing the error are not present (the three “print()” statements). Where are they hiding?

1 Like

Sorry, I forgot to copy/paste them underneath my 2nd if: else - I originally put it in the Before Experiment tab but when I put it in the current tab, it showed this:

We need the print statements before that error occurs - please put them at the beginning of the code component, so we can see why the error is occurring.

1 Like
Generating PsychoPy script...

## Running: C:\Users\Cooco\Desktop\Final_Experiment_20805898\Final_ProdEffects_lastrun.py ##
5012.6628     INFO     Loaded monitor calibration from ['2021_03_02 14:51']
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
0.6308     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'PTB', 'pyo', 'pygame'] (in that order).
5.8617     WARNING     User requested fullscreen with size [1504 1003], but screen is actually [2256, 1504]. Using actual size
8.3435     WARNING     Starting Microphone but sound lib preference is set to be ['sounddevice', 'PTB', 'pyo', 'pygame']. Clashes might occur since 'pyo' is not preferred lib but is needed for Microphone
seed:

<class 'str'>
Traceback (most recent call last):
  File "_mt19937.pyx", line 178, in numpy.random._mt19937.MT19937._legacy_seeding
TypeError: 'str' object cannot be interpreted as an integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Cooco\Desktop\Final_Experiment_20805898\Final_ProdEffects_lastrun.py", line 127, in <module>
    np.random.seed(expInfo['seed']) 
  File "mtrand.pyx", line 244, in numpy.random.mtrand.RandomState.seed
  File "_mt19937.pyx", line 166, in numpy.random._mt19937.MT19937._legacy_seeding
  File "_mt19937.pyx", line 186, in numpy.random._mt19937.MT19937._legacy_seeding
TypeError: Cannot cast array from dtype('<U1') to dtype('int64') according to the rule 'safe'
##### Experiment ended. #####

I put it before all the code and it gave me the same thing.

OK. Please select all of the text in the “Begin experiment” tab and delete it. Then paste this code (have removed a lot of unnecessary stuff):

# not sure what these are or why they need to be defined, so leaving them here:
CB1 = 1
CB2 = 1
errormessage = ''
error_num = 0

# fixed order per subject:
np.random.seed(int(expInfo['seed'])) 
row_numbers = list(range(40))
shuffle(row_numbers)

if expInfo['session'] == '001':
    selected_rows =  row_numbers[0:20]
    hugeloop_num = 1
    hugeloop2_num = 0
elif expInfo ['session'] == '002':
    selected_rows = row_numbers[20:40]
    hugeloop2_num = 1
    hugeloop_num = 0
else:
    errormessage = 'Incorrect session! Try 001 or 002.'
    error_num = 1
    hugeloop_num = 0
    hugeloop2_num = 0

Make sure there is nothing in the “Before experiment” tab.

1 Like
    np.random.seed(int(expInfo['seed'])) 
ValueError: invalid literal for int() with base 10: ''
##### Experiment ended. #####

Unsure what this means

This is why I really need to see the output of the print statements. Again, delete everything in that tab, and put this:

print('seed:')
print(expInfo['seed'])
print(type(expInfo['seed']))

CB1 = 1
CB2 = 1
errormessage = ''
error_num = 0

# fixed order per subject:
np.random.seed(int(expInfo['seed'])) 
row_numbers = list(range(40))
shuffle(row_numbers)

if expInfo['session'] == '001':
    selected_rows = row_numbers[0:20]
    hugeloop_num = 1
    hugeloop2_num = 0
elif expInfo ['session'] == '002':
    selected_rows = row_numbers[20:40]
    hugeloop2_num = 1
    hugeloop_num = 0
else:
    errormessage = 'Incorrect session! Try 001 or 002.'
    error_num = 1
    hugeloop_num = 0
    hugeloop2_num = 0
1 Like

But also, it looks like you simply haven’t entered a value in the ‘seed’ field in the experiment info dialog?

1 Like

## Running: C:\Users\Cooco\Desktop\Final_Experiment_20805898\Final_ProdEffects_lastrun.py ##
6143.9345     INFO     Loaded monitor calibration from ['2021_03_02 14:51']
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
0.7956     WARNING     We strongly recommend you activate the PTB sound engine in PsychoPy prefs as the preferred audio engine. Its timing is vastly superior. Your prefs are currently set to use ['sounddevice', 'PTB', 'pyo', 'pygame'] (in that order).
11.0955     WARNING     User requested fullscreen with size [1504 1003], but screen is actually [2256, 1504]. Using actual size
13.5297     WARNING     Starting Microphone but sound lib preference is set to be ['sounddevice', 'PTB', 'pyo', 'pygame']. Clashes might occur since 'pyo' is not preferred lib but is needed for Microphone
seed:
1
<class 'str'>
14.9712     WARNING     t of last frame was 21.33ms (=1/46)
15.0183     WARNING     t of last frame was 20.46ms (=1/48)
Traceback (most recent call last):
  File "C:\Users\Cooco\Desktop\Final_Experiment_20805898\Final_ProdEffects_lastrun.py", line 109, in <module>
    selected_rows =  first_session_rows
NameError: name 'first_session_rows' is not defined
##### Experiment ended. #####

It won’t print for some reason?

No, it is working, and I can see you have entered the value 1, and it no longer causes an error later. There was a typo in the suggested code lower down though. Again, please delete everything and copy afresh from the post above.

1 Like

Okay so it worked, but it still repeats some of the stimuli in the list as opposed to only presenting it once. It’s also presenting words from the 2nd half of my list as well.

OK, that shouldn’t happen (assuming here that you are comparing session 1 to session 2 values)?

That’s intentional - my reading of the design was to show a random selection of half of the stimuli in one session, and the remaining half in the second, with random orders. As mentioned, your design description was ambiguous.

You’ll have to explain exactly what you want to achieve. Now that the code is running, it should be easy to modify to fit your actual intended design.

1 Like

Got it - at this point, I just don’t want any words to repeat themselves and that’s it! Then we should be done haha

Did you put $selected_rows in the “selected rows” field of your loop?

1 Like