def plot_confidence_matrix( bird, block, segment, hemi, e1=5, e2=4, pfile="/auto/tdrive/mschachter/data/aggregate/decoders_pairwise_coherence_single.h5", ): # read the confusion matrix for this decoder pcf = AggregatePairwiseDecoder.load(pfile) i = ( (pcf.df["bird"] == bird) & (pcf.df["block"] == block) & (pcf.df["segment"] == segment) & (pcf.df["hemi"] == hemi) & (pcf.df["order"] == "self+cross") & (pcf.df["e1"] == e1) & (pcf.df["e2"] == e2) & (pcf.df["decomp"] == "locked") ) assert i.sum() == 1, "More than one model (i.sum()=%d)" % i.sum() index = pcf.df["index"][i].values[0] print "confidence_matrices.keys()=", pcf.confidence_matrices.keys() # shuffle the entries in the confidence matrix around key = ("locked", "self+cross", "pair") cnames = list(pcf.class_names[key][index]) print "cnames=", cnames cmats = pcf.confidence_matrices[key] print "cmats.shape=", cmats.shape C1 = cmats[index] C = np.zeros_like(C1) for i, cname1 in enumerate(cnames): for j, cname2 in enumerate(cnames): ii = DECODER_CALL_TYPES.index(cname1) jj = DECODER_CALL_TYPES.index(cname2) C[ii, jj] = C1[i, j] tick_labels = [CALL_TYPE_SHORT_NAMES[ct] for ct in DECODER_CALL_TYPES] # plot the confidence matrix figsize = (15.5, 11) fig = plt.figure(figsize=figsize) plt.imshow(C, origin="lower", interpolation="nearest", aspect="auto", vmin=0, vmax=1, cmap=plt.cm.afmhot) plt.xticks(range(len(CALL_TYPES)), tick_labels) plt.yticks(range(len(CALL_TYPES)), tick_labels) plt.axis("tight") plt.colorbar() plt.title("PCC=%0.2f" % (np.diag(C).mean())) fname = os.path.join(get_this_dir(), "confidence_matrix.svg") plt.savefig(fname, facecolor="w", edgecolor="none")
def reorder_confidence_matrix(C, class_names): Cro = np.zeros_like(C) cnames = list(class_names) for i,cname1 in enumerate(cnames): for j,cname2 in enumerate(cnames): ii = DECODER_CALL_TYPES.index(cname1) jj = DECODER_CALL_TYPES.index(cname2) Cro[ii, jj] = C[i, j] return Cro
def draw_category_perf_and_confusion(agg, df_me): df0 = df_me[df_me.band == 0] lfp_perfs = df0['perf_category_lfp'].values spike_perfs = df0['perf_category_spike'].values spike_rate_perfs = df0['perf_category_spike_rate'].values bp_data = {'Vocalization Type':[lfp_perfs, spike_perfs, spike_rate_perfs]} cmats = dict() for decomp in ['locked', 'spike_psd', 'spike_rate']: # compute average confusion matrix for spikes and LFP i = (agg.df.e1 == -1) & (agg.df.e2 == -1) & (agg.df.decomp == decomp) & (agg.df.band == 0) & \ (agg.df.aprop == 'category') & (agg.df.exfreq == False) & (agg.df.exel == False) print '%s, i.sum()=%d' % (decomp, i.sum()) df = agg.df[i] ci = df.cmat_index.values C = agg.confusion_matrices[ci, :, :] Cmean = C.mean(axis=0) cnames = agg.stim_class_names[ci][0] # reorder confusion matrix Cro = np.zeros_like(Cmean) for k,cname1 in enumerate(cnames): for j,cname2 in enumerate(cnames): i1 = DECODER_CALL_TYPES.index(cname1) i2 = DECODER_CALL_TYPES.index(cname2) Cro[i1, i2] = Cmean[k, j] cmats[decomp] = Cro figsize = (16, 12) fig = plt.figure(figsize=figsize) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.05, right=0.99, hspace=0.40, wspace=0.20) # gs = plt.GridSpec(1, 100) gs = plt.GridSpec(2, 2) # make a boxplot ax = plt.subplot(gs[0, 0]) grouped_boxplot(bp_data, subgroup_names=['LFP', 'Spike PSD', 'Spike Rate'], subgroup_colors=[COLOR_BLUE_LFP, COLOR_YELLOW_SPIKE, COLOR_RED_SPIKE_RATE], box_spacing=1.5, ax=ax) plt.xticks([]) plt.ylabel('PCC') plt.title('Vocalization Type Decoder Performance') # plot the mean LFP confusion matrix ax = plt.subplot(gs[0, 1]) plt.imshow(cmats['locked'], origin='lower', interpolation='nearest', aspect='auto', vmin=0, vmax=1, cmap=plt.cm.afmhot) xtks = [CALL_TYPE_SHORT_NAMES[ct] for ct in DECODER_CALL_TYPES] plt.xticks(range(len(DECODER_CALL_TYPES)), xtks) plt.yticks(range(len(DECODER_CALL_TYPES)), xtks) plt.colorbar(label='PCC') plt.title('Mean LFP Decoder Confusion Matrix') # plot the mean spike confusion matrix ax = plt.subplot(gs[1, 0]) plt.imshow(cmats['spike_psd'], origin='lower', interpolation='nearest', aspect='auto', vmin=0, vmax=1, cmap=plt.cm.afmhot) xtks = [CALL_TYPE_SHORT_NAMES[ct] for ct in DECODER_CALL_TYPES] plt.xticks(range(len(DECODER_CALL_TYPES)), xtks) plt.yticks(range(len(DECODER_CALL_TYPES)), xtks) plt.colorbar(label='PCC') plt.title('Mean Spike PSD Decoder Confusion Matrix') fname = os.path.join(get_this_dir(), 'perf_boxplots_category.svg') plt.savefig(fname, facecolor='w', edgecolor='none') # plot the mean spike rate confusion matrix ax = plt.subplot(gs[1, 1]) plt.imshow(cmats['spike_rate'], origin='lower', interpolation='nearest', aspect='auto', vmin=0, vmax=1, cmap=plt.cm.afmhot) xtks = [CALL_TYPE_SHORT_NAMES[ct] for ct in DECODER_CALL_TYPES] plt.xticks(range(len(DECODER_CALL_TYPES)), xtks) plt.yticks(range(len(DECODER_CALL_TYPES)), xtks) plt.colorbar(label='PCC') plt.title('Mean Spike Rate Decoder Confusion Matrix') fname = os.path.join(get_this_dir(), 'perf_boxplots_category.svg') plt.savefig(fname, facecolor='w', edgecolor='none')