Session 11 - MEG MNE Flashcards
What is MNE?
a Python module for analysing MEG and EEG data.
Many of the code below will run in Spyder for example, - (2)
the interactive plots are not interactive in Colab.
We will try to run our code in Spyder today but use Colab for ‘testing’
Previously, we have learned to install additional python modules using
‘pip’
To install MNE on Google Colab or Windows/Spyder machines, we enter:
To install MNE on Spyder, you type this (‘pip install mne’) on the - (2)
in the terminal to the bottom right.
Remember to ‘restart kernel’ after you do this. You might also have to set the Graphics output type to ‘QT - to ensure plots in Spyder are interactive
MEG data come in a variety of formats. Most of the existing data which is available at YNiC is from a
4-D system like this:
All the file paths in this section is relative to s8_meg directory in the material directory, so need to change the directory you are working in Spyder or replace the full paths such as.. C:\Users\gs1211\ . . .
This code is saying that:
This code fetches a specific branch (s8_meg
) from a Git repository (https://vcs.ynic.york.ac.uk/cn/pin-material.git
) and clones it into the current directory
We can check git repository is there by saying:
This command lists the contents of the directory s8_meg
within the pin-material
directory, showing details such as permissions, ownership, size, and modification time, with the most recently modified files appearing first.
We have loaded in the MNE python module. And We have loaded in the MNE python module. And we have also git-cloned some MEG data. Now we can use a
python script to look at the data:
First we want to change the directory where s8_meg live by saying the following code - (2):
This code imports the Python libraries ‘os’ and ‘mne’.
Then, it changes the current working directory to ‘/content/pin-material/s8_meg/’ using the ‘os.chdir()’ function.
For spyder to change directory we can change
os.chdir(‘/content/pin-material/s8_meg/’) to..
You need to change this on Windows - something like C:\Users\aw890\pin-material\s8_meg
What is the difference between the use of backslashes () and forward slashes (/) in file paths?
Backslashes () are typically used in Windows file paths, while forward slashes (/) are used in Unix-based systems like Linux or macOS.
What is the main difference between the organization of MEG data and fMRI data?
MEG data is organized by sensors, whereas fMRI data is organized by voxels in the brain.
How are sensors positioned in MEG data collection?
Sensors in MEG are fixed in space within a helmet, detecting brain activity at a distance.
What is the purpose of registration in MEG data analysis?
Registration ensures accurate positioning of sensors relative to the head and brain.
What is the role of a 3D digitizer like a ‘Polhemus’ in MEG data analysis?
A 3D digitizer like a ‘Polhemus’ measures head and facial features to facilitate accurate sensor positioning.
What is the next step after examining position data in an old MEG dataset?
The subsequent step involves analyzing individual MEG timecourse data.
What is the purpose of the following code snippet?
This code reads MEG data from a file and extracts different parts of the MEG dataset for analysis.
What function is used to read the MEG data from a file from mne?
The mne.io.read_raw_bti()
function from the MNE module is used to read the MEG data from the specified file.
What does this line of code mean?
raw = mne.io.read_raw_bti(‘./R1025_P1119a_4/c,rfDC’) - (2)
This line of code reads MEG data from a file of participant R1025_P1119a_4 using the read_raw_bti
function from the MNE module.
The specified file path is ‘./R1025_P1119a_4/c,rfDC’.
What is output of
raw = mne.io.read_raw_bti(‘./R1025_P1119a_4/c,rfDC’
print(raw)
What does the raw.info
object contain?
if raw = mne.io.read_raw_bti(‘./R1025_P1119a_4/c,rfDC’
The raw.info
object contains basic information about the MEG data, such as the number of channels and device information.
Outputting
print(raw.info)
raw - loaded mne data
The raw object containing meg data loaded via mne
has a member called info which is.. - (2)
his is an mne Info structure which behaves something like a dictionary.
It has a number of elements which refer to details about the scan (rather than the raw data).
What does this output show of raw.info? - (7)
Going from top to bottom, we can see that at present there are no bads set: i.e. we have not configured bad channels yet.
There is a list of channel names in the ch_list attribute, and the chs attribute is actually a list of all channel details; when printed, it simply shows a summary of what types of channels we have.
On our MEG system system, the main MEG channels are referred to as A1 to A248. When loading the data into MNE, these are mapped into MEG 001 to MEG 248; the reference channels are also remapped in the same way. We therefore need to remember to refer to the channel names in the way that MNE expects.
The next few lines refer to the transform which is calculated based on the “Coil-on-Head” calculations and the movement into the standard space that MNE uses: we ignore this for now.
Under dig, we see that we have 1558 digitisation points: these come from the Polhemus head digitisation system which is used with the 4D MEG scanner.
We can also see that the data were filtered between 0 and 339.1Hz.
We then get some information about the date of the scan, the total number of channels and then the sfreq: the sampling frequency.
What valuable information does raw.info.keys()
provide? - (2)
By printing the keys available in the raw.info
structure, we gain insight into the metadata associated with the MEG data being analyzed.
This metadata may include information such as acquisition parameters, stimulus information, device configuration, channel information, data processing history, and more. .
What does this following code snippet do? - (4)
Accessing the keys in the raw.info structure
This code snippet extracts digitisation data from the MEG dataset, storing it in a variable named ‘dig’.
It then prints the type of data contained in ‘dig’ and displays the first few elements of the digitisation data, providing insight into the location of critical head structures.
Finally, it prints the total number of elements in the digitisation data.
What does the line dig = raw.info['dig']
accomplish? - (2)
This line extracts digitisation data from the MEG dataset, specifically the set of 3D points in space that indicate the location of critical head structures. I
t stores this data in a variable named ‘dig’.
What does the code print(type(dig))
do?
The code print(type(dig))
prints the type of data contained in the variable ‘dig’, which is <class ‘list’>
What information does the line print(dig[0:5])
provide? - (2)
The line print(dig[0:5])
displays the first five elements of the digitisation data stored in the variable ‘dig’.
These elements represent 3D points indicating critical head structures, such as the nasion and inion as well as extra points when the stylus stroked the head structure
What does the code print(len(dig))
show?
The code print(len(dig))
prints the total number of elements in the digitisation data, providing insight into the overall size
Explain the output - (3)
[<DigPoint | LPA : (-74.6, 0.0, 0.0) mm, ….., Extra #2 : (-19.5, 66.3, 39.6) mm…]
We see that the digitisation points are stored as a list of objects of type DigPoint.
If we want to plot these data, we will need to extract them.
We see that the first three are different to the rest which seem to be called Extra X.
Those first three points define the coordinate system and although they are both interesting and useful, we don´t need them right now. Instead we will focus on the Éxtra’points which contain the shape of the head.
What does the code ex_1 = dig[3]
do? - (2)
The code ex_1 = dig[3]
extracts a digitisation point from the digitisation data stored in the variable ‘dig’.
Specifically, it selects the digitisation point after the first three points and assigns it to the variable ‘ex_1’.
What does the code print(type(ex_1))
reveal?
The code print(type(ex_1))
prints the class of the object stored in the variable ‘ex_1’, indicating that it belongs to the class mne_fiff.digitisation.DigPoint
, representing a digitisation point.
What does the code print(dir(ex_1))
accomplish? - (2)
The code print(dir(ex_1))
lists all methods and attributes associated with the digitisation point stored in the variable ‘ex_1’.
However, the output can be cluttered, making it difficult to discern relevant information.
What does ([x for x in dir(ex_1) if not x.startswith(‘_’)]) do? - (2)
The code [x for x in dir(ex_1) if not x.startswith('_')]
produces a cleaner version of the list by filtering out methods and attributes that start with an underscore (‘_’).
This list comprehension generates a new list containing only the methods and attributes accessible for the digitisation point, making it easier to navigate.
What does the method startswith()
accomplish? - (2)
The method startswith()
checks whether a string starts with a specified prefix.
It returns True
if the string starts with the prefix, and False
otherwise.
These are all
lot of private methods (things which start with an underscore “_something”) which clutter up our view, so we decide to exclude them using a list comprehension.
what does this mean:
[x for x in dir(ex_1) if not x.startswith(‘_’)]
It says that we are going to loop over every item in dir(ex_1) and we will keep it (x for x in) if it doesn’t start with an underscore: (if not x.startswith(‘_’)).
What does this code do? - (2)
- It first prints the data type (
float
) and the value of the sampling frequency (sfreq
) associated with the MEG data. - Then, it prints the data type (
list
) and the list of channel names (ch_names
) in the MEG data.
Output of this code
What does the code print(ex_1.keys())
accomplish?
The code print(ex_1.keys())
prints out the keys associated with the digitisation point ex_1
,
What does the code print(ex_1['r'])
display?
The code print(ex_1['r'])
prints out the values of the ‘r’ key associated with the digitisation point ex_1
, which represent the x, y, and z coordinates of the point in a tuple where Polheums is pointing to
What does the code print(ex_1['ident'])
reveal? - (2)
The code print(ex_1['ident'])
displays the value associated with the ‘ident’ key for the digitisation point ex_1
.
In this case, it indicates the identification of the point, which might not hold significant information.
What information does the code print(ex_1['kind'])
provide? - (2)
The code print(ex_1['kind'])
reveals the type of point represented by the digitisation point ex_1
.
It indicates that the point is of type ‘FIFFV_POINT_EXTRA’, which suggests that it is an extra point originating from the Polhemus.
What does the code print(ex_1['coord_frame'])
indicate? - (2)
The code print(ex_1['coord_frame'])
displays the coordinate frame associated with the digitisation point ex_1
.
In this case, it indicates that the coordinates are referenced to the head coordinate system (‘FIFFV_COORD_HEAD’).
What is the purpose of the following code snippet? - (2)
The purpose of this code snippet is to extract digitisation points from the ‘digitisation’ data stored in the variable ‘dig’.
It specifically selects points originating from the Polhemus device and stores their coordinates in a numpy array.
What does the line if pt['kind'] == mne.io.constants.FIFF.FIFFV_POINT_EXTRA:
check for? - (2)
The line if pt['kind'] == mne.io.constants.FIFF.FIFFV_POINT_EXTRA:
checks whether a digitisation point is of the type ‘FIFFV_POINT_EXTRA’, indicating that it originated from the Polhemus device.
These are the types (or ‘kind’) of things we want! They are ‘extra’ because they come from the Polhemus and not the scanner hardware itself.
What does the code out_pts.append(pt['r'])
accomplish?
The code out_pts.append(pt['r'])
extracts the coordinates of digitisation points in a tuple of (x,y,z) originating from the Polhemus device and appends them to a list called ‘out_pts’. - specifcying location of digitsation point in 3D space
What does the line print(f'Here are the dimensions of the point array: {out_pts.shape}')
display? - (3)
prints the dimensions of the numpy array containing the digitisation points.
Here are the dimensions of the point array: (1555, 3)
So, we finish with 1555 Polhemus points. We started (back in the info structure) with 1558 points, and found that 3 of them were “cardinal points”, so this makes sense.
The three reference points typically found in digitisation data that we don’t want are: - (3)
Nasion: The point at the bridge of the nose.
Left preauricular: The point in front of the left ear.
Right preauricular: The point in front of the right ear.
What does this code do?- - (3)
This code snippet utilizes numpy and matplotlib to create a 3D scatter plot of digitisation points stored in the numpy array ‘out_pts’.
It first creates a figure and an axis with 3D projection, extracts the x, y, and z coordinates from ‘out_pts’, and then plots these points as a scatter plot in 3D space using the ‘scatter’ function.
Finally, it displays the plot.