pytour 3.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tour/vis.py ADDED
@@ -0,0 +1,201 @@
1
+ import mne
2
+ import numpy as np
3
+ from matplotlib import pyplot as plt
4
+ from matplotlib import gridspec
5
+
6
+ def plot_biosemi128(r, title, chan_idx, folder = None, units = 'r', res = 1024, **kwargs):
7
+ r = np.array(r)
8
+ # print('TRFResult plot - r: ', r)
9
+ assert len(r) == 128
10
+ kwargs['sensors'] = True if 'sensors' not in kwargs else kwargs['sensors']
11
+ fig = plot_data(
12
+ r, title = title, chan_idx = chan_idx, res = res, units = units, **kwargs
13
+ )
14
+ if 'ax' not in kwargs:
15
+ fig.suptitle(title)
16
+ if folder is not None:
17
+ new_title = title.replace(" ","_").replace("\n","_")
18
+ fig.savefig(f'{folder}/{new_title}.png', dpi = 300)
19
+ plt.close(fig)
20
+
21
+ def plot_data(
22
+ data,
23
+ fs = 64,
24
+ times = None,
25
+ title = '',
26
+ chan_idx = None,
27
+ time_intvl = None,
28
+ units = 'a.u.',
29
+ montage = None,
30
+ mode = 'joint',
31
+ tmin = 0,
32
+
33
+ **kwargs
34
+ ):
35
+ data = np.array(data)
36
+ ifAx = False
37
+ if montage is None:
38
+ montage = mne.channels.make_standard_montage('biosemi128')
39
+ chnames_map = dict(
40
+ C17 = 'Fpz',
41
+ # C21 = 'Fz',
42
+ # A1 = 'Cz',
43
+ D23 = 'T7',
44
+ B26 = 'T8',
45
+ # A19 = 'Pz',
46
+ A23 = 'Oz'
47
+ )
48
+
49
+ for k,v in chnames_map.items():
50
+ montage.ch_names[montage.ch_names.index(k)] = v
51
+
52
+ chNames = montage.ch_names
53
+
54
+ # print(chNames)
55
+ try:
56
+ info = mne.create_info(chNames, fs,ch_types = 'eeg', montage = montage)
57
+ except:
58
+ info = mne.create_info(chNames, fs,ch_types = 'eeg')
59
+ info.set_montage(montage = montage)
60
+
61
+ kwargs['sensors'] = False if 'sensors' not in kwargs else kwargs['sensors']
62
+ kwargs['res'] = 256 if 'res' not in kwargs else kwargs['res']
63
+ kwargs['outlines'] ='head' if 'outlines' not in kwargs else kwargs['outlines']
64
+ show_names = kwargs.get('show_names', False)
65
+ if 'show_names' in kwargs:
66
+ del kwargs['show_names']
67
+ if show_names:
68
+ names = montage.ch_names
69
+ else:
70
+ names = None
71
+ ts_args = kwargs.get('ts_args', None)
72
+
73
+ if time_intvl is not None:
74
+ time_intvl = np.array(time_intvl)
75
+ if time_intvl.ndim == 1:
76
+ time_intvl = time_intvl[None,...]
77
+ # print(time_intvl)
78
+ #calculate the intersted time point and average time window
79
+ average_window = time_intvl[:,1] - time_intvl[:,0]
80
+ timepoint = time_intvl.mean(1)
81
+ else:
82
+ average_window = None
83
+
84
+ chanMask = None
85
+ if chan_idx is not None:
86
+ chanMask = np.zeros(data.shape,dtype = bool)
87
+ for i in chan_idx:
88
+ chanMask[i] = True
89
+
90
+ kwargs['cmap'] = plt.get_cmap("bwr") if 'cmap' not in kwargs else kwargs['cmap']
91
+ kwargs['show'] = False if 'show' not in kwargs else kwargs['show']
92
+
93
+ maskParam = dict(
94
+ marker='o',
95
+ markerfacecolor='w',
96
+ markeredgecolor='k',
97
+ linewidth=0,
98
+ markersize=8
99
+ )
100
+
101
+ maskParam2_default = dict(
102
+ marker='o',
103
+ markerfacecolor='w',
104
+ markeredgecolor='k',
105
+ linewidth=0,
106
+ markersize=4
107
+ )
108
+ maskParam2 = kwargs.get('maskParam', maskParam2_default)
109
+ if 'maskParam' in kwargs:
110
+ del kwargs['maskParam']
111
+
112
+ if data.ndim == 2:
113
+
114
+ default_ts_args={
115
+ "units": units,
116
+ "scalings": dict(eeg=1),
117
+ "highlight": time_intvl,
118
+ }
119
+
120
+ if ts_args is not None:
121
+ default_ts_args.update(ts_args)
122
+
123
+
124
+ mneW = mne.EvokedArray(data,info, tmin = tmin)
125
+ if montage is not None:
126
+ mneW.set_montage(montage)
127
+
128
+ if times is None:
129
+ if time_intvl is None:
130
+ if mode == 'joint':
131
+ times = 'peaks'
132
+ else:
133
+ times = 'auto'
134
+ else:
135
+ times = timepoint
136
+
137
+ if mode == 'joint':
138
+ print(default_ts_args)
139
+ fig = mneW.plot_joint(
140
+ times = times,
141
+ topomap_args=dict(
142
+ scalings = 1,
143
+ mask = chanMask,
144
+ mask_params= maskParam,
145
+ average = average_window
146
+ ),
147
+ ts_args = default_ts_args,
148
+ show = kwargs.get('show', True),
149
+ title = title
150
+ )
151
+ else:
152
+ fig = mneW.plot_topomap(
153
+ times = times,
154
+ time_unit='s',
155
+ scalings = 1,
156
+ title = title,
157
+ units = units,
158
+ cbar_fmt='%3.3f',
159
+ mask = chanMask,
160
+ mask_params= maskParam2,
161
+ colorbar=False,
162
+ # names = None,
163
+ **kwargs
164
+ )
165
+
166
+
167
+ elif data.ndim == 1:
168
+ if 'ax' in kwargs:
169
+ ax1 = kwargs['ax']
170
+ del kwargs['ax']
171
+ ifAx = True
172
+ else:
173
+ fig = plt.figure(tight_layout=True)
174
+ gridspec_kw={'width_ratios': [19, 1]}
175
+ gs = gridspec.GridSpec(4, 2,**gridspec_kw)
176
+ ax1 = fig.add_subplot(gs[:, 0])
177
+ ax2 = fig.add_subplot(gs[1:3, 1])
178
+ # print('contours')
179
+ im,cm = mne.viz.plot_topomap(
180
+ data.squeeze(),
181
+ info,
182
+ axes = ax1,
183
+ mask = chanMask,
184
+ names = names,
185
+ mask_params= maskParam2,
186
+ sphere = 'eeglab',
187
+ contours = 2,
188
+ **kwargs
189
+ )
190
+ # cbar_ax = fig.add_axes([ax_x_start, ax_y_start, ax_x_width, ax_y_height])
191
+ if not ifAx:
192
+ clb = fig.colorbar(im, cax=ax2)
193
+ clb.ax.set_title(units,fontsize=10) # title on top of colorbar
194
+ fig.suptitle(title)
195
+ else:
196
+ raise NotImplementedError
197
+
198
+ if not ifAx:
199
+ return fig
200
+ else:
201
+ return im