Use of different units for position and size of stimuli?

691 views
Skip to first unread message

René Dutschke

unread,
Aug 20, 2014, 3:50:12 AM8/20/14
to psychop...@googlegroups.com


Dear all,

I would like to know if it is possible to use different units for the position and the size of a stimulus. In particular I am trying to show a variable number of rectangles which are equally (horizontally) distributed across the screen (so here I would like to use the unit 'norm') but with a constant size (e.g. with the unit 'cm'), even across a number of screens with different sizes. Is there a way to handle that? As far as I understand, changing the unit in either the window or the stimulus itself affects all parameters, or am i wrong?

I hope this isn't a redundant question, but I'm very new to PsychoPy and couldn't find something in the documentation or the mailing list.

Best,
René

Jonas Lindeløv

unread,
Aug 21, 2014, 5:20:49 AM8/21/14
to psychop...@googlegroups.com
It's possible. Changing the unit affects all unit-dependent parameters in such a way that the appearance of the stimulus doesn't change. So you can do something like this:

stim = visual.Rect(win)
stim
.units = 'deg'
stim
.width = 3  # degrees
stim
.units = 'pix'
print stim.width  # should give a value in pixels, i.e. different than 5
stim
.pos = (300, -200)  # in pix
stim
.units = 'cm'
print stim.width  # should give a value in cm.
stim
.height = 4  # in cm


Should work! That's the intent at least. Do get back to us whether it works as expected. Of course you can only use "deg" if you have entered details about your monitor dimensions in Monitor Center.

Best,
Jonas

René Dutschke

unread,
Aug 22, 2014, 5:14:58 AM8/22/14
to psychop...@googlegroups.com
Unfortunately, it doesn't seem to work. Even when I try to replicate your example code, I don't get the result I thought I would get. If i get it right, at the first "print stim.width" it should return the converted value of 3 deg to pixel, right? But I still get "3". I will attach a minimal working example to make more clear what I want to achieve:

from __future__ import division
import sys, brewer2mpl
sys
.path.append("C:\\Program Files (x86)\\PsychoPy2\\Lib\\site-packages")
import psychopy
from psychopy import core, visual, event

# creation of window; note that in the end I want to create it with varying monitorsettings (e.g. mon1, mon2 etc with different resolutions)
mainwin
= visual.Window([800, 600], monitor = 'testMonitor', fullscr = False, units='norm')

ndecks
= 4 # represents the number of stimuli I want to present, should be changeable in the end

# in the following I will generate the coordinates (and appearance properties) for the stimuli
stimprops
= {'deck%i'%key : int for key in range(1,ndecks+1)}

for i in range(1, ndecks+1):
    stimprops
['deck%i'%i] = {'pos':[(-1 + 1/ndecks + (i-1)*(2/ndecks)),0],
                           
'col':brewer2mpl.get_map('Set1','qualitative',
                             ndecks
).mpl_colors[i-1]}


# now a way how the presentation works, but with changing sizes of the stimuli according to the monitorsize
width
= 0.1
height
= 0.2
stims
= {'deck%i'%key : int for key in range(1,ndecks+1)}
for i in stims:
    stims
[i] = visual.Rect(mainwin, width, height, pos = stimprops[i]['pos'],
                   fillColor
= stimprops[i]['col'],fillColorSpace = 'rgb')



                

#and here's how I tried to implement your suggestion, but it doesn't adjust the size of the stimuli properly
#width = 2
#height = 3
#stims = {'deck%i'%key : int for key in range(1,ndecks+1)}

#for i in stims:

#     stims[i] = visual.Rect(mainwin, width, height, units = 'cm',

#                        fillColor = stimprops[i]['col'],fillColorSpace = 'rgb')

#     stims[i].units = 'norm'

#     stims[i].pos =  stimprops[i]['pos']  


# presenting the stimuli:

for i in stims:

    stims[i].draw()


mainwin.flip()

event.waitKeys()

core.quit()


When trying your suggestion it still draws the stimuli with the unit 'norm', hence they are way to big (in fact reaching beyond the window). Is there a solution to this? I think I will just try to find reasonable values for width and height which will work on all monitors (the scaling has some advantage as well), but nevertheless it would be nice to have an option to use different units for different parameters (for example as additional parameters in ShapeStim() ).

Best,
René

Jonathan Peirce

unread,
Aug 22, 2014, 6:30:36 AM8/22/14
to psychop...@googlegroups.com
Hi Rene,

Jonas' solution might work for values that are independent (spatial frequency and size?), but the location of a stimulus vertex depends on both size and position and for some coordinate systems (e.g. degrees when correcting for flat screens) the size on the screen actually alters with location. I'm afraid that doing what you want withing the PsychoPy code would likely be complex and fragile

For your case you might want to choose pixels and write the conversion in your script. For cm psychopy includes a conversion function:
    http://www.psychopy.org/api/tools/monitorunittools.html#psychopy.tools.monitorunittools.cm2pix
For normalised to pix you could write your own::
def norm2pix(norm, win):
    x = norm[0]*win.size[0]/2.0
    y = norm[1]*win.size[1]/2.0
    return [x,y]
Then in your script you would have::
stim = visual.Rect(win, .... units='pix')
stim.pos = norm2pix([0.1,0.2], win)
stim.size = cm2pix([3,2.5], win.monitor) #needs monitor not just win
hope that helps. NB none of the code above is tested - it's to give you ideas. Make sure it's doing what you expect!
Jon
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/acbbaa69-3d74-4009-ab82-2db8bb597c3c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Jonathan Peirce
Nottingham Visual Neuroscience

http://www.peirce.org.uk/

René Dutschke

unread,
Aug 22, 2014, 7:41:40 AM8/22/14
to psychop...@googlegroups.com
Thank you Jon, that works perfectly.
Reply all
Reply to author
Forward
0 new messages