forked from imcf/python-imcflibs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmisc.py
More file actions
450 lines (357 loc) Β· 12.3 KB
/
misc.py
File metadata and controls
450 lines (357 loc) Β· 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""Miscellaneous ImageJ related functions, mostly convenience wrappers."""
import csv
import sys
import time
import smtplib
import os
from ij import IJ # pylint: disable-msg=import-error
from ij.plugin import ImageCalculator
from . import prefs
from ..log import LOG as log
def show_status(msg):
"""Update the ImageJ status bar and issue a log message.
Parameters
----------
msg : str
The message to display in the ImageJ status bar and log.
"""
log.info(msg)
IJ.showStatus(msg)
def show_progress(cur, final):
"""Update ImageJ's progress bar and print the current progress to the log.
Parameters
----------
cur : int
Current progress value.
final : int
Total value representing 100% completion.
Notes
-----
`ij.IJ.showProgress` internally increments the given `cur` value by 1.
"""
log.info("Progress: %s / %s (%s)", cur + 1, final, (1.0 + cur) / final)
IJ.showProgress(cur, final)
def error_exit(msg):
"""Log an error message and exit.
Parameters
----------
msg : str
The error message to log.
"""
log.error(msg)
sys.exit(msg)
def elapsed_time_since(start, end=None):
"""Generate a string with the time elapsed between two timepoints.
Parameters
----------
start : float
The start time, typically obtained via `time.time()`.
end : float, optional
The end time. If not given, the current time is used.
Returns
-------
str
The elapsed time formatted as `HH:MM:SS.ss`.
"""
if not end:
end = time.time()
hours, rem = divmod(end - start, 3600)
minutes, seconds = divmod(rem, 60)
return "{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), seconds)
def percentage(part, whole):
"""Calculate the percentage of a value based on total.
Parameters
----------
part : float
The portion value of a total.
whole : float
The total value.
Returns
-------
float
The percentage value.
"""
return 100 * float(part) / float(whole)
def calculate_mean_and_stdv(float_values):
"""Calculate mean and standard deviation from a list of floats.
Parameters
----------
float_values : list of float
List containing float numbers.
Returns
-------
tuple of (float, float)
Mean and standard deviation of the input list.
"""
mean = sum(float_values) / len(float_values)
tot = 0.0
for x in float_values:
tot = tot + (x - mean) ** 2
return [mean, (tot / (len(float_values))) ** 0.5]
def find_focus(imp):
"""Find the slice of a stack that is the best focused one.
First, calculate the variance of the pixel values in each slice. The slice
with the highest variance is considered the best focused as this typically
indicates more contrast and sharpness.
Parameters
----------
imp : ij.ImagePlus
A single-channel ImagePlus stack.
Returns
-------
int
The slice number of the best focused slice.
Raises
------
SystemExit
If the image has more than one channel.
Notes
-----
Currently only single-channel stacks are supported.
"""
imp_dimensions = imp.getDimensions()
# Check if more than 1 channel
# FUTURE Could be improved for multi channel
if imp_dimensions[2] != 1:
sys.exit("Image has more than one channel, please reduce dimensionality")
# Loop through each time point
for plane in range(1, imp_dimensions[4] + 1):
focused_slice = 0
norm_var = 0
imp.setT(plane)
# Loop through each z plane
for current_z in range(1, imp_dimensions[3] + 1):
imp.setZ(current_z)
pix_array = imp.getProcessor().getPixels()
mean = (sum(pix_array)) / len(pix_array)
pix_array = [(x - mean) * (x - mean) for x in pix_array]
# pix_array = pix_array*pix_array
sumpix_array = sum(pix_array)
var = sumpix_array / (imp_dimensions[0] * imp_dimensions[1] * mean)
if var > norm_var:
norm_var = var
focused_slice = current_z
return focused_slice
def send_mail(job_name, recipient, filename, total_execution_time):
"""Send an email using the SMTP server and sender email configured in ImageJ's Preferences.
Parameters
----------
job_name : string
Job name to display in the email.
recipient : string
Recipient's email address.
filename : string
The name of the file to be passed in the email.
total_execution_time : str
The time it took to process the file in the format [HH:MM:SS:ss].
"""
# Retrieve sender email and SMTP server from preferences
sender = prefs.Prefs.get("imcf.sender_email", "").strip()
server = prefs.Prefs.get("imcf.smtpserver", "").strip()
# Ensure the sender and server are configured from Prefs
if not sender:
log.info("Sender email is not configured. Please check IJ_Prefs.txt.")
return
if not server:
log.info("SMTP server is not configured. Please check IJ_Prefs.txt.")
return
# Ensure the recipient is provided
if not recipient.strip():
log.info("Recipient email is required.")
return
# Form the email subject and body
subject = "Your {0} job finished successfully".format(job_name)
body = (
"Dear recipient,\n\n"
"This is an automated message.\n"
"Your dataset '{0}' has been successfully processed "
"({1} [HH:MM:SS:ss]).\n\n"
"Kind regards,\n"
"The IMCF-team"
).format(filename, total_execution_time)
# Form the complete message
message = ("From: {0}\nTo: {1}\nSubject: {2}\n\n{3}").format(
sender, recipient, subject, body
)
# Try sending the email, print error message if it wasn't possible
try:
smtpObj = smtplib.SMTP(server)
smtpObj.sendmail(sender, recipient, message)
log.debug("Successfully sent email")
except smtplib.SMTPException as err:
log.warning("Error: Unable to send email: %s", err)
return
def progressbar(progress, total, line_number, prefix=""):
"""Progress bar for the IJ log window.
Show a progress bar in the log window of Fiji at a specific line independent
of the main Fiji progress bar.
Parameters
----------
progress : int
Current step of the loop.
total : int
Total number of steps for the loop.
line_number : int
Number of the line to be updated.
prefix : str, optional
Text to use before the progress bar, by default an empty string.
"""
size = 20
x = int(size * progress / total)
IJ.log(
"\\Update%i:%s[%s%s] %i/%i\r"
% (
line_number,
timed_log(prefix, True),
"#" * x,
"." * (size - x),
progress,
total,
)
)
def timed_log(message, as_string=False):
"""Print a message to the ImageJ log window, prefixed with a timestamp.
If `as_string` is set to True, nothgin will be printed to the log window,
instead the formatted log message will be returned as a string.
Parameters
----------
message : str
Message to print
as_string : bool, optional
Flag to request the formatted string to be returned instead of printing
it to the log. By default False.
"""
formatted = time.strftime("%H:%M:%S", time.localtime()) + ": " + message + " "
if as_string:
return formatted
IJ.log(formatted)
def get_free_memory():
"""Get the free memory that is available to ImageJ.
Returns
-------
free_memory : int
The free memory in bytes.
"""
max_memory = int(IJ.maxMemory())
used_memory = int(IJ.currentMemory())
free_memory = max_memory - used_memory
return free_memory
def setup_clean_ij_environment(rm=None, rt=None): # pylint: disable-msg=unused-argument
"""Set up a clean and defined ImageJ environment.
This funtion clears the active results table, the ROI manager, and the log.
Additionally, it closes all open images and resets the ImageJ options,
performing a [*Fresh Start*][fresh_start].
Parameters
----------
rm : RoiManager, optional
Will be ignored (kept for keeping API compatibility).
rt : ResultsTable, optional
Will be ignored (kept for keeping API compatibility).
Notes
-----
"Fresh Start" is described in the [ImageJ release notes][ij_relnotes],
following a [suggestion by Robert Haase][fresh_start].
[ij_relnotes]: https://imagej.nih.gov/ij/notes.html
[fresh_start]: https://forum.image.sc/t/43102
"""
IJ.run("Fresh Start", "")
IJ.log("\\Clear")
prefs.fix_ij_options()
def sanitize_image_title(imp):
"""Remove special chars and various suffixes from the title of an ImagePlus.
Parameters
----------
imp : ImagePlus
The ImagePlus to be renamed.
Notes
-----
The function removes the full path of the image file (if present), retaining
only the base filename using `os.path.basename()`.
"""
# sometimes (unclear when) the title contains the full path, therefore we
# simply call `os.path.basename()` on it to remove all up to the last "/":
image_title = os.path.basename(imp.getTitle())
image_title = image_title.replace(".czi", "")
image_title = image_title.replace(" ", "_")
image_title = image_title.replace("_-_", "")
image_title = image_title.replace("__", "_")
image_title = image_title.replace("#", "Series")
imp.setTitle(image_title)
def subtract_images(imp1, imp2):
"""Subtract one image from the other (imp1 - imp2).
Parameters
----------
imp1: ij.ImagePlus
The ImagePlus that is to be subtracted from.
imp2: ij.ImagePlus
The ImagePlus that is to be subtracted.
Returns
-------
ij.ImagePlus
The ImagePlus resulting from the subtraction.
"""
ic = ImageCalculator()
subtracted = ic.run("Subtract create", imp1, imp2)
return subtracted
def close_images(list_of_imps):
"""Close all open ImagePlus objects given in a list.
Parameters
----------
list_of_imps: list(ij.ImagePlus)
A list of open ImagePlus objects.
"""
for imp in list_of_imps:
imp.changes = False
imp.close()
def get_threshold_value_from_method(imp, method, ops):
"""Get the value of a selected AutoThreshold method for the given ImagePlus.
This is useful to figure out which threshold value will be calculated by the
selected method for the given stack *without* actually having to apply it.
Parameters
----------
imp : ij.ImagePlus
The image from which to get the threshold value.
method : {'huang', 'ij1', 'intermodes', 'isoData', 'li', 'maxEntropy',
'maxLikelihood', 'mean', 'minError', 'minimum', 'moments', 'otsu',
'percentile', 'renyiEntropy', 'rosin', 'shanbhag', 'triangle', 'yen'}
The AutoThreshold method to use.
ops: ops.OpService
The ImageJ Ops service instance, usually retrieved through a _Script
Parameter_ at the top of the script, as follows:
```
#@ OpService ops
```
Returns
-------
int
The threshold value chosen by the selected method.
"""
histogram = ops.run("image.histogram", imp)
threshold_value = ops.run("threshold.%s" % method, histogram)
threshold_value = int(round(threshold_value.get()))
return threshold_value
def write_results(out_file, content):
"""Write the results to a csv file.
Parameters
----------
out_file : str
Path to the output file.
content : list of OrderedDict
List of dictionaries representing the results.
"""
# Check if the output file exists
if not os.path.exists(out_file):
# If the file does not exist, create it and write the header
with open(out_file, "wb") as f:
dict_writer = csv.DictWriter(
f, content[0].keys(), delimiter=";"
)
dict_writer.writeheader()
dict_writer.writerows(content)
else:
# If the file exists, append the results
with open(out_file, "ab") as f:
dict_writer = csv.DictWriter(
f, content[0].keys(), delimiter=";"
)
dict_writer.writerows(content)