def plotISIhistHz(session,area,unit,start,stop,
    FS=1000,style='bar',color='k',nbins=30,label=None,
    fmin=0,FMAX=100,
    linestyle='-',lw=2,w=0.8):
    ByTrialSpikesMS = metaloadvariable(session,area,'ByTrialSpikesMS')
    NUNITS, NTRIALS = shape(ByTrialSpikesMS)
    isi = []
    for it in range(NTRIALS):
        isi.extend(diff(getTrial(session,area,unit,start,stop,it+1)))
    isi = FS/array(isi)
    if style=='bar':
        hist(isi,linspace(fmin,FMAX,nbins+1),facecolor='k',label=label,rwidth=w)
        a,b = ylim()
        plot((modefind(isi),)*2,ylim(),color='r',lw=lw)
        ylim(a,b)
        nicelimits()
        isi = isi[~isnan(isi)]
        isi = isi[~isinf(isi)]
        mf = modefind(isi)
        if (not isnan(mf)) and (not isinf(mf)):
            xticks(int32([xlim()[0],int(round(modefind(isi))),xlim()[1]]))
    elif style=='line':
        h,_ = histogram(isi,bins=nbins,range=(0,100))
        x = linspace(fmin,FMAX,1+nbins)
        x = 0.5*(x[1:]+x[:-1])
        plot(x,h,linestyle,lw=1,color=color,label=label)
    else:
        raise ValueError("Plot style must be line or bar")
    bareaxis(gca())
    xlabel('Frequency (Hz)')
    ylabel('N')
    title('ISI histogram\n(frequency)')
    gca().yaxis.labelpad = -20
def plotISIhist(session,area,unit,start,stop):
    cla()
    ByTrialSpikesMS = metaloadvariable(session,area,'ByTrialSpikesMS')
    NUNITS,NTRIALS = shape(ByTrialSpikesMS)
    isi = []
    for it in range(NTRIALS):
        isi.extend(diff(getTrial(session,area,unit,start,stop,it+1)))
    hist(isi,linspace(0,120,31),facecolor='k')
    a,b = ylim()
    plot((modefind(isi),)*2,ylim(),color='r',lw=2)
    ylim(a,b)
    nicelimits()
    xticks(int32([xlim()[0],int(round(modefind(isi))),xlim()[1]]))
    bareaxis(gca())
    xlabel('Time (ms)')
    ylabel('N')
    title('ISI histogram (time)')
    gca().yaxis.labelpad = -20
def isimodefreq(session,area,unit,start,stop,FS=1000):
    ByTrialSpikesMS = metaloadvariable(session,area,'ByTrialSpikesMS')
    NUNITS,NTRIALS = shape(ByTrialSpikesMS)
    isi = []
    for it in range(NTRIALS):
        isi.extend(diff(getTrial(session,area,unit,start,stop,it+1)))
    isi = FS/array(isi)
    isi = isi[~isnan(isi)]
    isi = isi[~isinf(isi)]
    mf = modefind(isi)
    return mf
def do_unit_ISI_plot(session, area, unit,
    INFOXPOS = 70, LABELSIZE=8, NBINS=20, FMAX = 250):
    cla()
    spikes = []
    for trial in get_good_trials(session):
        spikes.append(cgid.spikes.get_spikes_event(
            session,area,unit,trial,6,-1000,0))
        spikes.append(cgid.spikes.get_spikes_event(
            session,area,unit,trial,8,-1000,0))
    ISI_events = array(list(flatten(map(diff,spikes))))
    SNR        = get_unit_SNR(session,area,unit)
    histc,edges = histogram(ISI_events, bins = linspace(0,FMAX,NBINS+1))
    dx         = diff(edges)[0]
    bar(edges[:-1]+dx*0.1,histc,width=dx*0.8,color=GATHER[-1],edgecolor=(0,)*4)
    allisi = array(ISI_events)
    K   = 20
    x,y = kdepeak(log(K+allisi[allisi>0]))
    x   = exp(x)-K
    y   = y/(K+x)
    y   = y*len(allisi)*dx
    plot(x,y,color=RUST,lw=1.5)
    mean_rate           = sum(map(len,spikes))/float(len(get_good_trials(session))*2)
    ISI_cv              = std(allisi)/mean(allisi)
    burstiness          = sum(allisi<5)/float(len(allisi))*100
    ll                  = 1./mean(allisi)
    expected_short_isi  = (1.0-exp(-ll*10))*100
    mode                = modefind(allisi)
    residual_burstiness = burstiness-expected_short_isi
    LH = LABELSIZE+4
    text(INFOXPOS,ylim()[1]-pixels_to_yunits(20   ),'Mean rate = %d Hz'%mean_rate,
        horizontalalignment='left',
        verticalalignment  ='bottom',fontsize=LABELSIZE)
    text(INFOXPOS,ylim()[1]-pixels_to_yunits(20+LH*1),'ISI CV = %0.2f'%ISI_cv,
        horizontalalignment='left',
        verticalalignment  ='bottom',fontsize=LABELSIZE)
    text(INFOXPOS,ylim()[1]-pixels_to_yunits(20+LH*2),'SNR = %0.1f'%SNR,
        horizontalalignment='left',
        verticalalignment  ='bottom',fontsize=LABELSIZE)
    text(INFOXPOS,ylim()[1]-pixels_to_yunits(20+LH*3),'Mode freq. = %0.1f'%mode,
        horizontalalignment='left',
        verticalalignment  ='bottom',fontsize=LABELSIZE)
    axvline(mode,lw=2,color=TURQUOISE)
    xlabel('ms',fontsize=LABELSIZE)
    ylabel('No. Events',fontsize=LABELSIZE)
    fudgey(10)
    fudgex(5)
    xlim(0,FMAX)
    nicex()
    nicey()
    title('Monkey %s area %s\nsession %s unit %s'%(session[0],area,session[-2:],unit),loc='center',fontsize=7)
    return mean_rate,ISI_cv,SNR,mode