|
16 | 16 | )
|
17 | 17 | from .multiproc import run_pool
|
18 | 18 |
|
| 19 | +try: |
| 20 | + import tables as tb |
| 21 | + |
| 22 | + _has_tb = True |
| 23 | +except ModuleNotFoundError: |
| 24 | + _has_tb = False |
| 25 | + |
19 | 26 |
|
20 | 27 | class FileHandler:
|
21 | 28 | """Class for handling file I/O."""
|
@@ -227,6 +234,128 @@ def get_gson(
|
227 | 234 |
|
228 | 235 | return geo_objs
|
229 | 236 |
|
| 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 | + |
230 | 359 | @staticmethod
|
231 | 360 | def write_mat(
|
232 | 361 | fname: Union[str, Path],
|
|
0 commit comments