Skip to content

Commit 58ab521

Browse files
committed
Add 'imagej.stitching' module
1 parent 506892f commit 58ab521

1 file changed

Lines changed: 116 additions & 0 deletions

File tree

src/imcflibs/imagej/stitching.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"""Functions to work on stitching datasets."""
2+
3+
from os.path import join
4+
5+
from java.lang.System import getProperty # pylint: disable-msg=import-error
6+
7+
import micrometa # pylint: disable-msg=import-error
8+
import ij # pylint: disable-msg=import-error
9+
10+
from imcflibs.imagej.misc import show_status, show_progress, error_exit
11+
from ..strtools import flatten
12+
from ..log import LOG as log
13+
14+
15+
def process_fluoview_project(infile):
16+
"""Process a FluoView mosaic project file.
17+
18+
Parameters
19+
----------
20+
infile : str
21+
The full path to either an `MATL_Mosaic.log` or any `*.omp2info` file
22+
from a FluoView MATL acquisition.
23+
24+
Returns
25+
-------
26+
micrometa.experiment.MosaicExperiment
27+
The mosaic object if parsing the project file has been successful.
28+
"""
29+
if infile[-9:] == '.omp2info':
30+
mosaicclass = micrometa.fluoview.FluoView3kMosaic
31+
elif infile[-4:] == '.log':
32+
mosaicclass = micrometa.fluoview.FluoViewMosaic
33+
else:
34+
error_exit('Unsupported input file: %s' % infile)
35+
36+
log.info("Parsing project file: [%s]", infile)
37+
ij.IJ.showStatus("Parsing mosaics...")
38+
39+
mosaics = mosaicclass(infile, runparser=False)
40+
total = len(mosaics.mosaictrees)
41+
ij.IJ.showProgress(0.0)
42+
show_status("Parsed %s / %s mosaics" % (0, total))
43+
for i, subtree in enumerate(mosaics.mosaictrees):
44+
log.info("Parsing mosaic %s...", i+1)
45+
try:
46+
mosaics.add_mosaic(subtree, i)
47+
except (ValueError, IOError) as err:
48+
log.warn('Skipping mosaic %s: %s', i, err)
49+
except RuntimeError as err:
50+
log.warn('Error parsing mosaic %s, SKIPPING: %s', i, err)
51+
show_progress(i, total)
52+
show_status("Parsed %s / %s mosaics" % (i+1, total))
53+
show_progress(total, total)
54+
show_status("Parsed %i mosaics." % total)
55+
56+
if not mosaics:
57+
error_exit("Couldn't find any (valid) mosaics in the project file!")
58+
log.info(mosaics.summarize())
59+
60+
return mosaics
61+
62+
63+
def gen_macro(mosaics, indir, outfile=None, opts=None):
64+
"""Generate stitcher macro code and optionally save it in a file.
65+
66+
Parameters
67+
----------
68+
mosaics : micrometa.experiment.MosaicExperiment
69+
The mosaic object of the stitching experiment.
70+
indir : str
71+
The path to use as input directory *INSIDE* the macro.
72+
outfile : str (optional)
73+
The path to a file for saving the generated macro code.
74+
opts : dict (optional)
75+
A dict to be passed on to micrometa.imagej.gen_stitching_macro().
76+
77+
Returns
78+
-------
79+
list(str)
80+
The generated macro code as a list of strings (one str per line).
81+
"""
82+
templates = join(getProperty('fiji.dir'), 'jars', 'python-micrometa.jar')
83+
log.info("Using macro templates from [%s].", templates)
84+
log.info("Using [%s] as base directory.", indir)
85+
86+
# set the default stitcher options
87+
stitcher_options = {
88+
'export_format': '".ids"',
89+
'split_z_slices': 'false',
90+
'rotation_angle': 0,
91+
'stitch_regression': 0.3,
92+
'stitch_maxavg_ratio': 2.5,
93+
'stitch_abs_displace': 3.5,
94+
'compute' : 'false',
95+
}
96+
# merge explicit options, overriding the defaults from above if applicable:
97+
if opts:
98+
stitcher_options.update(opts)
99+
100+
code = micrometa.imagej.gen_stitching_macro(
101+
name=mosaics.infile['dname'],
102+
path=indir,
103+
tplpfx='templates/imagej-macro/stitching',
104+
tplpath=templates,
105+
opts=stitcher_options
106+
)
107+
108+
log.debug("============= begin of generated macro code =============")
109+
log.debug(flatten(code))
110+
log.debug("============= end of generated macro code =============")
111+
112+
if outfile is not None:
113+
log.info('Writing stitching macro.')
114+
micrometa.imagej.write_stitching_macro(code, outfile)
115+
116+
return code

0 commit comments

Comments
 (0)