large-image-source-zarr 1.35.8.dev16__tar.gz → 1.35.8.dev18__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.
Files changed (17) hide show
  1. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/PKG-INFO +7 -5
  2. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr/__init__.py +243 -75
  3. large_image_source_zarr-1.35.8.dev18/large_image_source_zarr/zarr3.py +59 -0
  4. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr.egg-info/PKG-INFO +7 -5
  5. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr.egg-info/SOURCES.txt +1 -0
  6. large_image_source_zarr-1.35.8.dev18/large_image_source_zarr.egg-info/requires.txt +17 -0
  7. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr.egg-info/scm_file_list.json +1 -0
  8. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/setup.py +6 -3
  9. large_image_source_zarr-1.35.8.dev16/large_image_source_zarr.egg-info/requires.txt +0 -11
  10. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/LICENSE +0 -0
  11. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/README.rst +0 -0
  12. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr/girder_source.py +0 -0
  13. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr.egg-info/dependency_links.txt +0 -0
  14. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr.egg-info/entry_points.txt +0 -0
  15. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/large_image_source_zarr.egg-info/top_level.txt +0 -0
  16. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/pyproject.toml +0 -0
  17. {large_image_source_zarr-1.35.8.dev16 → large_image_source_zarr-1.35.8.dev18}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: large-image-source-zarr
3
- Version: 1.35.8.dev16
3
+ Version: 1.35.8.dev18
4
4
  Summary: A OME Zarr tilesource for large_image.
5
5
  Home-page: https://github.com/girder/large_image
6
6
  Author: Kitware, Inc.
@@ -18,13 +18,15 @@ Classifier: Programming Language :: Python :: 3.15
18
18
  Requires-Python: >=3.10
19
19
  Description-Content-Type: text/x-rst
20
20
  License-File: LICENSE
21
- Requires-Dist: large-image>=1.35.8.dev16
22
- Requires-Dist: zarr<3
23
- Requires-Dist: numcodecs<0.16
21
+ Requires-Dist: large-image>=1.35.8.dev18
22
+ Requires-Dist: zarr<3; python_version < "3.12"
23
+ Requires-Dist: numcodecs<0.16; python_version < "3.12"
24
+ Requires-Dist: zarr>=3.2.1; python_version >= "3.12"
25
+ Requires-Dist: zarr-sqlite; python_version >= "3.12"
24
26
  Requires-Dist: imagecodecs-numcodecs!=2024.9.22,!=2026.8.16
25
27
  Requires-Dist: filelock>=3.13
26
28
  Provides-Extra: girder
27
- Requires-Dist: girder-large-image>=1.35.8.dev16; extra == "girder"
29
+ Requires-Dist: girder-large-image>=1.35.8.dev18; extra == "girder"
28
30
  Provides-Extra: all
29
31
  Requires-Dist: large-image-converter; extra == "all"
30
32
  Dynamic: author
@@ -28,8 +28,16 @@ with contextlib.suppress(importlib.metadata.PackageNotFoundError):
28
28
 
29
29
 
30
30
  zarr = None
31
+ zarr3 = None
32
+ SQLiteStore = None
31
33
  # Not part of the zarr data; used for cross-process safety
32
34
  _internalFiles = ('.zarr_flock', '.zarr_stamp')
35
+ # Metadata file names for each zarr format
36
+ _metadataFiles = {2: '.zattrs', 3: 'zarr.json'}
37
+ # The OME-Zarr specification version written for each zarr format. OME-Zarr
38
+ # 0.4 is the last version that uses zarr format 2; 0.5 and later use zarr
39
+ # format 3 and store their metadata under an "ome" key.
40
+ _omeVersions = {2: '0.4', 3: '0.5'}
33
41
 
34
42
 
35
43
  def _lazyImport():
@@ -37,7 +45,7 @@ def _lazyImport():
37
45
  Import the zarr module. This is done when needed rather than in the module
38
46
  initialization because it is slow.
39
47
  """
40
- global zarr
48
+ global zarr, zarr3, SQLiteStore
41
49
 
42
50
  if zarr is None:
43
51
  try:
@@ -47,6 +55,86 @@ def _lazyImport():
47
55
  except ImportError:
48
56
  msg = 'zarr module not found.'
49
57
  raise TileSourceError(msg)
58
+ zarr3 = packaging.version.parse(zarr.__version__).major >= 3
59
+ if zarr3:
60
+ from .zarr3 import LegacySQLiteStore
61
+ SQLiteStore = LegacySQLiteStore
62
+ # zarr 3 warns about any file that isn't zarr data, such as our
63
+ # lock files or the METADATA.ome.xml file in OME zarr images
64
+ warnings.filterwarnings(
65
+ 'ignore', category=UserWarning,
66
+ message=r'Object at .* is not recognized as a component of a Zarr hierarchy')
67
+ else:
68
+ SQLiteStore = zarr.SQLiteStore
69
+
70
+
71
+ def _omeAttrs(attrs):
72
+ """
73
+ Get the OME-Zarr metadata from a group's attributes. OME-Zarr 0.5 and
74
+ later store it under an "ome" key; earlier versions store it at the top
75
+ level.
76
+
77
+ :param attrs: a dictionary of group attributes.
78
+ :returns: the dictionary containing the OME-Zarr keys such as
79
+ "multiscales".
80
+ """
81
+ if isinstance(attrs.get('ome'), dict):
82
+ return attrs['ome']
83
+ return attrs
84
+
85
+
86
+ def _omeAxes(multiscale):
87
+ """
88
+ Get the axes list from a multiscales entry. OME-Zarr 0.6 moved the axes
89
+ into coordinate systems.
90
+
91
+ :param multiscale: a multiscales dictionary.
92
+ :returns: a list of axis dictionaries.
93
+ """
94
+ if 'axes' not in multiscale and multiscale.get('coordinateSystems'):
95
+ return multiscale['coordinateSystems'][0]['axes']
96
+ return multiscale['axes']
97
+
98
+
99
+ def _omeScale(dataset):
100
+ """
101
+ Get the scale transformation of a multiscales dataset entry. OME-Zarr
102
+ 0.6 can wrap it in a sequence transformation.
103
+
104
+ :param dataset: a dictionary from the multiscales datasets list.
105
+ :returns: a list of scale values or None.
106
+ """
107
+ for trans in dataset.get('coordinateTransformations', []):
108
+ if trans.get('type') == 'sequence':
109
+ trans = next(
110
+ (t for t in trans.get('transformations', []) if t.get('type') == 'scale'),
111
+ {})
112
+ if trans.get('type') == 'scale':
113
+ return trans['scale']
114
+ return None
115
+
116
+
117
+ def _jsonSafe(value):
118
+ """
119
+ Convert a value to what it will be when written as zarr attributes. zarr 2
120
+ converted numpy numbers to python numbers when writing attributes; zarr 3
121
+ does not.
122
+
123
+ :param value: a json-serializable value that may contain numpy numbers.
124
+ :returns: the value with only python types.
125
+ """
126
+
127
+ def default(obj):
128
+ if isinstance(obj, np.bool_):
129
+ return bool(obj)
130
+ if isinstance(obj, np.integer):
131
+ return int(obj)
132
+ if isinstance(obj, np.floating):
133
+ return float(obj)
134
+ msg = f'Object of type {type(obj).__name__} is not JSON serializable'
135
+ raise TypeError(msg)
136
+
137
+ return json.loads(json.dumps(value, default=default))
50
138
 
51
139
 
52
140
  def _copyStore(src, dst):
@@ -57,9 +145,13 @@ def _copyStore(src, dst):
57
145
  :param src: the source store.
58
146
  :param dst: the destination store.
59
147
  """
60
- zarr.copy_store(
61
- src, dst, if_exists='replace',
62
- excludes=[f'^{re.escape(name)}$' for name in _internalFiles])
148
+ if zarr3:
149
+ from .zarr3 import copy_store
150
+ copy_store(src, dst, excludes=_internalFiles)
151
+ else:
152
+ zarr.copy_store(
153
+ src, dst, if_exists='replace',
154
+ excludes=[f'^{re.escape(name)}$' for name in _internalFiles])
63
155
 
64
156
 
65
157
  class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
@@ -75,6 +167,8 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
75
167
  'zgroup': SourcePriority.PREFERRED,
76
168
  'zattrs': SourcePriority.PREFERRED,
77
169
  'zarray': SourcePriority.PREFERRED,
170
+ # zarr.json files in zarr format 3
171
+ 'json': SourcePriority.LOW,
78
172
  'db': SourcePriority.MEDIUM,
79
173
  'zip': SourcePriority.LOWER,
80
174
  }
@@ -98,6 +192,10 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
98
192
  parameters.
99
193
 
100
194
  :param path: a filesystem path for the tile source.
195
+ :param zarr_format: when creating a new image, the zarr format to
196
+ write: 2 or 3. This defaults to 3 if the zarr library supports
197
+ it, which produces OME-Zarr 0.5 files. Zarr format 2 produces
198
+ OME-Zarr 0.4 files that can be read by zarr 2.
101
199
  """
102
200
  super().__init__(path, **kwargs)
103
201
 
@@ -106,7 +204,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
106
204
  self._initNew(path, **kwargs)
107
205
  else:
108
206
  self._initOpen(**kwargs)
109
- internal = self.getInternalMetadata().get('zarr', {}).get('base', {})
207
+ internal = _omeAttrs(self.getInternalMetadata().get('zarr', {}).get('base', {}))
110
208
  multiscale = internal.get('multiscales', [None])[0]
111
209
  if multiscale is not None:
112
210
  self._imageDescription = multiscale.get('metadata', {}).get('description')
@@ -121,15 +219,21 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
121
219
  self._frameUnits = None
122
220
  if not os.path.isfile(self._largeImagePath) and '//:' not in self._largeImagePath:
123
221
  raise TileSourceFileNotFoundError(self._largeImagePath) from None
124
- if os.path.basename(self._largeImagePath) in {'.zgroup', '.zattrs', '.zarray'}:
222
+ if os.path.basename(self._largeImagePath) in {
223
+ '.zgroup', '.zattrs', '.zarray', 'zarr.json'}:
125
224
  with contextlib.suppress(Exception):
126
225
  self._zarr = zarr.open(os.path.dirname(self._largeImagePath), mode='r')
127
226
  if self._zarr is None:
128
227
  with contextlib.suppress(Exception):
129
228
  self._zarr = zarr.open(self._largeImagePath, mode='r')
229
+ if self._zarr is None and zarr3 and self._largeImagePath.endswith('.zip'):
230
+ with contextlib.suppress(Exception):
231
+ self._zarr = zarr.open(zarr.storage.ZipStore(
232
+ self._largeImagePath, mode='r'), mode='r')
130
233
  if self._zarr is None:
131
234
  with contextlib.suppress(Exception):
132
- self._zarr = zarr.open(zarr.SQLiteStore(self._largeImagePath), mode='r')
235
+ self._zarr = zarr.open(SQLiteStore(
236
+ self._largeImagePath, **({'read_only': True} if zarr3 else {})), mode='r')
133
237
  if self._zarr is None:
134
238
  if not os.path.isfile(self._largeImagePath):
135
239
  raise TileSourceFileNotFoundError(self._largeImagePath) from None
@@ -151,8 +255,22 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
151
255
  self._created = False
152
256
  if not self._tempdir.exists():
153
257
  self._created = True
154
- self._zarr_store = zarr.DirectoryStore(str(self._tempdir))
155
- self._zarr = zarr.open(self._zarr_store, mode='a')
258
+ zarr_format = kwargs.get('zarr_format')
259
+ if not self._created:
260
+ # Another process created this image; use its format
261
+ zarr_format = 3 if (self._tempdir / 'zarr.json').exists() else 2
262
+ if zarr_format is None:
263
+ zarr_format = 3 if zarr3 else 2
264
+ if zarr_format not in _metadataFiles or (zarr_format == 3 and not zarr3):
265
+ msg = f'zarr_format {zarr_format} is not supported.'
266
+ raise TileSourceError(msg)
267
+ self._zarrFormat = zarr_format
268
+ if zarr3:
269
+ self._zarr_store = zarr.storage.LocalStore(str(self._tempdir))
270
+ self._zarr = zarr.open_group(self._zarr_store, mode='a', zarr_format=self._zarrFormat)
271
+ else:
272
+ self._zarr_store = zarr.DirectoryStore(str(self._tempdir))
273
+ self._zarr = zarr.open(self._zarr_store, mode='a')
156
274
  self._largeImagePath = None
157
275
  self._dims = {}
158
276
  self.sizeX = self.sizeY = self.levels = 0
@@ -209,6 +327,23 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
209
327
  msg = 'Not an editable image'
210
328
  raise TileSourceError(msg)
211
329
 
330
+ def _createKwargs(self, axes=None):
331
+ """
332
+ Get the keyword arguments used when creating zarr arrays for an
333
+ editable image.
334
+
335
+ :param axes: an optional list of axis names in array order. With
336
+ zarr format 3, these are stored as the array's dimension names,
337
+ which OME-Zarr 0.5 requires.
338
+ :returns: a dictionary of keyword arguments.
339
+ """
340
+ if not zarr3:
341
+ return {'write_empty_chunks': False}
342
+ kwargs = {'zarr_format': self._zarrFormat}
343
+ if axes is not None and self._zarrFormat == 3:
344
+ kwargs['dimension_names'] = list(axes)
345
+ return kwargs
346
+
212
347
  def _getGeneralAxes(self, arr):
213
348
  """
214
349
  Examine a zarr array an guess what the axes are. We assume the two
@@ -259,12 +394,11 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
259
394
  arrays that might be associated images. These have to be culled
260
395
  for the actual groups used in the series.
261
396
  """
262
- attrs = group.attrs.asdict() if group is not None else {}
397
+ attrs = _omeAttrs(group.attrs.asdict()) if group is not None else {}
263
398
  min_version = packaging.version.Version('0.4')
264
399
  is_ome = (
265
400
  isinstance(attrs.get('multiscales', None), list) and
266
- 'omero' in attrs and
267
- isinstance(attrs['omero'], dict) and
401
+ len(attrs['multiscales']) > 0 and
268
402
  all(isinstance(m, dict) for m in attrs['multiscales']) and
269
403
  all(packaging.version.Version(m['version']) >= min_version
270
404
  for m in attrs['multiscales'] if 'version' in m))
@@ -272,17 +406,18 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
272
406
  axes_values = None
273
407
  axes_units = None
274
408
  if is_ome:
275
- axes = {axis['name']: idx for idx, axis in enumerate(
276
- attrs['multiscales'][0]['axes'])}
409
+ ome_axes = _omeAxes(attrs['multiscales'][0])
410
+ axes = {axis['name']: idx for idx, axis in enumerate(ome_axes)}
277
411
  axes_values = {
278
412
  axis['name']: axis.get('values')
279
- for axis in attrs['multiscales'][0]['axes']
413
+ for axis in ome_axes
280
414
  }
281
415
  axes_units = {
282
416
  axis['name']: axis.get('unit')
283
- for axis in attrs['multiscales'][0]['axes']
417
+ for axis in ome_axes
284
418
  }
285
- if isinstance(attrs['omero'].get('channels'), list):
419
+ if (isinstance(attrs.get('omero'), dict) and
420
+ isinstance(attrs['omero'].get('channels'), list)):
286
421
  channels = [channel['label'] for channel in attrs['omero']['channels']]
287
422
  if all(channel.startswith('Channel ') for channel in channels):
288
423
  channels = None
@@ -325,13 +460,14 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
325
460
  """
326
461
  if results is None:
327
462
  results = {'best': None, 'series': [], 'associated': []}
328
- if isinstance(group, zarr.core.Array):
463
+ if isinstance(group, zarr.Array):
329
464
  self._scanZarrArray(None, group, results)
330
465
  return results
331
- for val in group.values():
332
- if isinstance(val, zarr.core.Array):
466
+ for key in sorted(group):
467
+ val = group[key]
468
+ if isinstance(val, zarr.Array):
333
469
  self._scanZarrArray(group, val, results)
334
- elif isinstance(val, zarr.hierarchy.Group):
470
+ elif isinstance(val, zarr.Group):
335
471
  results = self._scanZarrGroup(val, results)
336
472
  return results
337
473
 
@@ -373,11 +509,11 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
373
509
  self._mm_x = self._mm_y = None
374
510
  baseGroup, baseArray = self._series[0]
375
511
  try:
376
- ms = baseGroup.attrs.asdict()['multiscales'][0]
377
- self._mm_x = ms['datasets'][0]['coordinateTransformations'][0][
378
- 'scale'][self._axes['x']] * unit[ms['axes'][self._axes['x']]['unit']]
379
- self._mm_y = ms['datasets'][0]['coordinateTransformations'][0][
380
- 'scale'][self._axes['y']] * unit[ms['axes'][self._axes['y']]['unit']]
512
+ ms = _omeAttrs(baseGroup.attrs.asdict())['multiscales'][0]
513
+ axes = _omeAxes(ms)
514
+ scale = _omeScale(ms['datasets'][0])
515
+ self._mm_x = scale[self._axes['x']] * unit[axes[self._axes['x']]['unit']]
516
+ self._mm_y = scale[self._axes['y']] * unit[axes[self._axes['y']]['unit']]
381
517
  except Exception:
382
518
  pass
383
519
 
@@ -609,7 +745,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
609
745
  with self._tileLock:
610
746
  img = np.transpose(arr, trans).squeeze()
611
747
  if len(img.shape) == 2:
612
- img.expand_dims(axis=2)
748
+ img = np.expand_dims(img, axis=2)
613
749
  return large_image.tilesource.base._imageToPIL(img)
614
750
 
615
751
  @methodcache()
@@ -670,7 +806,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
670
806
  if axes[-1] != 's':
671
807
  axes.append('s')
672
808
  tile = tile[..., np.newaxis]
673
- if mask is not None and len(axes) - 1 == len(mask.shape):
809
+ if mask is not None and len(mask.shape) == 2:
674
810
  mask = mask[:, :, np.newaxis]
675
811
  if 'x' not in axes or 'y' not in axes:
676
812
  err = 'Invalid value for axes. Must contain "y" and "x".'
@@ -723,12 +859,11 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
723
859
  [(0, s - arr.shape[i]) for i, s in enumerate(new_shape)],
724
860
  )
725
861
  new_arr = zarr.zeros(
726
- new_shape, chunks=chunking, dtype=arr.dtype,
727
- write_empty_chunks=False)
862
+ new_shape, chunks=chunking, dtype=arr.dtype, **self._createKwargs())
728
863
  new_arr[:] = arr[:]
729
864
  arr = new_arr
730
865
  else:
731
- arr.resize(*new_shape)
866
+ arr.resize(new_shape)
732
867
  return arr
733
868
 
734
869
  def addTile(self, tile, x=0, y=0, mask=None, axes=None, **kwargs):
@@ -793,7 +928,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
793
928
  # if writing to base data, invalidate generated levels
794
929
  for path in list(current_arrays):
795
930
  if path != store_path:
796
- self._zarr_store.rmdir(path)
931
+ del self._zarr[path]
797
932
  self._arrays = None
798
933
  chunking = None
799
934
  if store_path not in current_arrays:
@@ -810,7 +945,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
810
945
  chunks=chunking,
811
946
  store=self._zarr_store,
812
947
  path=store_path,
813
- write_empty_chunks=False,
948
+ **self._createKwargs(axes),
814
949
  )
815
950
  self._arrays = None
816
951
  chunking = None
@@ -835,11 +970,12 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
835
970
  arr[placement_slices] = tile
836
971
  if chunking:
837
972
  zarr.array(
838
- arr,
973
+ arr[...] if zarr3 else arr,
839
974
  chunks=chunking,
840
975
  overwrite=True,
841
976
  store=self._zarr_store,
842
977
  path=store_path,
978
+ **self._createKwargs(axes),
843
979
  )
844
980
  self._arrays = None
845
981
 
@@ -882,6 +1018,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
882
1018
  data,
883
1019
  store=self._zarr_store,
884
1020
  path=f'{imageKey}/image',
1021
+ **self._createKwargs(),
885
1022
  )
886
1023
  self._associatedImages[imageKey] = (group, arr)
887
1024
  self._markChanged()
@@ -946,7 +1083,10 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
946
1083
  self._lastStamp = stamp
947
1084
  self._arrays = None
948
1085
  # zarr caches group attributes in memory, so reopen the group
949
- self._zarr = zarr.open_group(self._zarr_store, mode='a')
1086
+ if zarr3:
1087
+ self._zarr = zarr.open_group(self._zarr_store, mode='a', zarr_format=self._zarrFormat)
1088
+ else:
1089
+ self._zarr = zarr.open_group(self._zarr_store, mode='a')
950
1090
  with contextlib.suppress(TileSourceError):
951
1091
  # Don't write our metadata first; that would replace what the
952
1092
  # other process wrote
@@ -971,6 +1111,47 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
971
1111
  self._arrays = dict(self._zarr.arrays())
972
1112
  return self._arrays
973
1113
 
1114
+ def _getChannelsInternalMetadata(self, base_array, channel_axis):
1115
+ """
1116
+ Get the omero channels metadata for the image.
1117
+
1118
+ :param base_array: the zarr array of the base level of the image.
1119
+ :param channel_axis: the index of the channel axis in the array.
1120
+ :returns: a list of channel dictionaries.
1121
+ """
1122
+ # The omero specification requires a window for each channel.
1123
+ # Scanning the data for the actual range is expensive, so use the
1124
+ # range of the data type.
1125
+ dtype = np.dtype(base_array.dtype)
1126
+ if dtype.kind in 'iu':
1127
+ window_min, window_max = int(np.iinfo(dtype).min), int(np.iinfo(dtype).max)
1128
+ elif dtype.kind == 'b':
1129
+ window_min, window_max = 0, 1
1130
+ else:
1131
+ window_min, window_max = 0.0, 1.0
1132
+ channels = []
1133
+ for c in range(base_array.shape[channel_axis]):
1134
+ channel_metadata = {
1135
+ 'active': True,
1136
+ 'coefficient': 1,
1137
+ 'color': 'FFFFFF',
1138
+ 'family': 'linear',
1139
+ 'inverted': False,
1140
+ 'label': f'Band {c + 1}',
1141
+ 'window': {
1142
+ 'end': window_max,
1143
+ 'max': window_max,
1144
+ 'min': window_min,
1145
+ 'start': window_min,
1146
+ },
1147
+ }
1148
+ if len(self._channelNames) > c:
1149
+ channel_metadata['label'] = self._channelNames[c]
1150
+ if len(self._channelColors) > c:
1151
+ channel_metadata['color'] = self._channelColors[c]
1152
+ channels.append(channel_metadata)
1153
+ return channels
1154
+
974
1155
  def _writeInternalMetadata(self):
975
1156
  """
976
1157
  Write metadata to Zarr attributes.
@@ -980,7 +1161,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
980
1161
  with self._threadLock, self._processLock():
981
1162
  self._syncFromOtherProcesses()
982
1163
  name = str(self._tempdir.name).split('/')[-1]
983
- arrays = self._currentArrays()
1164
+ arrays = dict(sorted(self._currentArrays().items()))
984
1165
  datasets = []
985
1166
  axes = []
986
1167
  channels = []
@@ -1009,41 +1190,12 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
1009
1190
  rdefs['defaultZ'] = 0
1010
1191
  axes.append(self._getAxisInternalMetadata(a))
1011
1192
  if channel_axis is not None and len(arrays) > 0:
1012
- base_array = list(arrays.values())[0]
1013
- base_shape = base_array.shape
1014
- for c in range(base_shape[channel_axis]):
1015
- channel_metadata = {
1016
- 'active': True,
1017
- 'coefficient': 1,
1018
- 'color': 'FFFFFF',
1019
- 'family': 'linear',
1020
- 'inverted': False,
1021
- 'label': f'Band {c + 1}',
1022
- }
1023
- # slicing = tuple(
1024
- # slice(None)
1025
- # if k != ('c' if 'c' in self._axes else 's')
1026
- # else c
1027
- # for k, v in self._axes.items()
1028
- # )
1029
- # channel_data = base_array[slicing]
1030
- # channel_min = np.min(channel_data)
1031
- # channel_max = np.max(channel_data)
1032
- # channel_metadata['window'] = {
1033
- # 'end': channel_max,
1034
- # 'max': channel_max,
1035
- # 'min': channel_min,
1036
- # 'start': channel_min,
1037
- # }
1038
- if len(self._channelNames) > c:
1039
- channel_metadata['label'] = self._channelNames[c]
1040
- if len(self._channelColors) > c:
1041
- channel_metadata['color'] = self._channelColors[c]
1042
- channels.append(channel_metadata)
1193
+ channels = self._getChannelsInternalMetadata(
1194
+ list(arrays.values())[0], channel_axis)
1043
1195
  # Guidelines from https://ngff.openmicroscopy.org/latest/
1196
+ version = _omeVersions[self._zarrFormat]
1044
1197
  attrs = {
1045
1198
  'multiscales': [{
1046
- 'version': '0.5',
1047
1199
  'name': name,
1048
1200
  'axes': axes,
1049
1201
  'datasets': datasets,
@@ -1056,18 +1208,29 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
1056
1208
  }],
1057
1209
  'omero': {
1058
1210
  'id': 1,
1059
- 'version': '0.5',
1060
1211
  'name': name,
1061
1212
  'channels': channels,
1062
1213
  'rdefs': rdefs,
1063
1214
  },
1064
- 'bioformats2raw.layout': 3,
1065
1215
  }
1216
+ if self._zarrFormat == 3:
1217
+ # OME-Zarr 0.5 and later store the metadata under an "ome"
1218
+ # key with a single version
1219
+ attrs = {'ome': {'version': version, **attrs}}
1220
+ else:
1221
+ # OME-Zarr 0.4 stores the keys at the top level, each with a
1222
+ # version
1223
+ attrs['multiscales'][0]['version'] = version
1224
+ attrs['omero']['version'] = version
1225
+ attrs = _jsonSafe(attrs)
1066
1226
  # Writing attributes is slow and makes other processes reread
1067
1227
  # them, so only do so if they changed
1068
1228
  current = self._zarr.attrs.asdict()
1069
1229
  if any(current.get(k) != v for k, v in attrs.items()):
1070
- self._zarr.attrs.update(attrs)
1230
+ if zarr3:
1231
+ self._zarr.update_attributes(attrs)
1232
+ else:
1233
+ self._zarr.attrs.update(attrs)
1071
1234
  self._markChanged()
1072
1235
 
1073
1236
  @property
@@ -1451,7 +1614,7 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
1451
1614
 
1452
1615
  if self.crop:
1453
1616
  left, top, width, height = self.crop
1454
- source = new()
1617
+ source = new(zarr_format=self._zarrFormat)
1455
1618
  source._zarr.attrs.update(self._zarr.attrs)
1456
1619
  for frame in self.getMetadata().get('frames', [{'Index': 0}]):
1457
1620
  frame_position = {
@@ -1506,18 +1669,19 @@ class ZarrFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
1506
1669
  str(source._tempdir), path,
1507
1670
  ignore=shutil.ignore_patterns(*_internalFiles))
1508
1671
  elif suffix in ['.db', '.sqlite']:
1509
- sqlite_store = zarr.SQLiteStore(path)
1672
+ sqlite_store = SQLiteStore(path)
1510
1673
  _copyStore(source._zarr_store, sqlite_store)
1511
1674
  sqlite_store.close()
1512
1675
  elif suffix == '.zip':
1513
- zip_store = zarr.ZipStore(path)
1676
+ zip_store = (zarr.storage.ZipStore(path, mode='w') if zarr3 else
1677
+ zarr.ZipStore(path))
1514
1678
  _copyStore(source._zarr_store, zip_store)
1515
1679
  zip_store.close()
1516
1680
 
1517
1681
  else:
1518
1682
  from large_image_converter import convert
1519
1683
 
1520
- attrs_path = source._tempdir / '.zattrs'
1684
+ attrs_path = source._tempdir / _metadataFiles[source._zarrFormat]
1521
1685
  params = {}
1522
1686
  if lossy and self.dtype == np.uint8:
1523
1687
  params['compression'] = 'jpeg'
@@ -1546,5 +1710,9 @@ def new(*args, **kwargs):
1546
1710
  """
1547
1711
  Create a new image, collecting the results from patches of numpy arrays or
1548
1712
  smaller images.
1713
+
1714
+ :param zarr_format: the zarr format to write: 2 or 3. This defaults to 3
1715
+ if the zarr library supports it, which produces OME-Zarr 0.5 files.
1716
+ Zarr format 2 produces OME-Zarr 0.4 files that can be read by zarr 2.
1549
1717
  """
1550
1718
  return ZarrFileTileSource(NEW_IMAGE_PATH_FLAG + str(uuid.uuid4()), *args, **kwargs)
@@ -0,0 +1,59 @@
1
+ """
2
+ Helpers for zarr >= 3, which no longer includes a SQLite store or a function to
3
+ copy stores.
4
+ """
5
+
6
+ from zarr.core.buffer import default_buffer_prototype
7
+ from zarr.core.sync import sync
8
+ from zarr_sqlite import SQLiteStore
9
+
10
+
11
+ class LegacySQLiteStore(SQLiteStore):
12
+ """
13
+ A zarr-sqlite store that can also read the files written by the
14
+ SQLiteStore in zarr 2. Those files only have a ``zarr`` table with the
15
+ same key and value columns, but lack the metadata table and application
16
+ id that zarr-sqlite requires.
17
+ """
18
+
19
+ def __init__(self, database, *, read_only=False, journal_mode='DELETE', **kwargs):
20
+ # Don't leave written files in WAL mode; they are meant to be
21
+ # distributed as a single file.
22
+ super().__init__(database, read_only=read_only, journal_mode=journal_mode, **kwargs)
23
+
24
+ async def _validate_schema(self):
25
+ if self.read_only:
26
+ rows = await self._conn.fetchall(
27
+ 'SELECT name FROM sqlite_master WHERE type = ? AND name = ?',
28
+ ('table', 'zarr_sqlitestore_metadata'))
29
+ if not rows:
30
+ rows = await self._conn.fetchall('PRAGMA table_info(zarr)')
31
+ if not rows:
32
+ msg = 'Not a zarr SQLite file'
33
+ raise ValueError(msg)
34
+ return None
35
+ return await super()._validate_schema()
36
+
37
+ def with_read_only(self, read_only=False):
38
+ return type(self)(self.database, read_only=read_only)
39
+
40
+
41
+ async def _copy_store(src, dst, excludes):
42
+ prototype = default_buffer_prototype()
43
+ async for key in src.list():
44
+ if key not in excludes:
45
+ await dst.set(key, await src.get(key, prototype=prototype))
46
+
47
+
48
+ def copy_store(src, dst, excludes=()):
49
+ """
50
+ Copy all keys from one zarr store to another.
51
+
52
+ :param src: the source store.
53
+ :param dst: the destination store.
54
+ :param excludes: keys that should not be copied.
55
+ """
56
+ sync(_copy_store(src, dst, excludes))
57
+
58
+
59
+ __all__ = ['LegacySQLiteStore', 'copy_store']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: large-image-source-zarr
3
- Version: 1.35.8.dev16
3
+ Version: 1.35.8.dev18
4
4
  Summary: A OME Zarr tilesource for large_image.
5
5
  Home-page: https://github.com/girder/large_image
6
6
  Author: Kitware, Inc.
@@ -18,13 +18,15 @@ Classifier: Programming Language :: Python :: 3.15
18
18
  Requires-Python: >=3.10
19
19
  Description-Content-Type: text/x-rst
20
20
  License-File: LICENSE
21
- Requires-Dist: large-image>=1.35.8.dev16
22
- Requires-Dist: zarr<3
23
- Requires-Dist: numcodecs<0.16
21
+ Requires-Dist: large-image>=1.35.8.dev18
22
+ Requires-Dist: zarr<3; python_version < "3.12"
23
+ Requires-Dist: numcodecs<0.16; python_version < "3.12"
24
+ Requires-Dist: zarr>=3.2.1; python_version >= "3.12"
25
+ Requires-Dist: zarr-sqlite; python_version >= "3.12"
24
26
  Requires-Dist: imagecodecs-numcodecs!=2024.9.22,!=2026.8.16
25
27
  Requires-Dist: filelock>=3.13
26
28
  Provides-Extra: girder
27
- Requires-Dist: girder-large-image>=1.35.8.dev16; extra == "girder"
29
+ Requires-Dist: girder-large-image>=1.35.8.dev18; extra == "girder"
28
30
  Provides-Extra: all
29
31
  Requires-Dist: large-image-converter; extra == "all"
30
32
  Dynamic: author
@@ -4,6 +4,7 @@ pyproject.toml
4
4
  setup.py
5
5
  large_image_source_zarr/__init__.py
6
6
  large_image_source_zarr/girder_source.py
7
+ large_image_source_zarr/zarr3.py
7
8
  large_image_source_zarr.egg-info/PKG-INFO
8
9
  large_image_source_zarr.egg-info/SOURCES.txt
9
10
  large_image_source_zarr.egg-info/dependency_links.txt
@@ -0,0 +1,17 @@
1
+ large-image>=1.35.8.dev18
2
+ imagecodecs-numcodecs!=2024.9.22,!=2026.8.16
3
+ filelock>=3.13
4
+
5
+ [:python_version < "3.12"]
6
+ zarr<3
7
+ numcodecs<0.16
8
+
9
+ [:python_version >= "3.12"]
10
+ zarr>=3.2.1
11
+ zarr-sqlite
12
+
13
+ [all]
14
+ large-image-converter
15
+
16
+ [girder]
17
+ girder-large-image>=1.35.8.dev18
@@ -2,6 +2,7 @@
2
2
  "files": [
3
3
  "large_image_source_zarr/__init__.py",
4
4
  "large_image_source_zarr/girder_source.py",
5
+ "large_image_source_zarr/zarr3.py",
5
6
  "pyproject.toml",
6
7
  "setup.py"
7
8
  ]
@@ -35,11 +35,14 @@ setup(
35
35
  python_requires='>=3.10',
36
36
  install_requires=[
37
37
  f'large-image{limit_version}',
38
- # zarr 3 removes the SQLiteStore
39
- 'zarr<3',
38
+ # zarr 3 removes the SQLiteStore; zarr-sqlite restores it but
39
+ # requires Python >= 3.12
40
+ 'zarr<3; python_version < "3.12"',
40
41
  # numcodecs had been required by zarr, but now needs to be asked for
41
42
  # 0.16 requires zarr 3 (but does specify such)
42
- 'numcodecs<0.16',
43
+ 'numcodecs<0.16; python_version < "3.12"',
44
+ 'zarr>=3.2.1; python_version >= "3.12"',
45
+ 'zarr-sqlite; python_version >= "3.12"',
43
46
  # Without imagecodecs-numcodecs, some jpeg encoded data cannot be read
44
47
  'imagecodecs-numcodecs!=2024.9.22,!=2026.8.16',
45
48
  # Needs is_singleton
@@ -1,11 +0,0 @@
1
- large-image>=1.35.8.dev16
2
- zarr<3
3
- numcodecs<0.16
4
- imagecodecs-numcodecs!=2024.9.22,!=2026.8.16
5
- filelock>=3.13
6
-
7
- [all]
8
- large-image-converter
9
-
10
- [girder]
11
- girder-large-image>=1.35.8.dev16