Skip to content

Commit 791b400

Browse files
authored
Merge pull request #1429 from zm711/gallery
Update Examples in Gallery
2 parents 51f0246 + 0b325c9 commit 791b400

File tree

10 files changed

+124
-136
lines changed

10 files changed

+124
-136
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ doc/*.plx
7676
doc/*.nev
7777
doc/*.ns5
7878
doc/*.nix
79-
doc/*.nwb
79+
doc/*.nwb
80+
*.plx
81+
*.smr
82+
B95.zip
83+
grouped_ephys

doc/source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
)
253253

254254
sphinx_gallery_conf = {
255+
# 'only_warn_on_example_error': True, # helps with debugging broken examples
255256
"examples_dirs": "../../examples", # path to your example scripts
256257
"gallery_dirs": "examples", # path to where to save gallery generated output
257258
}

examples/generated_data.py

Lines changed: 0 additions & 128 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@
2929
plt.plot(signal.times, signal)
3030
plt.xlabel(signal.sampling_period.dimensionality)
3131
plt.ylabel(signal.dimensionality)
32+
33+
plt.show()
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
image_seq = ImageSequence(l, sampling_rate=500 * pq.Hz, spatial_scale="m", units="V")
2525

2626
result = image_seq.signal_from_region(
27-
CircularRegionOfInterest(50, 50, 25),
28-
CircularRegionOfInterest(10, 10, 5),
29-
PolygonRegionOfInterest((50, 25), (50, 45), (14, 65), (90, 80)),
27+
CircularRegionOfInterest(image_seq,50, 50, 25),
28+
CircularRegionOfInterest(image_seq, 10, 10, 5),
29+
PolygonRegionOfInterest(image_seq,(50, 25), (50, 45), (14, 65), (90, 80)),
3030
)
3131

3232
for i in range(len(result)):
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""
2+
Example for usecases.rst
3+
"""
4+
5+
from itertools import cycle
6+
import numpy as np
7+
from quantities import ms, mV, kHz
8+
import matplotlib.pyplot as plt
9+
from neo import Block, Segment, ChannelView, Group, SpikeTrain, AnalogSignal
10+
11+
store_signals = False
12+
13+
block = Block(name="probe data", tetrode_ids=["Tetrode #1", "Tetrode #2"])
14+
block.segments = [
15+
Segment(name="trial #1", index=0),
16+
Segment(name="trial #2", index=1),
17+
Segment(name="trial #3", index=2),
18+
]
19+
20+
n_units = {"Tetrode #1": 2, "Tetrode #2": 5}
21+
22+
# Create a group for each neuron, annotate each group with the tetrode from which it was recorded
23+
groups = []
24+
counter = 0
25+
for tetrode_id, n in n_units.items():
26+
groups.extend([Group(name=f"neuron #{counter + i + 1}", tetrode_id=tetrode_id) for i in range(n)])
27+
counter += n
28+
block.groups.extend(groups)
29+
30+
iter_group = cycle(groups)
31+
32+
# Create dummy data, one segment at a time
33+
for segment in block.segments:
34+
35+
# create two 4-channel AnalogSignals with dummy data
36+
signals = {
37+
"Tetrode #1": AnalogSignal(np.random.rand(1000, 4) * mV, sampling_rate=10 * kHz, tetrode_id="Tetrode #1"),
38+
"Tetrode #2": AnalogSignal(np.random.rand(1000, 4) * mV, sampling_rate=10 * kHz, tetrode_id="Tetrode #2"),
39+
}
40+
if store_signals:
41+
segment.analogsignals.extend(signals.values())
42+
43+
# create spike trains with dummy data
44+
# we will pretend the spikes have been extracted from the dummy signal
45+
for tetrode_id in ("Tetrode #1", "Tetrode #2"):
46+
for i in range(n_units[tetrode_id]):
47+
spiketrain = SpikeTrain(np.random.uniform(0, 100, size=30) * ms, t_stop=100 * ms)
48+
# assign each spiketrain to the appropriate segment
49+
segment.spiketrains.append(spiketrain)
50+
# assign each spiketrain to a given neuron
51+
current_group = next(iter_group)
52+
current_group.add(spiketrain)
53+
if store_signals:
54+
# add to the group a reference to the signal from which the spikes were obtained
55+
# this does not give a 1:1 correspondance between spike trains and signals,
56+
# for that we could use additional groups (and have groups of groups)
57+
current_group.add(signals[tetrode_id])
58+
59+
60+
# Now plot the data
61+
62+
# .. by trial
63+
plt.figure()
64+
for seg in block.segments:
65+
print(f"Analyzing segment {seg.index}")
66+
stlist = [st - st.t_start for st in seg.spiketrains]
67+
plt.subplot(len(block.segments), 1, seg.index + 1)
68+
count, bins = np.histogram(stlist)
69+
plt.bar(bins[:-1], count, width=bins[1] - bins[0])
70+
plt.title(f"PSTH in segment {seg.index}")
71+
plt.show()
72+
73+
# ..by neuron
74+
75+
plt.figure()
76+
for i, group in enumerate(block.groups):
77+
stlist = [st - st.t_start for st in group.spiketrains]
78+
plt.subplot(len(block.groups), 1, i + 1)
79+
count, bins = np.histogram(stlist)
80+
plt.bar(bins[:-1], count, width=bins[1] - bins[0])
81+
plt.title(f"PSTH of unit {group.name}")
82+
plt.show()
83+
84+
# ..by tetrode
85+
86+
plt.figure()
87+
for i, tetrode_id in enumerate(block.annotations["tetrode_ids"]):
88+
stlist = []
89+
for unit in block.filter(objects=Group, tetrode_id=tetrode_id):
90+
stlist.extend([st - st.t_start for st in unit.spiketrains])
91+
plt.subplot(2, 1, i + 1)
92+
count, bins = np.histogram(stlist)
93+
plt.bar(bins[:-1], count, width=bins[1] - bins[0])
94+
plt.title(f"PSTH blend of tetrode {tetrode_id}")
95+
plt.show()
File renamed without changes.
Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,24 @@
66

77
import matplotlib.pyplot as plt
88
import numpy as np
9-
from neo.core import CircularRegionOfInterest, RectangularRegionOfInterest, PolygonRegionOfInterest
9+
from neo.core import CircularRegionOfInterest, RectangularRegionOfInterest, PolygonRegionOfInterest, ImageSequence
1010
from numpy.random import rand
11+
import random
12+
import quantities as pq
1113

1214

15+
# First we create our image_sequence. Let's generate some data
16+
17+
l = []
18+
for frame in range(50):
19+
l.append([])
20+
for y in range(100):
21+
l[frame].append([])
22+
for x in range(100):
23+
l[frame][y].append(random.randint(0, 50))
24+
25+
image_seq = ImageSequence(l, sampling_rate=500 * pq.Hz, spatial_scale="m", units="V")
26+
1327
def plot_roi(roi, shape):
1428
img = rand(120, 100)
1529
pir = np.array(roi.pixels_in_region()).T
@@ -22,17 +36,17 @@ def plot_roi(roi, shape):
2236
ax.add_artist(shape)
2337

2438

25-
roi = CircularRegionOfInterest(x=50.3, y=50.8, radius=30.2)
39+
roi = CircularRegionOfInterest(image_sequence=image_seq, x=50.3, y=50.8, radius=30.2)
2640
shape = plt.Circle(roi.centre, roi.radius, color="r", fill=False)
2741
plt.subplot(1, 3, 1)
2842
plot_roi(roi, shape)
2943

30-
roi = RectangularRegionOfInterest(x=50.3, y=40.2, width=40.1, height=50.3)
44+
roi = RectangularRegionOfInterest(image_sequence=image_seq, x=50.3, y=40.2, width=40.1, height=50.3)
3145
shape = plt.Rectangle((roi.x - roi.width / 2.0, roi.y - roi.height / 2.0), roi.width, roi.height, color="r", fill=False)
3246
plt.subplot(1, 3, 2)
3347
plot_roi(roi, shape)
3448

35-
roi = PolygonRegionOfInterest((20.3, 30.2), (80.7, 30.1), (55.2, 59.4))
49+
roi = PolygonRegionOfInterest(image_seq, (20.3, 30.2), (80.7, 30.1), (55.2, 59.4))
3650
shape = plt.Polygon(np.array(roi.vertices), closed=True, color="r", fill=False)
3751
plt.subplot(1, 3, 3)
3852
plot_roi(roi, shape)

0 commit comments

Comments
 (0)