def run(a=None, **kwargs): try: import neuroml import pyneuroml import xlrd except Exception as e: print_("Cannot import one of the required packages. Please install!\n" "Exception: %s\n" % e) try: if os.environ.has_key('C302_HOME'): os.environ['C302_HOME'] sys.path.append(os.environ['C302_HOME']) print_('Python path now: %s' % sys.path) import c302 import c302_utils except Exception as e: print_( "Cannot import c302!\n" "Exception: %s\n" % e + "Please set environment variable C302_HOME to point to the directory: CElegansNeuroML/CElegans/pythonScripts/c302!\n" ) exit() a = build_namespace(a, **kwargs) gen_start = time.time() ref = a.reference if not os.path.isdir('simulations'): os.mkdir('simulations') sim_ref = "%s_%s_%s" % (a.c302params, ref, time.ctime().replace( ' ', '_').replace(':', '.')) sim_dir = "simulations/%s" % (sim_ref) os.mkdir(sim_dir) #exec('from %s import ParameterisedModel'%a.c302params) #params = ParameterisedModel() id = '%s_%s' % (a.c302params, ref) exec('from c302_%s import setup' % ref) setup(a.c302params, generate=True, duration=a.duration, dt=a.dt, target_directory=sim_dir) lems_file0 = '%s/LEMS_c302_%s.xml' % (sim_dir, id) lems_file = '%s/LEMS_c302.xml' % (sim_dir) print_("Renaming %s -> %s" % (lems_file0, lems_file)) os.rename(lems_file0, lems_file) announce("Generating NEURON files from: %s..." % lems_file) pynml.run_lems_with_jneuroml_neuron(lems_file, only_generate_scripts=True, nogui=True, load_saved_data=False, verbose=True) main_nrn_py = open('%s/LEMS_c302_nrn.py' % (sim_dir), 'r') updated = '' for line in main_nrn_py: line = line.replace('GenericCell.hoc', '%s/GenericCell.hoc' % sim_dir) line = line.replace('GenericNeuronCell.hoc', '%s/GenericNeuronCell.hoc' % sim_dir) line = line.replace('GenericMuscleCell.hoc', '%s/GenericMuscleCell.hoc' % sim_dir) line = line.replace("open('time.dat", "open('%s/time.dat" % sim_dir) line = line.replace("open('c302_", "open('%s/c302_" % sim_dir) updated += line main_nrn_py.close() main_nrn_py = open('%s/LEMS_c302_nrn.py' % (sim_dir), 'w') main_nrn_py.write(updated) main_nrn_py.close() run_dir = '.' command = 'nrnivmodl %s' % sim_dir announce("Compiling NMODL files for NEURON...") pynml.execute_command_in_dir(command, run_dir, prefix="nrnivmodl: ") command = './Release/Sibernetic -c302 -f worm -no_g -l_to lpath=%s timelimit=%s timestep=%s' % ( sim_dir, a.duration / 1000.0, a.dt / 1000) env = {"PYTHONPATH": "./src:./%s" % sim_dir} sim_start = time.time() announce( "Executing main Sibernetic simulation of %sms using: \n\n %s \n\n in %s with %s" % (a.duration, command, run_dir, env)) #pynml.execute_command_in_dir('env', run_dir, prefix="Sibernetic: ",env=env,verbose=True) pynml.execute_command_in_dir(command, run_dir, prefix="Sibernetic: ", env=env, verbose=True) sim_end = time.time() reportj = {} reportj['duration'] = '%s ms' % a.duration reportj['dt'] = '%s ms' % a.dt reportj['sim_ref'] = sim_ref reportj['reference'] = a.reference reportj['c302params'] = a.c302params reportj['generation_time'] = '%s s' % (sim_start - gen_start) reportj['run_time'] = '%s s' % (sim_end - sim_start) reportj['command'] = '%s' % (command) report_file = open("%s/report.json" % sim_dir, 'w') report_file.write(pp.pformat(reportj)) report_file.close() announce("Generating images for neuronal activity...") results = pynml.reload_saved_data(lems_file, plot=False, show_plot_already=False, simulator=None, verbose=True) c302_utils.plot_c302_results(results, config=a.reference, parameter_set=a.c302params, directory=sim_dir, save=True, show_plot_already=False) pos_file_name = os.path.abspath('%s/position_buffer.txt' % sim_dir) announce("Plotting positions of worm body particles in %s..." % pos_file_name) from plot_positions import plot_positions if not os.path.isfile(pos_file_name): time.sleep(2) plot_positions(pos_file_name, rate_to_plot=int(a.duration / 5)) announce("Finished in %s sec!\n\nSimulation saved in: %s\n\n"%((sim_end-sim_start),sim_dir) + \ "Report of simulation at: %s/report.json\n\n"%(sim_dir)+ \ "Rerun simulation with: ./Release/Sibernetic -l_from lpath=%s\n"%(sim_dir))
import sys from pyneuroml import pynml example_lems_file = 'LEMS_NML2_Ex5_DetCell.xml' pynml.reload_saved_data(example_lems_file, plot=True, show_plot_already=False) example_lems_file = 'LEMS_NML2_Ex9_FN.xml' pynml.reload_saved_data(example_lems_file, plot=True, show_plot_already=False) if not '-nogui' in sys.argv: from matplotlib import pyplot as plt plt.show()
def run(a=None,**kwargs): try: import neuroml import pyneuroml import xlrd except Exception as e: print_("Cannot import one of the required packages. Please install!\n" "Exception: %s\n"%e) try: if os.environ.has_key('C302_HOME'): os.environ['C302_HOME'] sys.path.append(os.environ['C302_HOME']) print_('Python path now: %s'%sys.path) import c302 import c302_utils except Exception as e: print_("Cannot import c302!\n" "Exception: %s\n"%e +"Please set environment variable C302_HOME to point to the directory: CElegansNeuroML/CElegans/pythonScripts/c302!\n") exit() a = build_namespace(a,**kwargs) gen_start = time.time() ref = a.reference if not os.path.isdir('simulations'): os.mkdir('simulations') sim_ref = "%s_%s_%s"%(a.c302params,ref, time.ctime().replace(' ','_' ).replace(':','.' )) sim_dir = "simulations/%s"%(sim_ref) os.mkdir(sim_dir) #exec('from %s import ParameterisedModel'%a.c302params) #params = ParameterisedModel() id = '%s_%s'%(a.c302params,ref) exec('from c302_%s import setup'%ref) setup(a.c302params, generate=True, duration = a.duration, dt = a.dt, target_directory=sim_dir) lems_file0 = '%s/LEMS_c302_%s.xml'%(sim_dir,id) lems_file = '%s/LEMS_c302.xml'%(sim_dir) print_("Renaming %s -> %s"%(lems_file0,lems_file)) os.rename(lems_file0,lems_file) announce("Generating NEURON files from: %s..."%lems_file) pynml.run_lems_with_jneuroml_neuron(lems_file, only_generate_scripts=True, nogui=True, load_saved_data=False, verbose=True) main_nrn_py = open('%s/LEMS_c302_nrn.py'%(sim_dir),'r') updated ='' for line in main_nrn_py: line = line.replace('GenericCell.hoc','%s/GenericCell.hoc'%sim_dir) line = line.replace('GenericNeuronCell.hoc','%s/GenericNeuronCell.hoc'%sim_dir) line = line.replace('GenericMuscleCell.hoc','%s/GenericMuscleCell.hoc'%sim_dir) line = line.replace("open('time.dat","open('%s/time.dat"%sim_dir) line = line.replace("open('c302_","open('%s/c302_"%sim_dir) updated += line main_nrn_py.close() main_nrn_py = open('%s/LEMS_c302_nrn.py'%(sim_dir),'w') main_nrn_py.write(updated) main_nrn_py.close() run_dir = '.' command = 'nrnivmodl %s'%sim_dir announce("Compiling NMODL files for NEURON...") pynml.execute_command_in_dir(command, run_dir, prefix="nrnivmodl: ") command = './Release/Sibernetic -c302 -f worm -no_g -l_to lpath=%s timelimit=%s timestep=%s'%(sim_dir,a.duration/1000.0,a.dt/1000) env={"PYTHONPATH":"./src:./%s"%sim_dir} sim_start = time.time() announce("Executing main Sibernetic simulation of %sms using: \n\n %s \n\n in %s with %s"%(a.duration, command, run_dir, env)) #pynml.execute_command_in_dir('env', run_dir, prefix="Sibernetic: ",env=env,verbose=True) pynml.execute_command_in_dir(command, run_dir, prefix="Sibernetic: ",env=env,verbose=True) sim_end = time.time() reportj = {} reportj['duration'] = '%s ms'%a.duration reportj['dt'] = '%s ms'%a.dt reportj['sim_ref'] = sim_ref reportj['reference'] = a.reference reportj['c302params'] = a.c302params reportj['generation_time'] = '%s s'%(sim_start-gen_start) reportj['run_time'] = '%s s'%(sim_end-sim_start) reportj['command'] = '%s'%(command) report_file = open("%s/report.json"%sim_dir,'w') report_file.write(pp.pformat(reportj)) report_file.close() announce("Generating images for neuronal activity...") results = pynml.reload_saved_data(lems_file, plot=False, show_plot_already=False, simulator=None, verbose=True) c302_utils.plot_c302_results(results, config=a.reference, parameter_set=a.c302params, directory=sim_dir, save=True, show_plot_already=False) pos_file_name = os.path.abspath('%s/position_buffer.txt'%sim_dir) announce("Plotting positions of worm body particles in %s..."%pos_file_name) from plot_positions import plot_positions if not os.path.isfile(pos_file_name): time.sleep(2) plot_positions(pos_file_name,rate_to_plot = int(a.duration/5)) announce("Finished in %s sec!\n\nSimulation saved in: %s\n\n"%((sim_end-sim_start),sim_dir) + \ "Report of simulation at: %s/report.json\n\n"%(sim_dir)+ \ "Rerun simulation with: ./Release/Sibernetic -l_from lpath=%s\n"%(sim_dir))
def main (argv): args = process_args() #for v in range(int(args.minV),int(args.maxV)+5,5): print get_rainbow_color_for_volts(v, args) #exit() results = pynml.reload_saved_data(args.lems_file_name, plot=False) times = [t*1000 for t in results['t']] dt = times[1]-times[0] #stepTime = (args.skip+1)*dt t = 0 times_used = [] frame_indices = [] to_skip = 0 index = 0 while t<=args.endTime: if to_skip == 0: times_used.append(t) frame_indices.append(index) to_skip = args.skip else: to_skip -=1 index+=1 t = times[index] print_comment_v("There are %i time points total, max: %f ms, dt: %f ms"%(len(times),times[-1], dt)) print_comment_v("times_used: %s; frame_indices %s"%(times_used, frame_indices)) print_comment_v("All refs: %s"%results.keys()) volt_colors = {} for ref in results.keys(): if ref!='t': pathBits = ref.split('/') pop = pathBits[0] index = pathBits[1] seg = pathBits[3] ref2 = '%s_%s'%(pop, index) if seg == '0' or seg == 'v': volt_color =[] for i in frame_indices: v = results[ref][i]*1000 colour = get_rainbow_color_for_volts(v, args) if args.rainbow else get_color_for_volts(v, args) volt_color.append(colour) volt_colors[ref2] = volt_color print_comment_v("All refs: %s"%volt_colors.keys()) print_comment_v("All volt_colors: %s"%volt_colors) t=args.startTime index = 0 #give the single frames an alphabetical order maxind = "00000" ind = "00000" bat_file_name = "%s_pov.bat"%(args.prefix) bat_file = open(bat_file_name, 'w') sh_file_name = "%s_pov.sh"%(args.prefix) sh_file = open(sh_file_name, 'w') for fi in frame_indices: t = times[fi] print_comment_v("\n---- Exporting for time: %f, index %i frame index %i ----\n"%(t, index, fi)) if not args.singlecell: in_file_name = args.prefix+"_net.inc" in_file = open(in_file_name) out_file_name = args.prefix+"_net.inc"+str(index) out_file = open(out_file_name, 'w') print_comment_v("in_file_name %s; out_file_name: %s"%(in_file_name,out_file_name)) for line in in_file: if line.strip().startswith("//"): ref = line.strip()[2:] if ref in volt_colors.keys(): vs = volt_colors[ref] #print_comment_v(('-- %s: %s '%(ref,len(vs))) out_file.write(" %s // %s t= %s\n" %(vs[index], ref, t)) elif ref+".0" in volt_colors.keys(): vs = volt_colors[ref+".0"] out_file.write(" "+vs[index]+" //"+ref+" t= "+str(t)+"\n") else: out_file.write("// No ref there: "+ref+"\n") print_comment_v("Missing ref: "+ref) else: out_file.write(line) in_file.close() out_file.close() print_comment_v("Written file: %s for time: %f"%(out_file_name, t)) in_file = open(args.prefix+".pov") out_file_name = "%s_T%i.pov"%(args.prefix, index) out_file = open(out_file_name, 'w') clock = args.rotations * (t-args.startTime)/(args.endTime-args.startTime) pre = '%s_net.inc'%args.prefix pre = pre.split('/')[-1] post = '%s_net.inc%i'%(args.prefix,index) post = post.split('/')[-1] print_comment_v("Swapping %s for %s"%(pre, post)) for line in in_file: if line.find(pre)>=0: out_file.write(line.replace(pre,post)) else: out_file.write(line.replace("clock", str(clock))) print_comment_v("Written file: %s for time: %f"%(out_file_name, t)) in_file.close() out_file.close() toEx = os.path.realpath(out_file_name) bat_file.write("C:\\Users\\Padraig\\AppData\\Local\\Programs\\POV-Ray\\v3.7\\bin\\pvengine.exe %s /nr /exit\n"%toEx) sh_file.write("povray %s %s\n"%(args.povrayOptions,toEx) ) else: ind = maxind[0:len(maxind)-len(str(index))] #compute index indentation in_file = open(args.prefix+"_cells.inc") out_file_name = args.prefix+"_cells.inc"+ind+str(index) out_file = open(out_file_name, 'w') dummy_ref = 'CELL_GROUP_NAME_0' for line in in_file: if line.strip().startswith("//"): ref = line.strip()[2:] ref = ref.replace(dummy_ref, args.singlecell) if ref in volts.keys(): vs = volts[ref] out_file.write(" "+vs[index]+"\n//"+ref+" t= "+ind+str(t)+"\n") else: out_file.write("//No ref found: "+ref+", was looking for "+dummy_ref+"\n") else: out_file.write(line) in_file.close() out_file.close() print_comment_v("Written file: %s for time: %f"%(out_file_name, t)) in_file = open(args.prefix+".pov") out_file_name = "%s_T%s%i.pov"%(args.prefix, ind, index) out_file = open(out_file_name, 'w') for line in in_file: pre = '%s_cells.inc'%args.prefix post = '%s_cells.inc%s%i'%(args.prefix, ind, index) if line.find(pre)>=0: out_file.write(line.replace(pre,post)) else: clock = args.rotations * (t-args.startTime)/(args.endTime-args.startTime) out_file.write(line.replace("clock", str(clock))) print_comment_v("Written file: %s for time: %f"%(out_file_name, t)) in_file.close() out_file.close() toEx = os.path.realpath(out_file_name) bat_file.write("C:\\Users\\Padraig\\AppData\\Local\\Programs\\POV-Ray\\v3.7\\bin\\pvengine.exe %s /nr /exit\n"%toEx) sh_file.write("povray %s %s\n"%(args.povrayOptions,toEx) ) index=index+1 print_comment_v("Done!: ") print_comment_v("\nTo generate images type:\n\n bash %s_pov.sh\n\n"%args.prefix)
def main(argv): args = process_args() #for v in range(int(args.minV),int(args.maxV)+5,5): print get_rainbow_color_for_volts(v, args) #exit() results = pynml.reload_saved_data(args.lems_file_name, plot=False) times = [t * 1000 for t in results['t']] dt = times[1] - times[0] #stepTime = (args.skip+1)*dt t = 0 times_used = [] frame_indices = [] to_skip = 0 index = 0 while t <= args.endTime: if to_skip == 0: times_used.append(t) frame_indices.append(index) to_skip = args.skip else: to_skip -= 1 index += 1 t = times[index] print_comment_v("There are %i time points total, max: %f ms, dt: %f ms" % (len(times), times[-1], dt)) print_comment_v("times_used: %s; frame_indices %s" % (times_used, frame_indices)) print_comment_v("All refs: %s" % results.keys()) volt_colors = {} for ref in results.keys(): if ref != 't': pathBits = ref.split('/') pop = pathBits[0] index = pathBits[1] seg = pathBits[3] ref2 = '%s_%s' % (pop, index) if seg == '0' or seg == 'v': volt_color = [] for i in frame_indices: v = results[ref][i] * 1000 colour = get_rainbow_color_for_volts( v, args) if args.rainbow else get_color_for_volts( v, args) volt_color.append(colour) volt_colors[ref2] = volt_color print_comment_v("All refs: %s" % volt_colors.keys()) print_comment_v("All volt_colors: %s" % volt_colors) t = args.startTime index = 0 #give the single frames an alphabetical order maxind = "00000" ind = "00000" bat_file_name = "%s_pov.bat" % (args.prefix) bat_file = open(bat_file_name, 'w') sh_file_name = "%s_pov.sh" % (args.prefix) sh_file = open(sh_file_name, 'w') for fi in frame_indices: t = times[fi] print_comment_v( "\n---- Exporting for time: %f, index %i frame index %i ----\n" % (t, index, fi)) if not args.singlecell: in_file_name = args.prefix + "_net.inc" in_file = open(in_file_name) out_file_name = args.prefix + "_net.inc" + str(index) out_file = open(out_file_name, 'w') print_comment_v("in_file_name %s; out_file_name: %s" % (in_file_name, out_file_name)) for line in in_file: if line.strip().startswith("//"): ref = line.strip()[2:] if ref in volt_colors.keys(): vs = volt_colors[ref] #print_comment_v(('-- %s: %s '%(ref,len(vs))) out_file.write(" %s // %s t= %s\n" % (vs[index], ref, t)) elif ref + ".0" in volt_colors.keys(): vs = volt_colors[ref + ".0"] out_file.write(" " + vs[index] + " //" + ref + " t= " + str(t) + "\n") else: out_file.write("// No ref there: " + ref + "\n") print_comment_v("Missing ref: " + ref) else: out_file.write(line) in_file.close() out_file.close() print_comment_v("Written file: %s for time: %f" % (out_file_name, t)) in_file = open(args.prefix + ".pov") out_file_name = "%s_T%i.pov" % (args.prefix, index) out_file = open(out_file_name, 'w') clock = args.rotations * (t - args.startTime) / (args.endTime - args.startTime) pre = '%s_net.inc' % args.prefix pre = pre.split('/')[-1] post = '%s_net.inc%i' % (args.prefix, index) post = post.split('/')[-1] print_comment_v("Swapping %s for %s" % (pre, post)) for line in in_file: if line.find(pre) >= 0: out_file.write(line.replace(pre, post)) else: out_file.write(line.replace("clock", str(clock))) print_comment_v("Written file: %s for time: %f" % (out_file_name, t)) in_file.close() out_file.close() toEx = os.path.realpath(out_file_name) bat_file.write( "C:\\Users\\Padraig\\AppData\\Local\\Programs\\POV-Ray\\v3.7\\bin\\pvengine.exe %s /nr /exit\n" % toEx) sh_file.write("povray %s %s\n" % (args.povrayOptions, toEx)) else: ind = maxind[0:len(maxind) - len(str(index))] #compute index indentation in_file = open(args.prefix + "_cells.inc") out_file_name = args.prefix + "_cells.inc" + ind + str(index) out_file = open(out_file_name, 'w') dummy_ref = 'CELL_GROUP_NAME_0' for line in in_file: if line.strip().startswith("//"): ref = line.strip()[2:] ref = ref.replace(dummy_ref, args.singlecell) if ref in volts.keys(): vs = volts[ref] out_file.write(" " + vs[index] + "\n//" + ref + " t= " + ind + str(t) + "\n") else: out_file.write("//No ref found: " + ref + ", was looking for " + dummy_ref + "\n") else: out_file.write(line) in_file.close() out_file.close() print_comment_v("Written file: %s for time: %f" % (out_file_name, t)) in_file = open(args.prefix + ".pov") out_file_name = "%s_T%s%i.pov" % (args.prefix, ind, index) out_file = open(out_file_name, 'w') for line in in_file: pre = '%s_cells.inc' % args.prefix post = '%s_cells.inc%s%i' % (args.prefix, ind, index) if line.find(pre) >= 0: out_file.write(line.replace(pre, post)) else: clock = args.rotations * (t - args.startTime) / ( args.endTime - args.startTime) out_file.write(line.replace("clock", str(clock))) print_comment_v("Written file: %s for time: %f" % (out_file_name, t)) in_file.close() out_file.close() toEx = os.path.realpath(out_file_name) bat_file.write( "C:\\Users\\Padraig\\AppData\\Local\\Programs\\POV-Ray\\v3.7\\bin\\pvengine.exe %s /nr /exit\n" % toEx) sh_file.write("povray %s %s\n" % (args.povrayOptions, toEx)) index = index + 1 print_comment_v("Done!: ") print_comment_v("\nTo generate images type:\n\n bash %s_pov.sh\n\n" % args.prefix)
import sys from pyneuroml import pynml example_lems_file = 'LEMS_NML2_Ex5_DetCell.xml' pynml.reload_saved_data(example_lems_file, plot=True, show_plot_already=False) example_lems_file = 'LEMS_NML2_Ex9_FN.xml' pynml.reload_saved_data(example_lems_file, plot=True, show_plot_already=False) if '-nogui' not in sys.argv: from matplotlib import pyplot as plt plt.show()