def neuron_instance(neuron_import): """Sets/Resets the rxd test environment. Provides 'data', a dictionary used to store voltages and rxd node values for comparisons with the 'correct_data'. """ h, rxd, save_path = neuron_import data = {'record_count': 0, 'data': []} h.load_file('stdrun.hoc') h.nrnunit_use_legacy(True) # pytest fixtures at the function scope that require neuron_instance will go # out of scope after neuron_instance. So species, sections, etc. will go # out of scope after neuron_instance is torn down. # Here we assert that no section left alive. If the assertion fails it is # due to a problem with the previous test, not the test which failed. gc.collect() for sec in h.allsec(): assert (False) cvode = h.CVode() cvode.active(False) cvode.atol(1e-3) h.dt = 0.025 h.stoprun = False def gather(): return collect_data(h, rxd, data, save_path) cvode.extra_scatter_gather(0, gather) yield (h, rxd, data) for r in rxd.rxd._all_reactions[:]: if r(): rxd.rxd._unregister_reaction(r) for s in rxd.species._all_species: if s(): s().__del__() rxd.region._all_regions = [] rxd.region._region_count = 0 rxd.region._c_region_lookup = None rxd.species._species_counts = 0 rxd.section1d._purge_cptrs() rxd.initializer.has_initialized = False rxd.rxd.free_conc_ptrs() rxd.rxd.free_curr_ptrs() rxd.rxd.rxd_include_node_flux1D(0, None, None, None) rxd.species._has_1d = False rxd.species._has_3d = False rxd.rxd._zero_volume_indices = numpy.ndarray(0, dtype=numpy.int_) rxd.set_solve_type(dimension=1) cvode.extra_scatter_gather_remove(gather)
def neuron_nosave_instance(neuron_import): """Sets/Resets the rxd test environment.""" h, rxd, save_path = neuron_import h.load_file("stdrun.hoc") h.load_file("import3d.hoc") h.nrnunit_use_legacy(True) # pytest fixtures at the function scope that require neuron_instance will go # out of scope after neuron_instance. So species, sections, etc. will go # out of scope after neuron_instance is torn down. # Here we assert that no section left alive. If the assertion fails it is # due to a problem with the previous test, not the test which failed. gc.collect() for sec in h.allsec(): assert False cvode = h.CVode() cvode.active(False) cvode.atol(1e-3) h.dt = 0.025 h.stoprun = False yield (h, rxd, save_path) for r in rxd.rxd._all_reactions[:]: if r(): rxd.rxd._unregister_reaction(r) for s in rxd.species._all_species: if s(): s().__del__() rxd.region._all_regions = [] rxd.region._region_count = 0 rxd.region._c_region_lookup = None rxd.species._species_counts = 0 rxd.section1d._purge_cptrs() rxd.initializer.has_initialized = False rxd.rxd.free_conc_ptrs() rxd.rxd.free_curr_ptrs() rxd.rxd.rxd_include_node_flux1D(0, None, None, None) rxd.species._has_1d = False rxd.species._has_3d = False rxd.rxd._zero_volume_indices = numpy.ndarray(0, dtype=numpy.int_) rxd.set_solve_type(dimension=1)
def do_test(test_to_run, results_location, num_record=10): import os # switch to the directory initial_path = os.getcwd() + '/' the_dir, the_file = os.path.split(test_to_run) os.chdir(the_dir) from neuron import h import itertools h.nrnunit_use_legacy(True) data = {'record_count': 0, 'data': []} do_test.data = data record_count = 0 def collect_data(): """grabs the membrane potential data, h.t, and the rxd state values""" from neuron import rxd data['record_count'] += 1 if data['record_count'] > num_record: save_and_cleanup() all_potentials = [ seg.v for seg in itertools.chain.from_iterable(h.allsec()) ] rxd_1d = list(rxd.node._states) rxd_3d = [] rxd_ecs = [] for sp in rxd.species._all_species: s = sp() if s and hasattr(s, '_intracellular_instances'): for key, ics in sorted(s._intracellular_instances.items(), key=lambda val: val[0]._id): rxd_3d += list(ics.states) if s and hasattr(s, '_extracellular_instances'): for ecs in sorted(s._extracellular_instances.values(), key=lambda spe: spe._grid_id): rxd_ecs += list(ecs.states.flatten()) all_rxd = rxd_1d + rxd_3d + rxd_ecs local_data = [h.t] + all_potentials + all_rxd # remove data before t=0 if h.t == 0: data['data'] = [] data['record_count'] = 1 # remove previous record if h.t is the same if data['record_count'] > 1 and h.t == data['data'][-len(local_data)]: data['record_count'] -= 1 del data['data'][-len(local_data):] # add new data record data['data'].extend(local_data) # print correct record length if data['record_count'] == 2: outstr = "<BAS_RL %i BAS_RL> %s %s" % (len(local_data), repr( h.t), data['record_count']) print(outstr) def save_and_cleanup(): import array os.chdir(initial_path) # save the data with open(results_location, 'wb') as f: array.array('d', data['data']).tofile(f) import sys sys.exit(0) h.CVode().extra_scatter_gather(0, collect_data) # now run it exec_test(the_file, globals(), globals()) # should only get here if very few steps save_and_cleanup()
"""Neuron simulation functions and NetworkBuilder class.""" # Authors: Mainak Jas <*****@*****.**> # Sam Neymotin <*****@*****.**> # Blake Caldwell <*****@*****.**> from copy import deepcopy import numpy as np from neuron import h # This is due to: https://github.com/neuronsimulator/nrn/pull/746 from neuron import __version__ if int(__version__[0]) >= 8: h.nrnunit_use_legacy(1) from .cell import _ArtificialCell from .params import _long_name, _short_name from .extracellular import _ExtracellularArrayBuilder from .network import pick_connection # a few globals _PC = None _CVODE = None # We need to maintain a reference to the last # NetworkBuilder instance that ran pc.gid_clear(). Even if # pc is global, if pc.gid_clear() is called within a new # NetworkBuilder, it will seg fault. _LAST_NETWORK = None
def switch_units(legacy): try: h.nrnunit_use_legacy(legacy) except: pass