Skip to content

Commit 51dfa2b

Browse files
committed
fix examples displaying wrong order
1 parent b3538ce commit 51dfa2b

File tree

7 files changed

+136
-21
lines changed

7 files changed

+136
-21
lines changed

examples/plot_igorio.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
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
@@ -23,7 +25,10 @@
2325
zip_ref.extract(path=".", member=filename) # extract file to dir
2426
zip_ref.close()
2527

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

examples/plot_imageseq.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
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
1522

1623
l = []
1724
for frame in range(50):
@@ -29,6 +36,9 @@
2936
PolygonRegionOfInterest(image_seq,(50, 25), (50, 45), (14, 65), (90, 80)),
3037
)
3138

39+
###############################################################
40+
# It is easy to plot our results using matplotlib
41+
3242
for i in range(len(result)):
3343
plt.figure()
3444
plt.plot(result[i].times, result[i])

examples/plot_multi_tetrode_example.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""
2-
Example for usecases.rst
2+
Analyzing and Plotting Data with Neo Structures
3+
===============================================
34
"""
45

56
from itertools import cycle
@@ -57,8 +58,10 @@
5758
current_group.add(signals[tetrode_id])
5859

5960

60-
# Now plot the data
61+
###################################################
62+
# Now we will plot the data
6163

64+
###################################################
6265
# .. by trial
6366
plt.figure()
6467
for seg in block.segments:
@@ -70,6 +73,7 @@
7073
plt.title(f"PSTH in segment {seg.index}")
7174
plt.show()
7275

76+
####################################################
7377
# ..by neuron
7478

7579
plt.figure()
@@ -81,7 +85,8 @@
8185
plt.title(f"PSTH of unit {group.name}")
8286
plt.show()
8387

84-
# ..by tetrode
88+
###########################################################
89+
# ..by tetrode (or other electrode number)
8590

8691
plt.figure()
8792
for i, tetrode_id in enumerate(block.annotations["tetrode_ids"]):

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)

examples/plot_read_files_neo_rawio.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,98 @@
44
55
compare with read_files_neo_io.py
66
"""
7+
###########################################################
8+
# First we import a RawIO from neo.rawio
9+
# For this example we will use PlexonRawIO
710

811
import urllib
912
from neo.rawio import PlexonRawIO
1013

1114
url_repo = "https://web.gin.g-node.org/NeuralEnsemble/ephy_testing_data/raw/master/"
1215

16+
##############################################################
1317
# Get Plexon files
18+
# We will be pulling these files down, but if you have local file
19+
# then all you need to do is specify the file location on your
20+
# computer
21+
1422
distantfile = url_repo + "plexon/File_plexon_3.plx"
1523
localfile = "File_plexon_3.plx"
1624
urllib.request.urlretrieve(distantfile, localfile)
1725

18-
# create a reader
26+
###############################################################
27+
# Create a reader
28+
# All it takes to create a reader is giving the filename
29+
# Then we need to do the slow step of parsing the header with the
30+
# `parse_header` function. This collects metadata as well as
31+
# make all future steps much faster for us
32+
1933
reader = PlexonRawIO(filename="File_plexon_3.plx")
2034
reader.parse_header()
2135
print(reader)
36+
# we can view metadata in the header
2237
print(reader.header)
2338

39+
###############################################################
2440
# Read signal chunks
41+
# This is how we read raw data. We choose indices that we want or
42+
# we can use None to mean look at all channels. We also need to
43+
# specify the block of data (block_index) as well as the segment
44+
# (seg_index). Then we give the index start and stop. Since we
45+
# often thing in time to go from time to index would just require
46+
# the sample rate (so index = time / sampling_rate)
2547
channel_indexes = None # could be channel_indexes = [0]
2648
raw_sigs = reader.get_analogsignal_chunk(
2749
block_index=0, seg_index=0, i_start=1024, i_stop=2048, channel_indexes=channel_indexes
2850
)
51+
52+
# raw_sigs are not voltages so to convert to voltages we do the follwing
2953
float_sigs = reader.rescale_signal_raw_to_float(raw_sigs, dtype="float64")
54+
# each rawio gives you access to lots of information about your data
3055
sampling_rate = reader.get_signal_sampling_rate()
3156
t_start = reader.get_signal_t_start(block_index=0, seg_index=0)
3257
units = reader.header["signal_channels"][0]["units"]
58+
# and we can display all of this information
3359
print(raw_sigs.shape, raw_sigs.dtype)
3460
print(float_sigs.shape, float_sigs.dtype)
35-
print(sampling_rate, t_start, units)
61+
print(f"{sampling_rate=}, {t_start=}, {units=}")
62+
63+
64+
####################################################################
65+
# Some rawio's and file formats all provide information about spikes
66+
# If an rawio can't read this data it will raise an error, but lucky
67+
# for us PlexonRawIO does have spikes data!!
3668

3769
# Count units and spikes per unit
3870
nb_unit = reader.spike_channels_count()
3971
print("nb_unit", nb_unit)
4072
for spike_channel_index in range(nb_unit):
4173
nb_spike = reader.spike_count(block_index=0, seg_index=0, spike_channel_index=spike_channel_index)
42-
print("spike_channel_index", spike_channel_index, "nb_spike", nb_spike)
74+
print(f"{spike_channel_index=}\n{nb_spike}\n")
4375

44-
# Read spike times
76+
# Read spike times and rescale (just like analogsignal above)
4577
spike_timestamps = reader.get_spike_timestamps(
4678
block_index=0, seg_index=0, spike_channel_index=0, t_start=0.0, t_stop=10.0
4779
)
48-
print(spike_timestamps.shape, spike_timestamps.dtype, spike_timestamps[:5])
80+
print(f"{spike_timestamps.shape=}\n{spike_timestamps.dtype=}\n{spike_timestamps[:5]=}")
4981
spike_times = reader.rescale_spike_timestamp(spike_timestamps, dtype="float64")
50-
print(spike_times.shape, spike_times.dtype, spike_times[:5])
82+
print(f"{spike_times.shape=}\n{spike_times.dtype=}\n{spike_times[:5]}")
83+
84+
#######################################################################
85+
# Some file formats can also give waveform information. We are lucky
86+
# again. Our file has waveform data!!
5187

5288
# Read spike waveforms
5389
raw_waveforms = reader.get_spike_raw_waveforms(
5490
block_index=0, seg_index=0, spike_channel_index=0, t_start=0.0, t_stop=10.0
5591
)
56-
print(raw_waveforms.shape, raw_waveforms.dtype, raw_waveforms[0, 0, :4])
92+
print(f"{raw_waveforms.shape=}\n{raw_waveforms.dtype=}\n{raw_waveforms[0, 0, :4]=}")
5793
float_waveforms = reader.rescale_waveforms_to_float(raw_waveforms, dtype="float32", spike_channel_index=0)
58-
print(float_waveforms.shape, float_waveforms.dtype, float_waveforms[0, 0, :4])
94+
print(f"{float_waveforms.shape=}\n{float_waveforms.dtype=}{float_waveforms[0, 0, :4]=}")
95+
96+
#########################################################################
97+
# RawIOs can also read event timestamps. But looks like our luck ran out
98+
# let's grab a new file to see this feature
5999

60100
# Read event timestamps and times (take another file)
61101
distantfile = url_repo + "plexon/File_plexon_2.plx"
@@ -66,14 +106,14 @@
66106
reader = PlexonRawIO(filename="File_plexon_2.plx")
67107
reader.parse_header()
68108
nb_event_channel = reader.event_channels_count()
69-
print("nb_event_channel", nb_event_channel)
109+
print("nb_event_channel:", nb_event_channel)
70110
for chan_index in range(nb_event_channel):
71111
nb_event = reader.event_count(block_index=0, seg_index=0, event_channel_index=chan_index)
72112
print("chan_index", chan_index, "nb_event", nb_event)
73113

74114
ev_timestamps, ev_durations, ev_labels = reader.get_event_timestamps(
75115
block_index=0, seg_index=0, event_channel_index=0, t_start=None, t_stop=None
76116
)
77-
print(ev_timestamps, ev_durations, ev_labels)
117+
print(f"{ev_timestamps=}\n{ev_durations=}\n{ev_labels=}")
78118
ev_times = reader.rescale_event_timestamp(ev_timestamps, dtype="float64")
79-
print(ev_times)
119+
print(f"{ev_times=}")

examples/plot_read_proxy_with_lazy_load.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,37 @@
44
55
"""
66

7+
################################################
8+
# Import our packages first
9+
710
import urllib
811
import neo
912
import quantities as pq
1013
import numpy as np
1114

1215
url_repo = "https://web.gin.g-node.org/NeuralEnsemble/ephy_testing_data/raw/master/"
1316

14-
# Get Plexon files
17+
18+
###############################################
19+
# Let's get a file
20+
21+
# Get med file
1522
distantfile = url_repo + "micromed/File_micromed_1.TRC"
1623
localfile = "./File_micromed_1.TRC"
1724
urllib.request.urlretrieve(distantfile, localfile)
1825

26+
##################################################
1927
# create a reader
28+
2029
reader = neo.MicromedIO(filename="File_micromed_1.TRC")
2130
reader.parse_header()
2231

2332

2433
lim0, lim1 = -20 * pq.ms, +20 * pq.ms
2534

35+
#####################################################
36+
# Now let's make a function that we want to apply to
37+
# look at lazy for eager uses of the API
2638

2739
def apply_my_fancy_average(sig_list):
2840
"""basic average along triggers and then channels
@@ -33,6 +45,9 @@ def apply_my_fancy_average(sig_list):
3345
sigs = np.stack(sig_list, axis=0)
3446
return np.mean(np.mean(sigs, axis=0), axis=1)
3547

48+
##################################################
49+
# We start with eager where `lazy=False`. Everything
50+
# is loaded into memory
3651

3752
seg = reader.read_segment(lazy=False)
3853
triggers = seg.events[0]
@@ -44,6 +59,11 @@ def apply_my_fancy_average(sig_list):
4459
all_sig_chunks.append(anasig_chunk)
4560
m1 = apply_my_fancy_average(all_sig_chunks)
4661

62+
#####################################################
63+
# Here we do `lazy=True`, so we do lazy loading. We
64+
# only load the events that we want into memory
65+
# and we use a proxy object for our analogsignal until we
66+
# load it chunk by chunk (no running out of memory!)
4767

4868
seg = reader.read_segment(lazy=True)
4969
triggers = seg.events[0].load(time_slice=None) # this load all trigers in memory
@@ -55,5 +75,8 @@ def apply_my_fancy_average(sig_list):
5575
all_sig_chunks.append(anasig_chunk)
5676
m2 = apply_my_fancy_average(all_sig_chunks)
5777

78+
##########################################################
79+
# We see that either way the result is the same, but
80+
# we do not exhaust our RAM/memory
5881
print(m1)
5982
print(m2)

examples/plot_roi_demo.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
=====================================
44
55
"""
6+
#############################################
7+
# Import our packages
68

79
import matplotlib.pyplot as plt
810
import numpy as np
@@ -11,7 +13,7 @@
1113
import random
1214
import quantities as pq
1315

14-
16+
##################################################################
1517
# First we create our image_sequence. Let's generate some data
1618

1719
l = []
@@ -24,6 +26,8 @@
2426

2527
image_seq = ImageSequence(l, sampling_rate=500 * pq.Hz, spatial_scale="m", units="V")
2628

29+
#################################################################
30+
# Now we will write a function for plotting an roi
2731
def plot_roi(roi, shape):
2832
img = rand(120, 100)
2933
pir = np.array(roi.pixels_in_region()).T
@@ -35,6 +39,8 @@ def plot_roi(roi, shape):
3539
ax = plt.gca()
3640
ax.add_artist(shape)
3741

42+
##################################################################
43+
# Finally we will plot each roi
3844

3945
roi = CircularRegionOfInterest(image_sequence=image_seq, x=50.3, y=50.8, radius=30.2)
4046
shape = plt.Circle(roi.centre, roi.radius, color="r", fill=False)

0 commit comments

Comments
 (0)