Exemplo n.º 1
0
def compare(sim_data_file, show_plot_already=True, dataset=471141261):
    dat_file_name = '../data/%s.dat' % dataset

    x = []
    y = []
    colors = []
    linestyles = []

    data, indeces = pynml.reload_standard_dat_file(dat_file_name)

    for ii in indeces:
        x.append(data['t'])
        y.append(data[ii])
        colors.append('lightgrey')
        linestyles.append('-')

    data, indeces = pynml.reload_standard_dat_file(sim_data_file)

    r = lambda: random.randint(0, 255)

    for ii in indeces:
        x.append(data['t'])
        y.append(data[ii])
        c = '#%02X%02X%02X' % (r(), r(), r())
        colors.append(c)
        linestyles.append('-')

    pynml.generate_plot(x,
                        y,
                        "Comparing tuned cell to: %s" % dat_file_name,
                        xaxis='Input current (nA)',
                        yaxis='Membrane potential (mV)',
                        colors=colors,
                        linestyles=linestyles,
                        show_plot_already=show_plot_already)
def compare(sim_data_file, show_plot_already=True, dataset=471141261):
    dat_file_name = '../data/%s.dat'%dataset
    
    x = []
    y = []
    colors = []
    linestyles = []

    data, indeces = pynml.reload_standard_dat_file(dat_file_name)

    for ii in indeces:
        x.append(data['t'])
        y.append(data[ii])
        colors.append('lightgrey')
        linestyles.append('-')

    data, indeces = pynml.reload_standard_dat_file(sim_data_file)

    r = lambda: random.randint(0,255)

    for ii in indeces:
        x.append(data['t'])
        y.append(data[ii])
        c = '#%02X%02X%02X' % (r(),r(),r())
        colors.append(c)
        linestyles.append('-')

    pynml.generate_plot(x,
                        y, 
                        "Comparing tuned cell to: %s"%dat_file_name, 
                        xaxis = 'Input current (nA)', 
                        yaxis = 'Membrane potential (mV)', 
                        colors = colors, 
                        linestyles = linestyles, 
                        show_plot_already=show_plot_already)
Exemplo n.º 3
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))
Exemplo n.º 4
0
def generate_traces_plot(config,parameter_set,xvals,yvals,info,labels,save,save_fig_path,voltage,muscles):
                         
    file_name = 'traces_%s%s_%s_%s.png'%(('muscles' if muscles else 'neuron'),('' if voltage else '_activity'),config,parameter_set)
    
    pynml.generate_plot(xvals,
                        yvals,
                        info,
                        labels=labels,
                        xaxis="Time (ms)",
                        yaxis="Membrane potential (mV)" if voltage else "Activity",
                        show_plot_already=False,
                        save_figure_to=(None if not save else save_fig_path%(file_name)),
                        cols_in_legend_box=8)
Exemplo n.º 5
0
def plot_sim_data(sweeps_to_tune_against: List, simulation_id: str,
                  memb_pots: Dict) -> None:
    """Plot data from our fitted simulation

    :param simulation_id: string id of simulation
    :type simulation_id: str
    """
    # Plot
    data_array = np.loadtxt("%s.v.dat" % simulation_id)

    # construct data for plotting
    counter = 0
    time_vals_list = []
    sim_v_list = []
    data_v_list = []
    data_t_list = []
    stim_vals = []
    for acq in sweeps_to_tune_against:
        stim_vals.append("{}pA".format(currents[acq]))

        # remains the same for all columns
        time_vals_list.append(data_array[:, 0] * 1000.0)
        sim_v_list.append(data_array[:, counter + 1] * 1000.0)

        data_v_list.append(memb_pots[acq][1])
        data_t_list.append(memb_pots[acq][0])

        counter = counter + 1

    # Model membrane potential plot
    generate_plot(
        xvalues=time_vals_list,
        yvalues=sim_v_list,
        labels=stim_vals,
        title="Membrane potential (model)",
        show_plot_already=False,
        save_figure_to="%s-model-v.png" % simulation_id,
        xaxis="time (ms)",
        yaxis="membrane potential (mV)",
    )
    # data membrane potential plot
    generate_plot(
        xvalues=data_t_list,
        yvalues=data_v_list,
        labels=stim_vals,
        title="Membrane potential (exp)",
        show_plot_already=False,
        save_figure_to="%s-exp-v.png" % simulation_id,
        xaxis="time (ms)",
        yaxis="membrane potential (mV)",
    )
Exemplo n.º 6
0
def generate_traces_plot(config,parameter_set,xvals,yvals,info,labels,save,save_fig_path,voltage,muscles):
                         
    file_name = 'traces_%s%s_%s_%s.png'%(('muscles' if muscles else 'neuron'),('' if voltage else '_activity'),config,parameter_set)
    
    pynml.generate_plot(xvals,
                        yvals,
                        info,
                        labels=labels,
                        xaxis="Time (ms)",
                        yaxis="Membrane potential (mV)" if voltage else "Activity",
                        show_plot_already=False,
                        save_figure_to=(None if not save else save_fig_path%(file_name)),
                        cols_in_legend_box=8,
                        title_above_plot=True)
Exemplo n.º 7
0
def plot_data(sim_id):
    """Plot the sim data.

    Load the data from the file and plot the graph for the membrane potential
    using the pynml generate_plot utility function.

    :sim_id: ID of simulaton

    """
    data_array = np.loadtxt(sim_id + ".dat")
    pynml.generate_plot([data_array[:, 0]], [data_array[:, 1]],
                        "Membrane potential (soma seg 0)",
                        show_plot_already=False,
                        save_figure_to=sim_id + "_seg0_soma0-v.png",
                        xaxis="time (s)",
                        yaxis="membrane potential (V)")
    pynml.generate_plot([data_array[:, 0]], [data_array[:, 2]],
                        "Membrane potential (soma seg 1)",
                        show_plot_already=False,
                        save_figure_to=sim_id + "_seg1_soma0-v.png",
                        xaxis="time (s)",
                        yaxis="membrane potential (V)")
    pynml.generate_plot([data_array[:, 0]], [data_array[:, 3]],
                        "Membrane potential (axon seg 0)",
                        show_plot_already=False,
                        save_figure_to=sim_id + "_seg0_axon0-v.png",
                        xaxis="time (s)",
                        yaxis="membrane potential (V)")
    pynml.generate_plot([data_array[:, 0]], [data_array[:, 4]],
                        "Membrane potential (axon seg 1)",
                        show_plot_already=False,
                        save_figure_to=sim_id + "_seg1_axon0-v.png",
                        xaxis="time (s)",
                        yaxis="membrane potential (V)")
def main(args=None):
    """Main"""

    vs = [(v - 100) * 0.001 for v in range(200)]

    for f in ['IM.channel.nml', 'Kd.channel.nml']:
        nml_doc = pynml.read_neuroml2_file(f)

        for ct in nml_doc.ComponentType:

            ys = []
            for v in vs:
                req_variables = {'v': '%sV' % v, 'vShift': '10mV'}
                vals = pynml.evaluate_component(ct,
                                                req_variables=req_variables)
                print(vals)
                if 'x' in vals:
                    ys.append(vals['x'])
                if 't' in vals:
                    ys.append(vals['t'])
                if 'r' in vals:
                    ys.append(vals['r'])

            ax = pynml.generate_plot([vs], [ys],
                                     "Some traces from %s in %s" %
                                     (ct.name, f),
                                     show_plot_already=False)
            print(vals)

    plt.show()
Exemplo n.º 9
0
def main(args=None):
    """Main"""
    
    vs = [(v-100)*0.001 for v in range(200)]
    
    for f in ['IM.channel.nml','Kd.channel.nml']:
        nml_doc = pynml.read_neuroml2_file(f)

        for ct in nml_doc.ComponentType:

            ys = []
            for v in vs:
                req_variables = {'v':'%sV'%v,'vShift':'10mV'}
                vals = pynml.evaluate_component(ct,req_variables=req_variables)
                print vals
                if 'x' in vals:
                    ys.append(vals['x'])
                if 't' in vals:
                    ys.append(vals['t'])
                if 'r' in vals:
                    ys.append(vals['r'])

            ax = pynml.generate_plot([vs],[ys],          
                             "Some traces from %s in %s"%(ct.name,f),
                             show_plot_already=False )       

            print vals
        
    plt.show()
Exemplo n.º 10
0
def plot_data(sim_id):
    """Plot the sim data.

    Load the data from the file and plot the graph for the membrane potential
    using the pynml generate_plot utility function.

    :sim_id: ID of simulaton

    """
    data_array = np.loadtxt(sim_id + ".dat")
    pynml.generate_plot([data_array[:, 0]], [data_array[:, 1]], "Membrane potential", show_plot_already=False, save_figure_to=sim_id + "-v.png", xaxis="time (s)", yaxis="membrane potential (V)")
    pynml.generate_plot([data_array[:, 0]], [data_array[:, 2]], "channel current", show_plot_already=False, save_figure_to=sim_id + "-i.png", xaxis="time (s)", yaxis="channel current (A)")
    pynml.generate_plot([data_array[:, 0], data_array[:, 0]], [data_array[:, 3], data_array[:, 4]], "current density", labels=["Na", "K"], show_plot_already=False, save_figure_to=sim_id + "-iden.png", xaxis="time (s)", yaxis="current density (A_per_m2)")
Exemplo n.º 11
0
def analyse_spiketime_vs_dt(nml2_file,
                            target,
                            duration,
                            simulator,
                            cell_v_path,
                            dts,
                            verbose=False,
                            spike_threshold_mV=0,
                            show_plot_already=True,
                            save_figure_to=None,
                            num_of_last_spikes=None):

    from pyelectro.analysis import max_min
    import numpy as np

    all_results = {}

    dts = list(np.sort(dts))

    for dt in dts:
        if verbose:
            print_comment_v(" == Generating simulation for dt = %s ms" % dt)
        ref = str("Sim_dt_%s" % dt).replace('.', '_')
        lems_file_name = "LEMS_%s.xml" % ref
        generate_lems_file_for_neuroml(ref,
                                       nml2_file,
                                       target,
                                       duration,
                                       dt,
                                       lems_file_name,
                                       '.',
                                       gen_plots_for_all_v=True,
                                       gen_saves_for_all_v=True,
                                       copy_neuroml=False)

        if simulator == 'jNeuroML':
            results = pynml.run_lems_with_jneuroml(lems_file_name,
                                                   nogui=True,
                                                   load_saved_data=True,
                                                   plot=False,
                                                   verbose=verbose)
        if simulator == 'jNeuroML_NEURON':
            results = pynml.run_lems_with_jneuroml_neuron(lems_file_name,
                                                          nogui=True,
                                                          load_saved_data=True,
                                                          plot=False,
                                                          verbose=verbose)

        print("Results reloaded: %s" % results.keys())

        all_results[dt] = results

    xs = []
    ys = []
    labels = []

    spxs = []
    spys = []
    linestyles = []
    markers = []
    colors = []
    spike_times_final = []
    array_of_num_of_spikes = []

    for dt in dts:
        t = all_results[dt]['t']
        v = all_results[dt][cell_v_path]
        xs.append(t)
        ys.append(v)
        labels.append(dt)

        mm = max_min(v, t, delta=0, peak_threshold=spike_threshold_mV)

        spike_times = mm['maxima_times']

        spike_times_final.append(spike_times)

        array_of_num_of_spikes.append(len(spike_times))

    max_num_of_spikes = max(array_of_num_of_spikes)

    min_dt_spikes = spike_times_final[0]

    bound_dts = [math.log(dts[0]), math.log(dts[-1])]

    if num_of_last_spikes == None:

        num_of_spikes = len(min_dt_spikes)

    else:

        if len(min_dt_spikes) >= num_of_last_spikes:

            num_of_spikes = num_of_last_spikes

        else:

            num_of_spikes = len(min_dt_spikes)

    spike_indices = [(-1) * ind for ind in range(1, num_of_spikes + 1)]

    if len(min_dt_spikes) > abs(spike_indices[-1]):

        earliest_spike_time = min_dt_spikes[spike_indices[-1] - 1]

    else:

        earliest_spike_time = min_dt_spikes[spike_indices[-1]]

    for spike_ind in range(0, max_num_of_spikes):

        spike_time_values = []

        dt_values = []

        for dt in range(0, len(dts)):

            if spike_times_final[dt] != []:

                if len(spike_times_final[dt]) >= spike_ind + 1:

                    if spike_times_final[dt][spike_ind] >= earliest_spike_time:

                        spike_time_values.append(
                            spike_times_final[dt][spike_ind])

                        dt_values.append(math.log(dts[dt]))

        linestyles.append('')

        markers.append('o')

        colors.append('g')

        spxs.append(dt_values)

        spys.append(spike_time_values)

    for last_spike_index in spike_indices:

        vertical_line = [
            min_dt_spikes[last_spike_index], min_dt_spikes[last_spike_index]
        ]

        spxs.append(bound_dts)

        spys.append(vertical_line)

        linestyles.append('--')

        markers.append('')

        colors.append('k')

    pynml.generate_plot(spxs,
                        spys,
                        "Spike times vs dt",
                        colors=colors,
                        linestyles=linestyles,
                        markers=markers,
                        xaxis='ln ( dt (ms) )',
                        yaxis='Spike times (s)',
                        show_plot_already=show_plot_already,
                        save_figure_to=save_figure_to)

    if verbose:
        pynml.generate_plot(xs,
                            ys,
                            "Membrane potentials in %s for %s" %
                            (simulator, dts),
                            labels=labels,
                            show_plot_already=show_plot_already,
                            save_figure_to=save_figure_to)
Exemplo n.º 12
0

tt = read_dat_file(t_nrn_f)
vv = read_dat_file(v_nrn_f)
cc = read_dat_file(ca_nrn_f)
x.append(tt)
y.append(vv)
xc.append(tt)
yc.append(cc)


pynml.generate_plot(
    x,
    y,
    "V from: %s and %s" % (v_nml2_f, v_nrn_f),
    xaxis="Time (ms)",
    yaxis="Membrane potential (mV)",
    labels=["NML2", "NRN"],
    ylim=[-100, 40],
    show_plot_already=False,
)

pynml.generate_plot(
    xc,
    yc,
    "[Ca2+] from: %s and %s" % (ca_nml2_f, ca_nrn_f),
    xaxis="Time (ms)",
    yaxis="Ca conc (mM)",
    labels=["NML2", "NRN"],
    show_plot_already=False,
)
def generate_lems(glif_dir, curr_pA, show_plot=True):

    os.chdir(glif_dir)
    
    with open('model_metadata.json', "r") as json_file:
        model_metadata = json.load(json_file)

    with open('neuron_config.json', "r") as json_file:
        neuron_config = json.load(json_file)

    with open('ephys_sweeps.json', "r") as json_file:
        ephys_sweeps = json.load(json_file)

    template_cell = '''<Lems>

      <%s %s/>

    </Lems>
    '''

    type = '???'
    print(model_metadata['name'])
    if '(LIF)' in model_metadata['name']:
        type = 'glifCell'
    if '(LIF-ASC)' in model_metadata['name']:
        type = 'glifAscCell'
    if '(LIF-R)' in model_metadata['name']:
        type = 'glifRCell'
    if '(LIF-R-ASC)' in model_metadata['name']:
        type = 'glifRAscCell'
    if '(LIF-R-ASC-A)' in model_metadata['name']:
        type = 'glifRAscATCell'
        
    cell_id = 'GLIF_%s'%glif_dir

    attributes = ""

    attributes +=' id="%s"'%cell_id
    attributes +='\n            C="%s F"'%neuron_config["C"]
    attributes +='\n            leakReversal="%s V"'%neuron_config["El"]
    attributes +='\n            reset="%s V"'%neuron_config["El"]
    attributes +='\n            thresh="%s V"'%( float(neuron_config["th_inf"]) * float(neuron_config["coeffs"]["th_inf"]))
    attributes +='\n            leakConductance="%s S"'%(1/float(neuron_config["R_input"]))
    
    if 'Asc' in type:
        attributes +='\n            tau1="%s s"'%neuron_config["asc_tau_array"][0]
        attributes +='\n            tau2="%s s"'%neuron_config["asc_tau_array"][1]
        attributes +='\n            amp1="%s A"'% ( float(neuron_config["asc_amp_array"][0]) * float(neuron_config["coeffs"]["asc_amp_array"][0]) )
        attributes +='\n            amp2="%s A"'% ( float(neuron_config["asc_amp_array"][1]) * float(neuron_config["coeffs"]["asc_amp_array"][1]) )
        
    if 'glifR' in type:
        attributes +='\n            bs="%s per_s"'%neuron_config["threshold_dynamics_method"]["params"]["b_spike"]
        attributes +='\n            deltaThresh="%s V"'%neuron_config["threshold_dynamics_method"]["params"]["a_spike"]
        attributes +='\n            fv="%s"'%neuron_config["voltage_reset_method"]["params"]["a"]
        attributes +='\n            deltaV="%s V"'%neuron_config["voltage_reset_method"]["params"]["b"]
        
    if 'glifRAscATCell' in type:
        attributes +='\n            bv="%s per_s"'%neuron_config["threshold_dynamics_method"]["params"]["b_voltage"]
        attributes +='\n            a="%s per_s"'%neuron_config["threshold_dynamics_method"]["params"]["a_voltage"]
        

    file_contents = template_cell%(type, attributes)

    print(file_contents)

    cell_file_name = '%s.xml'%(cell_id)
    cell_file = open(cell_file_name,'w')
    cell_file.write(file_contents)
    cell_file.close()


    import opencortex.build as oc

    nml_doc, network = oc.generate_network("Test_%s"%glif_dir)

    pop = oc.add_single_cell_population(network,
                                         'pop_%s'%glif_dir,
                                         cell_id)


    pg = oc.add_pulse_generator(nml_doc,
                           id="pg0",
                           delay="100ms",
                           duration="1000ms",
                           amplitude="%s pA"%curr_pA)


    oc.add_inputs_to_population(network,
                                "Stim0",
                                pop,
                                pg.id,
                                all_cells=True)



    nml_file_name = '%s.net.nml'%network.id
    oc.save_network(nml_doc, nml_file_name, validate=True)
    

    thresh = 'thresh'
    if 'glifR' in type:
        thresh = 'threshTotal'

    lems_file_name = oc.generate_lems_simulation(nml_doc, 
                                network, 
                                nml_file_name, 
                                include_extra_lems_files = [cell_file_name,'../GLIFs.xml'],
                                duration =      1200, 
                                dt =            0.01,
                                gen_saves_for_quantities = {'thresh.dat':['pop_%s/0/GLIF_%s/%s'%(glif_dir,glif_dir,thresh)]},
                                gen_plots_for_quantities = {'Threshold':['pop_%s/0/GLIF_%s/%s'%(glif_dir,glif_dir,thresh)]})
    
    results = pynml.run_lems_with_jneuroml(lems_file_name,
                                     nogui=True,
                                     load_saved_data=True)

    print("Ran simulation; results reloaded for: %s"%results.keys())
    
    info = "Model %s; %spA stimulation"%(glif_dir,curr_pA)

    times = [results['t']]
    vs = [results['pop_%s/0/GLIF_%s/v'%(glif_dir,glif_dir)]]
    labels = ['LEMS - jNeuroML']

    original_model_v = 'original.v.dat'
    if os.path.isfile(original_model_v):
        data, indices = pynml.reload_standard_dat_file(original_model_v)
        times.append(data['t'])
        vs.append(data[0])
        labels.append('Allen SDK')


    pynml.generate_plot(times,
                        vs, 
                        "Membrane potential; %s"%info, 
                        xaxis = "Time (s)", 
                        yaxis = "Voltage (V)", 
                        labels = labels,
                        grid = True,
                        show_plot_already=False,
                        save_figure_to='Comparison_%ipA.png'%(curr_pA))

    times = [results['t']]
    vs = [results['pop_%s/0/GLIF_%s/%s'%(glif_dir,glif_dir,thresh)]]
    labels = ['LEMS - jNeuroML']

    original_model_th = 'original.thresh.dat'
    if os.path.isfile(original_model_th):
        data, indeces = pynml.reload_standard_dat_file(original_model_th)
        times.append(data['t'])
        vs.append(data[0])
        labels.append('Allen SDK')


    pynml.generate_plot(times,
                        vs, 
                        "Threshold; %s"%info, 
                        xaxis = "Time (s)", 
                        yaxis = "Voltage (V)", 
                        labels = labels,
                        grid = True,
                        show_plot_already=show_plot,
                        save_figure_to='Comparison_Threshold_%ipA.png'%(curr_pA))
                            
    readme = '''
## Model: %(id)s

### Original model

%(name)s

[Allen Cell Types DB electrophysiology page for specimen](http://celltypes.brain-map.org/mouse/experiment/electrophysiology/%(spec)s)

[Neuron configuration](neuron_config.json); [model metadata](model_metadata.json); [electrophysiology summary](ephys_sweeps.json)

#### Original traces:

**Membrane potential**

Current injection of %(curr)s pA

![Original](MembranePotential_%(curr)spA.png)

**Threshold**

![Threshold](Threshold_%(curr)spA.png)

### Conversion to NeuroML 2

LEMS version of this model: [GLIF_%(id)s.xml](GLIF_%(id)s.xml)

[Definitions of LEMS Component Types](../GLIFs.xml) for GLIFs.

This model can be run locally by installing [jNeuroML](https://github.com/NeuroML/jNeuroML) and running:

    jnml LEMS_Test_%(id)s.xml

#### Comparison:

**Membrane potential**

Current injection of %(curr)s pA

![Comparison](Comparison_%(curr)spA.png)

**Threshold**

![Comparison](Comparison_Threshold_%(curr)spA.png)'''
    
    readme_file = open('README.md','w')
    curr_str = str(curr_pA)
    # @type curr_str str
    if curr_str.endswith('.0'):
        curr_str = curr_str[:-2]
    readme_file.write(readme%{"id":glif_dir,"name":model_metadata['name'],"spec":model_metadata["specimen_id"],"curr":curr_str})
    readme_file.close()

    os.chdir('..')
    
    return model_metadata, neuron_config, ephys_sweeps
def analyse_cell(dataset_id, type, info, nogui = False, densities=False, analysis_dir='../../data/'):
    
    reference = '%s_%s'%(type,dataset_id)
    cell_file = '%s/%s.cell.nml'%(type,reference)
    
    print("====================================\n\n   Analysing cell: %s, dataset %s\n"%(cell_file,dataset_id))
    
    nml_doc = pynml.read_neuroml2_file(cell_file)
    notes = nml_doc.cells[0].notes if len(nml_doc.cells)>0 else nml_doc.izhikevich2007_cells[0].notes
    meta_nml = eval(notes[notes.index('{'):])
    summary = "Fitness: %s (max evals: %s, pop: %s)"%(meta_nml['fitness'],meta_nml['max_evaluations'],meta_nml['population_size'])
    print summary
    
    images = 'summary/%s_%s.png'
    if_iv_data_files = 'summary/%s_%s.dat'
    

    data, v_sub, curents_sub, freqs, curents_spike = get_if_iv_for_dataset('%s%s_analysis.json'%(analysis_dir,dataset_id))
    
    if densities:

        dataset = {}
        seed = meta_nml['seed']
        if isinstance(seed, tuple):
            seed = seed[0]
        layer = str(data['location'].split(',')[-1].strip().replace(' ',''))
        ref = '%s_%s_%s'%(dataset_id,layer,int(seed))

        dataset['id'] = dataset_id
        dataset['reference'] = ref
        metas = ['aibs_cre_line','aibs_dendrite_type','location']
        for m in metas:
            dataset[m] = str(data[m])

        metas2 = ['fitness','population_size','seed']
        for m in metas2:
            dataset[m] = meta_nml[m]
            
        # Assume images below already generated...
        if type=='HH':
            
            
            cell = nml_doc.cells[0]
            
            sgv_files, all_info = generate_channel_density_plots(cell_file, text_densities=True, passives_erevs=True)
            sgv_file =sgv_files[0]
            for c in all_info:
                if c == cell.id:
                    cc = 'tuned_cell_info'
                else:
                    cc = c
                dataset[cc] = all_info[c]
        
            info['datasets'][ref] = dataset
            
        elif type=='Izh':
            
            dataset['tuned_cell_info'] = {}
            izh_cell = nml_doc.izhikevich2007_cells[0]
                        
            for p in ['C','a','b','c','d','k','vpeak','vr','vt']:
            
                dataset['tuned_cell_info'][p] = get_value_in_si(getattr(izh_cell, p))
            
            '''
            sgv_files, all_info = generate_channel_density_plots(cell_file, text_densities=True, passives_erevs=True)
            sgv_file =sgv_files[0]
            for c in all_info:
                if c == cell.id:
                    cc = 'tuned_cell_info'
                else:
                    cc = c
                dataset[cc] = all_info[c]'''
        
        info['datasets'][ref] = dataset
        
    else:

        traces_ax, if_ax, iv_ax = generate_current_vs_frequency_curve(cell_file, 
                                            reference, 
                                            simulator = 'jNeuroML_NEURON',
                                            start_amp_nA =         -0.1, 
                                            end_amp_nA =           0.4, 
                                            step_nA =              0.01, 
                                            analysis_duration =    1000, 
                                            analysis_delay =       50,
                                            plot_voltage_traces =  False,
                                            plot_if =              not nogui,
                                            plot_iv =              not nogui, 
                                            xlim_if =              [-200, 400],
                                            ylim_if =              [-10, 120],
                                            xlim_iv =              [-200, 400],
                                            ylim_iv =              [-120, -40],
                                            save_if_figure_to =    images%(reference, 'if'), 
                                            save_iv_figure_to =    images%(reference, 'iv'),
                                            save_if_data_to =      if_iv_data_files%(reference, 'if'), 
                                            save_iv_data_to =      if_iv_data_files%(reference, 'iv'), 
                                            show_plot_already = False,
                                            return_axes = True)


        iv_ax.plot(curents_sub, v_sub,   color='#ff2222',marker='o', linestyle='',zorder=1)   
        if_ax.plot(curents_spike, freqs ,color='#ff2222',marker='o', linestyle='',zorder=1)

        iv_ax.get_figure().savefig(images%(reference, 'iv'),bbox_inches='tight')
        if_ax.get_figure().savefig(images%(reference, 'if'),bbox_inches='tight')
        
        
        offset = 100 # mV 
        
        ifv_x = []
        ifv_y = []
        markers = []
        lines = []
        colors = []
        
        cols = {'Izh':'r','HH':'g','AllenHH':'b'}
        
        for ii in ['if','iv']:
            for tt in ['Izh','HH','AllenHH']:
                rr = '%s_%s'%(tt,dataset_id)
                f = if_iv_data_files%(rr, ii)
                if os.path.isfile(f):
                    print("--- Opening: %s"%f)
                    data, indeces = reload_standard_dat_file(f)
                    
                    ifv_x.append(data['t'])
                    
                    if ii=='if':
                        ifv_y.append([ff-offset for ff in data[0]])
                    else:
                        ifv_y.append([vv for vv in data[0]])
                        
                    
                    markers.append('')
                    colors.append(cols[tt])
                    lines.append('-')
                    
        ifv_x.append(curents_sub)
        vvsub = [vv for vv in v_sub]
        
        ifv_y.append(vvsub)
        
        sub_color = '#888888'
        markers.append('D')
        colors.append('k')
        lines.append('')
        
        ifv_x.append(curents_spike)
        ifv_y.append([ff-offset for ff in freqs])
        
        markers.append('o')
        colors.append(sub_color)
        lines.append('')
        
        import matplotlib
        import matplotlib.pyplot as plt

        print ifv_x
        print ifv_y
        ylim = [-105, -20]
        font_size = 18
        ax1 = pynml.generate_plot(ifv_x,
                    ifv_y, 
                    summary, 
                    markers=markers,
                    colors=colors,
                    linestyles=lines,
                    show_plot_already=False,
                    xlim = [-100, 400],
                    font_size = font_size,
                    ylim = ylim,
                    title_above_plot=False)
                    
        plt.xlabel('Input current (pA)', fontsize = font_size)
        plt.ylabel("Steady membrane potential (mV)", fontsize = font_size)
        
        ax2 = ax1.twinx()
        plt.ylim([ylim[0]+offset,ylim[1]+offset])
        plt.ylabel('Firing frequency (Hz)', color=sub_color, fontsize = font_size)
        ax2.tick_params(axis='y', colors=sub_color)
        
        
        #plt.axis('off')
        
        plt.savefig(images%(reference, 'if_iv'+"_FIG"),bbox_inches='tight')
        

        temp_dir = 'temp/'

        print("Copying %s to %s"%(cell_file, temp_dir))
        shutil.copy(cell_file, temp_dir)

        net_file = generate_network_for_sweeps(type, dataset_id, '%s.cell.nml'%(reference), reference, temp_dir, data_dir=analysis_dir)

        lems_file_name = 'LEMS_Test_%s_%s.xml'%(type,dataset_id)

        generate_lems_file_for_neuroml('Test_%s_%s'%(dataset_id,type),
                                       net_file,
                                       'network_%s_%s'%(dataset_id,type), 
                                       1500, 
                                       0.01, 
                                       lems_file_name,
                                       temp_dir,
                                       gen_plots_for_all_v=False,
                                       copy_neuroml = False)

        simulator = "jNeuroML_NEURON"

        if simulator == "jNeuroML":
            results = pynml.run_lems_with_jneuroml(temp_dir+lems_file_name, 
                                                    nogui=True, 
                                                    load_saved_data=True, 
                                                    plot=False,
                                                    show_plot_already=False)
        elif simulator == "jNeuroML_NEURON":
            results = pynml.run_lems_with_jneuroml_neuron(temp_dir+lems_file_name, 
                                                    nogui=True, 
                                                    load_saved_data=True, 
                                                    plot=False,
                                                    show_plot_already=False)

        x = []
        y = []

        print results.keys()

        tt = [t*1000 for t in results['t']]
        for i in range(len(results)-1):
            x.append(tt)
            y.append([v*1000 for v in results['Pop0/%i/%s_%s/v'%(i,type,dataset_id)]])

        pynml.generate_plot(x,
                    y, 
                    summary, 
                    show_plot_already=False,
                    ylim = [-120, 60],
                    save_figure_to = images%(reference, 'traces'),
                    title_above_plot=True)
                 
        ax = pynml.generate_plot(x,
                    y, 
                    summary, 
                    show_plot_already=False,
                    ylim = [-120, 60],
                    title_above_plot=False)
                    
        ax.set_xlabel(None)
        ax.set_ylabel(None)
        plt.axis('off')
        
        fig_file = images%(reference, 'traces'+"_FIG")
        plt.savefig(fig_file, bbox_inches='tight', pad_inches=0)
        from PIL import Image
        img = Image.open(fig_file)

        img2 = img.crop((60, 40, 660, 480))
        img2.save(fig_file)
def analyse_cell(dataset_id, type, nogui = False):
    
    
    reference = '%s_%s'%(type,dataset_id)
    cell_file = '%s.cell.nml'%(reference)
    
    images = 'summary/%s_%s.png'
    
    generate_current_vs_frequency_curve(cell_file, 
                                        reference, 
                                        simulator = 'jNeuroML_NEURON',
                                        start_amp_nA =         -0.1, 
                                        end_amp_nA =           0.4, 
                                        step_nA =              0.01, 
                                        analysis_duration =    1000, 
                                        analysis_delay =       50,
                                        plot_voltage_traces =  False,
                                        plot_if =              not nogui,
                                        plot_iv =              not nogui, 
                                        xlim_if =              [-200, 400],
                                        ylim_if =              [-10, 120],
                                        xlim_iv =              [-200, 400],
                                        ylim_iv =              [-120, -40],
                                        save_if_figure_to=images%(reference, 'if'), 
                                        save_iv_figure_to=images%(reference, 'iv'),
                                        show_plot_already = False)
               
    temp_dir = 'temp/'
    
    shutil.copy(cell_file, temp_dir)
    
    net_file = generate_network_for_sweeps(type, dataset_id, '%s.cell.nml'%(reference), reference, temp_dir)
    
    lems_file_name = 'LEMS_Test_%s_%s.xml'%(type,dataset_id)
    
    generate_lems_file_for_neuroml('Test_%s_%s'%(dataset_id,type),
                                   net_file,
                                   'network_%s_%s'%(dataset_id,type), 
                                   1500, 
                                   0.01, 
                                   lems_file_name,
                                   temp_dir,
                                   gen_plots_for_all_v=False,
                                   copy_neuroml = False)
    
    simulator = "jNeuroML_NEURON"
    
    if simulator == "jNeuroML":
        results = pynml.run_lems_with_jneuroml(temp_dir+lems_file_name, 
                                                nogui=True, 
                                                load_saved_data=True, 
                                                plot=False,
                                                show_plot_already=False)
    elif simulator == "jNeuroML_NEURON":
        results = pynml.run_lems_with_jneuroml_neuron(temp_dir+lems_file_name, 
                                                nogui=True, 
                                                load_saved_data=True, 
                                                plot=False,
                                                show_plot_already=False)
                                                
    x = []
    y = []
    
    print results.keys()
    
    tt = [t*1000 for t in results['t']]
    for i in range(len(results)-1):
        x.append(tt)
        y.append([v*1000 for v in results['Pop0/%i/%s_%s/v'%(i,type,dataset_id)]])
        
    pynml.generate_plot(x,
                y, 
                "Cell: %s"%dataset_id, 
                xaxis = "Time (ms)", 
                yaxis = "Membrane potential (mV)",
                show_plot_already=False,
                ylim = [-120, 60],
                save_figure_to = images%(reference, 'traces'))
Exemplo n.º 16
0
def generate(cell_id, duration, reference, 
             Bee=1,
             Ensyn = 10, 
             Erates = [50,100],
             st_onset = 0,
             st_duration = 1e9,
             format='hdf5',
             simulator=None,
             num_processors=1,
             target_group='soma_group',
             temperature='35degC'):

    #Insyn = int(Ensyn * 0.2)
    #bInsyn = int(bEnsyn * 0.2)
    
    cell_file = '../%s.cell.nml'%cell_id
    if '/' in cell_id:
        cell_id=cell_id.split('/')[-1]

    nml_doc, network = oc.generate_network(reference, temperature=temperature)

    oc.include_neuroml2_cell_and_channels(nml_doc,cell_file,cell_id)
    
    
    synAmpaEE = oc.add_exp_one_syn(nml_doc, id="synAmpaEE", gbase="%snS"%Bee,
                             erev="0mV", tau_decay="1ms")


    pop = oc.add_population_in_rectangular_region(network,
                                        'L23_pop',
                                        cell_id,
                                        len(Erates),
                                        0,0,0,
                                        100,100,100)
    
          
    
    to_plot = {'Some_voltages':[]}
    to_save = {'%s_voltages.dat'%cell_id:[]}
    
    interesting_seg_ids = [0,200,1000,2000,2500,2949] # [soma, .. some dends .. , axon]
    interesting_seg_ids = [0] # [soma, .. some dends .. , axon]

    
    for i,r in enumerate(Erates):
        
        syn0 = oc.add_transient_poisson_firing_synapse(nml_doc,
                                           id="%s_stim_%s"%(synAmpaEE.id,r),
                                           average_rate="%s Hz"%r,
                                           synapse_id=synAmpaEE.id,
                                           delay='%s ms'%st_onset,
                                           duration='%s ms'%st_duration)
                                           
        oc.add_targeted_inputs_to_population(network, 
                                             "Esyn_%s"%r,
                                             pop, 
                                             syn0.id, 
                                             segment_group=target_group,
                                             number_per_cell = Ensyn,
                                             all_cells=False,
                                             only_cells=[i])
                                             
        for seg_id in interesting_seg_ids:
            to_plot.values()[0].append('%s/%s/%s/%s/v'%(pop.id, i, pop.component,seg_id))
            to_save.values()[0].append('%s/%s/%s/%s/v'%(pop.id, i, pop.component,seg_id))
            
                                

    nml_file_name = '%s.net.nml'%network.id + ('.h5' if format=='hdf5' else '')
    
    target_dir='./'
    
    oc.save_network(nml_doc, 
                    nml_file_name, 
                    validate=False, 
                    use_subfolder=True,
                    target_dir=target_dir,
                    format=format)



    lems_file_name, lems_sim = oc.generate_lems_simulation(nml_doc,
                                network, 
                                nml_file_name, 
                                duration, 
                                dt = 0.025,
                                target_dir=target_dir,
                                gen_plots_for_all_v = False,
                                plot_all_segments = False,
                                gen_plots_for_quantities = to_plot,   #  Dict with displays vs lists of quantity paths
                                gen_saves_for_all_v = False,
                                save_all_segments = False,
                                gen_saves_for_quantities = to_save,   #  Dict with file names vs lists of quantity paths
                                verbose = True)
                                
    if simulator:
                
        print ("Running %s for %sms in %s"%(lems_file_name, duration, simulator))

        traces, events = oc.simulate_network(lems_file_name,
                 simulator,
                 max_memory='4000M',
                 nogui=True,
                 load_saved_data=True,
                 reload_events=True,
                 plot=False,
                 verbose=True,
                 num_processors=num_processors)
                 
        rates = {}
        tt = [t*1000 for t in traces['t']]
        for tk in traces.keys():
            if tk!='t':
                rate = get_rate_from_trace(tt,[v*1000 for v in traces[tk]])
                print("Cell %s has rate %s Hz"%(tk,rate))
                i = int(tk.split('/')[1])
                rates[Erates[i]]=rate

        print Erates
        print rates

        ax = pynml.generate_plot([Erates],             
                                 [ [rates[r] for r in Erates] ],                
                                 "FF plots",               
                                 xaxis = 'Input frequency (Hz)',        
                                 yaxis = 'Firing frequency (Hz)',  
                                 markers=['o'],
                                 show_plot_already=True)     # Save figure
                                
        file_name = '%s.%s.%ssyns.%s.rates'%(cell_id,target_group,Ensyn,temperature)     
        f = open(file_name,'w')
        for r in Erates:
            f.write('%s\t%s\n'%(r,rates[r]))
        f.close()
        
        print("Finished! Saved rates data to %s"%file_name)
Exemplo n.º 17
0
                                                  'IzPop1[{}]'.format(pre),
                                                  'spike')

lems_simulation_file = simulation.save_to_file()

pynml.run_lems_with_jneuroml_neuron(lems_simulation_file,
                                    max_memory="2G",
                                    nogui=True,
                                    plot=False)

# Load the data from the file and plot the spike times
# using the pynml generate_plot utility function.
data_array_0 = np.loadtxt("%s.0.spikes.dat" % simulation_id)
data_array_1 = np.loadtxt("%s.1.spikes.dat" % simulation_id)
times_0 = data_array_0[:, 1]
times_1 = data_array_1[:, 1]
ids_0 = data_array_0[:, 0]
ids_1 = [id + size0 for id in data_array_1[:, 0]]
pynml.generate_plot(
    [times_0, times_1],
    [ids_0, ids_1],
    "Spike times",
    show_plot_already=False,
    save_figure_to="%s-spikes.png" % simulation_id,
    xaxis="time (s)",
    yaxis="cell ID",
    colors=['b', 'r'],
    linewidths=['0', '0'],
    markers=['.', '.'],
)
Exemplo n.º 18
0
def generate_current_vs_frequency_curve(nml2_file, 
                                        cell_id, 
                                        start_amp_nA, 
                                        end_amp_nA, 
                                        step_nA, 
                                        analysis_duration, 
                                        analysis_delay, 
                                        dt = 0.05,
                                        temperature = "32degC",
                                        spike_threshold_mV=0.,
                                        plot_voltage_traces=False,
                                        plot_if=True,
                                        plot_iv=False,
                                        xlim_if =              None,
                                        ylim_if =              None,
                                        xlim_iv =              None,
                                        ylim_iv =              None,
                                        show_plot_already=True, 
                                        save_if_figure_to=None, 
                                        save_iv_figure_to=None, 
                                        simulator="jNeuroML",
                                        include_included=True):
                                            
                                            
    from pyelectro.analysis import max_min
    from pyelectro.analysis import mean_spike_frequency
    import numpy as np
    
    print_comment_v("Generating FI curve for cell %s in %s using %s (%snA->%snA; %snA steps)"%
        (cell_id, nml2_file, simulator, start_amp_nA, end_amp_nA, step_nA))
    
    sim_id = 'iv_%s'%cell_id
    duration = analysis_duration+analysis_delay
    ls = LEMSSimulation(sim_id, duration, dt)
    
    ls.include_neuroml2_file(nml2_file, include_included=include_included)
    
    stims = []
    amp = start_amp_nA
    while amp<=end_amp_nA : 
        stims.append(amp)
        amp+=step_nA
        
    
    number_cells = len(stims)
    pop = nml.Population(id="population_of_%s"%cell_id,
                        component=cell_id,
                        size=number_cells)
    

    # create network and add populations
    net_id = "network_of_%s"%cell_id
    net = nml.Network(id=net_id, type="networkWithTemperature", temperature=temperature)
    ls.assign_simulation_target(net_id)
    net_doc = nml.NeuroMLDocument(id=net.id)
    net_doc.networks.append(net)
    net_doc.includes.append(nml.IncludeType(nml2_file))
    net.populations.append(pop)
    
    for i in range(number_cells):
        stim_amp = "%snA"%stims[i]
        input_id = ("input_%s"%stim_amp).replace('.','_').replace('-','min')
        pg = nml.PulseGenerator(id=input_id,
                                    delay="0ms",
                                    duration="%sms"%duration,
                                    amplitude=stim_amp)
        net_doc.pulse_generators.append(pg)

        # Add these to cells
        input_list = nml.InputList(id=input_id,
                                 component=pg.id,
                                 populations=pop.id)
        input = nml.Input(id='0', 
                              target="../%s[%i]"%(pop.id, i), 
                              destination="synapses")  
        input_list.input.append(input)
        net.input_lists.append(input_list)
    
    
    net_file_name = '%s.net.nml'%sim_id
    pynml.write_neuroml2_file(net_doc, net_file_name)
    ls.include_neuroml2_file(net_file_name)
    
    disp0 = 'Voltage_display'
    ls.create_display(disp0,"Voltages", "-90", "50")
    of0 = 'Volts_file'
    ls.create_output_file(of0, "%s.v.dat"%sim_id)
    
    for i in range(number_cells):
        ref = "v_cell%i"%i
        quantity = "%s[%i]/v"%(pop.id, i)
        ls.add_line_to_display(disp0, ref, quantity, "1mV", pynml.get_next_hex_color())
    
        ls.add_column_to_output_file(of0, ref, quantity)
    
    lems_file_name = ls.save_to_file()
    
    if simulator == "jNeuroML":
        results = pynml.run_lems_with_jneuroml(lems_file_name, 
                                                nogui=True, 
                                                load_saved_data=True, 
                                                plot=plot_voltage_traces,
                                                show_plot_already=False)
    elif simulator == "jNeuroML_NEURON":
        results = pynml.run_lems_with_jneuroml_neuron(lems_file_name, 
                                                nogui=True, 
                                                load_saved_data=True, 
                                                plot=plot_voltage_traces,
                                                show_plot_already=False)
                                                
    
    #print(results.keys())
    if_results = {}
    iv_results = {}
    for i in range(number_cells):
        t = np.array(results['t'])*1000
        v = np.array(results["%s[%i]/v"%(pop.id, i)])*1000
        
        mm = max_min(v, t, delta=0, peak_threshold=spike_threshold_mV)
        spike_times = mm['maxima_times']
        freq = 0
        if len(spike_times) > 2:
            count = 0
            for s in spike_times:
                if s >= analysis_delay and s < (analysis_duration+analysis_delay):
                    count+=1
            freq = 1000 * count/float(analysis_duration)
                    
        mean_freq = mean_spike_frequency(spike_times) 
        # print("--- %s nA, spike times: %s, mean_spike_frequency: %f, freq (%fms -> %fms): %f"%(stims[i],spike_times, mean_freq, analysis_delay, analysis_duration+analysis_delay, freq))
        if_results[stims[i]] = freq
        
        if freq == 0:
            iv_results[stims[i]] = v[-1]
        
    if plot_if:
        
        stims = sorted(if_results.keys())
        stims_pA = [ii*1000 for ii in stims]
        
        freqs = [if_results[s] for s in stims]
            
        pynml.generate_plot([stims_pA],
                            [freqs], 
                            "Frequency versus injected current for: %s"%nml2_file, 
                            colors = ['k'], 
                            linestyles=['-'],
                            markers=['o'],
                            xaxis = 'Input current (pA)', 
                            yaxis = 'Firing frequency (Hz)',
                            xlim = xlim_if,
                            ylim = ylim_if,
                            grid = True,
                            show_plot_already=False,
                            save_figure_to = save_if_figure_to)
    if plot_iv:
        
        stims = sorted(iv_results.keys())
        stims_pA = [ii*1000 for ii in sorted(iv_results.keys())]
        vs = [iv_results[s] for s in stims]
            
        pynml.generate_plot([stims_pA],
                            [vs], 
                            "Final membrane potential versus injected current for: %s"%nml2_file, 
                            colors = ['k'], 
                            linestyles=['-'],
                            markers=['o'],
                            xaxis = 'Input current (pA)', 
                            yaxis = 'Membrane potential (mV)', 
                            xlim = xlim_iv,
                            ylim = ylim_iv,
                            grid = True,
                            show_plot_already=False,
                            save_figure_to = save_iv_figure_to)
    
    if show_plot_already:
        from matplotlib import pyplot as plt
        plt.show()
        
        
    return if_results
Exemplo n.º 19
0
def analyse_spiketime_vs_dt(nml2_file, 
                            target,
                            duration,
                            simulator,
                            cell_v_path,
                            dts,
                            verbose=False,
                            spike_threshold_mV = 0,
                            show_plot_already=True,
                            save_figure_to=None,
                            num_of_last_spikes=None):
                                
    from pyelectro.analysis import max_min
    import numpy as np
    
    all_results = {}
    
    dts=list(np.sort(dts))
    
    for dt in dts:
        if verbose:
            print_comment_v(" == Generating simulation for dt = %s ms"%dt)
        ref = str("Sim_dt_%s"%dt).replace('.','_')
        lems_file_name = "LEMS_%s.xml"%ref
        generate_lems_file_for_neuroml(ref, 
                                   nml2_file, 
                                   target, 
                                   duration, 
                                   dt, 
                                   lems_file_name,
                                   '.',
                                   gen_plots_for_all_v = True,
                                   gen_saves_for_all_v = True,
                                   copy_neuroml = False,
                                   seed=None)
                                   
        if simulator == 'jNeuroML':
             results = pynml.run_lems_with_jneuroml(lems_file_name, nogui=True, load_saved_data=True, plot=False, verbose=verbose)
        if simulator == 'jNeuroML_NEURON':
             results = pynml.run_lems_with_jneuroml_neuron(lems_file_name, nogui=True, load_saved_data=True, plot=False, verbose=verbose)
             
        print("Results reloaded: %s"%results.keys())
             
        all_results[dt] = results
        
    xs = []
    ys = []
    labels = []
    
    spxs = []
    spys = []
    linestyles = []
    markers = []
    colors=[]
    spike_times_final=[]
    array_of_num_of_spikes=[]
    
    for dt in dts:
        t = all_results[dt]['t']
        v = all_results[dt][cell_v_path]
        xs.append(t)
        ys.append(v)
        labels.append(dt)
        
        mm = max_min(v, t, delta=0, peak_threshold=spike_threshold_mV)
        
        spike_times = mm['maxima_times']
        
        spike_times_final.append(spike_times)
        
        array_of_num_of_spikes.append(len(spike_times))
        
    max_num_of_spikes=max(array_of_num_of_spikes)
    
    min_dt_spikes=spike_times_final[0]
    
    bound_dts=[math.log(dts[0]),math.log(dts[-1])]
    
    if num_of_last_spikes == None:
    
       num_of_spikes=len(min_dt_spikes)
       
    else:
       
       if len(min_dt_spikes) >=num_of_last_spikes:
       
          num_of_spikes=num_of_last_spikes
          
       else:
       
          num_of_spikes=len(min_dt_spikes)
    
    spike_indices=[(-1)*ind for ind in range(1,num_of_spikes+1) ]
    
    if len(min_dt_spikes) > abs(spike_indices[-1]):
    
       earliest_spike_time=min_dt_spikes[spike_indices[-1]-1]
       
    else:
     
       earliest_spike_time=min_dt_spikes[spike_indices[-1]]
       
    for spike_ind in range(0,max_num_of_spikes):
    
        spike_time_values=[]
        
        dt_values=[]
        
        for dt in range(0,len(dts)):
        
            if spike_times_final[dt] !=[]:
           
               if len(spike_times_final[dt]) >= spike_ind+1:
               
                  if spike_times_final[dt][spike_ind] >= earliest_spike_time:
             
                     spike_time_values.append(spike_times_final[dt][spike_ind])
               
                     dt_values.append(math.log(dts[dt]))       
        
        linestyles.append('')
               
        markers.append('o')
       
        colors.append('g')
       
        spxs.append(dt_values)
       
        spys.append(spike_time_values)
    
    for last_spike_index in spike_indices:
       
       vertical_line=[min_dt_spikes[last_spike_index],min_dt_spikes[last_spike_index] ]
          
       spxs.append(bound_dts)
          
       spys.append(vertical_line)
          
       linestyles.append('--')
          
       markers.append('')
       
       colors.append('k')
    
    pynml.generate_plot(spxs, 
          spys, 
          "Spike times vs dt",
          colors=colors,
          linestyles = linestyles,
          markers = markers,
          xaxis = 'ln ( dt (ms) )', 
          yaxis = 'Spike times (s)',
          show_plot_already=show_plot_already,
          save_figure_to=save_figure_to) 
    
    if verbose:
        pynml.generate_plot(xs, 
                  ys, 
                  "Membrane potentials in %s for %s"%(simulator,dts),
                  labels = labels,
                  show_plot_already=show_plot_already,
                  save_figure_to=save_figure_to)
p =[]
f = []
m = []
c = []

for r in reports:
    rep = reports[r]
    ptype = rep['Prototype'] if rep['Prototype']!='Izhikevich' else 'Izh'
    print("%s:\t%s %s\t %s(p%s)\t%s"%(r,ptype,rep['dataset'],rep['max_evaluations'],rep['population_size'],rep['fitness']))
    p.append([rep['population_size']])
    f.append([rep['fitness']])
    m.append('.' if rep['Prototype']=='Izhikevich' else ('o' if rep['Prototype']=='AllenHH' else 'x'))
    c.append('k' if rep['Prototype']=='Izhikevich' else ('b' if rep['Prototype']=='AllenHH' else 'r'))
    
print p
print f
print m
print c

from pyneuroml import pynml
pynml.generate_plot(p,                        # Add 2 sets of x values
                         f,                   # Add 2 sets of y values
                         "Some traces",                  # Title
                         markers = m,
                         colors = c,
                         logx = True,
                         logy = True,
                         show_plot_already=True)    

      
Exemplo n.º 21
0
    for line in f:
        if len(line.strip()) > 0: vals.append(float(line.strip()))
    return vals


tt = read_dat_file(t_nrn_f)
vv = read_dat_file(v_nrn_f)
cc = read_dat_file(ca_nrn_f)
x.append(tt)
y.append(vv)
xc.append(tt)
yc.append(cc)

pynml.generate_plot(x,
                    y,
                    "V from: %s and %s" % (v_nml2_f, v_nrn_f),
                    xaxis="Time (ms)",
                    yaxis="Membrane potential (mV)",
                    labels=["NML2", "NRN"],
                    ylim=[-100, 40],
                    show_plot_already=False)

pynml.generate_plot(xc,
                    yc,
                    "[Ca2+] from: %s and %s" % (ca_nml2_f, ca_nrn_f),
                    xaxis="Time (ms)",
                    yaxis="Ca conc (mM)",
                    labels=["NML2", "NRN"],
                    show_plot_already=False)

plt.show()
Exemplo n.º 22
0
    #plot_cell_firing('../cells/AllenHH/AllenHH_477127614.cell.nml', num_processors=num_processors, show_plot=show)

    #plot_cell_firing('../cells/AllenHH/AllenHH_476686112.cell.nml', num_processors=num_processors, show_plot=show)

    #plot_cell_firing('../cells/BBP/cNAC187_L23_NBC_9d37c4b1f8_0_0.cell.nml', num_processors=num_processors, show_plot=show)

    plot_cell_firing('../cells/SmithEtAl2013/L23_Retuned_477127614.cell.nml',
                     num_processors=num_processors,
                     show_plot=show)

refs = [('AllenHH_477127614', 'L23_Retuned_477127614'),
        ('AllenHH_476686112', 'cNAC187_L23_NBC_9d37c4b1f8_0_0')]

for ref in refs:

    for ii in ['IF', 'IV']:
        point_neuron, cols = pynml.reload_standard_dat_file('%s_%s.dat' %
                                                            (ref[0], ii))
        detailed_neuron, cols = pynml.reload_standard_dat_file('%s_%s.dat' %
                                                               (ref[1], ii))
        print detailed_neuron

        pynml.generate_plot([point_neuron['t'], detailed_neuron['t']],
                            [point_neuron[0], detailed_neuron[0]],
                            "2 cells: %s, %s" % ref,
                            markers=['o', 'o'],
                            labels=[ref[0], ref[1]],
                            show_plot_already=False)

plt.show()
def analyse_cell(dataset_id,
                 type,
                 info,
                 nogui=False,
                 densities=False,
                 analysis_dir='../../data/'):

    reference = '%s_%s' % (type, dataset_id)
    cell_file = '%s/%s.cell.nml' % (type, reference)

    print(
        "====================================\n\n   Analysing cell: %s, dataset %s\n"
        % (cell_file, dataset_id))

    nml_doc = pynml.read_neuroml2_file(cell_file)
    notes = nml_doc.cells[0].notes if len(
        nml_doc.cells) > 0 else nml_doc.izhikevich2007_cells[0].notes
    meta_nml = eval(notes[notes.index('{'):])
    summary = "Fitness: %s (max evals: %s, pop: %s)" % (
        meta_nml['fitness'], meta_nml['max_evaluations'],
        meta_nml['population_size'])
    print summary

    images = 'summary/%s_%s.png'
    if_iv_data_files = 'summary/%s_%s.dat'

    data, v_sub, curents_sub, freqs, curents_spike = get_if_iv_for_dataset(
        '%s%s_analysis.json' % (analysis_dir, dataset_id))

    if densities:

        dataset = {}
        seed = meta_nml['seed']
        if isinstance(seed, tuple):
            seed = seed[0]
        layer = str(data['location'].split(',')[-1].strip().replace(' ', ''))
        ref = '%s_%s_%s' % (dataset_id, layer, int(seed))

        dataset['id'] = dataset_id
        dataset['reference'] = ref
        metas = ['aibs_cre_line', 'aibs_dendrite_type', 'location']
        for m in metas:
            dataset[m] = str(data[m])

        metas2 = ['fitness', 'population_size', 'seed']
        for m in metas2:
            dataset[m] = meta_nml[m]

        # Assume images below already generated...
        if type == 'HH':

            cell = nml_doc.cells[0]

            sgv_files, all_info = generate_channel_density_plots(
                cell_file, text_densities=True, passives_erevs=True)
            sgv_file = sgv_files[0]
            for c in all_info:
                if c == cell.id:
                    cc = 'tuned_cell_info'
                else:
                    cc = c
                dataset[cc] = all_info[c]

            info['datasets'][ref] = dataset

        elif type == 'Izh':

            dataset['tuned_cell_info'] = {}
            izh_cell = nml_doc.izhikevich2007_cells[0]

            for p in ['C', 'a', 'b', 'c', 'd', 'k', 'vpeak', 'vr', 'vt']:

                dataset['tuned_cell_info'][p] = get_value_in_si(
                    getattr(izh_cell, p))
            '''
            sgv_files, all_info = generate_channel_density_plots(cell_file, text_densities=True, passives_erevs=True)
            sgv_file =sgv_files[0]
            for c in all_info:
                if c == cell.id:
                    cc = 'tuned_cell_info'
                else:
                    cc = c
                dataset[cc] = all_info[c]'''

        info['datasets'][ref] = dataset

    else:

        traces_ax, if_ax, iv_ax = generate_current_vs_frequency_curve(
            cell_file,
            reference,
            simulator='jNeuroML_NEURON',
            start_amp_nA=-0.1,
            end_amp_nA=0.4,
            step_nA=0.01,
            analysis_duration=1000,
            analysis_delay=50,
            plot_voltage_traces=False,
            plot_if=not nogui,
            plot_iv=not nogui,
            xlim_if=[-200, 400],
            ylim_if=[-10, 120],
            xlim_iv=[-200, 400],
            ylim_iv=[-120, -40],
            save_if_figure_to=images % (reference, 'if'),
            save_iv_figure_to=images % (reference, 'iv'),
            save_if_data_to=if_iv_data_files % (reference, 'if'),
            save_iv_data_to=if_iv_data_files % (reference, 'iv'),
            show_plot_already=False,
            return_axes=True)

        iv_ax.plot(curents_sub,
                   v_sub,
                   color='#ff2222',
                   marker='o',
                   linestyle='',
                   zorder=1)
        if_ax.plot(curents_spike,
                   freqs,
                   color='#ff2222',
                   marker='o',
                   linestyle='',
                   zorder=1)

        iv_ax.get_figure().savefig(images % (reference, 'iv'),
                                   bbox_inches='tight')
        if_ax.get_figure().savefig(images % (reference, 'if'),
                                   bbox_inches='tight')

        offset = 100  # mV

        ifv_x = []
        ifv_y = []
        markers = []
        lines = []
        colors = []

        cols = {'Izh': 'r', 'HH': 'g', 'AllenHH': 'b'}

        for ii in ['if', 'iv']:
            for tt in ['Izh', 'HH', 'AllenHH']:
                rr = '%s_%s' % (tt, dataset_id)
                f = if_iv_data_files % (rr, ii)
                if os.path.isfile(f):
                    print("--- Opening: %s" % f)
                    data, indeces = reload_standard_dat_file(f)

                    ifv_x.append(data['t'])

                    if ii == 'if':
                        ifv_y.append([ff - offset for ff in data[0]])
                    else:
                        ifv_y.append([vv for vv in data[0]])

                    markers.append('')
                    colors.append(cols[tt])
                    lines.append('-')

        ifv_x.append(curents_sub)
        vvsub = [vv for vv in v_sub]

        ifv_y.append(vvsub)

        sub_color = '#888888'
        markers.append('D')
        colors.append('k')
        lines.append('')

        ifv_x.append(curents_spike)
        ifv_y.append([ff - offset for ff in freqs])

        markers.append('o')
        colors.append(sub_color)
        lines.append('')

        import matplotlib
        import matplotlib.pyplot as plt

        print ifv_x
        print ifv_y
        ylim = [-105, -20]
        font_size = 18
        ax1 = pynml.generate_plot(ifv_x,
                                  ifv_y,
                                  summary,
                                  markers=markers,
                                  colors=colors,
                                  linestyles=lines,
                                  show_plot_already=False,
                                  xlim=[-100, 400],
                                  font_size=font_size,
                                  ylim=ylim,
                                  title_above_plot=False)

        plt.xlabel('Input current (pA)', fontsize=font_size)
        plt.ylabel("Steady membrane potential (mV)", fontsize=font_size)

        ax2 = ax1.twinx()
        plt.ylim([ylim[0] + offset, ylim[1] + offset])
        plt.ylabel('Firing frequency (Hz)',
                   color=sub_color,
                   fontsize=font_size)
        ax2.tick_params(axis='y', colors=sub_color)

        #plt.axis('off')

        plt.savefig(images % (reference, 'if_iv' + "_FIG"),
                    bbox_inches='tight')

        temp_dir = 'temp/'

        print("Copying %s to %s" % (cell_file, temp_dir))
        shutil.copy(cell_file, temp_dir)

        net_file = generate_network_for_sweeps(type,
                                               dataset_id,
                                               '%s.cell.nml' % (reference),
                                               reference,
                                               temp_dir,
                                               data_dir=analysis_dir)

        lems_file_name = 'LEMS_Test_%s_%s.xml' % (type, dataset_id)

        generate_lems_file_for_neuroml('Test_%s_%s' % (dataset_id, type),
                                       net_file,
                                       'network_%s_%s' % (dataset_id, type),
                                       1500,
                                       0.01,
                                       lems_file_name,
                                       temp_dir,
                                       gen_plots_for_all_v=False,
                                       copy_neuroml=False)

        simulator = "jNeuroML_NEURON"

        if simulator == "jNeuroML":
            results = pynml.run_lems_with_jneuroml(temp_dir + lems_file_name,
                                                   nogui=True,
                                                   load_saved_data=True,
                                                   plot=False,
                                                   show_plot_already=False)
        elif simulator == "jNeuroML_NEURON":
            results = pynml.run_lems_with_jneuroml_neuron(
                temp_dir + lems_file_name,
                nogui=True,
                load_saved_data=True,
                plot=False,
                show_plot_already=False)

        x = []
        y = []

        print results.keys()

        tt = [t * 1000 for t in results['t']]
        for i in range(len(results) - 1):
            x.append(tt)
            y.append([
                v * 1000
                for v in results['Pop0/%i/%s_%s/v' % (i, type, dataset_id)]
            ])

        pynml.generate_plot(x,
                            y,
                            summary,
                            show_plot_already=False,
                            ylim=[-120, 60],
                            save_figure_to=images % (reference, 'traces'),
                            title_above_plot=True)

        ax = pynml.generate_plot(x,
                                 y,
                                 summary,
                                 show_plot_already=False,
                                 ylim=[-120, 60],
                                 title_above_plot=False)

        ax.set_xlabel(None)
        ax.set_ylabel(None)
        plt.axis('off')

        fig_file = images % (reference, 'traces' + "_FIG")
        plt.savefig(fig_file, bbox_inches='tight', pad_inches=0)
        from PIL import Image
        img = Image.open(fig_file)

        img2 = img.crop((60, 40, 660, 480))
        img2.save(fig_file)
Exemplo n.º 24
0
def generate_current_vs_frequency_curve(nml2_file, 
                                        cell_id, 
                                        start_amp_nA =          -0.1, 
                                        end_amp_nA =            0.1,
                                        step_nA =               0.01, 
                                        custom_amps_nA =        [], 
                                        analysis_duration =     1000, 
                                        analysis_delay =        0, 
                                        pre_zero_pulse =        0,
                                        post_zero_pulse =       0,
                                        dt =                    0.05,
                                        temperature =           "32degC",
                                        spike_threshold_mV =    0.,
                                        plot_voltage_traces =   False,
                                        plot_if =               True,
                                        plot_iv =               False,
                                        xlim_if =               None,
                                        ylim_if =               None,
                                        xlim_iv =               None,
                                        ylim_iv =               None,
                                        label_xaxis =           True,
                                        label_yaxis =           True,
                                        show_volts_label =      True,
                                        grid =                  True,
                                        font_size =             12,
                                        if_iv_color =           'k',
                                        linewidth =             1,
                                        bottom_left_spines_only = False,
                                        show_plot_already =     True, 
                                        save_voltage_traces_to = None, 
                                        save_if_figure_to =     None, 
                                        save_iv_figure_to =     None, 
                                        save_if_data_to =       None, 
                                        save_iv_data_to =       None, 
                                        simulator =             "jNeuroML",
                                        num_processors =        1,
                                        include_included =      True,
                                        title_above_plot =      False,
                                        return_axes =           False,
                                        verbose =               False):
                                            
    print_comment("Running generate_current_vs_frequency_curve() on %s (%s)"%(nml2_file,os.path.abspath(nml2_file)), verbose)                
    from pyelectro.analysis import max_min
    from pyelectro.analysis import mean_spike_frequency
    import numpy as np
    traces_ax = None
    if_ax = None
    iv_ax = None
    
    
    sim_id = 'iv_%s'%cell_id
    total_duration = pre_zero_pulse+analysis_duration+analysis_delay+post_zero_pulse
    pulse_duration = analysis_duration+analysis_delay
    end_stim = pre_zero_pulse+analysis_duration+analysis_delay
    ls = LEMSSimulation(sim_id, total_duration, dt)
    
    ls.include_neuroml2_file(nml2_file, include_included=include_included)
    
    stims = []
    if len(custom_amps_nA)>0:
        stims = [float(a) for a in custom_amps_nA]
        stim_info = ['%snA'%float(a) for a in custom_amps_nA]
    else:
        amp = start_amp_nA
        while amp<=end_amp_nA : 
            stims.append(amp)
            amp+=step_nA
        
        stim_info = '(%snA->%snA; %s steps of %snA; %sms)'%(start_amp_nA, end_amp_nA, len(stims), step_nA, total_duration)
        
    print_comment_v("Generating an IF curve for cell %s in %s using %s %s"%
        (cell_id, nml2_file, simulator, stim_info))
        
    number_cells = len(stims)
    pop = nml.Population(id="population_of_%s"%cell_id,
                        component=cell_id,
                        size=number_cells)
    

    # create network and add populations
    net_id = "network_of_%s"%cell_id
    net = nml.Network(id=net_id, type="networkWithTemperature", temperature=temperature)
    ls.assign_simulation_target(net_id)
    net_doc = nml.NeuroMLDocument(id=net.id)
    net_doc.networks.append(net)
    net_doc.includes.append(nml.IncludeType(nml2_file))
    net.populations.append(pop)

    for i in range(number_cells):
        stim_amp = "%snA"%stims[i]
        input_id = ("input_%s"%stim_amp).replace('.','_').replace('-','min')
        pg = nml.PulseGenerator(id=input_id,
                                    delay="%sms"%pre_zero_pulse,
                                    duration="%sms"%pulse_duration,
                                    amplitude=stim_amp)
        net_doc.pulse_generators.append(pg)

        # Add these to cells
        input_list = nml.InputList(id=input_id,
                                 component=pg.id,
                                 populations=pop.id)
        input = nml.Input(id='0', 
                              target="../%s[%i]"%(pop.id, i), 
                              destination="synapses")  
        input_list.input.append(input)
        net.input_lists.append(input_list)
    
    
    net_file_name = '%s.net.nml'%sim_id
    pynml.write_neuroml2_file(net_doc, net_file_name)
    ls.include_neuroml2_file(net_file_name)
    
    disp0 = 'Voltage_display'
    ls.create_display(disp0,"Voltages", "-90", "50")
    of0 = 'Volts_file'
    ls.create_output_file(of0, "%s.v.dat"%sim_id)
    
    for i in range(number_cells):
        ref = "v_cell%i"%i
        quantity = "%s[%i]/v"%(pop.id, i)
        ls.add_line_to_display(disp0, ref, quantity, "1mV", pynml.get_next_hex_color())
    
        ls.add_column_to_output_file(of0, ref, quantity)
    
    lems_file_name = ls.save_to_file()
    
    print_comment("Written LEMS file %s (%s)"%(lems_file_name,os.path.abspath(lems_file_name)), verbose)   

    if simulator == "jNeuroML":
        results = pynml.run_lems_with_jneuroml(lems_file_name, 
                                                nogui=True, 
                                                load_saved_data=True, 
                                                plot=False,
                                                show_plot_already=False,
                                                verbose=verbose)
    elif simulator == "jNeuroML_NEURON":
        results = pynml.run_lems_with_jneuroml_neuron(lems_file_name, 
                                                nogui=True, 
                                                load_saved_data=True, 
                                                plot=False,
                                                show_plot_already=False,
                                                verbose=verbose)
    elif simulator == "jNeuroML_NetPyNE":
        results = pynml.run_lems_with_jneuroml_netpyne(lems_file_name, 
                                                nogui=True, 
                                                load_saved_data=True, 
                                                plot=False,
                                                show_plot_already=False,
                                                num_processors = num_processors,
                                                verbose=verbose)
    else:
        raise Exception("Sorry, cannot yet run current vs frequency analysis using simulator %s"%simulator)
    
    print_comment("Completed run in simulator %s (results: %s)"%(simulator,results.keys()), verbose)  
        
    #print(results.keys())
    times_results = []
    volts_results = []
    volts_labels = []
    if_results = {}
    iv_results = {}
    for i in range(number_cells):
        t = np.array(results['t'])*1000
        v = np.array(results["%s[%i]/v"%(pop.id, i)])*1000

        if plot_voltage_traces:
            times_results.append(t)
            volts_results.append(v)
            volts_labels.append("%s nA"%stims[i])
            
        mm = max_min(v, t, delta=0, peak_threshold=spike_threshold_mV)
        spike_times = mm['maxima_times']
        freq = 0
        if len(spike_times) > 2:
            count = 0
            for s in spike_times:
                if s >= pre_zero_pulse + analysis_delay and s < (pre_zero_pulse + analysis_duration+analysis_delay):
                    count+=1
            freq = 1000 * count/float(analysis_duration)
                    
        mean_freq = mean_spike_frequency(spike_times) 
        #print("--- %s nA, spike times: %s, mean_spike_frequency: %f, freq (%fms -> %fms): %f"%(stims[i],spike_times, mean_freq, analysis_delay, analysis_duration+analysis_delay, freq))
        if_results[stims[i]] = freq
        
        if freq == 0:
            if post_zero_pulse==0:
                iv_results[stims[i]] = v[-1]
            else:
                v_end = None
                for j in range(len(t)):
                    if v_end==None and t[j]>=end_stim:
                        v_end = v[j]
                iv_results[stims[i]] = v_end
            
    if plot_voltage_traces:
            
        traces_ax = pynml.generate_plot(times_results,
                            volts_results, 
                            "Membrane potential traces for: %s"%nml2_file, 
                            xaxis = 'Time (ms)' if label_xaxis else ' ', 
                            yaxis = 'Membrane potential (mV)' if label_yaxis else '',
                            xlim = [total_duration*-0.05,total_duration*1.05],
                            show_xticklabels = label_xaxis,
                            font_size = font_size,
                            bottom_left_spines_only = bottom_left_spines_only,
                            grid = False,
                            labels = volts_labels if show_volts_label else [],
                            show_plot_already=False,
                            save_figure_to = save_voltage_traces_to,
                            title_above_plot = title_above_plot,
                            verbose=verbose)
    
        
    if plot_if:
        
        stims = sorted(if_results.keys())
        stims_pA = [ii*1000 for ii in stims]
        
        freqs = [if_results[s] for s in stims]
        
        if_ax = pynml.generate_plot([stims_pA],
                            [freqs], 
                            "Firing frequency versus injected current for: %s"%nml2_file, 
                            colors = [if_iv_color], 
                            linestyles=['-'],
                            markers=['o'],
                            linewidths = [linewidth],
                            xaxis = 'Input current (pA)' if label_xaxis else ' ', 
                            yaxis = 'Firing frequency (Hz)' if label_yaxis else '',
                            xlim = xlim_if,
                            ylim = ylim_if,
                            show_xticklabels = label_xaxis,
                            show_yticklabels = label_yaxis,
                            font_size = font_size,
                            bottom_left_spines_only = bottom_left_spines_only,
                            grid = grid,
                            show_plot_already=False,
                            save_figure_to = save_if_figure_to,
                            title_above_plot = title_above_plot,
                            verbose=verbose)
                            
        if save_if_data_to:
            with open(save_if_data_to,'w') as if_file:
                for i in range(len(stims_pA)):
                    if_file.write("%s\t%s\n"%(stims_pA[i],freqs[i]))
    if plot_iv:
        
        stims = sorted(iv_results.keys())
        stims_pA = [ii*1000 for ii in sorted(iv_results.keys())]
        vs = [iv_results[s] for s in stims]
        
        xs = []
        ys = []
        xs.append([])
        ys.append([])
        
        for si in range(len(stims)):
            stim = stims[si]
            if len(custom_amps_nA)==0 and si>1 and (stims[si]-stims[si-1])>step_nA*1.01:
                xs.append([])
                ys.append([])
                
            xs[-1].append(stim*1000)
            ys[-1].append(iv_results[stim])
            
        iv_ax = pynml.generate_plot(xs,
                            ys, 
                            "V at %sms versus I below threshold for: %s"%(end_stim,nml2_file), 
                            colors = [if_iv_color for s in xs], 
                            linestyles=['-' for s in xs],
                            markers=['o' for s in xs],
                            xaxis = 'Input current (pA)' if label_xaxis else '', 
                            yaxis = 'Membrane potential (mV)' if label_yaxis else '', 
                            xlim = xlim_iv,
                            ylim = ylim_iv,
                            show_xticklabels = label_xaxis,
                            show_yticklabels = label_yaxis,
                            font_size = font_size,
                            linewidths = [linewidth for s in xs],
                            bottom_left_spines_only = bottom_left_spines_only,
                            grid = grid,
                            show_plot_already=False,
                            save_figure_to = save_iv_figure_to,
                            title_above_plot = title_above_plot,
                            verbose=verbose)
                            
                            
        if save_iv_data_to:
            with open(save_iv_data_to,'w') as iv_file:
                for i in range(len(stims_pA)):
                    iv_file.write("%s\t%s\n"%(stims_pA[i],vs[i]))
    
    if show_plot_already:
        from matplotlib import pyplot as plt
        plt.show()
        
    if return_axes:
        return traces_ax, if_ax, iv_ax
        
    return if_results
c = []

for r in reports:
    rep = reports[r]
    ptype = rep['Prototype'] if rep['Prototype'] != 'Izhikevich' else 'Izh'
    print("%s:\t%s %s\t %s(p%s)\t%s" %
          (r, ptype, rep['dataset'], rep['max_evaluations'],
           rep['population_size'], rep['fitness']))
    p.append([rep['population_size']])
    f.append([rep['fitness']])
    m.append('.' if rep['Prototype'] == 'Izhikevich' else (
        'o' if rep['Prototype'] == 'AllenHH' else 'x'))
    c.append('k' if rep['Prototype'] == 'Izhikevich' else (
        'b' if rep['Prototype'] == 'AllenHH' else 'r'))

print p
print f
print m
print c

from pyneuroml import pynml
pynml.generate_plot(
    p,  # Add 2 sets of x values
    f,  # Add 2 sets of y values
    "Some traces",  # Title
    markers=m,
    colors=c,
    logx=True,
    logy=True,
    show_plot_already=True)
    x = []
    y = []
    for i in indices:
        x.append(data['t'])
        y.append(data[i])

    totals = []
    for j in range(len(data['t'])):
        tot = 0
        for i in indices:
            tot+=data[i][j]
        totals.append(tot)

    labels = indices
    x.append(data['t'])
    y.append(totals)
    labels.append('total')

    pynml.generate_plot(x,
                        y, 
                        "States from file: %s"%file, 
                        xaxis = "Time (ms)", 
                        yaxis = "State occupancy",
                        labels = labels,
                        show_plot_already=False,
                        save_figure_to = None,
                        cols_in_legend_box = 6)


plt.show()
Exemplo n.º 27
0
def run(a=None, **kwargs):

    a = build_namespace(a, **kwargs)

    pynml.print_comment_v(
        'Generating spiketime plot for %s; plotting: %s; save to: %s' %
        (a.spiketime_files, a.show_plots_already, a.save_spike_plot_to))

    xs = []
    ys = []
    labels = []
    markers = []
    linestyles = []

    offset_id = 0

    max_time = 0
    max_id = 0
    unique_ids = []
    times = OrderedDict()
    ids_in_file = OrderedDict()

    if a.format == 'sonata' or a.format == 's':

        for file_name in a.spiketime_files:
            ids_times = read_sonata_spikes_hdf5_file(file_name)

            x = []
            y = []
            max_id_here = 0

            name = file_name.split('/')[-1]
            if name.endswith('_spikes.h5'): name = name[:-10]
            elif name.endswith('.h5'): name = name[:-3]
            times[name] = []
            ids_in_file[name] = []

            for id in ids_times:

                for t in ids_times[id]:

                    id_shifted = offset_id + int(float(id))
                    max_id = max(max_id, id_shifted)

                    if not id_shifted in ids_in_file[name]:
                        ids_in_file[name].append(id_shifted)
                    times[name].append(t)
                    max_id_here = max(max_id_here, id_shifted)
                    max_time = max(t, max_time)
                    if not id_shifted in unique_ids:
                        unique_ids.append(id_shifted)
                    x.append(t)
                    y.append(id_shifted)

            print("max_id_here in %s: %i" % (file_name, max_id_here))
            labels.append("%s (%i)" % (name, max_id_here - offset_id))
            offset_id = max_id_here + 1
            xs.append(x)
            ys.append(y)
            markers.append('.')
            linestyles.append('')

        xlim = [max_time / -20.0, max_time * 1.05]
        ylim = [max_id_here / -20., max_id_here * 1.05]
        markersizes = []
        for xx in xs:
            if len(unique_ids) > 50:
                markersizes.append(2)
            elif len(unique_ids) > 200:
                markersizes.append(1)
            else:
                markersizes.append(4)
    else:

        for file_name in a.spiketime_files:
            pynml.print_comment_v("Loading spike times from: %s" % file_name)
            spikes_file = open(file_name)
            x = []
            y = []
            max_id_here = 0

            name = spikes_file.name
            if name.endswith('.spikes'): name = name[:-7]
            if name.endswith('.spike'): name = name[:-6]
            times[name] = []
            ids_in_file[name] = []

            for line in spikes_file:
                if not line.startswith('#'):
                    if a.format == 'id_t':
                        [id, t] = line.split()
                    elif a.format == 't_id':
                        [t, id] = line.split()
                    id_shifted = offset_id + int(float(id))
                    max_id = max(max_id, id_shifted)
                    t = float(t)
                    if not id_shifted in ids_in_file[name]:
                        ids_in_file[name].append(id_shifted)
                    times[name].append(t)
                    max_id_here = max(max_id_here, id_shifted)
                    max_time = max(t, max_time)
                    if not id_shifted in unique_ids:
                        unique_ids.append(id_shifted)
                    x.append(t)
                    y.append(id_shifted)

            #print("max_id_here in %s: %i"%(file_name,max_id_here))
            labels.append("%s (%i)" % (name, max_id_here - offset_id))
            offset_id = max_id_here + 1
            xs.append(x)
            ys.append(y)
            markers.append('.')
            linestyles.append('')

        xlim = [max_time / -20.0, max_time * 1.05]
        ylim = [max_id_here / -20., max_id_here * 1.05]
        markersizes = []
        for xx in xs:
            if len(unique_ids) > 50:
                markersizes.append(2)
            elif len(unique_ids) > 200:
                markersizes.append(1)
            else:
                markersizes.append(4)

    pynml.generate_plot(xs,
                        ys,
                        "Spike times from: %s" % a.spiketime_files,
                        labels=labels,
                        linestyles=linestyles,
                        markers=markers,
                        xaxis="Time (s)",
                        yaxis="Cell index",
                        xlim=xlim,
                        ylim=ylim,
                        markersizes=markersizes,
                        grid=False,
                        show_plot_already=False,
                        save_figure_to=a.save_spike_plot_to,
                        legend_position='right')

    if a.rates:

        plt.figure()
        bins = a.rate_bins
        for name in times:
            tt = times[name]
            ids_here = len(ids_in_file[name])

            plt.hist(tt,
                     bins=bins,
                     histtype='step',
                     weights=[bins * max(tt) / (float(ids_here))] * len(tt),
                     label=name + "_h")
            hist, bin_edges = np.histogram(
                tt,
                bins=bins,
                weights=[bins * max(tt) / (float(ids_here))] * len(tt))
            '''
            width = bin_edges[1]-bin_edges[0]
            mids = [i+width/2 for i in bin_edges[:-1]]
            plt.plot(mids, hist,label=name)'''

        plt.figure()

        for name in times:
            tt = times[name]
            ids_here = len(ids_in_file[name])

            hist, bin_edges = np.histogram(
                tt,
                bins=bins,
                weights=[bins * max(tt) / (float(ids_here))] * len(tt))

            width = bin_edges[1] - bin_edges[0]
            mids = [i + width / 2 for i in bin_edges[:-1]]

            boxes = [5, 10, 20, 50]
            boxes = [20, 50]
            boxes = [int(a.rate_window)]
            for b in boxes:
                box = np.ones(b)

                hist_c = np.convolve(hist / len(box), box)

                ys = hist_c
                xs = [i / (float(len(ys))) for i in range(len(ys))]
                plt.plot(xs, ys, label=name + '_%i_c' % b)

        #plt.legend()

    if a.show_plots_already:
        plt.show()
    else:
        plt.close()
def analyse_cell(dataset_id, type, nogui=False):

    reference = '%s_%s' % (type, dataset_id)
    cell_file = '%s.cell.nml' % (reference)

    images = 'summary/%s_%s.png'

    generate_current_vs_frequency_curve(
        cell_file,
        reference,
        simulator='jNeuroML_NEURON',
        start_amp_nA=-0.1,
        end_amp_nA=0.4,
        step_nA=0.01,
        analysis_duration=1000,
        analysis_delay=50,
        plot_voltage_traces=False,
        plot_if=not nogui,
        plot_iv=not nogui,
        xlim_if=[-200, 400],
        ylim_if=[-10, 120],
        xlim_iv=[-200, 400],
        ylim_iv=[-120, -40],
        save_if_figure_to=images % (reference, 'if'),
        save_iv_figure_to=images % (reference, 'iv'),
        show_plot_already=False)

    temp_dir = 'temp/'

    shutil.copy(cell_file, temp_dir)

    net_file = generate_network_for_sweeps(type, dataset_id,
                                           '%s.cell.nml' % (reference),
                                           reference, temp_dir)

    lems_file_name = 'LEMS_Test_%s_%s.xml' % (type, dataset_id)

    generate_lems_file_for_neuroml('Test_%s_%s' % (dataset_id, type),
                                   net_file,
                                   'network_%s_%s' % (dataset_id, type),
                                   1500,
                                   0.01,
                                   lems_file_name,
                                   temp_dir,
                                   gen_plots_for_all_v=False,
                                   copy_neuroml=False)

    simulator = "jNeuroML_NEURON"

    if simulator == "jNeuroML":
        results = pynml.run_lems_with_jneuroml(temp_dir + lems_file_name,
                                               nogui=True,
                                               load_saved_data=True,
                                               plot=False,
                                               show_plot_already=False)
    elif simulator == "jNeuroML_NEURON":
        results = pynml.run_lems_with_jneuroml_neuron(temp_dir +
                                                      lems_file_name,
                                                      nogui=True,
                                                      load_saved_data=True,
                                                      plot=False,
                                                      show_plot_already=False)

    x = []
    y = []

    print results.keys()

    tt = [t * 1000 for t in results['t']]
    for i in range(len(results) - 1):
        x.append(tt)
        y.append([
            v * 1000
            for v in results['Pop0/%i/%s_%s/v' % (i, type, dataset_id)]
        ])

    pynml.generate_plot(x,
                        y,
                        "Cell: %s" % dataset_id,
                        xaxis="Time (ms)",
                        yaxis="Membrane potential (mV)",
                        show_plot_already=False,
                        ylim=[-120, 60],
                        save_figure_to=images % (reference, 'traces'))
Exemplo n.º 29
0
def runNetwork(Be, Bi, nn_stim, show_gui=True):

    Ntrials = 1
    bw = 50.
    N_max_rec_v = 2001

    rec_conn = {'EtoE': 1, 'EtoI': 1, 'ItoE': 1, 'ItoI': 1}

    print('####################')
    print('### Be: %s, Bi: %s, nn_stim: %s) ' % (Be, Bi, nn_stim))
    print('####################')

    Bee, Bei = Be, Be
    Bie, Bii = Bi, Bi

    N = defaultParams.N
    NE = defaultParams.NE
    NI = defaultParams.NI

    print('\n # -----> size of pert. inh: %s; base rate %s; pert rate %s' %
          (nn_stim, defaultParams.r_bkg, defaultParams.r_stim))

    r_extra = np.zeros(N)
    r_extra[NE:NE + nn_stim] = defaultParams.r_stim

    rr1 = defaultParams.r_bkg * np.ones(N)
    rr2 = rr1 + r_extra

    # -- restart the simulator
    net_tools._nest_start_()

    np.random.seed(1234)
    init_seed = np.random.randint(1, 1234, defaultParams.n_cores)
    nest.SetStatus([0], [{'rng_seeds': init_seed.tolist()}])

    # -- exc & inh neurons
    exc_neurons = net_tools._make_neurons_(NE, neuron_model=defaultParams.cell_type, \
    myparams={'b':NE*[0.], 'a':NE*[0.]})
    inh_neurons = net_tools._make_neurons_(NI, neuron_model=defaultParams.cell_type, \
    myparams={'b':NE*[0.],'a':NE*[0.]})

    # -- L23 recurrent connectivity
    W_EtoE = _mycon_(NE, NE, Bee, .15)
    W_EtoI = _mycon_(NE, NI, Bei, .15)
    W_ItoE = _mycon_(NI, NE, Bie, 1.)
    W_ItoI = _mycon_(NI, NI, Bii, 1.)

    all_neurons = exc_neurons + inh_neurons

    # -- recurrent connectivity
    if rec_conn['EtoE']:
        net_tools._connect_pops_(exc_neurons, exc_neurons, W_EtoE)
    if rec_conn['EtoI']:
        net_tools._connect_pops_(exc_neurons, inh_neurons, W_EtoI)
    if rec_conn['ItoE']:
        net_tools._connect_pops_(inh_neurons, exc_neurons, W_ItoE)
    if rec_conn['ItoI']:
        net_tools._connect_pops_(inh_neurons, inh_neurons, W_ItoI)

    # -- recording spike data
    spikes_all = net_tools._recording_spikes_(neurons=all_neurons)

    if N <= N_max_rec_v:
        v_all = net_tools._recording_voltages_(neurons=all_neurons)

    # -- background input
    pos_inp = nest.Create("poisson_generator", N)

    for ii in range(N):
        nest.Connect([pos_inp[ii]], [all_neurons[ii]], \
        syn_spec = {'weight':defaultParams.Be_bkg, 'delay':defaultParams.delay_default})

    # -- simulating network for N-trials
    for tri in range(Ntrials):
        print('')
        print('# -> trial # ', tri + 1)

        ## transient
        for ii in range(N):
            nest.SetStatus([pos_inp[ii]], {'rate': rr1[ii]})
        net_tools._run_simulation_(defaultParams.Ttrans)

        ## baseline
        for ii in range(N):
            nest.SetStatus([pos_inp[ii]], {'rate': rr1[ii]})
        net_tools._run_simulation_(defaultParams.Tblank)

        ## perturbing a subset of inh
        for ii in range(N):
            nest.SetStatus([pos_inp[ii]], {'rate': rr2[ii]})
        net_tools._run_simulation_(defaultParams.Tstim)

    # -- reading out spiking activity
    spd = net_tools._reading_spikes_(spikes_all)

    if N <= N_max_rec_v:
        v_rec = net_tools._reading_voltages_(v_all)
        all_v = {}
        all_t = []
        for i in range(len(v_rec['senders'])):
            v = v_rec['V_m'][i]
            t = v_rec['times'][i]
            index = v_rec['senders'][i]
            if not index in all_v:
                all_v[index] = []
            all_v[index].append(v)
            if not t in all_t:
                all_t.append(t)

    # -- computes the rates out of spike data in a given time interval
    def _rate_interval_(spikedata, T1, T2, bw=bw):
        tids = (spikedata['times'] > T1) * (spikedata['times'] < T2)
        rr = np.histogram2d(spikedata['times'][tids], spikedata['senders'][tids], \
             range=((T1,T2),(1,N)), bins=((T2-T1)/bw,N))[0] / (bw/1e3)
        return rr

    rout_blank = np.zeros((Ntrials, int(defaultParams.Tblank / bw), N))
    rout_stim = np.zeros((Ntrials, int(defaultParams.Tstim / bw), N))
    for tri in range(Ntrials):
        Tblock = defaultParams.Tstim + defaultParams.Tblank + defaultParams.Ttrans
        rblk = _rate_interval_(
            spd, Tblock * tri + defaultParams.Ttrans,
            Tblock * tri + defaultParams.Ttrans + defaultParams.Tblank)
        rstm = _rate_interval_(
            spd, Tblock * tri + defaultParams.Ttrans + defaultParams.Tblank,
            Tblock * (tri + 1))
        rout_blank[tri, :, :] = rblk
        rout_stim[tri, :, :] = rstm

    print('##########')
    print('## Mean firing rates {Exc | Inh (pert.) | Inh (non-pert.)}')
    print('## Before pert.: ', \
    np.round(rout_blank[:,:,0:NE].mean(),1), \
    np.round(rout_blank[:,:,NE:NE+nn_stim].mean(),1), \
    np.round(rout_blank[:,:,NE+nn_stim:].mean(),1) )
    print('## After pert.: ', \
    np.round(rout_stim[:,:,0:NE].mean(),1), \
    np.round(rout_stim[:,:,NE:NE+nn_stim].mean(),1), \
    np.round(rout_stim[:,:,NE+nn_stim:].mean(),1) )
    print('##########')

    xs = [[], [], []]
    ys = [[], [], []]
    all_e = []
    all_ip = []
    all_inp = []
    for i in range(len(spd['senders'])):
        cellnum = spd['senders'][i]
        time = spd['times'][i]
        if cellnum <= NE:
            xs[0].append(time)
            all_e.append(time)
            ys[0].append(cellnum)
        elif cellnum <= NE + nn_stim:
            xs[1].append(time)
            all_ip.append(time)
            ys[1].append(cellnum)
        else:
            xs[2].append(time)
            all_inp.append(time)
            ys[2].append(cellnum)

    if show_gui:

        from pyneuroml import pynml
        print(
            "Plotting %s spikes for %s E cells, %s spikes for %s Ip cells, %s spikes for %s Inp cells"
            % (len(xs[0]), NE, len(xs[1]), nn_stim, len(
                xs[2]), N - NE - nn_stim))

        mksz = 1 if N > 100 else None
        pynml.generate_plot(xs,
                            ys,
                            "Spike times: Be=%s; Bi=%s; N=%s; p=%s" %
                            (Be, Bi, N, nn_stim),
                            xaxis="Time (s)",
                            yaxis="Cell number",
                            colors=['red', 'black', 'blue'],
                            linestyles=['', '', ''],
                            markers=['.', '.', '.'],
                            markersizes=[mksz, mksz, mksz],
                            ylim=[0, N + 1],
                            grid=False,
                            show_plot_already=False)

        plt.figure()
        bins = 15
        plt.hist(all_e,
                 bins=bins,
                 histtype='step',
                 weights=[1 / float(NE)] * len(all_e),
                 color='red')
        plt.hist(all_ip,
                 bins=bins,
                 histtype='step',
                 weights=[1 / float(nn_stim)] * len(all_ip),
                 color='black')
        plt.hist(all_inp,
                 bins=bins,
                 histtype='step',
                 weights=[1 / float(N - NE - nn_stim)] * len(all_inp),
                 color='blue',
                 ls='--')
        plt.title("Histogram of spikes")

        if N <= N_max_rec_v:
            xs = []
            ys = []
            colors = []

            for i in all_v:
                xs.append(all_t)
                ys.append(all_v[i])
                if i <= NE:
                    colors.append('red')
                elif i <= NE + nn_stim:
                    colors.append('black')
                else:
                    colors.append('blue')

                f = open('cell_%i.dat' % i, 'w')
                for ti in range(len(all_t)):
                    t = all_t[ti] / 1000.
                    v = all_v[i][ti] / 1000.
                    f.write('%s\t%s\n' % (t, v))
                f.close()

            #print("Plotting %s traces for %s E cells, %s traces for %s Ip cells, %s traces for %s Inp cells"%(len(xs[0]), NE ,len(xs[1]), nn_stim, len(xs[2]), N-NE-nn_stim))

            pynml.generate_plot(xs,
                                ys,
                                "Voltage traces: Be=%s; Bi=%s; N=%s; p=%s" %
                                (Be, Bi, N, nn_stim),
                                xaxis="Time (s)",
                                yaxis="Membrane potential (V)",
                                colors=colors,
                                grid=False,
                                show_plot_already=False)
Exemplo n.º 30
0
                            elif 'Exc' in vs:
                                colours.append((1-tr_shade_e,1-tr_shade_e,1))
                                tr_shade_e*=0.8
                            elif 'Inh2' in vs:
                                colours.append((1,1-tr_shade_i,1))
                                tr_shade_e*=0.8
                            else:
                                colours.append((1,1-tr_shade_i,1-tr_shade_i))
                                tr_shade_i*=0.8
                                
                    
                    print(colours)
                    pynml.generate_plot(all_t, all_v, 
                                        "Sim g=%s, i=%s"%(g,i),
                                        colors=colours,
                                        show_plot_already=False,
                                        xaxis = 'Time (ms)',            # x axis legend
                                        yaxis = 'Membrane potential (mV)',   # y axis legend
                                        save_figure_to='%s_traces.png'%desc)
                count+=1
                    
                

        fig = pl.figure(figsize=(16,8))
        info = "%s: scale %s, %s ms"%(run_in_simulator,scalePops, duration)

        fig.canvas.set_window_title(info)
        pl.suptitle(info)

        _plot_(Rexc.T, g_rng, i_rng, 221, 'Rates Exc (Hz)')
        _plot_(Rinh.T, g_rng, i_rng, 222, 'Rates Inh (Hz)')
Exemplo n.º 31
0
    max_id_here = 0
    for line in spikes_file:
        if not line.startswith('#'):
            if format == 'id_t':
                [id, t] = line.split()
            elif format == 't_id':
                [t, id] = line.split()
            id_shift = max_id+int(float(id))
            max_id_here = max(max_id_here,id_shift) 
            x.append(t)
            y.append(id_shift)
    max_id = max_id_here
    xs.append(x)
    ys.append(y)
    labels.append(spikes_file.name)
    markers.append('.')
    linestyles.append('')



pynml.generate_plot(xs,
                    ys, 
                    "Spike times from: %s"%spikes_file.name, 
                    labels = labels, 
                    linestyles=linestyles,
                    markers=markers,
                    xaxis = "Time (s)", 
                    yaxis = "Cell number", 
                    grid = False,
                    show_plot_already=True)
    
Exemplo n.º 32
0
y = []
for i in indices:
    x.append(data["t"])
    y.append(data[i])

totals = []
for j in range(len(data["t"])):
    tot = 0
    for i in indices:
        tot += data[i][j]
    totals.append(tot)

labels = indices
x.append(data["t"])
y.append(totals)
labels.append("total")

pynml.generate_plot(
    x,
    y,
    "States",
    xaxis="Time (ms)",
    yaxis="State occupancy",
    labels=labels,
    show_plot_already=False,
    save_figure_to=None,
)


plt.show()
Exemplo n.º 33
0
######## Some example data

ts = [t*0.01 for t in range(20000)]

siny = [math.sin(t/10) for t in ts]
cosey = [ math.exp(t/-80)*math.cos(t/5) for t in ts]


########  Generate a plot for this quickly with generate_plot

ax = pynml.generate_plot([ts,ts],                        # Add 2 sets of x values
                         [siny,cosey],                   # Add 2 sets of y values
                         "Some traces",                  # Title
                         xaxis = 'Time (ms)',            # x axis legend
                         yaxis = 'Arbitrary units...',   # y axis legend
                         linewidths = [2,3],             # Thicknesses of each trace
                         show_plot_already=False,        # Show or wait for plt.show()?
                         font_size = 10,                 # Font
                         bottom_left_spines_only = True, # Box or just x & y axes
                         save_figure_to='quick.png')     # Save figure


########   Add another trace
                 
ts_ = [t*0.1 for t in range(2000)]
randy = [ random.random() for t in ts_]
ax.plot(ts_,randy,'.') # Won't be included in saved PNG!


########   Show complete plot
Exemplo n.º 34
0
def generate_current_vs_frequency_curve(nml2_file,
                                        cell_id,
                                        start_amp_nA,
                                        end_amp_nA,
                                        step_nA,
                                        analysis_duration,
                                        analysis_delay,
                                        dt=0.05,
                                        temperature="32degC",
                                        spike_threshold_mV=0.,
                                        plot_voltage_traces=False,
                                        plot_if=True,
                                        plot_iv=False,
                                        xlim_if=None,
                                        ylim_if=None,
                                        xlim_iv=None,
                                        ylim_iv=None,
                                        show_plot_already=True,
                                        save_if_figure_to=None,
                                        save_iv_figure_to=None,
                                        simulator="jNeuroML",
                                        include_included=True):

    from pyelectro.analysis import max_min
    from pyelectro.analysis import mean_spike_frequency
    import numpy as np

    print_comment_v(
        "Generating FI curve for cell %s in %s using %s (%snA->%snA; %snA steps)"
        % (cell_id, nml2_file, simulator, start_amp_nA, end_amp_nA, step_nA))

    sim_id = 'iv_%s' % cell_id
    duration = analysis_duration + analysis_delay
    ls = LEMSSimulation(sim_id, duration, dt)

    ls.include_neuroml2_file(nml2_file, include_included=include_included)

    stims = []
    amp = start_amp_nA
    while amp <= end_amp_nA:
        stims.append(amp)
        amp += step_nA

    number_cells = len(stims)
    pop = nml.Population(id="population_of_%s" % cell_id,
                         component=cell_id,
                         size=number_cells)

    # create network and add populations
    net_id = "network_of_%s" % cell_id
    net = nml.Network(id=net_id,
                      type="networkWithTemperature",
                      temperature=temperature)
    ls.assign_simulation_target(net_id)
    net_doc = nml.NeuroMLDocument(id=net.id)
    net_doc.networks.append(net)
    net_doc.includes.append(nml.IncludeType(nml2_file))
    net.populations.append(pop)

    for i in range(number_cells):
        stim_amp = "%snA" % stims[i]
        input_id = ("input_%s" % stim_amp).replace('.',
                                                   '_').replace('-', 'min')
        pg = nml.PulseGenerator(id=input_id,
                                delay="0ms",
                                duration="%sms" % duration,
                                amplitude=stim_amp)
        net_doc.pulse_generators.append(pg)

        # Add these to cells
        input_list = nml.InputList(id=input_id,
                                   component=pg.id,
                                   populations=pop.id)
        input = nml.Input(id='0',
                          target="../%s[%i]" % (pop.id, i),
                          destination="synapses")
        input_list.input.append(input)
        net.input_lists.append(input_list)

    net_file_name = '%s.net.nml' % sim_id
    pynml.write_neuroml2_file(net_doc, net_file_name)
    ls.include_neuroml2_file(net_file_name)

    disp0 = 'Voltage_display'
    ls.create_display(disp0, "Voltages", "-90", "50")
    of0 = 'Volts_file'
    ls.create_output_file(of0, "%s.v.dat" % sim_id)

    for i in range(number_cells):
        ref = "v_cell%i" % i
        quantity = "%s[%i]/v" % (pop.id, i)
        ls.add_line_to_display(disp0, ref, quantity, "1mV",
                               pynml.get_next_hex_color())

        ls.add_column_to_output_file(of0, ref, quantity)

    lems_file_name = ls.save_to_file()

    if simulator == "jNeuroML":
        results = pynml.run_lems_with_jneuroml(lems_file_name,
                                               nogui=True,
                                               load_saved_data=True,
                                               plot=plot_voltage_traces,
                                               show_plot_already=False)
    elif simulator == "jNeuroML_NEURON":
        results = pynml.run_lems_with_jneuroml_neuron(lems_file_name,
                                                      nogui=True,
                                                      load_saved_data=True,
                                                      plot=plot_voltage_traces,
                                                      show_plot_already=False)

    #print(results.keys())
    if_results = {}
    iv_results = {}
    for i in range(number_cells):
        t = np.array(results['t']) * 1000
        v = np.array(results["%s[%i]/v" % (pop.id, i)]) * 1000

        mm = max_min(v, t, delta=0, peak_threshold=spike_threshold_mV)
        spike_times = mm['maxima_times']
        freq = 0
        if len(spike_times) > 2:
            count = 0
            for s in spike_times:
                if s >= analysis_delay and s < (analysis_duration +
                                                analysis_delay):
                    count += 1
            freq = 1000 * count / float(analysis_duration)

        mean_freq = mean_spike_frequency(spike_times)
        # print("--- %s nA, spike times: %s, mean_spike_frequency: %f, freq (%fms -> %fms): %f"%(stims[i],spike_times, mean_freq, analysis_delay, analysis_duration+analysis_delay, freq))
        if_results[stims[i]] = freq

        if freq == 0:
            iv_results[stims[i]] = v[-1]

    if plot_if:

        stims = sorted(if_results.keys())
        stims_pA = [ii * 1000 for ii in stims]

        freqs = [if_results[s] for s in stims]

        pynml.generate_plot([stims_pA], [freqs],
                            "Frequency versus injected current for: %s" %
                            nml2_file,
                            colors=['k'],
                            linestyles=['-'],
                            markers=['o'],
                            xaxis='Input current (pA)',
                            yaxis='Firing frequency (Hz)',
                            xlim=xlim_if,
                            ylim=ylim_if,
                            grid=True,
                            show_plot_already=False,
                            save_figure_to=save_if_figure_to)
    if plot_iv:

        stims = sorted(iv_results.keys())
        stims_pA = [ii * 1000 for ii in sorted(iv_results.keys())]
        vs = [iv_results[s] for s in stims]

        pynml.generate_plot(
            [stims_pA], [vs],
            "Final membrane potential versus injected current for: %s" %
            nml2_file,
            colors=['k'],
            linestyles=['-'],
            markers=['o'],
            xaxis='Input current (pA)',
            yaxis='Membrane potential (mV)',
            xlim=xlim_iv,
            ylim=ylim_iv,
            grid=True,
            show_plot_already=False,
            save_figure_to=save_iv_figure_to)

    if show_plot_already:
        from matplotlib import pyplot as plt
        plt.show()

    return if_results
Exemplo n.º 35
0
                                colours.append(
                                    (1 - tr_shade_e2, 1, 1 - tr_shade_e2))
                                tr_shade_e2 *= 0.8
                            elif 'Exc' in vs:
                                colours.append(
                                    (1 - tr_shade_e, 1 - tr_shade_e, 1))
                                tr_shade_e *= 0.8
                            else:
                                colours.append(
                                    (1, 1 - tr_shade_i, 1 - tr_shade_i))
                                tr_shade_i *= 0.8

                    print colours
                    pynml.generate_plot(all_t,
                                        all_v,
                                        "Sim g=%s, i=%s" % (g, i),
                                        colors=colours,
                                        show_plot_already=False)
                count += 1

        fig = pl.figure(figsize=(16, 8))
        info = "%s: scale %s, %s ms" % (run_in_simulator, scalePops, duration)

        fig.canvas.set_window_title(info)
        pl.suptitle(info)

        _plot_(Rexc.T, g_rng, i_rng, 221, 'Rates Exc (Hz)')
        _plot_(Rinh.T, g_rng, i_rng, 222, 'Rates Inh (Hz)')

        pl.subplots_adjust(wspace=.3, hspace=.3)
Exemplo n.º 36
0
import sys
from pyneuroml import pynml

cell = sys.argv[1]

print("Comparing behaviour of cell %s to original NEURON code" % cell)

orig_dat = '../%s.soma.dat' % cell
orig_results, indices = pynml.reload_standard_dat_file(orig_dat)
print("Reloaded NEURON data: %s" % orig_results.keys())

results = pynml.run_lems_with_jneuroml_neuron('LEMS_%s.xml' % cell,
                                              nogui=True,
                                              load_saved_data=True)

print("Reloaded data: %s" % results.keys())

xs = []
ys = []
labels = []

xs.append([t * 1000 for t in results['t']])
ys.append([v * 1000 for v in results['Pop_%scell/0/%scell/v' % (cell, cell)]])
labels.append('jNeuroML_NEURON')

xs.append(orig_results['t'])
ys.append(orig_results[0])
labels.append('NEURON')

pynml.generate_plot(xs, ys, 'Plot of %s' % cell, labels=labels)
Exemplo n.º 37
0
data, indices = pynml.reload_standard_dat_file('KAHP.states.dat')
x = []
y = []
for i in indices:
    x.append(data['t'])
    y.append(data[i])
    
totals = []
for j in range(len(data['t'])):
    tot = 0
    for i in indices:
        tot+=data[i][j]
    totals.append(tot)
    
labels = indices
x.append(data['t'])
y.append(totals)
labels.append('total')

pynml.generate_plot(x,
                    y, 
                    "States", 
                    xaxis = "Time (ms)", 
                    yaxis = "State occupancy",
                    labels = labels,
                    show_plot_already=False,
                    save_figure_to = None)
                    

plt.show()
Exemplo n.º 38
0
            data, indices = pynml.reload_standard_dat_file(ivf)
            print("Loaded %s from %s" % (data.keys(), ivf))
            iv_xs.append(data['t'])
            iv_ys.append(data[0])
            iv_labels.append(ref)

            iff = '%s_IF.dat' % ref
            data, indices = pynml.reload_standard_dat_file(iff)
            print("Loaded %s from %s" % (data.keys(), iff))
            if_xs.append(data['t'])
            if_ys.append(data[0])
            if_labels.append(ref)

        pynml.generate_plot(iv_xs,
                            iv_ys,
                            "IV curves",
                            labels=iv_labels,
                            markers=['o'] * len(iv_labels),
                            show_plot_already=False)
        pynml.generate_plot(if_xs,
                            if_ys,
                            "IF curves",
                            labels=if_labels,
                            markers=['o'] * len(if_labels))
    else:

        #plot_cell_firing('../cells/AllenHH/AllenHH_477127614.cell.nml')
        #plot_cell_firing('../cells/AllenHH/AllenHH_476686112.cell.nml')
        #plot_cell_firing('../cells/SmithEtAl2013/L23_Retuned_477127614.cell.nml')
        plot_cell_firing('../cells/Thalamocortical/L23PyrRS.cell.nml',
                         quick=False,
                         num_processors=18,
Exemplo n.º 39
0
    def __init__(self,
                 runner,
                 vary,
                 fixed={},
                 verbose=False,
                 num_parallel_runs=1,
                 plot_all=False,
                 save_plot_all_to=None,
                 heatmap_all=False,
                 save_heatmap_to=None,
                 heatmap_lims=None,
                 show_plot_already=False,
                 peak_threshold=0):

        self.sim = load_simulation_json(runner.nmllite_sim)

        self.colormap = 'jet'

        ps_id = 'ParamSweep_%s_%s' % (self.sim.id, time.ctime().replace(
            ' ', '_').replace(':', '.'))

        print_v(
            "Initialising ParameterSweep %s with %s, %s (%i parallel runs)" %
            (ps_id, vary, fixed, num_parallel_runs))

        os.mkdir(ps_id)
        self.result_dir = os.path.abspath(ps_id)

        self.runner = runner
        self.fixed = fixed
        self.vary = vary
        self.verbose = verbose
        self.num_parallel_runs = num_parallel_runs

        self.plot_all = plot_all
        self.save_plot_all_to = save_plot_all_to
        self.heatmap_all = heatmap_all
        self.save_heatmap_to = save_heatmap_to
        self.heatmap_lims = heatmap_lims
        self.show_plot_already = show_plot_already

        self.index = 0
        self.total_todo = 1
        self.report = OrderedDict()
        self.report['Varied parameters'] = vary
        self.report['Fixed parameters'] = fixed
        self.report['Simulations'] = OrderedDict()
        for v in vary:
            self.total_todo *= len(vary[v])

        self.analysis_var = {
            'peak_delta': 0,
            'baseline': 0,
            'dvdt_threshold': 0,
            'peak_threshold': peak_threshold
        }

        if self.plot_all:
            self.ax = pynml.generate_plot(
                [],
                [],  # Add 2 sets of y values
                "Traces generated from %s" % self.sim.id,  # Title
                labels=[],
                xaxis='Time (ms)',  # x axis legend
                yaxis='Membrane potential (mV)',  # y axis legend
                title_above_plot=True,
                show_plot_already=False)  # Save figure

        if self.heatmap_all:
            if len(self.vary) != 1:
                raise Exception(
                    'Heatmap can only be used when only one parameter is varied...'
                )
            self.hm_x = None
            self.hm_y = []
            self.hm_z = []
Exemplo n.º 40
0
            if seg == 'seg1406':
                all_seg_1406.append(res[0])

    avg_seg0 = np.zeros(len(all_seg_0[0]))
    for l in all_seg_0:
        for i in range(len(l)):
            avg_seg0[i] += l[i]

    avg_seg1406 = np.zeros(len(all_seg_1406[0]))
    for l in all_seg_1406:
        for i in range(len(l)):
            avg_seg1406[i] += l[i]

    pynml.generate_plot(xs,
                        ys,
                        'V clamp currents %s' % x,
                        ylim=lims[x],
                        labels=labels,
                        show_plot_already=False)

    xs = []
    ys = []
    labels = []

    xs.append(time_points)
    ys.append(avg_seg0 / len(all_seg_0))
    labels.append('%s average' % (seg))

    pynml.generate_plot(xs,
                        ys,
                        'V clamp current averages at soma %s' % x,
                        ylim=lims[x],
Exemplo n.º 41
0
def generate_current_vs_frequency_curve(nml2_file,
                                        cell_id,
                                        start_amp_nA=-0.1,
                                        end_amp_nA=0.1,
                                        step_nA=0.01,
                                        custom_amps_nA=[],
                                        analysis_duration=1000,
                                        analysis_delay=0,
                                        pre_zero_pulse=0,
                                        post_zero_pulse=0,
                                        dt=0.05,
                                        temperature="32degC",
                                        spike_threshold_mV=0.,
                                        plot_voltage_traces=False,
                                        plot_if=True,
                                        plot_iv=False,
                                        xlim_if=None,
                                        ylim_if=None,
                                        xlim_iv=None,
                                        ylim_iv=None,
                                        label_xaxis=True,
                                        label_yaxis=True,
                                        show_volts_label=True,
                                        grid=True,
                                        font_size=12,
                                        if_iv_color='k',
                                        linewidth=1,
                                        bottom_left_spines_only=False,
                                        show_plot_already=True,
                                        save_voltage_traces_to=None,
                                        save_if_figure_to=None,
                                        save_iv_figure_to=None,
                                        save_if_data_to=None,
                                        save_iv_data_to=None,
                                        simulator="jNeuroML",
                                        num_processors=1,
                                        include_included=True,
                                        title_above_plot=False,
                                        return_axes=False,
                                        verbose=False):

    print_comment(
        "Running generate_current_vs_frequency_curve() on %s (%s)" %
        (nml2_file, os.path.abspath(nml2_file)), verbose)
    from pyelectro.analysis import max_min
    from pyelectro.analysis import mean_spike_frequency
    import numpy as np
    traces_ax = None
    if_ax = None
    iv_ax = None

    sim_id = 'iv_%s' % cell_id
    total_duration = pre_zero_pulse + analysis_duration + analysis_delay + post_zero_pulse
    pulse_duration = analysis_duration + analysis_delay
    end_stim = pre_zero_pulse + analysis_duration + analysis_delay
    ls = LEMSSimulation(sim_id, total_duration, dt)

    ls.include_neuroml2_file(nml2_file, include_included=include_included)

    stims = []
    if len(custom_amps_nA) > 0:
        stims = [float(a) for a in custom_amps_nA]
        stim_info = ['%snA' % float(a) for a in custom_amps_nA]
    else:
        amp = start_amp_nA
        while amp <= end_amp_nA:
            stims.append(amp)
            amp += step_nA

        stim_info = '(%snA->%snA; %s steps of %snA; %sms)' % (
            start_amp_nA, end_amp_nA, len(stims), step_nA, total_duration)

    print_comment_v("Generating an IF curve for cell %s in %s using %s %s" %
                    (cell_id, nml2_file, simulator, stim_info))

    number_cells = len(stims)
    pop = nml.Population(id="population_of_%s" % cell_id,
                         component=cell_id,
                         size=number_cells)

    # create network and add populations
    net_id = "network_of_%s" % cell_id
    net = nml.Network(id=net_id,
                      type="networkWithTemperature",
                      temperature=temperature)
    ls.assign_simulation_target(net_id)
    net_doc = nml.NeuroMLDocument(id=net.id)
    net_doc.networks.append(net)
    net_doc.includes.append(nml.IncludeType(nml2_file))
    net.populations.append(pop)

    for i in range(number_cells):
        stim_amp = "%snA" % stims[i]
        input_id = ("input_%s" % stim_amp).replace('.',
                                                   '_').replace('-', 'min')
        pg = nml.PulseGenerator(id=input_id,
                                delay="%sms" % pre_zero_pulse,
                                duration="%sms" % pulse_duration,
                                amplitude=stim_amp)
        net_doc.pulse_generators.append(pg)

        # Add these to cells
        input_list = nml.InputList(id=input_id,
                                   component=pg.id,
                                   populations=pop.id)
        input = nml.Input(id='0',
                          target="../%s[%i]" % (pop.id, i),
                          destination="synapses")
        input_list.input.append(input)
        net.input_lists.append(input_list)

    net_file_name = '%s.net.nml' % sim_id
    pynml.write_neuroml2_file(net_doc, net_file_name)
    ls.include_neuroml2_file(net_file_name)

    disp0 = 'Voltage_display'
    ls.create_display(disp0, "Voltages", "-90", "50")
    of0 = 'Volts_file'
    ls.create_output_file(of0, "%s.v.dat" % sim_id)

    for i in range(number_cells):
        ref = "v_cell%i" % i
        quantity = "%s[%i]/v" % (pop.id, i)
        ls.add_line_to_display(disp0, ref, quantity, "1mV",
                               pynml.get_next_hex_color())

        ls.add_column_to_output_file(of0, ref, quantity)

    lems_file_name = ls.save_to_file()

    print_comment(
        "Written LEMS file %s (%s)" %
        (lems_file_name, os.path.abspath(lems_file_name)), verbose)

    if simulator == "jNeuroML":
        results = pynml.run_lems_with_jneuroml(lems_file_name,
                                               nogui=True,
                                               load_saved_data=True,
                                               plot=False,
                                               show_plot_already=False,
                                               verbose=verbose)
    elif simulator == "jNeuroML_NEURON":
        results = pynml.run_lems_with_jneuroml_neuron(lems_file_name,
                                                      nogui=True,
                                                      load_saved_data=True,
                                                      plot=False,
                                                      show_plot_already=False,
                                                      verbose=verbose)
    elif simulator == "jNeuroML_NetPyNE":
        results = pynml.run_lems_with_jneuroml_netpyne(
            lems_file_name,
            nogui=True,
            load_saved_data=True,
            plot=False,
            show_plot_already=False,
            num_processors=num_processors,
            verbose=verbose)
    else:
        raise Exception(
            "Sorry, cannot yet run current vs frequency analysis using simulator %s"
            % simulator)

    print_comment(
        "Completed run in simulator %s (results: %s)" %
        (simulator, results.keys()), verbose)

    #print(results.keys())
    times_results = []
    volts_results = []
    volts_labels = []
    if_results = {}
    iv_results = {}
    for i in range(number_cells):
        t = np.array(results['t']) * 1000
        v = np.array(results["%s[%i]/v" % (pop.id, i)]) * 1000

        if plot_voltage_traces:
            times_results.append(t)
            volts_results.append(v)
            volts_labels.append("%s nA" % stims[i])

        mm = max_min(v, t, delta=0, peak_threshold=spike_threshold_mV)
        spike_times = mm['maxima_times']
        freq = 0
        if len(spike_times) > 2:
            count = 0
            for s in spike_times:
                if s >= pre_zero_pulse + analysis_delay and s < (
                        pre_zero_pulse + analysis_duration + analysis_delay):
                    count += 1
            freq = 1000 * count / float(analysis_duration)

        mean_freq = mean_spike_frequency(spike_times)
        #print("--- %s nA, spike times: %s, mean_spike_frequency: %f, freq (%fms -> %fms): %f"%(stims[i],spike_times, mean_freq, analysis_delay, analysis_duration+analysis_delay, freq))
        if_results[stims[i]] = freq

        if freq == 0:
            if post_zero_pulse == 0:
                iv_results[stims[i]] = v[-1]
            else:
                v_end = None
                for j in range(len(t)):
                    if v_end == None and t[j] >= end_stim:
                        v_end = v[j]
                iv_results[stims[i]] = v_end

    if plot_voltage_traces:

        traces_ax = pynml.generate_plot(
            times_results,
            volts_results,
            "Membrane potential traces for: %s" % nml2_file,
            xaxis='Time (ms)' if label_xaxis else ' ',
            yaxis='Membrane potential (mV)' if label_yaxis else '',
            xlim=[total_duration * -0.05, total_duration * 1.05],
            show_xticklabels=label_xaxis,
            font_size=font_size,
            bottom_left_spines_only=bottom_left_spines_only,
            grid=False,
            labels=volts_labels if show_volts_label else [],
            show_plot_already=False,
            save_figure_to=save_voltage_traces_to,
            title_above_plot=title_above_plot,
            verbose=verbose)

    if plot_if:

        stims = sorted(if_results.keys())
        stims_pA = [ii * 1000 for ii in stims]

        freqs = [if_results[s] for s in stims]

        if_ax = pynml.generate_plot(
            [stims_pA], [freqs],
            "Firing frequency versus injected current for: %s" % nml2_file,
            colors=[if_iv_color],
            linestyles=['-'],
            markers=['o'],
            linewidths=[linewidth],
            xaxis='Input current (pA)' if label_xaxis else ' ',
            yaxis='Firing frequency (Hz)' if label_yaxis else '',
            xlim=xlim_if,
            ylim=ylim_if,
            show_xticklabels=label_xaxis,
            show_yticklabels=label_yaxis,
            font_size=font_size,
            bottom_left_spines_only=bottom_left_spines_only,
            grid=grid,
            show_plot_already=False,
            save_figure_to=save_if_figure_to,
            title_above_plot=title_above_plot,
            verbose=verbose)

        if save_if_data_to:
            with open(save_if_data_to, 'w') as if_file:
                for i in range(len(stims_pA)):
                    if_file.write("%s\t%s\n" % (stims_pA[i], freqs[i]))
    if plot_iv:

        stims = sorted(iv_results.keys())
        stims_pA = [ii * 1000 for ii in sorted(iv_results.keys())]
        vs = [iv_results[s] for s in stims]

        xs = []
        ys = []
        xs.append([])
        ys.append([])

        for si in range(len(stims)):
            stim = stims[si]
            if len(custom_amps_nA) == 0 and si > 1 and (
                    stims[si] - stims[si - 1]) > step_nA * 1.01:
                xs.append([])
                ys.append([])

            xs[-1].append(stim * 1000)
            ys[-1].append(iv_results[stim])

        iv_ax = pynml.generate_plot(
            xs,
            ys,
            "V at %sms versus I below threshold for: %s" %
            (end_stim, nml2_file),
            colors=[if_iv_color for s in xs],
            linestyles=['-' for s in xs],
            markers=['o' for s in xs],
            xaxis='Input current (pA)' if label_xaxis else '',
            yaxis='Membrane potential (mV)' if label_yaxis else '',
            xlim=xlim_iv,
            ylim=ylim_iv,
            show_xticklabels=label_xaxis,
            show_yticklabels=label_yaxis,
            font_size=font_size,
            linewidths=[linewidth for s in xs],
            bottom_left_spines_only=bottom_left_spines_only,
            grid=grid,
            show_plot_already=False,
            save_figure_to=save_iv_figure_to,
            title_above_plot=title_above_plot,
            verbose=verbose)

        if save_iv_data_to:
            with open(save_iv_data_to, 'w') as iv_file:
                for i in range(len(stims_pA)):
                    iv_file.write("%s\t%s\n" % (stims_pA[i], vs[i]))

    if show_plot_already:
        from matplotlib import pyplot as plt
        plt.show()

    if return_axes:
        return traces_ax, if_ax, iv_ax

    return if_results
         currents_rate_spike[current] = 0
         currents_v_sub[current] = sweeps[s]["pyelectro_iclamp_analysis"][steady_state_key]
         
 curents_sub = currents_v_sub.keys()
 curents_sub.sort()
 v = [currents_v_sub[c] for c in curents_sub]
 
 target_file = 'summary/%s_%s.png'
 
 pynml.generate_plot([curents_sub],
                     [v], 
                     "Subthreshold responses: %s"%id, 
                     colors = ['k'], 
                     linestyles=['-'],
                     markers=['o'],
                     xaxis = "Current (pA)", 
                     yaxis = "Steady state (mV)", 
                     xlim = [-200, 400],
                     ylim = [-120, -40],
                     grid = True,
                     show_plot_already=False,
                     save_figure_to = target_file%('subthreshold', id))
 #plt.plot(curents_sub, , color='k', linestyle='-', marker='o')
 
 curents_spike = currents_rate_spike.keys()
 curents_spike.sort()
 v = [currents_rate_spike[c] for c in curents_spike]
 
 pynml.generate_plot([curents_spike],
                     [v], 
                     "Spiking frequencies: %s"%id, 
Exemplo n.º 43
0
def run(a=None,**kwargs): 

    a = build_namespace(a,**kwargs)
    
    pynml.print_comment_v('Generating spiketime plot for %s; plotting: %s; save to: %s'%(a.spiketime_files, a.show_plots_already, a.save_spike_plot_to))
        
    xs = []
    ys = []
    labels = []
    markers = []
    linestyles = []

    offset_id = 0

    max_time = 0
    max_id = 0
    unique_ids = []
    times = OrderedDict()
    ids_in_file = OrderedDict()
    
    if a.format == 'sonata' or a.format == 's':
        
        for file_name in a.spiketime_files:
            ids_times = read_sonata_spikes_hdf5_file(file_name)
            
            x = []
            y = []
            max_id_here = 0

            name = file_name.split('/')[-1]
            if name.endswith('_spikes.h5'): name = name[:-10]
            elif name.endswith('.h5'): name = name[:-3]
            times[name] = []
            ids_in_file[name] = []

            for id in ids_times:
                
                for t in ids_times[id]:
            
                    id_shifted = offset_id+int(float(id))
                    max_id = max(max_id,id_shifted)

                    if not id_shifted in ids_in_file[name]:
                        ids_in_file[name].append(id_shifted)
                    times[name].append(t)
                    max_id_here = max(max_id_here,id_shifted) 
                    max_time = max(t,max_time)
                    if not id_shifted in unique_ids:
                        unique_ids.append(id_shifted)
                    x.append(t)
                    y.append(id_shifted)

            print("max_id_here in %s: %i"%(file_name,max_id_here))
            labels.append("%s (%i)"%(name,max_id_here-offset_id))
            offset_id = max_id_here+1
            xs.append(x)
            ys.append(y)
            markers.append('.')
            linestyles.append('')
            

        xlim = [max_time/-20.0, max_time*1.05]
        ylim = [max_id_here/-20., max_id_here*1.05]
        markersizes = []
        for xx in xs:
            if len(unique_ids)>50:
               markersizes.append(2) 
            elif len(unique_ids)>200:
               markersizes.append(1) 
            else:
               markersizes.append(4) 
    else:
    
        for file_name in a.spiketime_files:
            pynml.print_comment_v("Loading spike times from: %s"%file_name)
            spikes_file = open(file_name)
            x = []
            y = []
            max_id_here = 0

            name = spikes_file.name
            if name.endswith('.spikes'): name = name[:-7]
            if name.endswith('.spike'): name = name[:-6]
            times[name] = []
            ids_in_file[name] = []

            for line in spikes_file:
                if not line.startswith('#'):
                    if a.format == 'id_t':
                        [id, t] = line.split()
                    elif a.format == 't_id':
                        [t, id] = line.split()
                    id_shifted = offset_id+int(float(id))
                    max_id = max(max_id,id_shifted)
                    t = float(t)
                    if not id_shifted in ids_in_file[name]:
                        ids_in_file[name].append(id_shifted)
                    times[name].append(t)
                    max_id_here = max(max_id_here,id_shifted) 
                    max_time = max(t,max_time)
                    if not id_shifted in unique_ids:
                        unique_ids.append(id_shifted)
                    x.append(t)
                    y.append(id_shifted)

            #print("max_id_here in %s: %i"%(file_name,max_id_here))
            labels.append("%s (%i)"%(name,max_id_here-offset_id))
            offset_id = max_id_here+1
            xs.append(x)
            ys.append(y)
            markers.append('.')
            linestyles.append('')


        xlim = [max_time/-20.0, max_time*1.05]
        ylim = [max_id_here/-20., max_id_here*1.05]
        markersizes = []
        for xx in xs:
            if len(unique_ids)>50:
               markersizes.append(2) 
            elif len(unique_ids)>200:
               markersizes.append(1) 
            else:
               markersizes.append(4) 
            
    
    pynml.generate_plot(xs,
                        ys, 
                        "Spike times from: %s"%a.spiketime_files, 
                        labels = labels, 
                        linestyles=linestyles,
                        markers=markers,
                        xaxis = "Time (s)", 
                        yaxis = "Cell index", 
                        xlim = xlim,
                        ylim = ylim,
                        markersizes = markersizes,
                        grid = False,
                        show_plot_already=False,
                        save_figure_to=a.save_spike_plot_to,
                        legend_position='right')
                        
    if a.rates:

        plt.figure()
        bins = a.rate_bins
        for name in times:
            tt = times[name]
            ids_here = len(ids_in_file[name])
            
            plt.hist(tt, bins=bins,histtype='step',weights=[bins*max(tt)/(float(ids_here))]*len(tt),label=name+"_h")
            hist, bin_edges = np.histogram(tt, bins=bins,weights=[bins*max(tt)/(float(ids_here))]*len(tt))
            '''
            width = bin_edges[1]-bin_edges[0]
            mids = [i+width/2 for i in bin_edges[:-1]]
            plt.plot(mids, hist,label=name)'''
            
            
        plt.figure()
        
        for name in times:
            tt = times[name]
            ids_here = len(ids_in_file[name])
            
            hist, bin_edges = np.histogram(tt, bins=bins,weights=[bins*max(tt)/(float(ids_here))]*len(tt))
        
            width = bin_edges[1]-bin_edges[0]
            mids = [i+width/2 for i in bin_edges[:-1]]
            
            boxes = [5,10,20,50]
            boxes = [20,50]
            boxes = [int(a.rate_window)]
            for b in boxes:
                box = np.ones(b)
                
                hist_c = np.convolve(hist/len(box), box)

                ys = hist_c
                xs = [i/(float(len(ys))) for i in range(len(ys))]
                plt.plot(xs, ys,label=name+'_%i_c'%b)
            
        #plt.legend()
        
    if a.show_plots_already:
        plt.show()
    else:
        plt.close() 
Exemplo n.º 44
0
    3)
comp['iCa_CavN'] = (
    'pop/0/cell1/biophysicalProperties/membraneProperties/CavN_all/iDensity',
    4)

for var in comp.keys():
    lems, nrn = comp[var]
    xs = []
    ys = []
    labels = []

    xs.append([t * 1000 for t in results['t']])
    factor = 1
    if var == 'v': factor = 1000  #  1 V = 1000.0 mV
    if var == 'iCa_CavL': factor = .1  #  1 A_per_m2 = 0.1 mA_per_cm2
    if var == 'iCa_CavN': factor = .1  #  1 A_per_m2 = 0.1 mA_per_cm2
    ys.append([v * factor for v in results[lems]])
    labels.append('jNeuroML_NEURON')

    xs.append(orig_results['t'])
    ys.append(orig_results[nrn])
    labels.append('NEURON')

    pynml.generate_plot(xs,
                        ys,
                        'Plot of %s' % var,
                        labels=labels,
                        show_plot_already=False)

plt.show()
Exemplo n.º 45
0
    def plotLines(self,
                  first_param,
                  value,
                  second_param=None,
                  save_figure_to=None,
                  logx=False,
                  logy=False):

        all_pvals = OrderedDict()
        all_lines = OrderedDict()

        all_traces = []

        DEFAULT_TRACE = self.sim.id
        if not second_param:
            all_traces = [DEFAULT_TRACE]
        else:
            for ref, info in self.report['Simulations'].items():
                val2 = get_value_in_si(info['parameters'][second_param])
                trace_id = '%s__%s' % (second_param, val2)
                trace_id = val2
                if not trace_id in all_traces:
                    all_traces.append(trace_id)

        for t in all_traces:
            all_lines[t] = {}
            all_pvals[t] = {}

        for ref, info in self.report['Simulations'].items():
            print_v('Checking %s: %s' % (ref, info['parameters']))

            pval = get_value_in_si(info['parameters'][first_param])

            if not second_param:
                trace_id = DEFAULT_TRACE
            else:
                val2 = get_value_in_si(info['parameters'][second_param])
                trace_id = val2

            if ':' in value:
                matching_ref = value.split(':')[0]
                feature = value.split(':')[1]
            else:
                matching_ref = '*'
                feature = value

            for cell_ref in info['analysis']:
                print_v('Checking if %s matches %s, feature: %s (from %s)' %
                        (cell_ref, matching_ref, feature, value))
                if matching_ref == cell_ref or matching_ref == '*':
                    #print('y')
                    if not cell_ref in all_lines[trace_id]:
                        all_lines[trace_id][cell_ref] = []
                        all_pvals[trace_id][cell_ref] = []

                    vval = info['analysis'][cell_ref][feature]

                    all_lines[trace_id][cell_ref].append(vval)
                    all_pvals[trace_id][cell_ref].append(pval)

        print_v('Plot x: %s' % all_pvals)
        print_v('Plot y: %s' % all_lines)

        xs = []
        ys = []
        labels = []
        markers = []
        colors = []
        maxy = -1 * sys.float_info.max

        for t in all_traces:

            for ref in all_lines[t]:
                print_v('Add data %s, %s' % (t, ref))

                xs.append(all_pvals[t][ref])
                ys.append(all_lines[t][ref])

                maxy = max(maxy, max(all_lines[t][ref]))

                labels.append('%s - %s' % (t, ref))
                markers.append('o')

                pop_id = ref.split('[')[0] if '[' in ref else ref.split('/')[0]
                if self.last_network_ran:
                    pop = self.last_network_ran.get_child(
                        pop_id, 'populations')
                    color = [float(c) for c in pop.properties['color'].split()]
                else:
                    color = [random.random(), random.random(), random.random()]
                    pop = None
                print_v("This trace %s has population %s: %s, so color: %s" %
                        (ref, pop_id, pop, color))
                colors.append(color)

        xlim = None
        ylim = None

        yaxis = value.replace('_', ' ')
        yaxis = yaxis[0].upper() + yaxis[1:]

        if value == 'mean_spike_frequency':
            yaxis += ' (Hz)'
            ylim = [maxy * -0.1, maxy * 1.1]
            print_v('Setting y axes on freq plot to: %s' % ylim)

        ax = pynml.generate_plot(xs,
                                 ys,
                                 "Plot of %s vs %s in %s" %
                                 (value, first_param, self.sim),
                                 xaxis=first_param,
                                 yaxis=yaxis,
                                 labels=labels if len(labels) > 1 else None,
                                 markers=markers,
                                 colors=colors,
                                 xlim=xlim,
                                 ylim=ylim,
                                 logx=logx,
                                 logy=logy,
                                 show_plot_already=False,
                                 legend_position='right',
                                 save_figure_to=save_figure_to)  # Save figure
Exemplo n.º 46
0
######## Some example data

ts = [t * 0.01 for t in range(20000)]

siny = [math.sin(t / 10) for t in ts]
cosey = [math.exp(t / -80) * math.cos(t / 5) for t in ts]

########  Generate a plot for this quickly with generate_plot

ax = pynml.generate_plot(
    [ts, ts],  # Add 2 sets of x values
    [siny, cosey],  # Add 2 sets of y values
    "Some traces",  # Title
    xaxis='Time (ms)',  # x axis legend
    yaxis='Arbitrary units...',  # y axis legend
    linewidths=[2, 3],  # Thicknesses of each trace
    show_plot_already=False,  # Show or wait for plt.show()?
    font_size=10,  # Font
    bottom_left_spines_only=True,  # Box or just x & y axes
    save_figure_to='quick.png')  # Save figure

########   Add another trace

ts_ = [t * 0.1 for t in range(2000)]
randy = [random.random() for t in ts_]
ax.plot(ts_, randy, '.')  # Won't be included in saved PNG!

########   Show complete plot

plt.show()
Exemplo n.º 47
0
def analyse_extracted_data():

    analysed = []

    analysed = [f for f in os.listdir('.') if (f.endswith('_analysis.json'))]
    #analysed = [ f for f in os.listdir('.') if (f.endswith('_analysis.json')) ]

    analysed.sort()

    info = {}
    info[
        'info'] = 'Cell models tuned to data extracted from Cell Types Database. Note: these are preliminary models designed to test the framework for generating NeuroML models from this data, not the final, optimised models.'

    info['datasets'] = []

    to_include = None
    #to_include = ['468120757']
    #to_include = ['477127614','476686112']
    #to_include = ['477127614']
    #to_include = ['464198958']

    for f in analysed:

        if to_include == None or f.split('_')[0] in to_include:

            print("*************************\n Looking at: %s" % f)

            dataset = {}
            info['datasets'].append(dataset)
            '''
            with open(f, "r") as json_file:
                data = json.load(json_file)

            id = data['data_set_id']
            sweeps = data['sweeps']

            print("Looking at data analysis in %s (dataset: %s)"%(f,id))

            dataset['id'] = id

            currents_v_sub = {}
            currents_rate_spike = {}

            for s in sweeps.keys():
                current = float(sweeps[s]["sweep_metadata"]["aibs_stimulus_amplitude_pa"])
                print("Sweep %s (%s pA)"%(s, current))

                freq_key = '%s:mean_spike_frequency'%(s)
                steady_state_key = '%s:average_1000_1200'%(s)

                if sweeps[s]["pyelectro_iclamp_analysis"].has_key(freq_key):
                    currents_rate_spike[current] = sweeps[s]["pyelectro_iclamp_analysis"][freq_key]
                else:
                    currents_rate_spike[current] = 0
                    currents_v_sub[current] = sweeps[s]["pyelectro_iclamp_analysis"][steady_state_key]

            curents_sub = currents_v_sub.keys()
            curents_sub.sort()
            v = [currents_v_sub[c] for c in curents_sub]
            '''
            data, v_sub, curents_sub, v, curents_spike = get_if_iv_for_dataset(
                f)

            id = data['data_set_id']
            dataset['id'] = data['data_set_id']
            metas = ['aibs_cre_line', 'aibs_dendrite_type', 'location']
            for m in metas:
                dataset[m] = data[m]

            target_file = 'summary/%s_%s.png'

            pynml.generate_plot([curents_sub], [v_sub],
                                "Subthreshold responses: %s" % id,
                                colors=['k'],
                                linestyles=['-'],
                                markers=['o'],
                                xaxis="Current (pA)",
                                yaxis="Steady state (mV)",
                                xlim=[-200, 400],
                                ylim=[-120, -40],
                                grid=True,
                                show_plot_already=False,
                                save_figure_to=target_file %
                                ('subthreshold', id))
            #plt.plot(curents_sub, , color='k', linestyle='-', marker='o')

            pynml.generate_plot([curents_spike], [v],
                                "Spiking frequencies: %s" % id,
                                colors=['k'],
                                linestyles=['-'],
                                markers=['o'],
                                xaxis="Current (pA)",
                                yaxis="Firing frequency (Hz)",
                                xlim=[-200, 400],
                                ylim=[-10, 120],
                                grid=True,
                                show_plot_already=False,
                                save_figure_to=target_file % ('spikes', id))

            data0, indices = pynml.reload_standard_dat_file('%s.dat' % id)
            x = []
            y = []
            tt = [t * 1000 for t in data0['t']]
            for i in indices:
                x.append(tt)
                y.append([v * 1000 for v in data0[i]])

            cols = ['b', 'g', 'r', 'c', 'm', 'y', 'k']

            ax = pynml.generate_plot(x,
                                     y,
                                     "Example traces from: %s" % id,
                                     xaxis="Time (ms)",
                                     yaxis="Membrane potential (mV)",
                                     ylim=[-120, 60],
                                     colors=cols,
                                     show_plot_already=False,
                                     save_figure_to=target_file %
                                     ('traces', id))

            ax.spines['right'].set_visible(False)
            ax.spines['top'].set_visible(False)
            ax.yaxis.set_ticks_position('left')
            ax.xaxis.set_ticks_position('bottom')

            sweeps = data['sweeps']

            from data_helper import CURRENT_DATASETS, DATASET_TARGET_SWEEPS

            i = 0
            for s in DATASET_TARGET_SWEEPS[data['data_set_id']]:
                current = float(sweeps[str(s)]["sweep_metadata"]
                                ["aibs_stimulus_amplitude_pa"])
                print("--- Sweep %s (%s pA)" % (s, current))
                plt.text(1320,
                         +8 * i,
                         "%s pA" % (float('%.2f' % current)),
                         color=cols[i])
                i += 1

            plt.savefig(target_file % ('traces', id),
                        bbox_inches='tight',
                        pad_inches=0)

            ax = pynml.generate_plot(x,
                                     y,
                                     "Example traces from: %s" % id,
                                     ylim=[-120, 60],
                                     colors=cols,
                                     grid=False,
                                     show_plot_already=False)

            scale = 1 if not '477127614' in f else 1000

            ax.plot([1300 * scale, 1300 * scale, 1500 * scale], [40, 20, 20],
                    color='k',
                    linewidth=5,
                    marker='',
                    solid_capstyle="butt",
                    solid_joinstyle='miter')

            plt.axis('off')

            fig_file = target_file % ('traces_FIG', id)
            plt.savefig(fig_file, bbox_inches='tight', pad_inches=0)
            from PIL import Image
            img = Image.open(fig_file)

            img2 = img.crop((60, 40, 660, 480))
            img2.save(fig_file)

    print(info)
    make_html_file(info)
    make_html_file(info, template=HTML_TEMPLATE_FILE_FIG, target='Figure.html')
    make_md_file()

    if not nogui:
        plt.show()
Exemplo n.º 48
0
def analyse_spiketime_vs_dx(lems_path_dict, 
                            simulator,
                            cell_v_path,
                            verbose=False,
                            spike_threshold_mV = 0,
                            show_plot_already=True,
                            save_figure_to=None,
                            num_of_last_spikes=None):
                                
    from pyelectro.analysis import max_min
    
    all_results = {}
    comp_values=[]
    for num_of_comps in lems_path_dict.keys():
        comp_values.append(int(num_of_comps))
        if verbose:
            print_comment_v(" == Generating simulation for electrotonic length = %s"%(dx))
                                   
        if simulator == 'jNeuroML':
             results = pynml.run_lems_with_jneuroml(lems_path_dict[num_of_comps], nogui=True, load_saved_data=True, plot=False, verbose=verbose)
        if simulator == 'jNeuroML_NEURON':
             results = pynml.run_lems_with_jneuroml_neuron(lems_path_dict[num_of_comps], nogui=True, load_saved_data=True, plot=False, verbose=verbose)
             
        print("Results reloaded: %s"%results.keys())
             
        all_results[int(num_of_comps)] = results
        
    xs = []
    ys = []
    labels = []
    
    spxs = []
    spys = []
    linestyles = []
    markers = []
    colors=[]
    spike_times_final=[]
    array_of_num_of_spikes=[]
    
    comp_values=list(np.sort(comp_values))
    
    for num_of_comps in comp_values:
        t = all_results[num_of_comps]['t']
        v = all_results[num_of_comps][cell_v_path]
        xs.append(t)
        ys.append(v)
        labels.append(num_of_comps)
        
        mm = max_min(v, t, delta=0, peak_threshold=spike_threshold_mV)
        
        spike_times = mm['maxima_times']
        
        spike_times_final.append(spike_times)
        
        array_of_num_of_spikes.append(len(spike_times))
        
    max_num_of_spikes=max(array_of_num_of_spikes)
    
    max_comps_spikes=spike_times_final[-1]
    
    bound_comps=[comp_values[0],comp_values[-1]]
    
    if num_of_last_spikes == None:
    
       num_of_spikes=len(max_comps_spikes)
       
    else:
       
       if len(max_comps_spikes) >=num_of_last_spikes:
       
          num_of_spikes=num_of_last_spikes
          
       else:
       
          num_of_spikes=len(max_comps_spikes)
          
    spike_indices=[(-1)*ind for ind in range(1,num_of_spikes+1) ]
    
    if len(max_comps_spikes) > abs(spike_indices[-1]):
    
       earliest_spike_time=max_comps_spikes[spike_indices[-1]-1]
       
    else:
     
       earliest_spike_time=max_comps_spikes[spike_indices[-1]]
       
    for spike_ind in range(0,max_num_of_spikes):
   
        spike_time_values=[]
        
        compartments=[]
        
        for comp_value in range(0,len(comp_values)):
        
            if spike_times_final[comp_value] !=[]:
           
              if len(spike_times_final[comp_value]) >= spike_ind+1 :
              
                 if spike_times_final[comp_value][spike_ind] >= earliest_spike_time:
             
                    spike_time_values.append(spike_times_final[comp_value][spike_ind])
               
                    compartments.append(comp_values[comp_value])       
        
        linestyles.append('')
               
        markers.append('o')
       
        colors.append('b')
       
        spxs.append(compartments)
       
        spys.append(spike_time_values)
    
    for last_spike_index in spike_indices:
       
        vertical_line=[max_comps_spikes[last_spike_index],max_comps_spikes[last_spike_index] ]
          
        spxs.append(bound_comps)
          
        spys.append(vertical_line)
          
        linestyles.append('--')
          
        markers.append('')
       
        colors.append('k')
             
    pynml.generate_plot(spxs, 
          spys, 
          "Spike times vs spatial discretization",
          colors=colors,
          linestyles = linestyles,
          markers = markers,
          xaxis = 'Number of internal divisions',
          yaxis = 'Spike times (s)',
          show_plot_already=show_plot_already,
          save_figure_to=save_figure_to)       
    
    if verbose:
        pynml.generate_plot(xs, 
                  ys, 
                  "Membrane potentials in %s for %s"%(simulator,dts),
                  labels = labels,
                  show_plot_already=show_plot_already,
                  save_figure_to=save_figure_to)