def prepare_sim_bundle(cls, sim): simstring = cPickle.dumps(sim) simmd5sum = StrUtils.get_hash_md5(simstring) simloc = LocMgr.get_simulation_tmp_dir() + simmd5sum[0:2] simloc = LocMgr.ensure_dir_exists(simloc) simfilename = Join(simloc, simmd5sum + cls.simsuffix) FileIO.write_to_file(txt=simstring, filename=simfilename) bundle = SimMetaDataBundle(sim) return bundle
def _write_to_file(self, bundlefilename=None): bundleloc = LocMgr.get_simulation_tmp_dir() bundlesuffix = '.bundle' if bundlefilename is None: bundle_dir = bundleloc + '/' + self.get_sim_md5sum()[0:2] + '/' bundle_dir = LocMgr.ensure_dir_exists(bundle_dir) bundle_fname = self.get_sim_md5sum() + bundlesuffix bundlefilename = os.path.join(bundle_dir, bundle_fname) FileIO.write_to_file(txt=cPickle.dumps(self), filename=bundlefilename) # print 'bundlefilename', bundlefilename return bundlefilename
def build_modfile(cls, modfile): output_filename = modfile.get_built_filename_full(ensure_built=False) if not os.path.exists(output_filename): LogMgr.info("Does not exist: building: %s" % output_filename) mod_txt_filename = FileIO.write_to_file(modfile.modtxt, suffix=".mod") ModFileCompiler.check_modfile_units(mod_txt_filename) mod_dyn_filename = _build_mod_file(mod_txt_filename, modfile=modfile) shutil.move(mod_dyn_filename, output_filename) else: LogMgr.info("Already Built") return output_filename
def build_modfile(cls, modfile): output_filename = modfile.get_built_filename_full(ensure_built=False) if not os.path.exists(output_filename): LogMgr.info('Does not exist: building: %s' % output_filename) mod_txt_filename = FileIO.write_to_file(modfile.modtxt, suffix='.mod') ModFileCompiler.check_modfile_units(mod_txt_filename) mod_dyn_filename = _build_mod_file(mod_txt_filename, modfile=modfile) shutil.move(mod_dyn_filename, output_filename) else: LogMgr.info('Already Built') return output_filename
def _run_no_spawn(self): # Generate Random data: if False or MockControl.is_mock_simulation: return self.run_return_random_walks() def nrn(func, *args, **kwargs): return_value = func(*args, **kwargs) if return_value != 1.0: raise ValueError('nrn Command Failed') stdout = cStringIO.StringIO () with redirect_stdout(stdout_stream=stdout, stderr_stream=stdout) as display_output: # Create the HOC and ModFiles: hoc_data = MHocFile() mod_files = MModFileSet() for sim_obj in self.simulation_objects: # print 'BUILDING HOC:', sim_obj sim_obj.build_hoc(hoc_data) # print 'BUILDING MOD:', sim_obj sim_obj.build_mod(mod_files) t_mod_build_start = time.time() mod_files.build_all() time_taken = time.time() - t_mod_build_start #print 'Time for Building Mod-Files: ', time_taken # Open Neuron: import neuron # Insert the mod-files: for modfile in mod_files: nrn(neuron.h.nrn_load_dll, modfile.get_built_filename_full()) # Write the HOC file: t_sim_start = time.time() hoc_filename = FileIO.write_to_file( str(hoc_data), suffix='.hoc') nrn(neuron.h.load_file, hoc_filename) self.hocfilename = hoc_filename class Event(object): def __init__(self): self.interval = 5.0 self.fih = neuron.h.FInitializeHandler(0.01, self.callback) def callback(self): #print display_output #display_output.stdout_prev.write('Simulating: t=%.0f/%.0fms \r' % (neuron.h.t, float(neuron.h.tstop))) #display_output.stdout_prev.flush() sys.__stdout__.write('Simulating: t=%.0f/%.0fms \r' % (neuron.h.t, float(neuron.h.tstop))) sys.__stdout__.flush() if neuron.h.t + self.interval < neuron.h.tstop: neuron.h.cvode.event(neuron.h.t + self.interval, self.callback) Event() print 'Running Simulation' neuron.h.run() assert neuron.h.t + 1 >= neuron.h.tstop print 'Time for Simulation: ', time.time() - t_sim_start # Extract the values back out: time_array = np.array(neuron.h.__getattribute__(NeuronSimulationConstants.TimeVectorName)) * NeuronSimulationConstants.TimeUnit t_trace_read_start = time.time() traces = [] records = hoc_data[MHocFileData.Recordables] for (record_obj, hoc_details) in records.iteritems(): data_array = np.array(neuron.h.__getattribute__(hoc_details["recVecName"])) * record_obj.get_unit() tr = TraceVariableDT(name=record_obj.name, comment=record_obj.get_description(), time=time_array, data=data_array, tags=record_obj.get_tags()) traces.append(tr) print 'Time for Extracting Data: (%d records)' % len(records), \ time.time() - t_trace_read_start self.result = SimulationResult(traces, self) return self.result
def _run_no_spawn(self): # Generate Random data: # MockControl.is_mock_simulation = True if False or MockControl.is_mock_simulation: return self.run_return_random_walks() def nrn(func, *args, **kwargs): return_value = func(*args, **kwargs) if return_value != 1.0: raise ValueError("nrn Command Failed") stdout = cStringIO.StringIO() with redirect_stdout(stdout_stream=stdout, stderr_stream=stdout) as display_output: # Create the HOC and ModFiles: hoc_data = MHocFile() mod_files = MModFileSet() for sim_obj in self.simulation_objects: # print 'BUILDING HOC:', sim_obj sim_obj.build_hoc(hoc_data) # print 'BUILDING MOD:', sim_obj sim_obj.build_mod(mod_files) t_mod_build_start = time.time() mod_files.build_all() time_taken = time.time() - t_mod_build_start # print 'Time for Building Mod-Files: ', time_taken # Open Neuron: import neuron # Insert the mod-files: for modfile in mod_files: nrn(neuron.h.nrn_load_dll, modfile.get_built_filename_full()) # Write the HOC file: t_sim_start = time.time() hoc_filename = FileIO.write_to_file(str(hoc_data), suffix=".hoc") print "Running Hoc File: %s" % hoc_filename nrn(neuron.h.load_file, hoc_filename) self.hocfilename = hoc_filename class UpdateEvent(object): def __init__(self): self.interval = 5.0 self.fih = neuron.h.FInitializeHandler(0.01, self.callback) self.starttime = datetime.datetime.now() def callback(self): # print display_output # display_output.stdout_prev.write('Simulating: t=%.0f/%.0fms \r' % (neuron.h.t, float(neuron.h.tstop))) # display_output.stdout_prev.flush() time_elapsed = (datetime.datetime.now() - self.starttime).seconds expected_time = None remaining_time = None if neuron.h.t > 5: expected_time = time_elapsed * (float(neuron.h.tstop) / neuron.h.t) remaining_time = int(expected_time - time_elapsed) sys.__stdout__.write( "Simulating: t=%.0f/%.0fms (Remaining:%s s) \r" % (neuron.h.t, float(neuron.h.tstop), remaining_time) ) sys.__stdout__.flush() if neuron.h.t + self.interval < neuron.h.tstop: neuron.h.cvode.event(neuron.h.t + self.interval, self.callback) UpdateEvent() print "Running Simulation" neuron.h.run() assert neuron.h.t + 1 >= neuron.h.tstop print "Time for Simulation: ", time.time() - t_sim_start # Extract the values back out: time_array = ( np.array(neuron.h.__getattribute__(NeuronSimulationConstants.TimeVectorName)) * NeuronSimulationConstants.TimeUnit ) t_trace_read_start = time.time() traces = [] records = hoc_data[MHocFileData.Recordables] for (record_obj, hoc_details) in records.iteritems(): data_array = np.array(neuron.h.__getattribute__(hoc_details["recVecName"])) * record_obj.get_unit() trace_type = TraceVariableDT if self.simsettings["cvode"] else TraceFixedDT tr = trace_type( name=record_obj.name, comment=record_obj.get_description(), time=time_array, data=data_array, tags=record_obj.get_tags(), ) # Simplify traces if self.simsettings["simplify_traces"]: print "CVODE:?", self.simsettings["cvode"] assert self.simsettings["cvode"] is False tr = tr.simplify(self.simsettings.simplify_traces) traces.append(tr) print "Time for Extracting Data: (%d records)" % len(records), time.time() - t_trace_read_start self.result = SimulationResult(traces=traces, evsets=[], simulation=self) self.do_result_post_processing() return self.result
def _build_modfile_local(mod_filename_short, modfile=None): print os.getcwd() mod_file_basename = mod_filename_short.replace('.mod', '') c_filename = mod_file_basename + '.c' la_filename = mod_file_basename + '.la' lo_filename = mod_file_basename + '.lo' so_filename = mod_file_basename + '.so' libs_dir = '.libs/' # Check for some existing files: #for gen_file in gen_files: # if os.path.exists(gen_file): # assert False, 'We never get here' # LocMgr.BackupDirectory(gen_file) c_filename = mod_file_basename + '.c' output = _simple_exec(ModBuilderParams.nocmodlpath, mod_filename_short) if not os.path.exists(c_filename): print 'Failed to compile modfile. Error:' print output, '\n' assert False # Add the extra registration function into our mod file: new_register_func = """\n modl_reg(){ _%s_reg(); }""" \ % mod_file_basename FileIO.append_to_file(new_register_func, c_filename) # Compile the .c file -> .so: compile_str = ModBuilderParams.get_compile_str(c_filename, lo_filename) link_str = ModBuilderParams.get_link_str(lo_filename, la_filename) if SettingsMgr.simulator_is_verbose(): print 'IN:', ModBuilderParams.libtoolpath, print compile_str print link_str compile_flags = modfile.additional_compile_flags if modfile else '' link_flags = modfile.additional_link_flags if modfile else '' op1 = _simple_exec( ModBuilderParams.libtoolpath, ModBuilderParams.get_compile_str( c_filename, lo_filename, additional_compile_flags=compile_flags)) op2 = _simple_exec( ModBuilderParams.libtoolpath, ModBuilderParams.get_link_str(lo_filename, la_filename, additional_link_flags=link_flags)) if SettingsMgr.simulator_is_verbose() or True: print 'OP1:', op1 print 'OP2:', op2 # Copy the correct .so from the libDir to the build_dir: shutil.move(os.path.join(libs_dir, mod_file_basename + '.so.0.0.0'), so_filename) # Clean up: if True: os.remove(c_filename) os.remove(mod_filename_short) for ext in ['.la', '.lo']: os.remove(mod_file_basename + ext) for ext in ['.la', '.lai', '.o', '.so', '.so.0']: os.remove(os.path.join(libs_dir, mod_file_basename + ext)) os.rmdir(libs_dir) return so_filename
def save_to_file(self, filename): res_string = pickle.dumps(self) return FileIO.write_to_file( res_string, filename=filename, filedirectory=LocMgr.get_simulation_tmp_dir())
def _to_file_multi(cls, filename, morphs, regionname_to_int_map=None): return FileIO.write_to_file(txt=cls._to_str_multi(morphs, regionname_to_int_map=regionname_to_int_map) , filename=filename)
def __call__(self, result, bundle): assert self.filename filename = self.filename resstring = cPickle.dumps(result) FileIO.write_to_file(txt=resstring, filename=filename)
def __call__(self, result, bundle): assert self.filename filename = self.filename resstring = cPickle.dumps(result) FileIO.write_to_file(txt=resstring, filename=filename) print "Size of results file: %.1f (MB)" % (os.path.getsize(filename)/1.e6)
def _build_modfile_local(mod_filename_short, modfile=None): print os.getcwd() mod_file_basename = mod_filename_short.replace(".mod", "") c_filename = mod_file_basename + ".c" la_filename = mod_file_basename + ".la" lo_filename = mod_file_basename + ".lo" so_filename = mod_file_basename + ".so" libs_dir = ".libs/" # Check for some existing files: # for gen_file in gen_files: # if os.path.exists(gen_file): # assert False, 'We never get here' # LocMgr.BackupDirectory(gen_file) c_filename = mod_file_basename + ".c" output = _simple_exec(ModBuilderParams.nocmodlpath, mod_filename_short) if not os.path.exists(c_filename): print "Failed to compile modfile. Error:" print output, "\n" assert False # Add the extra registration function into our mod file: new_register_func = """\n modl_reg(){ _%s_reg(); }""" % mod_file_basename FileIO.append_to_file(new_register_func, c_filename) # Compile the .c file -> .so: compile_str = ModBuilderParams.get_compile_str(c_filename, lo_filename) link_str = ModBuilderParams.get_link_str(lo_filename, la_filename) if SettingsMgr.simulator_is_verbose(): print "IN:", ModBuilderParams.libtoolpath, print compile_str print link_str compile_flags = modfile.additional_compile_flags if modfile else "" link_flags = modfile.additional_link_flags if modfile else "" op1 = _simple_exec( ModBuilderParams.libtoolpath, ModBuilderParams.get_compile_str(c_filename, lo_filename, additional_compile_flags=compile_flags), ) op2 = _simple_exec( ModBuilderParams.libtoolpath, ModBuilderParams.get_link_str(lo_filename, la_filename, additional_link_flags=link_flags), ) if SettingsMgr.simulator_is_verbose() or True: print "OP1:", op1 print "OP2:", op2 # Copy the correct .so from the libDir to the build_dir: shutil.move(os.path.join(libs_dir, mod_file_basename + ".so.0.0.0"), so_filename) # Clean up: if True: os.remove(c_filename) os.remove(mod_filename_short) for ext in [".la", ".lo"]: os.remove(mod_file_basename + ext) for ext in [".la", ".lai", ".o", ".so", ".so.0"]: os.remove(os.path.join(libs_dir, mod_file_basename + ext)) os.rmdir(libs_dir) return so_filename
def _build_modfile_local(mod_filename_short, modfile=None): print os.getcwd() mod_file_basename = mod_filename_short.replace('.mod', '') c_filename = mod_file_basename + '.c' la_filename = mod_file_basename + '.la' lo_filename = mod_file_basename + '.lo' so_filename = mod_file_basename + '.so' libs_dir = '.libs/' c_filename = mod_file_basename + '.c' output = _simple_exec(ModBuilderParams.nocmodlpath, mod_filename_short, err_ok=True) if not os.path.exists(c_filename): print 'Failed to compile modfile. Error:' print output, '\n' assert False # Add the extra registration function into our mod file: new_register_func = """\n modl_reg(){ _%s_reg(); }""" \ % mod_file_basename FileIO.append_to_file(new_register_func, c_filename) # Compile the .c file -> .so: compile_str = ModBuilderParams.get_compile_str(c_filename, lo_filename) link_str = ModBuilderParams.get_link_str(lo_filename, la_filename) compile_flags = modfile.additional_compile_flags if modfile else '' link_flags = modfile.additional_link_flags if modfile else '' if SettingsMgr.simulator_is_verbose(): print 'IN:', ModBuilderParams.libtoolpath, print compile_str print link_str op1 = _simple_exec(ModBuilderParams.libtoolpath, ModBuilderParams.get_compile_str(c_filename, lo_filename, additional_compile_flags=compile_flags)) op2 = _simple_exec(ModBuilderParams.libtoolpath, ModBuilderParams.get_link_str(lo_filename, la_filename, additional_link_flags=link_flags)) for filename in [c_filename, lo_filename, la_filename]: if not os.path.exists(filename): assert False, 'Error building mod-file!' if SettingsMgr.simulator_is_verbose() or True: print 'OP1:', op1 print 'OP2:', op2 # Copy the correct .so from the libDir to the build_dir: shutil.move( os.path.join(libs_dir, mod_file_basename + '.so.0.0.0'), so_filename) # Clean up: if True: os.remove(c_filename) os.remove(mod_filename_short) for ext in ['.la', '.lo']: os.remove(mod_file_basename + ext) for ext in ['.la', '.lai', '.o', '.so', '.so.0']: os.remove(os.path.join(libs_dir, mod_file_basename + ext)) os.rmdir(libs_dir) return so_filename
def _to_file_multi(cls, filename, morphs, regionname_to_int_map=None): return FileIO.write_to_file(txt=cls._to_str_multi( morphs, regionname_to_int_map=regionname_to_int_map), filename=filename)
def _run_no_spawn(self): # Generate Random data: if False or MockControl.is_mock_simulation: return self.run_return_random_walks() def nrn(func, *args, **kwargs): return_value = func(*args, **kwargs) if return_value != 1.0: raise ValueError('nrn Command Failed') stdout = cStringIO.StringIO() with redirect_stdout(stdout_stream=stdout, stderr_stream=stdout) as display_output: # Create the HOC and ModFiles: hoc_data = MHocFile() mod_files = MModFileSet() for sim_obj in self.simulation_objects: # print 'BUILDING HOC:', sim_obj sim_obj.build_hoc(hoc_data) # print 'BUILDING MOD:', sim_obj sim_obj.build_mod(mod_files) t_mod_build_start = time.time() mod_files.build_all() time_taken = time.time() - t_mod_build_start #print 'Time for Building Mod-Files: ', time_taken # Open Neuron: import neuron # Insert the mod-files: for modfile in mod_files: nrn(neuron.h.nrn_load_dll, modfile.get_built_filename_full()) # Write the HOC file: t_sim_start = time.time() hoc_filename = FileIO.write_to_file(str(hoc_data), suffix='.hoc') nrn(neuron.h.load_file, hoc_filename) self.hocfilename = hoc_filename class Event(object): def __init__(self): self.interval = 5.0 self.fih = neuron.h.FInitializeHandler(0.01, self.callback) def callback(self): #print display_output #display_output.stdout_prev.write('Simulating: t=%.0f/%.0fms \r' % (neuron.h.t, float(neuron.h.tstop))) #display_output.stdout_prev.flush() sys.__stdout__.write('Simulating: t=%.0f/%.0fms \r' % (neuron.h.t, float(neuron.h.tstop))) sys.__stdout__.flush() if neuron.h.t + self.interval < neuron.h.tstop: neuron.h.cvode.event(neuron.h.t + self.interval, self.callback) Event() print 'Running Simulation' neuron.h.run() assert neuron.h.t + 1 >= neuron.h.tstop print 'Time for Simulation: ', time.time() - t_sim_start # Extract the values back out: time_array = np.array( neuron.h.__getattribute__(NeuronSimulationConstants.TimeVectorName) ) * NeuronSimulationConstants.TimeUnit t_trace_read_start = time.time() traces = [] records = hoc_data[MHocFileData.Recordables] for (record_obj, hoc_details) in records.iteritems(): data_array = np.array( neuron.h.__getattribute__( hoc_details["recVecName"])) * record_obj.get_unit() tr = TraceVariableDT(name=record_obj.name, comment=record_obj.get_description(), time=time_array, data=data_array, tags=record_obj.get_tags()) traces.append(tr) print 'Time for Extracting Data: (%d records)' % len(records), \ time.time() - t_trace_read_start self.result = SimulationResult(traces, self) return self.result