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!

PyCharm vs Spyder: a quick comparison of two Python IDEs

In this post, PyCharm vs Spyder will be compared. If you have followed my blog you may have noticed that a lot of focus have been put on how to learn programming (particularly in Python). I have also written about Integrated Development Environments (IDEs). I think that an IDE may, in fact, be very useful when learning how to code. Of course, when it comes to Python IDEs it may be hard to choose the best one (e.g., PyCharm vs Spyder?).

Spyder is one of my long-time favorite IDEs, and I am mainly using Spyder when I have to write code in Windows environments. However, in one of my blog posts PyCharm was suggested in one comment (see the comments on this post: Why Spyder is the Best Python IDE for Science) that I should test PyCharm.

After testing out PyCharm I started to like this IDE. In this post you will find my views on the two IDEs. E.g., I intend to answer the question; which is the best Python IDE; PyCharm or Spyder?

How to do Descriptive Statistics in Python using Numpy

In this short post we are going to revisit the topic on how to carry out summary/descriptive statistics in Python. In the previous post, I used Pandas (but also SciPy and Numpy, see Descriptive Statistics Using Python) but now we are only going to use Numpy. The descriptive statistics we are going to calculate are the central tendency (in this case only the mean),  standard deviation, percentiles (25 and 75), min, and max.

How to use Python to create an iCalendar file from a Word table

One of the great things with programming is that you can automate things that is boring. As a student I often get schedules in the form of Word documents.

I prefer to have all my activities in a calendar and used to manually put every time in a course into my calendar. Today I got a new schedule and thought: I could probably do this using Python.

After some searching around on the Internet I found the Python packages python-docx and iCalendar. In this post I will show you how to use these to packages to create an iCalender file that can be loaded in to a lot of available calendars.

Exploring response time distributions using Python

Inspired by my post for the JEPS Bulletin (Python programming in Psychology), where I try to show how Python can be used from collecting to analyzing and visualizing data, I have started to learn more data exploring techniques for Psychology experiments (e.g., response time and accuracy). Here are some methods, using Python, for visualization of distributed data that I have learned; kernel density estimation, cumulative distribution functions, delta plots, and conditional accuracy functions. These graphing methods let you explore your data in a way just looking at averages will not (e.g., Balota & Yap, 2011).

Kernel density estimation, Cumulative distribution functions, Delta plots, and Conditional Accuracy Functions
Kernel density estimation, Cumulative distribution functions, Delta plots, and Conditional Accuracy Functions

Required Python packages

I used the following Python packages; Pandas for data storing/manipulation, NumPy for some calculations, Seaborn for most of the plotting, and Matplotlib for some tweaking of the plots. Any script using these functions should import them:

Best Python libraries for Psychology researchers


Python is gaining popularity in many fields of science. This means that there also are many applications and libraries specifically for use in Psychological research.

For instance, there are Python packages for collecting data, doing basic statistics and analysing brain imaging data. In this post, I have collected some useful Python packages for researchers within the field of Psychology and Neuroscience. I have used and tested some of them but others I have yet to try.

E-prime how-to: save data to csv-file using InLine scripts

In this post I will describe, shortly, how to use InLine scripts in E-prime to save your data in comma-separated values (CSV) files. For those who are not familiar with E-prime it is an experiment generating software based on visual basic (i.e., it has its own scripting language called e-basic). Its main purpose is to make building experiment easy/easier. It offers a drag-and-drop graphical user interface (GUI) which is fairly easy to use (although I prefer OpenSesame and PsychoPy – which both offers drag-and-drop GUIs). See the Wikipedia article if you want to know more about e-prime.

This guide will assume that you have worked with e-prime before. That is, you should already have a, more or less, ready experiment that you can add the scripts to.  In the guide I use a Simon task created in e-prime as an example.

PsychoPy video tutorials

PsychoPy, as I have previously written about (e.g., Free and Useful Software and PsychoPy tutorial) is really a great Python tool for creating Psychology experiments. You can write Python code by either using  “code view” or import the package in your favourite IDE.  Furthermore, you can use the builder mode and just drag and drop different items and PsychoPy will create a Python Script for you.

If need inline scripts (in Python, of course) can be inserted. That is, you can combine drag-and-drop building with some coding.

In this post I have collected some tutorial videos that can be useful for someone unfamiliar with PsychoPy.

Two-way ANOVA for repeated measures using Python

Previously I have shown how to analyze data collected using within-subjects designs using rpy2 (i.e., R from within Python) and Pyvttbl. In this post I will extend it into a factorial ANOVA using Python (i.e., Pyvttbl). In fact, we are going to carry out a Two-way ANOVA but the same method will enable you to analyze any factorial design. I start with importing the Python libraries that  are going to be use.

Three ways to do a two-way ANOVA with Python

In an earlier post I showed four different techniques that enables one-way analysis of variance (ANOVA) using Python.  In this post we are going to learn how to do two-way ANOVA for independent measures using Python.

An important advantage of the two-way ANOVA is that it is more efficient compared to the one-way. There are two assignable sources of variation – supp and dose in our example – and this helps to reduce error variation thereby making this design more efficient. Two-way ANOVA (factorial) can be used to, for instance, compare the means of populations that are different in two ways. It can also be used to analyse the mean responses in an experiment with two factors. Unlike One-Way ANOVA, it enables us to test the effect of two factors at the same time. One can also test for independence of the factors provided there are more than one observation in each cell. The only restriction is that the number of observations in each cell has to be equal (there is no such restriction in case of one-way ANOVA).