spike_file.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import os
  4. from array import array
  5. from stat import ST_SIZE
  6. import numpy as np
  7. from .config import OBJSIMPY_DIR
  8. import logging
  9. logger = logging.getLogger(__name__)
  10. def read(filename):
  11. f = open(filename, 'rb')
  12. size = os.stat(filename)[ST_SIZE]
  13. n_spikes = size//8
  14. logger.info("read({}):\nsize={}, n_spikes={}".format(filename, size, n_spikes))
  15. spikes = array('i')
  16. spikes.fromfile(f, 2*n_spikes)
  17. f.close()
  18. spike_times = map(lambda i:spikes[2*i], range(n_spikes))
  19. neuron_numbers = map(lambda i:spikes[2*i+1], range(n_spikes))
  20. return list(spike_times), list(neuron_numbers)
  21. def write(spike_times_ms, senders, filename, first_node_id=0, dt=0.1):
  22. ''' dt is time step in ms
  23. '''
  24. steps = np.int64(spike_times_ms / dt)
  25. neuron_numbers = senders - first_node_id
  26. times_and_neurons = array('i', np.int64(np.array(np.array(zip(steps, neuron_numbers)).flat)))
  27. f = open(filename, "wb")
  28. times_and_neurons.tofile(f)
  29. f.close()
  30. def _from_memory(detec):
  31. import nest
  32. ev = nest.GetStatus(detec, "events")[0]
  33. return ev["times"], ev["senders"]
  34. if __name__=="__main__":
  35. import pylab,sys
  36. filename = os.path.join(OBJSIMPY_DIR, '..', 'test', 'data', 'simdata_minimal', 'MyLayerspikes.dat.bin')
  37. n_paras = len(sys.argv)-1
  38. if (n_paras>0):
  39. filename = sys.argv[1]
  40. spike_times, neuron_numbers = read(filename)
  41. n_spikes = len(spike_times)
  42. start_index = 0
  43. if (n_paras>1):
  44. start = float(sys.argv[2])
  45. while start_index < n_spikes:
  46. if spike_times[start_index] >= start:
  47. break
  48. start_index += 1
  49. stop_index = start_index
  50. if (n_paras>2):
  51. stop = float(sys.argv[3])
  52. while stop_index < n_spikes:
  53. if spike_times[stop_index] >= stop:
  54. break
  55. stop_index += 1
  56. else:
  57. stop_index = n_spikes
  58. print("start_index="+str(start_index))
  59. print("stop_index="+str(stop_index))
  60. pylab.plot(spike_times[start_index:stop_index], neuron_numbers[start_index:stop_index],'.')
  61. pylab.show()