def test_streamer(): compt = moose.CubeMesh('/compt') assert compt r = moose.Reac('/compt/r') a = moose.Pool('/compt/a') a.concInit = 1 b = moose.Pool('/compt/b') b.concInit = 2 c = moose.Pool('/compt/c') c.concInit = 0.5 moose.connect(r, 'sub', a, 'reac') moose.connect(r, 'prd', b, 'reac') moose.connect(r, 'prd', c, 'reac') r.Kf = 0.1 r.Kb = 0.01 outfile = 'streamer_test.csv' if os.path.exists(outfile): os.remove(outfile) tabA = moose.Table2('/compt/a/tab') tabB = moose.Table2('/compt/tabB') tabC = moose.Table2('/compt/tabB/tabC') print(tabA, tabB, tabC) moose.connect(tabA, 'requestOut', a, 'getConc') moose.connect(tabB, 'requestOut', b, 'getConc') moose.connect(tabC, 'requestOut', c, 'getConc') # Now create a streamer and use it to write to a stream st = moose.Streamer('/compt/streamer') st.outfile = outfile print("outfile set to: %s " % st.outfile) st.addTable(tabA) st.addTables([tabB, tabC]) assert st.numTables == 3 moose.reinit() t = 100 print('[INFO] Running for %d seconds' % t) moose.start(t) outfile = st.outfile moose.quit() # Otherwise Streamer won't flush the rest of entries. print('Moose is done. Waiting for monitor to shut down...') # Now read the table and verify that we have written print('[INFO] Reading file %s' % outfile) if 'csv' in outfile: data = np.loadtxt(outfile, skiprows=1) else: data = np.load(outfile) # Total rows should be 58 (counting zero as well). # print(data) # print( data.dtype ) assert data.shape >= (101, ), data.shape print('[INFO] Test 2 passed') return 0
def test_other(): a1 = moose.Pool('/ada') assert a1.className == 'Pool', a1.className finfo = moose.getFieldDict(a1.className) s = moose.Streamer('/asdada') p = moose.PulseGen('pg1') assert p.delay[0] == 0.0 p.delay[1] = 0.99 assert p.delay[1] == 0.99, p.delay[1]
def test( ): compt = moose.CubeMesh( '/compt' ) r = moose.Reac( '/compt/r' ) a = moose.Pool( '/compt/a' ) a.concInit = 1 b = moose.Pool( '/compt/b' ) b.concInit = 2 c = moose.Pool( '/compt/c' ) c.concInit = 0.5 moose.connect( r, 'sub', a, 'reac' ) moose.connect( r, 'prd', b, 'reac' ) moose.connect( r, 'prd', c, 'reac' ) r.Kf = 0.1 r.Kb = 0.01 tabA = moose.Table2( '/compt/a/tab' ) tabB = moose.Table2( '/compt/tabB' ) tabC = moose.Table2( '/compt/tabB/tabC' ) print(tabA, tabB, tabC) moose.connect( tabA, 'requestOut', a, 'getConc' ) moose.connect( tabB, 'requestOut', b, 'getConc' ) moose.connect( tabC, 'requestOut', c, 'getConc' ) # Now create a streamer and use it to write to a stream st = moose.Streamer( '/compt/streamer' ) st.outfile = os.path.join( os.getcwd(), 'temp.npy' ) print(("outfile set to: %s " % st.outfile )) assert st.outfile == os.path.join( os.getcwd(), 'temp.npy' ), st.outfile st.addTable( tabA ) st.addTables( [ tabB, tabC ] ) assert st.numTables == 3 moose.reinit( ) print( '[INFO] Running for 57 seconds' ) moose.start( 57 ) outfile = st.outfile moose.quit() # Otherwise Streamer won't flush the rest of entries. # Now read the table and verify that we have written print( '[INFO] Reading file %s' % outfile ) if 'csv' in outfile: data = np.loadtxt(outfile, skiprows=1 ) else: data = np.load( outfile ) # Total rows should be 58 (counting zero as well). # print(data) # print( data.dtype ) time = data['time'] print( time ) assert data.shape >= (58,), data.shape print( '[INFO] Test 2 passed' ) return 0
def buildLargeSystem(useStreamer=False): # create a huge system. if moose.exists('/comptB'): moose.delete('/comptB') moose.CubeMesh('/comptB') tables = [] for i in range(300): r = moose.Reac('/comptB/r%d' % i) a = moose.Pool('/comptB/a%d' % i) a.concInit = 10.0 b = moose.Pool('/comptB/b%d' % i) b.concInit = 2.0 c = moose.Pool('/comptB/c%d' % i) c.concInit = 0.5 moose.connect(r, 'sub', a, 'reac') moose.connect(r, 'prd', b, 'reac') moose.connect(r, 'prd', c, 'reac') r.Kf = 0.1 r.Kb = 0.01 # Make table name large enough such that the header is larger than 2^16 # . Numpy version 1 can't handle such a large header. If format 1 is # then this test will fail. t = moose.Table2('/comptB/TableO1%d' % i + 'abc' * 100) moose.connect(t, 'requestOut', a, 'getConc') tables.append(t) if useStreamer: s = moose.Streamer('/comptB/streamer') s.datafile = 'data2.npy' print("[INFO ] Total tables %d" % len(tables)) # Add tables using wilcardFind. s.addTables(moose.wildcardFind('/comptB/##[TYPE=Table2]')) print("Streamer has %d table" % s.numTables) assert s.numTables == len(tables), (s.numTables, len(tables)) moose.reinit() moose.start(10) if useStreamer: # load the data data = np.load(s.datafile) header = str(data.dtype.names) assert len(header) > 2**16 else: data = {x.columnName: x.vector for x in tables} return data
def test_other(): a1 = moose.Pool('/ada') assert a1.className == 'Pool', a1.className finfo = moose.getFieldDict(a1.className) s = moose.Streamer('/asdada') p = moose.PulseGen('pg1') assert p.delay[0] == 0.0 p.delay[1] = 0.99 assert p.delay[1] == 0.99, p.delay[1] c = moose.Stoich('/dadaa') v1 = moose.getFieldNames(c) v2 = c.getFieldNames() assert v1 == v2 assert len(v1) > 10, v1
def test_sanity(): a = moose.Table('/t1') b = moose.Table('/t1/t1') c = moose.Table('/t1/t1/t1') print(a) print(b) print(c) st = moose.Streamer('/s') st.outfile = 'a.txt' assert st.outfile == 'a.txt' st.addTable(a) assert (st.numTables == 1) st.addTable(b) assert (st.numTables == 2) st.addTable(c) assert (st.numTables == 3) st.addTable(c) assert (st.numTables == 3) st.addTable(c) assert (st.numTables == 3) st.removeTable(c) assert (st.numTables == 2) st.removeTable(c) assert (st.numTables == 2) st.removeTable(a) assert (st.numTables == 1) st.removeTable(b) assert (st.numTables == 0) st.removeTable(b) assert (st.numTables == 0) print('Sanity test passed') st.addTables([a, b, c]) assert st.numTables == 3 st.removeTables([a, a, c]) assert st.numTables == 1
def buildSystem(outfile): if moose.exists('/compt'): moose.delete('/compt') compt = moose.CubeMesh('/compt') assert compt r = moose.Reac('/compt/r') a = moose.Pool('/compt/a') a.concInit = 1 b = moose.Pool('/compt/b') b.concInit = 2 c = moose.Pool('/compt/c') c.concInit = 0.5 moose.connect(r, 'sub', a, 'reac') moose.connect(r, 'prd', b, 'reac') moose.connect(r, 'prd', c, 'reac') r.Kf = 0.1 r.Kb = 0.01 tabA = moose.Table2('/compt/a/tab') tabB = moose.Table2('/compt/tabB') tabC = moose.Table2('/compt/tabB/tabC') print(tabA, tabB, tabC) moose.connect(tabA, 'requestOut', a, 'getConc') moose.connect(tabB, 'requestOut', b, 'getConc') moose.connect(tabC, 'requestOut', c, 'getConc') # Now create a streamer and use it to write to a stream st = moose.Streamer('/compt/streamer') st.outfile = outfile print("outfile set to: %s " % st.outfile) st.addTable(tabA) st.addTables([tabB, tabC]) assert st.numTables == 3 return st
This class takes over moose.Table and make sure that their vector is written to stream. Once a chunk of data is written, it is removed from vector. This way, moose never runs out of memory during simulation. """ __author__ = "Dilawar Singh" __copyright__ = "Copyright 2016, Dilawar Singh" __credits__ = ["NCBS Bangalore"] __license__ = "GNU GPL" __version__ = "1.0.0" __maintainer__ = "Dilawar Singh" __email__ = "*****@*****.**" __status__ = "Development" import sys import moose print('[INFO] Using moose from %s' % moose.__file__) t1 = moose.Table('/t1') t2 = moose.Table('/t1/t1') a = moose.Streamer('/a') assert (a.streamname == 'stdout'), 'default is stdout, got %s' % a.streamname # Add another table a.addTable(t1) a.addTable(t2) a.removeTable(t1)
def moose_main(corticalinput): import numpy as np import matplotlib.pyplot as plt # plt.ion() import moose from moose_nerp.prototypes import (create_model_sim, clocks, inject_func, create_network, tables, net_output) from moose_nerp import d1opt as model from moose_nerp import str_net as net # additional, optional parameter overrides specified from with python terminal # model.Condset.D1.NaF[model.param_cond.prox] /= 3 # model.Condset.D1.KaS[model.param_cond.prox] *= 3 net.connect_dict['D1']['ampa']['extern1'].dend_loc.postsyn_fraction = 0.8 net.param_net.tt_Ctx_SPN.filename = corticalinput print('cortical_fraction = {}'.format(net.connect_dict['D1']['ampa']['extern1'].dend_loc.postsyn_fraction)) model.synYN = True model.plasYN = True model.calYN = True model.spineYN = True net.single = True create_model_sim.setupOptions(model) param_sim = model.param_sim param_sim.useStreamer = True param_sim.plotdt = .1e-3 param_sim.stim_loc = model.NAME_SOMA param_sim.stim_paradigm = 'inject' param_sim.injection_current = [0] # [-0.2e-9, 0.26e-9] param_sim.injection_delay = 0.2 param_sim.injection_width = 0.4 param_sim.simtime = 21 net.num_inject = 0 net.confile = 'str_connect_plas_simd1opt_{}_corticalfraction_{}'.format(net.param_net.tt_Ctx_SPN.filename, 0.8) if net.num_inject == 0: param_sim.injection_current = [0] #################################-----------create the model: neurons, and synaptic inputs model = create_model_sim.setupNeurons(model, network=not net.single) all_neur_types = model.neurons # FSIsyn,neuron = cell_proto.neuronclasses(FSI) # all_neur_types.update(neuron) population, connections, plas = create_network.create_network(model, net, all_neur_types) ###### Set up stimulation - could be current injection or plasticity protocol # set num_inject=0 to avoid current injection if net.num_inject < np.inf: inject_pop = inject_func.inject_pop(population['pop'], net.num_inject) else: inject_pop = population['pop'] # Does setupStim work for network? # create_model_sim.setupStim(model) pg = inject_func.setupinj(model, param_sim.injection_delay, param_sim.injection_width, inject_pop) moose.showmsg(pg) ##############--------------output elements if net.single: # fname=model.param_stim.Stimulation.Paradigm.name+'_'+model.param_stim.location.stim_dendrites[0]+'.npz' # simpath used to set-up simulation dt and hsolver simpath = ['/' + neurotype for neurotype in all_neur_types] create_model_sim.setupOutput(model) else: # population of neurons spiketab, vmtab, plastab, catab = net_output.SpikeTables(model, population['pop'], net.plot_netvm, plas, net.plots_per_neur) # simpath used to set-up simulation dt and hsolver simpath = [net.netname] clocks.assign_clocks(simpath, param_sim.simdt, param_sim.plotdt, param_sim.hsolve, model.param_cond.NAME_SOMA) if model.synYN and (param_sim.plot_synapse or net.single): # overwrite plastab above, since it is empty syntab, plastab, stp_tab = tables.syn_plastabs(connections, model) nonstim_plastab = tables.nonstimplastabs(plas) # Streamer to prevent Tables filling up memory on disk # This is a hack, should be better implemented if param_sim.useStreamer == True: allTables = moose.wildcardFind('/##[ISA=Table]') streamer = moose.Streamer('/streamer') streamer.outfile = 'plas_simd1opt_{}_corticalfraction_{}.npy'.format(net.param_net.tt_Ctx_SPN.filename, 0.8) moose.setClock(streamer.tick, 0.1) for t in allTables: if any(s in t.path for s in ['plas', 'VmD1_0', 'extern', 'Shell_0']): streamer.addTable(t) else: t.tick = -2 ################### Actually run the simulation def run_simulation(injection_current, simtime): print(u'◢◤◢◤◢◤◢◤ injection_current = {} ◢◤◢◤◢◤◢◤'.format(injection_current)) pg.firstLevel = injection_current moose.reinit() moose.start(simtime, True) traces, names = [], [] for inj in param_sim.injection_current: run_simulation(injection_current=inj, simtime=param_sim.simtime) weights = [w.value for w in moose.wildcardFind('/##/plas##[TYPE=Function]')] plt.figure() plt.hist(weights, bins=100) plt.title('plas_sim_{}_corticalfraction_{}'.format(net.param_net.tt_Ctx_SPN.filename, cortical_fraction)) plt.savefig('plas_simd1opt_{}_corticalfraction_{}.png'.format(net.param_net.tt_Ctx_SPN.filename, 0.8)) if param_sim.useStreamer == True: import atexit atexit.register(moose.quit) return weights
def main( compt_name, **kwargs ): global args global model_path_ global curr_subsec_ global compt_ moose.Neutral( model_path_ ) args = kwargs # NOTE: Try to keep volumne of all voxels close to 3.1416e-22m^3. In these # experiments, the length of compartment is set to 180nM and volumne math # Πe-22 m^3. voxels = [ ] nVoxels = int(args['num_voxels']) voxelL = args['voxel_length'] assert nVoxels > 0, nVoxels assert voxelL > 0, voxelL radius = 23.56e-9 args[ 'voxel_volume' ] = math.pi * radius * radius * voxelL args[ 'camkii_conc' ] = n_to_conc( args[ 'camkii' ] / nVoxels ) args[ 'pp1_conc' ] = n_to_conc( args[ 'pp1' ] / nVoxels ) if isinstance( args['diff_dict'], str ): args[ 'diff_dict' ] = eval( args[ 'diff_dict' ] ) _logger.info( 'Diffusion dict %s' % args[ 'diff_dict' ] ) compt_ = moose.CylMesh( '%s/%s' % (model_path_, compt_name ) ) compt_.x1 = nVoxels * voxelL compt_.r0 = compt_.r1 = radius _logger.info( '++ Volume of compt %g' % compt_.volume ) for i in range( args[ 'num_voxels' ] ): curr_subsec_ = 'sw%d' % i voxel = moose.Neutral( '%s/%s' % ( compt_.path, curr_subsec_ ) ) _logger.info( "== Creating voxles %d" % i ) _logger.debug( '=== Created subsystem inside %s' % voxel.name ) make_model( voxel, **kwargs ) voxels.append( voxel ) if args[ 'enable_subunit_exchange' ]: for x in args[ 'diff_dict' ].keys( ): _logger.info( "Setting up diffusion for %s" % x ) try: setup_diffusion( voxels, x ) except Exception as e: _logger.warn( 'Could not enable diffusion for %s' % x ) _logger.warn( '\tError was %s' % e ) setup_solvers( compt_, stochastic = True ) print_compt( compt_ ) # Now add solvers and other goodies in compartment. moose.setClock(18, args['record_dt'] ) # Table2 tables = setupTables( ) st = moose.Streamer( '/model/streamer' ) st.outfile = args['outfile'] _logger.info( 'Added streamer with outfile %s' % st.outfile ) st.addTables( tables ) t1 = time.time() stamp = datetime.datetime.now().isoformat() # moose.setClock( 10, 0.003 ) # moose.setClock( 15, 0.03 ) # moose.setClock( 16, 0.03 ) moose.reinit() print( '== Gainers' ) print( sorted( gainers_ ) ) print( '== Loosers' ) print( sorted( loosers_ ) ) print( '== Dephosphorylated' ) print( sorted( dephospho_ ) ) # sanity tests verify( ) runtime = 24 * 3600 * float( args[ 'simtime' ] ) _logger.info('Running for %d seconds' % runtime ) t = time.time( ) #compartment_to_graph( '' ) moose.start( runtime, 1 ) print( '[INFO] Time taken %f' % (time.time() - t ) ) # append parameters to streamer file. with open( st.outfile, 'a' ) as f: f.write( "# %s" % str( args ) ) print( '[INFO] Appended simulation parameters to file' ) _logger.info( 'Total time taken %s' % (time.time() - t1 ) )
else: # population of neurons spiketab, vmtab, plastab, catab = net_output.SpikeTables(model, population['pop'], net.plot_netvm, plas, net.plots_per_neur) # simpath used to set-up simulation dt and hsolver simpath = [net.netname] clocks.assign_clocks(simpath, param_sim.simdt, param_sim.plotdt, param_sim.hsolve, model.param_cond.NAME_SOMA) if model.synYN and (param_sim.plot_synapse or net.single): # overwrite plastab above, since it is empty syntab, plastab, stp_tab = tables.syn_plastabs(connections, model) nonstim_plastab = tables.nonstimplastabs(plas) # Streamer to prevent Tables filling up memory on disk # This is a hack, should be better implemented if param_sim.useStreamer == True: allTables = moose.wildcardFind('/##[ISA=Table]') streamer = moose.Streamer('/streamer') streamer.outfile = 'plas_sim_{}.npy'.format(net.param_net.tt_Ctx_SPN.filename) moose.setClock(streamer.tick, 0.1) for t in allTables: if any(s in t.path for s in ['plas', 'VmD1_0', 'extern', 'Shell_0']): streamer.addTable(t) else: t.tick = -2 spinedistdict = {} for sp in moose.wildcardFind('D1/##/#head#[ISA=CompartmentBase]'): dist, _ = util.get_dist_name(sp) path = sp.path spinedistdict[path] = dist
def main(compt_name, **kwargs): global args global model_path_ global curr_subsec_ global compt_ moose.Neutral(model_path_) args = kwargs voxels = [] l = args['voxel_length'] radius = 30e-9 args['volume'] = math.pi * radius * radius * l args['camkii_conc'] = n_to_conc(args['camkii']) args['pp1_conc'] = n_to_conc(args['pp1']) args['psd_volume'] = 0.0 if isinstance(args['diff_dict'], str): args['diff_dict'] = eval(args['diff_dict']) _logger.info('Diffusion dict %s' % args['diff_dict']) compt_ = moose.CylMesh('%s/%s' % (model_path_, compt_name)) compt_.x1 = l compt_.r0 = compt_.r1 = radius _logger.info('Volume of compt %g' % compt_.volume) for i in range(args['num_voxels']): curr_subsec_ = 'sw%d' % i voxel = moose.Neutral('%s/%s' % (compt_.path, curr_subsec_)) _logger.info("Creating voxles %d" % i) _logger.debug('==== Created subsystem inside %s' % voxel.name) make_model(voxel, **kwargs) voxels.append(voxel) for x in ['x']: _logger.info("Setting up diffusion for %s" % x) setup_diffusion(voxels, x) setup_solvers(compt_, stochastic=True) print_compt(compt_) # Now add solvers and other goodies in compartment. moose.setClock(18, args['record_dt']) # Table2 tables = setupTables() st = moose.Streamer('/model/streamer') st.outfile = args['outfile'] _logger.info('Added streamer with outfile %s' % st.outfile) st.addTables(tables) t1 = time.time() stamp = datetime.datetime.now().isoformat() # moose.setClock( 10, 0.003 ) # moose.setClock( 15, 0.03 ) # moose.setClock( 16, 0.03 ) moose.reinit() print('== Gainers') print(sorted(gainers_)) print('== Loosers') print(sorted(loosers_)) print('== Dephosphorylated') print(sorted(dephospho_)) # sanity tests verify() runtime = 24 * 3600 * float(args['simtime']) _logger.info('Running for %d seconds' % runtime) moose.start(runtime, 1) # append parameters to streamer file. with open(st.outfile, 'a') as f: f.write("# %s" % str(args)) print('[INFO] Appended simulation parameters to file') _logger.info('Total time taken %s' % (time.time() - t1))