/
topography.py
100 lines (78 loc) · 3.24 KB
/
topography.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import mlab
def topography(value, x, y, cmap=plt.cm.jet, nx=512, ny=512, plotsensors=True, vmin=None, vmax=None, colorbar=True):
"""Simple plot of a topography given one value per channel and its
position (x,y) through a layout.
Most of the code is taken from:
http://stackoverflow.com/questions/3864899/resampling-irregularly-spaced-data-to-a-regular-grid-in-python
with some minor additions.
cmap : colormap
nx, ny : size of regular grid.
plotsensors : whether you want to plot sensor positions
vmin : minimum value for the colormap
vmax : max value for the colormap
colorbar : whether to plot the colorbar or not
"""
z = value
xmin = x.min()
ymin = y.min()
xmax = x.max()
ymax = y.max()
# Generate a regular grid to interpolate the data.
xi = np.linspace(xmin, xmax, nx)
yi = np.linspace(ymin, ymax, ny)
xi, yi = np.meshgrid(xi, yi)
# Normalize coordinate system
def normalize_x(data):
data = data.astype(np.float)
return (data - xmin) / (xmax - xmin)
def normalize_y(data):
data = data.astype(np.float)
return (data - ymin) / (ymax - ymin)
x_new, xi_new = normalize_x(x), normalize_x(xi)
y_new, yi_new = normalize_y(y), normalize_y(yi)
# Interpolate using delaunay triangulation
zi = mlab.griddata(x_new, y_new, z, xi_new, yi_new)
# Plot the results
plt.pcolormesh(xi,yi,zi, cmap=cmap, vmin=vmin, vmax=vmax)
if colorbar: plt.colorbar(shrink=0.75)
if plotsensors:
plt.scatter(x,y,c='w')
# plt.axis([xmin*0.95, xmax*1.05, ymin*0.95, ymax*1.05])
plt.axis('equal')
plt.axis('off')
def hypertopography(values, x, y, cmap=plt.cm.jet, nx=64, ny=64, plotsensors=True, vmin=None, vmax=None, colorbar=True, zoom_factor=0.08, smooth_autovalues=False):
"""Plot a topography of topographies, useful to represent
relational information between channels, e.g. connectivity,
coherence, etc.
See topography for an explanation of most parameters.
zoom_factor : this is the relative size of each mini topography
smooth_autovalues : whether to smooth or not diagonal values.
"""
if smooth_autovalues:
values = values.copy()
mean = values.mean(0)
values[np.diag_indices(values.shape[0])] = mean
# set a common range for colors:
if vmin is None: vmin = values.min()
if vmax is None: vmax = values.max()
for i in range(values.shape[0]):
topography(values[i], x*zoom_factor + x[i], y*zoom_factor + y[i], nx=nx, ny=nx, plotsensors=False, vmin=vmin, vmax=vmax, colorbar=False)
if plotsensors:
plt.plot(x[i]*zoom_factor + x[i], y[i]*zoom_factor + y[i], 'k.', markersize=8)
if colorbar: plt.colorbar()
if __name__ == '__main__':
from mne.layouts import read_layout
layout = read_layout('Vectorview-mag.lout')
x = layout.pos[:,0]
y = layout.pos[:,1]
# generate some values:
# value = np.sin((layout.pos[:,:2]**2).sum(1)*10)
value = np.random.rand(x.size)
plt.figure()
topography(value, x, y)
plt.figure()
values = np.random.rand(x.size, x.size)
hypertopography(values, x, y)
plt.show()