Visar inlägg med etikett Psychology. Visa alla inlägg
Visar inlägg med etikett Psychology. Visa alla inlägg

tisdag 3 maj 2016

More on learning how to code Python - a Cognitive scientists journey to coding

In this post I will continue the discussion on programming in Python for cognitive scientists. I will go from a perspective of data collection, to analysis, and finally to writing your results up (yes, you can basically use python for all these tasks!)

Collecting data

There are several ways you may collect data as a cognitive scientist. All depends on your research question(s). I will in this post only discuss two; collecting online data using social media and/or questionnaires, and collecting data using laboratory experiments. In fact, I will barely mention the first one but you can scrape a lot of behavior:ish data off, for instance, Twitter and maybe throw in questionnaires in that.

Creating experiments to use in data collection

Programming, or building, experiments have for long been carried out with crappy and expensive tools (e.g., E-prime). Although I understand the attraction in simpler experiment building tools where you drag and drop objects. When you are finished building your experiment you generate a script by pressing a button. All fine. However, you may at times need to do more advanced stuff and then you will need add stuff like inline code (e.g., write some scripts and add to the "timeline" in the builder). Recently, it has appeared a couple of free and open-source Python tools for creating experiments. Two of them, PsychoPy and OpenSesame, offers builders and inline scripting (much like e-prime).
OpenSesame builder GUI
Some of the others just gives you an API to ease some of the coding of your experiment (PsychoPy can be used as a library, also). That is, you import it as it was any other Python library (after you have installed it, of course). For instance, if you use the Python library Expyriment you will import what you need from the library:

from expyriment import design, control, stimuli, io, misc

On Expyriments website you can find some beginner's tutorials.

If you are interested in using PsychoPy's builder mode you can watch the following youtube tutorial:

In this tutorial you will learn how to create a classical psychology experiment; the stroop task (of course, in its original form pen and paper were used...). For a psycho-linguistic researcher the following tutorial may be more adequate:
 
More resources on Psychopy can be found on the software's resources page.  You will find out that coding using the library of Psychopy (e.g., importing the stuff you need for your experiment from the PsychoPy library is much like the short Expyriment example above).
When you have learned how to create and code your own experiment in Python you will be able to collect a lot of data you probably want to analyze your data. Although MATLAB, and more recently, R have had the majority of the cognitive science crowd when it comes to analysis (you can also create experiment in MATLAB using psychotoolbox and such) you can OF COURSE do your analysis in Python.

Data analysis

Common statistical methods in Psychology, and related fields, are linear regression, t-test, and analysis of variance (ANOVA). Especially when it comes to experiments when doing more subjective survey studies other techniques such as factor analysis (FA) and structural equation modelling (SEM) are carried out. Of course, an experimental design may also need such multivariate analysis'. If you are interested in FA and SEM in Python I must disappoint you here, however. As far as I know you can only carry out principal component analysis (which is not 'real' factor analysis according to my old stats teacher!)

Enough of my rambling you say? What CAN I do in Python?! Well you CAN do t-tests, linear regression (non-linear also), ANOVA, etc. For instance, using the package Statsmodels we can carry out all of the methods (except for repeated measures ANOVA, however). Sci-kit learn, a machine learning library, can also do a lot of statistics. Of course, SciPy can do some basic parameteric tests and Pandas (and SciPy and NumPy) can carry out most descriptive statistics you'd want to have. Repeated measures ANOVA can be carried out using the package Pyvttbl which, sadly, seem to be un-maintained. No more updates of that...

That is it, most of the stuff I list here I found via this excellent site: Python and R as tools for data analysis and  creating Psychology experiments. If you follow the link you will find discussions on Python IDEs for Psychology researchers (or any other scientist), how to do ANOVA for repeated and dependent measures, and some descriptive statistics. All in Python.

That is it for me now. 

Please leave a comment if you have any suggestions!

måndag 25 april 2016

Five excellent Python video lectures!

In this post you will find 5 great videos containing lectures on how to carry out data analysis using Python. These seem to be part of a course called "Programming for Psychology in Python". It seems pretty awesome.

The array data type

The first video covers numpy and how to use numpy to create and handle arrays.

Creating figures

I think the heading is quite self-explaning; the second video covers the creation of figures. It is using a library called vuesz which I have never heard of. Will test it later!

Descriptive statistics   

Third, we get to know how to do descriptive stats using Python. Also more figures... But very interestingly a bootstrapping method in Python for getting confidence intervals! Great!

Inferential statistics

In this post we get to know how to to t-tests but also a simulation based approach for understanding false positives and multiple comparisons. Cool! Correlations and scatterplots.

Power Analysis

Again, self-explaining title. We get to learn how to simulate data to calculate power. Yes! We want to determine sample size! 

All of the above videos have a homepage in which you can read some and see Python code. Also, there are some exercises. Greate of you wanna learn stuff!

måndag 18 april 2016

The aim of this guide is to give both an introduction and to motivate the use of the Python programming language in research in the field of cognitive science. Past 10 years, we have seen a rapid development of scientific and numerical libraries in Python. In fact,  Python can now easily be used as a scientific and numerical computing environment and is a contender to propriety products such as MATLAB and Mathematica.  The goal of this guide is to put forward the areas of application and to highlight the advantages and appeals of using Python as the number one programming language in cognitive science.  Given the generality of the tools being discussed, it is  hoped  that this  guide  will  have  widespread  appeal  and relevance. That is, researchers in other fields may also find this guide informative.


Python can, as previously mentioned, be a strong contender with its choices (i.e., MATLAB).  MATLAB has for long been one of the favourite programming environments in cognitive science. Striking purposes of likeness in the middle of Python and Matlab are that both offer an intelligent  interactive array-processing  and  visualization  environment using  high-level  dynamic  programming  languages. Both are intended for quick prototyping and advancement. Both take into consideration consistent augmentation utilizing outer modules composed as a part of ordered languages like C/C++ and Fortran.

Python, be that as it may, incorporate that it is a broadly useful language whose application goes a long ways past numerical array-processing. Python is one of the main five programming languages right now being used all through the world. Python is a strikingly designed object-oriented language whose standard library is vast and extensive. Furthermore, Python is free open-source programming distributed according to an unrestricted software license. Similarly, its substantial arrangement of third-party modules and libraries are likewise, typically, released according to open-source programming licenses.

Numerical and Scientific Python


The essential Python language as presented in the past segment needs n-dimensional numerical arrays and the capacity to effortlessly plot and visualize information. These capacities, notwithstanding countless extraordinary reason investigative libraries are given by the Scipy/Numpy suite of modules. These libraries are consistently incorporated with jupyter to make a rich intelligent exhibit handling and representation environment, tantamount in usefulness to MATLAB and Mathematica.

Jupyter has a lot of really nice functions: Interactive superior parallel computing for clusters and multicore models, an online intuitive Notebook practically identical to that utilized as a part of Mathematica, sql-based searchable summon histories, in-line illustrations, and typical arithmetic with TEX-based yield. Markdown can be used to create reports.

PC based Experiments

PC based brain research and psychophysics analyses are presently verging on universal in cognitive science.  While these undertakings have been customarily taken care of by GUI-based projects like
e-prime, Presentation, and superlab , these projects don't take into account the adaptability and control that is frequently requested by researchers. While programming environments like Matlab are being utilized as a distinct options for GUI-based projects, MATLAB's special-purpose nature is not well suited to the non-numerical  programming  necessary  for  experimental  stimuli presentation and recording. Python, because of the generality of its language, have a broad pool of libraries for creating graphical interfaces (e.g. wx-python, pyGTK, pyQt), and computer game libraries (pyGame,  pyglet), Python takes into account significant adaptability and complexity in the outline test programming.  At present, there are no less than 5 Python-based stimuli presentation programs: PsychoPy, OpenSesame,  ExPyriment, vision-egg, and pyepl. Note, that both PsychoPy and OpenSesame offers GUI-based projects.

To conclude this post, Python can be used for many things. It is a general purpose language so you can, basically, do whatever you want. Althoug, R may be more common when it comes to statistics you can, of course, also analyze your data with Python. My last post cover some jupyter notebooks that teaches you analysis using Python: http://pythondataanalysis.blogspot.se/2016/04/great-resources-for-learning-how-to.html.
I will, however, return with more Python and data analysis-related stuff. Later!