Example #1
0
def refineSleepFromAccel(acceleration, sleep_ep):
    vl = acceleration[0].restrict(sleep_ep)
    vl = vl.as_series().diff().abs().dropna()
    a, _ = scipy.signal.find_peaks(vl, 0.025)
    peaks = nts.Tsd(vl.iloc[a])
    duration = np.diff(peaks.as_units('s').index.values)
    interval = nts.IntervalSet(start=peaks.index.values[0:-1],
                               end=peaks.index.values[1:])

    newsleep_ep = interval.iloc[duration > 15.0]
    newsleep_ep = newsleep_ep.reset_index(drop=True)
    newsleep_ep = newsleep_ep.merge_close_intervals(100000, time_units='us')

    #newsleep_ep	= sleep_ep.intersect(newsleep_ep)

    return newsleep_ep
Example #2
0
def path_plot(eps, position):
    #fig=figure(figsize=(15,16))
    fig = figure()
    for i in range(len(eps)):
        if len(eps) == 1:
            ax = subplot()
        else:
            ax = subplot(1, len(eps), i + 1)
        ep = eps.iloc[i]
        ep = nts.IntervalSet(ep[0], ep[1])
        plot(position['x'].restrict(ep),
             position['z'].restrict(ep),
             color='red',
             label=str(i),
             alpha=0.5)
        legend()
Example #3
0
def loadUFOs(path):
    """
	Name of the file should end with .evt.py.ufo
	"""
    import os
    name = path.split("/")[-1]
    files = os.listdir(path)
    filename = os.path.join(path, name + '.evt.py.ufo')
    if name + '.evt.py.ufo' in files:
        tmp = np.genfromtxt(path + '/' + name + '.evt.py.ufo')[:, 0]
        ripples = tmp.reshape(len(tmp) // 3, 3) / 1000
    else:
        print("No ufo in ", path)
        sys.exit()
    return (nts.IntervalSet(ripples[:, 0], ripples[:, 2], time_units='s'),
            nts.Ts(ripples[:, 1], time_units='s'))
def transition(states, template, epsilon=0):
    """
    author: BK
    states : dict of nts.Interval_set
    template : list of state.
    epsilon : int, will drop any 
     in which there is an epoch shorter than epsilon 's'
    This function will find transition that match the template 
    """
    if epsilon is list:
        print("eplist")
    long = pd.DataFrame()
    for s, i in states.items():
        i["state"] = s
        long = pd.concat((i, long))
        del i["state"]
    order = np.argsort(long.start)
    long = long.iloc[order]

    transition_times = []
    transition_intervals = []
    for i, s in enumerate(long.state):
        tmp = list(long.state[i:i + len(template)])
        if tmp == template:
            tmp_transition = long.iloc[i:i + len(template)]
            #             print(d.iloc[i:i+len(template)])
            length = (tmp_transition.end - tmp_transition.start) / 1_000_000
            if np.any(length.values < epsilon):
                continue
            tmp_pre = np.array(tmp_transition.end[:-1])
            tmp_post = np.array(tmp_transition.start[1:])
            tmp_times = np.mean([tmp_pre, tmp_post], 0)

            transition_intervals.append(
                [tmp_transition.start.iloc[0], tmp_transition.end.iloc[-1]])
            transition_times.append(tmp_times)

    transition_times = np.array(transition_times)
    transition_intervals = np.array(transition_intervals)
    transition_intervals = nts.IntervalSet(
        start=transition_intervals[:, 0],
        end=transition_intervals[:, 1],
        force_no_fix=True,
    )
    return transition_intervals, transition_times
def intervals(intervals,
              col='orange',
              alpha=0.5,
              time_units='s',
              ymin=0,
              ymax=1):

    if type(intervals) != nts.interval_set.IntervalSet:
        print(type(intervals))
        intervals = nts.IntervalSet(intervals['start'], intervals['end'])

    for interval in intervals.as_units(time_units).values:
        plt.axvspan(interval[0],
                    interval[1],
                    facecolor=col,
                    alpha=alpha,
                    ymin=ymin,
                    ymax=ymax)
Example #6
0
def freezing_intervals(speed,threshold, mode='single_speed',clean = False, t_merge = 0.5,t_drop = 1,save = False):
    
    """
        BK 8/11/20
        Input 
            speed: speed vector as output by bk.compute.speed (not yet implemented. But it's an nts.frame)
            treshold: arbritary units
    """
    
    
    if mode.lower() =='single_speed':
        fs =  1/scipy.stats.mode(np.diff(speed.as_units('s').index)).mode[0]
        freezing = speed.values<threshold
        
        if freezing[0] == 1: freezing[0] = 0
        if freezing[-1] == 1: freezing = np.append(freezing,0)

        dfreeze = np.diff(freezing.astype(np.int8))
        start = np.where(dfreeze == 1)[0]/fs + speed.as_units('s').index[0]
        end = np.where(dfreeze == -1)[0]/fs + speed.as_units('s').index[0]
    elif mode.lower() == 'multiple_speed':
        fs =  1/scipy.stats.mode(np.diff(speed.as_units('s').index)).mode[0]
        freezing = np.array((np.sum(speed.as_units('s'),axis = 1))/speed.shape[1] < threshold)
        
        if freezing[0] == 1: freezing[0] = 0
        if freezing[-1] == 1: freezing = np.append(freezing,0)

        dfreeze = np.diff(freezing.astype(np.int8))
        start = np.where(dfreeze == 1)[0]/fs + speed.as_units('s').index[0]
        end = np.where(dfreeze == -1)[0]/fs + speed.as_units('s').index[0]
    elif mode.lower() == 'pca':
        print('not implanted')
    else:
        print('Mode not recognized')
        return False
    freezing_intervals = nts.IntervalSet(start,end,time_units = 's')
    if clean:
        freezing_intervals = freezing_intervals.merge_close_intervals(t_merge,time_units = 's').drop_short_intervals(t_drop,time_units = 's')
        
    
    if save:
        np.save('freezing_intervals',freezing_intervals,allow_pickle = True)
    
    return freezing_intervals
def states():
    # BK : 17/09/2020
    # Return a dict with variable from States.
    #     if session_path == 0 : session_path = get_session_path(session_name)
    states = scipy.io.loadmat(path + "/States.mat")

    useless = ["__header__", "__version__", "__globals__"]
    for u in useless:
        del states[u]
    states_ = {}
    for state in states:
        states_.update({
            state:
            nts.IntervalSet(states[state][:, 0],
                            states[state][:, 1],
                            time_units="s")
        })

    return states_
Example #8
0
def optoeps(ttl_start, ttl_end, height=1):
    """
    This function help you to get the start and end times of the diferent 
    light intensities of the stimulation. It achieves this by looking for
    big differences in time among the TTL pulses. 

    Parameters
    ----------
    ttl_start : neuroseries.time_series.Ts
        start time of all the ttl
    ttl_end : neuroseries.time_series.Ts
        DESCRIPTION.
    height : float, int
        Height of the peak, the time difference in seconds between 2 TTL pulses.
        The default is 1.

    Returns
    -------
    stim_ep : 
        an interval set with the beginning and end of the different 
    intensities of the stimulation

    """
    t, _ = find_peaks(np.diff(ttl_start.as_units('s').index), height)
    stim_ep = np.sort(
        np.hstack(([ttl_start.index[0]], ttl_start.index[t],
                   ttl_start.index[t + 1], ttl_end.index[-1])))
    stim_ep = stim_ep.reshape(len(stim_ep) // 2, 2)
    stim_ep = nts.IntervalSet(start=stim_ep[:, 0], end=stim_ep[:, 1])
    plt.figure()
    plot(ttl_start.index)
    [axhline(stim_ep.loc[i, 'start']) for i in stim_ep.index]
    [axhline(stim_ep.loc[i, 'end']) for i in stim_ep.index]
    plt.plot(ttl_start.index)
    plt.ylabel("time")
    plt.title("Stimulation epochs at different intensities")
    plt.show()
    return stim_ep
Example #9
0
def f_ang_tcurves(cell_id,ep, spikes, position):
    '''computes the tcurve for a single cell each time the animal samples all angular bins
    inputs: cell_id= an integer corresponding to the cell column name in the tcurve data frame.
            ep= epoch (intervalSet)
            spikes= dictionary of spike times
            position=position['ry']
            
    outputs: fig
            peak_frate in each epoch
    '''
    ang_ep=full_ang(ep,position)
    eps=np.arange(len(ang_ep))
    fig=plt.figure()
    frates=pd.DataFrame(index=eps,columns=[0])
    sz=int(len(eps)/7)+1  
    for j,q in enumerate(eps): #epoch of interest 
        subplot(sz,7,j+1, projection='polar')
        f_ang_ep1=nts.IntervalSet(start=ang_ep.loc[j,'start'],end=ang_ep.loc[j,'end'])
        tcuve_f1=computeAngularTuningCurves(spikes,position,f_ang_ep1)
        frates.iloc[j,0]=tcuve_f1[cell_id].max()
        plt.plot(tcuve_f1[cell_id],label=str(j),color='k') 
        legend()
    return fig,frates
Example #10
0
    def testRange(self, data_class):
        self.tsd = data_class(self.tsd_t, self.tsd_d)
        range_interval = nts.IntervalSet(9.e8, 3.e9)

        int1_r = self.int1.intersect(range_interval)
        tsd_r = self.tsd.restrict(range_interval)
        tsd_r_r1 = self.tsd.restrict(int1_r)

        with nts.Range(range_interval):
            np.testing.assert_array_almost_equal_nulp(self.tsd.r.times(),
                                                      tsd_r.times())
            np.testing.assert_array_almost_equal_nulp(self.int1.r['start'],
                                                      int1_r['start'])
            np.testing.assert_array_almost_equal_nulp(self.int1.r['end'],
                                                      int1_r['end'])

            np.testing.assert_array_almost_equal_nulp(
                self.tsd.r.restrict(self.int1.r).times(), tsd_r_r1.times())

            # testing caching
            self.assertIsNotNone(self.tsd.r_cache)
            self.assertIsNotNone(self.int1.r_cache)
            np.testing.assert_array_almost_equal_nulp(self.tsd.r.times(),
                                                      tsd_r.times())
            np.testing.assert_array_almost_equal_nulp(self.int1.r['start'],
                                                      int1_r['start'])
            np.testing.assert_array_almost_equal_nulp(self.int1.r['end'],
                                                      int1_r['end'])
            np.testing.assert_array_almost_equal_nulp(
                self.tsd.r.restrict(self.int1.r).times(), tsd_r_r1.times())

        self.assertIsNone(self.tsd.r_cache)
        self.assertIsNone(self.int1.r_cache)

        with nts.Range(9.e8, 3.e9):
            np.testing.assert_array_almost_equal_nulp(self.tsd.r.times(),
                                                      tsd_r.times())
Example #11
0
def loadEpoch(path, epoch, episodes=None):
    """
	load the epoch contained in path	
	If the path contains a folder analysis, the function will load either the BehavEpochs.mat or the BehavEpochs.h5
	Run makeEpochs(data_directory, ['sleep', 'wake', 'sleep', 'wake'], file='Epoch_TS.csv') to create the BehavEpochs.h5

	Args:
		path: string
		epoch: string

	Returns:
		neuroseries.IntervalSet
	"""
    if not os.path.exists(path):  # Check for path
        print("The path " + path + " doesn't exist; Exiting ...")
        sys.exit()
    filepath = os.path.join(path, 'Analysis')
    if os.path.exists(filepath):  # Check for path/Analysis/
        listdir = os.listdir(filepath)
        file = [f for f in listdir if 'BehavEpochs' in f]
    if len(file) == 0:  # Running makeEpochs
        makeEpochs(path, episodes, file='Epoch_TS.csv')
        listdir = os.listdir(filepath)
        file = [f for f in listdir if 'BehavEpochs' in f]
    if file[0] == 'BehavEpochs.h5':
        new_file = os.path.join(filepath, 'BehavEpochs.h5')
        store = pd.HDFStore(new_file, 'r')
        if '/' + epoch in store.keys():
            ep = store[epoch]
            store.close()
            return nts.IntervalSet(ep)
        else:
            print("The file BehavEpochs.h5 does not contain the key " + epoch +
                  "; Exiting ...")
            sys.exit()
    elif file[0] == 'BehavEpochs.mat':
        behepochs = scipy.io.loadmat(os.path.join(filepath, file[0]))
        if epoch == 'wake':
            wake_ep = np.hstack(
                [behepochs['wakeEp'][0][0][1], behepochs['wakeEp'][0][0][2]])
            return nts.IntervalSet(wake_ep[:, 0],
                                   wake_ep[:, 1],
                                   time_units='s').drop_short_intervals(0.0)
        elif epoch == 'sleep':
            sleep_pre_ep, sleep_post_ep = [], []
            if 'sleepPreEp' in behepochs.keys():
                sleep_pre_ep = behepochs['sleepPreEp'][0][0]
                sleep_pre_ep = np.hstack([sleep_pre_ep[1], sleep_pre_ep[2]])
                sleep_pre_ep_index = behepochs['sleepPreEpIx'][0]
            if 'sleepPostEp' in behepochs.keys():
                sleep_post_ep = behepochs['sleepPostEp'][0][0]
                sleep_post_ep = np.hstack([sleep_post_ep[1], sleep_post_ep[2]])
                sleep_post_ep_index = behepochs['sleepPostEpIx'][0]
            if len(sleep_pre_ep) and len(sleep_post_ep):
                sleep_ep = np.vstack((sleep_pre_ep, sleep_post_ep))
            elif len(sleep_pre_ep):
                sleep_ep = sleep_pre_ep
            elif len(sleep_post_ep):
                sleep_ep = sleep_post_ep
            return nts.IntervalSet(sleep_ep[:, 0],
                                   sleep_ep[:, 1],
                                   time_units='s')
        ###################################
        # WORKS ONLY FOR MATLAB FROM HERE #
        ###################################
        elif epoch == 'sws':
            sampling_freq = 1250
            new_listdir = os.listdir(path)
            for f in new_listdir:
                if 'sts.SWS' in f:
                    sws = np.genfromtxt(os.path.join(path,
                                                     f)) / float(sampling_freq)
                    return nts.IntervalSet.drop_short_intervals(
                        nts.IntervalSet(sws[:, 0], sws[:, 1], time_units='s'),
                        0.0)

                elif '-states.mat' in f:
                    sws = scipy.io.loadmat(os.path.join(path, f))['states'][0]
                    index = np.logical_or(sws == 2, sws == 3) * 1.0
                    index = index[1:] - index[0:-1]
                    start = np.where(index == 1)[0] + 1
                    stop = np.where(index == -1)[0]
                    return nts.IntervalSet.drop_short_intervals(
                        nts.IntervalSet(start,
                                        stop,
                                        time_units='s',
                                        expect_fix=True), 0.0)

        elif epoch == 'rem':
            sampling_freq = 1250
            new_listdir = os.listdir(path)
            for f in new_listdir:
                if 'sts.REM' in f:
                    rem = np.genfromtxt(os.path.join(path,
                                                     f)) / float(sampling_freq)
                    return nts.IntervalSet(
                        rem[:, 0], rem[:, 1],
                        time_units='s').drop_short_intervals(0.0)

                elif '-states/m' in listdir:
                    rem = scipy.io.loadmat(path + f)['states'][0]
                    index = (rem == 5) * 1.0
                    index = index[1:] - index[0:-1]
                    start = np.where(index == 1)[0] + 1
                    stop = np.where(index == -1)[0]
                    return nts.IntervalSet(
                        start, stop, time_units='s',
                        expect_fix=True).drop_short_intervals(0.0)
Example #12
0
    return np.arange(ep.as_units('ms').start.iloc[0], ep.as_units('ms').end.iloc[-1], bin_size)

bins = makeBins(Epoch)
index = np.digitize(actual_pos.as_units('ms').index.values, bins)-1
down_actual_pos = actual_pos.groupby(index).mean()
down_actual_pos = pd.DataFrame(nts.Tsd(t = bins[0:-1]+np.diff(bins)/2, d =down_actual_pos.values[0:len(bins)-1], time_units = 'ms'))

#Compute Decoding Error
decoded_err=np.arctan2(np.sin(down_actual_pos-decoded_pos),np.cos(down_actual_pos-decoded_pos))

mean_decoded_err=np.abs(decoded_err).mean()

#decoded_err2=np.arctan2(np.sin(abs(down_actual_pos-decoded_pos)), np.cos(abs(down_actual_pos-decoded_pos)))
#mean_decoded_err2=decoded_err2.mean()

wake_ep_1_l2=nts.IntervalSet(start=wake_ep_ka43.loc[0,'end'] - 1.2e+8,end = wake_ep_ka43.loc[0,'end'])
#wake_ep_2_l2=nts.IntervalSet(start=wake_ep_ka30.loc[3,'end'] - 1.2e+8,end = wake_ep_ka30.loc[3,'end'])

#first 2mins
ac_pos_f2_idx=down_actual_pos.index < (down_actual_pos.index[0]+1.2e+8)
ac_pos_f2=down_actual_pos[ac_pos_f2_idx]
dec_pos_f2=decoded_pos[ac_pos_f2_idx]


##final 2mins
ac_pos_l2_idx=down_actual_pos.index > (down_actual_pos.index[-1]- 1.2e+8) 
ac_pos_l2=down_actual_pos[ac_pos_l2_idx]
dec_pos_l2=decoded_pos[ac_pos_l2_idx]

plt.figure(figsize=(16,7))
Example #13
0
    # spind_ep_thl	= spind_ep_thl.drop_short_intervals(200000).drop_long_intervals(3000000)
    # #count number of cycle, shoulb be superior to five peaks and troughs
    # peaks, troughs 	= getPeaksandTroughs(lfp_mean, 5)
    # index 		 	= np.zeros(len(spind_ep_thl))
    # for i in range(len(spind_ep_thl)):
    # 	n_peaks = len(peaks.restrict(nts.IntervalSet(spind_ep_thl.iloc[i]['start'], spind_ep_thl.iloc[i]['end'])))
    # 	n_troughs = len(troughs.restrict(nts.IntervalSet(spind_ep_thl.iloc[i]['start'], spind_ep_thl.iloc[i]['end'])))
    # 	if n_peaks >= 4 and n_troughs >= 4 : index[i] = 1
    # spind_ep_thl 	= spind_ep_thl[index==1]

    # writeNeuroscopeEvents("/mnt/DataGuillaume/MergedData/"+session+"/"+session.split("/")[1]+".evt.spd.thl", spind_ep_thl, "Spindles")
    spind_ep_thl = np.genfromtxt(data_directory + session + "/" +
                                 session.split("/")[1] + ".evt.spd.thl")[:, 0]
    spind_ep_thl = spind_ep_thl.reshape(len(spind_ep_thl) // 2, 2)
    spind_ep_thl = nts.IntervalSet(spind_ep_thl[:, 0],
                                   spind_ep_thl[:, 1],
                                   time_units='ms')

    # phase, pwr		= getPhase(lfp_hpc, 8, 18, 16, fs/5., power = True)
    # phase 			= phase.restrict(sws_ep)
    phase = phase_hpc

    spikes_spind = {n: spikes[n].restrict(spind_ep_thl) for n in spikes.keys()}
    spikes_phase = {
        n: phase.realign(spikes_spind[n], align='closest')
        for n in spikes_spind.keys()
    }

    # spind_thl_mod 		= np.ones((n_neuron,3))*np.nan
    spind_thl_mod = {}
    for n in range(len(spikes_phase.keys())):
Example #14
0
generalinfo = scipy.io.loadmat(data_directory +
                               'Mouse12-120806_GeneralInfo.mat')
shankstructure = loadShankStructure(generalinfo)
spikes, shanks = loadSpikeData(data_directory + 'Mouse12-120806_SpikeData.mat',
                               shankstructure['thalamus'])
thalamus_index = list(spikes.keys())

#compute firing rates for each neuron for 0.1 second time bins during the first rem epoch
HD_index = loadHDCellInfo(data_directory + 'Mouse12-120806_HDCells.mat',
                          thalamus_index)
HD_spikes = {}
for i in HD_index:
    HD_spikes[i] = spikes[i]
rem_ep = loadEpoch(data_directory, 'rem')
first_rem = rem_ep.loc[0]
first_rem = nts.IntervalSet(start=first_rem['start'], end=first_rem['end'])
binsize = 0.1
rem_start = first_rem.as_units('s')['start'].values[0]
rem_end = first_rem.as_units('s')['end']
duration = rem_end - rem_start
duration = duration + 0.1
num_points = duration / binsize
num_points = int(num_points)
for j in HD_index:
    my_neuron = HD_spikes[j]
    my_neuron = my_neuron.restrict(first_rem)
    first_spike = my_neuron.as_units('s').index[0]
    last_spike = my_neuron.as_units('s').index[-1]
    firing_rate = np.zeros(num_points)
    for i in range(num_points):
        start = first_rem.as_units('s')['start'] + i * binsize
Example #15
0
for x,s in enumerate(idx2):
    path=info.dir[s].replace('\\',"/")
  
    ############################################################################################### 
    # LOADING DATA
    ###############################################################################################
    episodes = info.filter(like='T').loc[s]
    events  = list(np.where((episodes == cond1) | (episodes== cond2))[0].astype('str'))
    
    spikes, shank                       = loadSpikeData(path)
    #n_channels, fs, shank_to_channel   = loadXML(path)
    position                            = loadPosition(path, events, episodes)
    wake_ep                             = loadEpoch(path, 'wake', episodes)
    #sleep_ep                            =loadEpoch(path,'sleep',episodes)
    
    ep1=nts.IntervalSet(start=wake_ep.loc[int(events[0])-1,'start'], end =wake_ep.loc[int(events[0])-1,'start']+6e+8)
    ep2=nts.IntervalSet(start=wake_ep.loc[int(events[-1])-1,'start'], end =wake_ep.loc[int(events[-1])-1,'start']+6e+8)
    #ep3=nts.IntervalSet(start=wake_ep.loc[int(events[-1])-1,'start'], end =wake_ep.loc[int(events[-1])-1,'start']+3e+8)
        
    tcurv_1 = computeAngularTuningCurves(spikes,position['ry'],ep1,60)
    tcurv_2 = computeAngularTuningCurves(spikes,position['ry'],ep2,60)
    #tcurv_train=computeAngularTuningCurves(spikes,position['ry'],ep3,60)
    
    stats=findHDCells(tcurv_2,ep2,spikes,position['ry'])
    hd_cells=stats['hd_cells']==True
    
    mean_frate,peak_frate, pfd  = computeFiringRates(spikes,[ep1, ep2],[cond1,cond2],[tcurv_1,tcurv_2]) 
    all_pfd=all_pfd.append(pfd[hd_cells]) 
                       #Preferred Firing Dir

    #Temp Code
Example #16
0
def loadEpoch(path, epoch):
    import scipy.io
    import neuroseries as nts
    import os, sys
    sampling_freq = 1250
    listdir = os.listdir(path)
    for f in listdir:
        if "BehavEpochs" in f:
            behepochs = scipy.io.loadmat(path + f)

    if epoch == 'wake':
        wake_ep = np.hstack(
            [behepochs['wakeEp'][0][0][1], behepochs['wakeEp'][0][0][2]])
        return nts.IntervalSet(wake_ep[:, 0], wake_ep[:, 1],
                               time_units='s').drop_short_intervals(0.0)

    elif epoch == 'sleep':
        sleep_pre_ep, sleep_post_ep = [], []
        if 'sleepPreEp' in behepochs.keys():
            sleep_pre_ep = behepochs['sleepPreEp'][0][0]
            sleep_pre_ep = np.hstack([sleep_pre_ep[1], sleep_pre_ep[2]])
            sleep_pre_ep_index = behepochs['sleepPreEpIx'][0]
        if 'sleepPostEp' in behepochs.keys():
            sleep_post_ep = behepochs['sleepPostEp'][0][0]
            sleep_post_ep = np.hstack([sleep_post_ep[1], sleep_post_ep[2]])
            sleep_post_ep_index = behepochs['sleepPostEpIx'][0]
        if len(sleep_pre_ep) and len(sleep_post_ep):
            sleep_ep = np.vstack((sleep_pre_ep, sleep_post_ep))
        elif len(sleep_pre_ep):
            sleep_ep = sleep_pre_ep
        elif len(sleep_post_ep):
            sleep_ep = sleep_post_ep
        return nts.IntervalSet(sleep_ep[:, 0], sleep_ep[:, 1], time_units='s')

    elif epoch == 'sws':
        for f in listdir:
            if 'sts.SWS' in f:
                sws = np.genfromtxt(path + f) / float(sampling_freq)
                return nts.IntervalSet.drop_short_intervals(
                    nts.IntervalSet(sws[:, 0], sws[:, 1], time_units='s'), 0.0)

            elif '-states.mat' in f:
                sws = scipy.io.loadmat(path + f)['states'][0]
                index = np.logical_or(sws == 2, sws == 3) * 1.0
                index = index[1:] - index[0:-1]
                start = np.where(index == 1)[0] + 1
                stop = np.where(index == -1)[0]
                return nts.IntervalSet.drop_short_intervals(
                    nts.IntervalSet(start,
                                    stop,
                                    time_units='s',
                                    expect_fix=True), 0.0)

    elif epoch == 'rem':
        for f in listdir:
            if 'sts.REM' in f:
                rem = np.genfromtxt(path + f) / float(sampling_freq)
                return nts.IntervalSet(
                    rem[:, 0], rem[:, 1],
                    time_units='s').drop_short_intervals(0.0)

            elif '-states.mat' in listdir:
                rem = scipy.io.loadmat(path + f)['states'][0]
                index = (rem == 5) * 1.0
                index = index[1:] - index[0:-1]
                start = np.where(index == 1)[0] + 1
                stop = np.where(index == -1)[0]
                return nts.IntervalSet(
                    start, stop, time_units='s',
                    expect_fix=True).drop_short_intervals(0.0)
Example #17
0
    hd_info = scipy.io.loadmat(data_directory + session +
                               '/Analysis/HDCells.mat')['hdCellStats'][:, -1]
    hd_info_neuron = np.array([hd_info[n] for n in spikes.keys()])

    if np.sum(1 - hd_info_neuron) > 5:
        wake_ep = loadEpoch(data_directory + session, 'wake')
        sleep_ep = loadEpoch(data_directory + session, 'sleep')
        sws_ep = loadEpoch(data_directory + session, 'sws')
        rem_ep = loadEpoch(data_directory + session, 'rem')
        sleep_ep = sleep_ep.merge_close_intervals(threshold=1.e3)
        sws_ep = sleep_ep.intersect(sws_ep)
        rem_ep = sleep_ep.intersect(rem_ep)
        speed = loadSpeed(data_directory + session +
                          '/Analysis/linspeed.mat').restrict(wake_ep)
        speed_ep = nts.IntervalSet(
            speed[speed > 2.5].index.values[0:-1],
            speed[speed > 2.5].index.values[1:]).drop_long_intervals(
                26000).merge_close_intervals(50000)
        wake_ep = wake_ep.intersect(speed_ep).drop_short_intervals(3000000)
        n_channel, fs, shank_to_channel = loadXML(data_directory + session +
                                                  "/" + session.split("/")[1] +
                                                  '.xml')

        rip_ep, rip_tsd = loadRipples(data_directory + session)
        # rip_ep			= sws_ep.intersect(rip_ep)
        # rip_tsd 		= rip_tsd.restrict(rip_ep)

        # theta_rem_ep 		= np.genfromtxt(data_directory+session+"/"+session.split("/")[1]+".rem.evt.theta")[:,0]
        # theta_rem_ep 		= theta_rem_ep.reshape(len(theta_rem_ep)//2,2)
        # theta_rem_ep 		= nts.IntervalSet(theta_rem_ep[:,0], theta_rem_ep[:,1], time_units = 'ms')

        # theta_wake_ep 		= np.genfromtxt(data_directory+session+"/"+session.split("/")[1]+".wake.evt.theta")[:,0]
Example #18
0
gca().set_yticks([])




###Drift in cells
gs2=GridSpecFromSubplotSpec( 6,6,subplot_spec=gs[0:2,1:])
   
eps=np.arange(len(ang_ep))
pairs=[1,2,3,4,21]#21
cells_i=[1]
for s,q in enumerate(cells_i):
    cells=[q]
    #fig.suptitle('#Cell_' +str(q),fontsize=25)
    for j,q in enumerate(pairs): #epoch of interest 
        f_ang_ep1=nts.IntervalSet(start=ang_ep.loc[j,'start'],end=ang_ep.loc[j,'end'])
        tcuve_f1=computeAngularTuningCurves(spikess,positions['ry'],f_ang_ep1)   
        for i,x in enumerate(cells):
            subplot(gs2[0:2,j+1], projection='polar')
            title('Epoch'+str(j),fontsize=10)
            plt.plot(tcuve_f1[x],color='k',alpha=0.75)
            remove_polarAx(gca(),True)
            gca().set_yticks([])


 
cells_i=[2]
for s,q in enumerate(cells_i):
    cells=[q]
    #fig.suptitle('#Cell_' +str(q),fontsize=25)
    for j,q in enumerate(pairs): #epoch of interest 
Example #19
0
    hpc_channel, float(fs), 'int16')
lfp_hpc = downsample(lfp_hpc.restrict(wake_ep), 1, 5)
lfp_filt_hpc = nts.Tsd(lfp_hpc.index.values,
                       butter_bandpass_filter(lfp_hpc, 5, 15, fs / 5, 2))
power = nts.Tsd(lfp_filt_hpc.index.values, np.abs(lfp_filt_hpc.values))
enveloppe, dummy = getPeaksandTroughs(power, 5)

index = (enveloppe.as_series() > np.percentile(enveloppe, 10)).values * 1.0
start_cand = np.where((index[1:] - index[0:-1]) == 1)[0] + 1
end_cand = np.where((index[1:] - index[0:-1]) == -1)[0]
if end_cand[0] < start_cand[0]: end_cand = end_cand[1:]
if end_cand[-1] < start_cand[-1]: start_cand = start_cand[0:-1]
tmp = np.where(end_cand != start_cand)
start_cand = enveloppe.index.values[start_cand[tmp]]
end_cand = enveloppe.index.values[end_cand[tmp]]
theta_ep = nts.IntervalSet(start_cand, end_cand)
theta_ep = theta_ep.drop_short_intervals(300000)
theta_ep = theta_ep.merge_close_intervals(30000).drop_short_intervals(1000000)

phase = getPhase(lfp_hpc, 5, 15, 16, fs / 5.)

phase = phase.restrict(theta_ep)
phase = phase.as_series()
tmp = phase.values + (2 * np.pi)
tmp = tmp % (2 * np.pi)
phase = nts.Tsd(t=phase.index.values, d=tmp)

spikes_phase = {
    n: phase.realign(spikesnohd[n].restrict(theta_ep), align='closest')
    for n in spikesnohd.keys()
}
Example #20
0
                       center=True,
                       min_periods=1).mean(std=200)
ratio2 = nts.Tsd(t=ratio2.index.values, d=ratio2.values)

# ratio2.as_series().to_hdf('../figures/figures_poster_2019/ratio2.h5', 'w')

# 	index 			= (ratio2.as_series() > 0).values*1.0
index = (ratio2 > 0).values * 1.0
start_cand = np.where((index[1:] - index[0:-1]) == 1)[0] + 1
end_cand = np.where((index[1:] - index[0:-1]) == -1)[0]
if end_cand[0] < start_cand[0]: end_cand = end_cand[1:]
if end_cand[-1] < start_cand[-1]: start_cand = start_cand[0:-1]
tmp = np.where(end_cand != start_cand)
start_cand = ratio2.index.values[start_cand[tmp]]
end_cand = ratio2.index.values[end_cand[tmp]]
good_ep = nts.IntervalSet(start_cand, end_cand)
good_ep = newsleep_ep.intersect(good_ep)
good_ep = good_ep.drop_short_intervals(10, time_units='s')
good_ep = good_ep.reset_index(drop=True)
good_ep = good_ep.merge_close_intervals(5, time_units='s')

theta_rem_ep = good_ep
sws_ep = newsleep_ep.set_diff(theta_rem_ep)
sws_ep = sws_ep.merge_close_intervals(0).drop_short_intervals(0)

plot(ratio.restrict(newsleep_ep))
plot(ratio2.restrict(newsleep_ep))
[plot(theta_rem_ep.loc[i], np.zeros(2)) for i in theta_rem_ep.index]
[plot(sws_ep.loc[i], np.ones(2)) for i in sws_ep.index]
show()
# sys.exit()
Example #21
0
else:
    hpc_channel = generalinfo['channelStructure'][0][0][1][0][0][0][0] - 1
spikes, shank = loadSpikeData(
    data_directory + session + '/Analysis/SpikeData.mat',
    shankStructure['thalamus'])
wake_ep = loadEpoch(data_directory + session, 'wake')
sleep_ep = loadEpoch(data_directory + session, 'sleep')
sws_ep = loadEpoch(data_directory + session, 'sws')
rem_ep = loadEpoch(data_directory + session, 'rem')
sleep_ep = sleep_ep.merge_close_intervals(threshold=1.e3)
sws_ep = sleep_ep.intersect(sws_ep)
rem_ep = sleep_ep.intersect(rem_ep)
speed = loadSpeed(data_directory + session +
                  '/Analysis/linspeed.mat').restrict(wake_ep)
speed_ep = nts.IntervalSet(
    speed[speed > 2.5].index.values[0:-1],
    speed[speed > 2.5].index.values[1:]).drop_long_intervals(
        26000).merge_close_intervals(50000)
wake_ep = wake_ep.intersect(speed_ep).drop_short_intervals(3000000)
# to match main_make_SWRinfo.py
spikes = {
    n: spikes[n]
    for n in spikes.keys() if len(spikes[n].restrict(sws_ep))
}
n_neuron = len(spikes)
allneurons = [
    session.split("/")[1] + "_" + str(list(spikes.keys())[i])
    for i in spikes.keys()
]
n_channel, fs, shank_to_channel = loadXML(data_directory + session + "/" +
                                          session.split("/")[1] + '.xml')
theta_mod, theta_ses = loadThetaMod(
Example #22
0
session_spind_mod2 = {}
session_spind_mod3 = {}
session_spind_mod4 = {}
session_spind_mod5 = {}
session_spind_mod6 = {}
session_cross_corr = {}
session_cross_corr_swr_spikes_in_thl_spindles = {}
session_cross_corr_swr_spikes_out_thl_spindles = {}

for session in datasets:
    print("session" + session)
    sws_ep = loadEpoch(data_directory + session, 'sws')
    tmp = np.genfromtxt("/mnt/DataGuillaume/MergedData/" + session + "/" +
                        session.split("/")[1] + ".evt.spd.thl")[:, 0]
    tmp = tmp.reshape(len(tmp) // 2, 2)
    spind_thl_ep = nts.IntervalSet(tmp[:, 0], tmp[:, 1], time_units='ms')
    tmp = np.genfromtxt("/mnt/DataGuillaume/MergedData/" + session + "/" +
                        session.split("/")[1] + ".evt.spd.hpc")[:, 0]
    tmp = tmp.reshape(len(tmp) // 2, 2)
    spind_hpc_ep = nts.IntervalSet(tmp[:, 0], tmp[:, 1], time_units='ms')
    spind_ep = spind_hpc_ep.intersect(spind_thl_ep).drop_short_intervals(0.0)
    spind_thl_no_hpc = spind_thl_ep.set_diff(
        spind_hpc_ep).drop_short_intervals(0.0)
    spind_hpc_no_thl = spind_hpc_ep.set_diff(
        spind_thl_ep).drop_short_intervals(0.0)
    store = pd.HDFStore("../data/phase_spindles/" + session.split("/")[1] +
                        ".lfp")
    phase_hpc = nts.Tsd(store['phase_hpc_spindles'])
    phase_thl = nts.Tsd(store['phase_thl_spindles'][0])
    store.close()
    spikes = {}
Example #23
0
all_dark_tc = []

###############################################################################
###Data Processing
##############################################################################
for x, s in enumerate(idx2):
    path = info.dir[s].replace('\\', "/")

    ###############################################################################################
    # LOADING DATA
    ###############################################################################################
    episodes = info.filter(like='T').loc[s]
    events = list(
        np.where((episodes == cond1) | (episodes == cond2))[0].astype('str'))

    spikes, shank = loadSpikeData(path)
    #n_channels, fs, shank_to_channel   = loadXML(path)
    position = loadPosition(path, events, episodes)
    wake_ep = loadEpoch(path, 'wake', episodes)

    ep1 = nts.IntervalSet(start=wake_ep.loc[int(events[0]) - 1, 'start'],
                          end=wake_ep.loc[int(events[0]) - 1, 'end'])
    ep2 = nts.IntervalSet(start=wake_ep.loc[int(events[-1]) - 1, 'start'],
                          end=wake_ep.loc[int(events[-1]) - 1, 'end'])

    tcurv_1 = computeAngularTuningCurves(spikes, position['ry'], ep1, 60)
    tcurv_2 = computeAngularTuningCurves(spikes, position['ry'], ep2, 60)

    stats = findHDCells(tcurv_2, ep2, spikes, position['ry'])
    hd = stats['hd_cells'] == True
Example #24
0
import sys
from pycircstat.descriptive import mean as circmean
import _pickle as cPickle

##############################################################
### LOAD PROCESSED DATA
##############################################################
path = r'F:\EphysData\Experiments\190425_\KA28-190425\KA28-190425'

episodes = pd.Series(['wake'])
events = list(np.where(episodes == 'wake')[0].astype('str'))
spikes, shank = loadSpikeData(path)
#n_channels, fs, shank_to_channel   = loadXML(path)
position = loadPosition(path, events, episodes)
wake_ep = loadEpoch(path, 'wake', episodes)

sz = int(len(spikes.keys()) / 4) + 1
for i in range(len(wake_ep)):
    figure()
    ep = nts.IntervalSet(start=wake_ep.loc[i, 'start'],
                         end=wake_ep.loc[i, 'start'] + 6e8)
    tc = computeAngularTuningCurves(spikes, position['ry'], ep, 60)
    for x in spikes.keys():
        subplot(sz, 4, x + 1, projection='polar')
        plot(tc[x], label=str(x))
        legend()

#path_plot(wake_ep, position)

#####################################################################################
Example #25
0
exemple.close()

data_directory = '/mnt/DataGuillaume/MergedData/'
session = 'Mouse12/Mouse12-120807'
generalinfo = scipy.io.loadmat(data_directory + session +
                               '/Analysis/GeneralInfo.mat')
shankStructure = loadShankStructure(generalinfo)
# spikes,shank	= loadSpikeData(data_directory+session+'/Analysis/SpikeData.mat', shankStructure['thalamus'])
wake_ep = loadEpoch(data_directory + session, 'wake')
sleep_ep = loadEpoch(data_directory + session, 'sleep')
sws_ep = loadEpoch(data_directory + session, 'sws')
rem_ep = loadEpoch(data_directory + session, 'rem')
sleep_ep = sleep_ep.merge_close_intervals(threshold=1.e3)
# sws_ep 			= sleep_ep.intersect(sws_ep)
# rem_ep 			= sleep_ep.intersect(rem_ep)
pre_ep = nts.IntervalSet(sleep_ep['start'][0], sleep_ep['end'][0])
post_ep = nts.IntervalSet(sleep_ep['start'][1], sleep_ep['end'][1])
pre_sws_ep = sws_ep.intersect(pre_ep)
pos_sws_ep = sws_ep.intersect(post_ep)
pre_sws_ep = get_xmin(pre_sws_ep.iloc[::-1], 30)
pos_sws_ep = get_xmin(pos_sws_ep, 30)

store = pd.HDFStore("../figures/figures_articles/figure3/pop_phase_shift.h5")
corr = store['corr']
phi = store['phi']
store.close()

store = pd.HDFStore("../figures/figures_articles/figure3/pca_analysis_3.h5")
ripscore = {}
remscore = {}
# for i, j in zip(range(3), ['hd', 'nohd_mod', 'nohd_nomod']):
Example #26
0
my_channels.as_units('ms').loc[5000:12000]
# And in microseconds which is the default mode
my_spike.loc[5000000:12000000]
my_eeg.loc[5000000:12000000]
my_channels.loc[5000000:12000000]
# Dont miss a zero...
# Observe the difference by plotting it
plot(my_channels.as_units('s'), '-')
plot(my_channels.as_units('s').loc[5:12], 'o-')
show()

# Observe with the difference of colors how you took only a subpart
# But defining subpart like that is tedious
# And here comes the IntervaSet which basically does the same
# For example the first and last 5 second are rem sleep, 5 to 10 s is wake
my_rem = nts.IntervalSet(start=[0, 10], end=[5, 15], time_units='s')
# Same for wake
my_wake = nts.IntervalSet(start=[5], end=[10], time_units='s')
#Observe the different variables by typing them in the terminal
my_rem
my_wake

# What are the spike that occurs during rem sleep?
spike_during_rem = my_spike.restrict(my_rem)
# What is the value of the eeg during wake?
eeg_during_wake = my_eeg.restrict(my_wake)
# What is the value of the channels during both wake and rem
wake_and_rem = my_wake.union(my_rem)
channels_wake_rem = my_channels.restrict(wake_and_rem)

# How to remove the first 2 second of the wake_and_rem interval :
Example #27
0
# you can plot the x position versus the y position
import matplotlib.pyplot as plt
plt.figure()
plt.plot(mouse_position['x'].values, mouse_position['y'].values)
plt.xlabel("x position (cm)")
plt.ylabel("y position (cm)")
plt.show()

# To compute the tuning curve of one neuron, we need to restrict it's activity to epoch of active exploration
# epoch means we need to use the object nts.IntervalSet
# The interval are contained in the file : Mouse12-120806_ArenaEpoch.txt
new_data = np.genfromtxt(data_directory + 'Mouse12-120806_ArenaEpoch.txt')
# Check your variables and look at it shape
# It's a 2 dimension table with start and end of epoch. We can integrate it in nts:
exploration = nts.IntervalSet(start=new_data[:, 0],
                              end=new_data[:, 1],
                              time_units='s')
# Check your variable again for example in second:
exploration.as_units('s')

# Next step is to compute an average firing rate for one neuron
# Let's take neuron 39 (it's the neuron with the highest number of spikes)
my_neuron = spikes[39]
# To speed up computation, we can restrict the time of spikes to epoch of wake
my_neuron = my_neuron.restrict(wake_ep)

# A firing rate is the number of spikes per time bin
# We need to loop over each bin and find the corresponding number of spikes that falls in the interval
# So first you define the size of your time bin, for example let's take one second time bin
bin_size = 1.0  # second
# Next we need to know the duration of our recording
def transitions_times(states, epsilon=1, verbose=False):
    '''
        states : dict of nts.Interval_Set
        
        This function compute transition in between Intervals in a dict.
        It returns a new dict with intervals and when the transition occurs
        
        epsilon : tolerance time delay between state
        
        This function does NOT WORK for triple transitions (ex : sws/rem/sws) ... 
        
    '''

    import itertools

    empty_state = []
    for state in states:
        if len(states[state]) == 0:
            empty_state.append(state)
            continue
        states[state] = states[state].drop_short_intervals(1)

    for i in empty_state:
        del states[i]

    transitions_intervals = {}
    transitions_timing = {}

    for items in itertools.permutations(states.keys(), 2):
        #         states[items[0]] = states[items[0]].drop_short_intervals(1)
        #         states[items[1]] = states[items[1]].drop_short_intervals(1)

        if verbose:
            print('Looking at transition from', items[0], ' to ', items[1])
        end = nts.Ts(np.array(states[items[0]].end + (epsilon * 1_000_000) +
                              1))
        in_next_epoch = states[items[1]].in_interval(end)

        transitions_intervals.update({items: []})
        transitions_timing.update({items: []})

        for n, t in enumerate(in_next_epoch):
            if np.isnan(t): continue
            start = states[items[0]].iloc[n].start
            trans = int(
                np.mean([
                    states[items[0]].iloc[n].end,
                    states[items[1]].iloc[int(t)].start
                ]))
            end = states[items[1]].iloc[int(t)].end
            transitions_intervals[items].append([start, end])
            transitions_timing[items].append(trans)

        if not transitions_timing[items] == []:
            transitions_intervals[items] = np.array(
                transitions_intervals[items])
            transitions_intervals[items] = nts.IntervalSet(
                transitions_intervals[items][:, 0],
                transitions_intervals[items][:, 1],
                force_no_fix=True)

            transitions_timing[items] = nts.Ts(
                t=np.array(transitions_timing[items]))
    return transitions_intervals, transitions_timing
Example #29
0
                        n_ttl_channels=1,
                        optitrack_ch=0)

tuning_curves = computeAngularTuningCurves(spikes, position['ry'], wake_ep, 60)
tuning_curves = smoothAngularTuningCurves(tuning_curves, 10, 2)

tokeep, stat = findHDCells(tuning_curves)

#figure()
#for i in range(len(tokeep)):
#    subplot(3,3,i+1, projection = 'polar')
#    plot(tuning_curves[tokeep[i]])

spikes = {n: spikes[n] for n in tokeep}

ep = nts.IntervalSet(start=wake_ep.loc[0, 'start'],
                     end=wake_ep.loc[0, 'start'] + 30 * 60 * 1000 * 1000)

iwak = makeRingManifold(spikes,
                        ep,
                        position['ry'],
                        position[['x', 'z']],
                        bin_size=300)

figure()
for i, n in enumerate(spikes):
    subplot(5, 4, i + 1, projection='polar')
    plot(tuning_curves[n])

show()
Example #30
0
    else:
        hpc_channel = generalinfo['channelStructure'][0][0][1][0][0][0][0] - 1
    spikes, shank = loadSpikeData(
        data_directory + session + '/Analysis/SpikeData.mat',
        shankStructure['thalamus'])
    wake_ep = loadEpoch(data_directory + session, 'wake')
    sleep_ep = loadEpoch(data_directory + session, 'sleep')
    sws_ep = loadEpoch(data_directory + session, 'sws')
    rem_ep = loadEpoch(data_directory + session, 'rem')
    sleep_ep = sleep_ep.merge_close_intervals(threshold=1.e3)
    sws_ep = sleep_ep.intersect(sws_ep)
    rem_ep = sleep_ep.intersect(rem_ep)
    speed = loadSpeed(data_directory + session +
                      '/Analysis/linspeed.mat').restrict(wake_ep)
    speed_ep = nts.IntervalSet(
        speed[speed > 2.5].index.values[0:-1],
        speed[speed > 2.5].index.values[1:]).drop_long_intervals(
            26000).merge_close_intervals(50000)
    wake_ep = wake_ep.intersect(speed_ep).drop_short_intervals(3000000)
    n_channel, fs, shank_to_channel = loadXML(data_directory + session + "/" +
                                              session.split("/")[1] + '.xml')
    rip_ep, rip_tsd = loadRipples(data_directory + session)
    rip_ep = sws_ep.intersect(rip_ep)
    rip_tsd = rip_tsd.restrict(rip_ep)
    hd_info = scipy.io.loadmat(data_directory + session +
                               '/Analysis/HDCells.mat')['hdCellStats'][:, -1]
    hd_info_neuron = np.array([hd_info[n] for n in spikes.keys()])

    spikes_sws = {
        n: spikes[n].restrict(sws_ep)
        for n in spikes.keys() if len(spikes[n].restrict(sws_ep))
    }