This is a hands-on tutorial covering the basics of programming in Python: a powerful yet user-friendly programming language that is a much-loved tool for scientists. We will start from the very basics and build up, touching on a variety of topics ("Hello world!", boolean logic, control flow, lists and loops, functions, possibly object-oriented programming), and you will be writing and running your own programs from the beginning.
No prior programming experience is needed. The goal is to provide you with the foundational tools for thinking "programmatically"—developing a sense for what kinds of problems can be solved by writing a program. The materials we will use in the tutorial will be designed to also act as a reference for your future Pythonic endeavors.
This tutorial will introduce you to PsychoPy – an open-source application written in Python that will allow you to design a wide range of neuroscience, psychology and psychophysics experiments.
In this workshop -- held on Thursday January 12th at 10am, and led by Russell -- you will get a quick, high-level introduction to data analysis in Python. We will cover the following Python tools:
- Jupyter notebooks, a web application that allows you to create and share documents that contain live (Python) code and code output, equations, visualizations and explanatory text. They are an excellent tool for doing reproducible #openscience.
- The pandas library, used for representing and manipulating tabular data structures (e.g., data in csv's or Excel spreadsheets).
- Seaborn, a data visualization library that allows generating both basic plots (bar plots, scatter plots) and more advanced plots (violin plots, heatmaps) a breeze.
- Scipy.stats and/or statsmodels, libraries with common statistical functions and tests in the general(ized) linear model and beyond.
We will cover 1-4 with data from WordBank, an open database of children's vocabulary development.
- Anaconda Python 3.5, which contains the Jupyter Notebook which Russell's session will use.
- Follow this link, and then click the icon for your computer's OS. You want Python 3.5. For Mac, you will probably find it easier to use the graphical installer. (For Windows, it appears you can only install via graphical installer!)
- Once you install Anaconda, open a terminal (Mac) or cmd prompt (Windows) and type
python --version
(just in case you're seeing that command split across lines in this readme, to be clear,python --version
should be typed all at once in a single line!). You should see 'Anaconda' somewhere in the output. If you don't see 'Anaconda' in the output, get in touch with one of us (Rachael is probably your best bet). - If you type
jupyter notebook
from the terminal/cmd-prompt, a tab with a file explorer interface should open in your default web browser. If you click 'New' in the upper-right, you should see an option for 'Notebook -- Python [root]'. If you click that, a new jupyter notebook should open in a new tab. Russell's session will be conducted in one of these.
- A code-oriented text editor / IDE (text editor != word processor), preferably with Markdown support, such as Atom (Rachael's recommendation), Sublime, Gedit, Spyder (included with Anaconda), PyCharm, Emacs, or Vim. You will use a text editor / IDE for Rachael's portion.
- PsychoPy. To install PsychoPy, first install the Anaconda distribution of Python in step 1, and then do the following:
- Open a terminal (Mac) or cmd prompt (Windows) and type
conda create -n psychopy-tutorial --channel https://conda.anaconda.org/CogSci psychopy pyglet wxpython python=2.7
. That should be all one line. This will (1) create a Python 2.7 'environment' on your machine, and (2) install PsychoPy and its dependencies (required packages/libraries) to that environment. - To activate this new environment when you want to work with PsychoPy, type
source activate psychopy-tutorial
(Mac) oractivate psychopy-tutorial
(Windows). - You should now be able to run a psychopy script (like this) in the current working directory with
python <psychopy-filename.py>
(replacing<psychopy-filename.py>
with the actual filename! So, if you downloaded the script above, you'd typepython 01_text.py
). (To download the file linked above, follow the link and click 'raw' above and to the right of the file's text. A page with just the script text should open. If you then right-click on that page and select 'save as', you can save the script to your computer (for testing purposes in advance of the workshops, it's probably best to just save it into whatever your default working directory is, e.g.,/Users/russellrichie
)) If you run the01_text.py
script, a window should pop up and display "Hello World", closer after a few seconds, and then return control of the console to you. - To deactivate this environment and return to the default Python 3.5 environment installed by Anaconda (so you can work on Rachael's and Russell's sessions, for example), type
source deactivate
(Mac) ordeactivate
(Windows).
- Open a terminal (Mac) or cmd prompt (Windows) and type