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?

Continue reading

Step-by-step guide for solving the Pyvttbl Float and NoneType error

In this short post I will show you a quick fix for the error “unsupported operand type(s) for +: ‘float’ and ‘NoneType’” with Pyvttbl. In earlier posts I have showed how to carry out ANOVA using Pyvttbl (among other packages. See posts 1, 2, 3, and 4 for ANOVA using pyvttbl). However,  Pyvttbl is not compatible with Python versions greater 1.11 (e.g., 1.12.0, that I am running). This may, of course, be due to that Pyvttbl have not been updated in quite some time. My solution to this problem involves setting up a Python virtual environment (the set up of the virtual environment it is based on the Hitchikers Guide to Python). You will learn how to set up the virtual environment in Linux and Windows.

Linux set-up

First, we will need to install the package virtualenv:

Next we continue with opening up a terminal window. Here, we change directory to the where we typically have our Python projects and create a directory for our new Pyvttbl environment:

How to set up virtual environment - tarting by creating it

The next step is to activate the virtual environment:

Now we should have a virtual environment set up and we can start installing numpy 1.1.0 and pyvttbl:

In the code snippet above, it is important that you set the version of Numpy (e.g., 1.11.0) that we want to install. Thus, we get the version that have been previously working with Pyvttbl.

Remember, if we want to use any other Python packages (e.g., jupyter notebooks, Seaborn, matplotlib) we need to install them in our virtual environment. We can, of course, just add any package to the pip install snippet above:

Now, lets check the version of Numpy using ipython:

Finally, if we want to do our ANOVA in a jupyter notebook we need to install a new kernel that uses the one in the virtual environment. If we don’t do this we will run the notebook from the global environment and, thus, we will import whatever version of Numpy we have installed on the system:

If we follow the image above, we also start up a Jupyter notebook. When creating a new notebook to run our python ANOVAs using pyvttbl in we need to select our new core (i.e., pyvttbl_env”).  In the notebook below (after Windows set-up) you can see that we don’t get the error “unsupported operand type(s) for +: ‘float’ and ‘NoneType’”. Note, we do get a warning that in the future what pvttbl is trying to do is not going to work.

Windows set-up

For Windows, the set-up is basically the same. First, we will need to install virtualenv:

Next step for Windows user is, of course, similar but we need to start the Command Prompt (here is how). Lets change the directory to where our Python project are intended to be stored and create the new Pyvttbl environment. First, we create a directory and then we create the virtual environment in this directory:

We continue by activating our virtual environment for running Numpy 1.11.0 and Pyvttbl:

Next step is to activate our virtual environment. Here, it is a bit different for Windows users compared to Linux users:

Now that we have a virtual environment set up we can install an older version of Numpy. Here we will also depart on how this is done in Linux.  I had a bit of a problem with installing Scipy (Pyvttbl depends on Scipy, for instance) using Pip.

Fortunately, there are Windows binaries for both Numpy and Scipy. Download the version you need and put it in the directory for the virtual environment and use pip to install:

Any additional packages that we may need in our data analysis will, of course, also have to be installed in the virtual environment. Luckily us, we can just use pip again (no external downloading, that is!):

Since we plan to run our ANOVA in a notebook we have one final step that need to be done. To get Jupyter to run WITHIN the virtual environment (or else it will use whatever Numpy version we had problems with earlier). Thus, we need to install a new kernel for the virtual environment. This is easy done using the command prompt, again:

Jupyter notebook

In this notebook, we can run our ANOVA using pyvttbl without the “unsupported operand type(s) for +: ‘float’ and ‘NoneType’ error! Note, Windows user will have a different Numpy versions (we installed 1.11.3) and if you run Linux you can of course install this version to. Of course, we do get the warning (the future is now!)

That was it, now we can use Pyvttbl on both Linux and Windows computers without the problem with float/NoneType. This is of course not an optimal solution but it does the trick. If anyone have another ideas on how to solve this problem, please let me know.

OpenSesame Tutorial – How to Create a Flanker Task

In this post you are going to learn how to create a simple experiment using the free experiment building software OpenSesame. As I have previously written about, OpenSesame, is an application, based on Python, for creating Psychology, Neuroscience, and Economics experiments. It offers a nice and easy to use interface.  In this interface you can drag-and-drop different objects. This means that you don’t have to know any Python programming at all to create an experiment.

How to create a psychology exeriment using OpenSesame

Continue reading

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.

Loading the data

In this example I am going to use the Toothgrowth dataset (download here). It is pretty easy to load a CSV file using the genfromtxt method:

Notice the arguments we pass. The first row has the names and that is why we set the argument ‘names’ to True. One of the columns, further, has strings. Setting ‘dtype‘ to None enables us to load both floats and integers into our data.

Continue reading

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.

Installing python-docx & iCalendar

Both Python packages can be installed using pip:

In the example code I used a table from a Word document containing 4 columns. It is a pretty simple example but in the first column store the date, the second the time, third the room (location), and the last the activity of the event (e.g., lecture).


Extracting a table from  a Word Document

In the first code chunk, below, we start by importing the needed modules. Apart from using Document from python-docx, Calendar and Event from iCalendar, we are going to use datetime from datetime. Datetime is used to store the date in a format that icalendar “likes”.

In the next chunk of code (in the same loop as above) we split the date and time. We do this since due to the formatting of the date and time in the example (“5/4” and “9-12). As previously mentioned the date need to be formatted in a certain way (e.g., using Pythons datetime). In the table from the Word document, some of the events are deadlines and, thus, have no set time. Therefore, we need to see if we have a time to use. If not, we set the time to 17:00-17:01. There is probably a better way to do this but this will do for now. The last line adds each event (as a Python dictionary) to our list containing all data.

Creating the iCalendar file

Now that we have a list of dictionaries containing our lectures/seminars (one for each dictionary) we can use iCalendar to create the calendar. First we create the calendar object and the continue with looping through our list of dictionaries. In the loop we create an event and add the information. In the example here we use the activity as both summary and description but we could have had a summary of the activity and a more detailed description if we’d liked.

The crucial parts, may be, are the ‘dtstart’ and ‘dtend’. This is the starting time and ending time of the event (e.g., a lecture). We continue to add the location (e.g., the room of the event) and add the event to our calender. Finally, we create a file (‘schedule.ics’), write the calender to the file, and close the file.

Now we have our iCalendar file (course_schedule.ics) and can load it into our calender software. I typically use Lightning (a calendar addon for Thunderbird). To open the iCalendar file we created using Python go to File, Open, and Calendar File. Finally select the your iCalendar file:

Loading iCalendar file created from Word table into LightningAfter you done that your new schedule should be loaded into Lightning. Your schedule will be loaded as a separate calendar. As you can see in the image below your lecture and computer labs will show up.

In this post, we learned how to use Python (python-docx) to extract a schedule from a table in a Word Document (.docx). We used the data to create an iCalendar file that we can load into many Calendar applications (e.g., Google, Lightning).

Python Video Tutorial: Creating a Flanker Task using Expyriment

In this video you learn how to create a Flanker task using the Python package Expyriment. If you don’t know Expyriment it is an open-source library. You can program your experiments and run on Linux, Windows, and OS-x computers as well as on Android devices.

In the tutorial you will get familiar with Expyriment and get to create a commonly used task in Psychology – the Flanker task. In this task, you are to respond on the direction of an arrow surrounded by distractors (arrows pointing in either the same or the other direction). It shows how hard it can be to ignore irrelevant information (arrows pointing in the wrong direction).

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:

Continue reading

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 packages for collecting data & 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.

Continue reading

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.

Continue reading

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 needed 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.

I created a playlist with 4 youtube tutorials. In the first video you will learn how to create a classical psychology experiment; the stroop task. The second is more into psycholinguistics and you will learn how to create a language experiment using PsychoPy. In the third you get to know how to create text input fields in PsychoPy. In this tutorial inline Python coding is used so you will also get to know how you may use programming. In the forth video you will get acquainted with using video stimuli in PsychoPy.


Programming for Psychology & Vision Science Tutorial series

Recently I found the following playlist on youtube  and it is amazing. In this series of tutorial videos you will learn how to use PsychoPy as a Python package. For instance,  it is starting at a very basic level; importing the visual module to create windows. In this video he uses my favourite Python IDE Spyder. The videos are actually screencasts from the course Programming for Psychology & Vision Science and contains 10 videos. The first 5 videos covers drawing stimuli on the screen (i.e., drawing to a video, gratings, shapes, images, dots). Watching these videos you will also learn how to collect responses, providing input, and saving your data.


That was all for now. If you know more good video tutorial for PsychoPy please leave a comment. Preferably the tutorials should cover coding but just building experiments with the builder mode is also fine.  I may update my playlist with more PsychoPy tutorials (the first playlist).