def plot_head_signals_tight_multiple_signals(all_signals, sensor_names=None, figsize=(10, 8), plot_args=None): assert sensor_names is not None assert all([len(signals) == len(all_signals[0]) for signals in all_signals]) if plot_args is None: plot_args = dict() figure = plt.figure(figsize=figsize) sensor_positions = [get_sensor_pos(name, tight_C_positions) for name in sensor_names] sensor_positions = np.array(sensor_positions) # sensors x 2(row and col) maxima = np.max(sensor_positions, axis=0) minima = np.min(sensor_positions, axis=0) max_row = maxima[0, 0] max_col = maxima[1, 0] min_row = minima[0, 0] min_col = minima[1, 0] rows = max_row - min_row + 1 cols = max_col - min_col + 1 first_ax = None # gridspec inside gridspec outer_grid = gridspec.GridSpec(rows, cols, wspace=0.3, hspace=0.5) for i in xrange(0, len(all_signals[0])): sensor_name = sensor_names[i] sensor_pos = sensor_positions[i] assert np.all(sensor_pos == get_sensor_pos(sensor_name, tight_C_positions)) row = sensor_pos[0] col = sensor_pos[1] inner_grid = gridspec.GridSpecFromSubplotSpec(len(all_signals), 1, subplot_spec=outer_grid[row - min_row, col - min_col], wspace=0.0, hspace=0.0) for signal_type in xrange(len(all_signals)): signal = all_signals[signal_type][i] if first_ax is None: ax = plt.Subplot(figure, inner_grid[signal_type, 0]) first_ax = ax else: ax = plt.Subplot(figure, inner_grid[signal_type, 0], sharey=first_ax, sharex=first_ax) if signal_type == 0: ax.set_title(sensor_name, fontsize=10) ax.plot(signal, **plot_args) ax.xaxis.grid(True) # make line at zero ax.axhline(y=0, ls=':', color="grey") figure.add_subplot(ax) if len(signal) == 600: plt.xticks([150, 300, 450], []) else: plt.xticks([]) plt.yticks([]) return figure
def plot_head_signals(signals, sensor_names=None, figsize=(12, 7), plot_args=None): assert sensor_names is None or len(signals) == len(sensor_names), ( "need " "sensor names for all sensor matrices") if sensor_names is None: sensor_names = map(str, range(len(signals))) if plot_args is None: plot_args = dict() figure = plt.figure(figsize=figsize) sensor_positions = [get_sensor_pos(name) for name in sensor_names] sensor_positions = np.array(sensor_positions) # sensors x 2(row and col) maxima = np.max(sensor_positions, axis=0) minima = np.min(sensor_positions, axis=0) max_row = maxima[0] max_col = maxima[1] min_row = minima[0] min_col = minima[1] rows = max_row - min_row + 1 cols = max_col - min_col + 1 first_ax = None for i in xrange(0, len(signals)): sensor_name = sensor_names[i] sensor_pos = sensor_positions[i] assert np.all(sensor_pos == get_sensor_pos(sensor_name)) # Transform to flat sensor pos row = sensor_pos[0] col = sensor_pos[1] subplot_ind = ( row - min_row ) * cols + col - min_col + 1 # +1 as matlab uses based indexing if first_ax is None: ax = figure.add_subplot(rows, cols, subplot_ind) first_ax = ax else: ax = figure.add_subplot(rows, cols, subplot_ind, sharey=first_ax, sharex=first_ax) signal = signals[i] ax.plot(signal, **plot_args) ax.set_title(sensor_name) ax.set_yticks([]) if len(signal) == 600: ax.set_xticks([150, 300, 450]) ax.set_xticklabels([]) ax.xaxis.grid(True) # make line at zero ax.axhline(y=0, ls=':', color="grey") return figure
def plot_head_signals(signals, sensor_names=None, figsize=(12, 7), plot_args=None): assert sensor_names is None or len(signals) == len(sensor_names), ("need " "sensor names for all sensor matrices") if sensor_names is None: sensor_names = map(str, range(len(signals))) if plot_args is None: plot_args = dict() figure = plt.figure(figsize=figsize) sensor_positions = [get_sensor_pos(name) for name in sensor_names] sensor_positions = np.array(sensor_positions) # sensors x 2(row and col) maxima = np.max(sensor_positions, axis=0) minima = np.min(sensor_positions, axis=0) max_row = maxima[0] max_col = maxima[1] min_row = minima[0] min_col = minima[1] rows = max_row - min_row + 1 cols = max_col - min_col + 1 first_ax = None for i in xrange(0, len(signals)): sensor_name = sensor_names[i] sensor_pos = sensor_positions[i] assert np.all(sensor_pos == get_sensor_pos(sensor_name)) # Transform to flat sensor pos row = sensor_pos[0] col = sensor_pos[1] subplot_ind = (row - min_row) * cols + col - min_col + 1 # +1 as matlab uses based indexing if first_ax is None: ax = figure.add_subplot(rows, cols, subplot_ind) first_ax = ax else: ax = figure.add_subplot(rows, cols, subplot_ind, sharey=first_ax, sharex=first_ax) signal = signals[i] ax.plot(signal, **plot_args) ax.set_title(sensor_name) ax.set_yticks([]) if len(signal) == 600: ax.set_xticks([150, 300, 450]) ax.set_xticklabels([]) ax.xaxis.grid(True) # make line at zero ax.axhline(y=0, ls=':', color="grey") return figure
def plot_chan_matrices(matrices, sensor_names, figname='', figure=None, figsize=(8, 4.5), yticks=(), yticklabels=(), correctness_matrices=None, colormap=cm.coolwarm, sensor_map=cap_positions, vmax=None, vmin=None, share_y_axes=True): """ figsize ignored if figure given """ assert len(matrices) == len(sensor_names), "need sensor names for all sensor matrices" if figure is None: figure = plt.figure(figsize=figsize) sensor_positions = [get_sensor_pos(name, sensor_map) for name in sensor_names] sensor_positions = np.array(sensor_positions) # #sensors x 2(row and col) x1(for some reason:)) maxima = np.max(sensor_positions, axis=0) minima = np.min(sensor_positions, axis=0) max_row = maxima[0] max_col = maxima[1] min_row = minima[0] min_col = minima[1] rows = max_row - min_row + 1 cols = max_col - min_col + 1 mean_abs_weight = np.mean(np.abs(matrices)) if (correctness_matrices is not None): mean_abs_weight = np.mean(np.abs(matrices * correctness_matrices)) if figname != '': figure.suptitle(figname, fontsize=14) # + ", pixel abs mean(x100): {:.3f}".format(mean_abs_weight * 100), first_ax = None for i in xrange(0, len(matrices)): sensor_name = sensor_names[i] sensor_pos = sensor_positions[i] assert np.all(sensor_pos == get_sensor_pos(sensor_name, sensor_map)) # Transform to flat sensor pos row = sensor_pos[0] col = sensor_pos[1] subplot_ind = (row - min_row) * cols + col - min_col + 1 # +1 as matlab uses based indexing if vmin is None: vmin = -2 * mean_abs_weight if vmax is None: vmax = 2 * mean_abs_weight if first_ax is None or not share_y_axes: ax = figure.add_subplot(rows, cols, subplot_ind) first_ax = ax elif share_y_axes: ax = figure.add_subplot(rows, cols, subplot_ind, sharey=first_ax) chan_matrix = matrices[i] ax.set_title(sensor_name) if (correctness_matrices is None): # ax.pcolor(chan_matrix.T, # cmap=cm.bwr, #origin='lower', #interpolation='nearest',#aspect='auto' # vmin=-mean_abs_weight * 2, vmax= mean_abs_weight * 2) ax.pcolorfast(chan_matrix.T, cmap=colormap, # origin='lower', #interpolation='nearest',#aspect='auto' vmin=vmin, vmax=vmax) # ax.imshow(chan_matrix.T, # interpolation='nearest', cmap=cm.bwr, origin='lower', # vmin=-mean_abs_weight * 2, vmax= mean_abs_weight * 2, # aspect='auto')#""" else: # Show correct and incorrect inputs with different colors # weighted also by degree of correctness correctness_mat = correctness_matrices[i] ax.imshow( np.ma.masked_where(correctness_mat < 0, chan_matrix * correctness_mat).T, interpolation='nearest', cmap=cm.bwr, origin='lower', vmin=vmin, vmax=vmax) # use yellow/brown for red(positive) incorrect values # (mask out correct or blue=negative values) # also take minus the values as otherwise they go from # minus-something to 0, that makes colormaps # more complicated to use :) ax.imshow( np.ma.masked_where( np.logical_or(correctness_mat > 0, chan_matrix < 0), - (chan_matrix * correctness_mat)).T, interpolation='nearest', cmap=cm.YlOrBr, origin='lower', vmin=0, vmax=vmax) # use purple for blue(negative) incorrect values ax.imshow( np.ma.masked_where( np.logical_or(correctness_mat > 0, chan_matrix >= 0), chan_matrix * correctness_mat).T, interpolation='nearest', cmap=cm.PuRd, origin='lower', vmin=0, vmax=vmax) ax.set_xticks([]) ax.set_yticks(yticks) ax.set_yticklabels(yticklabels) ax.tick_params(axis='both', which='major', labelsize=6) ax.grid(color='k', linewidth=0.1, linestyle=':') return figure
def plot_chan_matrices(matrices, sensor_names, figname='', figure=None, figsize=(8, 4.5), yticks=(), yticklabels=(), correctness_matrices=None, colormap=cm.coolwarm, sensor_map=cap_positions, vmax=None, vmin=None, share_y_axes=True): """ Chan matrices in [chan x row x col] order. figsize ignored if figure given """ assert len(matrices) == len( sensor_names), "need sensor names for all sensor matrices" if figure is None: figure = plt.figure(figsize=figsize) sensor_positions = [ get_sensor_pos(name, sensor_map) for name in sensor_names ] sensor_positions = np.array( sensor_positions) # #sensors x 2(row and col) x1(for some reason:)) maxima = np.max(sensor_positions, axis=0) minima = np.min(sensor_positions, axis=0) max_row = maxima[0] max_col = maxima[1] min_row = minima[0] min_col = minima[1] rows = max_row - min_row + 1 cols = max_col - min_col + 1 mean_abs_weight = np.mean(np.abs(matrices)) if (correctness_matrices is not None): mean_abs_weight = np.mean(np.abs(matrices * correctness_matrices)) if figname != '': figure.suptitle( figname, fontsize=14 ) # + ", pixel abs mean(x100): {:.3f}".format(mean_abs_weight * 100), first_ax = None for i in xrange(0, len(matrices)): sensor_name = sensor_names[i] sensor_pos = sensor_positions[i] assert np.all(sensor_pos == get_sensor_pos(sensor_name, sensor_map)) # Transform to flat sensor pos row = sensor_pos[0] col = sensor_pos[1] subplot_ind = ( row - min_row ) * cols + col - min_col + 1 # +1 as matlab uses based indexing if vmin is None: vmin = -2 * mean_abs_weight if vmax is None: vmax = 2 * mean_abs_weight if first_ax is None or not share_y_axes: ax = figure.add_subplot(rows, cols, subplot_ind) first_ax = ax elif share_y_axes: ax = figure.add_subplot(rows, cols, subplot_ind, sharey=first_ax) chan_matrix = matrices[i] ax.set_title(sensor_name) if (correctness_matrices is None): # ax.pcolor(chan_matrix.T, # cmap=cm.bwr, #origin='lower', #interpolation='nearest',#aspect='auto' # vmin=-mean_abs_weight * 2, vmax= mean_abs_weight * 2) ax.pcolorfast( chan_matrix.T, cmap= colormap, # origin='lower', #interpolation='nearest',#aspect='auto' vmin=vmin, vmax=vmax) # ax.imshow(chan_matrix.T, # interpolation='nearest', cmap=cm.bwr, origin='lower', # vmin=-mean_abs_weight * 2, vmax= mean_abs_weight * 2, # aspect='auto')#""" else: # Show correct and incorrect inputs with different colors # weighted also by degree of correctness correctness_mat = correctness_matrices[i] ax.imshow(np.ma.masked_where(correctness_mat < 0, chan_matrix * correctness_mat).T, interpolation='nearest', cmap=cm.bwr, origin='lower', vmin=vmin, vmax=vmax) # use yellow/brown for red(positive) incorrect values # (mask out correct or blue=negative values) # also take minus the values as otherwise they go from # minus-something to 0, that makes colormaps # more complicated to use :) ax.imshow(np.ma.masked_where( np.logical_or(correctness_mat > 0, chan_matrix < 0), -(chan_matrix * correctness_mat)).T, interpolation='nearest', cmap=cm.YlOrBr, origin='lower', vmin=0, vmax=vmax) # use purple for blue(negative) incorrect values ax.imshow(np.ma.masked_where( np.logical_or(correctness_mat > 0, chan_matrix >= 0), chan_matrix * correctness_mat).T, interpolation='nearest', cmap=cm.PuRd, origin='lower', vmin=0, vmax=vmax) ax.set_xticks([]) ax.set_yticks(yticks) ax.set_yticklabels(yticklabels) ax.tick_params(axis='both', which='major', labelsize=6) ax.grid(color='k', linewidth=0.1, linestyle=':') return figure
def plot_head_signals_tight_multiple_signals(all_signals, sensor_names=None, figsize=(10, 8), plot_args=None): assert sensor_names is not None assert all( [len(signals) == len(all_signals[0]) for signals in all_signals]) if plot_args is None: plot_args = dict() figure = plt.figure(figsize=figsize) sensor_positions = [ get_sensor_pos(name, tight_C_positions) for name in sensor_names ] sensor_positions = np.array(sensor_positions) # sensors x 2(row and col) maxima = np.max(sensor_positions, axis=0) minima = np.min(sensor_positions, axis=0) max_row = maxima[0, 0] max_col = maxima[1, 0] min_row = minima[0, 0] min_col = minima[1, 0] rows = max_row - min_row + 1 cols = max_col - min_col + 1 first_ax = None # gridspec inside gridspec outer_grid = gridspec.GridSpec(rows, cols, wspace=0.3, hspace=0.5) for i in xrange(0, len(all_signals[0])): sensor_name = sensor_names[i] sensor_pos = sensor_positions[i] assert np.all( sensor_pos == get_sensor_pos(sensor_name, tight_C_positions)) row = sensor_pos[0] col = sensor_pos[1] inner_grid = gridspec.GridSpecFromSubplotSpec( len(all_signals), 1, subplot_spec=outer_grid[row - min_row, col - min_col], wspace=0.0, hspace=0.0) for signal_type in xrange(len(all_signals)): signal = all_signals[signal_type][i] if first_ax is None: ax = plt.Subplot(figure, inner_grid[signal_type, 0]) first_ax = ax else: ax = plt.Subplot(figure, inner_grid[signal_type, 0], sharey=first_ax, sharex=first_ax) if signal_type == 0: ax.set_title(sensor_name, fontsize=10) ax.plot(signal, **plot_args) ax.xaxis.grid(True) # make line at zero ax.axhline(y=0, ls=':', color="grey") figure.add_subplot(ax) if len(signal) == 600: plt.xticks([150, 300, 450], []) else: plt.xticks([]) plt.yticks([]) return figure