bfio 2.4.9__tar.gz → 2.5.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: bfio
3
- Version: 2.4.9
3
+ Version: 2.5.0
4
4
  Summary: Simple reading and writing classes for tiled tiffs using Bio-Formats.
5
5
  Author-email: Nick Schaub <nick.schaub@nih.gov>, Sameeul Bashir Samee <sameeul.samee@axleinfo.com>
6
6
  Maintainer-email: Sameeul Bashir Samee <sameeul.samee@axleinfo.com>
@@ -37,8 +37,8 @@ Description-Content-Type: text/markdown
37
37
  License-File: LICENSE
38
38
  Requires-Dist: imagecodecs>=2021.2.26
39
39
  Requires-Dist: numpy
40
- Requires-Dist: ome-types>=0.6.2
41
- Requires-Dist: zarr<3,>=2.6.1
40
+ Requires-Dist: ome-types>=0.4.2
41
+ Requires-Dist: zarr>=3
42
42
  Requires-Dist: scyjava
43
43
  Requires-Dist: jpype1
44
44
  Requires-Dist: tifffile>=2022.8.12
@@ -49,7 +49,7 @@ Requires-Dist: pillow; extra == "dev"
49
49
  Requires-Dist: requests>=2.26.0; extra == "dev"
50
50
  Dynamic: license-file
51
51
 
52
- # **B**io**F**ormats **I**nput/**O**utput utility (bfio 2.4.9)
52
+ # **B**io**F**ormats **I**nput/**O**utput utility (bfio 2.5.0)
53
53
 
54
54
  [![Documentation Status](https://readthedocs.org/projects/bfio/badge/?version=latest)](https://bfio.readthedocs.io/en/latest/?badge=latest)
55
55
  [![PyPI](https://img.shields.io/pypi/v/bfio)](https://pypi.org/project/bfio/)
@@ -82,7 +82,7 @@ Docker containers with all necessary components are available (see
82
82
 
83
83
  ### Setting up Java
84
84
 
85
- **Note:** `bfio` can be used without Java, but only the `python` and `zarr`
85
+ **Note:** `bfio` can be used without Java, but only the `python`, `zarr`, `zarr3`, and `tensorstore`
86
86
  backends will be usable. This means only files in tiled OME Tiff or OME Zarr format can be
87
87
  read/written.
88
88
 
@@ -101,15 +101,15 @@ be installed using pip:
101
101
 
102
102
  ## Docker
103
103
 
104
- ### polusai/bfio:2.4.9
104
+ ### polusai/bfio:2.5.0
105
105
 
106
106
  Ubuntu based container with bfio and all dependencies (including Java).
107
107
 
108
- ### polusai/bfio:2.4.9-imagej
108
+ ### polusai/bfio:2.5.0-imagej
109
109
 
110
110
  Same as above, except comes with ImageJ and PyImageJ.
111
111
 
112
- ### polusai/bfio:2.4.9-tensorflow
112
+ ### polusai/bfio:2.5.0-tensorflow
113
113
 
114
114
  Tensorflow container with bfio installed.
115
115
 
@@ -1,4 +1,4 @@
1
- # **B**io**F**ormats **I**nput/**O**utput utility (bfio 2.4.9)
1
+ # **B**io**F**ormats **I**nput/**O**utput utility (bfio 2.5.0)
2
2
 
3
3
  [![Documentation Status](https://readthedocs.org/projects/bfio/badge/?version=latest)](https://bfio.readthedocs.io/en/latest/?badge=latest)
4
4
  [![PyPI](https://img.shields.io/pypi/v/bfio)](https://pypi.org/project/bfio/)
@@ -31,7 +31,7 @@ Docker containers with all necessary components are available (see
31
31
 
32
32
  ### Setting up Java
33
33
 
34
- **Note:** `bfio` can be used without Java, but only the `python` and `zarr`
34
+ **Note:** `bfio` can be used without Java, but only the `python`, `zarr`, `zarr3`, and `tensorstore`
35
35
  backends will be usable. This means only files in tiled OME Tiff or OME Zarr format can be
36
36
  read/written.
37
37
 
@@ -50,15 +50,15 @@ be installed using pip:
50
50
 
51
51
  ## Docker
52
52
 
53
- ### polusai/bfio:2.4.9
53
+ ### polusai/bfio:2.5.0
54
54
 
55
55
  Ubuntu based container with bfio and all dependencies (including Java).
56
56
 
57
- ### polusai/bfio:2.4.9-imagej
57
+ ### polusai/bfio:2.5.0-imagej
58
58
 
59
59
  Same as above, except comes with ImageJ and PyImageJ.
60
60
 
61
- ### polusai/bfio:2.4.9-tensorflow
61
+ ### polusai/bfio:2.5.0-tensorflow
62
62
 
63
63
  Tensorflow container with bfio installed.
64
64
 
@@ -6,8 +6,8 @@ requires = [
6
6
  "wheel",
7
7
  "imagecodecs>=2021.2.26",
8
8
  "numpy",
9
- "ome-types>=0.6.2",
10
- "zarr>=2.6.1,<3",
9
+ "ome-types>=0.4.2",
10
+ "zarr>=3",
11
11
  "scyjava",
12
12
  "jpype1",
13
13
  "tifffile>=2022.8.12",
@@ -22,8 +22,8 @@ dynamic = ["version"]
22
22
  dependencies = [
23
23
  "imagecodecs>=2021.2.26",
24
24
  "numpy",
25
- "ome-types>=0.6.2",
26
- "zarr>=2.6.1,<3",
25
+ "ome-types>=0.4.2",
26
+ "zarr>=3",
27
27
  "scyjava",
28
28
  "jpype1",
29
29
  "tifffile>=2022.8.12",
@@ -87,7 +87,7 @@ namespaces = false
87
87
 
88
88
 
89
89
  [tool.bumpversion]
90
- current_version = "2.4.9"
90
+ current_version = "2.5.0"
91
91
  commit = true
92
92
  tag = true
93
93
  commit_args = "--no-verify"
@@ -0,0 +1 @@
1
+ 2.5.0
@@ -16,11 +16,14 @@ import ome_types
16
16
  from tifffile import tifffile
17
17
  from xml.etree import ElementTree as ET
18
18
 
19
-
20
19
  # bfio internals
21
20
  from bfio import __version__ as version
22
21
  import bfio.base_classes
23
- from bfio.utils import start, clean_ome_xml_for_known_issues, pixels_per_cm
22
+ from bfio.utils import (
23
+ start,
24
+ clean_ome_xml_for_known_issues,
25
+ pixels_per_cm,
26
+ )
24
27
 
25
28
  logging.basicConfig(
26
29
  format="%(asctime)s - %(name)-8s - %(levelname)-8s - %(message)s",
@@ -1211,6 +1214,30 @@ try:
1211
1214
  import zarr
1212
1215
  from numcodecs import Blosc
1213
1216
 
1217
+ def _list_zarr_children(path, child_type="array"):
1218
+ """Filesystem-based fallback for enumerating zarr v2 store children.
1219
+
1220
+ In zarr-python v3, array_keys()/group_keys() may not reliably
1221
+ enumerate v2 format stores. This function checks subdirectories
1222
+ for .zarray (arrays) or .zgroup (groups) marker files.
1223
+
1224
+ Args:
1225
+ path: Path to the zarr group directory (str or Path).
1226
+ child_type: "array" to find arrays (.zarray), "group" to find
1227
+ groups (.zgroup).
1228
+
1229
+ Returns:
1230
+ Sorted list of child names.
1231
+ """
1232
+ p = Path(path)
1233
+ marker = ".zarray" if child_type == "array" else ".zgroup"
1234
+ children = []
1235
+ if p.is_dir():
1236
+ for child in p.iterdir():
1237
+ if child.is_dir() and (child / marker).exists():
1238
+ children.append(child.name)
1239
+ return sorted(children)
1240
+
1214
1241
  class ZarrReader(bfio.base_classes.AbstractReader):
1215
1242
  logger = logging.getLogger("bfio.backends.ZarrReader")
1216
1243
 
@@ -1224,35 +1251,41 @@ try:
1224
1251
  self._root = zarr.open(
1225
1252
  str(self.frontend._file_path.resolve()), mode="r"
1226
1253
  )
1227
- except zarr.errors.PathNotFoundError:
1254
+ except (FileNotFoundError, KeyError):
1228
1255
  # a workaround for pre-compute slide output directory structure
1229
1256
  data_zarr_path = str(self.frontend._file_path.resolve()) + "/data.zarr"
1230
1257
  self._root = zarr.open(data_zarr_path, mode="r")
1231
1258
 
1259
+ store_path = str(self.frontend._file_path.resolve())
1260
+
1232
1261
  if self.frontend.level is None:
1233
- if isinstance(self._root, zarr.core.Array):
1262
+ if isinstance(self._root, zarr.Array):
1234
1263
  self._rdr = self._root
1235
- elif isinstance(self._root, zarr.hierarchy.Group):
1264
+ elif isinstance(self._root, zarr.Group):
1236
1265
  # the top level is a group, check if this has any arrays
1237
- num_arrays = len(sorted(self._root.array_keys()))
1238
- if num_arrays > 0:
1239
- self._rdr = self._root[next(self._root.array_keys())]
1266
+ array_keys = _list_zarr_children(store_path, "array")
1267
+ if len(array_keys) > 0:
1268
+ self._rdr = self._root[array_keys[0]]
1240
1269
  else:
1241
1270
  # need to go one more level
1242
- self._root = self._root[next(self._root.group_keys())]
1243
- self._rdr = self._root[next(self._root.array_keys())]
1271
+ group_keys = _list_zarr_children(store_path, "group")
1272
+ self._root = self._root[group_keys[0]]
1273
+ sub_path = str(Path(store_path) / group_keys[0])
1274
+ sub_array_keys = _list_zarr_children(sub_path, "array")
1275
+ self._rdr = self._root[sub_array_keys[0]]
1244
1276
  else:
1245
1277
  pass
1246
1278
  else:
1247
- if isinstance(self._root, zarr.core.Array):
1279
+ if isinstance(self._root, zarr.Array):
1248
1280
  self.close()
1249
1281
  raise ValueError(
1250
1282
  "Level is specified but the zarr file does not contain "
1251
1283
  + "multiple resoulutions."
1252
1284
  )
1253
- elif isinstance(self._root, zarr.hierarchy.Group):
1254
- if len(sorted(self._root.array_keys())) > self.frontend.level:
1255
- self._rdr = self._root[self.frontend.level]
1285
+ elif isinstance(self._root, zarr.Group):
1286
+ array_keys = _list_zarr_children(store_path, "array")
1287
+ if len(array_keys) > self.frontend.level:
1288
+ self._rdr = self._root[str(self.frontend.level)]
1256
1289
  else:
1257
1290
  raise ValueError(
1258
1291
  "The zarr file does not contain resolution "
@@ -1279,7 +1312,7 @@ try:
1279
1312
  axes_metadata = self._root.attrs["multiscales"][data_key]["axes"]
1280
1313
  for axes in axes_metadata:
1281
1314
  self._axes_list.append(axes["name"])
1282
- except AttributeError or KeyError:
1315
+ except (AttributeError, KeyError):
1283
1316
  self.logger.warning(
1284
1317
  "Unable to find multiscales metadata. Z, C and T "
1285
1318
  + "dimensions might be incorrect."
@@ -1457,7 +1490,9 @@ try:
1457
1490
  if self.frontend.append is True:
1458
1491
  mode = "a"
1459
1492
  self._root = zarr.open_group(
1460
- store=str(self.frontend._file_path.resolve()), mode=mode
1493
+ store=str(self.frontend._file_path.resolve()),
1494
+ mode=mode,
1495
+ zarr_format=2,
1461
1496
  )
1462
1497
 
1463
1498
  # Create the metadata
@@ -1482,14 +1517,16 @@ try:
1482
1517
  "metadata": {"method": "mean"},
1483
1518
  }
1484
1519
  ]
1520
+
1521
+ store_path = str(self.frontend._file_path.resolve())
1485
1522
  if (
1486
1523
  self.frontend.append is True
1487
- and len(sorted(self._root.array_keys())) > 0
1524
+ and len(_list_zarr_children(store_path, "array")) > 0
1488
1525
  ):
1489
1526
  writer = self._root["0"]
1490
1527
  else:
1491
- writer = self._root.zeros(
1492
- "0",
1528
+ writer = self._root.create_array(
1529
+ name="0",
1493
1530
  shape=shape,
1494
1531
  chunks=(
1495
1532
  1,
@@ -1499,8 +1536,8 @@ try:
1499
1536
  self.frontend._TILE_SIZE,
1500
1537
  ),
1501
1538
  dtype=self.frontend.dtype,
1502
- compressor=compressor,
1503
- dimension_separator="/",
1539
+ compressors=compressor,
1540
+ fill_value=0,
1504
1541
  )
1505
1542
 
1506
1543
  # This is recommended to do for cloud storage to increase read/write
@@ -1552,6 +1589,233 @@ try:
1552
1589
  def close(self):
1553
1590
  pass
1554
1591
 
1592
+ class Zarr3Reader(ZarrReader):
1593
+ """Reader for zarr v3 format stores using zarr-python v3 API."""
1594
+
1595
+ logger = logging.getLogger("bfio.backends.Zarr3Reader")
1596
+
1597
+ def __init__(self, frontend):
1598
+ # Call AbstractReader.__init__ directly, skip ZarrReader.__init__
1599
+ bfio.base_classes.AbstractReader.__init__(self, frontend)
1600
+
1601
+ self.logger.debug("__init__(): Initializing _rdr (zarr v3)...")
1602
+ self.logger.debug(f"Level is {self.frontend.level}")
1603
+
1604
+ try:
1605
+ self._root = zarr.open(
1606
+ str(self.frontend._file_path.resolve()), mode="r"
1607
+ )
1608
+ except (FileNotFoundError, KeyError):
1609
+ data_zarr_path = str(self.frontend._file_path.resolve()) + "/data.zarr"
1610
+ self._root = zarr.open(data_zarr_path, mode="r")
1611
+
1612
+ if self.frontend.level is None:
1613
+ if isinstance(self._root, zarr.Array):
1614
+ self._rdr = self._root
1615
+ elif isinstance(self._root, zarr.Group):
1616
+ # Use native v3 group enumeration
1617
+ array_names = sorted(
1618
+ k for k, v in self._root.members() if isinstance(v, zarr.Array)
1619
+ )
1620
+ if len(array_names) > 0:
1621
+ self._rdr = self._root[array_names[0]]
1622
+ else:
1623
+ group_names = sorted(
1624
+ k
1625
+ for k, v in self._root.members()
1626
+ if isinstance(v, zarr.Group)
1627
+ )
1628
+ self._root = self._root[group_names[0]]
1629
+ array_names = sorted(
1630
+ k
1631
+ for k, v in self._root.members()
1632
+ if isinstance(v, zarr.Array)
1633
+ )
1634
+ self._rdr = self._root[array_names[0]]
1635
+ else:
1636
+ pass
1637
+ else:
1638
+ if isinstance(self._root, zarr.Array):
1639
+ self.close()
1640
+ raise ValueError(
1641
+ "Level is specified but the zarr file does not contain "
1642
+ + "multiple resoulutions."
1643
+ )
1644
+ elif isinstance(self._root, zarr.Group):
1645
+ array_names = sorted(
1646
+ k for k, v in self._root.members() if isinstance(v, zarr.Array)
1647
+ )
1648
+ if len(array_names) > self.frontend.level:
1649
+ self._rdr = self._root[str(self.frontend.level)]
1650
+ else:
1651
+ raise ValueError(
1652
+ "The zarr file does not contain resolution "
1653
+ + "level {}.".format(self.frontend.level)
1654
+ )
1655
+ else:
1656
+ raise ValueError(
1657
+ "The zarr file does not contain resolution level {}.".format(
1658
+ self.frontend.level
1659
+ )
1660
+ )
1661
+
1662
+ self._axes_list = []
1663
+
1664
+ def _get_axis_info(self):
1665
+ shape_len = len(self._rdr.shape)
1666
+ if shape_len == 5:
1667
+ self._axes_list = ["t", "c", "z", "y", "x"]
1668
+ else:
1669
+ try:
1670
+ # OME-NGFF 0.5: multiscales is under attrs["ome"]["multiscales"]
1671
+ ome_meta = self._root.attrs["ome"]
1672
+ axes_metadata = ome_meta["multiscales"][0]["axes"]
1673
+ for axes in axes_metadata:
1674
+ self._axes_list.append(axes["name"])
1675
+ except (AttributeError, KeyError):
1676
+ # Fall back to v2 location (attrs["multiscales"])
1677
+ try:
1678
+ axes_metadata = self._root.attrs["multiscales"][0]["axes"]
1679
+ for axes in axes_metadata:
1680
+ self._axes_list.append(axes["name"])
1681
+ except (AttributeError, KeyError):
1682
+ self.logger.warning(
1683
+ "Unable to find multiscales metadata. Z, C and T "
1684
+ + "dimensions might be incorrect."
1685
+ )
1686
+ if shape_len == 4:
1687
+ self._axes_list = ["c", "z", "y", "x"]
1688
+ elif shape_len == 3:
1689
+ self._axes_list = ["z", "y", "x"]
1690
+ elif shape_len == 2:
1691
+ self._axes_list = ["y", "x"]
1692
+
1693
+ def read_metadata(self):
1694
+ self.logger.debug("read_metadata(): Reading metadata (v3)...")
1695
+ # First try OME metadata (same as v2)
1696
+ metadata_path = self.frontend._file_path.joinpath("OME").joinpath(
1697
+ "METADATA.ome.xml"
1698
+ )
1699
+ if metadata_path.exists():
1700
+ if self._metadata is None:
1701
+ with open(metadata_path) as fr:
1702
+ metadata = fr.read()
1703
+
1704
+ try:
1705
+ self._metadata = ome_types.from_xml(metadata, validate=False)
1706
+ except ET.ParseError:
1707
+ if self.frontend.clean_metadata:
1708
+ cleaned = clean_ome_xml_for_known_issues(metadata)
1709
+ self._metadata = ome_types.from_xml(cleaned, validate=False)
1710
+ self.logger.warning(
1711
+ "read_metadata(): OME XML required reformatting."
1712
+ )
1713
+ else:
1714
+ raise
1715
+
1716
+ if self.frontend.level is not None:
1717
+ self._metadata.images[0].pixels.size_x = self._rdr.shape[-1]
1718
+ self._metadata.images[0].pixels.size_y = self._rdr.shape[-2]
1719
+
1720
+ return self._metadata
1721
+
1722
+ # Fall back to constructing metadata from array shape
1723
+ return super().read_metadata()
1724
+
1725
+ class Zarr3Writer(ZarrWriter):
1726
+ """Writer for zarr v3 format stores using zarr-python v3 API."""
1727
+
1728
+ logger = logging.getLogger("bfio.backends.Zarr3Writer")
1729
+
1730
+ def __init__(self, frontend):
1731
+ super().__init__(frontend)
1732
+
1733
+ def _init_writer(self):
1734
+ """Initialize file writing for zarr v3 format."""
1735
+ if self.frontend.append is False:
1736
+ if self.frontend._file_path.exists():
1737
+ shutil.rmtree(self.frontend._file_path)
1738
+
1739
+ shape = (
1740
+ self.frontend.T,
1741
+ self.frontend.C,
1742
+ self.frontend.Z,
1743
+ self.frontend.Y,
1744
+ self.frontend.X,
1745
+ )
1746
+
1747
+ mode = "w"
1748
+ if self.frontend.append is True:
1749
+ mode = "a"
1750
+
1751
+ self._root = zarr.open_group(
1752
+ store=str(self.frontend._file_path.resolve()),
1753
+ mode=mode,
1754
+ zarr_format=3,
1755
+ )
1756
+
1757
+ # Create the metadata
1758
+ metadata_path = (
1759
+ Path(self.frontend._file_path)
1760
+ .joinpath("OME")
1761
+ .joinpath("METADATA.ome.xml")
1762
+ )
1763
+
1764
+ if self.frontend.append is False or (
1765
+ self.frontend.append is True and metadata_path.exists() is False
1766
+ ):
1767
+ metadata_path.parent.mkdir(parents=True, exist_ok=True)
1768
+ with open(metadata_path, "w") as fw:
1769
+ fw.write(str(self.frontend._metadata.to_xml()))
1770
+
1771
+ self._root.attrs["ome"] = {
1772
+ "version": "0.5",
1773
+ "multiscales": [
1774
+ {
1775
+ "name": self.frontend._file_path.name,
1776
+ "axes": [
1777
+ {"name": "t", "type": "time"},
1778
+ {"name": "c", "type": "channel"},
1779
+ {"name": "z", "type": "space"},
1780
+ {"name": "y", "type": "space"},
1781
+ {"name": "x", "type": "space"},
1782
+ ],
1783
+ "datasets": [{"path": "0"}],
1784
+ "metadata": {"method": "mean"},
1785
+ }
1786
+ ],
1787
+ }
1788
+
1789
+ # Check for existing arrays when appending
1790
+ if self.frontend.append is True:
1791
+ existing_arrays = sorted(
1792
+ k for k, v in self._root.members() if isinstance(v, zarr.Array)
1793
+ )
1794
+ if len(existing_arrays) > 0:
1795
+ writer = self._root["0"]
1796
+ self._writer = writer
1797
+ return
1798
+
1799
+ writer = self._root.create_array(
1800
+ name="0",
1801
+ shape=shape,
1802
+ chunks=(
1803
+ 1,
1804
+ 1,
1805
+ 1,
1806
+ self.frontend._TILE_SIZE,
1807
+ self.frontend._TILE_SIZE,
1808
+ ),
1809
+ dtype=self.frontend.dtype,
1810
+ serializer=zarr.codecs.BytesCodec(),
1811
+ compressors=zarr.codecs.ZstdCodec(level=1),
1812
+ fill_value=0,
1813
+ )
1814
+
1815
+ # Skip zarr.consolidate_metadata() — not part of v3 spec
1816
+
1817
+ self._writer = writer
1818
+
1555
1819
  except ModuleNotFoundError:
1556
1820
  logger.info(
1557
1821
  "Zarr backend is not available. This could be due to a "
@@ -1569,3 +1833,11 @@ except ModuleNotFoundError:
1569
1833
  raise ImportError(
1570
1834
  "ZarrWriter class unavailable. Could not import" + " zarr."
1571
1835
  )
1836
+
1837
+ class Zarr3Reader(bfio.base_classes.AbstractReader):
1838
+ def __init__(self, frontend):
1839
+ raise ImportError("Zarr3Reader class unavailable. Could not import zarr.")
1840
+
1841
+ class Zarr3Writer(bfio.base_classes.AbstractWriter):
1842
+ def __init__(self, frontend):
1843
+ raise ImportError("Zarr3Writer class unavailable. Could not import zarr.")
@@ -107,7 +107,7 @@ class BioBase(object, metaclass=abc.ABCMeta):
107
107
  # Internally, keep the file_path as a Path object
108
108
  if isinstance(file_path, str):
109
109
  file_path = Path(file_path)
110
- self._file_path = file_path
110
+ self._file_path = file_path.expanduser().resolve()
111
111
 
112
112
  self._max_workers = (
113
113
  max_workers