Skip to content

Commit f13a5c2

Browse files
authored
formatting fixes (#15)
1 parent c734251 commit f13a5c2

6 files changed

Lines changed: 50 additions & 54 deletions

File tree

swmr_tools/datasource.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
logger = logging.getLogger(__name__)
88

9+
910
class DataSource:
1011
"""Iterator for returning dataset frames for any number of datasets. This
1112
class is largely a wrapper around the KeyFollower and FrameReader classes.
@@ -87,45 +88,42 @@ def create_dataset(self, data, fh, path):
8788

8889
scan_max = self.kf.maxshape
8990
maxshape = scan_max + data.shape
90-
shape = ([1] * len(scan_max) + list(data.shape))
91+
shape = [1] * len(scan_max) + list(data.shape)
9192
r = data.reshape(shape)
92-
return fh.create_dataset(path, data=r, maxshape = maxshape)
93+
return fh.create_dataset(path, data=r, maxshape=maxshape)
9394

9495
def append_data(self, data, slice_metadata, dataset):
95-
ds = tuple(slice(0,s,1) for s in data.shape)
96+
ds = tuple(slice(0, s, 1) for s in data.shape)
9697
fullslice = slice_metadata + ds
9798
current = dataset.shape
98-
new_shape = tuple(max(s.stop,c) for (s,c) in zip(fullslice,current))
99-
if (np.any(new_shape > current)):
99+
new_shape = tuple(max(s.stop, c) for (s, c) in zip(fullslice, current))
100+
if np.any(new_shape > current):
100101
dataset.resize(new_shape)
101102
dataset[fullslice] = data
102103

103104
@staticmethod
104-
def check_file_readable(path, datasets,timeout = 10, retrys = 5):
105+
def check_file_readable(path, datasets, timeout=10, retrys=5):
105106
start = time.time()
106107
dif = time.time() - start
107108

108109
while dif < timeout:
109110
try:
110-
with h5py.File(path,'r',libver = "latest", swmr = True) as fh:
111+
with h5py.File(path, "r", libver="latest", swmr=True) as fh:
111112
for d in datasets:
112-
tmp = fh[d]
113+
fh[d]
113114
return True
114115

115116
except Exception as e:
116117
logger.debug("Reading failed, retrying " + str(e))
117-
time.sleep(timeout/retrys)
118+
time.sleep(timeout / retrys)
118119
dif = time.time() - start
119120

120121
logger.error("Could not read file " + path)
121122
return False
122123

123124

124-
125-
126125
class SliceDict(dict):
127-
"""Dictionary with attributes for the slice metadata and maxshape of the scan
128-
"""
126+
"""Dictionary with attributes for the slice metadata and maxshape of the scan"""
129127

130128
def __init__(self, *args, **kw):
131129
super(SliceDict, self).__init__(*args, **kw)
@@ -203,10 +201,10 @@ def read_frame(self, index):
203201
shape = ds.shape
204202

205203
try:
206-
#might fail if dataset is cached
204+
# might fail if dataset is cached
207205
pos, slices, shape_slice = self.get_pos(index, shape)
208206
except ValueError:
209-
#refresh dataset and try again
207+
# refresh dataset and try again
210208
if hasattr(ds, "refresh"):
211209
ds.refresh()
212210

@@ -232,4 +230,3 @@ def get_pos(self, index, shape):
232230
pos = np.unravel_index(index, scan_shape)
233231

234232
return pos, slices, shape_slice
235-

swmr_tools/keyfollower.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class KeyFollower:
2626
2727
finished_dataset: string (optional)
2828
Path to a scalar hdf5 dataset which is zero when the file is being
29-
written to and non-zero when the file is complete. Used to stop
29+
written to and non-zero when the file is complete. Used to stop
3030
the iterator without waiting for the timeout
3131
3232
@@ -86,7 +86,7 @@ def check_datasets(self):
8686
if rank != -1 and rank != r:
8787
raise RuntimeError("Key datasets must have the same rank!")
8888

89-
if (self.maxshape is None):
89+
if self.maxshape is None:
9090
self.maxshape = tmp.maxshape[:rank]
9191
else:
9292
if np.all(self.maxshape != tmp.maxshape[:rank]):
@@ -171,7 +171,7 @@ def _is_next(self):
171171
new_max = np.argmax(merged == 0) - 1
172172

173173
if new_max < 0 and merged[0] != 0:
174-
#all keys non zero
174+
# all keys non zero
175175
new_max = merged.size - 1
176176

177177
if self.current_max == new_max:
@@ -211,9 +211,9 @@ def _check_finished_dataset(self):
211211
if self._prelim_finished_check and finished:
212212
return True
213213

214-
#go through the timeout loop once more
215-
#in case finish is flushed slightly before
216-
#the last of the keys
214+
# go through the timeout loop once more
215+
# in case finish is flushed slightly before
216+
# the last of the keys
217217
if finished:
218218
self._prelim_finished_check = True
219219

tests/test_DataSource.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ def test_iterates_complete_dataset():
2424
d = dset["data/complete"]
2525
assert dset.maxshape == [10]
2626
assert dset.index == val
27-
assert dset.slice_metadata == (slice(val,val+1,None),)
27+
assert dset.slice_metadata == (slice(val, val + 1, None),)
2828
print(dset.slice_metadata)
2929
assert d == val
3030
val = val + 1
31-

tests/test_KeyFollower.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44

55
def test_first_frame():
66

7-
key_paths = ["k1", "k2", "k3"]
8-
shape=[10]
9-
7+
key_paths = ["k1", "k2", "k3"]
8+
shape = [10]
109

1110
k1 = utils.make_mock(shape)
1211
k2 = utils.make_mock(shape)
1312
k3 = utils.make_mock(shape)
1413

15-
16-
f = {"k1": k1, "k2" : k2, "k3": k3}
14+
f = {"k1": k1, "k2": k2, "k3": k3}
1715
kf = KeyFollower(f, key_paths, timeout=0.1)
1816
kf.check_datasets()
1917

@@ -46,7 +44,6 @@ def test_first_frame():
4644

4745
assert current_key == 0
4846

49-
5047
kf.reset()
5148

5249
k1.dataset[1] = 1
@@ -70,14 +67,13 @@ def test_first_frame():
7067

7168
def test_first_frame_jagged():
7269

73-
key_paths = ["k1", "k2", "k3"]
70+
key_paths = ["k1", "k2", "k3"]
7471

7572
k1 = utils.make_mock([2])
7673
k2 = utils.make_mock([2])
77-
k3 = utils.make_mock([1],maxshape=[2])
74+
k3 = utils.make_mock([1], maxshape=[2])
7875

79-
80-
f = {"k1": k1, "k2" : k2, "k3": k3}
76+
f = {"k1": k1, "k2": k2, "k3": k3}
8177
kf = KeyFollower(f, key_paths, timeout=0.1)
8278
kf.check_datasets()
8379

@@ -111,6 +107,7 @@ def test_first_frame_jagged():
111107

112108
assert current_key == 0
113109

110+
114111
def test_iterates_complete_dataset():
115112

116113
key_paths = ["complete"]

tests/test_using_h5py.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import h5py
22
import numpy as np
33
import time
4-
import os
54
import multiprocessing as mp
65
from swmr_tools import KeyFollower, DataSource
76
from functools import reduce
@@ -31,6 +30,7 @@ def test_multiple_keys(tmp_path):
3130

3231
assert current_key == 5
3332

33+
3434
def test_complete_keys(tmp_path):
3535

3636
f = str(tmp_path / "f.h5")
@@ -54,6 +54,7 @@ def test_complete_keys(tmp_path):
5454

5555
assert current_key == 10
5656

57+
5758
def test_data_read(tmp_path):
5859

5960
f = str(tmp_path / "f.h5")
@@ -74,6 +75,7 @@ def test_data_read(tmp_path):
7475
assert np.all(d == base + (20 * count))
7576
count = count + 1
7677

78+
7779
def test_use_case_example(tmp_path):
7880

7981
f = str(tmp_path / "f.h5")
@@ -94,32 +96,33 @@ def test_use_case_example(tmp_path):
9496
for dset in df:
9597
d = dset["/data"]
9698
d = d.squeeze()
97-
r = d.sum(axis = 1)
98-
assert dset.maxshape == (2,3)
99+
r = d.sum(axis=1)
100+
assert dset.maxshape == (2, 3)
99101

100102
if output is None:
101-
output = df.create_dataset(r,oh,output_path)
103+
output = df.create_dataset(r, oh, output_path)
102104
else:
103-
df.append_data(r,dset.slice_metadata,output)
105+
df.append_data(r, dset.slice_metadata, output)
104106

105107
with h5py.File(o, "r") as oh:
106108
out = oh["/result"]
107-
assert out.shape == (2,3,4)
108-
assert out.maxshape == (2,3,4)
109+
assert out.shape == (2, 3, 4)
110+
assert out.maxshape == (2, 3, 4)
109111
print(out[...])
110-
assert 119+118+117+116+115 == out[1,2,3]
111-
assert out[0,1,0] != 0
112+
assert 119 + 118 + 117 + 116 + 115 == out[1, 2, 3]
113+
assert out[0, 1, 0] != 0
114+
112115

113116
def test_mock_scan(tmp_path):
114117
f = str(tmp_path / "scan.h5")
115118

116-
mp.set_start_method('spawn')
117-
p = mp.Process(target=mock_scan, args = (f,))
119+
mp.set_start_method("spawn")
120+
p = mp.Process(target=mock_scan, args=(f,))
118121
p.start()
119122

120-
DataSource.check_file_readable(f, ["/data","/key"], timeout = 5)
123+
DataSource.check_file_readable(f, ["/data", "/key"], timeout=5)
121124

122-
with h5py.File(f, "r", libver = "latest", swmr = True) as fh:
125+
with h5py.File(f, "r", libver="latest", swmr=True) as fh:
123126

124127
data_paths = ["/data"]
125128
key_paths = ["/key"]
@@ -130,11 +133,12 @@ def test_mock_scan(tmp_path):
130133
assert p.is_alive()
131134
for dset in df:
132135
d = dset["/data"]
133-
assert d[0,0,0].item() == count
136+
assert d[0, 0, 0].item() == count
134137
count = count + 1
135138

136139
p.join()
137140

141+
138142
def create_test_file(path):
139143

140144
with h5py.File(path, "w") as fh:
@@ -149,7 +153,7 @@ def create_test_file(path):
149153

150154
def mock_scan(path):
151155

152-
with h5py.File(path, "w", libver = "latest") as fh:
156+
with h5py.File(path, "w", libver="latest") as fh:
153157
maxn = 10
154158
maxshape = (maxn, 9, 10)
155159
shape = (1, 9, 10)
@@ -161,14 +165,13 @@ def mock_scan(path):
161165
ks = fh.create_dataset("key", data=k, maxshape=(20,))
162166
fh.swmr_mode = True
163167
for i in range(maxn):
164-
s = (i+1, 9, 10)
168+
s = (i + 1, 9, 10)
165169
if i != 0:
166170
ds.resize(s)
167-
ks.resize((i+1,))
168-
ds[i,:,:] = np.ones(shape) + i
171+
ks.resize((i + 1,))
172+
ds[i, :, :] = np.ones(shape) + i
169173
ds.flush()
170174
ks[i] = 1
171175
ks.flush()
172176
print("flush " + str(i))
173177
time.sleep(1)
174-

tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import numpy as np
33

44

5-
def make_mock(shape=[5, 10, 1, 1],maxshape=None):
5+
def make_mock(shape=[5, 10, 1, 1], maxshape=None):
66

77
mds = Mock()
88
mds.dataset = np.zeros(shape)

0 commit comments

Comments
 (0)