Thursday 18 September 2014

Psychopy First lesson


I'm currently working on a way to present Python to the NeuralCode group. Offering a means of creating a stimulus presentation script through Psychopy might get people involved in programming. It follows the principle of offering something that saves time or solves a problem immediately.


Psychopy can be downloaded as a self contained install. Meaning that Psychopy
and the version of Python it needs, along with the additional libraries, are all packaged together. This makes it easy to install and get going.


The teaching materials for Psychopy created by Prof. Gary Lupya are excellent and
I don't think I can do better. So here they are.

Exercise 1 on http://sapir.psych.wisc.edu

import time
import sys
from psychopy import visual, event, core

win = visual.Window([400,400], color="black", units='pix')
square = visual.PatchStim(win, tex="none", mask="none", color="blue", size=[100,100])
square.draw()
win.flip()
core.wait(.5)
sys.exit()

One of the first things you notice about Python code is that it is very 'readable'. I have left a line between the lines for loading libraries and the code for the actual workings of the program.


Starting at the line after the gap, 'win' is defined. It is a window with relevant details. (note the image to the left isn't created at this point, it is only defined - this image  and the one below are just to show you what is being defined)










The next line defines 'square' with details about where it will be displayed and other properties.










The next two lines need some explanation as they deal with properties of video card display. The square is drawn to a buffer and is then displayed on the screen with win.flip( ). This is coherent with how the video card creates the screen in memory and then displays it usually in two halves (top and bottom).

It 'flips' like one of those old mechanical clocks with the numbers in two halves.
It's important to realize that this is the timing bottleneck in any stimulus presentation program. Stimuli can only be presented to the screen in increments of the screen refresh rate. Any mention of timing accuracy greater than the screen refresh rate is not very useful. Response collection is another story.

 The last line waits for half a second or we wouldn't see anything before the program exits.

The next few exercise are about manipulating various properties of the square.

 Useful Psychopy code snippets

The first couple of challenges are pretty simple.
a) How would you increase the display time to 30 seconds?
b) How would you colour the square red?
 

Friday 12 September 2014

Advanced Statistical Analysis

When I was doing my honours year at Swinburne University, I was required to do a subject called Advanced Quantitative Analysis. By this stage my support of open source software had become strong enough for me to take on the subject using R. I had SPSS installed but only used it for checking the results.
I intended to blog the alternative analysis done in R as a resource for anyone else traveling the path less traveled. The workload and time restraints meant I didn't but today I think I see the opportunity to tick that box.

In the neural code group, one of the members has suggested we start with basic data file handling, cleaning the data and doing a t-test. So the first task is to generate some data.

There are several functions in R for generating random data.


Thursday 4 September 2014

Psychopy

Psychopy is a stimulus presentation program, written in Python. It is open source and cross platform and would be a replacement for ePrime or Presentation.

PsychoPy logo


There is also an excellent resource at Psych711 created by Prof. Gary Lupya.

Psychopy uses OpenGL to control presentation of stimuli on a frame by frame level. Stimuli are generated in real-time, meaning the stimuli can be modified by input from participants or data from an external sensor during the running of the experiment. Visual stimulation is synchronised to the graphics card refresh rate, which is the limit of timing precision in a computer based system.
Psychopy, being written in Python, can interact with a wide range of external hardware including USB devices like keyboards and mice, button boxes and even control of fMRI, EEG, or MEG machinery.
Psychopy can be scripted at a Python command line as well as having a graphical interface. Within the graphical interface, called Builder, Psychopy has modules for pictures, video, text as well as audio, and serial input. These modules allow basic parameters to be entered and the code is generated to achieve these requirements.

A GUI for quick construction

The Builder module in Psychopy is capable of creating standard stimuli and comes with several examples of historically significant experiments. The creation of text and basic shapes on screen is straightforward, as is collecting key presses or mouse clicks as responses.



Psychopy Builder module showing visual, sound, text and a code stimuli
the research environment 
The code behind the scenes

Behind the visual interface of the builder, the python script representing the desired elements is being created. This script can be modified if something not available in the builder is required. This seems an excellent compromise between the precise control of the command line and the ease of use of a graphical user interface, allowing even a beginner to create complex visual and auditory stimuli and gather responses from the keyboard and mouse.
The Psychopy Coder module showing the script version of the experiment.
Custom Code for anything else

There is also the custom code module within builder that allows for elements of code to be added to the GUI. This is how an Arduino connected to the serial port could be queried to gather the physiological data for an experiment. The experiment and the physiological recording could be started together and the information combined later, but the integration of the data within the experiment means the response of the participant can be used to modify the experiment. For example in a just noticeable difference (JND) analysis the participants response to each stimuli, affects the intensity of the next stimuli. This is only possible if response data is available to the experiment.
Declaring a serial object with the Pyserial library





Tuesday 2 September 2014

How do you teach everything.

I'm trying to plan a way to introduce the scientific tool chain. I'm starting to realise that most people don't love programming for it's own sake like I do. They see programming as a means to an end and want to get it over with as quickly and painlessly as possible. Like going to the dentist or doing their taxes.

I think LaTeX and Bibtex might be the gateway.  Especially with PhD candidates, their time is precious and they are only willing to put effort into something if it directly benefits them right now. At least with LaTeX I can show them something that will make the job of writing their thesis easier. I assume most people use 'Word' currently. 'Word' is woefully inadequate to write a multichapter piece of writing. LaTeX on the other hand, was made for this.

LaTeX Notes: Structuring Large Documents

Add version control so changes can be tracked and a section that was discarded three weeks ago as irrelevant, but is now exactly what is needed, can be restored with little effort.
Other reasons for a PhD candidate to use Git are given here - git-latex-workflow

From LaTeX, the concept of Knitr in R is approachable. Analysis will need to be done, so why not do it in R with RStudio? The analysis and writing are then both under version control. Add to that bibtex for referencing and a task that was a huge organisational nightmare is manageable.

I'm hoping that the lure of easy thesis writing might be the right bait for scientists to learn modern coding practice.