generated_data.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. This is an example for creating simple plots from various Neo structures.
  4. It includes a function that generates toy data.
  5. """
  6. from __future__ import division # Use same division in Python 2 and 3
  7. import numpy as np
  8. import quantities as pq
  9. from matplotlib import pyplot as plt
  10. import neo
  11. def generate_block(n_segments=3, n_channels=4, n_units=3,
  12. data_samples=1000, feature_samples=100):
  13. """
  14. Generate a block with a single recording channel group and a number of
  15. segments, recording channels and units with associated analog signals
  16. and spike trains.
  17. """
  18. feature_len = feature_samples / data_samples
  19. # Create Block to contain all generated data
  20. block = neo.Block()
  21. # Create multiple Segments
  22. block.segments = [neo.Segment(index=i) for i in range(n_segments)]
  23. # Create multiple ChannelIndexes
  24. block.channel_indexes = [neo.ChannelIndex(name='C%d' % i, index=i) for i in range(n_channels)]
  25. # Attach multiple Units to each ChannelIndex
  26. for channel_idx in block.channel_indexes:
  27. channel_idx.units = [neo.Unit('U%d' % i) for i in range(n_units)]
  28. # Create synthetic data
  29. for seg in block.segments:
  30. feature_pos = np.random.randint(0, data_samples - feature_samples)
  31. # Analog signals: Noise with a single sinewave feature
  32. wave = 3 * np.sin(np.linspace(0, 2 * np.pi, feature_samples))
  33. for channel_idx in block.channel_indexes:
  34. sig = np.random.randn(data_samples)
  35. sig[feature_pos:feature_pos + feature_samples] += wave
  36. signal = neo.AnalogSignal(sig * pq.mV, sampling_rate=1 * pq.kHz)
  37. seg.analogsignals.append(signal)
  38. channel_idx.analogsignals.append(signal)
  39. # Spike trains: Random spike times with elevated rate in short period
  40. feature_time = feature_pos / data_samples
  41. for u in channel_idx.units:
  42. random_spikes = np.random.rand(20)
  43. feature_spikes = np.random.rand(5) * feature_len + feature_time
  44. spikes = np.hstack([random_spikes, feature_spikes])
  45. train = neo.SpikeTrain(spikes * pq.s, 1 * pq.s)
  46. seg.spiketrains.append(train)
  47. u.spiketrains.append(train)
  48. block.create_many_to_one_relationship()
  49. return block
  50. block = generate_block()
  51. # In this example, we treat each segment in turn, averaging over the channels
  52. # in each:
  53. for seg in block.segments:
  54. print("Analysing segment %d" % seg.index)
  55. siglist = seg.analogsignals
  56. time_points = siglist[0].times
  57. avg = np.mean(siglist, axis=0) # Average over signals of Segment
  58. plt.figure()
  59. plt.plot(time_points, avg)
  60. plt.title("Peak response in segment %d: %f" % (seg.index, avg.max()))
  61. # The second alternative is spatial traversal of the data (by channel), with
  62. # averaging over trials. For example, perhaps you wish to see which physical
  63. # location produces the strongest response, and each stimulus was the same:
  64. # There are multiple ChannelIndex objects connected to the block, each
  65. # corresponding to a a physical electrode
  66. for channel_idx in block.channel_indexes:
  67. print("Analysing channel %d: %s" % (channel_idx.index, channel_idx.name))
  68. siglist = channel_idx.analogsignals
  69. time_points = siglist[0].times
  70. avg = np.mean(siglist, axis=0) # Average over signals of RecordingChannel
  71. plt.figure()
  72. plt.plot(time_points, avg)
  73. plt.title("Average response on channel %d" % channel_idx.index)
  74. # There are three ways to access the spike train data: by Segment,
  75. # by ChannelIndex or by Unit.
  76. # By Segment. In this example, each Segment represents data from one trial,
  77. # and we want a peristimulus time histogram (PSTH) for each trial from all
  78. # Units combined:
  79. for seg in block.segments:
  80. print("Analysing segment %d" % seg.index)
  81. stlist = [st - st.t_start for st in seg.spiketrains]
  82. count, bins = np.histogram(np.hstack(stlist))
  83. plt.figure()
  84. plt.bar(bins[:-1], count, width=bins[1] - bins[0])
  85. plt.title("PSTH in segment %d" % seg.index)
  86. # By Unit. Now we can calculate the PSTH averaged over trials for each Unit:
  87. for unit in block.list_units:
  88. stlist = [st - st.t_start for st in unit.spiketrains]
  89. count, bins = np.histogram(np.hstack(stlist))
  90. plt.figure()
  91. plt.bar(bins[:-1], count, width=bins[1] - bins[0])
  92. plt.title("PSTH of unit %s" % unit.name)
  93. # By ChannelIndex. Here we calculate a PSTH averaged over trials by
  94. # channel location, blending all Units:
  95. for chx in block.channel_indexes:
  96. stlist = []
  97. for unit in chx.units:
  98. stlist.extend([st - st.t_start for st in unit.spiketrains])
  99. count, bins = np.histogram(np.hstack(stlist))
  100. plt.figure()
  101. plt.bar(bins[:-1], count, width=bins[1] - bins[0])
  102. plt.title("PSTH blend of recording channel group %s" % chx.name)
  103. plt.show()