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 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 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 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 __call__(self, result, bundle): assert self.filename filename = self.filename resstring = cPickle.dumps(result) FileIO.write_to_file(txt=resstring, filename=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
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 _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 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())