Skip to content

Commit d4e946d

Browse files
bendichterrly
andauthored
update tutorial with best practices and Black formatting (#1656)
* update tutorial with best practices and Black formatting * update tutorial with best practices and Black formatting * Update docs/gallery/advanced_io/linking_data.py * Update docs/gallery/advanced_io/linking_data.py * Update docs/gallery/advanced_io/plot_iterative_write.py * Apply default black to gallery files * Fixes * Bug fix * Use isort in gallery files * downgrade flake8 * Fix * Fix windows issue * Update req-dev --------- Co-authored-by: Ryan Ly <[email protected]>
1 parent dd54e5a commit d4e946d

23 files changed

+1109
-803
lines changed

docs/gallery/advanced_io/h5dataio.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
'''
1+
"""
22
Defining HDF5 Dataset I/O Settings (chunking, compression, etc.)
33
================================================================
44
55
The HDF5 storage backend supports a broad range of advanced dataset I/O options, such as,
66
chunking and compression. Here we demonstrate how to use these features
77
from PyNWB.
8-
'''
8+
"""
99

1010
####################
1111
# Wrapping data arrays with :py:class:`~hdmf.backends.hdf5.h5_utils.H5DataIO`
@@ -19,30 +19,33 @@
1919
#
2020

2121
from datetime import datetime
22+
2223
from dateutil.tz import tzlocal
24+
2325
from pynwb import NWBFile
2426

2527
start_time = datetime(2017, 4, 3, 11, tzinfo=tzlocal())
26-
create_date = datetime(2017, 4, 15, 12, tzinfo=tzlocal())
2728

28-
nwbfile = NWBFile(session_description='demonstrate advanced HDF5 I/O features',
29-
identifier='NWB123',
30-
session_start_time=start_time,
31-
file_create_date=create_date)
29+
nwbfile = NWBFile(
30+
session_description="demonstrate advanced HDF5 I/O features",
31+
identifier="NWB123",
32+
session_start_time=start_time,
33+
)
3234

3335

3436
####################
3537
# Normally if we create a :py:class:`~pynwb.file.TimeSeries` we would do
3638

37-
from pynwb import TimeSeries
3839
import numpy as np
3940

41+
from pynwb import TimeSeries
42+
4043
data = np.arange(100, 200, 10)
4144
timestamps = np.arange(10)
4245
test_ts = TimeSeries(
43-
name='test_regular_timeseries',
46+
name="test_regular_timeseries",
4447
data=data,
45-
unit='SIunit',
48+
unit="SIunit",
4649
timestamps=timestamps,
4750
)
4851
nwbfile.add_acquisition(test_ts)
@@ -52,10 +55,11 @@
5255
# :py:class:`~hdmf.backends.hdf5.h5_utils.H5DataIO`. Everything else remains the same
5356

5457
from hdmf.backends.hdf5.h5_utils import H5DataIO
58+
5559
test_ts = TimeSeries(
56-
name='test_compressed_timeseries',
57-
data=H5DataIO(data=data, compression=True), # <----
58-
unit='SIunit',
60+
name="test_compressed_timeseries",
61+
data=H5DataIO(data=data, compression=True), # <----
62+
unit="SIunit",
5963
timestamps=timestamps,
6064
)
6165
nwbfile.add_acquisition(test_ts)
@@ -91,13 +95,13 @@
9195
data = np.arange(10000).reshape((1000, 10))
9296
wrapped_data = H5DataIO(
9397
data=data,
94-
chunks=True, # <---- Enable chunking
98+
chunks=True, # <---- Enable chunking
9599
maxshape=(None, 10), # <---- Make the time dimension unlimited and hence resizable
96100
)
97101
test_ts = TimeSeries(
98-
name='test_chunked_timeseries',
99-
data=wrapped_data, # <----
100-
unit='SIunit',
102+
name="test_chunked_timeseries",
103+
data=wrapped_data, # <----
104+
unit="SIunit",
101105
starting_time=0.0,
102106
rate=10.0,
103107
)
@@ -122,7 +126,7 @@
122126

123127
####################
124128
# Compression and Other I/O Filters
125-
# -----------------------------------
129+
# ---------------------------------
126130
#
127131
# HDF5 supports I/O filters, i.e, data transformation (e.g, compression) that are applied transparently on
128132
# read/write operations. I/O filters operate on a per-chunk basis in HDF5 and as such require the use of chunking.
@@ -133,13 +137,13 @@
133137

134138
wrapped_data = H5DataIO(
135139
data=data,
136-
compression='gzip', # <---- Use GZip
137-
compression_opts=4, # <---- Optional GZip aggression option
140+
compression="gzip", # <---- Use GZip
141+
compression_opts=4, # <---- Optional GZip aggression option
138142
)
139143
test_ts = TimeSeries(
140-
name='test_gzipped_timeseries',
141-
data=wrapped_data, # <----
142-
unit='SIunit',
144+
name="test_gzipped_timeseries",
145+
data=wrapped_data, # <----
146+
unit="SIunit",
143147
starting_time=0.0,
144148
rate=10.0,
145149
)
@@ -160,7 +164,7 @@
160164

161165
from pynwb import NWBHDF5IO
162166

163-
with NWBHDF5IO('advanced_io_example.nwb', 'w') as io:
167+
with NWBHDF5IO("advanced_io_example.nwb", "w") as io:
164168
io.write(nwbfile)
165169

166170
####################
@@ -170,17 +174,17 @@
170174
#
171175
# Nothing has changed for read. All the above advanced I/O features are handled transparently.
172176

173-
io = NWBHDF5IO('advanced_io_example.nwb', 'r')
177+
io = NWBHDF5IO("advanced_io_example.nwb", "r")
174178
nwbfile = io.read()
175179

176180
####################
177181
# Now lets have a look to confirm that all our I/O settings where indeed used.
178182

179183
for k, v in nwbfile.acquisition.items():
180-
print("name=%s, chunks=%s, compression=%s, maxshape=%s" % (k,
181-
v.data.chunks,
182-
v.data.compression,
183-
v.data.maxshape))
184+
print(
185+
"name=%s, chunks=%s, compression=%s, maxshape=%s"
186+
% (k, v.data.chunks, v.data.compression, v.data.maxshape)
187+
)
184188
io.close()
185189
####################
186190
#
@@ -228,8 +232,8 @@
228232
# This command automatically installs the filters. Here is an example of how you would use the Z Standard algorithm:
229233

230234
import hdf5plugin
231-
232235
from hdmf.backends.hdf5.h5_utils import H5DataIO
236+
233237
from pynwb.file import TimeSeries
234238

235239
wrapped_data = H5DataIO(
@@ -239,9 +243,9 @@
239243
)
240244

241245
test_ts = TimeSeries(
242-
name='test_gzipped_timeseries',
246+
name="test_gzipped_timeseries",
243247
data=wrapped_data,
244-
unit='SIunit',
248+
unit="SIunit",
245249
starting_time=0.0,
246250
rate=10.0,
247251
)
@@ -262,7 +266,7 @@
262266

263267
####################
264268
# Disclaimer
265-
# ----------------
269+
# ----------
266270
#
267271
# External links included in the tutorial are being provided as a convenience and for informational purposes only;
268272
# they do not constitute an endorsement or an approval by the authors of any of the products, services or opinions of

docs/gallery/advanced_io/linking_data.py

Lines changed: 37 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,60 +60,53 @@
6060
from datetime import datetime
6161
from uuid import uuid4
6262

63-
from dateutil.tz import tzlocal
64-
from pynwb import NWBFile
65-
from pynwb import TimeSeries
66-
from pynwb import NWBHDF5IO
6763
import numpy as np
64+
from dateutil.tz import tzlocal
65+
66+
from pynwb import NWBHDF5IO, NWBFile, TimeSeries
6867

6968
# Create the base data
7069
start_time = datetime(2017, 4, 3, 11, tzinfo=tzlocal())
7170
data = np.arange(1000).reshape((100, 10))
7271
timestamps = np.arange(100)
73-
filename1 = 'external1_example.nwb'
74-
filename2 = 'external2_example.nwb'
75-
filename3 = 'external_linkcontainer_example.nwb'
76-
filename4 = 'external_linkdataset_example.nwb'
72+
filename1 = "external1_example.nwb"
73+
filename2 = "external2_example.nwb"
74+
filename3 = "external_linkcontainer_example.nwb"
75+
filename4 = "external_linkdataset_example.nwb"
7776

7877
# Create the first file
7978
nwbfile1 = NWBFile(
80-
session_description="my first synthetic recording",
79+
session_description="demonstrate external files",
8180
identifier=str(uuid4()),
82-
session_start_time=datetime.now(tzlocal()),
83-
experimenter=["Baggins, Bilbo", ],
84-
lab="Bag End Laboratory",
85-
institution="University of Middle Earth at the Shire",
86-
experiment_description="I went on an adventure to reclaim vast treasures.",
87-
session_id="LONELYMTN001",
81+
session_start_time=start_time,
8882
)
8983
# Create the second file
90-
test_ts1 = TimeSeries(name='test_timeseries1',
91-
data=data,
92-
unit='SIunit',
93-
timestamps=timestamps)
84+
test_ts1 = TimeSeries(
85+
name="test_timeseries1", data=data, unit="SIunit", timestamps=timestamps
86+
)
9487
nwbfile1.add_acquisition(test_ts1)
9588

9689
# Write the first file
97-
with NWBHDF5IO(filename1, 'w') as io:
90+
with NWBHDF5IO(filename1, "w") as io:
9891
io.write(nwbfile1)
9992

10093
# Create the second file
10194
nwbfile2 = NWBFile(
102-
session_description='demonstrate external files',
95+
session_description="demonstrate external files",
10396
identifier=str(uuid4()),
10497
session_start_time=start_time,
10598
)
10699
# Create the second file
107100
test_ts2 = TimeSeries(
108-
name='test_timeseries2',
101+
name="test_timeseries2",
109102
data=data,
110-
unit='SIunit',
103+
unit="SIunit",
111104
timestamps=timestamps,
112105
)
113106
nwbfile2.add_acquisition(test_ts2)
114107

115108
# Write the second file
116-
with NWBHDF5IO(filename2, 'w') as io:
109+
with NWBHDF5IO(filename2, "w") as io:
117110
io.write(nwbfile2)
118111

119112

@@ -128,7 +121,7 @@
128121

129122
# Create the first file
130123
nwbfile4 = NWBFile(
131-
session_description='demonstrate external files',
124+
session_description="demonstrate external files",
132125
identifier=str(uuid4()),
133126
session_start_time=start_time,
134127
)
@@ -141,9 +134,9 @@
141134
#
142135

143136
# Get the first timeseries
144-
io1 = NWBHDF5IO(filename1, 'r')
137+
io1 = NWBHDF5IO(filename1, "r")
145138
nwbfile1 = io1.read()
146-
timeseries_1 = nwbfile1.get_acquisition('test_timeseries1')
139+
timeseries_1 = nwbfile1.get_acquisition("test_timeseries1")
147140
timeseries_1_data = timeseries_1.data
148141

149142
####################
@@ -154,9 +147,9 @@
154147

155148
# Create a new timeseries that links to our data
156149
test_ts4 = TimeSeries(
157-
name='test_timeseries4',
158-
data=timeseries_1_data, # <-------
159-
unit='SIunit',
150+
name="test_timeseries4",
151+
data=timeseries_1_data, # <-------
152+
unit="SIunit",
160153
timestamps=timestamps,
161154
)
162155
nwbfile4.add_acquisition(test_ts4)
@@ -172,10 +165,9 @@
172165

173166
# Create another timeseries that links to the same data
174167
test_ts5 = TimeSeries(
175-
name='test_timeseries5',
176-
data=H5DataIO(data=timeseries_1_data, # <-------
177-
link_data=True), # <-------
178-
unit='SIunit',
168+
name="test_timeseries5",
169+
data=H5DataIO(data=timeseries_1_data, link_data=True), # <-------
170+
unit="SIunit",
179171
timestamps=timestamps,
180172
)
181173
nwbfile4.add_acquisition(test_ts5)
@@ -184,9 +176,9 @@
184176
# Step 4: Write the data
185177
# ^^^^^^^^^^^^^^^^^^^^^^^
186178
#
187-
with NWBHDF5IO(filename4, 'w') as io4:
188-
io4.write(nwbfile4,
189-
link_data=True) # <-------- Specify default behavior to link rather than copy data
179+
with NWBHDF5IO(filename4, "w") as io4:
180+
# Use link_data=True to specify default behavior to link rather than copy data
181+
io4.write(nwbfile4, link_data=True)
190182
io1.close()
191183

192184
#####################
@@ -223,14 +215,14 @@
223215
#
224216

225217
# Get the first timeseries
226-
io1 = NWBHDF5IO(filename1, 'r', manager=manager)
218+
io1 = NWBHDF5IO(filename1, "r", manager=manager)
227219
nwbfile1 = io1.read()
228-
timeseries_1 = nwbfile1.get_acquisition('test_timeseries1')
220+
timeseries_1 = nwbfile1.get_acquisition("test_timeseries1")
229221

230222
# Get the second timeseries
231-
io2 = NWBHDF5IO(filename2, 'r', manager=manager)
223+
io2 = NWBHDF5IO(filename2, "r", manager=manager)
232224
nwbfile2 = io2.read()
233-
timeseries_2 = nwbfile2.get_acquisition('test_timeseries2')
225+
timeseries_2 = nwbfile2.get_acquisition("test_timeseries2")
234226

235227
####################
236228
# Step 2: Add the container to another NWBFile
@@ -243,15 +235,15 @@
243235

244236
# Create a new NWBFile that links to the external timeseries
245237
nwbfile3 = NWBFile(
246-
session_description='demonstrate external files',
238+
session_description="demonstrate external files",
247239
identifier=str(uuid4()),
248240
session_start_time=start_time,
249241
)
250-
nwbfile3.add_acquisition(timeseries_1) # <--------
251-
nwbfile3.add_acquisition(timeseries_2) # <--------
242+
nwbfile3.add_acquisition(timeseries_1) # <--------
243+
nwbfile3.add_acquisition(timeseries_2) # <--------
252244

253245
# Write our third file that includes our two timeseries as external links
254-
with NWBHDF5IO(filename3, 'w', manager=manager) as io3:
246+
with NWBHDF5IO(filename3, "w", manager=manager) as io3:
255247
io3.write(nwbfile3)
256248
io1.close()
257249
io2.close()

0 commit comments

Comments
 (0)