Skip to content

Commit fa3ebe4

Browse files
authored
Merge pull request #1456 from zm711/rtd-fix
Sphinx-Gallery Touchups
2 parents b29cfa9 + 207b851 commit fa3ebe4

9 files changed

+280
-41
lines changed

environment_testing.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ name: neo-test-env
22
channels:
33
- conda-forge
44
dependencies:
5+
- python=3.9
56
- datalad
67
- pip

examples/plot_igorio.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,21 @@
33
===========================
44
55
"""
6-
6+
###########################################################
7+
# Import our packages
78
import os
89
from urllib.request import urlretrieve
910
import zipfile
1011
import matplotlib.pyplot as plt
1112
from neo.io import get_io
1213

13-
14+
#############################################################
15+
# Then download some data
1416
# Downloaded from Human Brain Project Collaboratory
1517
# Digital Reconstruction of Neocortical Microcircuitry (nmc-portal)
1618
# http://microcircuits.epfl.ch/#/animal/8ecde7d1-b2d2-11e4-b949-6003088da632
19+
20+
1721
datafile_url = "https://microcircuits.epfl.ch/data/released_data/B95.zip"
1822
filename_zip = "B95.zip"
1923
filename = "grouped_ephys/B95/B95_Ch0_IDRest_107.ibw"
@@ -23,7 +27,10 @@
2327
zip_ref.extract(path=".", member=filename) # extract file to dir
2428
zip_ref.close()
2529

26-
30+
######################################################
31+
# Once we have our data we can use `get_io` to find an
32+
# io (Igor in this case). Then we read the analogsignals
33+
# Finally we will make some nice plots
2734
reader = get_io(filename)
2835
signal = reader.read_analogsignal()
2936
plt.plot(signal.times, signal)

examples/plot_imageseq.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,22 @@
44
55
"""
66

7+
##########################################################
8+
# Let's import some packages
9+
710
from neo.core import ImageSequence
811
from neo.core import RectangularRegionOfInterest, CircularRegionOfInterest, PolygonRegionOfInterest
912
import matplotlib.pyplot as plt
1013
import quantities as pq
1114

1215
import random
1316

14-
# generate data
17+
18+
############################################################
19+
# Now we need to generate some data
20+
# We will just make a nice box and then we can attach this
21+
# ImageSequence to a variety of ROIs
22+
# our ImageSequence will be 50 frames of 100x100 pixel images
1523

1624
l = []
1725
for frame in range(50):
@@ -21,6 +29,10 @@
2129
for x in range(100):
2230
l[frame][y].append(random.randint(0, 50))
2331

32+
#####################################################################
33+
# we then make our image sequence and pull out our results from the
34+
# image_seq
35+
2436
image_seq = ImageSequence(l, sampling_rate=500 * pq.Hz, spatial_scale="m", units="V")
2537

2638
result = image_seq.signal_from_region(
@@ -29,10 +41,13 @@
2941
PolygonRegionOfInterest(image_seq,(50, 25), (50, 45), (14, 65), (90, 80)),
3042
)
3143

44+
###############################################################
45+
# It is easy to plot our results using matplotlib
46+
3247
for i in range(len(result)):
3348
plt.figure()
3449
plt.plot(result[i].times, result[i])
3550
plt.xlabel("seconde")
3651
plt.ylabel("valeur")
37-
52+
plt.tight_layout()
3853
plt.show()

examples/plot_multi_tetrode_example.py

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
"""
2-
Example for usecases.rst
2+
Analyzing and Plotting Data with Neo Structures
3+
===============================================
34
"""
5+
######################################################
6+
# First we import some packages. Since we are making simulated
7+
# data we will import quite a few neo features as well as use
8+
# quantities to provide units
49

510
from itertools import cycle
611
import numpy as np
712
from quantities import ms, mV, kHz
813
import matplotlib.pyplot as plt
914
from neo import Block, Segment, ChannelView, Group, SpikeTrain, AnalogSignal
1015

16+
##########################################################################
17+
# For Neo we start with a block of data that will contain segments of data
18+
# so we will create a block of probe data that has a couple tetrodes
19+
# Then we will load in 3 segments (for examples trials of a stimulus)
1120
store_signals = False
1221

1322
block = Block(name="probe data", tetrode_ids=["Tetrode #1", "Tetrode #2"])
@@ -17,8 +26,13 @@
1726
Segment(name="trial #3", index=2),
1827
]
1928

29+
# we will decide how many units each tetrode has found. If only science was this easy
2030
n_units = {"Tetrode #1": 2, "Tetrode #2": 5}
2131

32+
##################################################################################
33+
# Neo can also have groups. Groups are structures within a block that can cross segments
34+
# for example we could group a neuron across trials or across probes.
35+
2236
# Create a group for each neuron, annotate each group with the tetrode from which it was recorded
2337
groups = []
2438
counter = 0
@@ -29,19 +43,28 @@
2943

3044
iter_group = cycle(groups)
3145

46+
##########################################################################################
47+
# Segments are also containers of data. Segments can hold raw signal data like an AnalogSignal
48+
# Segments can also hold spiketrain data (in a SpikeTrain). It can also hold event data (which
49+
# we are not show in this example)
50+
51+
3252
# Create dummy data, one segment at a time
3353
for segment in block.segments:
3454

35-
# create two 4-channel AnalogSignals with dummy data
55+
# create two 4-channel AnalogSignals with simulated data (because we have two tetrodes!)
56+
# note that the AnalogSignal with have numpy array-like data with units and sampling rates
57+
# Neo keeps track of these units while also giving you the flexibility of treating the raw data
58+
# like a numpy array
3659
signals = {
3760
"Tetrode #1": AnalogSignal(np.random.rand(1000, 4) * mV, sampling_rate=10 * kHz, tetrode_id="Tetrode #1"),
3861
"Tetrode #2": AnalogSignal(np.random.rand(1000, 4) * mV, sampling_rate=10 * kHz, tetrode_id="Tetrode #2"),
3962
}
4063
if store_signals:
4164
segment.analogsignals.extend(signals.values())
4265

43-
# create spike trains with dummy data
44-
# we will pretend the spikes have been extracted from the dummy signal
66+
# create spike trains with simulated data
67+
# we will pretend the spikes have been extracted from the simulated signal
4568
for tetrode_id in ("Tetrode #1", "Tetrode #2"):
4669
for i in range(n_units[tetrode_id]):
4770
spiketrain = SpikeTrain(np.random.uniform(0, 100, size=30) * ms, t_stop=100 * ms)
@@ -57,8 +80,16 @@
5780
current_group.add(signals[tetrode_id])
5881

5982

60-
# Now plot the data
83+
###################################################
84+
# Now we will plot the data
85+
# Neo doesn't provide it's own plotting functions, but
86+
# since its data can be treated like numpy arrays
87+
# it is easy to use standard packages like matplotlib
88+
# for all your plotting needs
89+
# We do a classic in neuroscience and show various ways
90+
# to plot a PSTH (Peristimulus histogram)
6191

92+
###################################################
6293
# .. by trial
6394
plt.figure()
6495
for seg in block.segments:
@@ -68,8 +99,10 @@
6899
count, bins = np.histogram(stlist)
69100
plt.bar(bins[:-1], count, width=bins[1] - bins[0])
70101
plt.title(f"PSTH in segment {seg.index}")
102+
plt.tight_layout()
71103
plt.show()
72104

105+
####################################################
73106
# ..by neuron
74107

75108
plt.figure()
@@ -79,9 +112,11 @@
79112
count, bins = np.histogram(stlist)
80113
plt.bar(bins[:-1], count, width=bins[1] - bins[0])
81114
plt.title(f"PSTH of unit {group.name}")
115+
plt.tight_layout()
82116
plt.show()
83117

84-
# ..by tetrode
118+
###########################################################
119+
# ..by tetrode (or other electrode number)
85120

86121
plt.figure()
87122
for i, tetrode_id in enumerate(block.annotations["tetrode_ids"]):
@@ -92,4 +127,5 @@
92127
count, bins = np.histogram(stlist)
93128
plt.bar(bins[:-1], count, width=bins[1] - bins[0])
94129
plt.title(f"PSTH blend of tetrode {tetrode_id}")
130+
plt.tight_layout()
95131
plt.show()

examples/plot_read_files_neo_io.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
55
"""
66

7+
####################################################
8+
# Start with package import and getting a datafile
9+
710
import urllib
811

912
import neo
@@ -15,11 +18,22 @@
1518
localfile = "File_plexon_3.plx"
1619
urllib.request.urlretrieve(distantfile, localfile)
1720

21+
22+
23+
###################################################
24+
# Now we can create our reader and read some data
25+
1826
# create a reader
1927
reader = neo.io.PlexonIO(filename="File_plexon_3.plx")
2028
# read the blocks
2129
blks = reader.read(lazy=False)
2230
print(blks)
31+
32+
######################################################
33+
# Once we have our blocks we can iterate through each
34+
# block of data and see the contents of all parts of
35+
# that data
36+
2337
# access to segments
2438
for blk in blks:
2539
for seg in blk.segments:
@@ -29,16 +43,28 @@
2943
for st in seg.spiketrains:
3044
print(st)
3145

46+
#######################################################
47+
# Let's look at another file type
48+
3249
# CED Spike2 files
3350
distantfile = url_repo + "spike2/File_spike2_1.smr"
3451
localfile = "./File_spike2_1.smr"
3552
urllib.request.urlretrieve(distantfile, localfile)
36-
3753
# create a reader
3854
reader = neo.io.Spike2IO(filename="File_spike2_1.smr")
55+
56+
#########################################################
57+
# Despite being a different raw file format we can access
58+
# the data in the same way
59+
3960
# read the block
4061
bl = reader.read(lazy=False)[0]
4162
print(bl)
63+
64+
##########################################################
65+
# Similarly we can view the different types of data within
66+
# the block (AnalogSignals and SpikeTrains)
67+
4268
# access to segments
4369
for seg in bl.segments:
4470
print(seg)

0 commit comments

Comments
 (0)