%matplotlib notebook

from mne.io import read_raw_bdf
from mne import find_events, Epochs

raw = read_raw_bdf('data/magic-trick-raw.bdf', preload=True)
raw.filter(0.5, 20)

events = find_events(raw)
event_id = {
    'Ace of spades': 1,
    'Jack of clubs': 2,
    'Queen of hearts': 3,
    'King of diamonds': 4,
    '10 of spades': 5,
    '3 of clubs': 6,
    '10 of hearts': 7,
    '3 of diamonds': 8,
    'King of spades': 9,
}
epochs = Epochs(raw, events, event_id, tmin=-0.2, tmax=0.8)

"""
Explanation: <img src="images/charizard.png" alt="Expert" width="200">
Computing the ERP
Welcome to the final level!
In this level, we will construct event related potentials (ERPs) and use them to figure out which playing card the volunteer was thinking about. The cell below contains the code we have written so far. Execute it to perform the analysis all the way to cutting epochs.
End of explanation
"""


print(epochs.ch_names)

"""
Explanation: Selecting epochs
Throughout this exercise, we have created many variables: raw, events, event_id, and epochs.
We have also learned that variables are like little machines of their own.
Let's take a look at even more things they can do!
Take for example our event_id dictionary.
We can look things up in it.
You do this using square brackets [ ]. (Python uses many different kinds of brackets!)
For example, to look up the event code for the laying card 'Ace of spades', you would write:
python
event_id['Ace of spades']
We can string together the "lookup" functionality and the visualization functionality, like this:
python
fig = epochs['Ace of spades'].plot()
Go ahead and try it: visualize the epochs belonging to the different paying cards.
If you need more code cells, you can use the Insert menu on the top of the page.
Visualizing the event-related potential
At this point, we have applied a frequency filter to the data and cut epochs: snippets of data that are likely contain signals that are related to the processing of the stimuli.
However, there are still so many overlapping signals that it's difficult to see anything.
During the lecture, you have heard about the "event-related potential" (ERP).
By averaging all the epochs corresponding to a stimulus, signals that are consistently present every time a stimulus was presented will remain, while all other signals will more or less cancel out.
The Epochs object provides a method named average to compute, you guessed it, the average of all the epochs.
This method produces an a
Like most objects, it has a plot method of its own to visualize it.
For example, here's how to compute the ERP using all of the epochs:
python
erp = epochs.average()
Try it below: create the ERP using all epochs. Then, use the plot method of the new variable containing the ERP to visualize it. Store the result in a variable (e.g. fig) or the figure will show up twice.
Making a better visualization
In the visualization you just made, the EEG signal at each sensor is drawn on top of the others.
(This is called a "butterfly" plot.)
These visualizations are great to determine when things are happening.
Around 0.1 seconds after a playing card was shown, we get a burst of activity (the P100).
But let's make a nicer figure, where each EEG sensor gets its own graph.
For extra niceness, lets also position the graphs to reflect the positions of the sensors.
The first order of business is to obtain the positions of the sensors.
Each sensor has a name, and you can see them by displaying the epochs.ch_names variable (yes, objects can not only have methods, they can also have variables). Execute the cell below and have a look at the names:
End of explanation
"""


epochs.plot_sensors()

"""
Explanation: As we have already seen, the "Status" channel does not contain the signal of an EEG electrode, but a special STIM signal.
Disregarding this channel, all the remaining channels contain the signals of 8 EEG electrodes.
The names of the electrodes follow a standardized naming scheme, called the 10-05 system.
Knowing this, we can infer the positions of the electrodes from their names by using the epochs.set_montage() method.
So go ahead and use the epochs.set_montage() method in the cell below, keeping these two things in mind:
 - The method needs as a parameter the naming scheme of the electrodes, which in our case is 'standard_1005'.
 - The method will work directly on the epochs variable, no need to assign the result to anything.
If you did it correctly, the code in the cell below will show us the inferred positions for the electrodes:
End of explanation
"""


erp1 = 

erp2 = 

erp3 = 

erp4 = 

erp5 = 

erp6 = 

erp7 = 

erp8 = 

erp9 = 

"""
Explanation: With the electrode positions figured out, we can now make use of the plot_evoked_topo function. It lives in the mne.viz module. In the cell below, use this function to visualize the ERP and get a good look at the signal recorded at each sensor.
Keep the following in mind:
 - Before you can use the plot_evoked_topo function, you must import it.
 - The plot_evoked_topo function lives in the mne.viz module, you can import it from there.
 - Take a look at the documentation of the function to figure out how to use it. Note that while there are many possible parameters, only one parameter is required. You can leave all optional parameters alone.
 - You can use the epochs.average() method to create an Evoked object that represents the event-related potential (called the "evoked response" in the documentation).
Whas this your card?
We are very close now to figuring out which playing card the volunteer was thinking about. What is left to do is to create the ERP for each playing card separately and then plotting them on top of each other so we can see which ERP has a peak that jumps out to us.
Remember that we could select the epochs belonging to a particular card with epochs['Ace of spades']. You can combine this with the average() method like so:
python
erp_ace_of_spades = epochs['Ace of spades'].average()
Go ahead and compute the ERP for each of the nine playing cards. Their names are:
'Ace of spades'
    'Jack of clubs'
    'Queen of hearts'
    'King of diamonds'
    '10 of spades'
    '3 of clubs'
    '10 of hearts'
    '3 of diamonds'
    'King of spades'
End of explanation
"""


erps = [erp1, erp2, erp3, erp4, erp5, erp6, erp7, erp8, erp9]

"""
Explanation: Nine variables containing nine ERPs are a bit unwieldy to type out every time. Let me make things a little bit more manageable and make a list variable. If you execute the cell below, a new variable is created (named erps) that is a list that contains the nine erp# variables we created above.
End of explanation
"""


