|
| 1 | +************************* |
| 2 | +Grouping and linking data |
| 3 | +************************* |
| 4 | + |
| 5 | + |
| 6 | +... to do |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +Migrating from ChannelIndex/Unit to ChannelView/Group |
| 12 | +============================================== |
| 13 | + |
| 14 | + |
| 15 | +Examples |
| 16 | +-------- |
| 17 | + |
| 18 | +A simple example with two tetrodes. Here the :class:`ChannelIndex` was not being |
| 19 | +used for grouping, simply to associate a name with each channel. |
| 20 | + |
| 21 | +Using :class:`ChannelIndex`:: |
| 22 | + |
| 23 | + import numpy as np |
| 24 | + from quantities import kHz, mV |
| 25 | + from neo import Block, Segment, ChannelIndex, AnalogSignal |
| 26 | + |
| 27 | + block = Block() |
| 28 | + segment = Segment() |
| 29 | + segment.block = block |
| 30 | + block.segments.append(segment) |
| 31 | + |
| 32 | + for i in (0, 1): |
| 33 | + signal = AnalogSignal(np.random.rand(1000, 4) * mV, |
| 34 | + sampling_rate=1 * kHz,) |
| 35 | + segment.analogsignals.append(signal) |
| 36 | + chx = ChannelIndex(name=f"Tetrode #{i + 1}", |
| 37 | + index=[0, 1, 2, 3], |
| 38 | + channel_names=["A", "B", "C", "D"]) |
| 39 | + chx.analogsignals.append(signal) |
| 40 | + block.channel_indexes.append(chx) |
| 41 | + |
| 42 | +Using array annotations, we annotate the channels of the :class:`AnalogSignal` directly:: |
| 43 | + |
| 44 | + import numpy as np |
| 45 | + from quantities import kHz, mV |
| 46 | + from neo import Block, Segment, AnalogSignal |
| 47 | + |
| 48 | + block = Block() |
| 49 | + segment = Segment() |
| 50 | + segment.block = block |
| 51 | + block.segments.append(segment) |
| 52 | + |
| 53 | + for i in (0, 1): |
| 54 | + signal = AnalogSignal(np.random.rand(1000, 4) * mV, |
| 55 | + sampling_rate=1 * kHz, |
| 56 | + channel_names=["A", "B", "C", "D"]) |
| 57 | + segment.analogsignals.append(signal) |
| 58 | + |
| 59 | + |
| 60 | +Now a more complex example: a 1x4 silicon probe, with a neuron on channels 0,1,2 and another neuron on channels 1,2,3. |
| 61 | +We create a :class:`ChannelIndex` for each neuron to hold the :class:`Unit` object associated with this spike sorting group. |
| 62 | +Each :class:`ChannelIndex` also contains the list of channels on which that neuron spiked. |
| 63 | + |
| 64 | +:: |
| 65 | + |
| 66 | + import numpy as np |
| 67 | + from quantities import ms, mV, kHz |
| 68 | + from neo import Block, Segment, ChannelIndex, Unit, SpikeTrain, AnalogSignal |
| 69 | + |
| 70 | + block = Block(name="probe data") |
| 71 | + segment = Segment() |
| 72 | + segment.block = block |
| 73 | + block.segments.append(segment) |
| 74 | + |
| 75 | + # create 4-channel AnalogSignal with dummy data |
| 76 | + signal = AnalogSignal(np.random.rand(1000, 4) * mV, |
| 77 | + sampling_rate=10 * kHz) |
| 78 | + # create spike trains with dummy data |
| 79 | + # we will pretend the spikes have been extracted from the dummy signal |
| 80 | + spiketrains = [ |
| 81 | + SpikeTrain(np.arange(5, 100) * ms, t_stop=100 * ms), |
| 82 | + SpikeTrain(np.arange(7, 100) * ms, t_stop=100 * ms) |
| 83 | + ] |
| 84 | + segment.analogsignals.append(signal) |
| 85 | + segment.spiketrains.extend(spiketrains) |
| 86 | + # assign each spiketrain to a neuron (Unit) |
| 87 | + units = [] |
| 88 | + for i, spiketrain in enumerate(spiketrains): |
| 89 | + unit = Unit(name=f"Neuron #{i + 1}") |
| 90 | + unit.spiketrains.append(spiketrain) |
| 91 | + units.append(unit) |
| 92 | + |
| 93 | + # create a ChannelIndex for each unit, to show which channels the spikes come from |
| 94 | + chx0 = ChannelIndex(name="Channel Group 1", index=[0, 1, 2]) |
| 95 | + chx0.units.append(units[0]) |
| 96 | + chx0.analogsignals.append(signal) |
| 97 | + units[0].channel_index = chx0 |
| 98 | + chx1 = ChannelIndex(name="Channel Group 2", index=[1, 2, 3]) |
| 99 | + chx1.units.append(units[1]) |
| 100 | + chx1.analogsignals.append(signal) |
| 101 | + units[1].channel_index = chx1 |
| 102 | + |
| 103 | + block.channel_indexes.extend((chx0, chx1)) |
| 104 | + |
| 105 | + |
| 106 | +Using :class:`ChannelView` and :class`Group`:: |
| 107 | + |
| 108 | + import numpy as np |
| 109 | + from quantities import ms, mV, kHz |
| 110 | + from neo import Block, Segment, ChannelView, Group, SpikeTrain, AnalogSignal |
| 111 | + |
| 112 | + block = Block(name="probe data") |
| 113 | + segment = Segment() |
| 114 | + segment.block = block |
| 115 | + block.segments.append(segment) |
| 116 | + |
| 117 | + # create 4-channel AnalogSignal with dummy data |
| 118 | + signal = AnalogSignal(np.random.rand(1000, 4) * mV, |
| 119 | + sampling_rate=10 * kHz) |
| 120 | + # create spike trains with dummy data |
| 121 | + # we will pretend the spikes have been extracted from the dummy signal |
| 122 | + spiketrains = [ |
| 123 | + SpikeTrain(np.arange(5, 100) * ms, t_stop=100 * ms), |
| 124 | + SpikeTrain(np.arange(7, 100) * ms, t_stop=100 * ms) |
| 125 | + ] |
| 126 | + segment.analogsignals.append(signal) |
| 127 | + segment.spiketrains.extend(spiketrains) |
| 128 | + # assign each spiketrain to a neuron (now using Group) |
| 129 | + units = [] |
| 130 | + for i, spiketrain in enumerate(spiketrains): |
| 131 | + unit = Group(spiketrain, name=f"Neuron #{i + 1}") |
| 132 | + units.append(unit) |
| 133 | + |
| 134 | + # create a ChannelView of the signal for each unit, to show which channels the spikes come from |
| 135 | + # and add it to the relevant Group |
| 136 | + view0 = ChannelView(signal, index=[0, 1, 2], name="Channel Group 1") |
| 137 | + units[0].add(view0) |
| 138 | + view1 = ChannelView(signal, index=[1, 2, 3], name="Channel Group 2") |
| 139 | + units[1].add(view1) |
| 140 | + |
| 141 | + block.groups.extend(units) |
| 142 | + |
| 143 | + |
| 144 | +Now each putative neuron is represented by a :class:`Group` containing the spiktrains of that neuron |
| 145 | +and a view of the signal selecting only those channels from which the spikes were obtained. |
0 commit comments