-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_B_branch_processing.py
1431 lines (1252 loc) · 56.3 KB
/
auto_B_branch_processing.py
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# @ File(label='Choose a directory with datasets', style='directory') datasets_dir
# @ File(label='Choose a file with metadata about embryo directions', style='file') metadata_file
# @ String(label='Dataset prefix', value='MGolden2022A-') dataset_name_prefix
# @ Boolean (label='Compress images?', value=true) compress_on_save
# @ Boolean (label='Use previously cropped stacks (if present)?', value=false) use_cropped_cache
# @ Boolean (label='Do histogram matching adjustment?', value=true) do_histogram_matching
# @ Integer (label='Percentage of overexposed pixels during histogram contrast adjustment', value=1) PERCENT_OVEREXPOSED_PIXELS
# Written by Artemiy Golden on Jan 2022 at AK Stelzer Group at Goethe Universitaet Frankfurt am Main
# For detailed documentation go to https://github.com/artgolden/fiji_scripts or read the README.md
# Last manual update of this line 2022.5.18 :)
from distutils.dir_util import mkpath
import math
import os
import re
import fnmatch
import json
import logging
from datetime import datetime
import traceback
from java.io import File
from ij.io import FileSaver
from ij import IJ, ImagePlus, ImageStack, WindowManager
from ij.plugin.filter import RankFilters
from fiji.threshold import Auto_Threshold
from ij.plugin.filter import ParticleAnalyzer
from ij.measure import ResultsTable
from ij.measure import Measurements
from ij.plugin.frame import RoiManager
from ij.process import ImageProcessor
from ij.plugin import RoiRotator
from ij.plugin import ZProjector
from ij.plugin import Slicer
from ij.plugin import StackCombiner
from ij.plugin import StackMaker
from ij.process import ByteProcessor
from ij.io import RoiDecoder
from ij.gui import PointRoi, RotatedRectRoi
from emblcmci import BleachCorrection_MH
#TODO:
# What to do if embryo is off-center so much that start_plane = int(round(middle_y - 75))
# gives negative values? Make a better solution than just shifting the 150 plane crop.
EXAMPLE_JSON_METADATA_FILE = """
Example JSON metadata file contents:
{
"datasets": [
{
"ID": 1,
"channel_1": {
"specimens_for_directions_1234": [
5,
6,
4,
7
]
},
"head_direction": "right",
"use_manual_bounding_box": false
},
{
"ID": 3,
"channel_1": {
"specimens_for_directions_1234": [
5,
6,
4,
7
]
},
"channel_2": {
"specimens_for_directions_1234": [
0,
2,
1,
3
]
},
"head_direction": "left",
"use_manual_bounding_box": true,
"planes_to_keep_per_direction": [
{
"start": 1,
"end": 150
},
{
"start": 10,
"end": 160
},
{
"start": 1,
"end": 150
},
{
"start": 1,
"end": 150
}
]
}
]
}"""
METADATA_DIR_NAME = "(B1)-Metadata"
TSTACKS_DIR_NAME = "(B3)-TStacks-ZM"
RAW_IMAGES_DIR_NAME = "(P0)-ZStacks-Raw"
RAW_CROPPED_DIR_NAME = "(B2)-ZStacks"
CONTRAST_DIR_NAME = "(B4)-TStacks-ZN"
MONTAGE_DIR_NAME = "(B5)-TStacks-ZN-Montage"
DATASET_ERROR_FILE_NAME = "B_BRANCH_ERRORED"
DATASET_FINISHED_FILE_NAME = "B_BRANCH_FINISHED"
DATASET_ACTIVE_FILE_NAME = "B_BRANCH_ACTIVE"
MANUAL_CROP_BOX_FILE_NAME = "manual_crop_box"
DEFAULT_CROP_BOX_WIDTH = 1100
MINIMUM_CROP_BOX_WIDTH = 1000
# Percentage of overexposed pixels during histogram contrast adjustment
# PERCENT_OVEREXPOSED_PIXELS = 1
class FredericFile:
"""
File naming for light-sheet image files. Frederic Strobl™
Example file name: MGolden2022A-DS0001TP0001DR0001CH0001PL(ZS).tif
:param file_name: full image file name
:type file_name: str
"""
dataset_name = ""
dataset_id = 0
time_point = ""
direction = ""
channel = ""
plane = ""
extension = ""
def __init__(self, file_name):
name_parts = re.split("-DS|TP|DR|CH|PL|\.", file_name)
if len(name_parts) != 7:
raise Exception(
"Image file name is improperly formatted! Check documentation inside the script.")
self.dataset_name, self.dataset_id, self.time_point, self.direction, self.channel, self.plane, self.extension = name_parts
self.extension.lower()
def get_name(self):
return "%s-DS%sTP%sDR%sCH%sPL%s.%s" % (self.dataset_name,
self.dataset_id,
self.time_point,
self.direction,
self.channel,
self.plane,
self.extension)
def process_datasets(datasets_dir, metadata_file, dataset_name_prefix):
"""Main function for processing the datasets.
Args:
datasets_dir (java.io.File): directory with subdirectories for each dataset (input from the user)
metadata_file (java.io.File): JSON file with metadata for each dataset (input from the user)
dataset_name_prefix (str): Prefix that will be added to all image files for all datasets (input from the user)
"""
# Converting a Java File object to a string.
if isinstance(metadata_file, File):
metadata_file = metadata_file.getAbsolutePath()
if isinstance(datasets_dir, File):
datasets_dir = datasets_dir.getAbsolutePath()
with open(metadata_file) as f:
try:
datasets_meta = json.load(f)
except ValueError as err:
print("Could not load the JSON metadata file.")
print("Error generated by the JSON parser: \n%s" % err)
print(EXAMPLE_JSON_METADATA_FILE)
exit(1)
now = datetime.now()
dt_string = now.strftime("%Y-%b-%d-%H%M%S")
logging.basicConfig(filename=os.path.join(datasets_dir, "%s-b_branch.log" % dt_string),
filemode='w',
format='%(asctime)s-%(levelname)s - %(message)s',
datefmt='%d-%b-%y %H:%M:%S',
level=logging.INFO)
# Check metadata file for correctness
for dataset in datasets_meta["datasets"]:
if "ID" not in dataset:
print("Error while parsing .json file. Did not find a dataset ID for one of the datsets. Exiting.")
exit(1)
dataset_id = dataset["ID"]
if "head_direction" not in dataset:
print("Error while parsing .json file: no head_direction for the dataset with ID: \"%s\". Exiting." % dataset_id)
print(EXAMPLE_JSON_METADATA_FILE)
exit(1)
specimen_directions_in_channels = []
for chan in range(3):
channel = "channel_%s" % chan
if channel in dataset:
if "specimens_for_directions_1234" not in dataset[channel]:
print("Error while parsing .json file: no specimens_for_directions_1234 field in channel_%s for the dataset with ID: \"%s\". Exiting." % (channel, dataset_id))
print(EXAMPLE_JSON_METADATA_FILE)
exit(1)
specimen_directions_in_channels.append(
tuple(dataset[channel]["specimens_for_directions_1234"]))
specimen_directions_in_channels = tuple(specimen_directions_in_channels)
if not is_dataset_ID_input_valid(dataset_id):
print("Error while parsing .json file: not a valid dataset ID: \"%s\". Dataset ID should be an integer from 0 to 9999. Exiting." % dataset_id)
print(EXAMPLE_JSON_METADATA_FILE)
exit(1)
for specimens in specimen_directions_in_channels:
if not is_specimen_input_valid(specimens):
print("Error while parsing .json file: not a valid specimen list \"%s\" for the dataset with ID: \"%s\". Exiting." % (
specimens, dataset_id))
print(EXAMPLE_JSON_METADATA_FILE)
exit(1)
if not dataset["head_direction"] in ["right", "left"]:
print("Error while parsing .json file: not a valid head_direction \"%s\" for the dataset with ID: \"%s\". Exiting." % (
dataset["head_direction"], dataset_id))
print(EXAMPLE_JSON_METADATA_FILE)
exit(1)
if specimen_directions_in_channels == ():
print("Error while parsing .json file: no channels found for the dataset with ID: \"%s\". Exiting." % dataset_id)
print(EXAMPLE_JSON_METADATA_FILE)
exit(1)
raw_images_dir = get_raw_images_dir(datasets_dir, dataset_id)
if not raw_images_dir:
print("Exiting.")
exit(1)
logging.info("Checked metadata file. No errors found.")
for dataset in datasets_meta["datasets"]:
skip_the_dataset = False
dataset_id = dataset["ID"]
specimen_directions_in_channels = []
for chan in range(3):
channel = "channel_%s" % chan
if channel in dataset:
specimen_directions_in_channels.append(
tuple(dataset[channel]["specimens_for_directions_1234"]))
specimen_directions_in_channels = tuple(specimen_directions_in_channels)
ndirections = len(specimen_directions_in_channels[0])
logging.info("\n%s\nStarted processing dataset: DS%04d \n%s\n" %
("#" * 100, dataset_id, "#" * 100))
print("Started processing dataset: DS%04d" % dataset_id)
raw_images_dir = get_raw_images_dir(datasets_dir, dataset_id)
root_dataset_dir = os.path.split(raw_images_dir)[0]
if os.path.exists(os.path.join(root_dataset_dir, DATASET_FINISHED_FILE_NAME)):
logging.info(
"Found %s file. Dataset DS%04d already processed, skipping." % (DATASET_FINISHED_FILE_NAME, dataset_id))
print("Found %s file. Dataset DS%04d already processed, skipping." %
(DATASET_FINISHED_FILE_NAME, dataset_id))
continue
if os.path.exists(os.path.join(root_dataset_dir, DATASET_ERROR_FILE_NAME)):
logging.info(
"Found %s file. Dataset DS%04d errored while previous processing, skipping." % (DATASET_ERROR_FILE_NAME, dataset_id))
print("Found %s file. Dataset DS%04d errored while previous processing, skipping." %
(DATASET_ERROR_FILE_NAME, dataset_id))
continue
if os.path.exists(os.path.join(root_dataset_dir, DATASET_ACTIVE_FILE_NAME)):
logging.info(
"Found %s file. Perhaps the dataset DS%04d is currently being processed by other Fiji instance, skipping." % (DATASET_ACTIVE_FILE_NAME, dataset_id))
print("Found %s file. Perhaps the dataset DS%04d is currently being processed by other Fiji instance, skipping." %
(DATASET_ACTIVE_FILE_NAME, dataset_id))
continue
open(os.path.join(root_dataset_dir, DATASET_ACTIVE_FILE_NAME), 'a').close()
logging.info("\tArranging raw image files")
try:
move_files(
raw_images_dir, specimen_directions_in_channels, dataset_id, dataset_name_prefix)
except ValueError as e:
logging.info(
"Error while moving files for the dataset:\"%s\", skipping the dataset. Error:\n %s" % (dataset_id, e))
print("Error while moving files for the dataset:\"%s\", skipping the dataset." % dataset_id)
print(e)
continue
logging.info("\tFiles arranged, starting processing.")
# We have to make sure that all directions in all channels have the same crop box dimensions.
dataset_minimal_crop_box_dims = (0, 0)
channel = 1
chan_dir_name = "CH%04d" % channel
raw_images_direction_dirs = make_directions_dirs(
os.path.join(raw_images_dir, chan_dir_name), ndirections)
meta_dir = os.path.join(root_dataset_dir, METADATA_DIR_NAME)
meta_d_dirs = make_directions_dirs(os.path.join(meta_dir, chan_dir_name), ndirections)
tstack_dataset_dirs = make_directions_dirs(
os.path.join(root_dataset_dir, TSTACKS_DIR_NAME, chan_dir_name), ndirections)
raw_cropped_dirs = make_directions_dirs(os.path.join(
root_dataset_dir, RAW_CROPPED_DIR_NAME, chan_dir_name), ndirections)
# Loop one time over all dimensions in channel 1 to determine the dataset_maximal_crop_box_width
for raw_dir, tstack_dir, m_dir, direction in zip(raw_images_direction_dirs, tstack_dataset_dirs, meta_d_dirs, range(1, ndirections + 1)):
logging.info(
"\tCreating crop templates for all directions in CH0001 to ensure that all crop templates have the same dimensions.")
logging.info("\tChannel: %s Direction: %s In the loop over directions. This iteration operating on the following directories:" % (
channel, direction))
logging.info("\n\t\t\t\t\t\t%s\n\t\t\t\t\t\t%s\n\t\t\t\t\t\t%s\n" %
(raw_dir, tstack_dir, m_dir))
tstack_backup_dir = os.path.join(tstack_dir, "uncropped_backup")
if not os.path.exists(tstack_backup_dir):
os.mkdir(tstack_backup_dir)
logging.info("\tChannel: %s Direction: %s Creating a stack of max Z-projections from raw stack." %
(channel, direction))
mproj_stack_file_name = get_tiff_name_from_dir(raw_dir)
mproj_stack_file_name.plane = "(ZM)"
mproj_stack_file_name.time_point = "(TS)"
mproj_stack_path = os.path.join(
tstack_backup_dir, mproj_stack_file_name.get_name())
if not os.path.exists(mproj_stack_path):
max_proj_stack = make_max_Z_projections_for_folder(raw_dir)
fs = FileSaver(max_proj_stack)
fs.saveAsTiff(mproj_stack_path)
else:
max_proj_stack = IJ.openImage(mproj_stack_path)
logging.info(
"\tChannel: %s Direction: %s Found existing max Z-projections. Using them." % (channel, direction))
max_time_proj_file_name = mproj_stack_file_name
max_time_proj_file_name.time_point = "(TM)"
max_time_proj_full_path = os.path.join(
m_dir, max_time_proj_file_name.get_name())
if not os.path.exists(max_time_proj_full_path):
max_time_proj = project_a_stack(max_proj_stack)
fs = FileSaver(max_time_proj)
fs.saveAsTiff(max_time_proj_full_path)
else:
logging.info(
"\tChannel: %s Direction: %s Found existing max TIME-projection, using it. \n\t\t\t\t\t%s" % (channel, direction, max_time_proj_full_path))
max_time_proj = IJ.openImage(max_time_proj_full_path)
logging.info("\tChannel: %s Direction: %s Creating a crop template from a stack of max projections." % (
channel, direction))
try:
crop_template, cropped_max_time_proj, dataset_minimal_crop_box_dims = create_crop_template(
max_time_proj, m_dir, dataset, dataset_minimal_crop_box_dims, use_dataset_box_dims=False)
except Exception as e:
logging.info(
"ERROR: Encountered an exception while trying to create a crop template. Skipping the dataset. Exception:\n%s" % e)
print("ERROR: Encountered an exception while trying to create a crop template. Skipping the dataset. Exception:\n%s" % e)
skip_the_dataset = True
open(os.path.join(root_dataset_dir, DATASET_ERROR_FILE_NAME), 'a').close()
os.remove(os.path.join(root_dataset_dir, DATASET_ACTIVE_FILE_NAME))
break
fs = FileSaver(cropped_max_time_proj)
fs.saveAsTiff(os.path.join(
m_dir, "cropped_max_time_proj.tif"))
# Main loop over the channels and directions for the dataset
for channel, _ in enumerate(specimen_directions_in_channels, start=1):
if skip_the_dataset == True:
break
chan_dir_name = "CH%04d" % channel
raw_images_direction_dirs = make_directions_dirs(
os.path.join(raw_images_dir, chan_dir_name), ndirections)
root_dataset_dir = os.path.split(raw_images_dir)[0]
meta_dir = os.path.join(root_dataset_dir, METADATA_DIR_NAME)
meta_d_dirs = make_directions_dirs(os.path.join(meta_dir, chan_dir_name), ndirections)
tstack_dataset_dirs = make_directions_dirs(
os.path.join(root_dataset_dir, TSTACKS_DIR_NAME, chan_dir_name), ndirections)
raw_cropped_dirs = make_directions_dirs(os.path.join(
root_dataset_dir, RAW_CROPPED_DIR_NAME, chan_dir_name), ndirections)
contrast_dirs = make_directions_dirs(os.path.join(
root_dataset_dir, CONTRAST_DIR_NAME, chan_dir_name), ndirections)
for direction, raw_dir, tstack_dir, m_dir, raw_cropped_dir in zip(range(1, ndirections + 1), raw_images_direction_dirs, tstack_dataset_dirs, meta_d_dirs, raw_cropped_dirs):
if skip_the_dataset == True:
break
# if direction > 1:
# print "Skipping all other directions for testing"
# break
logging.info("\tChannel: %s Direction: %s In the loop over directions. This iteration operating on the following directories:" % (
channel, direction))
logging.info("\n\t\t\t\t\t\t%s\n\t\t\t\t\t\t%s\n\t\t\t\t\t\t%s\n\t\t\t\t\t\t%s\n" % (
raw_dir, tstack_dir, m_dir, raw_cropped_dir))
tstack_backup_dir = os.path.join(tstack_dir, "uncropped_backup")
if not os.path.exists(tstack_backup_dir):
os.mkdir(tstack_backup_dir)
logging.info(
"\tChannel: %s Direction: %s Creating a stack of max Z-projections from raw stack." % (channel, direction))
mproj_stack_file_name = get_tiff_name_from_dir(raw_dir)
mproj_stack_file_name.plane = "(ZM)"
mproj_stack_file_name.time_point = "(TS)"
mproj_stack_path = os.path.join(
tstack_backup_dir, mproj_stack_file_name.get_name())
if not os.path.exists(mproj_stack_path):
max_proj_stack = make_max_Z_projections_for_folder(raw_dir)
save_tiff(max_proj_stack, mproj_stack_path)
else:
max_proj_stack = IJ.openImage(mproj_stack_path)
logging.info(
"\tChannel: %s Direction: %s Found existing max Z-projections. Using them." % (channel, direction))
max_time_proj_file_name = mproj_stack_file_name
max_time_proj_file_name.time_point = "(TM)"
max_time_proj_full_path = os.path.join(
m_dir, max_time_proj_file_name.get_name())
if not os.path.exists(max_time_proj_full_path):
max_time_proj = project_a_stack(max_proj_stack)
fs = FileSaver(max_time_proj)
fs.saveAsTiff(max_time_proj_full_path)
else:
logging.info(
"\tChannel: %s Direction: %s Found existing max TIME-projection, using it. \n\t\t\t\t\t%s" % (channel, direction, max_time_proj_full_path))
max_time_proj = IJ.openImage(max_time_proj_full_path)
logging.info("\tChannel: %s Direction: %s Creating a crop template from a stack of max projections." % (
channel, direction))
try:
crop_template, cropped_max_time_proj, dataset_minimal_crop_box_dims = create_crop_template(
max_time_proj, m_dir, dataset, dataset_minimal_crop_box_dims, use_dataset_box_dims=True)
except Exception as e:
traceback.print_exc()
logging.info(
"ERROR: Encountered an exception while trying to create a crop template. Skipping the dataset. Exception:\n%s" % e)
print("ERROR: Encountered an exception while trying to create a crop template. Skipping the dataset. Exception:\n%s" % e)
skip_the_dataset = True
open(os.path.join(root_dataset_dir, DATASET_ERROR_FILE_NAME), 'a').close()
os.remove(os.path.join(root_dataset_dir, DATASET_ACTIVE_FILE_NAME))
break
fs = FileSaver(cropped_max_time_proj)
fs.saveAsTiff(os.path.join(
m_dir, "cropped_max_time_proj.tif"))
# Cropping max projections
cropped_tstack_file_name = get_tiff_name_from_dir(
raw_dir)
cropped_tstack_file_name.time_point = "(TS)"
cropped_tstack_file_name.plane = "(ZM)"
if not use_cropped_cache or not os.path.exists(os.path.join(tstack_dir, cropped_tstack_file_name.get_name())):
logging.info("\tChannel: %s Direction: %s Cropping a stack of max projections." % (
channel, direction))
cropped_tstack = crop_stack_by_template(
max_proj_stack, crop_template, dataset)
save_tiff(cropped_tstack, os.path.join(
tstack_dir, cropped_tstack_file_name.get_name()))
else:
logging.info("\tChannel: %s Direction: %s Found existing cropped max projected stacks. Using them." % (
channel, direction))
cropped_tstack = IJ.openImage(os.path.join(
tstack_dir, cropped_tstack_file_name.get_name()))
# Cropping raw stacks
ntime_points = len(get_tiffs_in_directory(raw_dir))
stacks_done_list = get_tiffs_in_directory(raw_cropped_dir)
all_stacks_done = ntime_points == len(stacks_done_list)
if use_cropped_cache and all_stacks_done:
logging.info("\tChannel: %s Direction: %s Found existing cropped raw stacks, using them." % (
channel, direction))
else:
logging.info(
"\tChannel: %s Direction: %s Cropping raw image stacks." % (channel, direction))
planes_kept = (0, 0)
raw_stack_files = get_tiffs_in_directory(raw_dir)
if len(stacks_done_list) > 1:
logging.info("\tChannel: %s Direction: %s Found some existing cropped raw stacks, using them." % (
channel, direction))
files_to_crop = raw_stack_files[len(stacks_done_list) - 1 : ]
current_active_stack = len(stacks_done_list)
else:
files_to_crop = raw_stack_files
current_active_stack = 1
for direction, raw_stack_file_name in enumerate(files_to_crop):
raw_stack = IJ.openImage(raw_stack_file_name)
IJ.run(raw_stack, "Properties...",
"frames=1 pixel_width=1.0000 pixel_height=1.0000 voxel_depth=4.0000")
raw_stack_cropped = crop_stack_by_template(
raw_stack, crop_template, dataset)
if direction == 0:
logging.info("\tChannel: %s Direction: %s Finding which planes to keep in raw image stacks." % (
channel, direction))
if "planes_to_keep_per_direction" in dataset:
manual_planes_to_keep = (dataset["planes_to_keep_per_direction"][direction - 1]
["start"], dataset["planes_to_keep_per_direction"][direction - 1]["end"])
else:
manual_planes_to_keep = None
stack_for_finding_planes = IJ.openImage(raw_stack_files[0])
IJ.run(stack_for_finding_planes, "Properties...",
"frames=1 pixel_width=1.0000 pixel_height=1.0000 voxel_depth=4.0000")
stack_for_finding_planes = crop_stack_by_template(
stack_for_finding_planes, crop_template, dataset)
try:
planes_kept = find_planes_to_keep(stack_for_finding_planes, m_dir, manual_planes_to_keep)
except Exception as e:
logging.info("\tChannel: %s Direction: %s Encountered an exception while trying to find which planes to keep. Skipping the dataset. Exception: \n%s" % (
channel, direction, e))
print("\tChannel: %s Direction: %s Encountered an exception while trying to find which planes to keep. Skipping the dataset. Exception: \n%s" % (
channel, direction, e))
skip_the_dataset = True
open(os.path.join(root_dataset_dir, DATASET_ERROR_FILE_NAME), 'a').close()
os.remove(os.path.join(root_dataset_dir, DATASET_ACTIVE_FILE_NAME))
print("Encountered an exception while trying to find which planes to keep. Skipping the dataset.")
break
logging.info("\tChannel: %s Direction: %s Keeping planes: %s." %
(channel, direction, planes_kept))
logging.info(
"\tChannel: %s Direction: %s Cropping Raw stack for timepoint: %s/%s" % (channel, direction, current_active_stack, ntime_points))
raw_stack_cropped = reset_img_properties(raw_stack_cropped, voxel_depth=4)
raw_stack_cropped = subset_planes(raw_stack_cropped, planes_kept)
save_tiff(raw_stack_cropped, os.path.join(
raw_cropped_dir, os.path.split(raw_stack_file_name)[1]))
current_active_stack += 1
# Save unadjusted montages
logging.info("\tChannel: %s Creating montages from max projections." % (
channel))
montage_stack = ImagePlus()
montage_stack_name = None
for direction, tstack_dir in enumerate(tstack_dataset_dirs):
stack_path = os.path.join(
tstack_dir, get_tiff_name_from_dir(tstack_dir).get_name())
imp_stack = IJ.openImage(stack_path)
if direction == 0:
montage_stack = imp_stack.getStack()
montage_stack_name = get_tiff_name_from_dir(tstack_dir)
if direction > 0:
montage_stack = StackCombiner.combineHorizontally(StackCombiner(), montage_stack, imp_stack.getStack())
montage_stack_name.direction = "(AX)"
montage_stack_name.plane = "(ZM)"
montage_stack = ImagePlus("montage", montage_stack)
montage_dir = os.path.join(root_dataset_dir, MONTAGE_DIR_NAME)
if not os.path.exists(montage_dir):
mkpath(montage_dir)
save_tiff(montage_stack, os.path.join(montage_dir, montage_stack_name.get_name()))
# Creating bleach corrected and adjusted montages
logging.info("\tChannel: %s Creating histogram adjusted montages from max projections." % (
channel))
adj_montage_stack = montage_stack
adj_montage_stack_name = montage_stack_name
adj_montage_stack_name.direction = "(AX)"
adj_montage_stack_name.plane = "(ZA)"
if do_histogram_matching:
match_histograms_stack(adj_montage_stack)
adj_montage_stack_name.plane = "(ZH)"
adj_montage_stack, histogram_thresholds = threshold_histogram_stack(adj_montage_stack)
histogram_thresholds = {"lower_threshold": histogram_thresholds[0], "upper_threshold": histogram_thresholds[1]}
with open(os.path.join(meta_dir, chan_dir_name, "histogram_thresholds_used_for_contrast_adjustment.JSON"), "w") as hist_json:
json.dump(histogram_thresholds, hist_json, indent=4)
save_tiff(adj_montage_stack, os.path.join(montage_dir, adj_montage_stack_name.get_name()))
# Split the adjusted montage into separate stacks for directions
logging.info("\tChannel: %s Saving histogram adjusted stacks of max projections." % (
channel))
adjusted_stacks = split_montage_stack_to_list_of_stacks(adj_montage_stack, 1, ndirections)
for direction, tstack_dir, contr_dir, adj_max_proj_stack in zip(range(1, ndirections + 1), tstack_dataset_dirs, contrast_dirs, adjusted_stacks):
logging.info("\tChannel: %s Direction: %s Saving histogram adjusted stack." % (
channel, direction))
adj_stack_name = get_tiff_name_from_dir(tstack_dir)
adj_stack_name.plane = "(ZA)"
if do_histogram_matching:
adj_stack_name.plane = "(ZH)"
save_tiff(adj_max_proj_stack, os.path.join(contr_dir, adj_stack_name.get_name()))
# Save adjusted vertical montages
logging.info("\tChannel: %s Creating adjusted vertical montages from max projections." % (
channel))
vert_montage_stack = ImagePlus()
vert_montage_stack_name = None
for direction, tstack_dir in enumerate(tstack_dataset_dirs):
stack_path = os.path.join(
tstack_dir, get_tiff_name_from_dir(tstack_dir).get_name())
imp_stack = IJ.openImage(stack_path)
if direction == 0:
vert_montage_stack = imp_stack.getStack()
vert_montage_stack_name = get_tiff_name_from_dir(tstack_dir)
if direction > 0:
vert_montage_stack = StackCombiner.combineVertically(StackCombiner(), vert_montage_stack, imp_stack.getStack())
vert_montage_stack_name.direction = "(AY)"
vert_montage_stack_name.plane = "(ZA)"
vert_montage_stack = ImagePlus("montage", vert_montage_stack)
if do_histogram_matching:
match_histograms_stack(vert_montage_stack)
vert_montage_stack_name.plane = "(ZH)"
vert_montage_stack, _ = threshold_histogram_stack(vert_montage_stack)
montage_dir = os.path.join(root_dataset_dir, MONTAGE_DIR_NAME)
if not os.path.exists(montage_dir):
mkpath(montage_dir)
save_tiff(vert_montage_stack, os.path.join(montage_dir, vert_montage_stack_name.get_name()))
if skip_the_dataset == True:
logging.info("Had to skip the dataset DS%04d." % dataset_id)
continue
open(os.path.join(root_dataset_dir, DATASET_FINISHED_FILE_NAME), 'a').close()
os.remove(os.path.join(root_dataset_dir, DATASET_ACTIVE_FILE_NAME))
logging.info("Finished processing dataset DS%04d successfully." % dataset_id)
print("Finished processing dataset DS%04d successfully." % dataset_id)
logging.info("Finished processing all datasets.")
print("Finished processing all datasets.")
def get_raw_images_dir(datasets_dir, dataset_id):
dirs = [name for name in os.listdir(
datasets_dir) if os.path.isdir(os.path.join(datasets_dir, name))]
this_dataset_dir = fnmatch.filter(dirs, "DS%04d*" % dataset_id)
if len(this_dataset_dir) > 1:
error_msg = " Error: there are multiple directories for the dataset with ID: %04d." % dataset_id
logging.info(error_msg)
print(error_msg)
return None
if len(this_dataset_dir) == 0:
error_msg = " Error: there are no directories for the dataset with ID: %04d." % dataset_id
logging.info(error_msg)
print(error_msg)
return None
raw_images_dir = os.path.join(
datasets_dir, this_dataset_dir[0], RAW_IMAGES_DIR_NAME)
if not os.path.isdir(raw_images_dir):
error_msg = " Error: there are no %s directoriy for the dataset with ID: %04d." % (
RAW_IMAGES_DIR_NAME, dataset_id)
logging.info(error_msg)
print(error_msg)
return None
return raw_images_dir
def make_directions_dirs(input_dir, ndirections):
direction_dirs = []
for i in range(1, ndirections + 1):
new_dir = os.path.join(input_dir, "DR" + str(i).zfill(4))
direction_dirs.append(new_dir)
if not os.path.exists(new_dir):
mkpath(new_dir)
return direction_dirs
def get_tiff_name_from_dir(input_dir):
file_name = None
for fname in os.listdir(input_dir):
if fname.lower().endswith(".tif"):
file_name = fname
break
if file_name == None:
raise Exception("Did not find any TIFF files in a directory: %s" % input_dir)
file_name = FredericFile(file_name)
return file_name
def move_files(raw_images_dir, specimen_directions_in_channels, dataset_id, dataset_name_prefix):
"""Splits the embryo images by direction and puts in separate channel/direction folders.
Renames the files to conform to FredericFile format.
Args:
raw_images_dir (str): full path to mixed raw images files
specimens_per_direction ( ((4,3,2,1),(5,6,8,7)) ): a tuple, each element has info for a channel with this index.
Info is a tuple with correspondance speciment number and directions, where directions are corresponding index in the tuple.
dataset_id (int): ID number of the dataset
dataset_name_prefix (str): prefix that the user specifies for the dataset
Raises:
Exception: metadata does not contain the specimen extracted from the file names
"""
specimens_info = {}
ndirections = len(specimen_directions_in_channels[0])
for channel, directions in enumerate(specimen_directions_in_channels, start=1):
chan_dir = os.path.join(raw_images_dir, "CH%04d" % channel)
if not os.path.exists(chan_dir):
os.mkdir(chan_dir)
direction_dirs = make_directions_dirs(chan_dir, ndirections)
for direct, specimen in enumerate(directions, start=1):
specimens_info[specimen] = {"channel": channel, "direction": direct}
for file_name in os.listdir(raw_images_dir):
file_path = os.path.join(raw_images_dir, file_name)
if os.path.isdir(file_path):
continue
if not file_name.endswith((".tif", ".TIF")):
continue
specimen = int(
file_name[file_name.find("SPC0") + 4: file_name.find("SPC0") + 6])
time_point = int(
file_name[file_name.find("TL") + 2: file_name.find("TL") + 6]) + 1
# channel = int(file_name[file_name.find("CHN") + 3: file_name.find("TL") + 5]) + 1 # Not used for old mDSLM images
if specimen not in specimens_info:
raise ValueError("In the metadata for the dataset: DS %04d there is no entry for the specimen: %i" % (
dataset_id, specimen))
image_channel = specimens_info[specimen]["channel"]
embryo_direction = specimens_info[specimen]["direction"]
new_file_name = "%sDS%04dTP%04dDR%04dCH%04dPL(ZS).tif" % (dataset_name_prefix,
dataset_id,
time_point,
embryo_direction,
image_channel)
os.rename(file_path, os.path.join(
direction_dirs[embryo_direction - 1], new_file_name))
logging.info("New file \n%s\n Full path:\n%s\n Original name: \n%s\n Original path: \n%s\n" % (new_file_name,
os.path.join(
direction_dirs[embryo_direction - 1], new_file_name),
file_name,
file_path))
def is_specimen_input_valid(specimens_per_direction):
if not isinstance(specimens_per_direction, tuple):
return False
for i in specimens_per_direction:
if not isinstance(i, int):
return False
if i not in range(1000):
return False
return True
def is_dataset_ID_input_valid(dataset_id):
if not isinstance(dataset_id, int):
return False
if dataset_id in range(10000):
return True
return False
def get_tiffs_in_directory(directory):
"""Get all TIFF file paths in a directory. Subdirectories are not searched.
Args:
directory (str): full path to directory
Raises:
Exception: If no images were found
Returns:
str[]: list of full paths to tiff files
"""
file_names = []
for fname in os.listdir(directory):
if fname.lower().endswith(".tif"):
file_names.append(os.path.join(directory, fname))
file_names = sorted(file_names)
return file_names
def sort_tiff_list_by_timepoint(tiff_list):
pass
def project_a_stack(stack):
"""Project a stack of images along the Z axis
Args:
stack ImagePlus: stack of images to project
Returns:
ImagePlus: max-projection
"""
zp = ZProjector(stack)
zp.setMethod(ZProjector.MAX_METHOD)
zp.doProjection()
zpimp = zp.getProjection()
return zpimp
def make_max_Z_projections_for_folder(input_dir):
"""Takes all stacks from a directory, does their max-projection, makes a stack of max-projections, saves it to output directory and returns it.
Args:
input_dir (string): path to stacks of images (has to be images from ImagePlus objects)
output_dir (string): path to output folder
Returns:
ImagePlus: stack of max-projections
"""
fnames = get_tiffs_in_directory(input_dir)
if len(fnames) == 0:
raise Exception("No tiffs to process in %s" % input_dir)
# Open and stack images
img_for_dims = IJ.openImage(fnames[0])
stack_list = []
for fname in fnames:
stack = IJ.openImage(fname)
# Select which dimension to project
max_proj = project_a_stack(stack)
stack_list.append(max_proj.getProcessor())
max_proj_stack = ImageStack(img_for_dims.width, img_for_dims.height)
for slice in stack_list:
max_proj_stack.addSlice(None, slice)
max_proj_stack = ImagePlus("max_proj", max_proj_stack)
max_proj_stack = reset_img_properties(max_proj_stack, voxel_depth=1)
return max_proj_stack
def is_polygon_roi_overlapping_image_edges(image, roi):
"""Check if any of the roi coordinates are outside of the image.
Args:
image (ImagePlus): image to check over
roi (PolygonRoi): roi to check
Returns:
bool: is roi overlapping the edges?
"""
is_overlapping = False
if not all(x < image.getWidth() for x in roi.getPolygon().xpoints):
is_overlapping = True
if not all(x > 0 for x in roi.getPolygon().xpoints):
is_overlapping = True
if not all(y < image.getHeight() for y in roi.getPolygon().ypoints):
is_overlapping = True
if not all(y > 0 for y in roi.getPolygon().ypoints):
is_overlapping = True
return is_overlapping
def create_crop_template(max_time_projection, meta_dir, dataset, dataset_minimal_crop_box_dims, use_dataset_box_dims):
"""Crop max_time_projection, create crop template .roi object, save it in meta_dir.
Args:
max_time_projection (ImagePlus): a single image of max projection of a time stack of max projections
meta_dir (str): full path to metadata dir
dataset (dict): metadata about the dataset to extract information about embryo orientation
dataset_maximal_crop_box_dims (int, int): width, heigth
use_dataset_box_dims (bool):
Returns:
Roi: bounding box around the embryo with proper rotation and size
ImagePlus: cropped max time projection
(int, int): updated dataset maximal crop box dims
"""
updated_dataset_maximal_crop_box_dims = dataset_minimal_crop_box_dims
imp = max_time_projection
if dataset["use_manual_bounding_box"] == True:
logging.info("\tUsing manualy specified crop box.")
manual_roi = RoiDecoder(os.path.join(meta_dir, "%s.roi" % MANUAL_CROP_BOX_FILE_NAME)).getRoi()
rot_angle = round(get_polygon_roi_angle(manual_roi), ndigits=1)
# Always assuming that embryo is horizontal
embryo_length, embryo_width = get_rotated_rect_polygon_roi_dims(manual_roi)
embryo_center_x = manual_roi.getContourCentroid()[0]
embryo_center_y = manual_roi.getContourCentroid()[1]
else:
ip = imp.getProcessor().duplicate() # get pixel array?, as a copy
mask = ImagePlus("mask", ip)
radius = 4
RankFilters().rank(ip, radius, RankFilters.MEDIAN)
hist = ip.getHistogram()
triag_threshold = Auto_Threshold.Triangle(hist)
ip.setThreshold(triag_threshold, float("inf"), ImageProcessor.NO_LUT_UPDATE)
IJ.run(mask, "Convert to Mask", "")
# Repeated Erosion and Dilation to remove dirt, which shows as protrusions on the embryo mask
# Using IJ.run() for erosion and dilation has some persistent state after run
# that produces inconsistent behaviour and bugs
# This is why I am using ByteProcessor methods directly
bt = mask.getProcessor().convertToByteProcessor()
for i in range(40):
bt.erode(2, 0)
for i in range(40):
bt.dilate(2, 0)
for i in range(40):
bt.erode(3, 0)
for i in range(40):
bt.dilate(3, 0)
mask.setProcessor(bt)
IJ.run("Clear Results")
table = ResultsTable()
# Initialise without display (boolean value is ignored)
roim = RoiManager(False)
MIN_PARTICLE_SIZE = 10000 # pixel ^ 2
MAX_PARTICLE_SIZE = float("inf")
ParticleAnalyzer.setRoiManager(roim)
pa = ParticleAnalyzer(
ParticleAnalyzer.ADD_TO_MANAGER,
Measurements.ELLIPSE,
table,
MIN_PARTICLE_SIZE,
MAX_PARTICLE_SIZE)
pa.analyze(mask)
rot_angle = round(table.getValue("Angle", 0), ndigits=1)
if rot_angle > 90:
rot_angle -= 180
logging.info("\t Angle of the elipse fitted onto the embryo: %s", rot_angle)
roi_arr = roim.getRoisAsArray()
roim.runCommand('reset')
imp.setRoi(roi_arr[len(roi_arr) - 1])
IJ.run(imp, "Select Bounding Box (guess background color)", "")
bounding_roi = imp.getRoi()
# Crop the image so the borders are equidistant from the borders of the bounding box
# for the center of rotation to be aligned roughly with the center of the embryo
bounding_rectangle = bounding_roi.getBounds()
# extracting width field from java.awt.Rectangle
i = bounding_rectangle.x
j = bounding_rectangle.y
We = bounding_rectangle.width
He = bounding_rectangle.height
W = imp.getWidth()
H = imp.getHeight()
ax = i
bx = W - i - We
ay = j
by = H - j - He
xn = i - min(ax, bx)
yn = j - min(ay, by)
Wn = We + 2 * min(ax, bx)
Hn = He + 2 * min(ay, by)
IJ.run(imp, "Specify...", "width=%s height=%s x=%s y=%s" % (Wn, Hn, xn, yn))
equidistant_crop_roi = imp.getRoi()
mask.setRoi(equidistant_crop_roi)
mask_cropped = mask.crop()
# For some reason cropping a mask does not produce a proper binarized image (it is gray if you view it),
# So I had to redo the thresholding
IJ.run(mask_cropped, "Rotate... ", "angle=%s grid=1 interpolation=Bilinear" % rot_angle)
hist = mask_cropped.getProcessor().getHistogram()
triag_threshold = Auto_Threshold.Triangle(hist)
mask_cropped.getProcessor().setThreshold(
triag_threshold, float("inf"), ImageProcessor.NO_LUT_UPDATE)
IJ.run(mask_cropped, "Convert to Mask", "")
# Extract the precise embryo center in the rotated cropped image
roim = RoiManager(False)
MIN_PARTICLE_SIZE = 10000 # pixel ^ 2
MAX_PARTICLE_SIZE = float("inf")
ParticleAnalyzer.setRoiManager(roim)
# For some reason ParticleAnalyzer cannot be run without reinitialization,
# that's why repeating it here
pa = ParticleAnalyzer(
ParticleAnalyzer.ADD_TO_MANAGER,
Measurements.ELLIPSE,
table,
MIN_PARTICLE_SIZE,
MAX_PARTICLE_SIZE)
pa.analyze(mask_cropped)
table.reset()
roi_arr = roim.getRoisAsArray()
mask_cropped.setRoi(roi_arr[len(roi_arr) - 1])
roim.runCommand('reset')
IJ.run(mask_cropped, "Select Bounding Box (guess background color)", "")
cropped_bounding_roi = mask_cropped.getRoi()
embryo_length = cropped_bounding_roi.getBounds().width
embryo_width = cropped_bounding_roi.getBounds().height
logging.info("\tEmbryo dims: (%s, %s)" % (embryo_length, embryo_width))
embryo_cropped_rot_center_x = cropped_bounding_roi.getContourCentroid()[0]
embryo_cropped_rot_center_y = cropped_bounding_roi.getContourCentroid()[1]
# Transform the embryo center coordinates with the inverse rotation
rad_angle = math.radians(-rot_angle)
# switch to coordinates with origin in the cropped image center.
xC = embryo_cropped_rot_center_x - Wn / 2
yC = embryo_cropped_rot_center_y - Hn / 2
embryo_cropped_center_x = xC * \
math.cos(rad_angle) - yC * math.sin(rad_angle)
embryo_cropped_center_y = xC * \
math.sin(rad_angle) + yC * math.cos(rad_angle)
embryo_cropped_center_x += Wn / 2
embryo_cropped_center_y += Hn / 2
# Transform the embryo center coordinates to reverse cropping
embryo_center_x = embryo_cropped_center_x + xn
embryo_center_y = embryo_cropped_center_y + yn
logging.info("\tEmbryo center: (%s, %s)" % (embryo_center_x, embryo_center_y))
if use_dataset_box_dims == True:
dataset_width, dataset_height = dataset_minimal_crop_box_dims
logging.info("\tUsing dataset's global value of the crop box dims: %s, %s to create the crop template" % (dataset_width, dataset_height))
box_width, box_height = dataset_width, dataset_height
IJ.run(imp, "Specify...", "width=%s height=%s x=%s y=%s centered" %
(dataset_width, dataset_height, embryo_center_x, embryo_center_y))
else:
dataset_width, dataset_height = dataset_minimal_crop_box_dims
box_width = embryo_length + 12 + 4 - embryo_length % 4
box_height = embryo_width + 12 + 4 - embryo_width % 4
box_width = max(box_width, dataset_width)
box_height = max(box_height, dataset_height)
IJ.run(imp, "Specify...", "width=%s height=%s x=%s y=%s centered" %
(box_width, box_height, embryo_center_x, embryo_center_y))
updated_dataset_maximal_crop_box_dims = (box_width, box_height)
bounding_roi = imp.getRoi()
bounding_roi_rot = RoiRotator.rotate(bounding_roi, -rot_angle)
# Check if the rotated bounding box extend over the image
if is_polygon_roi_overlapping_image_edges(imp, bounding_roi_rot) == True:
if use_dataset_box_dims == True: