Skip to content

Commit 6bbcaf8

Browse files
committed
Merge branch 'code-kai-20230418' into devel
2 parents b74d6b0 + 97fbc56 commit 6bbcaf8

2 files changed

Lines changed: 149 additions & 1 deletion

File tree

src/imcflibs/imagej/bioformats.py

Lines changed: 114 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,28 @@
1717

1818
from loci.plugins.in import ImporterOptions # pdoc: skip
1919

20+
from loci.formats import ImageReader, Memoizer
21+
2022
from ..pathtools import gen_name_from_orig
2123
from ..log import LOG as log
2224

2325

2426
def import_image(
25-
filename, color_mode="color", split_c=False, split_z=False, split_t=False
27+
filename,
28+
color_mode="color",
29+
split_c=False,
30+
split_z=False,
31+
split_t=False,
32+
series_number=None,
33+
c_start=None,
34+
c_end=None,
35+
c_interval=None,
36+
z_start=None,
37+
z_end=None,
38+
z_interval=None,
39+
t_start=None,
40+
t_end=None,
41+
t_interval=None,
2642
):
2743
"""Open an image file using the Bio-Formats importer.
2844
@@ -39,6 +55,35 @@ def import_image(
3955
Whether to split the z-slices into separate ImagePlus objects.
4056
split_t : bool, optional
4157
Whether to split the time points into separate ImagePlus objects.
58+
series_number : int, optional
59+
open a specific Bio-Formats series
60+
c_start : int, optional
61+
only import a subset of channel starting with this one. Requires to set
62+
c_end and c_interval.
63+
c_end : int, optional
64+
only import channel(s) ending with this one. Requires to set c_start and
65+
c_interval.
66+
c_interval : int, optional
67+
only import a subset of channel with this interval. Requires to set
68+
c_start and c_end.
69+
z_start : int, optional
70+
only import a subset of planes starting with this one. Requires to set
71+
z_end and z_interval.
72+
z_end : int, optional
73+
only import a subset of planes ending with this one. Requires to set
74+
z_start and z_interval.
75+
z_interval : int, optional
76+
only import a subset of planes with this interval. Requires to set
77+
z_start and z_end.
78+
t_start : int, optional
79+
only import a subset of time points starting with this one. Requires to
80+
set t_end and t_interval.
81+
t_end : int, optional
82+
only import a subset of time points ending with this one. Requires to
83+
set t_start and t_interval.
84+
t_interval : int, optional
85+
only import a subset of time points with thsi interval. Requires to set
86+
t_start and t_end.
4287
4388
Returns
4489
-------
@@ -57,6 +102,33 @@ def import_image(
57102
options.setSplitFocalPlanes(split_z)
58103
options.setSplitTimepoints(split_t)
59104
options.setId(filename)
105+
if series_number is not None:
106+
options.setSeriesOn(series_number, True)
107+
108+
if c_start is not None:
109+
if series_number is None:
110+
series_number = 0
111+
options.setSpecifyRanges(True)
112+
options.setCBegin(series_number, c_start)
113+
options.setCEnd(series_number, c_end)
114+
options.setCStep(series_number, c_interval)
115+
116+
if z_start is not None:
117+
if series_number is None:
118+
series_number = 0
119+
options.setSpecifyRanges(True)
120+
options.setZBegin(series_number, z_start)
121+
options.setZEnd(series_number, z_end)
122+
options.setZStep(series_number, z_interval)
123+
124+
if t_start is not None:
125+
if series_number is None:
126+
series_number = 0
127+
options.setSpecifyRanges(True)
128+
options.setTBegin(series_number, t_start)
129+
options.setTEnd(series_number, t_end)
130+
options.setTStep(series_number, t_interval)
131+
60132
log.info("Reading [%s]", filename)
61133
orig_imps = BF.openImagePlus(options)
62134
log.debug("Opened [%s] %s", filename, type(orig_imps))
@@ -131,3 +203,44 @@ def export_using_orig_name(imp, path, orig_name, tag, suffix, overwrite=False):
131203
out_file = gen_name_from_orig(path, orig_name, tag, suffix)
132204
export(imp, out_file, overwrite)
133205
return out_file
206+
207+
208+
def get_series_count_from_ome_metadata(path_to_file):
209+
"""Get the Bio-Formates series count from a file on disk.
210+
211+
Useful to access a specific image in a container format like .czi, .nd2, .lif...
212+
213+
Parameters
214+
----------
215+
path_to_file : str
216+
The full path to the image file.
217+
218+
Returns
219+
-------
220+
int
221+
The number of Bio-Formats series detected in the image file metadata.
222+
"""
223+
reader = ImageReader()
224+
ome_meta = MetadataTools.createOMEXMLMetadata()
225+
reader.setMetadataStore(ome_meta)
226+
reader.setId(path_to_file)
227+
series_count = reader.getSeriesCount()
228+
reader.close()
229+
230+
return series_count
231+
232+
233+
def write_bf_memoryfile(path_to_file):
234+
"""Write a BF memo-file so subsequent access to the same file is faster.
235+
236+
The Bio-Formats memo-file is written next to the image file (i.e. in the
237+
same folder as the given file).
238+
239+
Parameters
240+
----------
241+
path_to_file : string
242+
The full path to the image file.
243+
"""
244+
reader = Memoizer(ImageReader())
245+
reader.setId(path_to_file)
246+
reader.close()
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# @ File (label="Select the python-imcf-libs testdata directory", style="directory") PYTHON_IMCFLIBS_TESTDATA
2+
3+
4+
import os
5+
from imcflibs.pathtools import parse_path
6+
from imcflibs.imagej import bioformats
7+
8+
9+
components = parse_path("systems/lsm700/beads/10x_phmax.czi", PYTHON_IMCFLIBS_TESTDATA)
10+
full_path = components["full"]
11+
bfmemofile = components["path"] + "." + components["fname"] + ".bfmemo"
12+
13+
if os.path.isfile(bfmemofile):
14+
print("BF memory file [%s] already exists, removing..." % bfmemofile)
15+
os.remove(bfmemofile)
16+
17+
18+
bioformats.write_bf_memoryfile(full_path)
19+
20+
21+
if not os.path.isfile(bfmemofile):
22+
print("Test FAILED: can't find BF memory file [%s]" % bfmemofile)
23+
else:
24+
print("Test passed, BF memory file [%s] was created." % bfmemofile)
25+
26+
print(
27+
"""
28+
================================= IMPORTANT =================================
29+
Bio-Formats will only create a memory-file ONCE per session, meaning if this
30+
script is being run multiple times subsequently without closing ImageJ in
31+
between it will FAIL as the memory-file gets deleted by the script but will
32+
only be re-created by Bio-Formats in a FRESH ImageJ instance!!
33+
================================= IMPORTANT =================================
34+
"""
35+
)

0 commit comments

Comments
 (0)