| 
 | 1 | +# test_nsdf.py ---   | 
 | 2 | +# Changed from nsdf.py from  | 
 | 3 | +# https://github.com/BhallaLab/moose-examples/snippets/nsdf.py  | 
 | 4 | + | 
 | 5 | +from __future__ import print_function  | 
 | 6 | +import numpy as np  | 
 | 7 | +from datetime import datetime  | 
 | 8 | +import getpass  | 
 | 9 | +import os  | 
 | 10 | + | 
 | 11 | +# Use in-repo moose to install.  | 
 | 12 | +import moose  | 
 | 13 | +print('using moose from: %s' % moose.__file__)  | 
 | 14 | + | 
 | 15 | +global nsdf  | 
 | 16 | + | 
 | 17 | +def setup_model():  | 
 | 18 | +    """Setup a dummy model with a PulseGen and a SpikeGen. The SpikeGen  | 
 | 19 | +    detects the leading edges of the pulses created by the PulseGen  | 
 | 20 | +    and sends out the event times. We record the PulseGen outputValue  | 
 | 21 | +    as Uniform data and leading edge time as Event data in the NSDF  | 
 | 22 | +    file.  | 
 | 23 | +
  | 
 | 24 | +    """  | 
 | 25 | +    global nsdf  | 
 | 26 | +    simtime = 100.0  | 
 | 27 | +    dt = 1e-3  | 
 | 28 | +    model = moose.Neutral('/model')  | 
 | 29 | +    pulse = moose.PulseGen('/model/pulse')  | 
 | 30 | +    pulse.level[0] = 1.0  | 
 | 31 | +    pulse.delay[0] = 10  | 
 | 32 | +    pulse.width[0] = 20  | 
 | 33 | +    t_lead = moose.SpikeGen('/model/t_lead')  | 
 | 34 | +    t_lead.threshold = 0.5  | 
 | 35 | +    moose.connect(pulse, 'output', t_lead,'Vm');  | 
 | 36 | +    nsdf = moose.NSDFWriter('/model/writer')  | 
 | 37 | +    nsdf.filename = 'nsdf_demo.h5'  | 
 | 38 | +    nsdf.mode = 2 #overwrite existing file  | 
 | 39 | +    nsdf.flushLimit = 100  | 
 | 40 | +    moose.connect(nsdf, 'requestOut', pulse, 'getOutputValue')  | 
 | 41 | +    print('event input', nsdf.eventInput, nsdf.eventInput.num)  | 
 | 42 | +    print(nsdf)  | 
 | 43 | + | 
 | 44 | +    nsdf.eventInput.num = 1  | 
 | 45 | +    ei = nsdf.eventInput[0]  | 
 | 46 | +    print(ei.path)  | 
 | 47 | +    moose.connect(t_lead, 'spikeOut', nsdf.eventInput[0], 'input')  | 
 | 48 | +    tab = moose.Table('spiketab')  | 
 | 49 | +    tab.threshold = t_lead.threshold  | 
 | 50 | +    clock = moose.element('/clock')  | 
 | 51 | +    for ii in range(32):  | 
 | 52 | +        moose.setClock(ii, dt)  | 
 | 53 | +    moose.connect(pulse, 'output', tab, 'spike')  | 
 | 54 | +    print(datetime.now().isoformat())  | 
 | 55 | +    moose.reinit()  | 
 | 56 | +    moose.start(simtime)  | 
 | 57 | +    print(datetime.now().isoformat())  | 
 | 58 | +    np.savetxt('nsdf.txt', tab.vector)  | 
 | 59 | +    ###################################  | 
 | 60 | +    # Set the environment attributes  | 
 | 61 | +    ###################################  | 
 | 62 | +    nsdf.stringAttr['title'] = 'NSDF writing demo for moose'  | 
 | 63 | +    nsdf.stringAttr['description'] = '''An example of writing data to NSDF file from MOOSE simulation. In  | 
 | 64 | +this simulation we generate square pules from a PulseGen object and  | 
 | 65 | +use a SpikeGen to detect the threshold crossing events of rising  | 
 | 66 | +edges. We store the pulsegen output as Uniform data and the threshold  | 
 | 67 | +crossing times as Event data. '''      | 
 | 68 | +    nsdf.stringAttr['creator'] = getpass.getuser()  | 
 | 69 | +    nsdf.stringVecAttr['software'] = ['python2.7', 'moose3' ]  | 
 | 70 | +    nsdf.stringVecAttr['method'] = ['']  | 
 | 71 | +    nsdf.stringAttr['rights'] = ''  | 
 | 72 | +    nsdf.stringAttr['license'] = 'CC-BY-NC'  | 
 | 73 | +    # Specify units. MOOSE is unit agnostic, so we explicitly set the  | 
 | 74 | +    # unit attibutes on individual datasets  | 
 | 75 | +    nsdf.stringAttr['/data/uniform/PulseGen/outputValue/tunit'] = 's'  | 
 | 76 | +    nsdf.stringAttr['/data/uniform/PulseGen/outputValue/unit'] = 'A'  | 
 | 77 | +    eventDataPath = '/data/event/SpikeGen/spikeOut/{}_{}_{}/unit'.format(t_lead.vec.value,  | 
 | 78 | +                                                                         t_lead.getDataIndex(),   | 
 | 79 | +                                                                         t_lead.fieldIndex)  | 
 | 80 | +    nsdf.stringAttr[eventDataPath] = 's'  | 
 | 81 | + | 
 | 82 | +if __name__ == '__main__':  | 
 | 83 | +    setup_model()  | 
 | 84 | +    # Very basic tests  | 
 | 85 | +    nsdfFile = 'nsdf.txt'  | 
 | 86 | +    if not os.path.exists( nsdf.filename ):  | 
 | 87 | +        raise Exception("Test failed. No files : %s" % nsdfFile)  | 
 | 88 | +    if not os.path.exists( nsdfFile ):  | 
 | 89 | +        raise Exception("Test failed. No file : %s" % nsdfFile)  | 
 | 90 | + | 
 | 91 | +    data = np.loadtxt( nsdfFile )  | 
 | 92 | +    assert len(data) == 60001, "Expected 60001 entries"  | 
0 commit comments