Skip to content

Instantly share code, notes, and snippets.

@qbilius
Created February 18, 2014 14:59
Show Gist options
  • Save qbilius/9072591 to your computer and use it in GitHub Desktop.
Save qbilius/9072591 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Python for Vision Researchers. Part 2: PsychoPy in Practice"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this part, you will learn how to use PsychoPy for various tasks.\n",
"\n",
"There are more extensive resources for vision scientists on [our GestaltReVision wiki](http://gestaltrevision.be/wiki/python/python).\n",
"\n",
"**Authors:** Maarten Demeyer, [Jonas Kubilius](http://klab.lt) \n",
"**Year:** 2014 \n",
"**Copyright:** Public Domain, duh "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Quick setup**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"(you'll have to rerun this cell every time the kernel dies)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"from psychopy import visual, core, event, monitors"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Exercise 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Draw a red rectangle on white background and show it until a key is pressed.*"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Information"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"No idea what to do, right? Let's outline the task:\n",
"\n",
"- open a window\n",
"- create a circle stimulus\n",
"- draw the stimulus\n",
"- wait for a key response\n",
"\n",
"OK, but you can't remember any PsychoPy commands? Me neither. I use [the online documentation](http://www.psychopy.org/api/visual.html) to help me out."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Solution"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"win = visual.Window(color='white')\n",
"rect = visual.Rect(win, width=.5, height=.3, fillColor='red')\n",
"rect.draw()\n",
"win.flip() # don't forget to flip when done with drawing all stimuli so that the stimuli become visible\n",
"event.waitKeys()\n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"136.9521 \tWARNING \tCreating new monitor...\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"136.9522 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Tip.** Notice the ``fillColor`` keyword. Make sure you understand why we use this and not just ``color``. Check out [the explanation of colors and color spaces](http://www.psychopy.org/general/colours.html)."
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Exercise 2: Japanese flag"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Draw a red circle on white background and show it for 5 seconds.*"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Information"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Oh, that sounds trivial now? Just change Rectangle to Circle? Well, try it:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"win = visual.Window(color='white')\n",
"circle = visual.Circle(win, radius=.4, fillColor='red')\n",
"circle.draw()\n",
"win.flip() # don't forget to flip when done with drawing all stimuli so that the stimuli become visible\n",
"event.waitKeys()\n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"144.7085 \tWARNING \tCreating new monitor...\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"144.7086 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And oops, you get an ellipse (at least I do). Why?"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Solution"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By default, PsychoPy is using normalized ('norm') units that are proportional to the window. Since the window is rectangular, everything gets distorted horizontally. In order to keep aspect ratio sane, use 'height' units. [Read more here](http://www.psychopy.org/general/units.html)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"win = visual.Window(color='white', units='height')\n",
"circle = visual.Circle(win, radius=.4, fillColor='red')\n",
"circle.draw()\n",
"win.flip() # don't forget to flip when done with drawing all stimuli so that the stimuli become visible\n",
"event.waitKeys()\n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"117.6303 \tWARNING \tCreating new monitor...\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"117.6304 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Usually, however, people use 'deg' units that allow defining stimuli in terms of their size in visual angle. However, to be able to use visual angle, you first have to define you monitor parameters: resolution, width, and viewing distance. (You can also apply gamma correction etc.)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mon = monitors.Monitor('My screen', width=37.5, distance=57)\n",
"mon.setSizePix((1280,1024))\n",
"win = visual.Window(color='white', units='deg', monitor=mon)\n",
"circle = visual.Circle(win, radius=.4, fillColor='red')\n",
"circle.draw()\n",
"win.flip() # don't forget to flip when done with drawing all stimuli so that the stimuli become visible\n",
"event.waitKeys()\n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"597.4312 \tWARNING \tCreating new monitor...\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"667.3886 \tWARNING \tCreating new monitor...\n"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"717.2290 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Pro Tip.** Hate specifying monitor resolution manually? (Note that wx is messed up and next time you run this snippet it's not gonna work because 'app' is somehow already there... just rename app to app2 then.)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import wx\n",
"app = wx.App(False) # create an app if there isn't one and don't show it\n",
"nmons = wx.Display.GetCount() # how many monitors we have\n",
"mon_sizes = [wx.Display(i).GetGeometry().GetSize() for i in range(nmons)]\n",
"print mon_sizes"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[wx.Size(1680, 1050), wx.Size(1280, 1024)]\n"
]
}
],
"prompt_number": 12
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Exercise 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Draw a fixation cross, a radial stimulus on the left (like used in fMRI for retinotopic mapping) and a gabor patch on the left all on the default ugly gray background.*"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Information"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Oh no, how do you make a gabor patch? And a radial stimulus?\n",
"\n",
"But again, chances are that other people needed these kind of stimulus in the past. Maybe PsychoPy has them built-in?"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Solution"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mon = monitors.Monitor('My screen', width=37.5, distance=57)\n",
"mon.setSizePix((1280,1024))\n",
"win = visual.Window(units='deg', monitor=mon)\n",
"\n",
"# make stimuli\n",
"fix_hor = visual.Line(win, start=(-.3, 0), end=(.3, 0))\n",
"fix_ver = visual.Line(win, start=(0, -.3), end=(0, .3))\n",
"radial = visual.RadialStim(win, size=(3,3), pos=(-(2/2.+3), 0))\n",
"gabor = visual.GratingStim(win, mask='gauss', size=(3,3), pos=(2/2.+3, 0))\n",
"\n",
"# draw stimuli\n",
"fix_hor.draw()\n",
"fix_ver.draw()\n",
"radial.draw()\n",
"gabor.draw()\n",
"\n",
"win.flip()\n",
"event.waitKeys()\n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"41.9402 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Follow-up: PsychoPy is not perfect yet"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"PsychoPy has been around for long enough to be a stable package. However, it is still evolving and bugs may occur. Some of them are quite complex but others are something you can easily fix as long as you're not afraid of getting your hand dirty. You shouldn't be, and I'll illustrate that with the following example:\n",
"\n",
"*Draw the same shapes as before but this time make the fixation cross black.*\n",
"\n",
"So that should be a piece of cake, right? [According to the documentation](http://www.psychopy.org/api/visual/line.html#psychopy.visual.Line.color), simply adding ``color='black'`` to the LineStim should do the trick. Go ahead ant try it:"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mon = monitors.Monitor('My screen', width=37.5, distance=57)\n",
"mon.setSizePix((1280,1024))\n",
"win = visual.Window(units='deg', monitor=mon)\n",
"\n",
"# make stimuli\n",
"fix_hor = visual.Line(win, start=(-.3, 0), end=(.3, 0), color='black')\n",
"fix_ver = visual.Line(win, start=(0, -.3), end=(0, .3), color='black')\n",
"radial = visual.RadialStim(win, size=(3,3), pos=(-(2/2.+3), 0))\n",
"gabor = visual.GratingStim(win, mask='gauss', size=(3,3), pos=(2/2.+3, 0))\n",
"\n",
"# draw stimuli\n",
"fix_hor.draw()\n",
"fix_ver.draw()\n",
"radial.draw()\n",
"gabor.draw()\n",
"\n",
"win.flip()\n",
"event.waitKeys()\n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "__init__() got an unexpected keyword argument 'color'",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-2-4de675636896>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;31m# make stimuli\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mfix_hor\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvisual\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwin\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m.3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m.3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'black'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[0mfix_ver\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvisual\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwin\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstart\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m.3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mend\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m.3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'black'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0mradial\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mvisual\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mRadialStim\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwin\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msize\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpos\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m2.\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Miniconda32\\envs\\psychopy\\lib\\site-packages\\psychopy\\visual\\line.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, win, start, end, **kwargs)\u001b[0m\n\u001b[0;32m 51\u001b[0m \u001b[1;31m#kwargs['lineColor'] = color\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 52\u001b[0m \u001b[1;31m#kwargs['lineColorSpace'] = colorSpace\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 53\u001b[1;33m \u001b[0mShapeStim\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwin\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 54\u001b[0m \u001b[1;31m#self.color = self.lineColor\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 55\u001b[0m \u001b[1;31m#self.colorSpace = self.lineColorSpace\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: __init__() got an unexpected keyword argument 'color'"
]
}
],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You should get an error along the lines of\n",
"\n",
"``TypeError: __init__() got an unexpected keyword argument 'color'``\n",
"\n",
"So now what? You need that black fixation cross real bad. Notice the error message tells you the whole hierarchy of how the problem came about:\n",
"\n",
"- it started with ``fix_hor = visual.Line(win, start=(-.3, 0), end=(.3, 0), color='black')`` -- clearly due to the ``color`` keyword cause it used to work before\n",
"- which called ``ShapeStim.__init__(self, win, **kwargs)`` and that raised an error.\n",
"\n",
"If you were to check out [ShapeStim's documentation](http://www.psychopy.org/api/visual/shapestim.html), you'd see that ShapeStim only accepts ``fillColor`` and ``lineColor`` but not ``color`` keywords (even though later in the documentation it seems as if there were a ``color`` keyword too -- yet another bug).\n",
"\n",
"OK, so if you don't care, just use ``lineColor='black'`` and it will do the job.\n",
"\n",
"However, consider that Jon Peirce and other people has put lots of love in creating PsychoPy. If you find something not working, why not let them know? You can easily report bugs [on Psychopy's GitHub repo](https://github.com/psychopy/psychopy/issues) or, if you're not confident there is a bug, just post it on the [Psychopy's help forum](http://groups.google.com/group/psychopy-users).\n",
"\n",
"But the best of all is trying to fix it yourself, and reporting the bug together with a fix. That way you help not only yourself, but also many other users. Let's see if we can fix this one. First, notice that the problem is that ShapeStim does not recognize the ``color`` keyword. We are not going to mess with ShapeStim because it has ``fillColor`` and ``lineColor`` for a reason. So instead we can modify Line to accept this keyword. So -- open up the file where Line is defined and change it. In my case, this is ``C:\\Miniconda32\\envs\\psychopy\\lib\\site-packages\\psychopy\\visual\\line.py``.\n",
"\n",
"Simply insert ``color=None`` in ``def __init__()`` (line 21 in my case), and ``kwargs['lineColor'] = color`` just below ``kwargs['fillColor'] = None`` (line 50) and ``self.color = self.lineColor`` right after calling ``ShapeStim.__init__()`` (line 51). That's it! Just restart the kernel in this notebook, reimport all packages at the top (to update them with this change), and run the code above again. That should run now.\n",
"\n",
"Note that this is not the full fix yet because we still need to include ``colorSpace`` keyword and also functions such as ``setColor`` and ``setColorSpace``, and there may be yet other compactibility issues to verify. But for our modest purposes, it's fixed!\n",
"\n",
"Let it be a lesson for you as well about the whole idea behind open source -- if something is not working, just open the source file and fix it. You're in control here. Now go ahead and fix a bug in your Windows or OS X.\n",
"\n",
"**Advanced.** The proper way to submit you fixes is by forking the repo, making a patch, and submitting a pull request, [as explained on GitHub's help](https://help.github.com/articles/using-pull-requests)."
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Exercise 4: Hinton's \"Lilac Chaser\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*In this exercise, we will create the famous [Hinton's \"Lilac Chaser\"](http://michaelbach.de/ot/col-lilacChaser/index.html). The display consists of 12 equally-spaced blurry pink dots on a larger circle (on a light gray background). Dots are disappearing one after another to create an illusion of a green dot moving.*"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Part 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Draw 12 equally-spaced dots on a larger circle. (Don't worry about making them blurry for now.) Also make a fixation spot.*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mon = monitors.Monitor('My screen', width=37.5, distance=57)\n",
"mon.setSizePix((1280,1024))\n",
"win = visual.Window(color='lightgray', units='deg', monitor=mon)\n",
"\n",
"# draw a fixation\n",
"fix = visual.Circle(win, radius=.1, fillColor='black')\n",
"fix.draw()\n",
"\n",
"r = 5 # radius of the larger circle\n",
"ncircles = 12\n",
"angle = 2 * np.pi / ncircles\n",
"\n",
"# make and draw stimuli\n",
"for i in range(ncircles):\n",
" pos = (r*np.cos(angle*i), r*np.sin(angle*i))\n",
" circle = visual.Circle(win, radius=.5, fillColor='purple', lineColor='purple', pos=pos)\n",
" circle.draw()\n",
"\n",
"win.flip()\n",
"event.waitKeys()\n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"325.5254 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Part 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Make dots disappear one at a time.*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mon = monitors.Monitor('My screen', width=37.5, distance=57)\n",
"mon.setSizePix((1280,1024))\n",
"win = visual.Window(color='lightgray', units='deg', monitor=mon)\n",
"\n",
"# make a fixation\n",
"fix = visual.Circle(win, radius=.1, fillColor='black')\n",
"\n",
"r = 5 # radius of the larger circle\n",
"ncircles = 12\n",
"angle = 2 * np.pi / ncircles\n",
"\n",
"# make and draw stimuli\n",
"dis = 0 # which one will disappear\n",
"\n",
"while len(event.getKeys()) == 0: \n",
" for i in range(ncircles):\n",
" if i != dis:\n",
" pos = (r*np.cos(angle*i), r*np.sin(angle*i))\n",
" circle = visual.Circle(win, radius=.5, fillColor='purple', lineColor='purple', pos=pos)\n",
" circle.draw()\n",
" dis = (dis + 1) % ncircles\n",
" fix.draw()\n",
" win.flip()\n",
" core.wait(.1)\n",
" \n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"378.7972 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Part 3 (advanced)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"*Optimize your code; make dots blurry.*"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"mon = monitors.Monitor('My screen', width=37.5, distance=57)\n",
"mon.setSizePix((1280,1024))\n",
"win = visual.Window(color='lightgray', units='deg', monitor=mon)\n",
"\n",
"r = 5 # radius of the larger circle\n",
"ncircles = 12\n",
"angle = 2 * np.pi / ncircles\n",
"\n",
"# make a stimuli\n",
"fix = visual.Circle(win, radius=.1, fillColor='black')\n",
"circle = visual.GratingStim(win, size=(2,2), tex=None, mask='gauss', color='purple')\n",
"\n",
"# make and draw stimuli\n",
"dis = 0 # which one will disappear\n",
"\n",
"while len(event.getKeys()) == 0: \n",
" for i in range(ncircles):\n",
" if i != dis:\n",
" pos = (r*np.cos(angle*i), r*np.sin(angle*i))\n",
" circle.setPos(pos)\n",
" circle.draw()\n",
" dis = (dis + 1) % ncircles\n",
" fix.draw()\n",
" win.flip()\n",
" core.wait(.1)\n",
" \n",
"win.close()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"34.7506 \tWARNING \tCreating new monitor...\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Resources"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- [PsychoPy](http://www.psychopy.org/)\n",
"- [Documentation](http://www.psychopy.org/api/api.html)\n",
"- [Help forum](http://groups.google.com/group/psychopy-users)\n",
"- Where are all my packages? ``import site; print site.getsitepackages()``\n",
"- [GitHub repository](https://github.com/psychopy/psychopy) with the latest (but unstable) version of Psychopy where bugs might have been fixed\n",
"- [Report bugs](https://github.com/psychopy/psychopy/issues)\n",
"- Cite PsychoPy in your papers (at least one of the folllowing):\n",
"\n",
" * Peirce, JW (2007) PsychoPy - Psychophysics software in Python. J Neurosci Methods, 162(1-2):8-13\n",
" * Peirce JW (2009) Generating stimuli for neuroscience using PsychoPy. Front. Neuroinform. 2:10. doi:10.3389/neuro.11.010.2008\n",
"\n"
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment