Press "Enter" to skip to content

Tag: Python

Every Psychologist Should Learn Programming

The aim of this post is to show you why you, as a psychology student or researcher (or any other kind researcher or student) should learn to program. The post is structured as follows. First I start with discussing why you should learn programming and then give some examples when programming skills are useful. I continue to suggest two programming languages that I think all Psychology students and researchers should learn.

Some good Python Blogs and Resources

In this post you will find some really good Python Blogs and Resources. Most of these really helped me when moving from proprietary software to free and Open Source software (e.g., PsychoPy written in Python).  The links are divided into two categories: general and research. In the ‘general’ category you will find good Python resources that might be more general and introducing. That is, more helpful for general problems and you might want to start here. In the ‘research’ category there are links to good Python resources that are more specific to use in research (e.g., PsychoPy and data analysis). The links in the ‘research’ category mainly contains links to Python blogs.

Python apps and libraries for creating experiments

In this post, I will describe the existing free Python applications and libraries for creating experiments.  So far, I have only used PsychoPy but I plan to test most of them. At least the ones that seem to still be maintained. All applications and libraries are open-source which makes it possible to download the source code and add your own stuff to it.

Free & Useful Software – PsychoPy

PsychoPy

This post will describe why I prefer using PsychoPy before other software. When I started getting involved in research (i.e., when doing my Bachelor’s and Masters theses) I used the software that was accessible for me. That is, some of the most commonly used software at the department I was studying at. After starting my Ph.D. studies I got more and more annoyed with one of the software we used to build experimental tasks  in (i.e., E-prime).

First, I was not familiar with the scripting language (called ‘e-basic’) that was needed, for instance use, to solve pseudo-randomization, and for sending signals to a specially built device I was going to use in my experiments. Second, I prefer to use Linux systems and e-prime is limited to Windows. In fact, e-prime seemed to be working best Windows Vista or earlier but our lab computers were, and are, running Windows 7 (might have changed since back then). Some time into my Ph.D. studies I found PsychoPy which is written in Python, a programming language that I did know, and it seemed like a good alternative to e-prime, and other similar software, to create experiments in.

Another Python script for randomization with constraints

Here you will find a Python function for randomization with constrains. This was written, specifically, for a psychological experiment (a shifting/task-switching task) I was planning to conduct. For this experiment, I needed a list of stimuli names that were quasi-randomized.  Fifty percent of the items in the list are followed by a stimulus in the same colour and the remaining 50 % are followed by a stimulus in a different colour.

Python script

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:

pip install scrapy

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):


scrapy startproject psysci

 

Item

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”).

Data to reverse in Python

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