Skip to content

Commit e6854cc

Browse files
committed
NF: either to norm sens topoplots globally + make downsampling optional
1 parent 732b68f commit e6854cc

File tree

1 file changed

+37
-18
lines changed

1 file changed

+37
-18
lines changed

eeg_fruend.py

+37-18
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def labels2binlabels(ds, mode):
5959
ds.labels[:]=N.array([i in filt for i in ds.labels], dtype='int')
6060

6161

62-
def loadData(subj):
62+
def loadData(subj, sr=None):
6363
"""Load data for one subject and return dataset.
6464
6565
:Parameter:
@@ -89,7 +89,8 @@ def loadData(subj):
8989

9090
#snippet_start resample
9191
# inplace resampling
92-
d.resample(sr=target_samplingrate)
92+
if sr is not None:
93+
d.resample(sr=sr)
9394
verbose(2, 'Downsampled data to %.1f Hz' % d.samplingrate)
9495
#snippet_end resample
9596

@@ -228,7 +229,7 @@ def topoFigure(ds, senses):
228229

229230
ax = fig.add_subplot(1, nsens+1, nsens+1, frame_on=False)
230231
cb = P.colorbar(shrink=0.95, fraction=0.05, drawedges=False,
231-
ticks=[0, 0.1, 0.2, 0.3, 0.4])
232+
ticks=[0, 0.2, 0.4])
232233
ax.axison = False
233234
# Expand things a bit
234235
fig.subplots_adjust(left=0.06, right=1.05, bottom=0.01, wspace=-0.2)
@@ -237,7 +238,8 @@ def topoFigure(ds, senses):
237238
#snippet_end figures
238239

239240

240-
def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1):
241+
def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1,
242+
globaly_normed=False):
241243
"""Plot topographies of given sensitivities at specified timepoints
242244
243245
:Parameters:
@@ -246,6 +248,9 @@ def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1):
246248
to plot sensitivities
247249
dt : float
248250
Duration (in seconds) to take for averaging the sensitivity
251+
globaly_normed : bool
252+
Either to norm sensitivities through all time points or just
253+
for a given time point separately
249254
250255
XXX: This function shares a lot of code with topoFigure, refactor
251256
"""
@@ -271,28 +276,42 @@ def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1):
271276
# we can do that only after we avg across splits
272277
avgbackproj = backproj.mean(axis=0)
273278

279+
if globaly_normed:
280+
# strip EOG scores (which are zero anyway,
281+
# as they had been stripped of before cross-validation)
282+
avgbackproj = avgbackproj[:-3]
283+
284+
# and normalize so that all scores squared sum up to 1
285+
avgbackproj = L2Normed(avgbackproj)
286+
287+
clim = 0.05
288+
274289
if timepoint == 'TotalAbs':
275290
# compute per channel scores and average across folds
276291
# (yields (nchannels, )
277-
scores = N.sum(Absolute(avgbackproj), axis=1)
292+
scores = N.mean(Absolute(avgbackproj), axis=1)
278293
elif timepoint == 'Total':
279294
# compute per channel scores and average across folds
280295
# (yields (nchannels, )
281-
scores = N.sum(avgbackproj, axis=1)
296+
scores = N.mean(avgbackproj, axis=1)
282297
elif N.isreal(timepoint):
283298
timesample = N.round((timepoint - ds.t0) / ds.dt)
284299
dsample = dt / ds.dt
285-
scores = N.sum(avgbackproj[:, timesample-dsample:timesample+dsample],
286-
axis=1)
300+
scores = N.mean(avgbackproj[:, timesample-dsample:timesample+dsample],
301+
axis=1)
287302
else:
288303
raise ValueError, "Don't know how to treat timepoint '%s'" % timepoint
289304

290-
# strip EOG scores (which are zero anyway,
291-
# as they had been stripped of before cross-validation)
292-
scores = scores[:-3]
293305

294-
# and normalize so that all scores squared sum up to 1
295-
scores = L2Normed(scores)
306+
if not globaly_normed:
307+
# strip EOG scores (which are zero anyway,
308+
# as they had been stripped of before cross-validation)
309+
scores = scores[:-3]
310+
311+
# and normalize so that all scores squared sum up to 1
312+
scores = L2Normed(scores)
313+
314+
clim = 0.4
296315

297316
# plot all EEG sensor scores
298317
plotHeadTopography(
@@ -301,7 +320,7 @@ def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1):
301320
plotsensors=True, resolution=50,
302321
interpolation='nearest')
303322
# ensure uniform scaling
304-
P.clim(vmin=-0.4, vmax=0.4)
323+
P.clim(vmin=-clim, vmax=clim)
305324

306325
if it == 0:
307326
# Mention sensitivity in the 0th column
@@ -328,8 +347,8 @@ def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1):
328347

329348
ax = fig.add_subplot(1, nsens+1, nsens+1, frame_on=False)
330349
cb = P.colorbar(shrink=0.95, fraction=0.05, drawedges=False,
331-
pad=0.9,
332-
ticks=[-0.4, -0.3, -0.2, -0.1, 0, 0.1, 0.2, 0.3, 0.4])
350+
pad=0.9, ticks=[-clim, 0, clim])
351+
333352

334353
ax.axison = False
335354
# Expand things a bit
@@ -339,7 +358,7 @@ def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1):
339358

340359
if __name__ == '__main__':
341360
# load dataset for some subject
342-
ds=loadData(subj)
361+
ds=loadData(subj, sr=target_samplingrate)
343362

344363
# artificially group into chunks
345364
nchunks = 6
@@ -398,7 +417,7 @@ def topoFigures(ds, senses, timepoints=['all', 'allabs'], dt=1):
398417
s *= -1.0
399418

400419
# (re)get pristine dataset for plotting of ERPs
401-
ds_pristine=loadData(subj)
420+
ds_pristine=loadData(subj, sr=target_samplingrate)
402421

403422
P.ioff()
404423

0 commit comments

Comments
 (0)