Press "Enter" to skip to content

Category: Programming

Programming related posts

In this category you will find posts that are related to programming and should be interesting for psychologists, cognitive scientists, and neuroscientists. Well, almost every researcher would probably find some of the information useful at some time!

Every research psychologist, cognitive scientist, and neuroscientist, should know how to program.. Knowing how to program and write scripts will make many of a researchers everyday tasks much easier. For instance, instead of going through line after line of raw data you can write a Python script that runs through each cell in each column. Furthermore, you get the possibility to use more advanced, and cutting edge, statistical techniques by using R statistical programming environment.

Another example might be to create experiments using PsychoPy (either by coding using Python or using the drag-and-drop interface) and the cheap and open-source Arduino microcontroller. Also, coding is fun and relaxing!

Collecting data with Scrapy

This rather long (counting the Python code) tutorial assumes that you are interested in scraping data off the web using Scrapy. It might also be limited to Linux.

Scrapy can be installed using pip:

To create a new Scrapy project open up a terminal and create a directory where you will store your Scrapy projects and change do that directory and run this (change ‘psysci’ to whatever your project will be named):



We start with the Item class and create an object to store our scraped data in. In my example I scrape the article title, type of article, the articles abstract, when the article was received and when it got accepted for publication, which year it was publicized (and month) and the articles keywords.

Reverse Scoring in Python

When working with questionnaires that use a Likert scale (e.g., strongly disagree, disagree, neutral, agree, strongly agree) we sometimes want to reverse the scoring.  For instance, in a questionnaire subjective experiences of cognition some of the questions can be positively worded (e.g., “I find it easy to read while talking in the telephone at the same time”). However, we often have questions that are negatively worded (e.g., “When there is music in the room I find it hard to concentrate on reading”).

In the case of positively worded questions we can score them like this; strongly disagree with a score of 1, disagree= 2, neutral =3, agree = 4 and strongly disagree =5. However, the same scoring can’t be applied for the negatively worded questions. We now need to reverse the score of these questions.  In the python example below, score 5 becomes 1, 4 becomes 2, 2 becomes 4 and 1 becomes 5. Three, on the other hand, (neutral) stays the same.

The following Python example uses Pandas DataFrame. If you need to learn more about Pandas check out my dataframe tutorial.

How to install PyschoPy on Linux Mint Debian Edition

How to Install PsychoPy on Linux Mint

Installing PsychoPy on Linux Mint was not straight forward.  This is a short guide on how to install PsychoPy on Linux Mint Debian edition. After some searching around on forums and such I found a solution and put it together in one place. PsychoPy is an application written in Python for creating experiments (e.g., for psychology or neuroscience). See my post PsychoPy – Free and Open Source Experiment builder written in Python for my thoughts on the software..

Python script for trial randomization

In this post you will find a Python script for randomizing conditions with constraints (i.e., never 2 consecutive trials of X). This script will in a later post be implemented in a cross-modal oddball task created in Psychopy.

I recently started to use Psychopy to build experiments. To build my experiments (mainly oddball tasks) I have, up until I found psychopy, used e-prime. In an oddball task one does not usually present several oddball (typically called “novel” or “deviant”) stimuli in succession. Without getting into detail; I was not able to solve this problem with e-primes scripting language “e-basic” so I made a python script instead. With this script I pre-generated .txt-files that I loaded into e-prime and got my constraints fulfilled.