123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import os
- from array import array
- from stat import ST_SIZE
- import numpy as np
- from .config import OBJSIMPY_DIR
- import logging
- logger = logging.getLogger(__name__)
- def read(filename):
- f = open(filename, 'rb')
- size = os.stat(filename)[ST_SIZE]
- n_spikes = size//8
- logger.info("read({}):\nsize={}, n_spikes={}".format(filename, size, n_spikes))
- spikes = array('i')
- spikes.fromfile(f, 2*n_spikes)
- f.close()
- spike_times = map(lambda i:spikes[2*i], range(n_spikes))
- neuron_numbers = map(lambda i:spikes[2*i+1], range(n_spikes))
- return list(spike_times), list(neuron_numbers)
- def write(spike_times_ms, senders, filename, first_node_id=0, dt=0.1):
- ''' dt is time step in ms
- '''
- steps = np.int64(spike_times_ms / dt)
- neuron_numbers = senders - first_node_id
- times_and_neurons = array('i', np.int64(np.array(np.array(zip(steps, neuron_numbers)).flat)))
- f = open(filename, "wb")
- times_and_neurons.tofile(f)
- f.close()
- def _from_memory(detec):
- import nest
- ev = nest.GetStatus(detec, "events")[0]
- return ev["times"], ev["senders"]
-
- if __name__=="__main__":
- import pylab,sys
- filename = os.path.join(OBJSIMPY_DIR, '..', 'test', 'data', 'simdata_minimal', 'MyLayerspikes.dat.bin')
- n_paras = len(sys.argv)-1
- if (n_paras>0):
- filename = sys.argv[1]
-
- spike_times, neuron_numbers = read(filename)
- n_spikes = len(spike_times)
- start_index = 0
- if (n_paras>1):
- start = float(sys.argv[2])
- while start_index < n_spikes:
- if spike_times[start_index] >= start:
- break
- start_index += 1
-
- stop_index = start_index
- if (n_paras>2):
- stop = float(sys.argv[3])
- while stop_index < n_spikes:
- if spike_times[stop_index] >= stop:
- break
- stop_index += 1
- else:
- stop_index = n_spikes
-
- print("start_index="+str(start_index))
- print("stop_index="+str(stop_index))
- pylab.plot(spike_times[start_index:stop_index], neuron_numbers[start_index:stop_index],'.')
- pylab.show()
-
|