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)
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_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()
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
if '-summary' in sys.argv: erefs = ['AllenHH_477127614', 'L23_Retuned_477127614', 'L23PyrRS'] #,'L23_Retuned_477127614'] irefs = ['AllenHH_476686112'] iv_xs = [] iv_ys = [] iv_labels = [] if_xs = [] if_ys = [] if_labels = [] for ref in erefs + irefs: ivf = '%s_IV.dat' % ref 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",
#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()
sim_dir = '../simulations/DefaultSimulationConfiguration__N/' t_nrn_f = sim_dir + 'time.dat' v_nrn_f = sim_dir + 'CG_CML_0.dat' ca_nrn_f = sim_dir + 'CG_CML_0.cad_CONC_ca.dat' x = [] y = [] xc = [] yc = [] if True or not l23: data, indices = pynml.reload_standard_dat_file(v_nml2_f) tt = [t * 1000 for t in data['t']] x.append(tt) y.append([v * 1000 for v in data[0]]) data, indices = pynml.reload_standard_dat_file(ca_nml2_f) tt = [t * 1000 for t in data['t']] xc.append(tt) yc.append([c for c in data[0]]) def read_dat_file(filename): f = open(filename)
sim_dir = "../simulations/DefaultSimulationConfiguration__N/" t_nrn_f = sim_dir + "time.dat" v_nrn_f = sim_dir + "CG_CML_0.dat" ca_nrn_f = sim_dir + "CG_CML_0.cad_CONC_ca.dat" x = [] y = [] xc = [] yc = [] if True or not l23: data, indices = pynml.reload_standard_dat_file(v_nml2_f) tt = [t * 1000 for t in data["t"]] x.append(tt) y.append([v * 1000 for v in data[0]]) data, indices = pynml.reload_standard_dat_file(ca_nml2_f) tt = [t * 1000 for t in data["t"]] xc.append(tt) yc.append([c for c in data[0]]) def read_dat_file(filename): f = open(filename)
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)
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)
from pyneuroml import pynml import matplotlib.pyplot as plt 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",
from pyneuroml import pynml import matplotlib.pyplot as plt files = ['NaV.states.dat', '../NEURON/Soma.states.dat'] for file in files: data, indices = pynml.reload_standard_dat_file(file) 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",
from pyneuroml import pynml import matplotlib.pyplot as plt 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",
from pyneuroml import pynml import matplotlib.pyplot as plt files = ['NaV.states.dat', '../NEURON/Soma.states.dat'] for file in files: data, indices = pynml.reload_standard_dat_file(file) 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,
lims2 = {'EPSC': (-7e-11, 0e-10), 'IPSC': (0e-8, 1.7e-10)} for x in lims.keys(): print("=====\nPlot for %s" % x) xs = [] ys = [] labels = [] all_seg_0 = [] all_seg_1406 = [] for f in os.listdir('./temp'): if f.startswith('v_clamps') and x in f and f.endswith('dat'): print('Adding %s' % f) seed = f.split('.')[1] seg = f.split('.')[0].split('_')[3] res, i = pynml.reload_standard_dat_file('temp/%s' % f) print res.keys() time_points = res['t'] xs.append(res['t']) ys.append(res[0]) labels.append('%s %s' % (seg, seed)) if seg == 'seg0': all_seg_0.append(res[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]
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)) data, indices = pynml.reload_standard_dat_file('%s.dat'%id) x = [] y = [] tt = [t*1000 for t in data['t']] for i in indices: x.append(tt) y.append([v*1000 for v in data[i]]) pynml.generate_plot(x, y, "Example traces from: %s"%id, xaxis = "Time (ms)", yaxis = "Membrane potential (mV)", ylim = [-120, 60], show_plot_already=False, save_figure_to = target_file%('traces', id))