diff --git a/Orange/data/io.py b/Orange/data/io.py index eb4ab8722e9..121ad40349a 100644 --- a/Orange/data/io.py +++ b/Orange/data/io.py @@ -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 [] @@ -560,32 +558,32 @@ def read_hdf5(name, as_str=False): return f[name] return None - 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 + + 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 - 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