Пример #1
0
def evaluate_HHExpLinearRate(rate, midpoint, scale, v):
    
    rate_si = get_value_in_si(rate)
    midpoint_si = get_value_in_si(midpoint)
    scale_si = get_value_in_si(scale)
    v_si = get_value_in_si(v)
        
    print_comment_v('Evaluating: rate * ((v - (midpoint))/scale) / ( 1 - exp(-1 * (v - (midpoint)) / scale)) ')
    print_comment_v('            %s * ((v - (%s))/%s) / ( 1 - exp(-1 * (v - (%s)) / %s))  for v = %s'%(rate, midpoint, scale, midpoint, scale, v))
    print_comment_v('            %s * ((%s - (%s))/%s) / ( 1 - exp(-1 * (%s - (%s)) / %s)) '%(rate_si, v_si, midpoint_si, scale_si, v_si, midpoint_si, scale_si))
    print_comment_v('            <... type="HHExpLinearRate" rate="%s" midpoint="%s" scale="%s" />'%(rate, midpoint, scale))
    r  = rate_si * ((v_si - (midpoint_si))/scale_si) / ( 1 - exp(-(v_si - (midpoint_si)) / scale_si))
    print_comment_v('   = %s per_s'%r)
    print_comment_v('   = %s per_ms'%(r/1000.))
Пример #2
0
def evaluate_HHSigmoidRate(rate, midpoint, scale, v):
    
    rate_si = get_value_in_si(rate)
    midpoint_si = get_value_in_si(midpoint)
    scale_si = get_value_in_si(scale)
    v_si = get_value_in_si(v)
        
    print_comment_v('Evaluating: rate / (1 + exp(-1 * (v - midpoint)/scale))  ')
    print_comment_v('            %s / ( 1 + exp(-1 * (v - (%s)) / %s))  for v = %s'%(rate, midpoint, scale, v))
    print_comment_v('            %s / ( 1 + exp(-1 * (%s - (%s)) / %s)) '%(rate_si, v_si, midpoint_si, scale_si))
    print_comment_v('            <... type="HHSigmoidRate" rate="%s" midpoint="%s" scale="%s" />'%(rate, midpoint, scale))
    r  = rate_si / (1 + exp(-1 * (v_si - midpoint_si)/scale_si))
    print_comment_v('   = %s per_s'%r)
    print_comment_v('   = %s per_ms'%(r/1000.))
Пример #3
0
def evaluate_HHExpRate(rate, midpoint, scale, v):
    '''
        Helper for putting values into HHExpRate,
        see also https://www.neuroml.org/NeuroML2CoreTypes/Channels.html#HHExpRate
    '''
    rate_si = get_value_in_si(rate)
    midpoint_si = get_value_in_si(midpoint)
    scale_si = get_value_in_si(scale)
    v_si = get_value_in_si(v)

    print_comment_v('Evaluating: rate * exp( (v - midpoint) / scale) ')
    print_comment_v('            %s * exp( (v - (%s)) / %s)  for v = %s' % (rate, midpoint, scale, v))
    print_comment_v('            %s * exp( (%s - (%s)) / %s) ' % (rate_si, v_si, midpoint_si, scale_si))
    print_comment_v('            <... type="HHExpRate" rate="%s" midpoint="%s" scale="%s" />' % (rate, midpoint, scale))
    r = rate_si * exp((v_si - midpoint_si) / scale_si)
    print_comment_v('   = %s per_s' % r)
    print_comment_v('   = %s per_ms' % (r / 1000.))
Пример #4
0
def generate_channel_density_plots(nml2_file,
                                   text_densities=False,
                                   passives_erevs=False,
                                   target_directory=None):
    nml_doc = read_neuroml2_file(nml2_file,
                                 include_includes=True,
                                 verbose=False,
                                 optimized=True)

    cell_elements = []
    cell_elements.extend(nml_doc.cells)
    cell_elements.extend(nml_doc.cell2_ca_poolses)
    svg_files = []
    all_info = {}

    for cell in cell_elements:
        info = {}
        all_info[cell.id] = info
        print_comment_v("Extracting channel density info from %s" % cell.id)
        sb = ''
        ions = {}
        maxes = {}
        mins = {}
        row = 0
        na_ions = []
        k_ions = []
        ca_ions = []
        other_ions = []

        if isinstance(cell, Cell2CaPools):
            cds = cell.biophysical_properties2_ca_pools.membrane_properties2_ca_pools.channel_densities + \
                cell.biophysical_properties2_ca_pools.membrane_properties2_ca_pools.channel_density_nernsts
        elif isinstance(cell, Cell):
            cds = cell.biophysical_properties.membrane_properties.channel_densities + \
                cell.biophysical_properties.membrane_properties.channel_density_nernsts

        epas = None
        ena = None
        ek = None
        eh = None
        eca = None

        for cd in cds:
            dens_si = get_value_in_si(cd.cond_density)
            print_comment_v(
                "cd: %s, ion_channel: %s, ion: %s, density: %s (SI: %s)" %
                (cd.id, cd.ion_channel, cd.ion, cd.cond_density, dens_si))

            ions[cd.ion_channel] = cd.ion
            erev_V = get_value_in_si(cd.erev) if hasattr(cd, 'erev') else None
            erev = '%s mV' % format_float(erev_V * 1000) if hasattr(
                cd, 'erev') else None

            if cd.ion == 'na':
                if cd.ion_channel not in na_ions:
                    na_ions.append(cd.ion_channel)
                ena = erev
                info['ena'] = erev_V
            elif cd.ion == 'k':
                if cd.ion_channel not in k_ions:
                    k_ions.append(cd.ion_channel)
                ek = erev
                info['ek'] = erev_V
            elif cd.ion == 'ca':
                if cd.ion_channel not in ca_ions:
                    ca_ions.append(cd.ion_channel)
                eca = erev
                info['eca'] = erev_V
            else:
                if cd.ion_channel not in other_ions:
                    other_ions.append(cd.ion_channel)
                if cd.ion == 'non_specific':
                    epas = erev
                    info['epas'] = erev_V
                if cd.ion == 'h':
                    eh = erev
                    info['eh'] = erev_V

            if cd.ion_channel in maxes:
                if dens_si > maxes[cd.ion_channel]:
                    maxes[cd.ion_channel] = dens_si
            else:
                maxes[cd.ion_channel] = dens_si
            if cd.ion_channel in mins:
                if dens_si < mins[cd.ion_channel]:
                    mins[cd.ion_channel] = dens_si
            else:
                mins[cd.ion_channel] = dens_si

        for ion_channel in na_ions + k_ions + ca_ions + other_ions:
            col = get_ion_color(ions[ion_channel])
            info[ion_channel] = {
                'max': maxes[ion_channel],
                'min': mins[ion_channel]
            }

            if maxes[ion_channel] > 0:
                sb += _get_rect(ion_channel, row, maxes[ion_channel],
                                mins[ion_channel], col[0], col[1], col[2],
                                text_densities)
                row += 1

        if passives_erevs:

            if ena:
                sb += add_text(row, "E Na = %s " % ena)
                row += 1
            if ek:
                sb += add_text(row, "E K = %s " % ek)
                row += 1
            if eca:
                sb += add_text(row, "E Ca = %s" % eca)
                row += 1
            if eh:
                sb += add_text(row, "E H = %s" % eh)
                row += 1
            if epas:
                sb += add_text(row, "E pas = %s" % epas)
                row += 1

            for sc in cell.biophysical_properties.membrane_properties.specific_capacitances:
                sb += add_text(row,
                               "C (%s) = %s" % (sc.segment_groups, sc.value))

                info['specific_capacitance_%s' %
                     sc.segment_groups] = get_value_in_si(sc.value)
                row += 1

            # sb+='<text x="%s" y="%s" fill="black" font-family="Arial">%s</text>\n'%(width/3., (height+spacing)*(row+1), text)

        sb = "<?xml version='1.0' encoding='UTF-8'?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + str(
            width + text_densities * 200) + "\" height=\"" + str(
                (height + spacing) * row) + "\">\n" + sb + "</svg>\n"

        print(sb)
        svg_file = nml2_file + "_channeldens.svg"
        if target_directory:
            svg_file = target_directory + "/" + svg_file.split('/')[-1]
        svg_files.append(svg_file)
        sf = open(svg_file, 'w')
        sf.write(sb)
        sf.close()
        print_comment_v("Written to %s" % os.path.abspath(svg_file))

        pp.pprint(all_info)

    return svg_files, all_info
Пример #5
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
Пример #6
0
    def _run_all(self):

        import pp
        ppservers = ()
        job_server = pp.Server(self.num_parallel_runs, ppservers=ppservers)
        print_v('\n  == Running %i jobs across %i local processes\n ' %
                (self.total_todo, job_server.get_ncpus()))
        jobs = []
        job_refs = {}

        submitted = 0
        for ref in self.report['Simulations']:

            report_here = self.report['Simulations'][ref]

            params = report_here['parameters']
            print_v("---- Submitting %s: %s" % (ref, params))

            job_dir = os.path.join(self.result_dir, ref)
            os.mkdir(job_dir)

            vars = (self.runner, submitted + 1, self.total_todo, job_dir,
                    params)

            job = job_server.submit(
                run_instance,
                args=vars,
                modules=('pyneuroml.pynml', 'shutil', 'neuroml', 'neuromllite',
                         'neuromllite.sweep.ParameterSweep',
                         'neuromllite.utils'))
            jobs.append(job)
            job_refs[len(jobs) - 1] = ref

            submitted += 1

        print_v("Submitted all jobs: %s" % job_refs)

        for job_i in range(len(jobs)):
            job = jobs[job_i]
            ref = job_refs[job_i]
            report_here = self.report['Simulations'][ref]
            report_here['analysis'] = OrderedDict()
            params = report_here['parameters']

            print_v("Checking parallel job %i/%i (%s)" %
                    (job_i, len(jobs), ref))
            traces, events = job()

            self.last_network_ran = None

            if len(traces) > 0:
                times = [t * 1000. for t in traces['t']]
                volts = OrderedDict()
                for tr in traces:
                    if tr.endswith('/v'):
                        volts[tr] = [v * 1000. for v in traces[tr]]
                    if tr.endswith('/r'): volts[tr] = [r for r in traces[tr]]

                print_v("Analysing %s..." % traces.keys())

                analysis_data = analysis.NetworkAnalysis(
                    volts,
                    times,
                    self.analysis_var,
                    start_analysis=0,
                    end_analysis=times[-1],
                    smooth_data=False,
                    show_smoothed_data=False,
                    verbose=self.verbose)

                analysed = analysis_data.analyse()

                for a in sorted(analysed.keys()):
                    ref0, var = a.split(':')
                    if not ref0 in report_here['analysis']:
                        report_here['analysis'][ref0] = OrderedDict()
                    report_here['analysis'][ref0][var] = analysed[a]

            for e in sorted(events.keys()):
                x = events[e]
                print_v('Examining event %s: %s -> %s (len: %i)' %
                        (e, x[0] if len(x) > 0 else '-',
                         x[-1] if len(x) > 0 else '-', len(x)))
                ref0 = '%s/spike' % e
                analysed = OrderedDict()
                l = len(x)
                tmax_si = self._get_sim_duration_ms(params) / 1000.
                f_hz = l / tmax_si
                #print_v('This has %s points in %s sec, so %s Hz'%(l,tmax_si, f_hz))
                analysed["mean_spike_frequency"] = f_hz

                if not ref0 in report_here['analysis']:
                    report_here['analysis'][ref0] = OrderedDict()

                report_here['analysis'][ref0] = analysed

            if self.plot_all or self.heatmap_all:
                for y in traces.keys():
                    if y != 't':
                        pop_id = y.split('[')[0] if '[' in y else y.split(
                            '/')[0]
                        pop = None  # self.last_network_ran.get_child(pop_id, 'populations')
                        if pop:
                            color = [
                                float(c)
                                for c in pop.properties['color'].split()
                            ]
                            #print_v("This trace %s has population %s: %s, so color: %s"%(y,pop_id,pop,color))
                        else:
                            #print_v("This trace %s has population %s: %s which has no color..."%(y,pop_id,pop))
                            color = [1, 0, 0]

                        if self.plot_all:
                            label = '%s (%s)' % (y, params)
                            self.ax.plot([t * 1000 for t in traces['t']],
                                         [v * 1000 for v in traces[y]],
                                         label=label)

                        if self.heatmap_all:
                            dt = self.sim.dt if not 'dt' in params else params[
                                'dt']
                            downscale = int(0.1 / dt)
                            d = [
                                traces[y][i] * 1000
                                for i in range(len(traces[y]))
                                if i % downscale == 0
                            ]
                            tt = [
                                traces['t'][i] * 1000
                                for i in range(len(traces['t']))
                                if i % downscale == 0
                            ]

                            param_name = self.vary.keys()[0]
                            pval = get_value_in_si(params[param_name])
                            if self.hm_x == None:
                                self.hm_x = tt
                            print_v(
                                '  ==  Trace %s (%s) downscaled by factor %i from %i to %i points for heatmap; y value: %s=%s'
                                % (y, ref, downscale, len(
                                    traces[y]), len(d), param_name, pval))
                            self.hm_y.append(pval)
                            self.hm_z.append(d)

            print_v("Finished checking parallel job %i/%i (%s)" %
                    (job_i, len(jobs), ref))

        job_server.print_stats()
        job_server.destroy()
        print_v("-------------------------------------------")
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)
Пример #8
0
    def handle_population(self,
                          population_id,
                          component,
                          size=-1,
                          component_obj=None,
                          properties={}):

        sizeInfo = " as yet unspecified size"
        if size >= 0:
            sizeInfo = ", size: " + str(size) + " cells"
        if component_obj:
            compInfo = " (%s)" % component_obj.__class__.__name__
        else:
            compInfo = ""

        print_v("Population: " + population_id + ", component: " + component +
                compInfo + sizeInfo + ", properties: %s" % properties)

        if size >= 0:
            for i in range(size):
                node_id = "%s_%i" % (population_id, i)
                node = {}

                comp = self.nl_network.get_child(component, "cells")
                base_dir = "./"  # for now...

                if properties:
                    node["metadata"] = properties

                node["parameters"] = {}
                node["input_ports"] = {}
                node["output_ports"] = {}
                if comp is not None:
                    if comp.lems_source_file:
                        fname = locate_file(comp.lems_source_file, base_dir)
                        model = MDFHandler._load_lems_file_with_neuroml2_types(
                            fname)
                        lems_comp = model.components.get(component)

                    if comp.neuroml2_cell:
                        model = MDFHandler._get_lems_model_with_neuroml2_types(
                        )
                        lems_comp = lems.Component(id_=comp.id,
                                                   type_=comp.neuroml2_cell)
                        for p in comp.parameters:
                            lems_comp.set_parameter(
                                p,
                                evaluate(comp.parameters[p],
                                         self.nl_network.parameters),
                            )

                    print_v("All LEMS comps in model: %s" %
                            model.components.keys())
                    print_v("This comp: %s" % lems_comp)
                    comp_type_name = lems_comp.type
                    lems_comp_type = model.component_types.get(comp_type_name)
                    notes = "Cell: [%s] is defined in %s and in Lems is: %s" % (
                        comp,
                        comp.lems_source_file,
                        lems_comp,
                    )

                    node["notes"] = notes

                    for p in lems_comp.parameters:
                        node["parameters"][p] = {
                            "value":
                            get_value_in_si(evaluate(lems_comp.parameters[p]))
                        }

                    for c in lems_comp_type.constants:
                        node["parameters"][c.name] = {
                            "value": get_value_in_si(c.value)
                        }

                    for sv in lems_comp_type.dynamics.state_variables:
                        node["parameters"][sv.name] = {}
                        if sv.exposure:
                            node["output_ports"][sv.exposure] = {
                                "value": sv.name
                            }

                    for dv in lems_comp_type.dynamics.derived_variables:

                        print_v("Converting: %s (exp: %s) = [%s] or [%s]" %
                                (dv.name, dv.exposure, dv.value, dv.select))
                        if dv.name == "INPUT":
                            node["input_ports"][dv.name] = {}
                        else:
                            if dv.value is not None:
                                node["parameters"][dv.name] = {
                                    "value": self._convert_value(dv.value)
                                }
                                if dv.exposure:
                                    node["output_ports"][dv.exposure] = {
                                        "value": dv.name
                                    }
                            if dv.select is not None:
                                in_port = dv.select.replace("[*]/", "_")
                                node["input_ports"][in_port] = {}
                                node["parameters"][dv.name] = {
                                    "value": in_port
                                }

                    conditions = 0
                    for eh in lems_comp_type.dynamics.event_handlers:

                        print_v("Converting: %s (type: %s)" % (eh, type(eh)))
                        if type(eh) == lems.OnStart:
                            for a in eh.actions:
                                if type(a) == lems.StateAssignment:
                                    node["parameters"][a.variable][
                                        "default_initial_value"] = a.value
                        if type(eh) == lems.OnCondition:
                            test = (eh.test.replace(".gt.", ">").replace(
                                ".geq.", ">=").replace(".lt.", "<").replace(
                                    ".leq.", "<=").replace(".eq.", "=="))
                            for a in eh.actions:
                                if type(a) == lems.StateAssignment:
                                    if (not "conditions"
                                            in node["parameters"][a.variable]):
                                        node["parameters"][
                                            a.variable]["conditions"] = {}

                                    node["parameters"][
                                        a.variable]["conditions"][
                                            "condition_%i" % conditions] = {
                                                "test": test,
                                                "value": a.value
                                            }
                            conditions += 1

                    for td in lems_comp_type.dynamics.time_derivatives:
                        node["parameters"][td.variable][
                            "time_derivative"] = self._convert_value(td.value)

                self.mdf_graph["nodes"][node_id] = node
Пример #9
0
    for seg in cell.morphology.segments:
        v_quantity = '%s/0/%s/%s/v' % (pop.id, cell_comp, seg.id)
        all_vs.append(v_quantity)

    if isinstance(cell, neuroml.Cell2CaPools):
        cds = cell.biophysical_properties2_ca_pools.membrane_properties2_ca_pools.channel_densities + \
            cell.biophysical_properties2_ca_pools.membrane_properties2_ca_pools.channel_density_nernsts
    elif isinstance(cell, neuroml.Cell):
        cds = cell.biophysical_properties.membrane_properties.channel_densities + \
            cell.biophysical_properties.membrane_properties.channel_density_nernsts

    all_currents = []
    gen_plots_for_quantities['Allcurrents'] = all_currents

    for cd in cds:
        dens_si = get_value_in_si(cd.cond_density)
        group = cd.segment_groups if cd.segment_groups else 'all'
        segs_here = cell.get_all_segments_in_group(group)
        print(
            "cd: %s, ion_channel: %s, ion: %s, density: %s (SI: %s), segs: %s"
            % (cd.id, cd.ion_channel, cd.ion, cd.cond_density, dens_si,
               segs_here))
        for seg_id in segs_here:
            quantity = '%s/0/%s/%s/biophys/membraneProperties/%s/iDensity' % (
                pop.id, cell_comp, seg_id, cd.id)
            all_currents.append(quantity)
            chan_curr_file = '%s_%s_seg%s.dat' % (cd.id, cd.ion, seg_id)
            gen_saves_for_quantities[chan_curr_file] = [quantity]

    generate_lems_file_for_neuroml(
        sim_id,
Пример #10
0
def generate_channel_density_plots(nml2_file, text_densities=False, passives_erevs=False, target_directory=None):
    
    nml_doc = read_neuroml2_file(nml2_file, include_includes=True, verbose=False, optimized=True)
    
    cell_elements = []
    cell_elements.extend(nml_doc.cells)
    cell_elements.extend(nml_doc.cell2_ca_poolses)
    svg_files = []
    all_info = {}
    
    for cell in cell_elements:
        info = {}
        all_info[cell.id] = info
        print_comment_v("Extracting channel density info from %s"%cell.id)
        sb = ''
        ions = {}
        maxes = {}
        mins = {}
        row = 0
        na_ions = []
        k_ions = []
        ca_ions = []
        other_ions = []
        
        if isinstance(cell, Cell2CaPools):
            cds = cell.biophysical_properties2_ca_pools.membrane_properties2_ca_pools.channel_densities + \
                cell.biophysical_properties2_ca_pools.membrane_properties2_ca_pools.channel_density_nernsts
        elif isinstance(cell, Cell):
            cds = cell.biophysical_properties.membrane_properties.channel_densities + \
                cell.biophysical_properties.membrane_properties.channel_density_nernsts
              
        epas = None
        ena = None
        ek = None
        eh = None
        eca = None
              
        for cd in cds:
            dens_si = get_value_in_si(cd.cond_density)
            print_comment_v("cd: %s, ion_channel: %s, ion: %s, density: %s (SI: %s)"%(cd.id,cd.ion_channel,cd.ion,cd.cond_density,dens_si))
            
            ions[cd.ion_channel] = cd.ion
            erev_V = get_value_in_si(cd.erev) if hasattr(cd,'erev') else None
            erev = '%s mV'%format_float(erev_V*1000) if hasattr(cd,'erev') else None
            
            if cd.ion == 'na':
                if not cd.ion_channel in na_ions: na_ions.append(cd.ion_channel)
                ena = erev
                info['ena']=erev_V
            elif cd.ion == 'k':
                if not cd.ion_channel in k_ions: k_ions.append(cd.ion_channel)
                ek = erev
                info['ek']=erev_V
            elif cd.ion == 'ca':
                if not cd.ion_channel in ca_ions: ca_ions.append(cd.ion_channel)
                eca = erev
                info['eca']=erev_V
            else:
                if not cd.ion_channel in other_ions: other_ions.append(cd.ion_channel)
                if cd.ion == 'non_specific':
                    epas = erev
                    info['epas']=erev_V
                if cd.ion == 'h':
                    eh = erev
                    info['eh']=erev_V
            
            if cd.ion_channel in maxes:
                if dens_si>maxes[cd.ion_channel]: maxes[cd.ion_channel]=dens_si
            else: 
                maxes[cd.ion_channel]=dens_si
            if cd.ion_channel in mins:
                if dens_si<mins[cd.ion_channel]: mins[cd.ion_channel]=dens_si
            else: 
                mins[cd.ion_channel]=dens_si
                
        for ion_channel in na_ions + k_ions + ca_ions + other_ions:
            col = get_ion_color(ions[ion_channel])
            info[ion_channel]={'max':maxes[ion_channel],'min':mins[ion_channel]}
            
            if maxes[ion_channel]>0:
                sb+=_get_rect(ion_channel, row, maxes[ion_channel],mins[ion_channel],col[0],col[1],col[2],text_densities)
                row+=1
            
        if passives_erevs:
            
            if ena:
                sb+=add_text(row, "E Na = %s "%ena)
                row+=1
            if ek:
                sb+=add_text(row, "E K = %s "%ek)
                row+=1
            if eca:
                sb+=add_text(row, "E Ca = %s"%eca)
                row+=1
            if eh:
                sb+=add_text(row, "E H = %s"%eh)
                row+=1
            if epas:
                sb+=add_text(row, "E pas = %s"%epas)
                row+=1
                
            for sc in cell.biophysical_properties.membrane_properties.specific_capacitances:
                sb+=add_text(row, "C (%s) = %s"%(sc.segment_groups, sc.value))
                
                info['specific_capacitance_%s'%sc.segment_groups]=get_value_in_si(sc.value)
                row+=1
                
                
            #sb+='<text x="%s" y="%s" fill="black" font-family="Arial">%s</text>\n'%(width/3., (height+spacing)*(row+1), text)
        
            
        sb="<?xml version='1.0' encoding='UTF-8'?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" width=\""+str(width+text_densities*200)+"\" height=\""+str((height+spacing)*row)+"\">\n"+sb+"</svg>\n"

        print(sb)
        svg_file = nml2_file+"_channeldens.svg"
        if target_directory:
            svg_file = target_directory+"/"+svg_file.split('/')[-1]
        svg_files.append(svg_file)
        sf = open(svg_file,'w')
        sf.write(sb)
        sf.close()
        print_comment_v("Written to %s"%os.path.abspath(svg_file))
        
        pp.pprint(all_info)
        
    return svg_files, all_info
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)
Пример #12
0
    def handle_population(self,
                          population_id,
                          component,
                          size=-1,
                          component_obj=None,
                          properties={}):

        sizeInfo = " as yet unspecified size"
        if size >= 0:
            sizeInfo = ", size: " + str(size) + " cells"
        if component_obj:
            compInfo = " (%s)" % component_obj.__class__.__name__
        else:
            compInfo = ""

        print_v("Population: " + population_id + ", component: " + component +
                compInfo + sizeInfo)

        if size >= 0:
            for i in range(size):
                node_id = '%s_%i' % (population_id, i)
                node = {}
                #node['type'] = {}
                #node['name'] = node_id
                #node['type']['NeuroML'] = component

                comp = self.nl_network.get_child(component, 'cells')
                base_dir = './'  # for now...

                node['parameters'] = {}
                node['input_ports'] = {}
                node['output_ports'] = {}
                if comp is not None and comp.lems_source_file:
                    fname = locate_file(comp.lems_source_file, base_dir)

                    model = MDFHandler._load_lems_file_with_neuroml2_types(
                        fname)

                    #print('All comp types: %s'%model.component_types.keys())
                    #print('All comps: %s'%model.components.keys())
                    lems_comp = model.components.get(component)
                    comp_type_name = lems_comp.type
                    lems_comp_type = model.component_types.get(comp_type_name)
                    notes = 'Cell: [%s] is defined in %s and in Lems is: %s' % (
                        comp, fname, lems_comp)

                    node['notes'] = notes

                    for p in lems_comp.parameters:
                        node['parameters'][p] = {
                            'value':
                            get_value_in_si(evaluate(lems_comp.parameters[p]))
                        }

                    for c in lems_comp_type.constants:
                        node['parameters'][c.name] = {
                            'value': get_value_in_si(c.value)
                        }

                    for dv in lems_comp_type.dynamics.derived_variables:

                        if dv.name == 'INPUT':
                            node['input_ports'][dv.name] = {}
                        else:
                            if dv.exposure:
                                #<DerivedVariable name="OUTPUT" dimension="none" exposure="OUTPUT" value="variable"/>
                                node['output_ports'][dv.exposure] = {
                                    'value': self._convert_value(dv.value)
                                }

                    for sv in lems_comp_type.dynamics.state_variables:
                        node['parameters'][sv.name] = {}

                    print(dir(lems_comp_type.dynamics))

                    for os in lems_comp_type.dynamics.event_handlers:
                        if type(os) == lems.OnStart:
                            for a in os.actions:
                                if type(a) == lems.StateAssignment:
                                    node['parameters'][a.variable][
                                        'default_initial_value'] = a.value

                    for td in lems_comp_type.dynamics.time_derivatives:
                        node['parameters'][td.variable][
                            'time_derivative'] = self._convert_value(td.value)

                if self.pnl_additions:
                    node['type']["PNL"] = type
                    node['type']["generic"] = None
                #node['parameters']['PNL'] = {}
                '''
                node['functions'] = []
                func_info = {}

                if self.pnl_additions:
                    func_info['type']={}
                    func_info['type']['generic']=function
                func_info['name']='Function_%s'%function
                func_info['args']={}
                for p in lems_comp.parameters:
                    func_info['args'][p] = {}
                    func_info['args'][p]['type'] = 'float'
                    func_info['args'][p]['source'] = '%s.input_ports.%s'%(node_id,p)

                    if comp.parameters is not None and p in comp.parameters:
                        func_info['args'][p]['value'] = evaluate(comp.parameters[p], self.nl_network.parameters)
                    else:
                        func_info['args'][p]['value'] = evaluate(lems_comp.parameters[p]) # evaluate to ensure strings -> ints/floats etc

                node['functions'].append(func_info)'''
                self.mdf_graph['nodes'][node_id] = node
            '''