Array Programming in Numpy
Authors
Setup
Download Data
import owncloud
from pathlib import Path
Path('data').mkdir(exist_ok=True, parents=True)
owncloud.Client.from_public_link('https://uni-bonn.sciebo.de/s/FxrxNSiYqYndRGW').get_file('/', 'data/eeg_epochs.npy')
owncloud.Client.from_public_link('https://uni-bonn.sciebo.de/s/Ff3nqPXRK3dxGSG').get_file('/', 'data/epoch_times.npy')TrueSection 1: Indexing and Slicing
Often, we want to access specific values stored inside an array. This
can be done by slicing and indexing. Indexing simply means getting the
element at a specific position. Python starts counting at 0, so x[0]
will get the 1st element of x. Slicing means getting all values
within a certain range by providing a start and stop position. When we
index and slice multi-dimensional arrays, we must provide multiple
coordinates - one per dimension. We can also omit a value to indicate
that we want to get all values along that dimension.
| Code | Description |
|---|---|
import numpy as np |
Import the module numpy under the alias np |
x = np.arange(2,7) |
Create an array with all integers between 2 and (not including) 7 and assign it to the variable x |
x[0] |
Get the 1st element of x |
x[-1] |
Get the last element of x |
x[2:5] |
Get the 3rd 4th and 5th elements of x |
x[:5] |
Get everything up to and including the 5th element of x |
x[2, 1] |
Get the element in the 3rd row and 2nd column of the 2-dimensional array x |
x[0, :] |
Get the whole first row of x |
x[1:5, 2] |
Get the values from rows 2 through 5 in the 3rd column of x |
Exercise: Import the Numpy module under the alias np.
Solution
import numpy as npExercise: Create an array with the integers between 1 and 10 and assign it to a variable x.
Solution
x = np.arange(1,10)
xarray([1, 2, 3, 4, 5, 6, 7, 8, 9])Exercise: Get the second element of x.
Solution
x[1]2Exercise: Get the second-to-last element of x.
Solution
x[-2]8Exercise: Get all the elements of x except for the first two.
Solution
x[2:]array([3, 4, 5, 6, 7, 8, 9])Exercise: Get all elements of x except for the first and last.
Solution
x[1:-1]array([2, 3, 4, 5, 6, 7, 8])Exercise: Run the cell below to define the 2-dimensional array x. (You don’t need to add or change any code.)
Solution
x = np.array([
[1,2,3],
[4,5,6],
[7,8,9]
])Exercise: Get the first row of x with the values [1, 2, 3].
Solution
x[0, :]array([1, 2, 3])Exercise: Get the second column of x with the values [2, 5, 8].
Solution
x[1]array([4, 5, 6])Exercise: Get every value of x except for the bottom row.
Solution
x[:, 1]array([2, 5, 8])Exercise: Get every value of x except for the rightmost column.
Solution
x[:,:-1]array([[1, 2],
[4, 5],
[7, 8]])Section 2: Filtering
Another way to get specific values from arrays is by filtering. By filtering, we can extract values from an array based on a specific condition. For example, we can get all values that are greater than 0. Filtering can be divided into two steps: 1. Create a mask that specifies which elements meet the condition. 2. Apply the mask to the array to filter it. By creating different masks we can vary the criteria for selecting data from an array.
| Code | Description |
|---|---|
mask = x>1 |
Create a mask that is True where x is greater than 1 and False otherwise |
mask = x=="k" |
Create a mask that is True where x is equal to "k" and False otherwise |
mask = x<=1 |
Create a mask that is True where x is smaller than or equal to 1 and False otherwise |
x[mask] |
Get all elements of x where the mask is True |
np.sum(mask) |
Sum all elements of the mask (True counts as 1, False as 0) |
np.size(mask) |
Get the number of elements in x |
x = np.random.randint(low=5, high=15, size=20) |
Create an array with 20 random integers between 5 and 15 and assign it to the variable x |
Exercise: Create an array with 100 random integers between 0 and 10 and assign it to the variable x.
Solution
x = np.random.randint(low=0,high=10,size=20)
xarray([0, 3, 8, 9, 6, 8, 4, 3, 9, 1, 3, 8, 1, 5, 1, 0, 7, 6, 0, 2])Exercise: Get all values of x that are smaller than 5.
Solution
mask = x<5
x[mask]array([0, 3, 4, 3, 1, 3, 1, 1, 0, 0, 2])Exercise: Get all values of x that are greater than 3.
Solution
mask = x>3
x[mask]array([8, 9, 6, 8, 4, 9, 8, 5, 7, 6])Exercise: Get all values of x that are smaller than or equal to 2.
Solution
mask = x<=2
x[mask]array([0, 1, 1, 1, 0, 0, 2])Exercise: Execute the cell below to define an array containing a gene sequence. Then, get all adenine ("A") nucleotides from this array.
seq = np.array(["A", "T", "G", "C", "G", "T", "A", "C", "G", "T", "T", "A", "G", "C", "T", "A", "G", "G", "C", "T", "T", "A", "C", "G", "A", "T", "C", "G", "T", "A"])Solution
mask = seq == "A"
seq[mask]array(['A', 'A', 'A', 'A', 'A', 'A', 'A'], dtype='<U1')Exercise: Is there more cytosine ("C") or guanine ("G") in the sequence?
Solution
np.size(seq[seq=='G']), np.size(seq[seq=='C'])(8, 6)Section 3: Analyzing Multi-Dimensional Data
Now that we learned about indexing, slicing and filtering, we have the
tools for analyzing multidimensional data! In this section we are going
to analyze electroencephalography (EEG) recordings of brain responses to
pure tones. The data are stored in a 3-dimensional array, where the
dimensions represent: 1. The number of trials or epochs
(i.e. repetitions of the same stimulus) 2. The number of EEG channels 3.
The number of time points in the recording When we use Numpy functions
to compute properties like mean or standard deviation on a
multidimensional array, we can use the axis argument to select a
specific dimension.
| Code | Description |
|---|---|
x=np.load("myfile.npy") |
Load the data stored in "myfile.npy" and assign them to the variable x |
np.save("newfile.npy", x) |
Save the array x to a file called "newfile.npy" |
np.shape(x) |
Get the shape of x (i.e. the size of all dimensions of x) |
x[0, :, :] |
Select the 1st element along the 2nd dimension of the 3D array x |
x[:, 4:10, :] |
Select the elements 5 to 10 along the 2nd dimension of the 3D array x |
x[1, :, :3] |
Select the 1st element along the 1st dimension and the first 3 elements along the 3rd dimension of the 3D array x |
np.mean(x, axis=0) |
Compute the mean across the 1st dimension of x |
np.std(x, axis=2) |
Compute the standard deviation across the 3rd dimension of x |
np.max(x) |
Get the maximum value of x |
Exercise: Run the cell below to load the data stored in the file eeg_epochs.npy and assign it to a variable called eeg.
Solution
eeg = np.load('data/eeg_epochs.npy')Exercise: How many epochs, channels and time points are there in the eeg data? Hint: The shape function gives you the dimensions of a data array.
Solution
eeg.shape(70, 59, 54)Exercise: Select the data from the first EEG channel.
Solution
eeg[:,0,:]array([[ 2.67680334e-05, 3.53526848e-05, 4.69128998e-05, ...,
4.12180852e-06, -3.86195567e-06, -2.89777466e-07],
[-1.30239872e-05, -1.09115245e-05, -4.11188614e-06, ...,
-7.63867312e-07, 6.58151272e-06, 6.19991800e-06],
[-1.00595697e-05, 4.88523393e-07, 4.72230958e-06, ...,
-2.46236180e-06, -3.20387595e-06, -5.78915245e-06],
...,
[ 2.52372107e-06, -1.59301144e-06, -9.74316541e-06, ...,
7.05183219e-06, 4.92650416e-06, -8.67060014e-06],
[-5.77128577e-06, -1.86679554e-06, 7.82888343e-06, ...,
9.23250416e-06, 5.31798981e-06, 2.36600820e-06],
[-1.02202164e-05, -1.27737461e-05, -1.95778938e-05, ...,
-1.81326091e-05, -2.71697951e-05, -2.43501838e-05]])Exercise: Select the data from the first 10 epochs for the 5th EEG channel.
Solution
eeg[:10, 4]array([[ 1.39659765e-05, 1.94901918e-05, 2.80597018e-05,
3.51590150e-05, 3.70034831e-05, 3.65254568e-05,
3.85490080e-05, 4.18792123e-05, 4.12654958e-05,
3.54950130e-05, 2.84851788e-05, 2.26845773e-05,
1.61354493e-05, 7.54263591e-06, 5.81742826e-07,
-6.74103167e-09, 4.95961635e-06, 9.76406387e-06,
1.07279989e-05, 9.19449400e-06, 7.60127597e-06,
6.43336564e-06, 5.70927773e-06, 5.62196261e-06,
5.04526556e-06, 3.10944867e-06, 1.92681839e-06,
3.45779694e-06, 5.17630336e-06, 4.30480985e-06,
3.80653720e-06, 7.13100330e-06, 1.02197941e-05,
7.65688613e-06, 3.70275756e-06, 6.70416145e-06,
1.46686401e-05, 1.76239245e-05, 1.33006174e-05,
9.30103348e-06, 9.74769420e-06, 1.05527914e-05,
8.97118506e-06, 7.99553000e-06, 9.12369553e-06,
8.97562557e-06, 5.85250288e-06, 3.37224821e-06,
4.86250273e-06, 8.71811215e-06, 1.15144652e-05,
1.21367429e-05, 1.10621673e-05, 8.79821604e-06],
[-1.07641624e-05, -9.19742670e-06, -6.33200336e-06,
-8.38390875e-06, -1.07952541e-05, -9.69771555e-06,
-8.68113844e-06, -1.01639860e-05, -1.04299776e-05,
-6.77367260e-06, -2.69844083e-06, -3.44833400e-06,
-9.51443409e-06, -1.52444572e-05, -1.39498369e-05,
-6.19179770e-06, -6.83841002e-07, -2.73764651e-06,
-5.83894499e-06, -1.76249707e-06, 5.37633172e-06,
4.97684156e-06, -3.04465668e-06, -8.08548489e-06,
-5.23942348e-06, -1.44483940e-06, -3.31956072e-06,
-7.65175719e-06, -8.51151329e-06, -7.79364597e-06,
-1.15751207e-05, -1.77041133e-05, -1.59217328e-05,
-4.17798408e-06, 4.15334057e-06, -1.35578907e-06,
-1.03312064e-05, -6.24266540e-06, 6.60309163e-06,
8.97269559e-06, -2.43820899e-06, -1.06255295e-05,
-6.54112981e-06, -1.02935906e-06, -3.56227828e-06,
-8.11631605e-06, -7.11677797e-06, -3.39900880e-06,
-1.59238462e-06, -6.33724023e-07, 3.70914486e-07,
-1.31158082e-07, -4.08882213e-07, 2.36467861e-06],
[-8.85988320e-06, -6.63548735e-06, -2.39947800e-06,
2.67121534e-06, 5.43425123e-06, 3.13712657e-06,
-1.25106528e-06, -1.53658296e-06, 2.27540077e-06,
3.39793383e-06, -8.56303635e-07, -4.89108047e-06,
-4.57862413e-06, -3.43371815e-06, -4.63744964e-06,
-4.40913080e-06, 8.36811808e-07, 6.85994215e-06,
7.47159961e-06, 3.81565498e-06, 2.11916047e-06,
4.81823123e-06, 8.32068332e-06, 8.40427828e-06,
5.33112372e-06, 3.55716035e-06, 6.08532616e-06,
9.45586951e-06, 7.68122508e-06, 1.65607132e-06,
-1.07227483e-06, 2.28671337e-06, 5.83857073e-06,
5.42016982e-06, 4.63868583e-06, 6.42312267e-06,
7.19208272e-06, 4.42813001e-06, 2.04648133e-06,
3.41370035e-06, 6.22337654e-06, 7.89353689e-06,
9.05865270e-06, 9.39864613e-06, 6.93878952e-06,
2.76603996e-06, 2.70028426e-07, 1.68617180e-07,
1.54154506e-06, 4.53520432e-06, 6.65257619e-06,
2.31975480e-06, -7.43566884e-06, -1.24993616e-05],
[ 5.41808862e-06, 7.28878265e-06, 5.58454295e-06,
1.68875382e-06, 1.07327708e-06, 3.15111934e-06,
1.97590588e-06, -3.29517921e-06, -6.99447309e-06,
-6.92766422e-06, -7.24414413e-06, -9.19540272e-06,
-7.88067863e-06, -1.86115880e-06, 2.58075209e-06,
1.41604363e-06, -9.69930832e-07, -2.43421312e-07,
1.38257852e-06, 1.59938044e-06, 2.44341014e-06,
4.00991265e-06, 2.57343168e-06, -1.21954298e-06,
-1.55049083e-06, 2.12253402e-06, 3.67047472e-06,
1.69367160e-06, 1.95229079e-06, 6.55254878e-06,
1.08386979e-05, 1.19209955e-05, 1.08925520e-05,
7.68513291e-06, 2.79634643e-06, 7.44187352e-07,
2.81605845e-06, 2.17904615e-06, -4.58043780e-06,
-9.23240538e-06, -5.27412409e-06, 1.22347954e-07,
-1.58935239e-06, -6.15279756e-06, -5.67552771e-06,
-2.04174757e-06, -5.78318310e-07, 2.45609326e-07,
3.14636405e-06, 4.70926394e-06, 2.03267028e-06,
-9.47867702e-07, -3.51785760e-07, 1.12613385e-06],
[ 5.96577240e-06, 2.49358171e-06, 1.56377449e-06,
4.15183992e-06, 4.96263740e-06, 3.32107210e-06,
3.96658594e-06, 7.33701210e-06, 8.31766318e-06,
5.12810117e-06, 1.72857135e-06, 3.80565594e-07,
-1.46773197e-06, -4.85024523e-06, -5.63308225e-06,
-9.46921861e-07, 4.73569539e-06, 4.97407217e-06,
8.32254840e-07, -5.77723669e-07, 3.33000842e-06,
7.30815940e-06, 6.63617221e-06, 3.24136843e-06,
1.29100892e-06, 1.37243496e-06, 1.68614009e-06,
1.57780317e-06, 1.57588635e-06, 1.91651999e-06,
2.09364785e-06, 9.92187791e-07, -1.78651025e-06,
-4.29953412e-06, -4.28927831e-06, -2.92468329e-06,
-2.81922460e-06, -2.79009135e-06, 5.93440659e-08,
3.74488700e-06, 3.73495238e-06, 1.17699516e-06,
8.41096125e-07, 1.61547128e-06, -1.76446034e-06,
-7.67274365e-06, -8.10147158e-06, -1.27581767e-06,
5.11993052e-06, 4.84301509e-06, -6.68824419e-08,
-4.31246503e-06, -5.16123421e-06, -2.79699793e-06],
[ 9.97966681e-07, -5.43301284e-06, -7.43204469e-06,
-3.71366497e-06, -7.84942057e-07, -2.67425245e-06,
-4.28652382e-06, -1.09287131e-06, 2.16877587e-06,
3.80687053e-07, -2.35468893e-06, -9.37551250e-07,
1.21741526e-06, 3.00243057e-07, 6.78355836e-07,
5.33785907e-06, 7.58268869e-06, 3.10967364e-06,
-6.32943915e-08, 4.37736203e-06, 7.89450013e-06,
1.49593677e-06, -6.85326178e-06, -4.31752484e-06,
5.53162301e-06, 9.19533280e-06, 5.01772298e-06,
3.12899886e-06, 7.42459428e-06, 1.18368742e-05,
1.26885496e-05, 1.25398316e-05, 1.28325659e-05,
1.27017403e-05, 1.34406275e-05, 1.56421452e-05,
1.49290168e-05, 8.72934632e-06, 3.03863793e-06,
3.77931806e-06, 6.42370108e-06, 3.61582958e-06,
-2.07961499e-06, -3.13223310e-06, 1.22034042e-07,
1.83492423e-06, 1.44230095e-06, 2.31791778e-06,
3.84439234e-06, 3.93332771e-06, 5.09258095e-06,
9.43788588e-06, 1.22221846e-05, 8.53822858e-06],
[-7.88108285e-06, -4.98156192e-06, -2.87493795e-06,
-2.84650756e-06, -3.25165304e-06, -2.57560735e-06,
-1.54930353e-06, -2.17389961e-06, -3.63043723e-06,
-2.11261851e-06, 2.27668476e-06, 3.87570220e-06,
1.12464474e-06, -1.68566323e-08, 2.21036053e-06,
7.64618685e-07, -6.28921538e-06, -9.73281009e-06,
-4.94485555e-06, -3.45870663e-07, -2.09517324e-06,
-4.37800820e-06, -2.65899154e-06, -2.15342034e-06,
-5.58081969e-06, -6.35728610e-06, -7.87158984e-07,
5.42347520e-06, 7.92215032e-06, 9.67563832e-06,
1.26203085e-05, 1.32848085e-05, 9.44269962e-06,
3.88468504e-06, 5.05318741e-07, 9.20132613e-07,
3.56709553e-06, 4.05332226e-06, -6.57359150e-09,
-4.19983323e-06, -2.89900562e-06, 1.43766052e-06,
1.71475223e-06, -2.73710048e-06, -6.34481215e-06,
-6.31592940e-06, -3.86461367e-06, 3.34865070e-07,
4.40428162e-06, 3.24027267e-06, -4.04840794e-06,
-9.38546499e-06, -6.06525138e-06, 1.10602432e-06],
[ 2.54570697e-06, -4.03626764e-08, -1.88481863e-06,
1.41051305e-06, 5.64781827e-06, 5.41195313e-06,
6.02845196e-07, -6.11183505e-06, -1.15057984e-05,
-1.22461503e-05, -8.86331116e-06, -6.42240674e-06,
-6.93730747e-06, -6.52723810e-06, -3.70295381e-06,
-2.61243984e-06, -4.29358087e-06, -4.02818218e-06,
-1.26503486e-06, -1.95604301e-06, -7.16923996e-06,
-1.00774514e-05, -7.96343826e-06, -6.45085100e-06,
-8.67892996e-06, -1.10520184e-05, -1.19476734e-05,
-1.36227106e-05, -1.42168314e-05, -9.33263135e-06,
-2.17225897e-06, -5.84305718e-07, -4.30937959e-06,
-6.51884642e-06, -6.27677964e-06, -7.71297965e-06,
-9.76726558e-06, -8.87239736e-06, -7.87411494e-06,
-1.03720169e-05, -1.18350894e-05, -8.32248564e-06,
-6.30349921e-06, -1.10102930e-05, -1.44285730e-05,
-8.77169330e-06, -1.42971364e-06, -1.66006525e-06,
-3.64126432e-06, 1.34621517e-06, 7.30548819e-06,
4.94102538e-06, -6.32256202e-08, 2.55733626e-06],
[-6.19448104e-06, -9.99704371e-06, -7.23260417e-06,
1.85470716e-06, 7.35089582e-06, 4.48625112e-06,
1.74342364e-07, 8.61768171e-07, 2.85467760e-06,
8.04463815e-07, -1.90077279e-06, 4.12336582e-07,
4.55284917e-06, 3.68234578e-06, -8.26246613e-08,
6.33120370e-07, 4.40919251e-06, 3.19218992e-06,
-1.96174304e-06, -1.27854072e-06, 5.72314568e-06,
7.43527615e-06, -1.18608229e-07, -5.58590222e-06,
-1.30227677e-06, 4.10162056e-06, 1.37442161e-06,
-3.70368705e-06, -1.49762176e-06, 4.84798610e-06,
5.93113723e-06, 1.84913648e-06, -9.33863651e-08,
1.68165051e-06, 2.75695314e-06, 1.82332351e-06,
1.07431963e-06, 3.28511789e-07, -1.42592630e-06,
-1.49532890e-06, 1.90937629e-06, 3.45043473e-06,
-2.66913602e-06, -1.19052160e-05, -1.39852282e-05,
-7.40037824e-06, -5.79087245e-07, 1.26690856e-06,
1.51316395e-06, 3.37646136e-06, 4.57484620e-06,
2.97800164e-06, 1.76294691e-07, -2.71090081e-06],
[ 1.75122149e-07, -3.54418159e-06, -5.82525284e-06,
-3.52358748e-06, 2.87560584e-06, 9.39261153e-06,
1.16124071e-05, 8.92741837e-06, 4.77359016e-06,
2.27184659e-06, 1.69501961e-06, 1.98495137e-06,
1.37074828e-06, -2.91423405e-06, -1.08283101e-05,
-1.62760913e-05, -1.35247354e-05, -5.73673803e-06,
-3.18184434e-07, 2.16616581e-06, 5.93655014e-06,
9.56343549e-06, 8.28739181e-06, 4.72812312e-06,
5.65226485e-06, 9.72273617e-06, 1.02810512e-05,
8.21824496e-06, 9.65677775e-06, 1.28936013e-05,
1.02426897e-05, 1.57072725e-06, -5.02001145e-06,
-4.66185065e-06, 4.68699734e-07, 6.00151265e-06,
8.08490979e-06, 5.68509433e-06, 3.42049288e-06,
5.78870675e-06, 8.72267311e-06, 6.34519751e-06,
3.23552832e-06, 6.88085533e-06, 1.23138300e-05,
1.00947250e-05, 3.93359544e-06, 2.82622418e-06,
3.36861518e-06, -6.04139032e-07, -7.59524455e-07,
1.10667900e-05, 2.19084760e-05, 1.59276145e-05]])Exercise: Compute the standard deviation across all EEG channels - which is called the global field potential (GFP) - and assign it to a new variable called gfp. What is the shape of this array?
Solution
gfp = np.std(eeg, axis = 1)
gfp.shape(70, 54)Exercise: Save the GFP to a file called "gfp.npy". Then load the file again to make sure it was stored.
Solution
np.save('gfp.npy', gfp)
gfp_check = np.load('gfp.npy')
np.shape(gfp_check)(70, 54)Exercise: Average the GFP across all epochs and find the peak value of this average GFP.
Solution
np.mean(gfp, axis=0).max()7.1624821620567494e-06Exercise: Average the GFP across time and epochs (so that the result is a single scalar value). Compute this value separately for the first and second half of epochs - which one is larger?
Solution
gfp[:,:int(gfp.shape[-1]/2)].mean(), gfp[:,int(gfp.shape[-1]/2):].mean()(6.150279958463817e-06, 6.662563282331686e-06)Section 4: Linking Arrays with Filtering
Filtering can also be used to link different arrays. In this section we
are going to load an array that contains the time points for the epochs
from the previous array. We can use that array to define a mask and then
apply that mask to the array of epoch. Numpy also allows us to combine
multiple masks using the and/or operators & and |. Combining two
Boolean arrays with & results in an array that is only True when
both arrays are True. Combining them with | results in an array that
is True when either array is True. In this section we are going to
use filtering to link the EEG data with the array containing the time
points at which the data were recorded (time point 0 represents the
stimulus onset).
Exercise: Load the data stored in the file epoch_times.npy and assign it to a variable called times.
Solution
times = np.load('data/epoch_times.npy')
timesarray([0. , 0.00665984, 0.01331968, 0.01997952, 0.02663936,
0.0332992 , 0.03995904, 0.04661888, 0.05327872, 0.05993856,
0.0665984 , 0.07325824, 0.07991809, 0.08657793, 0.09323777,
0.09989761, 0.10655745, 0.11321729, 0.11987713, 0.12653697,
0.13319681, 0.13985665, 0.14651649, 0.15317633, 0.15983617,
0.16649601, 0.17315585, 0.17981569, 0.18647553, 0.19313537,
0.19979521, 0.20645505, 0.21311489, 0.21977473, 0.22643457,
0.23309442, 0.23975426, 0.2464141 , 0.25307394, 0.25973378,
0.26639362, 0.27305346, 0.2797133 , 0.28637314, 0.29303298,
0.29969282, 0.30635266, 0.3130125 , 0.31967234, 0.32633218,
0.33299202, 0.33965186, 0.3463117 , 0.35297154])Exercise: How many time points are there? What’s the start and stop time?
Solution
np.size(times), times.min(), times.max()(54, 0.0, 0.3529715432464852)Exercise: Get all of the eeg data that was recorded in the first 0.2 seconds after stimulus onset and print the shape of the resulting array.
Solution
mask1 = times < 0.2
eeg[:,:,mask1]array([[[ 2.67680334e-05, 3.53526848e-05, 4.69128998e-05, ...,
1.66172575e-06, -2.26350410e-07, 2.38564375e-06],
[ 2.76873252e-05, 3.94603314e-05, 5.36404585e-05, ...,
3.49214983e-06, 2.40684146e-06, 4.35802517e-06],
[ 2.70946471e-05, 4.35415728e-05, 5.87683795e-05, ...,
8.94707911e-08, -2.23653416e-06, -2.58973621e-06],
...,
[-5.04276237e-06, -7.37953656e-06, -9.72836155e-06, ...,
6.97940708e-06, 7.91734055e-06, 8.32545809e-06],
[-3.39345013e-06, -5.44104380e-06, -8.21716755e-06, ...,
8.94165363e-06, 8.62031167e-06, 7.82095945e-06],
[-7.43880044e-06, -1.07589566e-05, -1.50315333e-05, ...,
2.74133193e-06, 1.58138952e-07, -5.31656240e-07]],
[[-1.30239872e-05, -1.09115245e-05, -4.11188614e-06, ...,
-9.08296289e-06, -1.26353916e-05, -1.55128908e-05],
[-8.18649945e-06, -6.56655153e-06, -2.14800222e-06, ...,
-8.16375986e-06, -8.36451792e-06, -8.29790004e-06],
[-9.67294801e-06, -8.77306030e-06, -2.49823302e-06, ...,
-8.60622631e-06, -8.92065594e-06, -9.95017770e-06],
...,
[ 3.61341830e-06, 1.91372046e-06, -4.44052644e-08, ...,
5.71804838e-06, 2.38057665e-06, 3.37922055e-07],
[ 6.85304463e-06, 4.19712265e-06, 2.50223795e-07, ...,
9.02662030e-06, 5.86477352e-06, 4.27539139e-06],
[ 4.69166056e-06, 2.18060707e-06, 4.71730855e-07, ...,
6.91781312e-07, -4.51021557e-06, -6.89437978e-06]],
[[-1.00595697e-05, 4.88523393e-07, 4.72230958e-06, ...,
5.10708007e-06, 8.79407159e-06, 5.45428661e-06],
[-1.04884125e-05, -3.61615047e-06, -6.38732159e-07, ...,
3.62746018e-06, 3.97880105e-06, 1.91869756e-06],
[-5.76496234e-06, -4.78567457e-06, -6.01386282e-06, ...,
6.71890253e-07, 1.55712275e-06, -4.60146902e-07],
...,
[ 3.13069405e-06, 2.27458242e-06, 1.21726081e-06, ...,
2.62487500e-06, 2.57619958e-06, 3.11705402e-06],
[ 4.21983496e-06, 2.88848067e-06, 1.33651615e-06, ...,
-6.17938998e-06, -5.59310421e-06, -4.52810285e-06],
[-1.70823439e-06, -1.76202564e-06, -1.09865338e-06, ...,
2.81277574e-06, 2.69237102e-06, 4.02374014e-06]],
...,
[[ 2.52372107e-06, -1.59301144e-06, -9.74316541e-06, ...,
2.37988538e-06, 4.72306403e-07, -9.82198578e-08],
[ 4.43244799e-07, -7.03348740e-06, -9.98836997e-06, ...,
7.26973052e-06, 6.97156437e-06, 8.61501534e-06],
[-2.99598368e-06, -4.12367379e-06, -2.01403225e-06, ...,
1.27820250e-05, 1.16616269e-05, 1.11855056e-05],
...,
[-8.26001136e-07, -1.78755004e-06, -1.20455793e-06, ...,
-2.86943823e-06, -5.14900770e-06, -6.61935452e-06],
[-7.18787902e-06, -8.31627066e-06, -7.90381517e-06, ...,
-1.27428478e-06, -3.98141218e-06, -6.09774980e-06],
[ 2.68978771e-06, 9.36397657e-08, -8.64475851e-07, ...,
-7.47735229e-06, -8.59252680e-06, -9.40957359e-06]],
[[-5.77128577e-06, -1.86679554e-06, 7.82888343e-06, ...,
-1.58719718e-06, 1.09900317e-06, 8.15820964e-06],
[-3.97099597e-06, 5.55704440e-06, 1.05251973e-05, ...,
1.85859615e-06, 5.85856991e-06, 5.91396417e-06],
[-4.36232487e-06, 7.54462220e-06, 1.54834975e-05, ...,
1.09317835e-05, 1.49907112e-05, 1.27321353e-05],
...,
[ 1.93881097e-08, -2.75858774e-06, -6.09023080e-06, ...,
-5.71592718e-06, -7.10738934e-06, -9.00270297e-06],
[ 2.22044985e-06, -1.78591609e-07, -3.56763509e-06, ...,
-9.36026334e-06, -9.34653529e-06, -1.00597920e-05],
[-2.95634767e-06, -3.87744092e-06, -4.71843947e-06, ...,
-2.43550634e-06, -4.21116130e-06, -5.93073261e-06]],
[[-1.02202164e-05, -1.27737461e-05, -1.95778938e-05, ...,
-1.59894498e-05, -2.09972464e-05, -2.70652128e-05],
[-1.45904934e-05, -1.80278732e-05, -2.37094046e-05, ...,
-2.31446928e-05, -2.15827234e-05, -2.75287971e-05],
[-1.41404014e-05, -1.62593358e-05, -2.29404232e-05, ...,
-2.56449439e-05, -2.35753609e-05, -2.94921752e-05],
...,
[ 3.53678018e-06, 4.35409251e-06, 6.89197268e-06, ...,
6.93768739e-06, 6.95296763e-06, 7.78063186e-06],
[ 4.00153184e-06, 5.16907380e-06, 7.59184209e-06, ...,
7.89749359e-06, 8.29173306e-06, 9.98176073e-06],
[ 8.23090435e-06, 7.78776610e-06, 9.30472558e-06, ...,
1.07833702e-05, 9.93765283e-06, 9.96667124e-06]]])Exercise: Get all of the eeg data that was recorded in the first 0.05 seconds after stimulus onset and print the shape of the resulting array.
Solution
mask = times < 0.05
np.shape(eeg[:,:,mask])(70, 59, 8)Example: Get all of the eeg data that was recorded between 0.1 and 0.2 seconds after stimulus onset and print the shape of the resulting array.
mask2 = (times>=0.1) & (times<=0.2)
eeg[:,:,mask2]array([[[ 2.80160261e-05, 2.59949301e-05, 2.68294130e-05, ...,
1.66172575e-06, -2.26350410e-07, 2.38564375e-06],
[ 2.76170501e-05, 3.18715226e-05, 3.08368527e-05, ...,
3.49214983e-06, 2.40684146e-06, 4.35802517e-06],
[ 2.79728694e-05, 3.10165105e-05, 3.10463504e-05, ...,
8.94707911e-08, -2.23653416e-06, -2.58973621e-06],
...,
[ 8.01735795e-06, 3.52157674e-06, -2.16066775e-08, ...,
6.97940708e-06, 7.91734055e-06, 8.32545809e-06],
[ 6.59051117e-06, 2.89922198e-06, -2.70402982e-07, ...,
8.94165363e-06, 8.62031167e-06, 7.82095945e-06],
[ 6.06759584e-06, 4.30270959e-06, 2.79291822e-06, ...,
2.74133193e-06, 1.58138952e-07, -5.31656240e-07]],
[[ 1.42250006e-06, 8.12658246e-06, 1.84651700e-06, ...,
-9.08296289e-06, -1.26353916e-05, -1.55128908e-05],
[ 1.76361866e-06, 6.62579841e-06, 4.08818220e-06, ...,
-8.16375986e-06, -8.36451792e-06, -8.29790004e-06],
[-4.75107980e-06, -2.17019184e-08, -1.94150094e-07, ...,
-8.60622631e-06, -8.92065594e-06, -9.95017770e-06],
...,
[ 2.48690096e-06, 4.48275800e-07, -6.37618290e-07, ...,
5.71804838e-06, 2.38057665e-06, 3.37922055e-07],
[ 3.89056501e-06, 2.36968889e-06, 2.25930252e-06, ...,
9.02662030e-06, 5.86477352e-06, 4.27539139e-06],
[ 3.15294471e-07, 2.44139289e-06, 3.40197645e-06, ...,
6.91781312e-07, -4.51021557e-06, -6.89437978e-06]],
[[-1.22818806e-07, 3.78277187e-06, 2.40105654e-06, ...,
5.10708007e-06, 8.79407159e-06, 5.45428661e-06],
[-4.18830914e-07, 1.21315137e-07, -2.42895926e-06, ...,
3.62746018e-06, 3.97880105e-06, 1.91869756e-06],
[-8.64947661e-06, -7.32808605e-06, -4.28594412e-06, ...,
6.71890253e-07, 1.55712275e-06, -4.60146902e-07],
...,
[ 5.41738515e-06, 4.40824450e-06, 4.89716392e-06, ...,
2.62487500e-06, 2.57619958e-06, 3.11705402e-06],
[ 4.13712249e-06, 9.56859690e-07, -2.28250345e-07, ...,
-6.17938998e-06, -5.59310421e-06, -4.52810285e-06],
[ 4.14470498e-06, 3.37396084e-06, 4.82300928e-06, ...,
2.81277574e-06, 2.69237102e-06, 4.02374014e-06]],
...,
[[-3.51635486e-06, 6.60966928e-06, 6.27044375e-06, ...,
2.37988538e-06, 4.72306403e-07, -9.82198578e-08],
[-4.09224604e-07, 2.40189960e-06, 5.34339134e-06, ...,
7.26973052e-06, 6.97156437e-06, 8.61501534e-06],
[-1.98180644e-06, 4.73560544e-06, 1.18553915e-05, ...,
1.27820250e-05, 1.16616269e-05, 1.11855056e-05],
...,
[ 2.81648263e-06, 5.51475913e-07, -6.22382252e-07, ...,
-2.86943823e-06, -5.14900770e-06, -6.61935452e-06],
[-2.05658543e-06, -3.85839086e-06, -4.47343914e-06, ...,
-1.27428478e-06, -3.98141218e-06, -6.09774980e-06],
[ 3.90547437e-06, 2.24442372e-06, 8.88315798e-07, ...,
-7.47735229e-06, -8.59252680e-06, -9.40957359e-06]],
[[-7.04463615e-06, 1.88668122e-06, 1.28505238e-05, ...,
-1.58719718e-06, 1.09900317e-06, 8.15820964e-06],
[-7.90312113e-07, 1.13385801e-06, 3.90060303e-06, ...,
1.85859615e-06, 5.85856991e-06, 5.91396417e-06],
[-1.24507346e-07, 4.37480688e-06, 1.36471494e-05, ...,
1.09317835e-05, 1.49907112e-05, 1.27321353e-05],
...,
[-7.83071189e-07, -2.65109634e-06, -4.11506269e-06, ...,
-5.71592718e-06, -7.10738934e-06, -9.00270297e-06],
[-6.22506667e-06, -7.35184313e-06, -8.53292443e-06, ...,
-9.36026334e-06, -9.34653529e-06, -1.00597920e-05],
[ 2.40773638e-06, -2.32628023e-07, -1.32363459e-06, ...,
-2.43550634e-06, -4.21116130e-06, -5.93073261e-06]],
[[-3.35537395e-05, -1.62927658e-05, -2.32019980e-06, ...,
-1.59894498e-05, -2.09972464e-05, -2.70652128e-05],
[-2.13408417e-05, -1.80931705e-05, -1.78119464e-05, ...,
-2.31446928e-05, -2.15827234e-05, -2.75287971e-05],
[-2.13674944e-05, -2.51372794e-05, -2.68841052e-05, ...,
-2.56449439e-05, -2.35753609e-05, -2.94921752e-05],
...,
[ 8.98991180e-06, 7.19123493e-06, 4.93157676e-06, ...,
6.93768739e-06, 6.95296763e-06, 7.78063186e-06],
[ 1.01566670e-05, 9.38192983e-06, 6.77708032e-06, ...,
7.89749359e-06, 8.29173306e-06, 9.98176073e-06],
[ 8.30284546e-06, 5.93039515e-06, 2.87558904e-06, ...,
1.07833702e-05, 9.93765283e-06, 9.96667124e-06]]])Exercise: Get all of the eeg data that was recorded before 0.1 and after 0.2 seconds and print the shape of the resulting array.
Solution
mask = (times<=0.1) & (times>=0.2)
np.shape(eeg[:,:,mask])(70, 59, 0)