def plot_good_coils(raw, t_step=1., t_window=0.2, dist_limit=0.005, show=True, verbose=None): """Plot the good coil count as a function of time.""" import matplotlib.pyplot as plt if isinstance(raw, dict): # fit_data calculated and stored to disk t = raw['fit_t'] counts = raw['counts'] n_coils = raw['n_coils'] else: t, counts, n_coils = compute_good_coils(raw, t_step, t_window, dist_limit) del t_step, t_window, dist_limit fig, ax = plt.subplots(figsize=(8, 2)) ax.step(t, counts, zorder=4, color='k', clip_on=False) ax.set(xlim=t[[0, -1]], ylim=[0, n_coils], xlabel='Time (sec)', ylabel='Good coils') ax.set(yticks=np.arange(n_coils + 1)) for comp, n, color in ((np.greater_equal, 5, '#2ca02c'), (np.equal, 4, '#98df8a'), (np.equal, 3, (1, 1, 0)), (np.less_equal, 2, (1, 0, 0))): mask = comp(counts, n) mask[:-1] |= comp(counts[1:], n) ax.fill_between(t, 0, n_coils, where=mask, color=color, edgecolor='none', linewidth=0, zorder=1) ax.grid(True) fig.tight_layout() plt_show(show) return fig
def tryThis(): fpath = (FILE_PATH % "test") + "blink.csv" raw = fileUtil.getDto(fpath) mneRaw = mneUtil.createMNEObject(raw.getEEGData(), raw.getEEGHeader(), "", raw.getSamplingRate()) mneUtil.filterData(mneRaw, 0.5, 30) mneRaw.resample(sFreq, npad='auto', n_jobs=2, verbose=True) ica = mneUtil.ICA(mneRaw) ica.plot_components(show=False) ica.plot_sources(mneRaw, show=False) mneUtil.plotRaw(mneRaw, show=False) plt_show() fileUtil.save(mneRaw, fpath + ".fif") fileUtil.saveICA(ica, fpath)
def _plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True, res=64, axes=None, names=None, show_names=False, mask=None, mask_params=None, outlines='head', contours=6, image_interp='bilinear', show=True, head_pos=None, onselect=None, extrapolate='box', border=0): import matplotlib.pyplot as plt from matplotlib.widgets import RectangleSelector data = np.asarray(data) if isinstance(pos, Info): # infer pos from Info object picks = _pick_data_channels(pos) # pick only data channels pos = pick_info(pos, picks) # check if there is only 1 channel type, and n_chans matches the data ch_type = {channel_type(pos, idx) for idx, _ in enumerate(pos["chs"])} info_help = ("Pick Info with e.g. mne.pick_info and " "mne.io.pick.channel_indices_by_type.") if len(ch_type) > 1: raise ValueError("Multiple channel types in Info structure. " + info_help) elif len(pos["chs"]) != data.shape[0]: raise ValueError("Number of channels in the Info object and " "the data array does not match. " + info_help) else: ch_type = ch_type.pop() if any(type_ in ch_type for type_ in ('planar', 'grad')): # deal with grad pairs from mne.channels.layout import (_merge_grad_data, find_layout, _pair_grad_sensors) picks, pos = _pair_grad_sensors(pos, find_layout(pos)) data = _merge_grad_data(data[picks]).reshape(-1) else: picks = list(range(data.shape[0])) pos = _find_topomap_coords(pos, picks=picks) if data.ndim > 1: raise ValueError("Data needs to be array of shape (n_sensors,); got " "shape %s." % str(data.shape)) # Give a helpful error message for common mistakes regarding the position # matrix. pos_help = ("Electrode positions should be specified as a 2D array with " "shape (n_channels, 2). Each row in this matrix contains the " "(x, y) position of an electrode.") if pos.ndim != 2: error = ("{ndim}D array supplied as electrode positions, where a 2D " "array was expected").format(ndim=pos.ndim) raise ValueError(error + " " + pos_help) elif pos.shape[1] == 3: error = ("The supplied electrode positions matrix contains 3 columns. " "Are you trying to specify XYZ coordinates? Perhaps the " "mne.channels.create_eeg_layout function is useful for you.") raise ValueError(error + " " + pos_help) # No error is raised in case of pos.shape[1] == 4. In this case, it is # assumed the position matrix contains both (x, y) and (width, height) # values, such as Layout.pos. elif pos.shape[1] == 1 or pos.shape[1] > 4: raise ValueError(pos_help) if len(data) != len(pos): raise ValueError("Data and pos need to be of same length. Got data of " "length %s, pos of length %s" % (len(data), len(pos))) norm = min(data) >= 0 vmin, vmax = _setup_vmin_vmax(data, vmin, vmax, norm) if cmap is None: cmap = 'Reds' if norm else 'RdBu_r' pos, outlines = _check_outlines(pos, outlines, head_pos) assert isinstance(outlines, dict) ax = axes if axes else plt.gca() _prepare_topomap(pos, ax) _use_default_outlines = any(k.startswith('head') for k in outlines) if _use_default_outlines: # prepare masking _autoshrink(outlines, pos, res) mask_params = _handle_default('mask_params', mask_params) # find mask limits xlim = np.inf, -np.inf, ylim = np.inf, -np.inf, mask_ = np.c_[outlines['mask_pos']] xmin, xmax = (np.min(np.r_[xlim[0], mask_[:, 0]]), np.max(np.r_[xlim[1], mask_[:, 0]])) ymin, ymax = (np.min(np.r_[ylim[0], mask_[:, 1]]), np.max(np.r_[ylim[1], mask_[:, 1]])) # interpolate the data, we multiply clip radius by 1.06 so that pixelated # edges of the interpolated image would appear under the mask head_radius = (None if extrapolate == 'local' else outlines['clip_radius'][0] * 1.06) xi = np.linspace(xmin, xmax, res) yi = np.linspace(ymin, ymax, res) Xi, Yi = np.meshgrid(xi, yi) interp = _GridData(pos, extrapolate, head_radius, border).set_values(data) Zi = interp.set_locations(Xi, Yi)() # plot outline patch_ = None if 'patch' in outlines: patch_ = outlines['patch'] patch_ = patch_() if callable(patch_) else patch_ patch_.set_clip_on(False) ax.add_patch(patch_) ax.set_transform(ax.transAxes) ax.set_clip_path(patch_) if _use_default_outlines: from matplotlib import patches patch_ = patches.Ellipse((0, 0), 2 * outlines['clip_radius'][0], 2 * outlines['clip_radius'][1], clip_on=True, transform=ax.transData) # plot interpolated map im = ax.imshow(Zi, cmap=cmap, vmin=vmin, vmax=vmax, origin='lower', aspect='equal', extent=(xmin, xmax, ymin, ymax), interpolation=image_interp) # This tackles an incomprehensible matplotlib bug if no contours are # drawn. To avoid rescalings, we will always draw contours. # But if no contours are desired we only draw one and make it invisible. linewidth = mask_params['markeredgewidth'] no_contours = False if isinstance(contours, (np.ndarray, list)): pass # contours precomputed elif contours == 0: contours, no_contours = 1, True if (Zi == Zi[0, 0]).all(): cont = None # can't make contours for constant-valued functions else: with warnings.catch_warnings(record=True): warnings.simplefilter('ignore') cont = ax.contour(Xi, Yi, Zi, contours, colors='k', linewidths=linewidth / 2.) if no_contours and cont is not None: for col in cont.collections: col.set_visible(False) if patch_ is not None: im.set_clip_path(patch_) if cont is not None: for col in cont.collections: col.set_clip_path(patch_) pos_x, pos_y = pos.T if sensors is not False and mask is None: _plot_sensors(pos_x, pos_y, sensors=sensors, ax=ax) elif sensors and mask is not None: idx = np.where(mask)[0] ax.plot(pos_x[idx], pos_y[idx], **mask_params) idx = np.where(~mask)[0] _plot_sensors(pos_x[idx], pos_y[idx], sensors=sensors, ax=ax) elif not sensors and mask is not None: idx = np.where(mask)[0] ax.plot(pos_x[idx], pos_y[idx], **mask_params) if isinstance(outlines, dict): _draw_outlines(ax, outlines) if show_names: if names is None: raise ValueError("To show names, a list of names must be provided" " (see `names` keyword).") if show_names is True: def _show_names(x): return x else: _show_names = show_names show_idx = np.arange(len(names)) if mask is None else np.where(mask)[0] for ii, (p, ch_id) in enumerate(zip(pos, names)): if ii not in show_idx: continue ch_id = _show_names(ch_id) ax.text(p[0], p[1], ch_id, horizontalalignment='center', verticalalignment='center', size='x-small') if onselect is not None: ax.RS = RectangleSelector(ax, onselect=onselect) plt_show(show) return im, cont, interp, patch_
def _plot(self, mneRaw, ica): ica.plot_components(show=False) ica.plot_sources(mneRaw, show=False) self.mneUtil.plotRaw(mneRaw, show=False) plt_show()
def plot_connectivity_circle( con, node_names, indices=None, n_lines=None, node_angles=None, node_width=None, node_colors=None, facecolor="black", textcolor="white", node_edgecolor="black", linewidth=1.5, colormap="hot", vmin=None, vmax=None, colorbar=True, title=None, colorbar_size=0.2, colorbar_pos=(-0.15, -0.05), fontsize_title=12, fontsize_names=10, fontsize_colorbar=10, padding=4.0, fig=None, subplot=111, interactive=True, node_linewidth=2.0, show=True, ): """Visualize connectivity as a circular graph. Note: This code is based on the circle graph example by Nicolas P. Rougier http://www.labri.fr/perso/nrougier/coding/. Parameters ---------- con : numpy.array Connectivity scores. Can be a square matrix, or a 1D array. If a 1D array is provided, "indices" has to be used to define the connection indices. node_names : list of str Node names. The order corresponds to the order in con. indices : tuple of arrays | None Two arrays with indices of connections for which the connections strenghts are defined in con. Only needed if con is a 1D array. n_lines : int | None If not None, only the n_lines strongest connections (strength=abs(con)) are drawn. node_angles : array, shape=(len(node_names,)) | None Array with node positions in degrees. If None, the nodes are equally spaced on the circle. See mne.viz.circular_layout. node_width : float | None Width of each node in degrees. If None, the minimum angle between any two nodes is used as the width. node_colors : list of tuples | list of str List with the color to use for each node. If fewer colors than nodes are provided, the colors will be repeated. Any color supported by matplotlib can be used, e.g., RGBA tuples, named colors. facecolor : str Color to use for background. See matplotlib.colors. textcolor : str Color to use for text. See matplotlib.colors. node_edgecolor : str Color to use for lines around nodes. See matplotlib.colors. linewidth : float Line width to use for connections. colormap : str Colormap to use for coloring the connections. vmin : float | None Minimum value for colormap. If None, it is determined automatically. vmax : float | None Maximum value for colormap. If None, it is determined automatically. colorbar : bool Display a colorbar or not. title : str The figure title. colorbar_size : float Size of the colorbar. colorbar_pos : 2-tuple Position of the colorbar. fontsize_title : int Font size to use for title. fontsize_names : int Font size to use for node names. fontsize_colorbar : int Font size to use for colorbar. padding : float Space to add around figure to accommodate long labels. fig : None | instance of matplotlib.pyplot.Figure The figure to use. If None, a new figure with the specified background color will be created. subplot : int | 3-tuple Location of the subplot when creating figures with multiple plots. E.g. 121 or (1, 2, 1) for 1 row, 2 columns, plot 1. See matplotlib.pyplot.subplot. interactive : bool When enabled, left-click on a node to show only connections to that node. Right-click shows all connections. node_linewidth : float Line with for nodes. show : bool Show figure if True. Returns ------- fig : instance of matplotlib.pyplot.Figure The figure handle. axes : instance of matplotlib.axes.PolarAxesSubplot The subplot handle. """ n_nodes = len(node_names) if node_angles is not None: if len(node_angles) != n_nodes: raise ValueError("node_angles has to be the same length " "as node_names") # convert it to radians node_angles = node_angles * np.pi / 180 else: # uniform layout on unit circle node_angles = np.linspace(0, 2 * np.pi, n_nodes, endpoint=False) if node_width is None: # widths correspond to the minimum angle between two nodes dist_mat = node_angles[None, :] - node_angles[:, None] dist_mat[np.diag_indices(n_nodes)] = 1e9 node_width = np.min(np.abs(dist_mat)) else: node_width = node_width * np.pi / 180 if node_colors is not None: if len(node_colors) < n_nodes: node_colors = cycle(node_colors) else: # assign colors using colormap (plt.cmp.spectral -> plt.spectral) cmap = cm.get_cmap("hsv", n_nodes) # 'Spectral' node_colors = [cmap(i / float(n_nodes)) for i in range(n_nodes)] # handle 1D and 2D connectivity information if con.ndim == 1: if indices is None: raise ValueError("indices has to be provided if con.ndim == 1") elif con.ndim == 2: print("Dimension: 2D") if con.shape[0] != n_nodes or con.shape[1] != n_nodes: raise ValueError("con has to be 1D or a square matrix") # we use the lower-triangular part print("Number_of_nodes : %i" % n_nodes) indices = np.tril_indices(n_nodes, -1) con = np.squeeze(con[indices]) else: raise ValueError("con has to be 1D or a square matrix") con = np.squeeze(con.T) # get the colormap if isinstance(colormap, str): colormap = plt.get_cmap(colormap) # Make figure background the same colors as axes if fig is None: fig = plt.figure(figsize=(14, 11), facecolor=facecolor) # Use a polar axes if not isinstance(subplot, tuple): subplot = (subplot,) axes = plt.subplot(*subplot, polar=True) axes.set_facecolor(facecolor) # No ticks, we'll put our own plt.xticks([]) plt.yticks([]) # Set y axes limit, add additional space if requested plt.ylim(0, 10 + padding) # Remove the black axes border which may obscure the labels axes.spines["polar"].set_visible(False) # Draw lines between connected nodes, only draw the strongest connections if n_lines is not None and len(con) > n_lines: con_thresh = np.sort(np.abs(con).ravel())[-n_lines] else: con_thresh = 0.0 # get the connections which we are drawing and sort by connection strength # this will allow us to draw the strongest connections first con_abs = np.abs(con) con_draw_idx = np.where(con_abs >= con_thresh)[1] con = np.squeeze(con[con_abs >= con_thresh].transpose()) con_abs = np.squeeze(con_abs[con_abs >= con_thresh].transpose()) indices = [np.squeeze(ind[con_draw_idx].transpose()) for ind in indices] # now sort them sort_idx = np.argsort(con_abs) # con_abs = con_abs[0, sort_idx] con = con[0, sort_idx] indices = [np.squeeze(ind[sort_idx].transpose()) for ind in indices] # Get vmin vmax for color scaling if vmin is None: vmin = np.min(con[np.abs(con) >= con_thresh]) if vmax is None: vmax = np.max(con) vrange = vmax - vmin # We want to add some "noise" to the start and end position of the # edges: We modulate the noise with the number of connections of the # node and the connection strength, such that the strongest connections # are closer to the node center nodes_n_con = np.zeros((n_nodes), dtype=np.int) for i, j in zip(indices[0], indices[1]): # print "i : %i / j: %i" % (i,j) nodes_n_con[i] += 1 nodes_n_con[j] += 1 # initialize random number generator so plot is reproducible rng = np.random.mtrand.RandomState(seed=0) n_con = len(indices[0]) noise_max = 0.25 * node_width start_noise = rng.uniform(-noise_max, noise_max, n_con) end_noise = rng.uniform(-noise_max, noise_max, n_con) nodes_n_con_seen = np.zeros_like(nodes_n_con) for i, (start, end) in enumerate(zip(indices[0], indices[1])): nodes_n_con_seen[start] += 1 nodes_n_con_seen[end] += 1 start_noise[i] *= (nodes_n_con[start] - nodes_n_con_seen[start]) / float( nodes_n_con[start] ) end_noise[i] *= (nodes_n_con[end] - nodes_n_con_seen[end]) / float( nodes_n_con[end] ) # scale connectivity for colormap (vmin<=>0, vmax<=>1) con_val_scaled = (con - vmin) / vrange # print("con_val_scaled.shape") con_val_scaled = np.squeeze(con_val_scaled.transpose()) # Finally, we draw the connections for pos, (i, j) in enumerate(zip(indices[0], indices[1])): # Start point t0, r0 = node_angles[i], 10 # End point t1, r1 = node_angles[j], 10 # Some noise in start and end point t0 += start_noise[pos] t1 += end_noise[pos] verts = [(t0, r0), (t0, 5), (t1, 5), (t1, r1)] codes = [ m_path.Path.MOVETO, m_path.Path.CURVE4, m_path.Path.CURVE4, m_path.Path.LINETO, ] path = m_path.Path(verts, codes) color = colormap(con_val_scaled[0, pos]) # Actual line patch = m_patches.PathPatch( path, fill=False, edgecolor=color, linewidth=linewidth, alpha=1.0 ) axes.add_patch(patch) # Draw ring with colored nodes height = np.ones(n_nodes) * 1.0 bars = axes.bar( node_angles, height, width=node_width, bottom=9, edgecolor=node_edgecolor, lw=node_linewidth, facecolor=".9", align="center", ) for bar, color in zip(bars, node_colors): bar.set_facecolor(color) # Draw node labels angles_deg = 180 * node_angles / np.pi for name, angle_rad, angle_deg in zip(node_names, node_angles, angles_deg): if angle_deg >= 270 or angle_deg <= 90: ha = "left" else: # Flip the label, so text is always upright angle_deg += 180 ha = "right" axes.text( angle_rad, 10.4, name, size=fontsize_names, rotation=angle_deg, rotation_mode="anchor", horizontalalignment=ha, verticalalignment="center", color=textcolor, ) if title is not None: plt.title(title, color=textcolor, fontsize=fontsize_title, axes=axes) if colorbar: sm = plt.cm.ScalarMappable(cmap=colormap, norm=plt.Normalize(vmin, vmax)) sm.set_array(np.linspace(vmin, vmax)) cb = plt.colorbar( sm, ax=axes, use_gridspec=False, shrink=colorbar_size, anchor=colorbar_pos ) cb_yticks = plt.getp(cb.ax.axes, "yticklabels") cb.ax.tick_params(labelsize=fontsize_colorbar) plt.setp(cb_yticks, color=textcolor) from functools import partial # Add callback for interaction if interactive: callback = partial( _plot_connectivity_circle_onpick, fig=fig, axes=axes, indices=indices, n_nodes=n_nodes, node_angles=node_angles, ) fig.canvas.mpl_connect("button_press_event", callback) plt_show(show) return fig, axes
def plot(self, fmin=0, fmax=None, proj=False, picks=None, ax=None, color='black', xscale='linear', area_mode='std', area_alpha=0.33, dB=True, estimate='auto', show=True, n_jobs=1, average=False, line_alpha=None, spatial_colors=True, verbose=None, sphere=None): from mne.viz.utils import _plot_psd, plt_show # set up default vars from packaging import version mne_version = version.parse(mne.__version__) has_new_mne = mne_version >= version.parse('0.22.0') has_20_mne = (mne_version >= version.parse('0.20.0') and mne_version < version.parse('0.22.0')) if has_new_mne: from mne.defaults import _handle_default from mne.io.pick import _picks_to_idx from mne.viz._figure import _split_picks_by_type if ax is None: import matplotlib.pyplot as plt fig, ax = plt.subplots() else: fig = ax.figure ax_list = [ax] units = _handle_default('units', None) picks = _picks_to_idx(self.info, picks) titles = _handle_default('titles', None) scalings = _handle_default('scalings', None) make_label = len(ax_list) == len(fig.axes) xlabels_list = [False] * (len(ax_list) - 1) + [True] (picks_list, units_list, scalings_list, titles_list) = _split_picks_by_type(self, picks, units, scalings, titles) elif has_20_mne: from mne.viz.utils import _set_psd_plot_params fig, picks_list, titles_list, units_list, scalings_list, \ ax_list, make_label, xlabels_list = _set_psd_plot_params( self.info, proj, picks, ax, area_mode) else: from mne.viz.utils import _set_psd_plot_params fig, picks_list, titles_list, units_list, scalings_list, ax_list, \ make_label = _set_psd_plot_params(self.info, proj, picks, ax, area_mode) del ax crop_inst = not (fmin == 0 and fmax is None) fmax = self.freqs[-1] if fmax is None else fmax inst = self.copy() if crop_inst: inst.crop(fmin=fmin, fmax=fmax) inst.average() # create list of psd's (one element for each channel type) psd_list = list() for picks in picks_list: psd_list.append(inst.data[picks]) args = [ inst, fig, inst.freqs, psd_list, picks_list, titles_list, units_list, scalings_list, ax_list, make_label, color, area_mode, area_alpha, dB, estimate, average, spatial_colors, xscale, line_alpha ] if has_20_mne or has_new_mne: args += [sphere, xlabels_list] fig = _plot_psd(*args) plt_show(show) return fig
def _plot(self): self.templateRaw = self.fileUtil.load(TEMPLATE_ICA_PATH + "blink_.raw.fif") self.templateICA.plot_components(show=False) self.templateICA.plot_sources(self.templateRaw, show=False) plt_show()
def _plot_connectivity_circle(con, node_names, indices=None, n_lines=None, node_angles=None, node_width=None, node_colors=None, facecolor='black', textcolor='white', node_edgecolor='black', linewidth=1.5, colormap='hot', vmin=None, vmax=None, colorbar=True, title=None, group_node_order=None, group_node_angles=None, group_node_width=None, group_colors=None, fontsize_groups=8, colorbar_size=0.2, colorbar_pos=(-0.3, 0.1), fontsize_title=12, fontsize_names=8, fontsize_colorbar=8, padding=6., fig=None, subplot=111, interactive=False, node_linewidth=2., show=True): """Visualize connectivity as a circular graph. Note: This code is based on the circle graph example by Nicolas P. Rougier http://www.labri.fr/perso/nrougier/coding/. Parameters ---------- con : array Connectivity scores. Can be a square matrix, or a 1D array. If a 1D array is provided, "indices" has to be used to define the connection indices. node_names : list of str Node names. The order corresponds to the order in con. indices : tuple of arrays | None Two arrays with indices of connections for which the connections strenghts are defined in con. Only needed if con is a 1D array. n_lines : int | None If not None, only the n_lines strongest connections (strength=abs(con)) are drawn. node_angles : array, shape=(len(node_names,)) | None Array with node positions in degrees. If None, the nodes are equally spaced on the circle. See mne.viz.circular_layout. node_width : float | None Width of each node in degrees. If None, the minimum angle between any two nodes is used as the width. node_colors : list of tuples | list of str List with the color to use for each node. If fewer colors than nodes are provided, the colors will be repeated. Any color supported by matplotlib can be used, e.g., RGBA tuples, named colors. group_node_order : list of str Group node names in correct order. group_node_angles : array, shape=(len(group_node_order,)) | None Array with node positions in degrees. If None, the nodes are equally spaced on the circle. See mne.viz.circular_layout. group_node_width : float | None Width of each group node in degrees. If None, the minimum angle between any two nodes is used as the width. group_colors : None List with colours to use for each group node. fontsize_groups : int The font size of the text used for group node labels. facecolor : str Color to use for background. See matplotlib.colors. textcolor : str Color to use for text. See matplotlib.colors. node_edgecolor : str Color to use for lines around nodes. See matplotlib.colors. linewidth : float Line width to use for connections. colormap : str Colormap to use for coloring the connections. vmin : float | None Minimum value for colormap. If None, it is determined automatically. vmax : float | None Maximum value for colormap. If None, it is determined automatically. colorbar : bool Display a colorbar or not. title : str The figure title. colorbar_size : float Size of the colorbar. colorbar_pos : 2-tuple Position of the colorbar. fontsize_title : int Font size to use for title. fontsize_names : int Font size to use for node names. fontsize_colorbar : int Font size to use for colorbar. padding : float Space to add around figure to accommodate long labels. fig : None | instance of matplotlib.pyplot.Figure The figure to use. If None, a new figure with the specified background color will be created. subplot : int | 3-tuple Location of the subplot when creating figures with multiple plots. E.g. 121 or (1, 2, 1) for 1 row, 2 columns, plot 1. See matplotlib.pyplot.subplot. interactive : bool When enabled, left-click on a node to show only connections to that node. Right-click shows all connections. node_linewidth : float Line with for nodes. show : bool Show figure if True. Returns ------- fig : instance of matplotlib.pyplot.Figure The figure handle. axes : instance of matplotlib.axes.PolarAxesSubplot The subplot handle. """ import matplotlib.path as m_path import matplotlib.patches as m_patches n_nodes = len(node_names) n_groups = len(group_node_order) if group_node_angles is not None: if len(group_node_angles) != n_groups: raise ValueError('group_node_angles has to be the same length ' 'as group_node_order') # convert it to radians group_node_angles = group_node_angles * np.pi / 180 if node_angles is not None: if len(node_angles) != n_nodes: raise ValueError('node_angles has to be the same length ' 'as node_names') # convert it to radians node_angles = node_angles * np.pi / 180 else: # uniform layout on unit circle node_angles = np.linspace(0, 2 * np.pi, n_nodes, endpoint=False) if node_width is None: # widths correspond to the minimum angle between two nodes dist_mat = node_angles[None, :] - node_angles[:, None] dist_mat[np.diag_indices(n_nodes)] = 1e9 node_width = np.min(np.abs(dist_mat)) else: node_width = node_width * np.pi / 180 if node_colors is not None: if len(node_colors) < n_nodes: node_colors = cycle(node_colors) else: # assign colors using colormap node_colors = [plt.cm.spectral(i / float(n_nodes)) for i in range(n_nodes)] # handle 1D and 2D connectivity information if con.ndim == 1: if indices is None: raise ValueError('indices has to be provided if con.ndim == 1') elif con.ndim == 2: if con.shape[0] != n_nodes or con.shape[1] != n_nodes: raise ValueError('con has to be 1D or a square matrix') # we use the lower-triangular part indices = np.tril_indices(n_nodes, -1) con = con[indices] else: raise ValueError('con has to be 1D or a square matrix') # get the colormap if isinstance(colormap, string_types): colormap = plt.get_cmap(colormap) # Make figure background the same colors as axes if fig is None: fig = plt.figure(figsize=(8, 8), facecolor=facecolor) # Use a polar axes if not isinstance(subplot, tuple): subplot = (subplot,) axes = plt.subplot(*subplot, polar=True, axisbg=facecolor) # No ticks, we'll put our own plt.xticks([]) plt.yticks([]) # Set y axes limit, add additonal space if requested # plt.ylim(0, 10 + padding) # increase space to allow for external group names plt.ylim(0, 18 + padding) # Remove the black axes border which may obscure the labels axes.spines['polar'].set_visible(False) # Draw lines between connected nodes, only draw the strongest connections if n_lines is not None and len(con) > n_lines: con_thresh = np.sort(np.abs(con).ravel())[-n_lines] else: con_thresh = 0. # get the connections which we are drawing and sort by connection strength # this will allow us to draw the strongest connections first con_abs = np.abs(con) con_draw_idx = np.where(con_abs >= con_thresh)[0] con = con[con_draw_idx] con_abs = con_abs[con_draw_idx] indices = [ind[con_draw_idx] for ind in indices] # now sort them sort_idx = np.argsort(con_abs) con_abs = con_abs[sort_idx] con = con[sort_idx] indices = [ind[sort_idx] for ind in indices] # Get vmin vmax for color scaling if vmin is None: vmin = np.min(con[np.abs(con) >= con_thresh]) if vmax is None: vmax = np.max(con) vrange = vmax - vmin # We want to add some "noise" to the start and end position of the # edges: We modulate the noise with the number of connections of the # node and the connection strength, such that the strongest connections # are closer to the node center nodes_n_con = np.zeros((n_nodes), dtype=np.int) for i, j in zip(indices[0], indices[1]): nodes_n_con[i] += 1 nodes_n_con[j] += 1 # initalize random number generator so plot is reproducible rng = np.random.mtrand.RandomState(seed=0) n_con = len(indices[0]) noise_max = 0.25 * node_width start_noise = rng.uniform(-noise_max, noise_max, n_con) end_noise = rng.uniform(-noise_max, noise_max, n_con) nodes_n_con_seen = np.zeros_like(nodes_n_con) for i, (start, end) in enumerate(zip(indices[0], indices[1])): nodes_n_con_seen[start] += 1 nodes_n_con_seen[end] += 1 start_noise[i] *= ((nodes_n_con[start] - nodes_n_con_seen[start]) / float(nodes_n_con[start])) end_noise[i] *= ((nodes_n_con[end] - nodes_n_con_seen[end]) / float(nodes_n_con[end])) # scale connectivity for colormap (vmin<=>0, vmax<=>1) con_val_scaled = (con - vmin) / vrange # Finally, we draw the connections for pos, (i, j) in enumerate(zip(indices[0], indices[1])): # Start point t0, r0 = node_angles[i], 10 # End point t1, r1 = node_angles[j], 10 # Some noise in start and end point t0 += start_noise[pos] t1 += end_noise[pos] verts = [(t0, r0), (t0, 5), (t1, 5), (t1, r1)] codes = [m_path.Path.MOVETO, m_path.Path.CURVE4, m_path.Path.CURVE4, m_path.Path.LINETO] path = m_path.Path(verts, codes) color = colormap(con_val_scaled[pos]) # Actual line patch = m_patches.PathPatch(path, fill=False, edgecolor=color, linewidth=linewidth, alpha=1.) axes.add_patch(patch) # Draw ring with colored nodes height = np.ones(n_nodes) * 1.0 bars = axes.bar(node_angles, height, width=node_width, bottom=9, edgecolor=node_edgecolor, lw=node_linewidth, facecolor='.9', align='center') for bar, color in zip(bars, node_colors): bar.set_facecolor(color) # Draw node labels angles_deg = 180 * node_angles / np.pi for name, angle_rad, angle_deg in zip(node_names, node_angles, angles_deg): if angle_deg >= 270: ha = 'left' else: # Flip the label, so text is always upright angle_deg += 180 ha = 'right' axes.text(angle_rad, 10.4, name, size=fontsize_names, rotation=angle_deg, rotation_mode='anchor', horizontalalignment=ha, verticalalignment='center', color=textcolor) # draw outer ring with group names group_heights = np.ones(n_groups) * 1.5 group_width = 2 * np.pi/n_groups # draw ring with group colours group_bars = axes.bar(group_node_angles, group_heights, width=group_width, bottom=22, linewidth=node_linewidth, facecolor='.9', edgecolor=node_edgecolor) for gbar, color in zip(group_bars, group_colors): gbar.set_facecolor(color) # add group labels for i in range(group_node_angles.size): # to modify the position of the labels theta = group_node_angles[i] + np.pi/n_groups # theta = group_node_angles[n_groups-1-i] + np.pi/n_groups plt.text(theta, 22.5, group_node_order[i], rotation=180*theta/np.pi-90, size=fontsize_groups, horizontalalignment='center', verticalalignment='center', color=textcolor) if title is not None: plt.title(title, color=textcolor, fontsize=fontsize_title, axes=axes) if colorbar: norm = plt.normalize_colors(vmin=vmin, vmax=vmax) sm = plt.cm.ScalarMappable(cmap=colormap, norm=norm) sm.set_array(np.linspace(vmin, vmax)) cb = plt.colorbar(sm, ax=axes, use_gridspec=False, shrink=colorbar_size, orientation='horizontal', anchor=colorbar_pos) cb_yticks = plt.getp(cb.ax.axes, 'yticklabels') cb.ax.tick_params(labelsize=fontsize_colorbar) plt.setp(cb_yticks, color=textcolor) # Add callback for interaction if interactive: callback = partial(_plot_connectivity_circle_onpick, fig=fig, axes=axes, indices=indices, n_nodes=n_nodes, node_angles=node_angles) fig.canvas.mpl_connect('button_press_event', callback) plt_show(show) return fig, axes
def plot_epochs(epochs, picks=None, scalings=None, n_epochs=20, n_channels=20, title=None, show=True, block=False, bad_epochs_idx=None, fix_log=None): """ Visualize epochs Bad epochs can be marked with a left click on top of the epoch. Bad channels can be selected by clicking the channel name on the left side of the main axes. Calling this function drops all the selected bad epochs as well as bad epochs marked beforehand with rejection parameters. Parameters ---------- epochs : instance of Epochs The epochs object picks : array-like of int | None Channels to be included. If None only good data channels are used. Defaults to None scalings : dict | 'auto' | None Scaling factors for the traces. If any fields in scalings are 'auto', the scaling factor is set to match the 99.5th percentile of a subset of the corresponding data. If scalings == 'auto', all scalings fields are set to 'auto'. If any fields are 'auto' and data is not preloaded, a subset of epochs up to 100mb will be loaded. If None, defaults to:: dict(mag=1e-12, grad=4e-11, eeg=20e-6, eog=150e-6, ecg=5e-4, emg=1e-3, ref_meg=1e-12, misc=1e-3, stim=1, resp=1, chpi=1e-4) n_epochs : int The number of epochs per view. Defaults to 20. n_channels : int The number of channels per view. Defaults to 20. title : str | None The title of the window. If None, epochs name will be displayed. Defaults to None. show : bool Show figure if True. Defaults to True block : bool Whether to halt program execution until the figure is closed. Useful for rejecting bad trials on the fly by clicking on an epoch. Defaults to False. bad_epochs_idx : array-like | None Indices of bad epochs to show. No bad epochs to visualize if None. fix_log : array, shape (n_channels, n_epochs) | None The bad segments to show in red and the interpolated segments to show in green. Returns ------- fig : Instance of matplotlib.figure.Figure The figure. Notes ----- The arrow keys (up/down/left/right) can be used to navigate between channels and epochs and the scaling can be adjusted with - and + (or =) keys, but this depends on the backend matplotlib is configured to use (e.g., mpl.use(``TkAgg``) should work). Full screen mode can be toggled with f11 key. The amount of epochs and channels per view can be adjusted with home/end and page down/page up keys. Butterfly plot can be toggled with ``b`` key. Right mouse click adds a vertical line to the plot. """ epochs.drop_bad() scalings = _compute_scalings(scalings, epochs) scalings = _handle_default('scalings_plot_raw', scalings) projs = epochs.info['projs'] bads = np.array(list(), dtype=int) if bad_epochs_idx is not None: bads = np.array(bad_epochs_idx).astype(int) params = {'epochs': epochs, 'info': copy.deepcopy(epochs.info), 'bad_color': (0.8, 0.8, 0.8), 't_start': 0, 'histogram': None, 'bads': bads, 'fix_log': fix_log} params['label_click_fun'] = partial(_pick_bad_channels, params=params) _prepare_mne_browse_epochs(params, projs, n_channels, n_epochs, scalings, title, picks) _prepare_projectors(params) _layout_figure(params) callback_close = partial(_close_event, params=params) params['fig'].canvas.mpl_connect('close_event', callback_close) try: plt_show(show, block=block) except TypeError: # not all versions have this plt_show(show) return params['fig']
def plot_topomap(data, pos, vmin=None, vmax=None, cmap=None, sensors=True, res=64, axes=None, names=None, show_names=False, mask=None, mask_params=None, outlines='head', image_mask=None, contours=6, image_interp='bilinear', show=True, head_pos=None, onselect=None, axis=None): ''' see the docstring for mne.viz.plot_topomap, which i've simply modified to return more objects ''' from matplotlib.widgets import RectangleSelector from mne.io.pick import (channel_type, pick_info, _pick_data_channels) from mne.utils import warn from mne.viz.utils import (_setup_vmin_vmax, plt_show) from mne.defaults import _handle_default from mne.channels.layout import _find_topomap_coords from mne.io.meas_info import Info from mne.viz.topomap import _check_outlines, _prepare_topomap, _griddata, _make_image_mask, _plot_sensors, \ _draw_outlines data = np.asarray(data) if isinstance(pos, Info): # infer pos from Info object picks = _pick_data_channels(pos) # pick only data channels pos = pick_info(pos, picks) # check if there is only 1 channel type, and n_chans matches the data ch_type = set(channel_type(pos, idx) for idx, _ in enumerate(pos["chs"])) info_help = ("Pick Info with e.g. mne.pick_info and " "mne.channels.channel_indices_by_type.") if len(ch_type) > 1: raise ValueError("Multiple channel types in Info structure. " + info_help) elif len(pos["chs"]) != data.shape[0]: raise ValueError("Number of channels in the Info object and " "the data array does not match. " + info_help) else: ch_type = ch_type.pop() if any(type_ in ch_type for type_ in ('planar', 'grad')): # deal with grad pairs from ..channels.layout import (_merge_grad_data, find_layout, _pair_grad_sensors) picks, pos = _pair_grad_sensors(pos, find_layout(pos)) data = _merge_grad_data(data[picks]).reshape(-1) else: picks = list(range(data.shape[0])) pos = _find_topomap_coords(pos, picks=picks) if data.ndim > 1: raise ValueError("Data needs to be array of shape (n_sensors,); got " "shape %s." % str(data.shape)) # Give a helpful error message for common mistakes regarding the position # matrix. pos_help = ("Electrode positions should be specified as a 2D array with " "shape (n_channels, 2). Each row in this matrix contains the " "(x, y) position of an electrode.") if pos.ndim != 2: error = ("{ndim}D array supplied as electrode positions, where a 2D " "array was expected").format(ndim=pos.ndim) raise ValueError(error + " " + pos_help) elif pos.shape[1] == 3: error = ("The supplied electrode positions matrix contains 3 columns. " "Are you trying to specify XYZ coordinates? Perhaps the " "mne.channels.create_eeg_layout function is useful for you.") raise ValueError(error + " " + pos_help) # No error is raised in case of pos.shape[1] == 4. In this case, it is # assumed the position matrix contains both (x, y) and (width, height) # values, such as Layout.pos. elif pos.shape[1] == 1 or pos.shape[1] > 4: raise ValueError(pos_help) if len(data) != len(pos): raise ValueError("Data and pos need to be of same length. Got data of " "length %s, pos of length %s" % (len(data), len(pos))) norm = min(data) >= 0 vmin, vmax = _setup_vmin_vmax(data, vmin, vmax, norm) if cmap is None: cmap = 'Reds' if norm else 'RdBu_r' pos, outlines = _check_outlines(pos, outlines, head_pos) if axis is not None: axes = axis warn('axis parameter is deprecated and will be removed in 0.13. ' 'Use axes instead.', DeprecationWarning) ax = axes if axes else plt.gca() pos_x, pos_y = _prepare_topomap(pos, ax) if outlines is None: xmin, xmax = pos_x.min(), pos_x.max() ymin, ymax = pos_y.min(), pos_y.max() else: xlim = np.inf, -np.inf, ylim = np.inf, -np.inf, mask_ = np.c_[outlines['mask_pos']] xmin, xmax = (np.min(np.r_[xlim[0], mask_[:, 0]]), np.max(np.r_[xlim[1], mask_[:, 0]])) ymin, ymax = (np.min(np.r_[ylim[0], mask_[:, 1]]), np.max(np.r_[ylim[1], mask_[:, 1]])) # interpolate data xi = np.linspace(xmin, xmax, res) yi = np.linspace(ymin, ymax, res) Xi, Yi = np.meshgrid(xi, yi) Zi = _griddata(pos_x, pos_y, data, Xi, Yi) if outlines is None: _is_default_outlines = False elif isinstance(outlines, dict): _is_default_outlines = any(k.startswith('head') for k in outlines) if _is_default_outlines and image_mask is None: # prepare masking image_mask, pos = _make_image_mask(outlines, pos, res) mask_params = _handle_default('mask_params', mask_params) # plot outline linewidth = mask_params['markeredgewidth'] patch = None if 'patch' in outlines: patch = outlines['patch'] patch_ = patch() if callable(patch) else patch patch_.set_clip_on(False) ax.add_patch(patch_) ax.set_transform(ax.transAxes) ax.set_clip_path(patch_) # plot map and countour im = ax.imshow(Zi, cmap=cmap, vmin=vmin, vmax=vmax, origin='lower', aspect='equal', extent=(xmin, xmax, ymin, ymax), interpolation=image_interp) # This tackles an incomprehensible matplotlib bug if no contours are # drawn. To avoid rescalings, we will always draw contours. # But if no contours are desired we only draw one and make it invisible . no_contours = False if contours in (False, None): contours, no_contours = 1, True cont = ax.contour(Xi, Yi, Zi, contours, colors='k', linewidths=linewidth) if no_contours is True: for col in cont.collections: col.set_visible(False) if _is_default_outlines: from matplotlib import patches patch_ = patches.Ellipse((0, 0), 2 * outlines['clip_radius'][0], 2 * outlines['clip_radius'][1], clip_on=True, transform=ax.transData) if _is_default_outlines or patch is not None: im.set_clip_path(patch_) if cont is not None: for col in cont.collections: col.set_clip_path(patch_) if sensors is not False and mask is None: _plot_sensors(pos_x, pos_y, sensors=sensors, ax=ax) elif sensors and mask is not None: idx = np.where(mask)[0] ax.plot(pos_x[idx], pos_y[idx], **mask_params) idx = np.where(~mask)[0] _plot_sensors(pos_x[idx], pos_y[idx], sensors=sensors, ax=ax) elif not sensors and mask is not None: idx = np.where(mask)[0] ax.plot(pos_x[idx], pos_y[idx], **mask_params) if isinstance(outlines, dict): _draw_outlines(ax, outlines) if show_names: if names is None: raise ValueError("To show names, a list of names must be provided" " (see `names` keyword).") if show_names is True: def _show_names(x): return x else: _show_names = show_names show_idx = np.arange(len(names)) if mask is None else np.where(mask)[0] for ii, (p, ch_id) in enumerate(zip(pos, names)): if ii not in show_idx: continue ch_id = _show_names(ch_id) ax.text(p[0], p[1], ch_id, horizontalalignment='center', verticalalignment='center', size='x-small') plt.subplots_adjust(top=.95) if onselect is not None: ax.RS = RectangleSelector(ax, onselect=onselect) plt_show(show) return ax, im, cont, pos_x, pos_y
data = raw._data for field in eegFields: channel = data[ch_names.index(field)] channel -= eegMean print mean(channel) def runTestData(): global probands probands = ["mp"] files = ["awake_full", "drowsy_full"] for f in files: csvToRaw(f + ".csv", f) rawWithEOGAndICA(f, f + "_eog") rawWithNormedGyroAll(f + "_eog", f + "_norm") def rawToCSV(proband, name): filePath = (FILE_PATH % str(proband)) + name dto = fileUtil.getDtoFromFif(filePath + ".raw.fif") fileUtil.saveDto(filePath + ".csv", dto) if __name__ == '__main__': #runTestData() #for proband in probands: # rawToCSV(proband, "EEGNormed") raw = loadRaw("1", "EEG") plotRaw(raw) plt_show()