Press "Enter" to skip to content

Category: Psychology

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.

Reverse Scoring using R Statistical Environment

This post is my first on R and it will describe a method on how to reverse scores using R.

Reverse scoring in R

Many instruments (i.e., questionnaires) contain items are phrased so that a strong agreement indicates something negative (e.g., “When there is music in the room I find it hard to concentrate on reading”). These items need to be reversed so that the data will be correct later for statistical analysis.

For more information on reverse scoring, please see my earlier post: Reverse scoring in Python.  Since I was more  familiar with Python  compared to R, and I had no a clue on how to do this in SPSS, I wrote a Python script. The Python script used a function that used Pandas DataFrame and it reversed the scores nice and quickly.

Questionnaire data to be reversed

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.

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.

What is the Cross-modal oddball paradigm?

My current research focus on how attention can be captured by deviating stimuli. I will briefly describe the paradigm I have used – the cross-modal oddball task. Before I go into the cross-modal oddball task I will describe the auditory oddball task. In the oddball paradigm the subjects are exposed to a repetitive stream of distracting sounds. Furthermore, they can be either passive (i.e., watching a film) or engaged in a task (i.e., making judgments of durations of the tones). Typically subjects are exposed to two types of  distractors; one standard, presented frequently (i.e., 80 % of trials), and an oddball (‘Novel’ or ‘Deviant’) presented on infrequent trials (i.e., 20 % of the trials).

Example of an oddball sequence. Black tones represent standard sounds and red represents deviant or novel sounds.

There is a plethora of research using the oddball paradigm in examining electrophysiological responses (i.e., ERPs) to deviating sounds (for reviews see; Bendixen, SanMiguel, & Schröger, 2012; Friedman, Cycowicz, & Gaeta, 2001). However, when engaged in a primary task there is also a behavioral cost – the response time to target tone is slowed by the presentation of a deviant (e.g., Berti, 2008).

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.