Skip to content

Commit b91b6b0

Browse files
committed
fix(utils): add h5 reading func to FileHandler
1 parent a0b7599 commit b91b6b0

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

cellseg_models_pytorch/utils/file_manager.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
)
1717
from .multiproc import run_pool
1818

19+
try:
20+
import tables as tb
21+
22+
_has_tb = True
23+
except ModuleNotFoundError:
24+
_has_tb = False
25+
1926

2027
class FileHandler:
2128
"""Class for handling file I/O."""
@@ -227,6 +234,128 @@ def get_gson(
227234

228235
return geo_objs
229236

237+
@staticmethod
238+
def read_h5_patch(
239+
path: Union[Path, str],
240+
ix: int,
241+
return_im: bool = True,
242+
return_inst: bool = True,
243+
return_type: bool = True,
244+
return_sem: bool = False,
245+
return_name: bool = False,
246+
return_nitems: bool = False,
247+
return_all_names: bool = False,
248+
) -> Dict[str, np.ndarray]:
249+
"""Read img & mask patches at index `ix` from a hdf5 db.
250+
251+
Parameters
252+
----------
253+
path : Path or str
254+
Path to the h5-db.
255+
ix : int
256+
Index for the hdf5 db-arrays.
257+
return_im : bool, default=True
258+
If True, returns an image. (If the db contains these.)
259+
return_inst : bool, default=True
260+
If True, returns a instance labelled mask. (If the db contains these.)
261+
return_type : bool, default=True
262+
If True, returns a type mask. (If the db contains these.)
263+
return_sem : bool, default=False
264+
If True, returns a semantic mask, (If the db contains these.)
265+
return_name : bool, default=False
266+
If True, returns a name for the patch, (If the db contains these.)
267+
return_nitems : bool, default=False
268+
If True, returns the number of items in the db.
269+
return_all_names : bool, default=False
270+
If True, returns all the names in the db.
271+
272+
Returns
273+
-------
274+
Dict[str, np.ndarray]:
275+
A Dict of numpy matrices. Img shape: (H, W, 3), mask shapes: (H, W).
276+
keys of the dict are: "im", "inst", "type", "sem"
277+
278+
Raises
279+
------
280+
IOError: If a mask that does not exist in the db is being read.
281+
"""
282+
if not _has_tb:
283+
raise ModuleNotFoundError(
284+
"`FileHandler.read_h5_patch` method requires pytables library. "
285+
"Install with `pip install tables`."
286+
)
287+
288+
path = Path(path)
289+
with tb.open_file(path.as_posix(), "r") as h5:
290+
out = {}
291+
292+
if return_im:
293+
try:
294+
out["image"] = h5.root.imgs[ix, ...]
295+
except Exception:
296+
raise IOError(
297+
"The HDF5 database does not contain images. Try "
298+
"setting `return_im=False`"
299+
)
300+
301+
if return_inst:
302+
try:
303+
out["inst"] = h5.root.insts[ix, ...]
304+
except Exception:
305+
raise IOError(
306+
"The HDF5 database does not contain instance labelled masks. "
307+
"Try setting `return_inst=False`"
308+
)
309+
310+
if return_type:
311+
try:
312+
out["type"] = h5.root.types[ix, ...]
313+
except Exception:
314+
raise IOError(
315+
"The HDF5 database does not contain type masks. Try setting "
316+
"`return_type = False` "
317+
)
318+
319+
if return_sem:
320+
try:
321+
out["sem"] = h5.root.areas[ix, ...]
322+
except Exception:
323+
raise IOError(
324+
"The HDF5 database does not contain semantic masks. Try "
325+
"setting `return_sem = False`"
326+
)
327+
328+
if return_name:
329+
try:
330+
fn = h5.root.fnames[ix]
331+
out["name"] = Path(fn.decode("UTF-8"))
332+
except Exception:
333+
raise IOError(
334+
"The HDF5 database does not contain patch names. Try "
335+
"setting `return_name = False`"
336+
)
337+
338+
if return_nitems:
339+
try:
340+
out["nitems"] = h5.root._v_attrs.n_items
341+
except Exception:
342+
raise IOError(
343+
"The HDF5 database does not contain attribute ``nitems. Try "
344+
"setting `return_nitems = False`"
345+
)
346+
347+
if return_all_names:
348+
try:
349+
names = h5.root.fnames[:]
350+
out["names"] = [Path(n.decode("UTF-8")) for n in names]
351+
except Exception:
352+
raise IOError(
353+
"The HDF5 database does not contain patch names. Try "
354+
"setting `return_all_names = False`"
355+
)
356+
357+
return out
358+
230359
@staticmethod
231360
def write_mat(
232361
fname: Union[str, Path],
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Fixes
2+
3+
- Add long missing h5 reading utility function to `FileHandler`

0 commit comments

Comments
 (0)