psychopy.org | Reference | Downloads | Github

Changing contrast

OS (e.g. Win10): Win10
PsychoPy version (e.g. 1.84.x): version 3.15
Standard Standalone? (y/n) If not then what?: Yes
What are you trying to achieve?:
I am trying to make it possible for me to adjust my stimGrating contrast from expInfo gui.

What did you try to make it work?:
I added a field named contrast in Experimental Settings. I added in some custom code under my trial routine. Under the Begin Experiment tab I entered:

grating.contrast = expInfo['contrast']

What specifically went wrong when you tried that?:

File “C:\Users\Quantum\Documents\Projects\testStim_lastrun.py”, line 422, in
win.flip()
File “C:\Program Files (x86)\PsychoPy3\lib\site-packages\psychopy\visual\window.py”, line 750, in flip
thisStim.draw()
File “C:\Program Files (x86)\PsychoPy3\lib\site-packages\psychopy\visual\grating.py”, line 308, in draw
self.contrast)
File “C:\Program Files (x86)\PsychoPy3\lib\site-packages\psychopy\visual\basevisual.py”, line 443, in _getDesiredRGB
desiredRGB = (rgb * contrast + 1) / 2.0
TypeError: ufunc ‘multiply’ did not contain a loop with signature matching types dtype(’<U32’) dtype(’<U32’) dtype(’<U32’)[testStim.psyexp|attachment]

(upload://lQ2076wV1JpXMLobyAWnxSkM2PP.psyexp) (24.9 KB)

Try adding some debugging code:

print(expInfo['contrast'])
print(type(expInfo['contrast']))

and tell us the results.

It printed out:

.5
<class ‘str’>

OK, that value is coming to you as a str (i.e. a string of characters) rather than a number. So you need to explicitly convert it to a floating point number representation:

grating.contrast = float(expInfo['contrast'])

i.e. the set of typed characters '0.5' is not equivalent to the actual number 0.5.

e.g.

'0.5' + '0.5' == '0.50.5'

whereas

0.5 + 0.5 == 1.0 # approximately