Skip to content

Commit 7db9a44

Browse files
committed
Updated day 1 stuff
1 parent 79b22bf commit 7db9a44

File tree

4 files changed

+329
-107
lines changed

4 files changed

+329
-107
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/data/*
1+
/data/*
2+
*.pyc

.ipynb_checkpoints/Main-checkpoint.ipynb

Lines changed: 205 additions & 51 deletions
Large diffs are not rendered by default.

Main.ipynb

Lines changed: 116 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,26 @@
1010
{
1111
"cell_type": "code",
1212
"execution_count": null,
13-
"metadata": {},
13+
"metadata": {
14+
"collapsed": false
15+
},
1416
"outputs": [],
1517
"source": [
16-
"# The following import commands are modules that have helpful functions. Don't worry about them.\n",
18+
"# The following import commands are modules that have helpful functions. \n",
1719
"import numpy as np\n",
1820
"import matplotlib.pyplot as plt\n",
1921
"import time\n",
2022
"import scipy\n",
21-
"import os"
23+
"import os\n",
24+
"print('Modules loaded!')"
2225
]
2326
},
2427
{
2528
"cell_type": "code",
2629
"execution_count": null,
27-
"metadata": {},
30+
"metadata": {
31+
"collapsed": false
32+
},
2833
"outputs": [],
2934
"source": [
3035
"# Load in the data using the program provided by OpenEphys. \n",
@@ -49,27 +54,64 @@
4954
{
5055
"cell_type": "code",
5156
"execution_count": null,
52-
"metadata": {},
57+
"metadata": {
58+
"collapsed": false
59+
},
5360
"outputs": [],
5461
"source": [
5562
"# Lets check out what we got\n",
5663
"print(\"The Sampling rate is :\", data['header']['sampleRate'])\n",
5764
"\n",
58-
"# There is a problem. The sampling rate is of type string even though we need it to be a float...\n",
59-
"print(\"Sample rate is type :\", type(data['header']['sampleRate']))\n",
60-
"\n",
65+
"# There is a problem. The sampling rate is of type string(unicode) even though we need it to be a float...\n",
66+
"print(\"Sample rate is type :\", type(data['header']['sampleRate']))"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {
73+
"collapsed": false
74+
},
75+
"outputs": [],
76+
"source": [
6177
"# So lets change it!\n",
6278
"sampleRate = float(data['header']['sampleRate'])\n",
6379
"print(\"The Sampling rate is still :\", sampleRate)\n",
6480
"print(\"But the type is now :\", type(sampleRate))\n",
65-
"print('Notice the precision changed. It\\'s now 30000.0!')\n",
66-
"print('') #Blank line\n",
67-
"\n",
81+
"print('Notice the precision changed. It\\'s now 30000.0!')"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"metadata": {
88+
"collapsed": false
89+
},
90+
"outputs": [],
91+
"source": [
92+
"# Compare this to an int?\n",
93+
"sampleRate = int(sampleRate)\n",
94+
"print('Still the same sampling rate : ', sampleRate)\n",
95+
"print('But now the type is : ', type(sampleRate))\n",
96+
"print('And once again the precision has changed!')"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"execution_count": null,
102+
"metadata": {
103+
"collapsed": false
104+
},
105+
"outputs": [],
106+
"source": [
68107
"# The data is a little longer so we won't print it out. We can look at how many values are stored though.\n",
69108
"print('Number of data points :', len(data['data']))\n",
70109
"# The timestamps have a different number of values because there is only 1 timestamp per 1024 data samples.\n",
71110
"# This is taken care of for you in the next step so don't worry about it.\n",
72-
"print('Number of timestamps :', len(data['timestamps']))"
111+
"print('Number of timestamps :', len(data['timestamps']))\n",
112+
"\n",
113+
"dataPerTs = len(data['data'])/len(data['timestamps'])\n",
114+
"print('Number of data samples per timestamp', dataPerTs)"
73115
]
74116
},
75117
{
@@ -92,7 +134,9 @@
92134
{
93135
"cell_type": "code",
94136
"execution_count": null,
95-
"metadata": {},
137+
"metadata": {
138+
"collapsed": true
139+
},
96140
"outputs": [],
97141
"source": [
98142
"class Con:\n",
@@ -124,23 +168,30 @@
124168
{
125169
"cell_type": "code",
126170
"execution_count": null,
127-
"metadata": {},
171+
"metadata": {
172+
"collapsed": false
173+
},
128174
"outputs": [],
129175
"source": [
130176
"plt.plot(con.ts,con.data, 'r') # This also takes a bit as there is a lot of data.\n",
131-
"# Now you have a plot of every voltage for every sample in the recording."
177+
"# Now you have a plot of every voltage for every sample in the recording.\n",
178+
"plt.show()"
132179
]
133180
},
134181
{
135182
"cell_type": "code",
136183
"execution_count": null,
137-
"metadata": {},
184+
"metadata": {
185+
"collapsed": false
186+
},
138187
"outputs": [],
139188
"source": [
140-
"# Now lets get only the first 2 seconds of data, we'll use the sampling rate we saved before\n",
189+
"# Now lets get only the first 10 seconds of data, we'll use the sampling rate we saved before\n",
141190
"timeElapsed = int(10 * con.fs) # Feel free to change the 1 to as many seconds as you (needs to be an integer for later)\n",
142191
"timeStart = int(0 * con.fs) # Where do you want to start (0 is the start, can change to be as many seconds after as you want)\n",
143-
"plt.plot(con.ts[timeStart:timeElapsed], con.data[timeStart:timeElapsed], 'r') # [startIndex:endIndex]"
192+
"timeEnd = timeElapsed + timeStart # We need what timestamp the end is. It is the start index + how many have elapsed over 10 sec\n",
193+
"plt.plot(con.ts[timeStart:timeEnd], con.data[timeStart:timeEnd], 'r') # [startIndex:endIndex]\n",
194+
"plt.show()"
144195
]
145196
},
146197
{
@@ -157,16 +208,19 @@
157208
{
158209
"cell_type": "code",
159210
"execution_count": null,
160-
"metadata": {},
211+
"metadata": {
212+
"collapsed": false
213+
},
161214
"outputs": [],
162215
"source": [
163-
"timeElapsed = int(2 * con.fs) \n",
164-
"timeStart = int(0 * con.fs)\n",
165-
"\n",
216+
"timeElapsed = int(10 * con.fs) # Feel free to change the 1 to as many seconds as you (needs to be an integer for later)\n",
217+
"timeStart = int(0 * con.fs) # Where do you want to start (0 is the start, can change to be as many seconds after as you want)\n",
218+
"timeEnd = timeElapsed + timeStart # We need what timestamp the end is. It is the start index + how many have elapsed over 10 sec\n",
166219
"# Put code here!\n",
167220
"# use matplotlib to create a vertical line (hint: use google!)\n",
168221
"\n",
169-
"plt.plot(con.ts[timeStart:timeElapsed], con.data[timeStart:timeElapsed], 'r') "
222+
"plt.plot(con.ts[timeStart:timeEnd], con.data[timeStart:timeEnd], 'r') \n",
223+
"plt.show()"
170224
]
171225
},
172226
{
@@ -181,16 +235,17 @@
181235
{
182236
"cell_type": "code",
183237
"execution_count": null,
184-
"metadata": {},
238+
"metadata": {
239+
"collapsed": true
240+
},
185241
"outputs": [],
186242
"source": [
187-
"timeElapsed = int(2 * con.fs) \n",
188-
"timeStart = int(0 * con.fs)\n",
189-
"\n",
190243
"# Put code here!\n",
191244
"\n",
192-
"plt.plot(con.ts[timeStart:timeElapsed], con.data[timeStart:timeElapsed], 'r') \n",
193-
"# use matplotlib to create a horizontal line (hint: use google!)"
245+
"plt.plot(con.ts[timeStart:timeEnd], con.data[timeStart:timeEnd], 'r') \n",
246+
"# use matplotlib to create a horizontal line (hint: use google!)\n",
247+
"\n",
248+
"plt.show()"
194249
]
195250
},
196251
{
@@ -199,43 +254,64 @@
199254
"source": [
200255
"## Third challenge\n",
201256
"\n",
202-
"Use the following imported module (butter) to create a bandpass filter to extract only the theta band (8-12hz)! I recommend writing a few functions to help you this with as well.\n",
257+
"Use the butter function from scipys signal library to create a bandpass filter to extract only the theta band (4-8hz)! If you want, writing a few functions may help you and be good practice. \n",
203258
"\n",
204-
"Use google to understand how to use butter. Also examples are very helpful!"
259+
"Use google to understand how to use butter. Examples are very helpful!"
205260
]
206261
},
207262
{
208263
"cell_type": "code",
209264
"execution_count": null,
210-
"metadata": {},
265+
"metadata": {
266+
"collapsed": true
267+
},
268+
"outputs": [],
269+
"source": [
270+
"# If you want to use functions write them here."
271+
]
272+
},
273+
{
274+
"cell_type": "code",
275+
"execution_count": null,
276+
"metadata": {
277+
"collapsed": false
278+
},
211279
"outputs": [],
212280
"source": [
213-
"from scipy.signal import butter, lfilter, sosfilt # You will also need to use either lfilter or sosfilt. \n",
214-
" # There are multiple ways of doing this\n",
281+
"from scipy import signal \n",
282+
"# Hints!\n",
283+
"# butter function can be accessed by signal.butter\n",
284+
"# You will also need to use lfilter function which is included in the signal library. \n",
285+
"# Use a filter order of 2\n",
286+
"# You will also need to normalize your band that you want to extract\n",
287+
"# by the nyquist frequency\n",
288+
"\n",
215289
"\n",
216290
"# Put code here\n",
217291
"\n",
218-
"plt.plot(con.ts[timeStart:timeElapsed], filteredData, 'r')"
292+
"plt.plot(con.ts[timeStart:timeEnd], filteredData[timeStart:timeEnd], 'r')\n",
293+
"plt.show()"
219294
]
220295
}
221296
],
222297
"metadata": {
298+
"anaconda-cloud": {},
223299
"kernelspec": {
224-
"display_name": "Python 3",
300+
"display_name": "Python [conda root]",
225301
"language": "python",
226-
"name": "python3"
302+
"name": "conda-root-py"
227303
},
228304
"language_info": {
229305
"codemirror_mode": {
230306
"name": "ipython",
231-
"version": 3
307+
"version": 2
232308
},
233309
"file_extension": ".py",
234310
"mimetype": "text/x-python",
235311
"name": "python",
236312
"nbconvert_exporter": "python",
237-
"pygments_lexer": "ipython3",
238-
"version": "3.6.8"
313+
"pygments_lexer": "ipython2",
314+
"version": "2.7.12"
239315
}
240316
},
241317
"nbformat": 4,

butter.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
from scipy.signal import butter, lfilter
1+
from scipy import signal
22

33
'''
4-
Option 1
5-
64
How Mark S did this.
5+
From "https://scipy-cookbook.readthedocs.io/items/ButterworthBandpass.html"
76
'''
8-
def butter_bandpass(lowcut, highcut, fs, order=5):
7+
def butter_bandpass(lowcut, highcut, fs, order=2):
98
nyq = 0.5 * fs
109
low = lowcut / nyq
1110
high = highcut / nyq
12-
b, a = butter(order, [low, high], btype='band')
11+
b, a = signal.butter(order, [low, high], btype='band')
1312
return b, a
1413

1514

16-
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
15+
def butter_bandpass_filter(data, lowcut, highcut, fs, order=2):
1716
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
18-
filteredData = lfilter(b, a, data)
17+
filteredData = signal.lfilter(b, a, data)
1918
return filteredData
2019

21-
22-
'''
23-
Option 2
24-
25-
Example from scipy.
26-
'''
27-
sos = signal.butter(10, 15, 'hp', fs=1000, output='sos')
28-
filtered = signal.sosfilt(sos, sig)

0 commit comments

Comments
 (0)