Exemple #1
0
def spike_loc(cl, vl, trigger_tm, target_cl,key=None):
    ''' Return the indices of vl['xs'] and vl['ys']
        that correspond to spikes with cluster label
        target_cl.
        
        Note: vl['xs'] and vl['ys'] have repeated values.
            We must return exactly one index for each unique
            location.'''
    cached_results = (try_cache(key) if key is not None else None)
    if cached_results is not None:
        logging.info('Got spike cluster %i from Cache.cache.',target_cl)
        return cached_results
    
    if target_cl == 1:
        logging.warning('Are you SURE you want to find cluster 1?')
        y = raw_input()
        if y in ['n', 'N']:
            return np.NAN
    
    # Get the times when a cluster in label 'target_cl' occurs
    # If target_cl is None, then find the iterations for everything
    st = cl['Time'][cl['Label']==target_cl]
    if st.shape == (0,):
        logging.info('No clusters with label %i. Quitting...', target_cl)
        return np.NAN
    
    # Ask user if he wants to waste time on an excessively large dataset
    '''
    logging.info('%i pts in cluster %i',len(st),target_cl)
    if 1.0*len(st)/len(cl['Label']) > .05:
        logging.warning('Are you SURE you want to proceed?')
        return np.NAN
        y = raw_input()
        if y in ['n', 'N']:
            return np.NAN'''
    
    # Get the vl indices corresponding to times in st
    spk_i = np.array(list(match_cl_to_vl(st, vl, trigger_tm)))
    
    #import pdb; pdb.set_trace()
    
    # Delete NaN values
    spk_i = spk_i[~np.isnan(spk_i)].astype(int)
    
    # Determine speed
    # Matlab rounds speed to 6 or 7 decimals, so do the same for consistency
    speed = matround(np.sqrt(vl['vxs']**2+vl['vys']**2),decimals=6)[spk_i]
    
    # Only leave spikes when rat is running faster than 2 in /sec
    spk_i = spk_i[(speed > 2) & (speed < 40) ]
    
    if key is not None: store_in_cache(key, spk_i)
    
    return np.unique(spk_i)
def match_cl_to_vl(times, vl, trigger_tm):
    ''' Return a generator with the iteration for each
        element in array 'times' '''
    times = times.astype(np.float64)
    times /= (cl_rate * 24 * 60 * 60)
    times += trigger_tm

    # Clean up the virmenLog iterations numbers by removing the
    #  nan ones and linearly interpolating between them
    f = np.nonzero(~np.isnan(vl['Iter num']))[0]
    y = matround(np.interp(range(len(vl['Iter num'])), f, vl['Iter num'][f]),
                 0)
    y = y.astype(int)

    # Iterations are Matlab indices, so they begin at 1
    #  Adjust them to fit Python
    y -= 1

    # Determine the iteration number at which each spike occurred

    # This is a real time killer
    """
    for ndx in range(len(times)):
        try:
            # Find iteration preceeding spike
            f = np.nonzero(vl['Iter time'] < times[ndx])[0][-1] # Take the latimes one
            # Make sure there is an iteration following the spike
            np.nonzero(vl['Iter time'] > times[ndx])[0][0]
            
            yield y[f]
        except:
            # If nonzero is empty, the spike occurred before the first
            #  or after the last spike
            yield np.NAN"""
    for ndx in range(len(times)):
        try:
            # Find iteration preceeding spike
            f = np.nonzero(
                vl['Iter time'] < times[ndx])[0][-1]  # Take the latimes one
            yield y[f]
        except:
            # If nonzero is empty, the spike occurred before the first
            #  or after the last spike
            yield np.NAN
def match_cl_to_vl(times, vl, trigger_tm):
    ''' Return a generator with the iteration for each
        element in array 'times' '''
    times = times.astype(np.float64)
    times /= (cl_rate*24*60*60)
    times += trigger_tm
    
    # Clean up the virmenLog iterations numbers by removing the
    #  nan ones and linearly interpolating between them
    f = np.nonzero(~np.isnan(vl['Iter num']))[0]
    y = matround(np.interp(range(len(vl['Iter num'])), f, vl['Iter num'][f]),0)
    y = y.astype(int)
    
    # Iterations are Matlab indices, so they begin at 1
    #  Adjust them to fit Python
    y -= 1

    # Determine the iteration number at which each spike occurred
    
    # This is a real time killer
    """
    for ndx in range(len(times)):
        try:
            # Find iteration preceeding spike
            f = np.nonzero(vl['Iter time'] < times[ndx])[0][-1] # Take the latimes one
            # Make sure there is an iteration following the spike
            np.nonzero(vl['Iter time'] > times[ndx])[0][0]
            
            yield y[f]
        except:
            # If nonzero is empty, the spike occurred before the first
            #  or after the last spike
            yield np.NAN"""
    for ndx in range(len(times)):
        try:
            # Find iteration preceeding spike
            f = np.nonzero(vl['Iter time'] < times[ndx])[0][-1] # Take the latimes one
            yield y[f]
        except:
            # If nonzero is empty, the spike occurred before the first
            #  or after the last spike
            yield np.NAN