/
paintera_multicut_workflow.py
1159 lines (991 loc) · 43.2 KB
/
paintera_multicut_workflow.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
import numpy as np
import os
import sys
from h5py import File
import json
from vigra.analysis import labelMultiArray, sizeFilterSegInplace, watershedsNew
from shutil import rmtree
from elf.io import open_file
import elf.segmentation.multicut as mc
import elf.segmentation.features as feats
from subprocess import call, run, DEVNULL
import multiprocessing as mp
import napari
from svm_tools.paintera_merge import convert_pre_merged_labels_to_assignments
from svm_tools.label_operations import relabel_consecutive
from svm_tools.volume_operations import crop_center
from svm_tools.position_generation_h5 import build_equally_spaced_volume_list
def _load_data(
filepath,
shape,
auto_crop_center,
channel=None,
normalize=False,
verbose=False,
relabel=False,
conncomp=False,
cache_folder=None
):
with open_file(filepath, 'r') as f:
data = f['data'][:]
if cache_folder is None:
name = os.path.splitext(filepath)[0]
else:
name = os.path.join(
cache_folder,
os.path.splitext(os.path.split(filepath)[1])[0]
)
if verbose:
print(name)
relabel_filepath = name + '_rl.h5'
crop_filepath = name + '_crop_center{}.h5'.format('_'.join([str(x) for x in shape]))
channel_filepath = name + '_ch{}.h5'.format(channel)
if relabel and os.path.exists(relabel_filepath):
if verbose:
print('Loading relabeled data ...')
with File(relabel_filepath, mode='r') as f:
data = f['data'][:]
filepath = relabel_filepath
else:
if auto_crop_center and os.path.exists(crop_filepath):
if verbose:
print('Loading cropped data ...')
with File(crop_filepath, mode='r') as f:
data = f['data'][:]
filepath = crop_filepath
else:
if channel is not None:
data = data[..., channel].squeeze()
with File(channel_filepath, mode='w') as f:
f.create_dataset('data', data=data, compression='gzip')
filepath = channel_filepath
if auto_crop_center:
if data.shape != shape:
data = crop_center(data, shape)
with File(crop_filepath, mode='w') as f:
f.create_dataset('data', data=data, compression='gzip')
filepath = crop_filepath
if normalize:
data = data.astype('float32')
data /= 255
if relabel:
if conncomp:
data = labelMultiArray(data.astype('float32'))
data = relabel_consecutive(data).astype('uint32')
with File(relabel_filepath, mode='w') as f:
f.create_dataset('data', data=data, compression='gzip')
filepath = relabel_filepath
if verbose:
print('data.shape = {}'.format(data.shape))
if channel:
return data, filepath, channel_filepath
else:
return data, filepath
def _write_data(
filepath,
data,
verbose=False
):
if verbose:
print('Writing to {}'.format(filepath))
with File(filepath, 'w') as f:
f.create_dataset('data', data=data, compression='gzip')
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def _query_increase_decrease_value(question):
valid = {"+": 'increase', "-": 'decrease'}
prompt = " [+/-/float]"
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if choice in valid:
return valid[choice]
else:
try:
value = float(choice)
return value
except ValueError:
sys.stdout.write("Please respond with '+' or '-' "
"or a value.\n")
def _query_commands():
valid = dict(
update='update',
u='update',
exit='exit',
q='exit',
editor='editor',
e='editor'
)
prompt = ' ?> '
print('\nUse the command line for following commands:')
print(' exit / q -> finish assignments and export')
print(' update / u -> updates Napari display')
print(' editor / e -> re-opens editor')
while True:
sys.stdout.write(prompt)
choice = input().lower()
if choice in valid:
return valid[choice]
else:
sys.stdout.write('No valid command')
def prepare_for_paintera(paintera_env_name, filepath, target_filepath,
activation_command='source activate', shell='/bin/bash',
src_name='data', tgt_name='data', verbose=False):
if verbose:
console_output = None
else:
console_output = DEVNULL
if paintera_env_name is not None:
activate = '{act} {paintera_env}\n'.format(act=activation_command, paintera_env=paintera_env_name)
return call([
'{act}'
'paintera-convert to-paintera '
'--container {src} --dataset {src_name} --output-container {tgt} --target-dataset {tgt_name}'.format(
act=activate,
src=filepath, src_name=src_name,
tgt=target_filepath, tgt_name=tgt_name
)
], shell=True, executable=shell, stdout=console_output, stderr=console_output)
else:
return run([
'bash --login -c '
'"paintera-convert to-paintera '
'--container {src} --dataset {src_name} --output-container {tgt} --target-dataset {tgt_name}"'.format(
src=filepath, src_name=src_name,
tgt=target_filepath, tgt_name=tgt_name
)
], shell=True, executable=shell, stdout=console_output, stderr=console_output)
def export_from_paintera(paintera_env_name, filepath, target_filepath,
activation_command='source activate', shell='/bin/bash',
src_name='data', tgt_name='data', verbose=False):
if verbose:
console_output = None
else:
console_output = DEVNULL
if paintera_env_name is not None:
activate = '{act} {paintera_env}\n'.format(act=activation_command, paintera_env=paintera_env_name)
return call([
'{act}'
'paintera-convert to-scalar '
'--consider-fragment-segment-assignment -i {fp} -I {src_name} -o {target_fp} -O {tgt_name}'.format(
act=activate,
fp=filepath,
target_fp=target_filepath,
src_name=src_name,
tgt_name=tgt_name
)
], shell=True, executable=shell, stdout=console_output, stderr=console_output)
else:
return run([
'bash --login -c '
'"paintera-convert to-scalar '
'--consider-fragment-segment-assignment -i {fp} -I {src_name} -o {target_fp} -O {tgt_name}"'.format(
fp=filepath,
target_fp=target_filepath,
src_name=src_name,
tgt_name=tgt_name
)
], shell=True, executable=shell, stdout=console_output, stderr=console_output)
def open_paintera(paintera_env_name, project_folder,
activation_command='source activate', shell='/bin/bash', verbose=False):
if verbose:
console_output = None
else:
console_output = DEVNULL
if paintera_env_name is not None:
activate = '{act} {paintera_env}\n'.format(act=activation_command, paintera_env=paintera_env_name)
return call([
'{act}'
'paintera {folder}'.format(
act=activate,
folder=project_folder
)
], shell=True, executable=shell, stdout=console_output, stderr=console_output)
else:
return call([
'bash --login -c '
'"paintera {folder}"'.format(
folder=project_folder
)
], shell=True, executable=shell, stdout=console_output, stderr=console_output)
def open_napari(data):
with napari.gui_qt():
viewer = napari.Viewer()
for item in data:
if item['type'] == 'label':
viewer.add_labels(item['data'], name=item['name'], visible=item['visible'])
elif item['type'] == 'raw':
viewer.add_image(item['data'], name=item['name'], visible=item['visible'])
return None
def _open_editor(filepath):
run([
'bash --login -c '
'"gedit {fp}"'.format(fp=filepath)
], shell=True)
def supervoxel_merging(mem, sv, beta=0.5, verbose=False):
rag = feats.compute_rag(sv)
costs = feats.compute_boundary_features(rag, mem)[:, 0]
edge_sizes = feats.compute_boundary_mean_and_length(rag, mem)[:, 1]
costs = mc.transform_probabilities_to_costs(costs, edge_sizes=edge_sizes, beta=beta)
node_labels = mc.multicut_kernighan_lin(rag, costs)
segmentation = feats.project_node_labels_to_pixels(rag, node_labels)
return segmentation
def multicut_module(
seg_filepath,
raw, mem, sv,
verbose=False
):
if not os.path.exists(seg_filepath):
user_happy = False
beta = 0.5
while not user_happy:
print(sv.shape)
# 1. Run Multicut
seg = supervoxel_merging(mem, sv, beta=beta, verbose=verbose)
# 2. Show results in Napari
print('\nShowing Multicut result for beta = {}'.format(beta))
print('Decide whether you are happy or there should be more or less merges and close Napari.')
to_show = [dict(type='raw', name='raw', data=raw, visible=True)]
if mem is not None:
to_show.append(dict(type='raw', name='mem', data=mem, visible=True))
to_show.append(dict(type='label', name='sv', data=sv, visible=False))
to_show.append(dict(type='label', name='seg', data=seg, visible=True))
open_napari(to_show)
# 3. Ask user if results are good
# If not, go back to run multicut (1), else continue
user_happy = query_yes_no('Happy with the result?', default='no')
if not user_happy:
change_beta = _query_increase_decrease_value(
'Less merges (increase beta) or more merges (decrease beta)?')
if type(change_beta) == str:
if change_beta == 'increase':
beta += 0.1
if beta > 1:
beta = 1
elif change_beta == 'decrease':
beta -= 0.1
if beta < 0:
beta = 0
else:
raise ValueError
elif type(change_beta) == float:
beta = change_beta
if beta > 1:
beta = 1
if beta < 0:
beta = 0
else:
raise ValueError
_write_data(seg_filepath, seg)
else:
print('Segmentation exists')
def paintera_merging_module(
results_folder,
paintera_proj_path,
activation_command,
paintera_env_name,
shell,
seg_filepath, seg_name,
full_raw_filepath, raw_name,
supervoxel_filepath, sv_name,
mem_pred_filepath, mem_name,
verbose
):
supervoxel_proj_path = os.path.join(results_folder, 'data.n5')
if not os.path.exists(os.path.join(results_folder, 'data.n5')):
if verbose:
print('Preparing raw ...')
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
if prepare_for_paintera(paintera_env_name, full_raw_filepath, os.path.join(results_folder, 'data.n5'),
activation_command, shell, verbose=verbose, src_name=raw_name, tgt_name='raw'):
pass
# raise RuntimeError
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
if mem_pred_filepath is not None:
if verbose:
print('Preparing membrane prediction ...')
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
if prepare_for_paintera(paintera_env_name, mem_pred_filepath, os.path.join(results_folder, 'data.n5'),
activation_command, shell, verbose=verbose, src_name=mem_name, tgt_name='mem'):
pass
# raise RuntimeError
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
if verbose:
print('Preparing supervoxels ...')
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
if prepare_for_paintera(paintera_env_name, supervoxel_filepath, supervoxel_proj_path,
activation_command, shell, verbose=verbose, src_name=sv_name, tgt_name='sv'):
pass
# raise RuntimeError
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
if verbose:
print('Assigning pre-merged segmentation to supervoxels')
convert_pre_merged_labels_to_assignments(
supervoxel_filepath, seg_filepath, paintera_proj_path, sv_name='data', merged_name=seg_name
)
if not os.path.exists(os.path.join(paintera_proj_path, 'attributes.json')):
# 5. Ask user to create paintera project and close paintera again
print('\n\nOpening paintera...\n')
print('Set up the Paintera project by loading the following data from')
print(os.path.join(results_folder, 'data.n5'))
print('Dataset names:')
print('1. Raw data (as type raw): "raw"')
if mem_pred_filepath is not None:
print('2. Membrane prediction (as type raw): "mem"')
print('3. Supervoxels (as type labels): "sv"')
else:
print('2. Supervoxels (as type labels): "sv"')
else:
print('\nPaintera project exists ...')
print('\nProof-read the segmentation as desired; save, commit changes and close Paintera when done.')
print('\nNote: DO NOT FORGET TO COMMIT CHANGES BEFORE CLOSING!')
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
if open_paintera(paintera_env_name, paintera_proj_path, activation_command, shell, verbose=verbose):
raise RuntimeError
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
# 8. Convert results to h5 and do the assignments
# Generate terminal commands:
# > conda activate paintera_env
# > paintera-convert to-scalar --consider-fragment-segment-assignment ...
print('Exporting from paintera ...')
if verbose:
print(supervoxel_proj_path)
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
if export_from_paintera(paintera_env_name, supervoxel_proj_path, os.path.join(results_folder, 'exported_seg.h5'),
activation_command, shell, src_name='sv', tgt_name='data',
verbose=verbose):
pass
# raise RuntimeError
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
assert os.path.exists(os.path.join(results_folder, 'exported_seg.h5'))
def paintera_merging_module2(
tmp_dir,
results_folder,
paintera_lock_file,
paintera_proj_path,
activation_command,
paintera_env_name,
shell,
seg_filepath,
full_raw_filepath,
supervoxel_filepath,
supervoxel_proj_path,
mem_pred_filepath,
result_dtype,
conncomp_on_paintera_export,
export_filepath=None,
export_name=None,
verbose=False
):
if verbose:
print('Preparing raw ...')
raw_n5 = os.path.join(tmp_dir, 'raw.n5')
if os.path.exists(raw_n5):
if os.path.exists(os.path.join(raw_n5, 'attributes.json')):
rmtree(raw_n5)
else:
print('raw.n5 exists but is not n5')
raise RuntimeError
# if not os.path.exists(os.path.join(results_folder, 'raw.n5')):
if True:
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
prepare_for_paintera(paintera_env_name, full_raw_filepath, os.path.join(tmp_dir, 'raw.n5'),
activation_command, shell)
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
# if mem_pred_filepath is not None:
# if verbose:
# print('Preparing membrane prediction ...')
# if not os.path.exists(os.path.join(results_folder, 'mem.n5')):
# print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
# prepare_for_paintera(paintera_env_name, mem_pred_filepath, os.path.join(results_folder, 'mem.n5'),
# activation_command, shell)
# print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
if verbose:
print('Preparing supervoxels ...')
sv_n5 = os.path.join(tmp_dir, 'sv.n5')
if os.path.exists(sv_n5):
if os.path.exists(os.path.join(sv_n5, 'attributes.json')):
rmtree(sv_n5)
else:
print('sv.n5 exists but is not n5')
raise RuntimeError
# if not os.path.exists(os.path.join(results_folder, 'sv.n5')):
if True:
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
prepare_for_paintera(paintera_env_name, supervoxel_filepath, supervoxel_proj_path,
activation_command, shell)
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
if not os.path.exists(os.path.join(paintera_proj_path, 'attributes.json')):
# 5. Ask user to create paintera project and close paintera again
print('\n\nOpening paintera...\n')
print('Set up the Paintera project by loading the following files from')
print(tmp_dir)
print('1. Raw data (as type raw): "raw.n5"')
if mem_pred_filepath is not None:
print('2. Membrane prediction (as type raw): "mem.n5"')
print('3. Supervoxels (as type labels): "sv.n5"')
else:
print('2. Supervoxels (as type labels): "sv.n5"')
print('\nIt is possible to change settings at this step, for example, '
'it is best to already switch off 3D rendering.')
print('\nNote: DO NOT YET PERFORM ANY MERGES!')
print('\nSave and close Paintera when done.')
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
open_paintera(paintera_env_name, paintera_proj_path, activation_command, shell)
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
else:
print('\nPaintera project exists ...')
# TODO why is there an if True here?
# if not os.path.exists(paintera_lock_file):
if True:
print('\nIntegrating pre-merged segmentation into Paintera project ...')
# 6. Integrate Multicut result to the paintera project
convert_pre_merged_labels_to_assignments(
supervoxel_filepath, seg_filepath, paintera_proj_path
)
open(paintera_lock_file, 'a').close()
else:
print('\nPaintera project already locked ...')
# 7. Ask user to open paintera again, to do the necessary annotations and then close paintera
print('\n\nOpening paintera...\n')
print('Perform the necessary corrections, then save and close Paintera.')
print('Consider committing to backend (CTRL + C) before starting to annotate to make Paintera run more fluently.')
print('\n>>> SHELL >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
open_paintera(paintera_env_name, paintera_proj_path, activation_command, shell)
print('\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n')
# 8. Convert results to h5 and do the assignments
# Generate terminal commands:
# > conda activate paintera_env
# > paintera-convert to-scalar --consider-fragment-segment-assignment ...
print('Exporting from paintera ...')
if export_filepath is None:
export_filepath = os.path.join(results_folder, '{}.h5'.format(export_name))
export_from_paintera(paintera_env_name, supervoxel_proj_path, export_filepath,
activation_command, shell)
if conncomp_on_paintera_export or result_dtype is not None:
with open_file(export_filepath, mode='r') as f:
exp_seg = f['data'][:]
if conncomp_on_paintera_export:
# Computing connected components
exp_seg = labelMultiArray(exp_seg.astype('float32'))
exp_seg = relabel_consecutive(exp_seg, sort_by_size=True)
if result_dtype is not None:
exp_seg = exp_seg.astype(result_dtype)
with File(export_filepath, mode='w') as f:
f.create_dataset('data', data=exp_seg, compression='gzip')
def small_objects_to_zero(m, size_filter):
u, c = np.unique(m, return_counts=True)
smalls = u[c < size_filter]
for small in smalls:
m[m == small] = 0
return m
def organelle_assignment_module(
results_folder,
organelle_assignments_filepath,
export_name,
mem_pred_filepath,
raw,
mem,
result_dtype,
export_binary=False,
conncomp_on_paintera_export=False,
merge_small_segments_on_paintera_export=False,
verbose=False
):
print('\nNapari and editor started in sub-processes.')
print('\nFill the text file with assignments')
with open_file(os.path.join(results_folder, 'exported_seg.h5'), mode='r') as f:
exp_seg = f['data'][:]
if conncomp_on_paintera_export:
# Computing connected components
exp_seg = labelMultiArray(exp_seg.astype('float32'))
if merge_small_segments_on_paintera_export:
# Merge small segments
print('Removing small segments ...')
# exp_seg = sizeFilterSegInplace(exp_seg.astype('uint32') + 1, int(np.max(exp_seg)), 48, checkAtBorder=True)
exp_seg = small_objects_to_zero(exp_seg + 1, 48)
print('Filling the holes ...')
print(f'exp_seg.shape = {exp_seg.shape}')
print(f'exp_seg.dtype = {exp_seg.dtype}')
exp_seg = watershedsNew(exp_seg.astype('float32'), seeds=exp_seg.astype('uint32'), neighborhood=26)[0] - 1
print('... done!')
exp_seg = relabel_consecutive(exp_seg, sort_by_size=True)
if not os.path.exists(organelle_assignments_filepath):
with open(organelle_assignments_filepath, mode='w') as f:
json.dump(
dict(
CYTO=dict(labels=[0], type='single'),
MITO=dict(labels=[], type='multi'),
# DMV=dict(labels=[], type='multi'),
# ER=dict(labels=[], type='single'),
# ENDO=dict(labels=[], type='multi'),
# LIPID=dict(labels=[], type='multi'),
# NUC=dict(labels=[], type='single'),
# EXT=dict(labels=[], type='single')
), f, indent=2)
all_ids = list(np.unique(exp_seg))
def _generate_organelle_maps():
try:
# This should be wrapped in a try/except in case of invalid json syntax
# and then be caught to tell user to correct it
# get the current organelle assignments from the text file
with open(organelle_assignments_filepath, mode='r') as f:
assignments = json.load(f)
maps = {}
assigned = []
for organelle, assignment in assignments.items():
print('found organelle: {}'.format(organelle))
maps[organelle] = np.zeros(exp_seg.shape, dtype=exp_seg.dtype)
val = 1
for idx in assignment['labels']:
maps[organelle][exp_seg == idx] = val
if assignment['type'] == 'multi':
val += 1
assigned.append(idx)
unassigned = np.setdiff1d(all_ids, assigned)
maps['MISC'] = np.zeros(exp_seg.shape, dtype=exp_seg.dtype)
val = 1
for idx in unassigned:
maps['MISC'][exp_seg == idx] = val
val += 1
map_names = sorted(maps.keys())
maps['SEMANTICS'] = np.zeros(exp_seg.shape, dtype=exp_seg.dtype)
for map_idx, map_name in enumerate(map_names):
maps['SEMANTICS'][maps[map_name] > 0] = map_idx
return maps
except:
print('Invalid json syntax!!! Fix the json file, save and update Napari again!')
return {}
def _print_help():
# I don't think we need explicit quit command any more
# print(' exit / q -> finish assignments and export')
print(' update / u -> updates Napari display')
print(' editor / e -> re-opens editor')
# start the editor in a sub-process
editor_p = mp.Process(target=_open_editor, args=(organelle_assignments_filepath,))
editor_p.start()
with napari.gui_qt():
viewer = napari.Viewer()
# add the initiail (static) layers
viewer.add_image(raw, name='raw')
if mem_pred_filepath is not None:
viewer.add_image(mem, name='mem', visible=False)
# add the initial organelle maps
organelle_maps = _generate_organelle_maps()
for name, data in organelle_maps.items():
is_visible = name == 'MISC'
viewer.add_labels(data, name=name, visible=is_visible)
viewer.add_labels(exp_seg, name='from Paintera', visible=True, opacity=0)
_print_help()
# I don't think this is necessary any more
# @viewer.bind_key('q')
# def quit(viewer):
# pass
@viewer.bind_key('h')
def help(viewer):
_print_help()
@viewer.bind_key('u')
def update(viewer):
print("Updating napari layers from organelle assignments ...")
new_organelle_maps = _generate_organelle_maps()
# iterate over the organelle maps, if we have it in the layers already, update the layer,
# otherwise add a new layer
# TODO this does not catch the case where a category is removed yet (the layer will persist)
# this should also be caught and the layer be removed
layers = viewer.layers
for name, data in new_organelle_maps.items():
is_visible = name == 'MISC'
# if name in layers:
try:
# This raises a key error if the layer does not exist
# FIXME is there a solution like 'if name in layers: ...' that does not error out?
name in layers
layers[name].data = data
except KeyError:
viewer.add_labels(data, name=name, visible=is_visible)
print("... done")
@viewer.bind_key('e')
def editor(viewer):
nonlocal editor_p
editor_p.terminate()
editor_p.join()
editor_p = mp.Process(target=_open_editor, args=(organelle_assignments_filepath,))
editor_p.start()
# 10. Export organelle maps
print('Exporting organelle maps ...')
organelle_maps = _generate_organelle_maps()
for map_name, map in organelle_maps.items():
if not os.path.exists(os.path.join(os.path.join(results_folder, 'results'))):
os.mkdir(os.path.join(os.path.join(results_folder, 'results')))
# Export labeled result
organelle_filepath = os.path.join(
results_folder,
'results',
export_name + '_{}.h5'.format(map_name)
)
_write_data(organelle_filepath, map.astype(result_dtype), verbose=verbose)
if export_binary:
# Export binary result
organelle_filepath = os.path.join(
results_folder,
'results',
export_name + '_{}_bin.h5'.format(map_name)
)
map = (1 - (1 - map.astype('float32') / map.max()).astype('uint8')) * 255
_write_data(organelle_filepath, map, verbose=verbose)
def data_loading_module(
results_folder,
supervoxel_filepath,
raw_filepath,
mem_pred_filepath,
mem_pred_channel,
annotation_shape,
auto_crop_center,
verbose
):
seg_filepath = os.path.join(
results_folder,
os.path.splitext(os.path.split(supervoxel_filepath)[1])[0] + '_seg.h5'
)
# Load raw, mem prediction, and supervoxels
full_raw_filepath = raw_filepath
raw, raw_filepath = _load_data(raw_filepath, annotation_shape, auto_crop_center,
verbose=verbose, cache_folder=results_folder)
if mem_pred_filepath is not None:
if mem_pred_channel is not None:
mem, mem_pred_filepath, mem_pred_channel_fp = _load_data(mem_pred_filepath, annotation_shape,
auto_crop_center,
normalize=True, channel=mem_pred_channel,
verbose=verbose,
cache_folder=results_folder)
else:
mem, mem_pred_channel_fp = _load_data(mem_pred_filepath, annotation_shape, auto_crop_center,
normalize=True, channel=mem_pred_channel, verbose=verbose,
cache_folder=results_folder)
else:
mem = None
mem_pred_channel_fp = None
sv, supervoxel_filepath = _load_data(supervoxel_filepath, annotation_shape,
auto_crop_center, verbose=verbose, relabel=True, cache_folder=results_folder, conncomp=True)
return(seg_filepath,
full_raw_filepath,
raw_filepath,
mem_pred_filepath,
mem_pred_channel_fp,
supervoxel_filepath,
raw, mem, sv)
def proof_reading_workflow(
results_folder,
raw_filepath,
supervoxel_folder,
seg_filepath=None,
seg_folder=None,
seg_filename_pattern='result_lmc_{z}_{y}_{x}.h5',
# mem_pred_filepath,
sv_filename_pattern='{z}_{y}_{x}.h5',
# mem_pred_channel=None,
# auto_crop_center=False,
# annotation_shape=None,
paintera_env_name='paintera_env',
activation_command='source',
shell='/bin/bash',
result_dtype='uint16',
# export_binary=False,
conncomp_on_paintera_export=True,
# pre_segmentation_filepath=None,
verbose=False
):
if not os.path.exists(results_folder):
os.mkdir(results_folder)
assert seg_filepath is not None or seg_folder is not None, 'Either seg_filepath or seg_folder has to be given!'
if seg_filepath is not None and seg_folder is not None:
print('Warning: Both seg_filepath and seg_folder given. Using seg_filepath = {}'.format(seg_filepath))
with File(raw_filepath, mode='r') as f:
full_shape = f['data'].shape
if verbose:
print('Full dataset shape = {}'.format(full_shape))
n, position_list = build_equally_spaced_volume_list(
full_shape,
target_shape=(512, 512, 512),
overlap=(256, 256, 256),
overshoot=True
)
print('Total number of cubes = {}'.format(n))
if verbose:
print(position_list)
# Make a temp directory to store intermediate files
tmp_dir = os.path.join(results_folder, 'tmp')
if not os.path.exists(tmp_dir):
os.mkdir(tmp_dir)
# __________________________________________________________________________________________________________________
# Iterate over full dataset
for pidx, position in enumerate(position_list):
x = position[1][2].start
y = position[1][1].start
z = position[1][0].start
export_filepath = os.path.join(results_folder, seg_filename_pattern.format(x=x, y=y, z=z))
if os.path.exists(export_filepath):
print('Cube {} / {} exists: {}'.format(pidx + 1, n, seg_filename_pattern.format(x=x, y=y, z=z)))
else:
print('Proof reading of cube {} / {}: {}'.format(pidx + 1, n, seg_filename_pattern.format(x=x, y=y, z=z)))
if verbose:
print(position)
# ______________________________________________________________________________________________________________
# Load data
with File(raw_filepath, mode='r') as f:
raw = f['data'][position[1]].astype('uint8')
if verbose:
print('Raw shape = {}'.format(raw.shape))
print('Raw dtype = {}'.format(raw.dtype))
if seg_filepath is not None:
if verbose:
print('Opening {}'.format(seg_filepath))
with File(seg_filepath, mode='r') as f:
seg = f['data'][position[1]]
else:
filepath = os.path.join(seg_folder, seg_filename_pattern.format(x=x, y=y, z=z))
if verbose:
print('Opening {}'.format(filepath))
with File(filepath, mode='r') as f:
seg = f['data'][:]
if verbose:
print('seg shape = {}'.format(seg.shape))
# Make a ROI map
roi_map = np.ones(raw.shape, dtype=bool)
roi_map[128: -128, 128: -128, 128: -128] = 0
# Visualize in napari
data = [
dict(type='raw', data=raw, name='raw', visible=True),
dict(type='label', data=seg, name='seg', visible=True),
dict(type='label', data=roi_map, name='roi', visible=True)
]
open_napari(data)
if not query_yes_no('Needs corrections?'):
continue
# print(np.unique(raw))
# print(raw.shape)
# print(raw.dtype)
raw[roi_map] = raw[roi_map] * 0.7
raw = raw.astype('uint8')
if verbose:
print(raw.shape)
print(raw.dtype)
tmp_seg_filepath = os.path.join(tmp_dir, 'seg.h5')
with File(tmp_seg_filepath, mode='w') as f:
f.create_dataset('data', data=seg, compression='gzip')
tmp_raw_filepath = os.path.join(tmp_dir, 'raw.h5')
with File(tmp_raw_filepath, mode='w') as f:
f.create_dataset('data', data=raw, compression='gzip')
# ______________________________________________________________________________________________________________
# Corrections with paintera
paintera_merging_module2(
tmp_dir, results_folder,
paintera_lock_file=os.path.join(tmp_dir, '.paintera_lock'),
paintera_proj_path=os.path.join(results_folder, 'paintera_proj'),
activation_command=activation_command,
paintera_env_name=paintera_env_name,
shell=shell,
seg_filepath=tmp_seg_filepath,
full_raw_filepath=tmp_raw_filepath,
supervoxel_filepath=os.path.join(supervoxel_folder, sv_filename_pattern.format(x=x, y=y, z=z)),
supervoxel_proj_path=os.path.join(tmp_dir, 'sv.n5'),
mem_pred_filepath=None,
result_dtype=result_dtype,
conncomp_on_paintera_export=conncomp_on_paintera_export,
export_filepath=export_filepath,
verbose=verbose
)
def pm_workflow(
results_folder,
raw_filepath,
mem_pred_filepath,
supervoxel_filepath,
raw_name='data',
mem_name='data',
sv_name='data',
mem_pred_channel=None,
auto_crop_center=False,
annotation_shape=None,
paintera_env_name='paintera_env',
activation_command='source',
shell='/bin/bash',
result_dtype='uint16',
export_binary=False,
conncomp_on_paintera_export=True,
merge_small_segments=False,
pre_segmentation_filepath=None,
verbose=False
):
"""
:param results_folder:
:param raw_filepath:
:param mem_pred_filepath:
:param supervoxel_filepath:
:param mem_pred_channel:
:param auto_crop_center:
:param annotation_shape: