Blog Archives

Programming experiments using PsychoPy – first impressions

Links to PsychoPy websiteI wrote a tiny post about PsychoPy a little while ago and it’s something I’ve been meaning to come back to since then. I’ve recently been tasked with an interesting problem; I need an experimental task for a bunch of undergrads to use in a ‘field study’ – something that they can run on their personal laptops, and test people in naturalistic environments (i.e. the participants’ homes). The task is based on a recent paper (Rezlescu et al., 2012) in PLoS One, and involves presenting face stimuli that vary in facial characteristics associated with trustworthiness, in a ‘game’ where the participant  plays the role of an investor and has to decide how much money they would invest in each person’s business. I was actually given a version of the experiment programmed (by someone else) in Matlab using the Psychtoolbox system. However, using this version seemed impractical for a number of reasons; firstly Matlab licences are expensive and getting a licenced version of Matlab on every student’s computer would have blown the budget available. Secondly, in my (admittedly, limited) experience with Matlab and Psychtoolbox, I’ve always found it to be a little… sensitive. What I mean is that whenever I’ve tried to transfer a (working) program onto another computer, I’ve generally run into trouble. Either the timing goes to hell, or  a different version of Matlab/Psychtoolbox is needed, or (in the worst cases) the program just crashes and needs debugging all over again. I could foresee getting this Matlab code working well on every single students’ laptop would be fraught with issues – some of them might be using OS X, and some might be using various versions of Windows – this is definitely going to cause problems.*

Somewhat counterintuitively therefore, I decided that the easiest thing to do was start from scratch and re-create the experiment using something else entirely. Since PsychoPy is a) entirely free, b) cross-platform (meaning it should work on any OS), and c) something I’d been meaning to look at seriously for a while anyway, it seemed like a good idea to try it out.

I’m happy to report it’s generally worked out pretty well. Despite being a complete novice with PsychoPy, and indeed the Python programming language, I managed to knock something reasonably decent together within a few hours. At times it was frustrating, but that’s always the case when programming experiments (at least, it’s always the case for a pretty rubbish programmer like me, anyway).

So, there are two separate modules to PsychoPy – the ‘Builder’ and the ‘Coder’. Since I’m a complete novice with Python, I steered clear of the Coder view, and pretty much used the Builder, which is a really nice graphical interface where experiments can be built up from modules (or ‘routines’) and flow parameters (i.e. ‘loop through X number of trials’) can be added. Here’s a view of the Builder with the main components labelled (clicky for bigness):


At the bottom is the Flow panel, where you add new routines or loops into your program. The large main Routine panel shows a set of tabs (one for each of your routines) where the events that occur in each of the routines can be defined on a timeline-style layout. At the right is a panel containing a list of stimuli (pictures, videos, random-dot-kinematograms, gratings etc.) and response types (keyboard, mouse, rating scales) that can be added to the routines. Once a stimulus or response is added to a routine, a properties box pops up which allows you to modify basic  (e.g. position, size, and colour of text) and some advanced (through the ‘modify everything’ field in some of the dialog boxes) characteristics.

It seems like it would be perfectly possible to build some basic kinds of experiments (e.g. a Stroop task) through the builder without ever having to look at any Python code. However, one of the really powerful features of the Builder interface is the ability to insert custom code snippets (using the ‘code’ component). These can be set to execute at the beginning or end of the experiment, routine, or on every frame. This aspect of the Builder really extends its capabilities and makes it a much more flexible, general-purpose tool. Even though I’m not that familiar with Python syntax, I was fairly easily able to get some if/else functions incorporating random number generation that calculated the amount returned to the investor on a trial, and to use those variables to display post-trial feedback. Clearly a bit of familiarity with the basics of programming logic is important to use these functions though.

This brings me to the Coder view – at any point the ‘Compile Script’ button in the toolbar can be pushed, which opens up the Coder view and displays a script derived from the current Builder view. The experiment can then be run either from the Builder or the Coder. I have to admit, I didn’t quite understand the relationship between the two at first –  I was under the impression that these were two views of the same set of underlying data, and changes in either one would be reflected in the other (a bit like the dual-view mode of HTML editors like Dreamweaver) but it turns out that’s not the case, and in fact, once I thought about it, that would be very difficult to implement with a ‘proper’ compiled language like Python. So, a script can be generated from the Builder, and the experiment can then be run from that script, however, changes made to it can not be propagated back to the Builder view. This means that unless you’re a serious Python ninja, you’re probably going to be doing most of the work in the Builder view. The Coder view is really good for debugging and working out how things fit together though – Python is (rightly) regarded as one of the most easily human-readable languages and if you’ve got a bit of experience with almost any other language, you shouldn’t find it too much of a problem to work out what’s going on.

Another nice feature is the ability of the ‘loop’ functions to read in the data it needs for each repeat of the loop (e.g. condition codes, text to be presented, picture filenames, etc.) from a plain text (comma separated) file or Excel sheet. Column headers in the input file become variables in the program and can then be referenced from other components. Data is also saved by default in the same two file formats – .csv and .xls. Finally, the PsychoPy installation comes with a set of nine pre-built demo experiments which range from the basic (Stroop) to more advanced ones (BART) which involve a few custom code elements.

There’s a couple of features that it doesn’t have which I think would be really useful – in particular in the Builder view it would be great if individual components could be copied and pasted between different routines. I found myself adding in a number of text elements and it was a bit laborious to go through them all and change the font, size, position etc. on each one so they were all the same. Of course ‘proper’ programmers working in the Coder view would be able to copy/paste these things very easily…

So, I like PsychoPy; I really do. I liked it even more when I transferred my program (written on a MacBook Air running OS X 10.8) onto a creaky old Windows XP desktop and it ran absolutely perfectly, first time. Amazing! I’m having a little bit of trouble getting it running well on a Windows Vista laptop (the program runs slowly and has some odd-looking artefacts on some of the pictures) but I’m pretty sure that’s an issue with the drivers for the graphics card and can be relatively easily fixed. Of course, Vista sucks, that could be the reason too.

So, I’d recommend PsychoPy to pretty much anybody – the Builder view makes it easy for novices to get started, and the code components and Coder view means it should keep seasoned code-warriors happy too. Plus, the holy trinity of being totally free, open-source, and cross-platform are huge advantages. I will definitely be using it again in future projects, and recommending it to students who want to learn this kind of thing.

Happy experimenting! TTFN.

*I don’t mean to unduly knock Matlab and/or Psychtoolbox – they’re both fantastically powerful and useful for some applications.

Matlab for Psychologists – 3-day intensive course at the University of Nottingham

I’ve just come across a fantastic-looking 3-day course running at the University of Nottingham called ‘Matlab for Psychologists‘. The curriculum looks like it starts from the very basics at the beginning of the course, and works up to some fairly advanced material by the end of the three days. The summer course has just finished, but there’s another one due to run in September.

Without a doubt, if you’re a PhD student or post-doc at the beginning of your research career, learning Matlab is certainly one of the most useful things you could possibly learn. You can use it for… well… everything really, since it’s a true, general-purpose programming language, albeit wrapped up in a semi-friendly GUI. Presenting stimuli with the psychophysics toolbox is one really popular usage in psychology research, as is general statistical analysis and plotting, and of course analysing fMRI data with the mighty SPM. If I was a PhD student I’d be begging my supervisor and/or department for the £375 to pay for this course – it’s likely to be some of the best money you’ll ever spend on your education.

The course is run by Antonia Hamilton of Nottingham University, and she also has some really good Matlab resources on her website here.

One other thing – while googling ‘Matlab for Psychologists’ I also came across this book with the same title, which was just published a few months ago. Not a whole-hearted recommendation as I haven’t read it, but it looks like it might be worth checking out if you’re keen to give Matlab a try and can’t get to the course.

Guest post by Hayley Thair: A student’s perspective on learning to program

Hayley, hard at work on her programming project.

deeply exciting day for this blogger today, as I’m excited to put up my first guest post. After writing my earlier piece on why (psychology) students should learn to code  I was interested in getting a current student’s perspective on the topic, and the delightful Hayley Thair was kind enough to write me a piece about her experience. I first met Hayley while she was working at the Science Museum on this project and she subsequently moved to Bangor to pursue a MSc in Clinical Neuropsychology. I hope this will help to further convince any other students who might be reading that it really is worthwhile putting a bit of time into learning a bit of coding. Here then is Hayley’s experiences of learning to program and what she feels she’s gained from it:

Something else to do with your PC…

Programming – yet another excuse I now have to spend even more time at my computer. Something that initially sounded rather scary, in a “I have no idea what I’m doing” kind of way, has become something incredibly useful that I am now confident in.I am currently completing my Masters in Clinical Neuropsychology and opted for a module called “practical programming.” Knowing that I have a huge research thesis to run and write up I figured knowing something about how to program would be invaluable! Unfortunately my thesis requires the use of Matlab, and the module taught me Visual Basic. However, I soon realised the fundamentals are the same and even if I couldn’t write Matlab code alone, I could certainly understand what was going on with the assistance of my supervisor.

I saw recently on the news that even primary school children are learning to code… this makes me hesitant to admit it was tricky to start with! However, once I learnt the basics I could design anything I wanted. Being short on ideas and running out of time to complete my mini-project I only managed to come up with a times-table game. It’s actually pretty cool, in a nerdy sort of way! I had two numbers being randomly generated to create the questions; a timer to make it more interesting; a scoring system so you can improve; and a fat robin as the loveable character to save!

Although what I made was simple, I felt a great sense of accomplishment in that I made and coded something from scratch without any help. This was a much greater feeling than anything I had at school in IT lessons. These, as far as I recall, were essentially “today let’s open Word.” I honestly can’t recall where I learnt my basic PC knowledge from, but it certainly wasn’t IT lessons at school. I think these lessons would be more engaging and fun if you were making something, like with programming. Being able to create something that’s yours and personalised would be far more entertaining than just being shown how to use something.

Either way, I’m glad I took the module as so many research assistant jobs ask that you be able to program. I think this puts me ahead of other applicants just because I’ll be able to design experiments and run them independently without needing someone else to come in and build my behavioural task for me.

What surprised me about programming, was that even though at first it was tricky, it suddenly became easy once you got the basics. Even if a piece of code doesn’t run (any programmer will be all too familiar with error messages!) you can continue to try to fix it and think of another way to word it. Essentially it’s all logic. You think what you want a button to do, and about how to break that down into simple step-by-step instructions, and weyhey it works! (Sometimes…) I like to think all those years of playing logic based games like Myst have finally proved useful! For people who enjoy learning something new, and constructing things it’s definitely worth a go. I didn’t find a textbook useful at all, but rather preferred viewing online YouTube tutorials for ideas once you have the basics. Visual Basic is free to download online and it easy to have a play around with as everything is clearly labelled, so I would suggest VB is a good starting point.

If you need another incentive, I’ve learnt to code to a confident level in just 10 lessons. It doesn’t take long to pick it up, and it’s now an invaluable skill that I can mention at interviews that (luckily for me) not everyone has!

SPM vs. FSL by Chris Rorden, and some miscellaneous fMRI tips

I’ve noticed that my previous postings (here and here) about the differences between popular bits of neuroimaging software have been pretty popular in terms of traffic, so I thought I’d just point you towards a similar powerpoint presentation I’ve just found authored by Chris Rorden. It’s fairly brief, but does contain a lot of good information about the key differences between FSL and SPM, particularly in terms of their different approaches to spatial normalisation and  You can download the .ppt file here.

For those who might already be a bit more advanced in their practice of the dark art of fMRI analysis, Chris also has an great set of scripts for SPM8 here, plus of course, his MRICron software is outstandingly useful.

Finally, for FSL users (see – something for everyone at this blog!) I have this little tip, from the mysteriously-named “neuroimager”, which is a fantastic and beautiful method of displaying functional results on a 3D-rendered brain image, using freesurfer.

The Joy of Stats

Statistics – the very word is guaranteed to bring a shudder of terror to the average undergraduate, and even full-grown lecturers have been known to quake in fear before its awesome power. Most psychology undergraduates don’t come from a hard-science or mathematics background, and statistics are probably the number one thing that they struggle with during their psychology courses. Personally, I got through my undergraduate stats exams with a mixture of vague understanding and rote memorisation, and it was only during my PhD that I actually started learning how to do things properly and, more importantly, actually understanding what I was doing, and why.

This is not the place to give any detailed information on the basics of statistics. That kind of material has been covered many, many times before by people infinitely more qualified than I. For that kind of stuff, a good place to start would be Andy Field’s book, available here. Andy explains things very clearly and is actually a very nice chap as well. What I’d like to do instead is do a quick run-down of popular stats software, and point out some resources which can help if you run into trouble. Read the rest of this entry