Skip to content

Commit

Permalink
io: use context manager for HDF5 reading
Browse files Browse the repository at this point in the history
  • Loading branch information
stuart-cls committed May 2, 2024
1 parent 88788c1 commit 63c71b3
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions Orange/data/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,6 @@ class HDF5Reader(FileFormat):
SUPPORT_SPARSE_DATA = False

def read(self):
h5file = f = h5py.File(self.filename, "r")

def read_domain(sub):
d = f['domain']
subdomain = d[sub].asstr() if sub in d else []
Expand All @@ -560,32 +558,32 @@ def read_hdf5(name, as_str=False):
return f[name]
return None

Check warning on line 559 in Orange/data/io.py

View check run for this annotation

Codecov / codecov/patch

Orange/data/io.py#L559

Added line #L559 was not covered by tests

try:
assert f.attrs['creator'] == "Orange"
except KeyError:
assert 'domain' in f

domain = Domain(*[[make_var(*args) for args in read_domain(subdomain)]
for subdomain in ['attributes', 'class_vars', 'metas']])

X = read_hdf5("X")
Y = read_hdf5("Y")


if len(domain.metas) > 1:
metas = np.hstack([read_hdf5(f'metas/{i}',
isinstance(attr, StringVariable))
for i, attr in enumerate(domain.metas)])
elif len(domain.metas) == 1:
metas = read_hdf5('metas/0',
isinstance(domain.metas[0], StringVariable)
)
else:
metas = None
with h5py.File(self.filename, "r") as f:
try:
assert f.attrs['creator'] == "Orange"
except KeyError:
assert 'domain' in f

Check warning on line 565 in Orange/data/io.py

View check run for this annotation

Codecov / codecov/patch

Orange/data/io.py#L564-L565

Added lines #L564 - L565 were not covered by tests

domain = Domain(*[[make_var(*args) for args in read_domain(subdomain)]
for subdomain in ['attributes', 'class_vars', 'metas']])

X = read_hdf5("X")
Y = read_hdf5("Y")

if len(domain.metas) > 1:
metas = np.hstack([read_hdf5(f'metas/{i}',

Check warning on line 574 in Orange/data/io.py

View check run for this annotation

Codecov / codecov/patch

Orange/data/io.py#L574

Added line #L574 was not covered by tests
isinstance(attr, StringVariable))
for i, attr in enumerate(domain.metas)])
elif len(domain.metas) == 1:
metas = read_hdf5('metas/0',
isinstance(domain.metas[0], StringVariable)
)
else:
metas = None

table = Table.from_numpy(domain, X, Y, metas)
if isinstance(self.filename, str):
table.name = path.splitext(path.split(self.filename)[-1])[0]
table = Table.from_numpy(domain, X, Y, metas)
if isinstance(self.filename, str):
table.name = path.splitext(path.split(self.filename)[-1])[0]
self.set_table_metadata(self.filename, table)
return table

Expand Down

0 comments on commit 63c71b3

Please sign in to comment.