Skip to content

Commit 405ea46

Browse files
committed
relocate import statements that require java to lazy import statement
this allows you to not have a working java installation to import and work with scportrait as long as you do not require the stitching capabilities
1 parent 419c345 commit 405ea46

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/scportrait/tools/stitch/_stitch.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
from scportrait.io.daskmmap import dask_array_from_path
2222
from scportrait.processing.images._image_processing import rescale_image
2323
from scportrait.tools.stitch._utils.ashlar_plotting import plot_edge_quality, plot_edge_scatter
24-
from scportrait.tools.stitch._utils.filereaders import (
25-
BioformatsReaderRescale,
26-
FilePatternReaderRescale,
27-
)
2824
from scportrait.tools.stitch._utils.filewriters import write_ome_zarr, write_spatialdata, write_tif, write_xml
29-
from scportrait.tools.stitch._utils.parallelized_ashlar import ParallelEdgeAligner, ParallelMosaic
30-
3125

3226
class Stitcher:
3327
"""
@@ -65,7 +59,7 @@ def __init__(
6559
do_intensity_rescale: bool | str = True,
6660
rescale_range: tuple = (1, 99),
6761
channel_order: list[str] = None,
68-
reader_type=FilePatternReaderRescale,
62+
reader_type="FilePatternReaderRescale",
6963
orientation: dict = None,
7064
plot_QC: bool = True,
7165
overwrite: bool = False,
@@ -112,8 +106,13 @@ def __init__(
112106
"""
113107
self._lazy_imports()
114108

109+
# workaround for lazy imports of module
110+
if self.reader_type == "FilePatternReaderRescale":
111+
self.reader_type = self.FilePatternReaderRescale
112+
115113
if orientation is None:
116114
orientation = {"flip_x": False, "flip_y": True}
115+
117116
self.input_dir = input_dir
118117
self.slidename = slidename
119118
self.outdir = outdir
@@ -158,10 +157,21 @@ def _lazy_imports(self):
158157
from ashlar.reg import EdgeAligner, Mosaic
159158
from ashlar.scripts.ashlar import process_axis_flip
160159

160+
from scportrait.tools.stitch._utils.filereaders import (
161+
BioformatsReaderRescale,
162+
FilePatternReaderRescale,
163+
)
164+
165+
from scportrait.tools.stitch._utils.parallelized_ashlar import ParallelEdgeAligner, ParallelMosaic
166+
161167
self.ashlar_thumbnail = thumbnail
162168
self.ashlar_EdgeAligner = EdgeAligner
163169
self.ashlar_Mosaic = Mosaic
164170
self.ashlar_process_axis_flip = process_axis_flip
171+
self.BioformatsReaderRescale = BioformatsReaderRescale
172+
self.FilePatternReaderRescale = FilePatternReaderRescale
173+
self.ParallelEdgeAligner = ParallelEdgeAligner
174+
self.ParallelMosaic = ParallelMosaic
165175

166176
def __exit__(self):
167177
self._clear_cache()
@@ -294,14 +304,14 @@ def _initialize_reader(self):
294304
"""
295305
Initialize the reader for reading image tiles.
296306
"""
297-
if self.reader_type == FilePatternReaderRescale:
307+
if self.reader_type == self.FilePatternReaderRescale:
298308
self.reader = self.reader_type(
299309
self.input_dir,
300310
self.pattern,
301311
self.overlap,
302312
rescale_range=self.rescale_range,
303313
)
304-
elif self.reader_type == BioformatsReaderRescale:
314+
elif self.reader_type == self.BioformatsReaderRescale:
305315
self.reader = self.reader_type(self.input_dir, rescale_range=self.rescale_range)
306316

307317
# setup correct orientation of slide (this depends on microscope used to generate the data)
@@ -564,7 +574,7 @@ class ParallelStitcher(Stitcher):
564574
do_intensity_rescale (bool or "full_image", optional): Flag to indicate whether to rescale image intensities (default is True). Alternatively, set to "full_image" to rescale the entire image.
565575
rescale_range (tuple or dict, optional): If all channels should be rescaled to the same range pass a tuple with the percentiles for rescaling (default is (1, 99)). Alternatively, a dictionary can be passed with the channel names as keys and the percentiles as values if each channel should be rescaled to a different range.
566576
channel_order (list, optional): Order of channels in the generated output mosaic. If none (default value) the order of the channels is left unchanged.
567-
reader_type (class, optional): Type of reader to use for reading image tiles (default is FilePatternReaderRescale).
577+
reader_type (class, optional): Type of reader to use for reading image tiles (default is "FilePatternReaderRescale").
568578
orientation (dict, optional): Dictionary specifying which dimensions of the slide to flip (default is {'flip_x': False, 'flip_y': True}).
569579
plot_QC (bool, optional): Flag to indicate whether to plot quality control (QC) figures (default is True).
570580
overwrite (bool, optional): Flag to indicate whether to overwrite the output directory if it already exists (default is False).
@@ -588,7 +598,7 @@ def __init__(
588598
WGAchannel: str = None,
589599
channel_order: list[str] = None,
590600
overwrite: bool = False,
591-
reader_type=FilePatternReaderRescale,
601+
reader_type="FilePatternReaderRescale",
592602
orientation=None,
593603
cache: str = None,
594604
threads: int = 20,
@@ -613,8 +623,9 @@ def __init__(
613623
overwrite,
614624
cache,
615625
)
626+
616627
# dirty fix to avoide multithreading error with BioformatsReader until this can be fixed
617-
if self.reader_type == BioformatsReaderRescale:
628+
if self.reader_type == self.BioformatsReaderRescale:
618629
threads = 1
619630
print(
620631
"BioformatsReaderRescale does not support multithreading for calculating the error threshold currently. Proceeding with 1 thread."
@@ -632,7 +643,7 @@ def _initialize_aligner(self):
632643
Returns:
633644
aligner (ParallelEdgeAligner): Initialized ParallelEdgeAligner object.
634645
"""
635-
aligner = ParallelEdgeAligner(
646+
aligner = self.ParallelEdgeAligner(
636647
self.reader,
637648
channel=self.stitching_channel_id,
638649
filter_sigma=self.filter_sigma,
@@ -644,7 +655,7 @@ def _initialize_aligner(self):
644655
return aligner
645656

646657
def _initialize_mosaic(self):
647-
mosaic = ParallelMosaic(
658+
mosaic =self.ParallelMosaic(
648659
self.aligner, self.aligner.mosaic_shape, verbose=True, channels=self.channels, n_threads=self.threads
649660
)
650661
return mosaic

0 commit comments

Comments
 (0)