def getNetworkWithin(in_dat, roiIndx): m=in_dat.copy() #Null the upper triangle, so that you don't get the redundant and #the diagonal values: idx_null = triu_indices(m.shape[0]) m[idx_null] = np.nan #Extract network values withinVals = m[roiIndx,:][:,roiIndx] return withinVals
def getNetworkWithin(in_dat, roiIndx): m = in_dat.copy() #Null the upper triangle, so that you don't get the redundant and #the diagonal values: idx_null = triu_indices(m.shape[0]) m[idx_null] = np.nan #Extract network values withinVals = m[roiIndx, :][:, roiIndx] return withinVals
def getNetworkWithin(in_dat, roiIndx): m=in_dat.copy() #Null the upper triangle, so that you don't get the redundant and #the diagonal values: newMat=np.zeros((m.shape[0], len(roiIndx), len(roiIndx))) #Iterate through each run for runNum in range(m.shape[0]): m_onerun=m[runNum][:][:] idx_null = triu_indices(m_onerun.shape[0]) m_onerun[idx_null] = np.nan #Extract network values withinVals = m_onerun[roiIndx,:][:,roiIndx] newMat[runNum][:][:]=withinVals return newMat
def getNetworkWithin_test(in_d, roiIndx): dat = in_d.copy() #Null the upper triangle, so that you don't get the redundant and #the diagonal values: idx_null = triu_indices(dat.shape[0]) dat[idx_null] = np.nan #Extract network values allNet = [] numROIs = roiIndx[1:] numStart = 0 while len(numROIs) > 0: for jj in numROIs: allNet.append(dat[jj][roiIndx[numStart]]) numStart += 1 numROIs = numROIs[1:] return allNet
def getNetworkWithin_test(in_d, roiIndx): dat=in_d.copy() #Null the upper triangle, so that you don't get the redundant and #the diagonal values: idx_null = triu_indices(dat.shape[0]) dat[idx_null] = np.nan #Extract network values allNet=[] numROIs=roiIndx[1:] numStart=0 while len(numROIs)>0: for jj in numROIs: allNet.append(dat[jj][roiIndx[numStart]]) numStart+=1 numROIs=numROIs[1:] return allNet
def drawmatrix_channels(in_m, channel_names=None, fig=None, x_tick_rot=0, size=None, cmap=plt.cm.RdBu_r, colorbar=True, color_anchor=None, title=None): r"""Creates a lower-triangle of the matrix of an nxn set of values. This is the typical format to show a symmetrical bivariate quantity (such as correlation or coherence between two different ROIs). Parameters ---------- in_m: nxn array with values of relationships between two sets of rois or channels channel_names (optional): list of strings with the labels to be applied to the channels in the input. Defaults to '0','1','2', etc. fig (optional): a matplotlib figure cmap (optional): a matplotlib colormap to be used for displaying the values of the connections on the graph title (optional): string to title the figure (can be like '$\alpha$') color_anchor (optional): determine the mapping from values to colormap if None, min and max of colormap correspond to min and max of in_m if 0, min and max of colormap correspond to max of abs(in_m) if (a,b), min and max of colormap correspond to (a,b) Returns ------- fig: a figure object """ N = in_m.shape[0] ind = np.arange(N) # the evenly spaced plot indices def channel_formatter(x, pos=None): thisind = np.clip(int(x), 0, N - 1) return channel_names[thisind] if fig is None: fig = plt.figure() if size is not None: fig.set_figwidth(size[0]) fig.set_figheight(size[1]) w = fig.get_figwidth() h = fig.get_figheight() ax_im = fig.add_subplot(1, 1, 1) # If you want to draw the colorbar: if colorbar: divider = make_axes_locatable(ax_im) ax_cb = divider.new_vertical(size="10%", pad=0.1, pack_start=True) fig.add_axes(ax_cb) # Make a copy of the input, so that you don't make changes to the original # data provided m = in_m.copy() # Null the upper triangle, so that you don't get the redundant and the # diagonal values: idx_null = triu_indices(m.shape[0]) m[idx_null] = np.nan # Extract the minimum and maximum values for scaling of the # colormap/colorbar: max_val = np.nanmax(m) min_val = np.nanmin(m) if color_anchor is None: color_min = min_val color_max = max_val elif color_anchor == 0: bound = max(abs(max_val), abs(min_val)) color_min = -bound color_max = bound else: color_min = color_anchor[0] color_max = color_anchor[1] # The call to imshow produces the matrix plot: im = ax_im.imshow(m, origin='upper', interpolation='nearest', vmin=color_min, vmax=color_max, cmap=cmap) # Formatting: ax = ax_im ax.grid(True) # Label each of the cells with the row and the column: if channel_names is not None: for i in range(0, m.shape[0]): if i < (m.shape[0] - 1): ax.text(i - 0.3, i, channel_names[i], rotation=x_tick_rot) if i > 0: ax.text(-1, i + 0.3, channel_names[i], horizontalalignment='right') ax.set_axis_off() ax.set_xticks(np.arange(N)) ax.xaxis.set_major_formatter(ticker.FuncFormatter(channel_formatter)) fig.autofmt_xdate(rotation=x_tick_rot) ax.set_yticks(np.arange(N)) ax.set_yticklabels(channel_names) ax.set_ybound([-0.5, N - 0.5]) ax.set_xbound([-0.5, N - 1.5]) # Make the tick-marks invisible: for line in ax.xaxis.get_ticklines(): line.set_markeredgewidth(0) for line in ax.yaxis.get_ticklines(): line.set_markeredgewidth(0) ax.set_axis_off() if title is not None: ax.set_title(title) # The following produces the colorbar and sets the ticks if colorbar: # Set the ticks - if 0 is in the interval of values, set that, as well # as the maximal and minimal values: if min_val < 0: ticks = [color_min, min_val, 0, max_val, color_max] # Otherwise - only set the minimal and maximal value: else: ticks = [color_min, min_val, max_val, color_max] # This makes the colorbar: cb = fig.colorbar(im, cax=ax_cb, orientation='horizontal', cmap=cmap, norm=im.norm, boundaries=np.linspace(color_min, color_max, 256), ticks=ticks, format='%.2f') # Set the current figure active axis to be the top-one, which is the one # most likely to be operated on by users later on fig.sca(ax) return fig
def drawmatrix_channels(in_m, channel_names=None, fig=None, x_tick_rot=0, size=None, cmap=plt.cm.RdBu_r, colorbar=True, color_anchor=None, title=None): """Creates a lower-triangle of the matrix of an nxn set of values. This is the typical format to show a symmetrical bivariate quantity (such as correlation or coherence between two different ROIs). Parameters ---------- in_m: nxn array with values of relationships between two sets of rois or channels channel_names (optional): list of strings with the labels to be applied to the channels in the input. Defaults to '0','1','2', etc. fig (optional): a matplotlib figure cmap (optional): a matplotlib colormap to be used for displaying the values of the connections on the graph title : optional, string If given, title to be drawn atop the matrix. Returns ------- fig: a figure object """ N = in_m.shape[0] ind = np.arange(N) # the evenly spaced plot indices def channel_formatter(x, pos=None): thisind = np.clip(int(x), 0, N - 1) return channel_names[thisind] if fig is None: fig = plt.figure() if size is not None: fig.set_figwidth(size[0]) fig.set_figheight(size[1]) w = fig.get_figwidth() h = fig.get_figheight() ax_im = fig.add_subplot(1, 1, 1) #If you want to draw the colorbar: if colorbar: divider = make_axes_locatable(ax_im) ax_cb = divider.new_vertical(size="20%", pad=0.2, pack_start=True) fig.add_axes(ax_cb) #Make a copy of the input, so that you don't make changes to the original #data provided m = in_m.copy() #Null the upper triangle, so that you don't get the redundant and the #diagonal values: idx_null = triu_indices(m.shape[0]) m[idx_null] = np.nan #Extract the minimum and maximum values for scaling of the #colormap/colorbar: max_val = np.nanmax(m) min_val = np.nanmin(m) if color_anchor is None: color_min = min_val color_max = max_val elif color_anchor == 0: bound = max(abs(max_val), abs(min_val)) color_min = -bound color_max = bound else: color_min = color_anchor[0] color_max = color_anchor[1] #The call to imshow produces the matrix plot: im = ax_im.imshow(m, origin='upper', interpolation='nearest', vmin=color_min, vmax=color_max, cmap=cmap) #Formatting: ax = ax_im ax.grid(True) #Label each of the cells with the row and the column: if channel_names is not None: for i in xrange(0, m.shape[0]): if i < (m.shape[0] - 1): ax.text(i - 0.3, i, channel_names[i], rotation=x_tick_rot) if i > 0: ax.text(-1, i + 0.3, channel_names[i], horizontalalignment='right') ax.set_axis_off() ax.set_xticks(np.arange(N)) ax.xaxis.set_major_formatter(ticker.FuncFormatter(channel_formatter)) fig.autofmt_xdate(rotation=x_tick_rot) ax.set_yticks(np.arange(N)) ax.set_yticklabels(channel_names) ax.set_ybound([-0.5, N - 0.5]) ax.set_xbound([-0.5, N - 1.5]) #Make the tick-marks invisible: for line in ax.xaxis.get_ticklines(): line.set_markeredgewidth(0) for line in ax.yaxis.get_ticklines(): line.set_markeredgewidth(0) ax.set_axis_off() if title is not None: ax.set_title(title) #The following produces the colorbar and sets the ticks if colorbar: #Set the ticks - if 0 is in the interval of values, set that, as well #as the maximal and minimal values: if min_val < 0: ticks = [min_val, 0, max_val] #Otherwise - only set the minimal and maximal value: else: ticks = [min_val, max_val] #This makes the colorbar: cb = fig.colorbar(im, cax=ax_cb, orientation='horizontal', cmap=cmap, norm=im.norm, boundaries=np.linspace(min_val, max_val, 256), ticks=ticks, format='%.2f') # Set the current figure active axis to be the top-one, which is the one # most likely to be operated on by users later on fig.sca(ax) return fig
sxx = alg.mtm_cross_spectrum( tspectra[i], tspectra[i], (w[i], w[i]), sides='onesided' ).real syy = alg.mtm_cross_spectrum( tspectra[j], tspectra[j], (w[i], w[j]), sides='onesided' ).real psd_mat[0,i,j] = sxx psd_mat[1,i,j] = syy coh_mat[i,j] = np.abs(sxy)**2 coh_mat[i,j] /= (sxx * syy) csd_mat[i,j] = sxy if i != j: coh_var[i,j] = utils.jackknifed_coh_variance( tspectra[i], tspectra[j], weights=(w[i], w[j]), last_freq=L ) upper_idc = utils.triu_indices(nseq, k=1) lower_idc = utils.tril_indices(nseq, k=-1) coh_mat[upper_idc] = coh_mat[lower_idc] coh_var[upper_idc] = coh_var[lower_idc] # convert this measure with the normalizing function coh_mat_xform = utils.normalize_coherence(coh_mat, 2*K-2) t025_limit = coh_mat_xform + dist.t.ppf(.025, K-1)*np.sqrt(coh_var) t975_limit = coh_mat_xform + dist.t.ppf(.975, K-1)*np.sqrt(coh_var) utils.normal_coherence_to_unit(t025_limit, 2*K-2, t025_limit) utils.normal_coherence_to_unit(t975_limit, 2*K-2, t975_limit) if L < n_samples: