Esempio n. 1
0
 def __init__(self, allen_id=None):
     self = self
     if allen_id == None:
         self.allen_id = 566302806
         glif_api = GlifApi()
         self.nc = glif_api.get_neuron_configs([self.allen_id
                                                ])[self.allen_id]
         self.glif = GlifNeuron.from_dict(self.nc)
         self.metad = glif_api.get_neuronal_models_by_id([self.allen_id])[0]
     else:
         glif_api = GlifApi()
         self.allen_id = allen_id
         self.glif = glif_api.get_neuronal_models_by_id([allen_id])[0]
         self.nc = glif_api.get_neuron_configs([allen_id])[allen_id]
         self.glif = GlifNeuron.from_dict(self.nc)
         self.metad = glif_api.get_neuronal_models_by_id([self.allen_id])[0]
Esempio n. 2
0
    def sim_Brian2_AllenSDK(neuron_config, data):
        stim = data['stimulus']
        dt = 1.0 / data['sampling_rate']

        stim_unit = data['stimulus_unit']
        if stim_unit == 'pA':
            stim = stim * 1E12  # convert pA to Amps

        # Brian2 simulation
        shift_stim = np.pad(stim[1:],(0,1),mode='edge')  # shift stimulus left once
        param_dict = load_AllenSDK_parameters(neuron_config, dt)
        init_values = load_AllenSDK_initial_values(neuron_config)
        # With method = 'euler' the error w.r.t. AllenSDK traces is smaller in some cases
        # AllenSDK solves exponentials analytically, which results in small discrepancies
        output_Brian2 = glif_model.run_brian_sim(shift_stim * b2.amp, dt * b2.second, init_values, param_dict, 'exact')
        t, V, I_0, I_1,  = output_Brian2
        print("info: Brian2 simulation DONE")

        # AllenSDK simulation
        glif = GlifNeuron.from_dict(neuron_config)
        glif.dt = dt
        output_AllenSDK = glif.run(stim)
        V_0 = output_AllenSDK['voltage']
        I_0_0 = output_AllenSDK['AScurrents'][:,0]
        I_1_0 = output_AllenSDK['AScurrents'][:,1]
        print("info: AllenSDK simulation DONE")

        return (t, V, V_0, I_0, I_0_0, I_1, I_1_0, )
Esempio n. 3
0
 def set_attrs(self, **attrs):
     self.model.attrs.update(attrs)
     #self.nc.update(attrs)
     for k, v in attrs.items():
         self.nc[k] = v
     self.glif = GlifNeuron.from_dict(self.nc)
     return self.glif
Esempio n. 4
0
    def get_voltage(self, neuron_config, stim_name):

        ephys_sweeps = self.cfg.ephys_sweeps

        ephys_sweep = next(s for s in ephys_sweeps
                           if s['stimulus_name'] == stim_name)

        ds = NwbDataSet(self.ephys_file_name)
        data = ds.get_sweep(ephys_sweep['sweep_number'])
        stimulus = data['stimulus']
        stimulus = stimulus[stimulus != 0]
        stimulus = stimulus[:self.cfg.stimulus_allow]

        # initialize the neuron
        neuron = GlifNeuron.from_dict(neuron_config)

        # Set dt
        neuron.dt = 1.0 / data['sampling_rate']

        # simulate the neuron
        output = neuron.run(stimulus)

        voltage = output['voltage'] * 1e3

        voltage = voltage[~np.isnan(voltage)]
        voltage = voltage[:self.cfg.signal_allow]

        return output, voltage, neuron, stimulus
Esempio n. 5
0
    def set_attrs(self):#, **attrs):
        nc = glif_api.get_neuron_configs([neuronal_model_id])[neuronal_model_id]
        neuron_config = glif_api.get_neuron_configs([neuronal_model_id])
        #nc = glif_api.get_neuron_configs([neuronal_model_id])[neuronal_model_id]
        #neuron_config = glif_api.get_neuron_configs([neuronal_model_id])

        neuron_config = neuron_config['566302806']
        self.nm = GlifNeuron.from_dict(neuron_config)
        return self.nm
Esempio n. 6
0
    def init_backend(self,
                     attrs=None,
                     cell_name='alice',
                     current_src_name='hannah',
                     DTC=None):
        backend = 'GLIF'
        super(GLIFBackend, self).init_backend()

        self.model._backend.use_memory_cache = False
        self.current_src_name = current_src_name
        self.cell_name = cell_name
        self.vM = None
        self.allen_id = None
        self.attrs = attrs
        self.nc = None

        self.temp_attrs = None

        if self.allen_id == None:
            try:
                self.nc = pickle.load(open(str('allen_id.p'), 'rb'))
            except:
                self.allen_id = 566302806
                glif_api = GlifApi()

                self.nc = glif_api.get_neuron_configs([self.allen_id
                                                       ])[self.allen_id]
                pickle.dump(copy.copy(self.nc), open(str('allen_id.p'), 'wb'))

        else:

            try:
                self.nc = pickle.load(open(str('allen_id.p'), 'rb'))
            except:
                glif_api = GlifApi()
                self.allen_id = allen_id
                self.glif = glif_api.get_neuronal_models_by_id([allen_id])[0]
                self.nc = glif_api.get_neuron_configs([self.allen_id
                                                       ])[self.allen_id]
                pickle.dump(self.nc, open(str('allen_id.p'), 'wb'))

        self.glif = GlifNeuron.from_dict(self.nc)

        if type(attrs) is not type(None):
            self.set_attrs(**attrs)
            self.sim_attrs = attrs

        if type(DTC) is not type(None):
            if type(DTC.attrs) is not type(None):

                self.set_attrs(**DTC.attrs)

            if hasattr(DTC, 'current_src_name'):
                self._current_src_name = DTC.current_src_name

            if hasattr(DTC, 'cell_name'):
                self.cell_name = DTC.cell_name
Esempio n. 7
0
def run_one_cell(glif_dir, curr_pA, dt=5e-7, show_plot=True):

    # initialize the neuron
    neuron_config = json_utilities.read('%s/neuron_config.json'%glif_dir)
    neuron = GlifNeuron.from_dict(neuron_config)

    # important! set the neuron's dt value for your stimulus in seconds
    neuron.dt = dt

    # make a short square pulse. stimulus units should be in Amps.
    stimulus = [ 0.0 ] * int(0.1/neuron.dt) + [ curr_pA * 1e-12 ] * int(1/neuron.dt) + [ 0.0 ] * int(0.1/neuron.dt)
    times = [ i*neuron.dt for i in range(len(stimulus)) ]

    # simulate the neuron
    output = neuron.run(stimulus)

    print "run.."

    voltage = output['voltage']
    threshold = output['threshold']
    spike_times = output['interpolated_spike_times']

    info = "Model %s; %spA stimulation; dt=%ss; %i spikes"%(glif_dir,curr_pA,dt,len(spike_times))
    print(info)
    
    v_file = open('%s/original.v.dat'%glif_dir,'w')
    th_file = open('%s/original.thresh.dat'%glif_dir,'w')
    
    for i in range(len(times)):
        t = times[i]
        v = voltage[i]
        th = threshold[i]
        v_file.write('%s\t%s\n'%(t,v))
        th_file.write('%s\t%s\n'%(t,th))
    v_file.close()
    th_file.close()
    
    pynml.generate_plot([times],
                            [voltage], 
                            "Membrane potential; %s"%info, 
                            colors = ['k'], 
                            xaxis = "Time (s)", 
                            yaxis = "Voltage (V)", 
                            grid = True,
                            show_plot_already=False,
                            save_figure_to='%s/MembranePotential_%ipA.png'%(glif_dir,curr_pA))
                
    pynml.generate_plot([times],
                            [threshold], 
                            "Threshold; %s"%info, 
                            colors = ['r'], 
                            xaxis = "Time (s)", 
                            yaxis = "Voltage (V)", 
                            grid = True,
                            show_plot_already=show_plot,
                            save_figure_to='%s/Threshold_%ipA.png'%(glif_dir,curr_pA))
Esempio n. 8
0
def test_simulate():
    logging.getLogger().setLevel(logging.DEBUG)
    neuron_config = json_utilities.read(os.path.join(OUTPUT_DIR, '%d_neuron_config.json' % NEURONAL_MODEL_ID))
    ephys_sweeps = json_utilities.read(os.path.join(OUTPUT_DIR, 'ephys_sweeps.json'))
    ephys_file_name = os.path.join(OUTPUT_DIR, '%d.nwb' % NEURONAL_MODEL_ID)

    neuron = GlifNeuron.from_dict(neuron_config)

    sweep_numbers = [ s['sweep_number'] for s in ephys_sweeps ]
    simulate_neuron(neuron, sweep_numbers, ephys_file_name, ephys_file_name, 0.05)
Esempio n. 9
0
def main():
    args = parse_arguments()

    logging.getLogger().setLevel(args.log_level)

    glif_api = None
    if (args.neuron_config_file is None or args.sweeps_file is None
            or args.ephys_file is None):

        assert args.neuronal_model_id is not None, Exception(
            "A neuronal model id is required if no neuron config file, sweeps file, or ephys data file is provided."
        )

        glif_api = GlifApi()
        glif_api.get_neuronal_model(args.neuronal_model_id)

    if args.neuron_config_file:
        neuron_config = json_utilities.read(args.neuron_config_file)
    else:
        neuron_config = glif_api.get_neuron_config()

    if args.sweeps_file:
        sweeps = json_utilities.read(args.sweeps_file)
    else:
        sweeps = glif_api.get_ephys_sweeps()

    if args.ephys_file:
        ephys_file = args.ephys_file
    else:
        ephys_file = 'stimulus_%d.nwb' % args.neuronal_model_id

        if not os.path.exists(ephys_file):
            logging.info("Downloading stimulus to %s." % ephys_file)
            glif_api.cache_stimulus_file(ephys_file)
        else:
            logging.warning("Reusing %s because it already exists." %
                            ephys_file)

    if args.output_ephys_file:
        output_ephys_file = args.output_ephys_file
    else:
        logging.warning(
            "Overwriting input file data with simulated data in place.")
        output_ephys_file = ephys_file

    neuron = GlifNeuron.from_dict(neuron_config)

    # filter out test sweeps
    sweep_numbers = [
        s['sweep_number'] for s in sweeps if s['stimulus_name'] != 'Test'
    ]

    simulate_neuron(neuron, sweep_numbers, ephys_file, output_ephys_file,
                    args.spike_cut_value)
Esempio n. 10
0
def test_3():
    neuron_config = json_utilities.read('neuron_config.json')
    ephys_sweeps = json_utilities.read('ephys_sweeps.json')
    ephys_file_name = 'stimulus.nwb'

    neuron = GlifNeuron.from_dict(neuron_config)

    # sweep_numbers = [ s['sweep_number'] for s in ephys_sweeps
    #                  if s['stimulus_units'] == 'Amps' ]
    sweep_numbers = [7]
    simulate_neuron(neuron, sweep_numbers,
                    ephys_file_name, ephys_file_name, 0.05)
Esempio n. 11
0
def test_3(configured_glif_api, neuron_config_file, ephys_sweeps_file):
    neuron_config = json_utilities.read(neuron_config_file)
    ephys_sweeps = json_utilities.read(ephys_sweeps_file)
    ephys_file_name = 'stimulus.nwb'

    neuron = GlifNeuron.from_dict(neuron_config)

    # sweep_numbers = [ s['sweep_number'] for s in ephys_sweeps
    #                  if s['stimulus_units'] == 'Amps' ]
    sweep_numbers = [7]
    simulate_neuron(neuron, sweep_numbers,
                    ephys_file_name, ephys_file_name, 0.05)
def generate_train_from_model_id(model_id,
                                 stimulus_amplitude=1e-8,
                                 duration=1e6,
                                 noise_exponent=0):
    glif_api = GlifApi()
    neuron_config = glif_api.get_neuron_configs([model_id])
    neuron = GlifNeuron.from_dict(neuron_config[model_id])
    stimulus = stimulus_amplitude * colorednoise(exponent=noise_exponent,
                                                 size=int(duration))
    neuron.dt = 5e-6
    output = neuron.run(stimulus)
    spike_times = output['interpolated_spike_times']
    return spike_times
Esempio n. 13
0
def main():
    args = parse_arguments()

    logging.getLogger().setLevel(args.log_level)

    glif_api = None
    if (args.neuron_config_file is None or 
        args.sweeps_file is None or
        args.ephys_file is None):

        assert args.neuronal_model_id is not None, Exception("A neuronal model id is required if no neuron config file, sweeps file, or ephys data file is provided.")

        glif_api = GlifApi()
        glif_api.get_neuronal_model(args.neuronal_model_id)

    if args.neuron_config_file:
        neuron_config = json_utilities.read(args.neuron_config_file)
    else:
        neuron_config = glif_api.get_neuron_config()

    if args.sweeps_file:
        sweeps = json_utilities.read(args.sweeps_file)
    else:
        sweeps = glif_api.get_ephys_sweeps()

    if args.ephys_file:
        ephys_file = args.ephys_file
    else:
        ephys_file = 'stimulus_%d.nwb' % args.neuronal_model_id

        if not os.path.exists(ephys_file):
            logging.info("Downloading stimulus to %s." % ephys_file)
            glif_api.cache_stimulus_file(ephys_file)
        else:
            logging.warning("Reusing %s because it already exists." % ephys_file)

    if args.output_ephys_file:
        output_ephys_file = args.output_ephys_file
    else:
        logging.warning("Overwriting input file data with simulated data in place.")
        output_ephys_file = ephys_file
        

    neuron = GlifNeuron.from_dict(neuron_config)

    # filter out test sweeps
    sweep_numbers = [ s['sweep_number'] for s in sweeps if s['stimulus_name'] != 'Test' ]

    simulate_neuron(neuron, sweep_numbers, ephys_file, output_ephys_file, args.spike_cut_value) 
Esempio n. 14
0
def runGlifNeuron(neuron_config, I, dt_ms):
    """Run a allensdk GlifNeuron and return voltages and spike-times"""
    # Get the neuron configuration
    neuron = GlifNeuron.from_dict(neuron_config)
    neuron.dt = dt_ms * 1.0e-03  # convert from miliseconds to seconds

    # Run the simulation
    output = neuron.run(I)

    # Return run-time, voltage trace and spike times
    voltages = output['voltage'] * 1.0e03  # convert to mV
    times = [t * dt_ms for t in xrange(len(I))]
    spike_times = output[
        'interpolated_spike_times'] * 1.0e03  # return interpolated spike-times in miliseconds
    #spike_times = output['grid_spike_times'] * 1.0e03 # return grid spike-times in miliseconds
    return times, voltages, spike_times
Esempio n. 15
0
def test_run_glifneuron(configured_glif_api, neuron_config_file):
    # initialize the neuron
    neuron_config = json_utilities.read(neuron_config_file)
    neuron = GlifNeuron.from_dict(neuron_config)

    # make a short square pulse. stimulus units should be in Amps.
    stimulus = [0.0] * 100 + [10e-9] * 100 + [0.0] * 100

    # important! set the neuron's dt value for your stimulus in seconds
    neuron.dt = 5e-6

    # simulate the neuron
    output = neuron.run(stimulus)

    voltage = output['voltage']
    threshold = output['threshold']
    spike_times = output['interpolated_spike_times']
Esempio n. 16
0
def test_2():
    # initialize the neuron
    neuron_config = json_utilities.read('neuron_config.json')
    neuron = GlifNeuron.from_dict(neuron_config)

    # make a short square pulse. stimulus units should be in Amps.
    stimulus = [0.0] * 100 + [10e-9] * 100 + [0.0] * 100

    # important! set the neuron's dt value for your stimulus in seconds
    neuron.dt = 5e-6

    # simulate the neuron
    output = neuron.run(stimulus)

    voltage = output['voltage']
    threshold = output['threshold']
    spike_times = output['interpolated_spike_times']
Esempio n. 17
0
def test_6(stimulus):
    # define your own custom voltage reset rule
    # this one linearly scales the input voltage
    def custom_voltage_reset_rule(neuron, voltage_t0, custom_param_a, custom_param_b):
        return custom_param_a * voltage_t0 + custom_param_b

    # initialize a neuron from a neuron config file
    neuron_config = json_utilities.read('neuron_config.json')
    neuron = GlifNeuron.from_dict(neuron_config)

    # configure a new method and overwrite the neuron's old method
    method = neuron.configure_method('custom', custom_voltage_reset_rule,
                                     {'custom_param_a': 0.1, 'custom_param_b': 0.0})
    neuron.voltage_reset_method = method

    truncate = 56041
    output = neuron.run(stimulus[0:truncate])
Esempio n. 18
0
def test_6(configured_glif_api, neuron_config_file, stimulus):
    # define your own custom voltage reset rule
    # this one linearly scales the input voltage
    def custom_voltage_reset_rule(neuron, voltage_t0, custom_param_a, custom_param_b):
        return custom_param_a * voltage_t0 + custom_param_b

    # initialize a neuron from a neuron config file
    neuron_config = json_utilities.read(neuron_config_file)
    neuron = GlifNeuron.from_dict(neuron_config)

    # configure a new method and overwrite the neuron's old method
    method = neuron.configure_method('custom', custom_voltage_reset_rule,
                                     {'custom_param_a': 0.1, 'custom_param_b': 0.0})
    neuron.voltage_reset_method = method

    truncate = 56041
    output = neuron.run(stimulus[0:truncate])
Esempio n. 19
0
def output():
    neuron_config = json_utilities.read('neuron_config.json')
    ephys_sweeps = json_utilities.read('ephys_sweeps.json')
    ephys_file_name = 'stimulus.nwb'

    # pull out the stimulus for the first sweep
    ephys_sweep = ephys_sweeps[0]
    ds = NwbDataSet(ephys_file_name)
    data = ds.get_sweep(ephys_sweep['sweep_number'])
    stimulus = data['stimulus']

    # initialize the neuron
    # important! update the neuron's dt for your stimulus
    neuron = GlifNeuron.from_dict(neuron_config)
    neuron.dt = 1.0 / data['sampling_rate']

    # simulate the neuron
    truncate = 56041
    output = neuron.run(stimulus[0:truncate])

    return output
Esempio n. 20
0
def test_run_glifneuron(configured_glif_api, neuron_config_file):
    # initialize the neuron
    neuron_config = json_utilities.read(neuron_config_file)
    neuron = GlifNeuron.from_dict(neuron_config)

    # make a short square pulse. stimulus units should be in Amps.
    stimulus = [0.0] * 100 + [10e-9] * 100 + [0.0] * 100

    # important! set the neuron's dt value for your stimulus in seconds
    neuron.dt = 5e-6

    # simulate the neuron
    output = neuron.run(stimulus)

    expected_fields = {"AScurrents", "grid_spike_times",
                       "interpolated_spike_threshold",
                       "interpolated_spike_times",
                       "interpolated_spike_voltage",
                       "spike_time_steps", "threshold", "voltage"}

    assert expected_fields.difference(output.keys()) == set()
Esempio n. 21
0
def test_run():
    # initialize the neuron
    neuron_config = json_utilities.read(
        os.path.join(OUTPUT_DIR, '%d_neuron_config.json' % NEURONAL_MODEL_ID))
    neuron = GlifNeuron.from_dict(neuron_config)

    # make a short square pulse. stimulus units should be in Amps.
    stimulus = [0.0] * 100 + [10e-9] * 100 + [0.0] * 100

    # important! set the neuron's dt value for your stimulus in seconds
    neuron.dt = 5e-6

    # simulate the neuron
    output = neuron.run(stimulus)

    voltage = output['voltage']
    threshold = output['threshold']

    plt.plot(voltage)
    plt.plot(threshold)
    plt.savefig(os.path.join(OUTPUT_DIR, 'plot.png'))
Esempio n. 22
0
def output(neuron_config_file, ephys_sweeps_file):
    neuron_config = json_utilities.read(neuron_config_file)
    ephys_sweeps = json_utilities.read(ephys_sweeps_file)
    ephys_file_name = 'stimulus.nwb'

    # pull out the stimulus for the first sweep
    ephys_sweep = ephys_sweeps[0]
    ds = NwbDataSet(ephys_file_name)
    data = ds.get_sweep(ephys_sweep['sweep_number'])
    stimulus = data['stimulus']

    # initialize the neuron
    # important! update the neuron's dt for your stimulus
    neuron = GlifNeuron.from_dict(neuron_config)
    neuron.dt = 1.0 / data['sampling_rate']

    # simulate the neuron
    truncate = 56041
    output = neuron.run(stimulus[0:truncate])

    return output
Esempio n. 23
0
def test_run():
    # initialize the neuron
    neuron_config = json_utilities.read(os.path.join(
        OUTPUT_DIR, '%d_neuron_config.json' % NEURONAL_MODEL_ID))
    neuron = GlifNeuron.from_dict(neuron_config)

    # make a short square pulse. stimulus units should be in Amps.
    stimulus = [0.0] * 100 + [10e-9] * 100 + [0.0] * 100

    # important! set the neuron's dt value for your stimulus in seconds
    neuron.dt = 5e-6

    # simulate the neuron
    output = neuron.run(stimulus)

    voltage = output['voltage']
    threshold = output['threshold']

    plt.plot(voltage)
    plt.plot(threshold)
    plt.savefig(os.path.join(OUTPUT_DIR, 'plot.png'))
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
Esempio n. 25
0
    def __init__(self):
        self = self

        import allensdk.core.json_utilities as json_utilities
        from allensdk.model.glif.glif_neuron import GlifNeuron
        try:
            from allensdk.api.queries.glif_api import GlifApi
            from allensdk.core.cell_types_cache import CellTypesCache
            import allensdk.core.json_utilities as json_utilities
            import sciunit
        except:
            import os
            os.system('pip install allensdk')
            from allensdk.api.queries.glif_api import GlifApi
            from allensdk.core.cell_types_cache import CellTypesCache
            import allensdk.core.json_utilities as json_utilities
            os.system('pip install git+https://github.com/scidash/sciunit@dev')


        neuronal_model_id = 566302806
        glif_api = GlifApi()
        nc = glif_api.get_neuron_configs([neuronal_model_id])[neuronal_model_id]
        self.nm = GlifNeuron.from_dict(nc)
Esempio n. 26
0
import allensdk.core.json_utilities as json_utilities
from allensdk.model.glif.glif_neuron import GlifNeuron

# initialize the neuron
neuron_config = json_utilities.read('neuron_config.json')
neuron = GlifNeuron.from_dict(neuron_config)

# make a short square pulse. stimulus units should be in Amps.
stimulus = [ 0.0 ] * 100 + [ 10e-9 ] * 100 + [ 0.0 ] * 100

# important! set the neuron's dt value for your stimulus in seconds
neuron.dt = 5e-6

# simulate the neuron
output = neuron.run(stimulus)

voltage = output['voltage']
threshold = output['threshold']
spike_times = output['interpolated_spike_times']
Esempio n. 27
0
def cycle(specimen_id_directory, ends_with, model_string, data_dict):
    '''Calculates the explained variance ratio for the specified specimen_id_directory and model.
    This function is here for the repetitive nature of the code.  
    inputs:
        specimen_id_directory: string
            path to structured data directory containing neuron_config, preprocessor, etc., files.
        ends_with: string
            end of file searching for:  options "_GLIF1_neuron_config.json","_GLIF2_neuron_config.json" etc.
        model_string: string
            string searching for in model name: options '(LIF)', '(LIF-R)', '(LIF-ASC)', '(LIF-R_ASC)', '(LIF-R_ASC_A')
        data_dict: dictionary
            contains data returned by extract_data
    output:
        writes explained variance ratios into file ending with 'exp_var_ratio_10ms.json' in specimen_id_directory
    '''

    # see if specified model configuration file exist in the data folders
    try:
        file = get_file_path_endswith(specimen_id_directory, ends_with)
    except:
        return

    specimen_id = int(os.path.basename(specimen_id_directory)[:9])
    cre = os.path.basename(specimen_id_directory)[10:]

    #confirming the dt of noise 1 and noise 2 are the same in the file
    if data_dict['n1_dt'] != data_dict['n2_dt']:
        raise Exception('The dt in noise 1 and noise 2 is not the same.')

    # initializing data structures for explained variance calculations
    ev = {}
    ev['after_opt'] = {}
    ev['before_opt'] = {}
    ev['model_spike_times_same_across_sweeps'] = {}
    ev['run_model_spike_times_match_database'] = {}

    # get the spike indicies of the model from the .nwb file in the database
    if specimen_id == 580895033:
        if ends_with == '_GLIF1_neuron_config.json':
            ev['model_spike_times_same_across_sweeps']['n1'] = 1
            ev['model_spike_times_same_across_sweeps']['n2'] = 1
            ev['run_model_spike_times_match_database']['n2'] = True
            ev["n2_after_opt_sanitycheck"] = 0.9838105341820447
            ev["after_opt"]["noise_1"] = 0.9793973377573459
            ev["after_opt"]["noise_2"] = 0.983810807305087
            ev["before_opt"]["noise_1"] = 0.8454442315760935
            ev["before_opt"]["noise_2"] = 0.8493365092125525
        if ends_with == '_GLIF2_neuron_config.json':
            ev['model_spike_times_same_across_sweeps']['n1'] = 1
            ev['model_spike_times_same_across_sweeps']['n2'] = 1
            ev['run_model_spike_times_match_database']['n2'] = True
            ev["n2_after_opt_sanitycheck"] = 0.9889582213030378
            ev["after_opt"]["noise_1"] = 0.9885054832008723
            ev["after_opt"]["noise_2"] = 0.988952726396574
            ev["before_opt"]["noise_1"] = 0.8852614534451949
            ev["before_opt"]["noise_2"] = 0.8882540368687765
        if ends_with == '_GLIF3_neuron_config.json':
            ev['model_spike_times_same_across_sweeps']['n1'] = 1
            ev['model_spike_times_same_across_sweeps']['n2'] = 1
            ev['run_model_spike_times_match_database']['n2'] = True
            ev["n2_after_opt_sanitycheck"] = 0.972059377795663
            ev["after_opt"]["noise_1"] = 0.964542013582842
            ev["after_opt"]["noise_2"] = 0.972065677218419
            ev["before_opt"]["noise_1"] = 0.9175506860780771
            ev["before_opt"]["noise_2"] = 0.9192162154035345
        if ends_with == '_GLIF4_neuron_config.json':
            ev['model_spike_times_same_across_sweeps']['n1'] = 1
            ev['model_spike_times_same_across_sweeps']['n2'] = 1
            ev['run_model_spike_times_match_database']['n2'] = True
            ev["n2_after_opt_sanitycheck"] = 0.9838078849900366
            ev["after_opt"]["noise_1"] = 0.9774371918205483
            ev["after_opt"]["noise_2"] = 0.983816449506429
            ev["before_opt"]["noise_1"] = 0.9481063969607645
            ev["before_opt"]["noise_2"] = 0.952096857211585
        if ends_with == '_GLIF5_neuron_config.json':
            ev['model_spike_times_same_across_sweeps']['n1'] = 1
            ev['model_spike_times_same_across_sweeps']['n2'] = 1
            ev['run_model_spike_times_match_database']['n2'] = True
            ev["n2_after_opt_sanitycheck"] = 0.9836467816928267
            ev["after_opt"]["noise_1"] = 0.9784782997497251
            ev["after_opt"]["noise_2"] = 0.983643486774882
            ev["before_opt"]["noise_1"] = 0.8846618004335125
            ev["before_opt"]["noise_2"] = 0.8904106067655934
        json_utilities.write(
            os.path.join(
                specimen_id_directory,
                str(specimen_id) + '_' + cre + ends_with[:7] +
                'exp_var_ratio_10ms.json'), ev)
        print '\twrote output to ', os.path.join(
            specimen_id_directory,
            str(specimen_id) + '_' + cre + ends_with[:7] +
            'exp_var_ratio_10ms.json')
        return

    model_n1_nwb_ind = get_model_spike_ind_from_nwb(
        ends_with, specimen_id_directory, model_string,
        data_dict['noise1_sweeps'], data_dict['n1_dt'], where_running)[0]

    # get explained variances
    ev['after_opt']['noise_1'] = exVar(data_dict['noise1_spike_ind'],
                                       [model_n1_nwb_ind], [.01],
                                       data_dict['n1_dt'],
                                       len(data_dict['noise1_stim']))[0]
    ev['after_opt']['noise_2'] = get_ev_from_folder(
        ends_with, specimen_id_directory,
        model_string)  # get explained varience ratio from glif api

    #----------------------------------------------------------------------------------------------
    #---------grabbing data along with performing a series of sanity checks for later use-----------
    #----------------------------------------------------------------------------------------------
    neuron_config = json_utilities.read(file)
    neuron_config['dt'] = data_dict['n2_dt']
    neuron = GlifNeuron.from_dict(neuron_config)  #set up model for running
    print '\trunning', specimen_id, 'noise 2 after optimization as a sanity check to compare with what is in database'
    model_n2_after = neuron.run(data_dict['noise2_stim'])  #running model
    print '\tfinished', specimen_id, 'running model on noise 2 after optimization as a sanity check to compare with what is in database'

    # before calculating explained variance this is a sanity check to make sure spike times and steps match with the dt within the same file
    assert model_n2_after['grid_spike_times'] / data_dict[
        'n2_dt'] == model_n2_after['spike_time_steps']

    # calculate the explained variance from the the model run
    ev_GLIF1_n2_after = exVar(data_dict['noise2_spike_ind'],
                              [model_n2_after['spike_time_steps']],
                              [.01], data_dict['n2_dt'],
                              len(data_dict['noise2_stim']))
    ev['n2_after_opt_sanitycheck'] = ev_GLIF1_n2_after[
        0]  #this is from the rerun done here

    # Sanity check to make sure model spike times from the database are all the same..
    # Note that all of these should have been eliminated via the "check_sweeps_and_rm_folders.py" script
    glif_spike_times_n1 = get_model_spike_times_from_nwb(
        ends_with, specimen_id_directory, model_string,
        data_dict['noise1_sweeps'], where_running)
    ev['model_spike_times_same_across_sweeps'][
        'n1'] = check_spike_times_identical(glif_spike_times_n1)

    glif_spike_times_n2 = get_model_spike_times_from_nwb(
        ends_with, specimen_id_directory, model_string,
        data_dict['noise2_sweeps'], where_running)
    ev['model_spike_times_same_across_sweeps'][
        'n2'] = check_spike_times_identical(glif_spike_times_n2)

    # sanity check to make sure calculated model spike times run here match what is in the Allen Institute Cell Types Database.
    # just checking against first sweep since they sweeps should all be identical
    ev['run_model_spike_times_match_database']['n2'] = np.allclose(
        model_n2_after['grid_spike_times'],
        glif_spike_times_n2[0],
        atol=.0001,
        rtol=0,
        equal_nan=True)

    #--------------------------------------------------------
    #--------------------------------------------------------
    #--------------------------------------------------------

    # running and calculating exp var for data before optimization
    neuron_config['dt'] = data_dict['n1_dt']
    neuron_config['coeffs']['th_inf'] = 1.0
    neuron = GlifNeuron.from_dict(neuron_config)
    print '\trunning noise 1', specimen_id, 'before optimization'
    model_n1_before = neuron.run(data_dict['noise1_stim'])
    ev_GLIF1_n1_before = exVar(data_dict['noise1_spike_ind'],
                               [model_n1_before['spike_time_steps']],
                               [.01], data_dict['n1_dt'],
                               len(data_dict['noise1_stim']))
    print '\tfinished noise 1', specimen_id, 'before optimization'

    print '\trunning noise 2', specimen_id, 'before optimization'
    model_n2_before = neuron.run(data_dict['noise2_stim'])
    ev_GLIF1_n2_before = exVar(data_dict['noise2_spike_ind'],
                               [model_n2_before['spike_time_steps']],
                               [.01], data_dict['n2_dt'],
                               len(data_dict['noise2_stim']))
    print '\tfinished noise 2', specimen_id, 'before optimization'

    ev['before_opt']['noise_1'] = ev_GLIF1_n1_before[0]
    ev['before_opt']['noise_2'] = ev_GLIF1_n2_before[0]

    # save the file to the local structured data directory
    json_utilities.write(
        os.path.join(
            specimen_id_directory,
            str(specimen_id) + '_' + cre + ends_with[:7] +
            'exp_var_ratio_10ms.json'), ev)
    print '\twrote output to ', os.path.join(
        specimen_id_directory,
        str(specimen_id) + '_' + cre + ends_with[:7] +
        'exp_var_ratio_10ms.json')
Esempio n. 28
0
 def set_attrs(self, **attrs):
     self.model.attrs.update(attrs)
     self.glif = GlifNeuron.from_dict(attrs)
     return self.glif
Esempio n. 29
0
import allensdk.core.json_utilities as json_utilities

from allensdk.model.glif.glif_neuron import GlifNeuron
from allensdk.model.glif.simulate_neuron import simulate_neuron

neuron_config = json_utilities.read('neuron_config.json')
ephys_sweeps = json_utilities.read('ephys_sweeps.json')
ephys_file_name = 'stimulus.nwb'

neuron = GlifNeuron.from_dict(neuron_config)

sweep_numbers = [ s['sweep_number'] for s in ephys_sweeps 
                  if s['stimulus_units'] == 'Amps' ]
simulate_neuron(neuron, sweep_numbers, ephys_file_name, ephys_file_name, 0.05)
Esempio n. 30
0
class glifBackend(Backend):

    backend = 'glif'
    try:
        from allensdk.api.queries.glif_api import GlifApi
        from allensdk.core.cell_types_cache import CellTypesCache
        import allensdk.core.json_utilities as json_utilities
    except:
        import os
        os.system('pip install allensdk')
        from allensdk.api.queries.glif_api import GlifApi
        from allensdk.core.cell_types_cache import CellTypesCache
        import allensdk.core.json_utilities as json_utilities

    neuronal_model_id = 566302806
    # download model metadata
    glif_api = GlifApi()
    nm = glif_api.get_neuronal_models_by_id([neuronal_model_id])[0]
    # download the model configuration file
    nc = glif_api.get_neuron_configs([neuronal_model_id])[neuronal_model_id]
    neuron_config = glif_api.get_neuron_configs([neuronal_model_id])
    json_utilities.write('neuron_config.json', neuron_config)

    # download information about the cell
    ctc = CellTypesCache()
    ctc.get_ephys_data(nm['specimen_id'], file_name='stimulus.nwb')
    ctc.get_ephys_sweeps(nm['specimen_id'], file_name='ephys_sweeps.json')
    import allensdk.core.json_utilities as json_utilities
    from allensdk.model.glif.glif_neuron import GlifNeuron

    # initialize the neuron
    neuron_config = json_utilities.read('neuron_config.json')
    neuron_config = neuron_config['566302806']

    neuron = GlifNeuron.from_dict(neuron_config)

    def init_backend(self, attrs=None, simulator='neuron', DTC=None):
        from pyNN import neuron
        self.neuron = neuron
        from pyNN.neuron import simulator as sim
        from pyNN.neuron import setup as setup
        from pyNN.neuron import Izhikevich
        from pyNN.neuron import Population
        from pyNN.neuron import DCSource
        self.Izhikevich = Izhikevich
        self.Population = Population
        self.DCSource = DCSource
        self.setup = setup
        self.model_path = None
        self.related_data = {}
        self.lookup = {}
        self.attrs = {}
        super(pyNNBackend, self).init_backend()  #*args, **kwargs)
        if DTC is not None:

            self.set_attrs(**DTC.attrs)

        backend = 'pyNN'

    def get_membrane_potential(self):
        """Must return a neo.core.AnalogSignal.
        And must destroy the hoc vectors that comprise it.
        """
        dt = float(copy.copy(self.neuron.dt))
        data = self.population.get_data().segments[0]
        return data.filter(name="v")[0]

    def _local_run(self):
        '''
        pyNN lazy array demands a minimum population size of 3. Why is that.
        '''
        import numpy as np
        results = {}
        #self.population.record('v')
        #self.population.record('spikes')
        # For ome reason you need to record from all three neurons in a population
        # In order to get the membrane potential from only the stimulated neuron.

        self.population[0:2].record(('v', 'spikes', 'u'))
        '''
        self.Iz.record('v')
        self.Iz.record('spikes')
        # For ome reason you need to record from all three neurons in a population
        # In order to get the membrane potential from only the stimulated neuron.

        self.Iz.record(('v', 'spikes','u'))
        '''
        #self.neuron.run(650.0)
        DURATION = 1000.0
        self.neuron.run(DURATION)

        data = self.population.get_data().segments[0]
        vm = data.filter(name="v")[0]  #/10.0
        results['vm'] = vm
        #print(vm)
        sample_freq = DURATION / len(vm)
        results['t'] = np.arange(0, len(vm), DURATION / len(vm))
        results['run_number'] = results.get('run_number', 0) + 1
        return results

    def load_model(self):
        self.Iz = None
        self.population = None
        self.setup(timestep=0.01, min_delay=1.0)
        import pyNN
        #i_offset=[0.014, 0.0, 0.0]
        pop = self.neuron.Population(
            3,
            pyNN.neuron.Izhikevich(a=0.02,
                                   b=0.2,
                                   c=-65,
                                   d=6,
                                   i_offset=[0.014, -65.0, 0.0]))  #,v=-65))
        self.population = pop

    def set_attrs(self, **attrs):
        #attrs = copy.copy(self.model.attrs)
        self.init_backend()
        #self.set_attrs(**attrs)
        self.model.attrs.update(attrs)
        assert type(self.model.attrs) is not type(None)
        attrs['i_offset'] = None
        attrs_ = {x: attrs[x] for x in ['a', 'b', 'c', 'd', 'i_offset']}
        attrs_['i_offset'] = 0.014  #[0.014,-attrs_['v0'],0.0]
        #self.population[0].initialize()
        self.population[0].set_parameters(**attrs_)

        print(self.population[0].get_parameters())
        self.neuron.h.psection()
        return self

    def inject_square_current(self, current):
        import copy
        attrs = copy.copy(self.model.attrs)
        self.init_backend()
        self.set_attrs(**attrs)
        c = copy.copy(current)
        if 'injected_square_current' in c.keys():
            c = current['injected_square_current']

        c['delay'] = re.sub('\ ms$', '', str(c['delay']))  # take delay
        c['duration'] = re.sub('\ ms$', '', str(c['duration']))
        c['amplitude'] = re.sub('\ pA$', '', str(c['amplitude']))
        stop = float(c['delay']) + float(c['duration'])
        start = float(c['delay'])
        amplitude = float(c['amplitude']) / 1000.0
        #print('amplitude',amplitude)
        electrode = self.neuron.DCSource(start=start,
                                         stop=stop,
                                         amplitude=amplitude)
        print(self.population[0])
        print(type(self.population[0]))
        print(self.population[0].get_parameters())

        electrode.inject_into(self.population[0:1])