Beginner level

Welcome! Our goal in this module will be to let you have a taste of what programming is like.

I'm going to assume that until now, all your interactions with a computer have been through a graphical user interface (GUI). These interfaces completely revolutionized how we interact with computers by simplifying what is an enormously complex machine into an appliance that has only a few functions.
Each function can be executed by pressing a button.
Like it's a coffee machine.

But the computer you are talking to right now is not a coffee machine. Once you drop the GUI mask and start interacting with the computer itself, its full potential becomes available to you!

We're going to have to start from scratch. So settle in. You've got a lot to learn about programming before we can get to data visualization.

Your first ever program

Programming code is written in a programming language. You'll have to learn this language in order to talk with the computer. Don't worry though, programming languages are designed to be very easy to learn. For the purposes of this hands-on session, you only need to know a little of the vocabulary and grammar of a programming language called Python.

The text you are reading now is inside a "cell". This notebook contains both text cells like this one, and "code cells" in which you can write programming code and have the computer "execute" it.

The first thing I'm going to teach you is how to use the computer as a calculator. In a programming language, arithmetic works just as you would expect it to work. Try typing 1 + 1 in the cell below and press Ctrl + Enter (both keys on your keyboard at the same time) to execute the code.

Note about colors As you type in programming code in the cell below, you will notice the computer will automatically color parts of the text. These colors are purely a visual aid for you, the programmer, and have no meaning in the programming language.

In [ ]:
1 + 1

Did it work? Hopefully the computer answered back with "2". Phenomenal computer power at work ladies and gentlemen! In the Python language, +, -, *, / all do what you would expect them to do. For example, to compute the average of 5, 3, 7 and 10, we would write:

(5 + 3 + 7 + 10) / 4

just like you would write it in math. Try it in the code cell above if you like.

Variables

The computer ran your little program, reported the answer back to you, and by now has forgotten all about it. If we wish for the computer to remember the result of a computation, so we may use it in another computation, we need to give it a name by using the = character. Try running the program below and you'll see what I mean:


In [ ]:
x = 1
y = 2
z = x + y
z * 3

In good mathematical tradition, I've named various things x, y and z and re-used them in various lines of the program. You are free to choose whatever names you like, but there are some rules. For example, you cannot name a result +, since that would be very confusing. Another important restriction is that names can not have spaces. For example, the answer is not a valid name, but the_answer is (notice I used an underscore "_" character instead of a space, which is a common thing that programmers do).

Naming things is terribly important in programming, for it bestows meaning. When you get down to it, a computer is just a pile of carefully arranged sand with a rectangle of little lights. It takes a human to bestow meaning to the patterns of light and currents of electricity flowing through the machine. Do the numbers mean sheep to barter? Nuclear missiles to fire? Thoughts in a persons brain?

Your turn, mighty human. Write an elegant program to solve the following math problem:

Jan has 5 coins and Mary has 3 coins.
How much coins do Jan and Mary have together?


In [ ]:

Functions and modules

For our next program, let's do something more difficult and compute the sine of 2 (it should be around 0.9). We can't easily compute that with just +, -, *, and /.

Programming languages are inspired a lot by the language of mathematics. In math, we would write the sine of 2 as:

$$\sin 2$$

Where "$\sin$" is a placeholder for "use whatever method you want to compute the sine". More formally, $\sin$ is called a function and 2 is an argument to this function.

Functions are central to programming. They are the verbs of the programming language. Once you've learned a few verbs you can say lots of things!

If numbers are your raw material, functions are the tools you use to manipulate them. There are tons of functions available to you. So many, that to keep track of them all, they are organized in different "modules". You can think of a module as a toolbox that has a nice label on it so we know what is inside. The sin function we want to use is kept inside the math module.

In order for us to use the sin function, we must first "import" it from the math module. In our metaphor, importing a function is like taking a tool from a toolbox and placing it on our workbench. In the grammar of our programming language, you say:

from math import sin

From that point on, the sin function is available for you to use. Computing the sine of two is then sin(2).

I've filled in the program for you below. Try running it.


In [ ]:
from math import sin
sin(2)

Your turn. Write a program that computes the cosine of 5. The function to compute the cosine is called cos and can also be found inside the math module (remember to import it first!). You can use the cell below to write your program in:


In [ ]:

We can combine functions with variables. Unfortunately, the answer to the cosine of 5 you have so cleverly computed above is already gone with the wind. You can "assign" the result of a function to a variable, just as we did above with numbers.

For example: here is how to assign the sine of 2 to a variable called my_result:


In [ ]:
my_result = sin(2)

Try running the above code cell. It doesn't seem to work??!! Nothing happened!

Actually, the program did work, but the computer is keeping silent. As we have seen, the computer will tell you the result that was produced by the last line in your program, but only if you did not assign that result to a variable. It was a choice made by programmers that were of the opinion that their computers were being too chatty.

Explicitly telling the computer to display something

There is a function that is not part of any module and is always available to you. This function will make the computer display things. It is called print and it writes text to the screen. Its name is a leftover from the times when computer monitors did not exist yet and the computer could only print things on paper. Funny how etymology applies to programming languages too.

Anyway, here is an example of the print function in action:


In [ ]:
my_result = sin(2)
print(my_result)

Ok, your turn. Write a program that assigns the cosine of 2 to the variable my_result. Use the print function to display the variable to check that it has changed.


In [ ]:

Working with text

Here is a little program that makes the computer say hello:


In [ ]:
print('hello')

In the Python programming language, literal text needs to always be surrounded by ' quotation marks. Without quotation marks, the program above would try to display the contents of a variable named hello. Try running this example and you'll see what I mean:


In [ ]:
hello = 'Hello, world!'
print(hello)

The print function can write multiple things at the same time by giving it more than one argument. To give multiple arguments, put a comma between them, like this:


In [ ]:
print('The man said:', hello, 'How are you?')

As a child, I would write little programs like this:


In [ ]:
name = input('What is your name?')
age = input('What is your age?')
color = input('What is your favorite color?')
pet = input('What kind of pet would you like?')
job = input('What do you want to be when you grow up?')

print('')
print('There once was a', pet, 'named', name)
print('Every day,', name, 'bought', age, 'bottles of lemonade.')
print('Until one day it turned', color, 'from drinking too much.')
print('It was rushed to the hospital by a', job)

To familiarize yourself with manipulating text in a programming language, try modifying the program above to tell a story of your own.

Loading some MEG data

Finally! We got all the ingredients we need to use MNE-Python. Using functions, saving results to variables and re-using them. These concepts allow us to do pretty powerful things.

We're going to instruct the computer to "load" some MEG data. The MEG data is currently stored as a file on the hard drive, called:
data/sample-raw.fif
The above "file path" may look weird to you if you are used to Windows. The computer you are talking to is a linux machine. Paths look a little different.

To "load" a file means to make it available as a variable so we can use it in our programming code. MNE-Python has a function that we can use to do this for us. It's called read_raw_fif and it lives in the mne.io module. Executing the cell below will import the function for you, so you can use it. Importing a function will also make it available "from now on", also in future cells.


In [ ]:
from mne.io import read_raw_fif

Now, everything you've learned so far needs to come together. I'm going to leave it up to you to use the function correctly. Make the computer load the MEG data and put it inside a variable called raw, by writing a single line of code.

Here are some pointers to help you along:

  1. The function you want to call is named: read_raw_fif. It has already been imported.
  2. Call it with a single argument: the name of the file to load. Remember the example sin(2)
  3. The name of the file to load is data/sample-raw.fif
  4. The name of the file is text. Remember what you know about text and quotation ' marks!
  5. Assign the result to a variable with the name raw using the = symbol.

    Write your single line of code in the cell below:


In [ ]:

If your code above was correct, executing the cell below will display some information about the data you've just loaded:


In [ ]:
print(raw)

Visualizing the MEG data

Now that the MEG data has been loaded into memory, we can look at it. The data is in it's "raw" form, meaning it has come straight out of the scanner and nothing has been done to it yet. MNE-Python has a visualization tool for data in it's raw form, called plot_raw. In programmer lingo, visualizing data points is referred to as "plotting)".

Again, I'll write the code to import the plot_raw function for you. This time, I'm also going to add some housekeeping code that will instruct the computer to send the graphics to your browser.


In [ ]:
%matplotlib notebook
print('From now on, all graphics will send to your browser.')

from mne.viz import plot_raw

It's up to you to write the code to call the function and give the raw variable as argument to this function. Ready? go!


In [ ]:

If you wrote the code correctly, you should be looking at a little interface that shows the data collected on all the MEG sensors. Try using the arrow keys and clicking in the figure to explore the data.

Continue onward to the adept level!

Great job making it this far. You've taken your first steps along the path towards becoming a data analyst. Take a look at the clock. If we still have some time, I'd like to invite you to move on to the next level.

Move on to the adept level