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 plot_psd_weights(agg, df): i = (df['decomp'] == 'locked') & (df['order'] == 'self') indices = df['psd_index'][i].values psds = agg.psds[('self', 'locked')][indices] psds = psds.reshape([psds.shape[0]*psds.shape[1]*psds.shape[2], psds.shape[3]]) psds = np.abs(psds) psd_sum = psds.sum(axis=1) zi = np.abs(psd_sum) == 0 psd_std = psds[~zi, :].std(axis=0, ddof=1) psd_mean = psds[~zi, :].mean(axis=0) psd_cv = psd_std / psd_mean freqs = agg.freqs[0] print 'freqs=',freqs fig = plt.figure(figsize=(24, 16)) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.05, right=0.99, hspace=0.40, wspace=0.20) ax = plt.subplot(2, 3, 1) plt.plot(freqs, psd_mean, 'k-', linewidth=2.0) plt.axis('tight') plt.xlabel('Frequency (Hz)') plt.ylabel('Mean') ax = plt.subplot(2, 3, 2) plt.plot(freqs, psd_std, 'k-', linewidth=7.0, alpha=0.7) plt.axis('tight') plt.xlabel('Frequency (Hz)') plt.ylabel('Model Abs. Weight SD') plt.title('Decoder Weight Variation') ax = plt.subplot(2, 3, 3) plt.plot(freqs, psd_cv, 'k-', linewidth=2.0) plt.axis('tight') plt.ylim(-25, 25) plt.xlabel('Frequency (Hz)') plt.ylabel('CV') fname = os.path.join(get_this_dir(), 'figs.svg') plt.savefig(fname, facecolor='w', edgecolor='none')
def draw_acoustic_perf_boxplots(agg, df_me): aprops_to_display = ['sal', 'meanspect', 'entropyspect', 'q2', 'maxAmp', 'skewtime'] aprops_names = {'sal':'sal', 'meanspect':'freq\nmean', 'entropyspect':'freq\nentropy', 'q2':'freq\nmedian', 'maxAmp':'max\namp', 'skewtime':'time\nskew'} decomps = ['self_spike_rate', 'self_locked', 'self+cross_locked'] sub_names = ['Spike Rate', 'LFP PSD', 'LFP Pairwise'] sub_clrs = [COLOR_RED_SPIKE_RATE, COLOR_BLUE_LFP, COLOR_PURPLE_LFP_CROSS] # decomps = ['self_locked', 'self+cross_locked'] # sub_names = ['LFP PSD', 'LFP Pairwise'] # sub_clrs = [COLOR_BLUE_LFP, COLOR_PURPLE_LFP_CROSS] bp_data = dict() for aprop in aprops_to_display: bd = list() for decomp in decomps: i = (df_me.decomp == decomp) & (df_me.aprop == aprop) perfs = df_me.r2[i].values bd.append(perfs) bp_data[aprops_names[aprop]] = bd figsize = (19, 3.5) 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) grouped_boxplot(bp_data, group_names=[aprops_names[aprop] for aprop in aprops_to_display], subgroup_names=sub_names, subgroup_colors=sub_clrs, box_spacing=1.) plt.xlabel('Acoustic Feature') plt.ylabel('Decoder R2') fname = os.path.join(get_this_dir(), 'perf_boxplots.svg') plt.savefig(fname, facecolor='w', edgecolor='none')
def draw_coherency_matrix( bird, block, segment, hemi, stim_id, trial, syllable_index, data_dir="/auto/tdrive/mschachter/data", exp=None, save=True, ): # load up the experiment if exp is None: bird_dir = os.path.join(data_dir, bird) exp_file = os.path.join(bird_dir, "%s.h5" % bird) stim_file = os.path.join(bird_dir, "stims.h5") exp = Experiment.load(exp_file, stim_file) seg = exp.get_segment(block, segment) # get the start and end times of the stimulus etable = exp.get_epoch_table(seg) i = etable["id"] == stim_id stim_times = zip(etable[i]["start_time"].values, etable[i]["end_time"].values) stim_times.sort(key=operator.itemgetter(0)) start_time, end_time = stim_times[trial] stim_dur = float(end_time - start_time) # get a slice of the LFP lfp_data = exp.get_lfp_slice(seg, start_time, end_time) electrode_indices, lfps, sample_rate = lfp_data[hemi] # rescale the LFPs to they are in uV lfps *= 1e6 # get the log spectrogram of the stimulus stim_spec_t, stim_spec_freq, stim_spec = exp.get_spectrogram_slice(seg, start_time, end_time) stim_spec_t = np.linspace(0, stim_dur, len(stim_spec_t)) stim_spec_dt = np.diff(stim_spec_t)[0] nz = stim_spec > 0 stim_spec[nz] = 20 * np.log10(stim_spec[nz]) + 100 stim_spec[stim_spec < 0] = 0 # get the amplitude envelope amp_env = stim_spec.std(axis=0, ddof=1) amp_env -= amp_env.min() amp_env /= amp_env.max() # segment the amplitude envelope into syllables merge_thresh = int(0.002 * sample_rate) events = break_envelope_into_events(amp_env, threshold=0.05, merge_thresh=merge_thresh) # translate the event indices into actual times events *= stim_spec_dt syllable_start, syllable_end, syllable_max_amp = events[syllable_index] syllable_si = int(syllable_start * sample_rate) syllable_ei = int(syllable_end * sample_rate) # compute all cross and auto-correlations if hemi == "L": electrode_order = ROSTRAL_CAUDAL_ELECTRODES_LEFT else: electrode_order = ROSTRAL_CAUDAL_ELECTRODES_RIGHT lags = np.arange(-20, 21) lags_ms = (lags / sample_rate) * 1e3 window_fraction = 0.35 noise_db = 25.0 nelectrodes = len(electrode_order) cross_mat = np.zeros([nelectrodes, nelectrodes, len(lags)]) for i in range(nelectrodes): for j in range(nelectrodes): lfp1 = lfps[i, syllable_si:syllable_ei] lfp2 = lfps[j, syllable_si:syllable_ei] if i != j: x = coherency(lfp1, lfp2, lags, window_fraction=window_fraction, noise_floor_db=noise_db) else: x = correlation_function(lfp1, lfp2, lags) _e1 = electrode_indices[i] _e2 = electrode_indices[j] i1 = electrode_order.index(_e1) i2 = electrode_order.index(_e2) # print 'i=%d, j=%d, e1=%d, e2=%d, i1=%d, i2=%d' % (i, j, _e1, _e2, i1, i2) cross_mat[i1, i2, :] = x # make a plot figsize = (24.0, 13.5) fig = plt.figure(figsize=figsize) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.03, right=0.99, hspace=0.10) gs = plt.GridSpec(nelectrodes, nelectrodes) for i in range(nelectrodes): for j in range(i + 1): ax = plt.subplot(gs[i, j]) plt.axhline(0, c="k") plt.axvline(0, c="k") _e1 = electrode_order[i] _e2 = electrode_order[j] plt.plot(lags_ms, cross_mat[i, j, :], "k-", linewidth=2.0) plt.xticks([]) plt.yticks([]) plt.axis("tight") plt.ylim(-0.5, 1.0) if j == 0: plt.ylabel("E%d" % electrode_order[i]) if i == nelectrodes - 1: plt.xlabel("E%d" % electrode_order[j]) if save: fname = os.path.join(get_this_dir(), "coherency_matrix.svg") plt.savefig(fname, facecolor="w", edgecolor="none")
def draw_figures(): pfile = '/auto/tdrive/mschachter/data/aggregate/decoders_pairwise_coherence_multi_freq.h5' agg = AggregatePairwiseDecoder.load(pfile) nbands = agg.df['band'].max() sample_rate = 381.4697265625 freqs = get_freqs(sample_rate) g = agg.df.groupby(['bird', 'block', 'segment', 'hemi']) """ # TODO: compute the average likelihood ratio between intecept-only and full model for all sites! i = (agg.df['bird'] == 'GreBlu9508M') & (agg.df['block'] == 'Site4') & (agg.df['segment'] == 'Call1') & (agg.df['hemi'] == 'L') & (agg.df['band'] == 0) assert i.sum() == 1 full_likelihood_for_null = agg.df['likelihood'][i].values[0] null_likelihood = 1.63 # for GreBlu9508_Site4_Call1_L null_likelihood_ratio = 2*(null_likelihood - full_likelihood_for_null) print 'full_likelihood_for_null=',full_likelihood_for_null print 'null_likelihood=',null_likelihood print 'null_likelihood_ratio=',null_likelihood_ratio """ full_likelihoods = list() likelihood_ratios = list() pccs = list() pcc_thresh = 0.25 single_band_likelihoods = list() single_band_pccs = list() for (bird,block,seg,hemi),gdf in g: # get the likelihood of the fullmodel i = gdf['band'] == 0 assert i.sum() == 1 num_samps = gdf[i]['num_samps'].values[0] print 'num_samps=%d' % num_samps full_likelihood = -gdf[i]['likelihood'].values[0] * num_samps pcc = gdf[i]['pcc'].values[0] if pcc < pcc_thresh: continue full_likelihoods.append(full_likelihood) pccs.append(pcc) # get the likelihood per frequency band ratio_by_band = np.zeros(nbands) single_likelihood_by_band = np.zeros(nbands) single_pcc_band = np.zeros(nbands) for k in range(nbands): i = (gdf['band'] == k+1) & (gdf['exfreq'] == True) assert i.sum() == 1 num_samps2 = gdf[i]['num_samps'].values[0] assert num_samps2 == num_samps leftout_likelihood = -gdf[i]['likelihood'].values[0] * num_samps i = (gdf['band'] == k+1) & (gdf['exfreq'] == False) assert i.sum() == 1 num_samps3 = gdf[i]['num_samps'].values[0] assert num_samps3 == num_samps2 single_leftout_likelihood = -gdf[i]['likelihood'].values[0] * num_samps pcc = gdf[i]['pcc'].values[0] print '(%s,%s,%s,%s,%d) leftout=%0.6f, full=%0.6f, single=%0.6f, single_pcc=%0.6f, num_samps=%d' % \ (bird, block, seg, hemi, k, leftout_likelihood, full_likelihood, single_leftout_likelihood, pcc, num_samps) # compute the likelihood ratio lratio = -2*(leftout_likelihood - full_likelihood) ratio_by_band[k] = lratio single_likelihood_by_band[k] = single_leftout_likelihood single_pcc_band[k] = pcc likelihood_ratios.append(ratio_by_band) single_band_likelihoods.append(single_likelihood_by_band) single_band_pccs.append(single_pcc_band) pccs = np.array(pccs) likelihood_ratios = np.array(likelihood_ratios) full_likelihoods = np.array(full_likelihoods) single_band_likelihoods = np.array(single_band_likelihoods) single_band_pccs = np.array(single_band_pccs) # exclude segments whose likelihood ratio goes below zero # i = np.array([np.any(lrat < 0) for lrat in likelihood_ratios]) i = np.ones(len(likelihood_ratios), dtype='bool') print 'i.sum()=%d' % i.sum() # compute significance threshold x = np.linspace(1, 150, 1000) df = 12 p = chi2.pdf(x, df) sig_thresh = max(x[p > 0.01]) # compute mean and std lrat_mean = likelihood_ratios[i, :].mean(axis=0) lrat_std = likelihood_ratios[i, :].std(axis=0, ddof=1) single_l_mean = single_band_likelihoods[i, :].mean(axis=0) single_l_std = single_band_likelihoods[i, :].std(axis=0, ddof=1) single_pcc_mean = single_band_pccs[i, :].mean(axis=0) single_pcc_std = single_band_pccs[i, :].std(axis=0, ddof=1) fig = plt.figure(figsize=(24, 16)) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.05, right=0.99, hspace=0.40, wspace=0.20) ax = plt.subplot(2, 3, 1) plt.plot(full_likelihoods, pccs, 'go', linewidth=2.0) plt.xlabel('log Likelihood') plt.ylabel('PCC') plt.axis('tight') ax = plt.subplot(2, 3, 2) for k,lrat in enumerate(likelihood_ratios[i, :]): plt.plot(freqs, lrat, '-', linewidth=2.0, alpha=0.7) plt.xlabel('Frequency (Hz)') plt.ylabel('Likelihood Ratio') plt.axis('tight') ax = plt.subplot(2, 3, 4) nsamps = len(likelihood_ratios) plt.errorbar(freqs, single_pcc_mean, yerr=single_pcc_std/np.sqrt(nsamps), ecolor='r', elinewidth=3.0, fmt='k-', linewidth=7.0, alpha=0.75) plt.xlabel('Frequency (Hz)') plt.ylabel('PCC') plt.title('Mean Single Band Decoder PCC') plt.axis('tight') ax = plt.subplot(2, 3, 5) plt.errorbar(freqs, single_l_mean, yerr=single_l_std/np.sqrt(nsamps), ecolor='r', elinewidth=3.0, fmt='k-', linewidth=7.0, alpha=0.75) plt.xlabel('Frequency (Hz)') plt.ylabel('log Likelihood') plt.title('Mean Single Band Decoder Likelihood') plt.axis('tight') ax = plt.subplot(2, 3, 6) plt.errorbar(freqs, lrat_mean, yerr=lrat_std/np.sqrt(nsamps), ecolor='r', elinewidth=3.0, fmt='k-', linewidth=7.0, alpha=0.75) plt.plot(freqs, np.ones_like(freqs)*sig_thresh, 'k--', linewidth=7.0, alpha=0.75) plt.xlabel('Frequency (Hz)') plt.ylabel('Likelihood Ratio') plt.title('Mean Likelihood Ratio') plt.axis('tight') plt.ylim(0, lrat_mean.max()) fname = os.path.join(get_this_dir(), 'figs.svg') plt.savefig(fname, facecolor='w', edgecolor='none') plt.show()
def plot_perf_bars(agg, df, perf='pcc'): g = df.groupby(['order', 'decomp']) aic_mean = dict() aic_std = dict() perf_mean = dict() perf_std = dict() nclasses = len(agg.class_names[0]) nelectrodes = len(agg.index2electrode[0]) nfreqs = len(agg.freqs[0]) nlags = len(agg.lags[0]) print 'nfreqs=%d, nlags=%d' % (nfreqs, nlags) nsamps = 0 for (otype,decomp),gdf in g: nparams = 0 if 'self' in otype: nparams += nfreqs*nelectrodes if 'cross' in otype: nparams += nlags*((nelectrodes**2 - nelectrodes) / 2) if decomp == 'total': nparams *= 2 nparams *= nclasses nsamps = len(gdf) print '%s/%s, nparams=%d, nsamps=%d' % (otype, decomp, nparams, nsamps) ll = -gdf.likelihood ll *= gdf.num_samps aic = 2*(nparams - ll) aic /= 1e3 aic_mean[(otype, decomp)] = aic.mean() aic_std[(otype, decomp)] = aic.std(ddof=1) perf_mean[(otype, decomp)] = gdf[perf].mean() perf_std[(otype, decomp)] = gdf[perf].std(ddof=1) decomps = ['locked', 'nonlocked', 'total'] orders = ['self', 'cross', 'self+cross'] nonlocked_means_aic = np.array([aic_mean[(o, 'nonlocked')] for o in orders]) nonlocked_stds_aic = np.array([aic_std[(o, 'nonlocked')] for o in orders]) locked_means_aic = np.array([aic_mean[(o, 'locked')] for o in orders]) locked_stds_aic = np.array([aic_std[(o, 'locked')] for o in orders]) total_means_aic = np.array([aic_mean[(o, 'total')] for o in orders]) total_stds_aic = np.array([aic_std[(o, 'total')] for o in orders]) nonlocked_means_perf = np.array([perf_mean[(o, 'nonlocked')] for o in orders]) nonlocked_stds_perf = np.array([perf_std[(o, 'nonlocked')] for o in orders]) locked_means_perf = np.array([perf_mean[(o, 'locked')] for o in orders]) locked_stds_perf = np.array([perf_std[(o, 'locked')] for o in orders]) total_means_perf = np.array([perf_mean[(o, 'total')] for o in orders]) total_stds_perf = np.array([perf_std[(o, 'total')] for o in orders]) # compute mean confusion matrix for self,total class_names = agg.class_names[0] i = (df.order == 'self+cross') & (df.decomp == 'total') indices = df['index'][i] Cmats = agg.confusion_matrices[indices] Cmean = Cmats.mean(axis=0) Cro = reorder_confidence_matrix(Cmean, class_names) aic_mean = np.mean(np.diag(Cro)) decomp_map = {'locked':'Trial-averaged', 'nonlocked':'Mean-subtracted', 'total':'Both'} decomp_leg = [decomp_map[d] for d in decomps] # make plots x = np.arange(3) + 1 fig = plt.figure(figsize=(24, 6)) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.05, right=0.99, hspace=0.30, wspace=0.20) ax = plt.subplot(1, 3, 1) plt.bar(x, locked_means_perf, width=0.2, yerr=nonlocked_stds_perf/np.sqrt(nsamps), facecolor='k', ecolor='k') plt.bar(x+0.25, nonlocked_means_perf, width=0.2, yerr=locked_stds_perf/np.sqrt(nsamps), facecolor='w', alpha=0.75, ecolor='k') plt.bar(x+0.5, total_means_perf, width=0.2, yerr=total_stds_perf/np.sqrt(nsamps), facecolor='#C0C0C0', alpha=0.75, ecolor='k') plt.axis('tight') plt.xlim(0.65, 4) plt.xticks(x+0.35, orders) plt.legend(decomp_leg, fontsize='x-small', loc=4) plt.title('Multi-electrode Performance') plt.ylabel(perf) ax = plt.subplot(1, 3, 2) plt.bar(x, locked_means_aic, width=0.2, yerr=nonlocked_stds_aic/np.sqrt(nsamps), facecolor='k', ecolor='k') plt.bar(x+0.25, nonlocked_means_aic, width=0.2, yerr=locked_stds_aic/np.sqrt(nsamps), facecolor='w', alpha=0.75, ecolor='k') plt.bar(x+0.5, total_means_aic, width=0.2, yerr=total_stds_aic/np.sqrt(nsamps), facecolor='#C0C0C0', alpha=0.75, ecolor='k') plt.axis('tight') plt.xlim(0.65, 4) plt.xticks(x+0.35, orders) plt.legend(decomp_leg, fontsize='x-small', loc=4) plt.title('Model Complexity') plt.ylabel('AIC * 1e-3') ax = plt.subplot(1, 3, 3) plt.imshow(Cro, 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 Confusion (self+cross, total) PCC=%0.2f' % aic_mean) fname = os.path.join(get_this_dir(), 'model_goodness.svg') plt.savefig(fname, facecolor='w', edgecolor='none') plt.show()
def draw_figures(bird, block, segment, hemi, e1, e2, stim_id, syllable_index, data_dir='/auto/tdrive/mschachter/data', exp=None): # load up the experiment if exp is None: bird_dir = os.path.join(data_dir, bird) exp_file = os.path.join(bird_dir, '%s.h5' % bird) stim_file = os.path.join(bird_dir, 'stims.h5') exp = Experiment.load(exp_file, stim_file) seg = exp.get_segment(block, segment) # get the start and end times of the stimulus etable = exp.get_epoch_table(seg) i = etable['id'] == stim_id stim_times = zip(etable[i]['start_time'].values, etable[i]['end_time'].values) stim_times.sort(key=operator.itemgetter(0)) stim_times = np.array(stim_times) stim_durs = stim_times[:, 1] - stim_times[:, 0] stim_dur = stim_durs.min() # aggregate the LFPs across trials lfps = list() sample_rate = None electrode_indices = None for start_time,end_time in stim_times: # get a slice of the LFP lfp_data = exp.get_lfp_slice(seg, start_time, end_time) electrode_indices,the_lfps,sample_rate = lfp_data[hemi] stim_dur_i = int(stim_dur*sample_rate) lfps.append(the_lfps[:, :stim_dur_i]) lfps = np.array(lfps) # rescale the LFPs to they are in uV lfps *= 1e6 # get the log spectrogram of the stimulus start_time_0 = stim_times[0][0] end_time_0 = stim_times[0][1] stim_spec_t,stim_spec_freq,stim_spec = exp.get_spectrogram_slice(seg, start_time_0, end_time_0) stim_spec_t = np.linspace(0, stim_dur, len(stim_spec_t)) stim_spec_dt = np.diff(stim_spec_t)[0] nz = stim_spec > 0 stim_spec[nz] = 20*np.log10(stim_spec[nz]) + 100 stim_spec[stim_spec < 0] = 0 # get the amplitude envelope amp_env = stim_spec.std(axis=0, ddof=1) amp_env -= amp_env.min() amp_env /= amp_env.max() # segment the amplitude envelope into syllables merge_thresh = int(0.002*sample_rate) events = break_envelope_into_events(amp_env, threshold=0.05, merge_thresh=merge_thresh) # translate the event indices into actual times events *= stim_spec_dt syllable_start,syllable_end,syllable_max_amp = events[syllable_index] syllable_start -= 0.005 syllable_end += 0.010 amp_env_rs = amp_env*(stim_spec_freq.max() - stim_spec_freq.min()) + stim_spec_freq.min() last_syllable_end = events[-1, 1] + 0.025 i1 = electrode_indices.index(e1) i2 = electrode_indices.index(e2) lfp1 = lfps[:, i1, :] lfp2 = lfps[:, i2, :] # zscore the LFP lfp1 -= lfp1.mean() lfp1 /= lfp1.std(ddof=1) lfp2 -= lfp2.mean() lfp2 /= lfp2.std(ddof=1) ntrials,nelectrodes,nt = lfps.shape ntrials_to_plot = 5 t = np.arange(nt) / sample_rate # plot the stimulus and raw LFP for two electrodes figsize = (24.0, 10) fig = plt.figure(figsize=figsize) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.03, right=0.99, hspace=0.10) gs = plt.GridSpec(3, 100) ax = plt.subplot(gs[0, :60]) plot_spectrogram(stim_spec_t, stim_spec_freq, stim_spec, ax=ax, ticks=True, fmin=300, fmax=8000, colormap='SpectroColorMap', colorbar=False) # plt.plot(stim_spec_t, amp_env_rs, 'k-', linewidth=2.0, alpha=0.50) plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0) plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0) plt.axis('tight') plt.xlim(0, last_syllable_end) # plot the first LFP (all trials) ax = plt.subplot(gs[1, :60]) for k in range(ntrials_to_plot): plt.plot(t, lfp1[k, :], '-', linewidth=2.0, alpha=0.75) plt.plot(t, lfp1.mean(axis=0), 'k-', linewidth=3.0) plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0) plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0) plt.xlabel('Time (ms)') plt.ylabel('E%d (z-scored)' % e1) plt.axis('tight') plt.xlim(0, last_syllable_end) ax = plt.subplot(gs[2, :60]) for k in range(ntrials_to_plot): plt.plot(t, lfp2[k, :], '-', linewidth=2.0, alpha=0.75) plt.plot(t, lfp2.mean(axis=0), 'k-', linewidth=3.0) plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0) plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0) plt.xlabel('Time (ms)') plt.ylabel('E%d (z-scored)' % e2) plt.axis('tight') plt.xlim(0, last_syllable_end) # restrict the lfps to a single syllable print 'syllable_start=%f, syllable_end=%f' % (syllable_start, syllable_end) syllable_si = int(syllable_start*sample_rate) syllable_ei = int(syllable_end*sample_rate) lfp1 = lfp1[:, syllable_si:syllable_ei] lfp2 = lfp2[:, syllable_si:syllable_ei] # compute the trial averaged and mean subtracted lfps lfp1_mean,lfp1_ms = compute_avg_and_ms(lfp1) lfp2_mean,lfp2_ms = compute_avg_and_ms(lfp2) psd_stats = get_psd_stats(bird, block, segment, hemi) freqs, psd1, psd2, psd1_ms, psd2_ms, c12, c12_nonlocked, c12_total = compute_spectra_and_coherence_single_electrode(lfp1, lfp2, sample_rate, e1, e2, psd_stats=psd_stats) lags_ms = get_lags_ms(sample_rate) lfp_absmax = max(np.abs(lfp1_mean).max(), np.abs(lfp2_mean).max()) lfp_ms_absmax = max(np.abs(lfp1_ms).max(), np.abs(lfp2_ms).max()) ax = plt.subplot(gs[0, 65:80]) plt.axhline(0, c='k') plt.plot(t[syllable_si:syllable_ei], lfp1_mean, 'k-', linewidth=3.0, alpha=1.) plt.plot(t[syllable_si:syllable_ei], lfp2_mean, '-', c='#c0c0c0', linewidth=3.0) # plt.xlabel('Time (s)') plt.ylabel('Trial-avg LFP') leg = custom_legend(['k', '#c0c0c0'], ['E%d' % e1, 'E%d' % e2]) plt.legend(handles=leg, fontsize='x-small') plt.axis('tight') plt.ylim(-lfp_absmax, lfp_absmax) ax = plt.subplot(gs[0, 85:]) plt.axhline(0, c='k') plt.plot(t[syllable_si:syllable_ei], lfp1_ms, 'k-', linewidth=3.0, alpha=1.) plt.plot(t[syllable_si:syllable_ei], lfp2_ms, '-', c='#c0c0c0', linewidth=3.0) # plt.xlabel('Time (s)') plt.ylabel('Mean-sub LFP') plt.legend(handles=leg, fontsize='x-small') plt.axis('tight') plt.ylim(-lfp_ms_absmax, lfp_ms_absmax) psd_max = max(psd1.max(), psd2.max(), psd1_ms.max(), psd2_ms.max()) ax = plt.subplot(gs[1, 65:80]) plt.axhline(0, c='k') plt.plot(freqs, psd1, 'k-', linewidth=3.0) plt.plot(freqs, psd2, '-', c='#c0c0c0', linewidth=3.0) plt.xlabel('Time (s)') plt.ylabel('Trial-avg Power') plt.legend(handles=leg, fontsize='x-small', loc=2) plt.axis('tight') # plt.ylim(0, psd_max) ax = plt.subplot(gs[1, 85:]) plt.axhline(0, c='k') plt.plot(freqs, psd1_ms, 'k-', linewidth=3.0) plt.plot(freqs, psd2_ms, '-', c='#c0c0c0', linewidth=3.0) plt.xlabel('Time (s)') plt.ylabel('Mean-sub Power') plt.legend(handles=leg, fontsize='x-small', loc=2) plt.axis('tight') # plt.ylim(0, psd_max) ax = plt.subplot(gs[2, 65:80]) plt.axhline(0, c='k') plt.axvline(0, c='k') plt.plot(lags_ms, c12_total, 'k-', linewidth=3.0, alpha=0.75) plt.plot(lags_ms, c12, '-', c='r', linewidth=3.0, alpha=0.75) plt.plot(lags_ms, c12_nonlocked, '-', c='b', linewidth=3.0, alpha=0.75) plt.xlabel('Lags (ms)') plt.ylabel('Coherency') leg = custom_legend(['k', 'r', 'b'], ['Raw', 'Trial-avg', 'Mean-sub']) plt.legend(handles=leg, fontsize='x-small') plt.axis('tight') plt.ylim(-0.2, 0.3) fname = os.path.join(get_this_dir(), 'raw+coherency.svg') plt.savefig(fname, facecolor='w', edgecolor='none')
def draw_freq_lkrats(agg, df_me): # aprops_to_display = ['category', 'maxAmp', 'meanspect', 'stdspect', 'q1', 'q2', 'q3', 'skewspect', 'kurtosisspect', # 'sal', 'entropyspect', 'meantime', 'stdtime', 'entropytime'] #aprops_to_display = ['category', 'maxAmp', 'sal', # 'entropytime', 'meanspect', 'entropyspect', # 'q1', 'q2', 'q3'] aprops_to_display = ['category', 'maxAmp', 'sal', 'q1', 'q2', 'q3'] assert isinstance(agg, AggregateLFPAndSpikePSDDecoder) freqs = agg.freqs nbands = len(freqs) # compute the significance threshold for each site, use it to normalize the likelihood ratio i = agg.df.bird != 'BlaBro09xxF' g = agg.df[i].groupby(['bird', 'block', 'segment', 'hemi']) num_sites = len(g) normed_lkrat_lfp = dict() normed_lkrat_spike = dict() for aprop in aprops_to_display: normed_lkrat_lfp[aprop] = np.zeros([num_sites, nbands]) normed_lkrat_spike[aprop] = np.zeros([num_sites, nbands]) for k,((bird,block,segment,hemi),gdf) in enumerate(g): i = (gdf.e1 != -1) & (gdf.e1 == gdf.e2) & (gdf.cell_index != -1) & (gdf.decomp == 'spike_psd') & \ (gdf.exel == False) & (gdf.exfreq == False) & (gdf.aprop == 'q2') ncells = i.sum() # print '%s,%s,%s,%s # of cells: %d' % (bird, block, segment, hemi, ncells) # get the likelihood ratios and normalize them by threshold for aprop in aprops_to_display: for b in range(1, nbands+1): i = (df_me.bird == bird) & (df_me.block == block) & (df_me.segment == segment) & (df_me.hemi == hemi) & \ (df_me.band == b) if i.sum() != 1: print 'i.sum()=%d, b=%d, (%s,%s,%s,%s)' % (i.sum(), b, bird, block, segment, hemi) assert i.sum() == 1 lkrats_lfp = df_me[i]['lkrat_%s_%s' % (aprop, 'lfp')].values[0] lkrats_spike = df_me[i]['lkrat_%s_%s' % (aprop, 'spike')].values[0] normed_lkrat_lfp[aprop][k, b-1] = lkrats_lfp normed_lkrat_spike[aprop][k, b-1] = lkrats_spike # make a list of data for multi plot plist = list() for aprop in aprops_to_display: plist.append({'lfp':normed_lkrat_lfp[aprop], 'spike':normed_lkrat_spike[aprop], 'freqs':freqs, 'aprop':aprop}) def _plot_freqs(pdata, ax): plt.sca(ax) nsamps_lfp = len(pdata['lfp']) lkrat_lfp_mean = pdata['lfp'].mean(axis=0) lkrat_lfp_std = pdata['lfp'].std(axis=0, ddof=1) / np.sqrt(nsamps_lfp) nsamps_spike = len(pdata['spike']) lkrat_spike_mean = pdata['spike'].mean(axis=0) lkrat_spike_std = pdata['spike'].std(axis=0, ddof=1) / np.sqrt(nsamps_spike) if pdata['aprop'] != 'category': plt.axhline(1.0, c='k', linestyle='dashed', alpha=0.7, linewidth=2.0) plt.errorbar(pdata['freqs'], lkrat_lfp_mean, yerr=lkrat_lfp_std, c=COLOR_BLUE_LFP, linewidth=7.0, alpha=0.9, ecolor='k', elinewidth=2.0) plt.errorbar(pdata['freqs']+2., lkrat_spike_mean, yerr=lkrat_spike_std, c=COLOR_YELLOW_SPIKE, linewidth=7.0, alpha=0.9, ecolor='k', elinewidth=2.0) plt.xlabel('Frequency (Hz)') plt.ylabel('Normalized LR') leg = custom_legend([COLOR_BLUE_LFP, COLOR_YELLOW_SPIKE], ['LFP', 'Spike']) plt.legend(handles=leg, fontsize='x-small') plt.title(pdata['aprop']) plt.axis('tight') if pdata['aprop'] != 'category': plt.ylim(0, 5) else: plt.axis('tight') figsize = (24, 9) multi_plot(plist, _plot_freqs, nrows=2, ncols=3, hspace=0.45, wspace=0.45, facecolor='w', figsize=figsize) fname = os.path.join(get_this_dir(), 'perf_by_freq.svg') plt.savefig(fname, facecolor='w', edgecolor='none')
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')
def draw_figures(bird, block, segment, hemi, e1, e2, stim_id, trial, syllable_index, data_dir='/auto/tdrive/mschachter/data', exp=None): # load up the experiment if exp is None: bird_dir = os.path.join(data_dir, bird) exp_file = os.path.join(bird_dir, '%s.h5' % bird) stim_file = os.path.join(bird_dir, 'stims.h5') exp = Experiment.load(exp_file, stim_file) seg = exp.get_segment(block, segment) # get the start and end times of the stimulus etable = exp.get_epoch_table(seg) i = etable['id'] == stim_id stim_times = zip(etable[i]['start_time'].values, etable[i]['end_time'].values) stim_times.sort(key=operator.itemgetter(0)) start_time,end_time = stim_times[trial] stim_dur = float(end_time - start_time) # get a slice of the LFP lfp_data = exp.get_lfp_slice(seg, start_time, end_time, zscore=True) electrode_indices,lfps,sample_rate = lfp_data[hemi] # get the log spectrogram of the stimulus stim_spec_t,stim_spec_freq,stim_spec = exp.get_spectrogram_slice(seg, start_time, end_time) stim_spec_t = np.linspace(0, stim_dur, len(stim_spec_t)) stim_spec_dt = np.diff(stim_spec_t)[0] nz = stim_spec > 0 stim_spec[nz] = 20*np.log10(stim_spec[nz]) + 100 stim_spec[stim_spec < 0] = 0 # get the amplitude envelope amp_env = stim_spec.std(axis=0, ddof=1) amp_env -= amp_env.min() amp_env /= amp_env.max() # segment the amplitude envelope into syllables merge_thresh = int(0.002*sample_rate) events = break_envelope_into_events(amp_env, threshold=0.05, merge_thresh=merge_thresh) # translate the event indices into actual times events *= stim_spec_dt syllable_start,syllable_end,syllable_max_amp = events[syllable_index] amp_env_rs = amp_env*(stim_spec_freq.max() - stim_spec_freq.min()) + stim_spec_freq.min() last_syllable_end = events[-1, 1] + 0.025 i1 = electrode_indices.index(e1) i2 = electrode_indices.index(e2) lfp1 = lfps[i1, :] lfp2 = lfps[i2, :] t = np.arange(len(lfp1)) / sample_rate legend = ['E%d' % e1, 'E%d' % e2] if hemi == 'L': electrode_order = ROSTRAL_CAUDAL_ELECTRODES_LEFT else: electrode_order = ROSTRAL_CAUDAL_ELECTRODES_RIGHT # get the power spectrum stats for this site psd_stats = get_psd_stats(bird, block, segment, hemi) # compute the power spectra and cross coherence for all electrodes lags_ms = get_lags_ms(sample_rate) spectra,cross_mat = compute_spectra_and_coherence_multi_electrode_single_trial(lfps, sample_rate, electrode_indices, electrode_order, psd_stats=psd_stats) # plot the stimulus and raw LFP for two electrodes figsize = (24.0, 10) fig = plt.figure(figsize=figsize) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.03, right=0.99, hspace=0.10) ax = plt.subplot(2, 1, 1) plot_spectrogram(stim_spec_t, stim_spec_freq, stim_spec, ax=ax, ticks=True, fmin=300, fmax=8000, colormap='SpectroColorMap', colorbar=False) # plt.plot(stim_spec_t, amp_env_rs, 'k-', linewidth=2.0, alpha=0.50) plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0) plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0) plt.axis('tight') plt.xlim(0, last_syllable_end) ax = plt.subplot(2, 1, 2) plt.plot(t, lfp1, 'b-', linewidth=3.0) plt.plot(t, lfp2, 'r-', linewidth=3.0, alpha=0.7) plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0) plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0) plt.xlabel('Time (ms)') plt.ylabel('LFP (z-scored)') plt.legend(legend) plt.axis('tight') plt.xlim(0, last_syllable_end) fname = os.path.join(get_this_dir(), 'raw.svg') plt.savefig(fname, facecolor='w', edgecolor='none') # restrict the lfps to a single syllable syllable_si = int(syllable_start*sample_rate) syllable_ei = int(syllable_end*sample_rate) lfp1 = lfp1[syllable_si:syllable_ei] lfp2 = lfp2[syllable_si:syllable_ei] # plot the two power spectra psd_ub = 6 psd_lb = 0 i1 = electrode_order.index(e1) i2 = electrode_order.index(e2) a1 = spectra[i1, :] a2 = spectra[i2, :] freqs = get_freqs(sample_rate) figsize = (10.0, 4.0) fig = plt.figure(figsize=figsize) plt.subplots_adjust(top=0.90, bottom=0.10, left=0.10, right=0.99, hspace=0.10) ax = plt.subplot(1, 2, 1) plt.plot(freqs, a1, 'b-', linewidth=3.0) plt.plot(freqs, a2, 'r-', linewidth=3.0) plt.xlabel('Frequency (Hz)') plt.ylabel('Power (z-scored)') plt.title('Power Spectrum') handles = custom_legend(['b', 'r'], legend) plt.legend(handles=handles, fontsize='small') plt.axis('tight') plt.ylim(psd_lb, psd_ub) # plot the coherency cf_lb = -0.1 cf_ub = 0.3 coh = cross_mat[i1, i2, :] ax = plt.subplot(1, 2, 2) plt.axhline(0, c='k') plt.axvline(0, c='k') plt.plot(lags_ms, coh, 'g-', linewidth=3.0) plt.xlabel('Frequency (Hz)') plt.title('Coherency') plt.axis('tight') plt.ylim(cf_lb, cf_ub) fname = os.path.join(get_this_dir(), 'auto+cross.svg') plt.savefig(fname, facecolor='w', edgecolor='none') # compute all cross and auto-correlations nelectrodes = len(electrode_order) # make a plot figsize = (24.0, 13.5) fig = plt.figure(figsize=figsize) plt.subplots_adjust(top=0.95, bottom=0.05, left=0.03, right=0.99, hspace=0.10) gs = plt.GridSpec(nelectrodes, nelectrodes) for i in range(nelectrodes): for j in range(i+1): ax = plt.subplot(gs[i, j]) plt.axhline(0, c='k') plt.axvline(0, c='k') _e1 = electrode_order[i] _e2 = electrode_order[j] clr = 'k' if i == j: if _e1 == e1: clr = 'b' elif _e1 == e2: clr = 'r' else: if _e1 == e1 and _e2 == e2: clr = 'g' if i == j: plt.plot(freqs, spectra[i, :], '-', c=clr, linewidth=2.0) else: plt.plot(lags_ms, cross_mat[i, j, :], '-', c=clr, linewidth=2.0) plt.xticks([]) plt.yticks([]) plt.axis('tight') if i != j: plt.ylim(cf_lb, cf_ub) else: plt.axhline(0, c='k') plt.ylim(psd_lb, psd_ub) if j == 0: plt.ylabel('E%d' % electrode_order[i]) if i == nelectrodes-1: plt.xlabel('E%d' % electrode_order[j]) fname = os.path.join(get_this_dir(), 'cross_all.svg') plt.savefig(fname, facecolor='w', edgecolor='none')
def draw_figures(stim_event, stim_ids, syllable_indices, e1=5, e2=2): assert isinstance(stim_event, StimEventTransform) sample_rate = stim_event.lfp_sample_rate lags_ms = get_lags_ms(sample_rate) cross_functions_total = dict() cross_functions_locked = dict() cross_functions_nonlocked = dict() specs = dict() syllable_times = dict() stim_end_time = dict() hemi = ','.join(stim_event.rcg_names) # compute all cross and auto-correlations if hemi == 'L': electrode_order = ROSTRAL_CAUDAL_ELECTRODES_LEFT else: electrode_order = ROSTRAL_CAUDAL_ELECTRODES_RIGHT freqs = None nelectrodes = None index2electrode = stim_event.index2electrode seg_uname = stim_event.seg_uname bird,block,seg = seg_uname.split('_') psd_stats = get_psd_stats(bird, stim_event.block_name, stim_event.segment_name, hemi) for stim_id,syllable_index in zip(stim_ids, syllable_indices): lfp = stim_event.lfp_reps_by_stim['raw'][stim_id] ntrials,nelectrodes,nt = lfp.shape # get the start and end time of the syllable i = (stim_event.segment_df['stim_id'] == stim_id) & (stim_event.segment_df['order'] == syllable_index) assert i.sum() > 0, "No syllable for stim_id=%d, order=%d" % (stim_id, syllable_index) assert i.sum() == 1, "More than one syllable for stim_id=%d, order=%d" % (stim_id, syllable_index) start_time = stim_event.segment_df[i]['start_time'].values[0] end_time = stim_event.segment_df[i]['end_time'].values[0] syllable_times[stim_id] = (start_time, end_time) # get the end time of the last syllable i = (stim_event.segment_df['stim_id'] == stim_id) stim_end_time[stim_id] = stim_event.segment_df[i]['end_time'].max() si = int((stim_event.pre_stim_time + start_time)*sample_rate) ei = int((stim_event.pre_stim_time + end_time)*sample_rate) # restrict the lfp to just the syllable time lfp = lfp[:, :, si:ei] specs[stim_id] = stim_event.spec_by_stim[stim_id] i1 = index2electrode.index(e1) lfp1 = lfp[:, i1, :] i2 = index2electrode.index(e2) lfp2 = lfp[:, i2, :] # compute the covariance functions pfreq, psd1, psd2, psd1_ms, psd2_ms, c12, c12_nonlocked, c12_total = compute_spectra_and_coherence_single_electrode(lfp1, lfp2, sample_rate, e1, e2, psd_stats=psd_stats) cross_functions_total[stim_id] = (psd1, psd2, c12_total) cross_functions_locked[stim_id] = (psd1, psd2, c12) cross_functions_nonlocked[stim_id] = (psd1, psd2, c12_nonlocked) # plot the cross covariance functions to overlap for each stim plot_cross_pair(stim_ids, cross_functions_total, electrode_order, freqs, lags_ms) plt.suptitle('Total Covariance') fname = os.path.join(get_this_dir(), 'cross_total.svg') plt.savefig(fname, facecolor='w', edgecolor='none') # plot_cross_mat(stim_ids, cross_functions_locked, electrode_order, freqs) plot_cross_pair(stim_ids, cross_functions_locked, electrode_order, freqs, lags_ms) plt.suptitle('Stim-locked Covariance') fname = os.path.join(get_this_dir(), 'cross_locked.svg') plt.savefig(fname, facecolor='w', edgecolor='none') # plot_cross_mat(stim_ids, cross_functions_nonlocked, electrode_order, freqs) plot_cross_pair(stim_ids, cross_functions_nonlocked, electrode_order, freqs, lags_ms) plt.suptitle('Non-locked Covariance') fname = os.path.join(get_this_dir(), 'cross_nonlocked.svg') plt.savefig(fname, facecolor='w', edgecolor='none') # plot the spectrograms fig_height = 2 fig_max_width = 6 spec_lens = np.array([stim_end_time[stim_id] for stim_id in stim_ids]) spec_ratios = spec_lens / spec_lens.max() spec_sample_rate = sample_rate for k,stim_id in enumerate(stim_ids): spec = specs[stim_id] syllable_start,syllable_end = syllable_times[stim_id] print 'stim_id=%d, syllable_start=%0.3f, syllable_end=%0.3f' % (stim_id, syllable_start, syllable_end) spec_t = np.arange(spec.shape[1]) / spec_sample_rate stim_end = stim_end_time[stim_id] figsize = (spec_ratios[k]*fig_max_width, fig_height) fig = plt.figure(figsize=figsize) ax = plt.gca() plot_spectrogram(spec_t, stim_event.spec_freq, spec, ax=ax, ticks=True, fmin=300., fmax=8000., colormap='SpectroColorMap', colorbar=False) plt.axvline(syllable_start, c='k', linestyle='dashed', linewidth=3.0) plt.axvline(syllable_end, c='k', linestyle='dashed', linewidth=3.0) plt.xlim(0, stim_end+0.005) fname = os.path.join(get_this_dir(), 'stim_spec_%d.svg' % stim_id)
def draw_figures(): d = get_full_data('GreBlu9508M', 'Site4', 'Call1', 'L', 287) syllable_index = 1 syllable_start = d['syllable_props'][syllable_index]['start_time'] - 0.020 syllable_end = d['syllable_props'][syllable_index]['end_time'] + 0.030 sr = d['lfp_sample_rate'] lfp_mean = d['lfp'].mean(axis=0) lfp_t = np.arange(lfp_mean.shape[1]) / sr nelectrodes,nt = lfp_mean.shape lfp_i = (lfp_t >= syllable_start) & (lfp_t <= syllable_end) electrode_order = d['electrode_order'] # compute the cross coherency between each pair of electrodes nelectrodes = 16 lags = np.arange(-20, 21) lags_ms = (lags / sr)*1e3 nlags = len(lags) window_fraction = 0.60 noise_floor_db = 25, cross_coherency = np.zeros([nelectrodes, nelectrodes, nlags]) for i in range(nelectrodes): for j in range(nelectrodes): if i == j: continue lfp1 = lfp_mean[i, lfp_i] lfp2 = lfp_mean[j, lfp_i] cross_coherency[i, j, :] = coherency(lfp1, lfp2, lags, window_fraction=window_fraction, noise_floor_db=noise_floor_db) figsize = (24, 13) fig = plt.figure(figsize=figsize) fig.subplots_adjust(top=0.95, bottom=0.02, right=0.97, left=0.03, hspace=0.20, wspace=0.20) gs = plt.GridSpec(nelectrodes, nelectrodes) for k in range(nelectrodes): for j in range(k): ax = plt.subplot(gs[k, j]) plt.axhline(0, c='k') plt.plot(lags_ms, cross_coherency[k, j], 'k-', linewidth=2.0, alpha=0.8) plt.axis('tight') plt.ylim(-.25, 0.5) plt.yticks([]) plt.xticks([]) if k == nelectrodes-1: plt.xlabel('E%d' % electrode_order[j]) xtks = [-40, 0, 40] plt.xticks(xtks, ['%d' % x for x in xtks]) if j == 0: plt.ylabel('E%d' % electrode_order[k]) ytks = [-0.2, 0.4] plt.yticks(ytks, ['%0.1f' % x for x in ytks]) ax = plt.subplot(gs[:7, (nelectrodes-8):]) voffset = 5 for n in range(nelectrodes): plt.plot(lfp_t, lfp_mean[nelectrodes-n-1, :] + voffset*n, 'k-', linewidth=3.0, alpha=0.75) plt.axis('tight') ytick_locs = np.arange(nelectrodes) * voffset plt.yticks(ytick_locs, list(reversed(d['electrode_order']))) plt.ylabel('Electrode') plt.axvline(syllable_start, c='k', linestyle='--', linewidth=3.0, alpha=0.7) plt.axvline(syllable_end, c='k', linestyle='--', linewidth=3.0, alpha=0.7) plt.xlabel('Time (s)') fname = os.path.join(get_this_dir(), 'figure.svg') plt.savefig(fname, facecolor='w', edgecolor='none') plt.show()