large-image-source-bioformats 1.35.7.dev8__tar.gz → 1.35.7.dev14__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 (16) hide show
  1. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/PKG-INFO +3 -3
  2. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats/__init__.py +142 -14
  3. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats.egg-info/PKG-INFO +3 -3
  4. large_image_source_bioformats-1.35.7.dev14/large_image_source_bioformats.egg-info/requires.txt +5 -0
  5. large_image_source_bioformats-1.35.7.dev8/large_image_source_bioformats.egg-info/requires.txt +0 -5
  6. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/LICENSE +0 -0
  7. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/README.rst +0 -0
  8. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats/girder_source.py +0 -0
  9. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats.egg-info/SOURCES.txt +0 -0
  10. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats.egg-info/dependency_links.txt +0 -0
  11. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats.egg-info/entry_points.txt +0 -0
  12. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats.egg-info/scm_file_list.json +0 -0
  13. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/large_image_source_bioformats.egg-info/top_level.txt +0 -0
  14. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/pyproject.toml +0 -0
  15. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/setup.cfg +0 -0
  16. {large_image_source_bioformats-1.35.7.dev8 → large_image_source_bioformats-1.35.7.dev14}/setup.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: large-image-source-bioformats
3
- Version: 1.35.7.dev8
3
+ Version: 1.35.7.dev14
4
4
  Summary: An bioformats tilesource for large_image.
5
5
  Home-page: https://github.com/girder/large_image
6
6
  Author: Kitware, Inc.
@@ -18,10 +18,10 @@ 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.7.dev8
21
+ Requires-Dist: large-image>=1.35.7.dev14
22
22
  Requires-Dist: python-bioformats>=1.5.2
23
23
  Provides-Extra: girder
24
- Requires-Dist: girder-large-image>=1.35.7.dev8; extra == "girder"
24
+ Requires-Dist: girder-large-image>=1.35.7.dev14; extra == "girder"
25
25
  Dynamic: author
26
26
  Dynamic: author-email
27
27
  Dynamic: classifier
@@ -54,15 +54,68 @@ javabridge = None
54
54
 
55
55
  _javabridgeStarted = None
56
56
  _javabridgeStartLock = threading.Lock()
57
- _javabridgeAttachLock = threading.Lock()
57
+ # This is reentrant because a tile source's __del__ can run on a thread that is
58
+ # in the middle of attaching to the JVM: the cyclic garbage collector can run
59
+ # during javabridge.attach, which makes Java calls while this lock is held.
60
+ _javabridgeAttachLock = threading.RLock()
58
61
  _bioformatsVersion = None
59
62
  _openImages = []
60
63
 
64
+ # Map bioformats FormatTools pixel types to (little endian, big endian) numpy
65
+ # dtypes.
66
+ _pixelDtypes = {
67
+ 0: (np.int8, np.int8),
68
+ 1: (np.uint8, np.uint8),
69
+ 2: ('<i2', '>i2'),
70
+ 3: ('<u2', '>u2'),
71
+ 4: ('<i4', '>i4'),
72
+ 5: ('<u4', '>u4'),
73
+ 6: ('<f4', '>f4'),
74
+ 7: ('<f8', '>f8'),
75
+ }
76
+
61
77
 
62
78
  # Default to ignoring files with no extension and some specific extensions.
63
79
  config.ConfigValues.setdefault(
64
80
  'source_bioformats_ignored_names',
65
81
  r'(^[^.]*|\.(jpg|jpeg|jpe|png|tif|tiff|ndpi|ome|nc|json|geojson|fits|isyntax|mrxs|zip|zarr(\.db|\.zip)))$') # noqa
82
+ # Options passed to the JVM when it is started. If None, the options from
83
+ # _defaultJvmArgs are used.
84
+ config.ConfigValues.setdefault('source_bioformats_jvm_args', None)
85
+ # Maximum JVM heap size (e.g., '4g'); None uses the JVM default.
86
+ config.ConfigValues.setdefault('source_bioformats_max_heap', None)
87
+
88
+ # The JVM honors two environment variables in addition to the options it is
89
+ # started with: JAVA_TOOL_OPTIONS is prepended to the options (so our options
90
+ # take precedence) and _JAVA_OPTIONS is appended (so it takes precedence over
91
+ # our options).
92
+ _jvmEnvironmentKeys = ('JAVA_TOOL_OPTIONS', '_JAVA_OPTIONS')
93
+ # A garbage collector that returns memory to the operating system when the
94
+ # heap shrinks. JDK 8 defaults to ParallelGC, which never does.
95
+ _jvmCollectorArgs = ['-XX:+UseG1GC']
96
+ # Shrink the heap once it is mostly free; these are honored by any collector.
97
+ _jvmHeapArgs = ['-XX:MinHeapFreeRatio=5', '-XX:MaxHeapFreeRatio=10']
98
+
99
+
100
+ def _defaultJvmArgs(logger):
101
+ """
102
+ Get the default options used to start the JVM.
103
+
104
+ These select a garbage collector that gives memory back to the operating
105
+ system and ask it to shrink the heap when most of it is free. The JVM
106
+ refuses to start if more than one garbage collector is selected, so if the
107
+ environment (see _jvmEnvironmentKeys) already selects one, only the heap
108
+ options are used.
109
+
110
+ :param logger: a logger for reporting what was chosen.
111
+ :returns: a list of JVM options.
112
+ """
113
+ envopts = ' '.join(os.environ.get(key, '') for key in _jvmEnvironmentKeys)
114
+ if re.search(r'-XX:\+Use\w*GC\b', envopts):
115
+ logger.info('Using the garbage collector from the environment for the '
116
+ 'Bioformats JVM.')
117
+ return list(_jvmHeapArgs)
118
+ return _jvmCollectorArgs + _jvmHeapArgs
66
119
 
67
120
 
68
121
  def _monitor_thread():
@@ -138,8 +191,24 @@ def _startJavabridge(logger):
138
191
  monitor.daemon = True
139
192
  monitor.start()
140
193
  try:
141
- javabridge.start_vm(class_path=bioformats.JARS, run_headless=True)
194
+ args = config.getConfig('source_bioformats_jvm_args')
195
+ # start_vm appends to the args list, so always pass a copy
196
+ args = list(args) if args is not None else _defaultJvmArgs(logger)
197
+ javabridge.start_vm(
198
+ args=args,
199
+ class_path=bioformats.JARS,
200
+ max_heap_size=config.getConfig('source_bioformats_max_heap'),
201
+ run_headless=True)
142
202
  _reduceLogging()
203
+ # start_vm attaches the calling thread and never detaches it,
204
+ # and kill_vm only detaches the thread it runs on. Detach
205
+ # here so this thread has no stale environment after the JVM
206
+ # is destroyed; freeing a Java object on such a thread at
207
+ # interpreter exit hangs the process. Everything in this
208
+ # module attaches and detaches around its own use of the JVM.
209
+ with _javabridgeAttachLock:
210
+ if javabridge.get_env():
211
+ javabridge.detach()
143
212
  atexit.register(_stopJavabridge)
144
213
  logger.info('Started JVM for Bioformats tile source.')
145
214
  _javabridgeStarted = True
@@ -157,6 +226,22 @@ def _stopJavabridge(*args, **kwargs):
157
226
  _javabridgeStarted = None
158
227
 
159
228
 
229
+ def _releaseJavaMemory():
230
+ """
231
+ Ask the JVM to collect garbage and shrink its heap. This only reduces
232
+ resident memory if the JVM was started with a collector that uncommits
233
+ memory (see the source_bioformats_jvm_args config value). The caller must
234
+ be attached to the JVM.
235
+
236
+ This uses the Runtime instance method rather than the static System.gc,
237
+ since javabridge holds the Python GIL during static void calls.
238
+ """
239
+ with contextlib.suppress(Exception):
240
+ runtime = javabridge.static_call(
241
+ 'java/lang/Runtime', 'getRuntime', '()Ljava/lang/Runtime;')
242
+ javabridge.call(runtime, 'gc', '()V')
243
+
244
+
160
245
  def _getBioformatsVersion():
161
246
  """
162
247
  Get the version of the jar file.
@@ -228,6 +313,7 @@ class BioformatsFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
228
313
  self.addKnownExtensions()
229
314
 
230
315
  self._tileLock = threading.RLock()
316
+ self._seriesPixelInfo = {}
231
317
 
232
318
  try:
233
319
  with _javabridgeAttachLock:
@@ -336,17 +422,27 @@ class BioformatsFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
336
422
 
337
423
  def __del__(self):
338
424
  if getattr(self, '_bioimage', None) is not None:
425
+ # The garbage collector can run this on any thread, including one
426
+ # that is partway through javabridge.attach or that owns the JVM
427
+ # environment without an attach count (javabridge's own monitor
428
+ # thread). Only attach if the thread has no environment and only
429
+ # detach if we attached, so the thread is left as we found it.
430
+ attached = False
339
431
  try:
340
432
  with _javabridgeAttachLock:
341
- javabridge.attach()
433
+ if javabridge.get_env() is None:
434
+ javabridge.attach()
435
+ attached = True
342
436
  self._bioimage.close()
343
437
  del self._bioimage
344
438
  _openImages.remove(weakref.ref(self))
439
+ if not _openImages:
440
+ _releaseJavaMemory()
345
441
  except Exception:
346
442
  pass
347
443
  finally:
348
- with _javabridgeAttachLock:
349
- if javabridge.get_env():
444
+ if attached:
445
+ with _javabridgeAttachLock:
350
446
  javabridge.detach()
351
447
 
352
448
  def _checkForOffset(self):
@@ -657,6 +753,42 @@ class BioformatsFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
657
753
  """
658
754
  return self._metadata
659
755
 
756
+ def _readRegion(self, series, c, z, t, x, y, w, h):
757
+ """
758
+ Read a region of a plane directly from the bioformats reader. This
759
+ bypasses bioformats.ImageReader.read, which rebuilds wrapper classes
760
+ and a ChannelSeparator and reads each channel separately on every
761
+ call. The caller must be attached to the JVM and hold the tile lock.
762
+
763
+ :param series: the bioformats series to read.
764
+ :param c: the effective channel index.
765
+ :param z: the z index.
766
+ :param t: the t index.
767
+ :param x: the left edge of the region.
768
+ :param y: the top edge of the region.
769
+ :param w: the width of the region.
770
+ :param h: the height of the region.
771
+ :returns: a numpy array of shape (h, w) or (h, w, 3) in the file's
772
+ native dtype.
773
+ """
774
+ rdr = self._bioimage.rdr
775
+ rdr.setSeries(series)
776
+ info = self._seriesPixelInfo.get(series)
777
+ if info is None:
778
+ info = self._seriesPixelInfo[series] = (
779
+ _pixelDtypes[rdr.getPixelType()][0 if rdr.isLittleEndian() else 1],
780
+ rdr.getRGBChannelCount(),
781
+ rdr.isInterleaved())
782
+ dtype, rgbcc, interleaved = info
783
+ buf = np.frombuffer(rdr.openBytesXYWH(rdr.getIndex(z, c, t), x, y, w, h), dtype)
784
+ if rgbcc == 1:
785
+ return buf.reshape(h, w)
786
+ if interleaved:
787
+ tile = buf.reshape(h, w, rgbcc)
788
+ else:
789
+ tile = buf.reshape(rgbcc, h, w).transpose(1, 2, 0)
790
+ return tile[:, :, :3] if rgbcc > 3 else tile
791
+
660
792
  @methodcache()
661
793
  def getTile(self, x, y, z, pilImageAllowed=False, numpyAllowed=False, **kwargs): # noqa
662
794
  self._xyzInRange(x, y, z)
@@ -700,18 +832,14 @@ class BioformatsFileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
700
832
  try:
701
833
  if width > 0 and height > 0:
702
834
  with self._tileLock:
703
- tile = self._bioimage.read(
704
- c=fc, z=fz, t=ft,
705
- series=fseries['series'][seriesLevel],
706
- rescale=False, # return internal data types
707
- XYWH=(offsetx, offsety, width, height))
835
+ tile = self._readRegion(
836
+ fseries['series'][seriesLevel], fc, fz, ft,
837
+ offsetx, offsety, width, height)
708
838
  else:
709
839
  # We need the same dtype, so read 1x1 at 0x0
710
840
  with self._tileLock:
711
- tile = self._bioimage.read(
712
- c=fc, z=fz, t=ft, series=fseries['series'][seriesLevel],
713
- rescale=False, # return internal data types
714
- XYWH=(0, 0, 1, 1))
841
+ tile = self._readRegion(
842
+ fseries['series'][seriesLevel], fc, fz, ft, 0, 0, 1, 1)
715
843
  tile = np.zeros(tuple([0, 0] + list(tile.shape[2:])), dtype=tile.dtype)
716
844
  format = TILE_FORMAT_NUMPY
717
845
  except javabridge.JavaException as exc:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: large-image-source-bioformats
3
- Version: 1.35.7.dev8
3
+ Version: 1.35.7.dev14
4
4
  Summary: An bioformats tilesource for large_image.
5
5
  Home-page: https://github.com/girder/large_image
6
6
  Author: Kitware, Inc.
@@ -18,10 +18,10 @@ 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.7.dev8
21
+ Requires-Dist: large-image>=1.35.7.dev14
22
22
  Requires-Dist: python-bioformats>=1.5.2
23
23
  Provides-Extra: girder
24
- Requires-Dist: girder-large-image>=1.35.7.dev8; extra == "girder"
24
+ Requires-Dist: girder-large-image>=1.35.7.dev14; extra == "girder"
25
25
  Dynamic: author
26
26
  Dynamic: author-email
27
27
  Dynamic: classifier
@@ -0,0 +1,5 @@
1
+ large-image>=1.35.7.dev14
2
+ python-bioformats>=1.5.2
3
+
4
+ [girder]
5
+ girder-large-image>=1.35.7.dev14
@@ -1,5 +0,0 @@
1
- large-image>=1.35.7.dev8
2
- python-bioformats>=1.5.2
3
-
4
- [girder]
5
- girder-large-image>=1.35.7.dev8