Skip to content

Commit a7fbd1c

Browse files
committed
Merge branch 'code-laurent-20230418' into devel
2 parents 6aca96b + 0e7f601 commit a7fbd1c

8 files changed

Lines changed: 116 additions & 72 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@
99
* `pathtools.join2` can be used to join paths, much like `os.path.join` except
1010
that it will work with `java.io.File` objects as well (but doesn't support
1111
more than two path components / parameters).
12+
* `resultstable` sub-module with the following functions:
13+
* `add_results_to_resultstable`
14+
* `get_resultstable`
15+
* `preset_results_column`
16+
* `misc.timed_log` for printing log messages with a timestamp.
17+
* Several functions in the `misc` submodule:
18+
* `calculate_mean_and_stdv`
19+
* `elapsed_time_since`
20+
* `find_focus`
21+
* `get_free_memory`
22+
* `percentage`
23+
* `progressbar`
24+
* `setup_clean_ij_environment`
25+
* `timed_log`
1226

1327
### Changed
1428

src/imcflibs/imagej/bioformats.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def export(imp, filename, overwrite=False):
137137
138138
Parameters
139139
----------
140-
imp : ImagePlus
140+
imp : ij.ImagePlus
141141
The ImagePlus object to be exported by Bio-Formats.
142142
filename : str
143143
The output filename, may include a full path.
@@ -178,7 +178,7 @@ def export_using_orig_name(imp, path, orig_name, tag, suffix, overwrite=False):
178178
179179
Parameters
180180
----------
181-
imp : ImagePlus
181+
imp : ij.ImagePlus
182182
The ImagePlus object to be exported by Bio-Formats.
183183
path : str or object that can be cast to a str
184184
The output path.

src/imcflibs/imagej/gpu.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ def erode_labels(clij2_instance, label_image, erosion_radius, channel=None):
88
99
Parameters
1010
----------
11-
clij2_instance : clij2_instance
11+
clij2_instance : net.haesleinhuepf.clij.CLIJ
1212
Instance of CLIJ to communicate with the GPU.
13-
label_image : ImagePlus
13+
label_image : ij.ImagePlus
1414
Label image to be eroded.
1515
erosion_radius : int
1616
Radius for erosion.
@@ -19,15 +19,14 @@ def erode_labels(clij2_instance, label_image, erosion_radius, channel=None):
1919
2020
Returns
2121
-------
22-
ImagePlus
22+
ij.ImagePlus
2323
Label image with eroded labels.
2424
"""
2525

2626
list_of_images = []
2727
channel_list = [channel] if channel else range(1, label_image.getNChannels() + 1)
2828

2929
for channel in channel_list:
30-
3130
current_channel = Duplicator().run(
3231
label_image,
3332
channel,
@@ -59,9 +58,9 @@ def dilate_labels(clij2_instance, label_image, dilation_radius, channel=None):
5958
6059
Parameters
6160
----------
62-
clij2_instance : clij2_instance
61+
clij2_instance : net.haesleinhuepf.clij.CLIJ
6362
Instance of CLIJ to communicate with the GPU.
64-
label_image : ImagePlus
63+
label_image : ij.ImagePlus
6564
Label Image to be dilated.
6665
erosion_radius : int
6766
Radius for dilation.
@@ -70,7 +69,7 @@ def dilate_labels(clij2_instance, label_image, dilation_radius, channel=None):
7069
7170
Returns
7271
-------
73-
ImagePlus
72+
ij.ImagePlus
7473
Label image with dilated labels.
7574
"""
7675

@@ -114,16 +113,16 @@ def merge_labels(clij2_instance, label_image, channel=None):
114113
115114
Parameters
116115
----------
117-
clij2_instance : clij2_instance
116+
clij2_instance : net.haesleinhuepf.clij.CLIJ
118117
Instance of CLIJ to communicate with the GPU.
119-
label_image : ImagePlus
118+
label_image : ij.ImagePlus
120119
Label image with touching labels.
121120
channel : int, optional
122121
Specific channel to apply label merging.
123122
124123
Returns
125124
-------
126-
ImagePlus
125+
ij.ImagePlus
127126
New ImagePlus with merged labels.
128127
"""
129128

@@ -135,7 +134,6 @@ def merge_labels(clij2_instance, label_image, channel=None):
135134
channel_list = [channel]
136135

137136
for channel in channel_list:
138-
139137
current_channel = Duplicator().run(
140138
label_image,
141139
channel,

src/imcflibs/imagej/labelimage.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
"""Functions to work with ImageJ label images."""
22

3-
# pylint: disable-msg=import-error
4-
53
from ij import IJ, ImagePlus, Prefs
64
from ij.plugin import Duplicator, ImageCalculator
5+
from ij.plugin.filter import ImageProcessor, ThresholdToSelection
76
from ij.process import FloatProcessor, ImageProcessor
8-
from ij.plugin.filter import ThresholdToSelection
9-
107
from inra.ijpb.label import LabelImages as li
118
from inra.ijpb.plugins import AnalyzeRegions
129

10+
1311
def label_image_to_roi_list(label_image, low_thresh=None):
1412
"""Converts a label image to a list of ROIs
1513
1614
Parameters
1715
----------
18-
label_image : ImagePlus
16+
label_image : ij.ImagePlus
1917
Label image to convert
2018
low_thresh : int, optional
2119
Value under which the labels should be discarded, by default None
@@ -73,14 +71,14 @@ def relate_label_images(label_image_ref, label_image_to_relate):
7371
7472
Parameters
7573
----------
76-
label_image_ref : ImagePlus
74+
label_image_ref : ij.ImagePlus
7775
Reference to use for the labels
78-
label_image_to_relate : ImagePlus
76+
label_image_to_relate : ij.ImagePlus
7977
Image to change for the labels
8078
8179
Returns
8280
-------
83-
ImagePlus
81+
ij.ImagePlus
8482
New ImagePlus with modified labels matching the reference
8583
"""
8684

@@ -97,7 +95,7 @@ def filter_objects(label_image, table, string, min_val, max_val):
9795
9896
Parameters
9997
----------
100-
label_image : ImagePlus
98+
label_image : ij.ImagePlus
10199
Label image on which to filter
102100
table : ResultsTable
103101
ResultsTable containing all the measurements on which to filter
@@ -111,7 +109,7 @@ def filter_objects(label_image, table, string, min_val, max_val):
111109
112110
Returns
113111
-------
114-
ImagePlus
112+
ij.ImagePlus
115113
Label image containing only the remaining labels
116114
"""
117115

@@ -129,7 +127,7 @@ def measure_objects_size_shape_2d(label_image):
129127
130128
Parameters
131129
----------
132-
label_image : ImagePlus
130+
label_image : ij.ImagePlus
133131
Label image on which to get the shapes
134132
135133
Returns

src/imcflibs/imagej/misc.py

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import time
55

66
from ij import IJ # pylint: disable-msg=import-error
7-
from ij.plugin.frame import RoiManager # pylint: disable-msg=import-error
87
from ij.measure import ResultsTable # pylint: disable-msg=import-error
8+
from ij.plugin.frame import RoiManager # pylint: disable-msg=import-error
99

1010
from ..log import LOG as log
1111

@@ -30,19 +30,18 @@ def error_exit(msg):
3030

3131

3232
def elapsed_time_since(start, end=None):
33-
"""Prints the elapsed time for execution
33+
"""Generate a string with the time elapsed between the two timepoints.
3434
3535
Parameters
3636
----------
37-
start : time
38-
Start time
39-
end : time, optional
40-
End time
37+
start : time.time
38+
Start time.
39+
end : time.time, optional
40+
End time. If skipped the current time will be used.
4141
4242
Returns
4343
-------
4444
str
45-
Formatted time elapsed
4645
"""
4746

4847
if not end:
@@ -54,19 +53,18 @@ def elapsed_time_since(start, end=None):
5453

5554

5655
def percentage(part, whole):
57-
"""Returns the percentage of a value based on total
56+
"""Calculate the percentage of a value based on total.
5857
5958
Parameters
6059
----------
6160
part : float
62-
Part
61+
Part.
6362
whole : float
64-
Complete size
63+
Complete size.
6564
6665
Returns
6766
-------
6867
float
69-
Percentage
7068
"""
7169
return 100 * float(part) / float(whole)
7270

@@ -82,7 +80,7 @@ def calculate_mean_and_stdv(float_values):
8280
Returns
8381
-------
8482
[float, float]
85-
Mean and standard deviation of the list.
83+
Mean (1st item) and standard deviation (2nd item) of the list.
8684
"""
8785
mean = sum(float_values) / len(float_values)
8886
tot = 0.0
@@ -92,17 +90,21 @@ def calculate_mean_and_stdv(float_values):
9290

9391

9492
def find_focus(imp):
95-
"""Function to get the focused stack. Works on single-channel images only.
93+
"""Find the slice of a stack that seems to bet the best focused one.
94+
95+
NOTE: currently only single-channel stacks are supported.
96+
97+
FIXME: explain what the function is actually doing, i.e. how does it decide
98+
what "the best focused one" is?
9699
97100
Parameters
98101
----------
99-
imp : ImagePlus
102+
imp : ij.ImagePlus
100103
A single-channel ImagePlus.
101104
102105
Returns
103106
-------
104107
int
105-
Slice number which seems to be the best focused one.
106108
"""
107109

108110
imp_dimensions = imp.getDimensions()
@@ -138,8 +140,8 @@ def find_focus(imp):
138140
def progressbar(progress, total, line_number, prefix=""):
139141
"""Progress bar for the IJ log window.
140142
141-
FIXME: how is this different from show_progressbar() above? Please explain
142-
in the function description here.
143+
Show a progress bar in the log window of Fiji at a specific line independent
144+
of the main Fiji progress bar.
143145
144146
Parameters
145147
----------
@@ -153,14 +155,34 @@ def progressbar(progress, total, line_number, prefix=""):
153155
Text to use before the progress bar, by default ''.
154156
"""
155157

156-
size = 30
158+
size = 20
157159
x = int(size * progress / total)
158160
IJ.log(
159161
"\\Update%i:%s[%s%s] %i/%i\r"
160-
% (line_number, prefix, "#" * x, "." * (size - x), progress, total)
162+
% (
163+
line_number,
164+
timed_log(prefix, True),
165+
"#" * x,
166+
"." * (size - x),
167+
progress,
168+
total,
169+
)
161170
)
162171

163172

173+
def timed_log(message, as_string=False):
174+
"""Print a message to the ImageJ log window with a timestamp added.
175+
176+
Parameters
177+
----------
178+
message : str
179+
Message to print
180+
"""
181+
if as_string:
182+
return time.strftime("%H:%M:%S", time.localtime()) + ": " + message + " "
183+
IJ.log(time.strftime("%H:%M:%S", time.localtime()) + ": " + message + " ")
184+
185+
164186
def get_free_memory():
165187
"""Get the free memory thats available to ImageJ.
166188
@@ -186,16 +208,11 @@ def setup_clean_ij_environment(rm=None, rt=None):
186208
rt : ResultsTable, optional
187209
A reference to an IJ-ResultsTable instance.
188210
"""
189-
# FIXME: use function(s) from the "roimanager" module!
190211
if not rm:
191-
rm = RoiManager.getInstance()
192-
if not rm:
193-
rm = RoiManager()
212+
rm = roimanager.get_roimanager()
194213

195214
if not rt:
196-
rt = ResultsTable.getInstance()
197-
if not rt:
198-
rt = ResultsTable()
215+
rt = resultstable.get_resultstable()
199216

200217
rm.runCommand("reset")
201218
rt.reset()

0 commit comments

Comments
 (0)