Example #1
0
def estimate_bias_in_uncorrected_ppc(signal,times,window=50,Fs=1000,nrand=100):
    tried = []
    for i in range(nrand):
        ff,ppc = uncorrectedppc(phase_randomize(signal),times,window,Fs)
        tried.append(ppc)
    bias = mean(tried,0)
    return ff,bias
Example #2
0
def ppc_phase_randomize_chance_level_sample(
    signal,times,window=50,Fs=1000,k=4,multitaper=True,
    biased=False,delta=100,taper=None):
    '''
    signal: 1D real valued signal
    times:  Times of events relative to signal
    window: Time around event to examine
    Fs:     sample rate for computing freqs
    k:      number of tapers
    Also accepts lists of signals / times

    Uses phase randomization to sample from the null hypothesis distribution.
    Returns the actual PPC samples rather than any summary statistics.
    You can do what you want with the distribution returned.
    '''
    if multitaper:
        print("Warning: multitaper can introduce a bias into PPC that depends on the number of tapers!")
        print("For a fixed number of tapers, the bias is constant, but be careful")

    if not taper is None and multitaper:
        print("A windowing taper was specified, but multitaper mode was also selected.")
        print("The taper argument is for providing a windowing function when not using multitaper estimation.")
        assert 0
    if type(taper) is types.FunctionType:
        taper = taper(window*2+1)
    if biased: warn('skipping bias correction entirely')
    assert window>0

    # need to compute PPC from phase randomized samples
    # We can't phase randomize the snippits because there may be some
    # correlation between them, we have to randomize the underlying LFP

    if len(shape(signal))==1:
        # only a single trial provided
        usetimes = discard_spikes_closer_than_delta(signal,times,delta)
        signal = phase_randomize(signal)
        snippits = array([nodc(signal[t-window:t+window+1]) for t in usetimes])
    elif len(shape(signal))==2:
        warn('assuming first dimension is trials / repititions')
        signals,alltimes = signal,times
        snippits = []
        for signal,times in zip(signals,alltimes):
            signal = phase_randomize(signal)
            N = len(signal)
            times = array(times)
            times = times[times>=window]
            times = times[times<N-window]
            t_o = -inf
            for t in times:
                if t-t_o>delta:
                    t_o = t
                    snippits.append(nodc(signal[t-window:t+window+1]))
    else: assert 0

    if biased:
        if multitaper: return fftppc_biased_multitaper(snippits,Fs,k),snippits
        else:          return fftppc_biased(snippits,Fs,taper=taper),snippits
    else:
        if multitaper: return fftppc_multitaper(snippits,Fs,k),snippits
        else:          return fftppc(snippits,Fs,taper=taper),snippits
    assert 0