lördag 30 april 2016

Python for Cognitive Scientists - Choosing your IDE

In my two previous posts I have shared some good learning resources for Python (here) and a short guide on why you should learn Python as a Cognitive Scientist (here). This post will touch on what I think is a very important tool when it comes to programming Python (but also for programming in any language); namely the Integrated Development Environment (IDE).

Although Python, as well as most programming languages, can be written using any text editor (e.g., Notepad in Windows or Gedit, Vim, Emacs, etc. in Linux) an IDE offers a little bit more (yes, both VIM and Emacs are very powerful but may have a steeper learning curve...). Below you can see a quote from the above linked Wikipedia article:
An integrated development environment (IDE) is a software application that provides comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. Most modern IDEs have intelligent code completion.
 I think the above quote illustrates pretty nicely why an IDE is a powerful and necessary tool for any programmer.

In this post I will consider three Python IDEs: Spyder, PyCharm, and Rodeo  (there are more. See this link, for example).

Spyder

Spyder is the IDE I have used most of my time and there are some aspects of it that I prefer before PyCharm and Rodeo.
[Spyder is] a powerful interactive development environment for the Python language with advanced editing, interactive testing, debugging and introspection features

Spyder uses iPython as its default command line environment (of course, you can choose to use the regular Python interpreter also). iPython comes with a lot of perks: such as that it has built-in support for Matplotlib. It also alleviates some of the issues with editing modules, since iPython supports auto-reloading modules.

In Spyder you can run a selected part of the script (F9) and a complete script file (F5) from within the editor.

It also offers deep introspection, highlights errors,  warnings, and opens up the docstring information when calling a function (i.e., iPython functionality). Errors and warnings are displayed on the left of the line number. Inspection is carried out by clicking and holding  over the error and warning icons. Highlighting is extremely helpful because when highlighting a variable all instances of it is also highligted; easily tracked, that is!

Furthermore, it has a very similar function to MATLAB (CTRL+D - Open definition feature in MATLAB): Spyder can find the file or line where a function was defined by holding CTRL and clicking the function name.
One really great feature of Spyder's interface is the object inspector. All objects that were created in the iPython console can be examined in the inspector. Note, you can also inspect any variable in the console; by typing the variable name and getting the output. Nice feature! It has been a while since I worked with MATLAB but as far as I remember MATLAB has a similar feature. I am, however, working quite a lot in RStudio which has a similar feature. For sure. I like PyCharm very much and it is free.

PyCharm

The next IDE is PyCharm. I have not worked that much in it and if you are a not a student (or an academic, I think) you may have to pay for the professional version of the IDE (Spyder is FREE!). I got myself a student license of the professional version some weeks ago and I start to like it!

PyCharm is one of the most popular Python IDEs. It’s has so many  features. For instance,  incredible code completion, code analysis,  andcode navigation. It also have very good Django, JavaScript, HTML, and CSS support, great debugger, to name a few!

PyCharm provides smart code completion, code inspections, on-the-fly error highlighting and quick-fixes, along with automated code refactorings and rich navigation capabilities.
 The interface of PyCharm can be customized. There are some themes that are dark (I need dark themes on my software) and you can make it look great.  As far as I understand when you execute your code (SHIFT-F10) your variables are not saved in the console. You can, however, select your code and right-click to choose "Execute selection in Console" (the console seems also to be iPython out-of-the box). I also like that it has good version control support (e.g., GitHub). That is great. I think I will continue using this and making it my number one IDE. Just need to find a way to have an object inspector or something similar. PyCharm supports plugins and there seems to be many (I use markdown many times so I installed a markdown plugin, for instance).  Maybe I will update this section when I have played around more with PyCharm.

Rodeo

I have not used Rodeo that much but I am going to mention it. Why? Well, I really like RStudio and Rodeo is basically RStudio for R. 
Rodeo is an installable app that runs natively (as a standalone application) on your desktop. It’s built with Electron, a cross-platform framework for building desktop apps with good ‘ole javascript and HTML.

Rodeo is light-weight. That is, there are now fancy features such as those in Spyder or PyCharm. The interface is nice and clean. You have a script part, an iPython console, an Environment (object inspector in Spyder), and areas for plots, directories, and so on. Rodeo may be for the RStudio users but if you looking for a lot of features such as does in RStudio; go for Spyder or PyCharm. Give it a try!

To summarize, I would like something that is a mix between Spyder and PyCharm. I like these two IDEs and I think you can use either one of them. If you are coming from MATLAB maybe Spyder is more familiar and the learning curve will be less steep compared to PyCharm. I think. however, that in the end PyCharm is the most powerful one.

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!


söndag 17 april 2016

Great resources for learning how to program in Python

In this post I am going to list a couple of great resources for learning how to write code in Python. I start with a couple of iPython notebooks. If you are not familliar with iPython (called jupyter these days):
"Notebook documents (or “notebooks”, all lower case) are documents produced by the Jupyter Notebook App which contain both computer code (e.g. python) and rich text elements (paragraph, equations, figures, links, etc...)."
  • Poll aggregation, web scraping, plotting, model evaluation, and forecasting (Homework 1) (solutions)
I follow up these with more notebooks and they are more on general Python learning;
I end this post with this 3+ hours long video on data analysis with Pandas. This is a tutorial that introduces you to manipulating and analyzing large and small structured data sets.  Hope these resources are enough for now! I may update the list when I find more!
The first jupyter/iPython notebooks come from a Harvard course.

It contains homeworks, and solutions to these homeworks. By doing the homeworks you will be guided through a number of data analysis, mining, scraping, manipulation problems with Python and iPython/Jupyter notebook!