{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Lecture notes from week 5\n", "Programming for the Behavioral Sciences\n", "\n", "An introduction to PsychoPy.\n", "\n", "[Psychopy](www.psychopy.org) is a free Python application to run lab studies. It can be installed just like any other python package in your own Python distribution (e.g., Anaconda), or through PsychoPy's own standalone version. If you don't like programming (which you do if you take this class), experiments can be build through a graphical user interface (PsychoPy Builder).\n", "\n", "To install and get started with psychopy in your Anaconda distribution, see information [here](https://www.psychopy.org/download.html#pip-install)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from psychopy import visual, core, event\n", "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 1\n", "Create a window, and remove it after 1 s" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "win = visual.Window()\n", "core.wait(1)\n", "win.close()" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Example 2\n", "A slightly more advanced example, where\n", " - A window is opened\n", " - A circle is shown until the participant presses any key" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create a window and a circle\n", "win = visual.Window() \n", "circle = visual.Circle(win, radius=0.1)\n", "\n", "# Show the circle until keypress\n", "circle.draw()\n", "win.flip()\n", "event.waitKeys()\n", "\n", "# Close the window\n", "win.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3 - Reaction time test\n", "Press space as soon as you circle appears. Reaction times are stored." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Parameters in the experiment\n", "nTrials = 5\n", "reaction_times = []\n", "\n", "# Create a window and the circle\n", "win = visual.Window() \n", "circle = visual.Circle(win, radius=0.1)\n", "\n", "# Run the trials\n", "for t in np.arange(nTrials):\n", " \n", " # Show an empty screen for 1 second\n", " win.flip()\n", " core.wait(1)\n", " \n", " # Show the circle until keypress \n", " circle.draw()\n", " win.flip()\n", " keypressed = False\n", " t0 = core.getTime()\n", " while not keypressed: \n", " k = event.getKeys()\n", " if k:\n", " reaction_times.append(core.getTime() - t0)\n", " break\n", "\n", "# Close the window\n", "win.close()\n", "print(np.array(reaction_times) * 1000) # in ms" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "### Question from class\n", "How fast is one loop in while/for loop?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import time\n", "import matplotlib.pyplot as plt\n", "\n", "# Run nLoops iterations\n", "nLoops = 10000\n", "\n", "loop_times = []\n", "for i in range(nLoops):\n", " loop_times.append(time.time())\n", " \n", "# The difference between two values are the loop-times (in microseconds)\n", "loop_dur = np.diff(loop_times) * 1000000 \n", "\n", "# Plot the results as a histogram (in milliseconds). Set the axis to [-1, +1] ms\n", "plt.hist(loop_dur, bins=100, range = (-1, 1))\n", "plt.show() \n", "\n", "# Compute the M \\pm SD of loop-time\n", "print('%.2f %.2f' % (np.mean(loop_dur), np.std(loop_dur)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conclusion: The loop times are tiny; on average they are less than 1 microsecond." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" } }, "nbformat": 4, "nbformat_minor": 2 }