def get_spikes(specimen_id):
    #---get spike times from the data
    dir_name = os.path.join(relative_path,
                            'mouse_nwb/specimen_' + str(specimen_id))
    all_sweeps = ctc.get_ephys_sweeps(
        specimen_id, os.path.join(dir_name, 'ephys_sweeps.json'))
    sweeps = get_sweep_num_by_name(all_sweeps, 'Noise 2')
    nwb = ctc.get_ephys_data(specimen_id, os.path.join(dir_name, 'ephys.json'))
    data = []
    bio_spike_times = []
    for s in sweeps:
        bio_spike_times.append(nwb.get_spike_times(s))
        data.append(nwb.get_sweep(s))
    dt = 1. / data[0]['sampling_rate']
    stim = data[0]['stimulus']

    #---get model spike times
    #find the folder name for specimen
    for dir in folders:
        sp_id = int(os.path.basename(dir)[:9])
        if sp_id == specimen_id:
            folder = dir

    out = {}
    out['dt'] = dt
    out['bio_spike_times'] = bio_spike_times
    out['stimulus'] = stim

    out['LIF_spike_times'] = get_model_spike_times_from_nwb(
        '_GLIF1_neuron_config.json',
        folder,
        '(LIF)',
        sweeps,
        where_running='external')
    out['LIFR_spike_times'] = get_model_spike_times_from_nwb(
        '_GLIF2_neuron_config.json',
        folder,
        '(LIF-R)',
        sweeps,
        where_running='external')
    out['LIFASC_spike_times'] = get_model_spike_times_from_nwb(
        '_GLIF3_neuron_config.json',
        folder,
        '(LIF-ASC)',
        sweeps,
        where_running='external')
    out['LIFRASC_spike_times'] = get_model_spike_times_from_nwb(
        '_GLIF4_neuron_config.json',
        folder,
        '(LIF-R-ASC)',
        sweeps,
        where_running='external')
    out['LIFRASCAT_spike_times'] = get_model_spike_times_from_nwb(
        '_GLIF5_neuron_config.json',
        folder,
        '(LIF-R-ASC-A)',
        sweeps,
        where_running='external')

    return out
Exemplo n.º 2
0
def extract_data(sweep_name, the_sweeps, nwb):
    '''loads data for desired sweeps from the specified nwb file.
    inputs:
        sweep name: string
            string specifying sweeps to look for in the_sweeps dictionary
        the_sweeps: dictionary
            describes sweeps in experiment returned by the allensdk ctc.get_ephys_sweeps
        nwb: object
            experiment data returned by allensdk ctc.get_ephys_data
    returns:
        sweeps: list of integers
            sweep numbers corresponding to specified sweep_name
        data: list of dictionaries
            data extracted from nwb
        spike_ind: list of arrays
            each array has the indices of the spikes
        spike_times:  list of arrays
            each array has the times of the spikes
        dt: float
            time step of first sweep
    '''

    sweeps = get_sweep_num_by_name(the_sweeps, sweep_name)
    data = []
    spike_times = []
    for s in sweeps:
        spike_times.append(nwb.get_spike_times(s))
        data.append(nwb.get_sweep(s))
    dt = 1. / data[0]['sampling_rate']
    stim = data[0]['stimulus']
    spike_ind = convert_spike_times_to_ind(spike_times, dt)

    return sweeps, data, spike_ind, spike_times, dt
Exemplo n.º 3
0
def main(specimen_id):
    #find the folder name for specimen
    for dir in folders:
        sp_id = int(os.path.basename(dir)[:9])
        if sp_id == specimen_id:
            folder = dir

    #---get spike times from the data
    all_sweeps = ctc.get_ephys_sweeps(specimen_id)
    sweeps = get_sweep_num_by_name(all_sweeps, 'Noise 2')
    nwb = ctc.get_ephys_data(specimen_id)
    data = []
    spike_times = []
    for s in sweeps:
        spike_times.append(nwb.get_spike_times(s))
        data.append(nwb.get_sweep(s))
    dt = 1. / data[0]['sampling_rate']
    stim_len = len(data[0]['stimulus'])

    # calculate explained variance
    ev_LIF = calc_ev('_GLIF1_neuron_config.json', folder, '(LIF)', sweeps,
                     stim_len, spike_times, dt)
    ev_LIFR = calc_ev('_GLIF2_neuron_config.json', folder, '(LIF-R)', sweeps,
                      stim_len, spike_times, dt)
    ev_LIFASC = calc_ev('_GLIF3_neuron_config.json', folder, '(LIF-ASC)',
                        sweeps, stim_len, spike_times, dt)
    ev_LIFRASC = calc_ev('_GLIF4_neuron_config.json', folder, '(LIF-R-ASC)',
                         sweeps, stim_len, spike_times, dt)
    ev_LIFRASCAT = calc_ev('_GLIF5_neuron_config.json', folder,
                           '(LIF-R-ASC-A)', sweeps, stim_len, spike_times, dt)

    return [ev_LIF, ev_LIFR, ev_LIFASC, ev_LIFRASC, ev_LIFRASCAT]
def get_model(path, EW):
    '''Runs the model for a specified neuron and model
    inputs:
        path: string
            folder path with files for the neuron
        EW: string
            end of file searching for:  options '_GLIF1_neuron_config.json',_GLIF2_neuron_config.json' etc.
    returns:
        run_data: dictionary
            contains data from the model run
            
   '''

    specimen_id=int(os.path.basename(path)[:9])
    file=get_file_path_endswith(path, EW)

    # load data
    dir_name=os.path.join(relative_path, 'mouse_nwb/specimen_'+ str(specimen_id))
    all_sweeps=ctc.get_ephys_sweeps(specimen_id,  os.path.join(dir_name, 'ephys_sweeps.json'))
    #all_sweeps=ctc.get_ephys_sweeps(specimen_id)
    sweeps=get_sweep_num_by_name(all_sweeps, 'Noise 2')
    
    noise2_sweeps = get_sweep_num_by_name(all_sweeps, 'Noise 2')
#    noise2_data=ctc.get_ephys_data(specimen_id).get_sweep(noise2_sweeps[0])
    noise2_data=ctc.get_ephys_data(specimen_id, os.path.join(dir_name, 'ephys.nwb')).get_sweep(noise2_sweeps[0])

    # run model with current
    stimulus2=noise2_data['stimulus']
    neuron_config=ju.read(file)
    neuron_config['dt']=1./noise2_data['sampling_rate'] #reset dt to the stimulus dt not the optimization dt
    neuron = GlifNeuron.from_dict(neuron_config)
    1/noise2_data['sampling_rate']
    run_data = neuron.run(stimulus2)
    run_data['time']=np.arange(0, len(run_data['voltage']))*neuron_config['dt']
    run_data['El_reference']=neuron_config['El_reference']    
    run_data['stimulus']=noise2_data['stimulus']

    return run_data
def make_plots(specimen_id):
    dir_name = os.path.join(relative_path,
                            'mouse_nwb/specimen_' + str(specimen_id))
    the_sweeps = ctc.get_ephys_sweeps(
        specimen_id, os.path.join(dir_name, 'ephys_sweeps.json'))
    noise1_sweeps = get_sweep_num_by_name(the_sweeps, 'Noise 1')

    # get data
    noise1 = {'current': [], 'voltage': [], 'bio_spike_times': []}
    voltage = []
    for s in noise1_sweeps:
        #data=ctc.get_ephys_data(specimen_id).get_sweep(s)
        data = ctc.get_ephys_data(specimen_id,
                                  os.path.join(dir_name,
                                               'ephys.nwb')).get_sweep(s)

        spike_times = ctc.get_ephys_data(
            specimen_id, os.path.join(dir_name,
                                      'ephys.nwb')).get_spike_times(s)
        voltage.append(data['response'])
        noise1['current'].append(data['stimulus'])
        noise1['bio_spike_times'].append(spike_times)
        sr = data['sampling_rate']
    dt = 1. / sr

    #--bessel the traces
    bessel = {'N': 4, 'freq': 10000}
    for trace in voltage:
        filt_coeff = (bessel["freq"]) / (
            sr / 2.)  # filter fraction of Nyquist frequency
        b, a = signal.bessel(bessel["N"], filt_coeff, "low")
        noise1['voltage'].append(signal.filtfilt(b, a, trace, axis=0))

    calc_spike_cut_and_v_reset_via_expvar_residuals(noise1['current'],
                                                    noise1['voltage'],
                                                    dt,
                                                    0,
                                                    0,
                                                    MAKE_PLOT=True,
                                                    PUBLICATION_PLOT=True,
                                                    SHOW_PLOT=True,
                                                    BLOCK=True)
def make_plots(specimen_id):
    dir_name = os.path.join(relative_path,
                            'mouse_nwb/specimen_' + str(specimen_id))
    the_sweeps = ctc.get_ephys_sweeps(
        int(specimen_id), os.path.join(dir_name, 'ephys_sweeps.json'))
    ssq_triple_sweeps = get_sweep_num_by_name(the_sweeps,
                                              'Short Square - Triple')
    if specimen_id == 512322162:  #the last sweep on this neuron is strange
        ssq_triple_sweeps = ssq_triple_sweeps[:-1]

    # put data in the format required for functions below
    multi_ssq_data = {'current': [], 'voltage': []}
    voltage = []
    for s in ssq_triple_sweeps[:-1]:
        data = ctc.get_ephys_data(specimen_id,
                                  os.path.join(dir_name,
                                               'ephys.json')).get_sweep(s)
        voltage.append(data['response'])
        multi_ssq_data['current'].append(data['stimulus'])
        sr = data['sampling_rate']
    dt = 1. / sr

    #--bessel the traces
    bessel = {'N': 4, 'freq': 10000}
    for trace in voltage:
        filt_coeff = (bessel["freq"]) / (
            sr / 2.)  # filter fraction of Nyquist frequency
        b, a = signal.bessel(bessel["N"], filt_coeff, "low")
        multi_ssq_data['voltage'].append(signal.filtfilt(b, a, trace, axis=0))

    multi_ssq_dv_cutoff, multi_ssq_thresh_frac = estimate_dv_cutoff(
        multi_ssq_data['voltage'], dt, efex.SHORT_SQUARE_TRIPLE_WINDOW_START,
        efex.SHORT_SQUARE_TRIPLE_WINDOW_END)
    (a_spike_component_of_threshold, b_spike_component_of_threshold, \
        mean_voltage_first_spike_of_blip) = calc_spike_component_of_threshold_from_multiblip(multi_ssq_data,
                                                                                             dt,
                                                                                             multi_ssq_dv_cutoff,
                                                                                             multi_ssq_thresh_frac,
                                                                                             MAKE_PLOT=True,
                                                                                             SHOW_PLOT=True,
                                                                                             BLOCK=True,
                                                                                             PUBLICATION_PLOT=True)
Exemplo n.º 7
0
def main():    
    # get list of data located in data folder
    folders=np.sort([os.path.join(relative_path, 'data', dir) for dir in  os.listdir(os.path.join(relative_path, 'data'))])
    
    aic_LIF=[]
    aic_LIFR=[]
    aic_LIFASC=[]
    aic_LIFRASC=[]
    aic_LIFRASCAT=[]
    
    sp_ids=[]
    cres=[]
    all_neurons=[]   
    for folder in folders:
        specimen_id=int(os.path.basename(folder)[:9])
        sp_ids.append(specimen_id)
        print specimen_id
        cre=os.path.basename(folder)[10:]
        cres.append(cre)
        
        #---get dt from the data
        #TODO: depricate after one working run though
#        cell_type_directory='/local2/workspace/cell_types/' #pointing to directory not the default ctc directory which will download to working directory
#        sweeps_file=os.path.join(cell_type_directory,'specimen_'+ str(specimen_id), 'ephys_sweeps.json')
#        data_file=os.path.join(cell_type_directory,'specimen_'+ str(specimen_id), 'ephys_sweeps.nwb')
#
#        the_sweeps=ctc.get_ephys_sweeps(specimen_id, sweeps_file)
#        data=ctc.get_ephys_data(specimen_id, data_file)
#        
#        noise1_sweeps=get_sweep_num_by_name(the_sweeps, 'Noise 1')
#        noise1_data=[]
        the_sweeps=ctc.get_ephys_sweeps(specimen_id)
        noise1_sweeps=get_sweep_num_by_name(the_sweeps, 'Noise 1')
        data=ctc.get_ephys_data(specimen_id)
        noise1_data=[]
        for s in noise1_sweeps:
            noise1_data.append(data.get_sweep(s))    
        dt=1./noise1_data[0]['sampling_rate']
        stim_len=len(noise1_data[0]['stimulus'])
        
        # using same sigma as was used in the spike time AIC calculation
        sigma=.01
        
        SS_of_model_data_v_diff_LIF, n_LIF=grab_diff_v_from_folder('_GLIF1_subthr_v.json', folder)
        SS_of_model_data_v_diff_LIFR, n_LIFR=grab_diff_v_from_folder('_GLIF2_subthr_v.json', folder)
        SS_of_model_data_v_diff_LIFASC, n_LIFASC=grab_diff_v_from_folder('_GLIF3_subthr_v.json', folder)
        SS_of_model_data_v_diff_LIFRASC, n_LIFRASC=grab_diff_v_from_folder('_GLIF4_subthr_v.json', folder)
        SS_of_model_data_v_diff_LIFRASCAT, n_LIFRASCAT=grab_diff_v_from_folder('_GLIF5_subthr_v.json', folder)
        
        # calculate the AIC
        aic_LIF.append(calc_aic(1, SS_of_model_data_v_diff_LIF, dt, sigma, n_LIF))
        aic_LIFR.append(calc_aic(2, SS_of_model_data_v_diff_LIFR, dt, sigma, n_LIFR))
        aic_LIFASC.append(calc_aic(3, SS_of_model_data_v_diff_LIFASC, dt, sigma, n_LIFASC))
        aic_LIFRASC.append(calc_aic(4, SS_of_model_data_v_diff_LIFRASC, dt, sigma, n_LIFRASC))
        aic_LIFRASCAT.append(calc_aic(5, SS_of_model_data_v_diff_LIFRASCAT, dt, sigma, n_LIFRASCAT))
        
    df=pd.DataFrame({'specimen_id':sp_ids,
                 'cre':cres,
                 'aic_LIF':aic_LIF, 
                 'aic_LIFR':aic_LIFR, 
                 'aic_LIFASC':aic_LIFASC, 
                 'aic_LIFRASC':aic_LIFRASC, 
                 'aic_LIFRASCAT':aic_LIFRASCAT})    
#------------------------------------------------------------------------------------------------

where_running = 'external'
#where_running='internal'

# load data out of configuration files
data_path = os.path.join(relative_path, 'create_data_dir/human_data')
folders = np.sort([os.path.join(data_path, f) for f in os.listdir(data_path)])

all_neurons = []
for ii, folder in enumerate(folders):
    # sort the data so that specifying start and end integers works
    specimen_id = int(os.path.basename(folder)[:9])
    cre = os.path.basename(folder)[10:]
    the_sweeps = ctc.get_ephys_sweeps(specimen_id)
    noise1_sweeps = get_sweep_num_by_name(the_sweeps, 'Noise 1')
    noise2_sweeps = get_sweep_num_by_name(the_sweeps, 'Noise 2')
    pairs = [['_GLIF1_neuron_config.json', '(LIF)'],
             ['_GLIF2_neuron_config.json', '(LIF-R)'],
             ['_GLIF3_neuron_config.json', '(LIF-ASC)'],
             ['_GLIF4_neuron_config.json', '(LIF-R-ASC)'],
             ['_GLIF5_neuron_config.json', '(LIF-R-ASC-A)']]
    for pair in pairs:  #TODO: update this to use new function or check files
        glif_spike_ind_n1 = get_model_spike_times_from_nwb(
            pair[0], folder, pair[1], noise1_sweeps, where_running)
        if check_spike_times_identical(glif_spike_ind_n1):
            pass
        else:
            print ii, specimen_id, 'has unmatching noise 1 spike times'
            print glif_spike_ind_n1
        glif_spike_ind_n2 = get_model_spike_times_from_nwb(
Exemplo n.º 9
0
current2 = plt.subplot2grid((10, 2), (0, 1))
data2 = plt.subplot2grid((10, 2), (1, 1))
LIF_Ctgf = plt.subplot2grid((10, 2), (2, 1))
LIFR_Ctgf = plt.subplot2grid((10, 2), (3, 1))
LIFASC_Ctgf = plt.subplot2grid((10, 2), (4, 1), rowspan=2)
LIFRASC_Ctgf = plt.subplot2grid((10, 2), (6, 1), rowspan=2)
LIFRASCAT_Ctgf = plt.subplot2grid((10, 2), (8, 1), rowspan=2)
x_lim = [18, 18.3]

##---474637203Htr3a------

dir_name = os.path.join(relative_path, 'mouse_nwb/specimen_' + str(474637203))
the_sweeps = ctc.get_ephys_sweeps(474637203,
                                  os.path.join(dir_name, 'ephys_sweeps.json'))
nwb = ctc.get_ephys_data(474637203, os.path.join(dir_name, 'ephys.json'))
sweeps = get_sweep_num_by_name(the_sweeps, 'Noise 2')
data = []
spike_times = []
for s in sweeps:
    spike_times.append(nwb.get_spike_times(s))
    data.append(nwb.get_sweep(s))

print 'loading LIF'
LIF_model = pickle.load(
    open("pkl_data/474637203Htr3a-Cre_NO152_LIF_model.pkl", "rb"))
print 'loading LIFR'
LIFR_model = pickle.load(
    open("pkl_data/474637203Htr3a-Cre_NO152_LIFR_model.pkl", "rb"))
print 'loading LIFASC'
LIFASC_model = pickle.load(
    open("pkl_data/474637203Htr3a-Cre_NO152_LIFASC_model.pkl", "rb"))