@@ -495,8 +495,9 @@ def apply_ilastik_parallel(
495
495
corners_chunks = [corners [i : i + 100 ] for i in range (0 , len (corners ), 100 )]
496
496
497
497
for corners_chunk in tqdm (corners_chunks , desc = "corner chunks" ):
498
- Parallel (n_jobs = self .ncpu )(
499
- delayed (self ._process_chunk )(
498
+ if self .ncpu == 1 :
499
+ for corner in tqdm (corners_chunk , leave = False ):
500
+ self ._process_chunk (
500
501
corner [0 ],
501
502
corner [1 ],
502
503
volume_base_dir ,
@@ -506,8 +507,20 @@ def apply_ilastik_parallel(
506
507
self .object_type ,
507
508
results_dir ,
508
509
)
509
- for corner in tqdm (corners_chunk , leave = False )
510
- )
510
+ else :
511
+ Parallel (n_jobs = self .ncpu )(
512
+ delayed (self ._process_chunk )(
513
+ corner [0 ],
514
+ corner [1 ],
515
+ volume_base_dir ,
516
+ layer_names ,
517
+ threshold ,
518
+ data_dir ,
519
+ self .object_type ,
520
+ results_dir ,
521
+ )
522
+ for corner in tqdm (corners_chunk , leave = False )
523
+ )
511
524
for f in os .listdir (data_dir ):
512
525
os .remove (os .path .join (data_dir , f ))
513
526
@@ -566,50 +579,58 @@ def _process_chunk(
566
579
fname = f"image_{ c1 [0 ]} _{ c1 [1 ]} _{ c1 [2 ]} .h5"
567
580
fname = data_dir / fname
568
581
569
- with h5py .File (fname , "w" ) as f :
570
- dset = f .create_dataset ("image_3channel" , data = image_3channel )
571
-
572
- subprocess .run (
573
- [
574
- f"{ self .ilastik_path } " ,
575
- "--headless" ,
576
- f"--project={ self .ilastik_project } " ,
577
- fname ,
578
- ],
579
- stdout = subprocess .PIPE ,
580
- stderr = subprocess .PIPE ,
581
- )
582
- # subprocess.run(["/Applications/ilastik-1.3.3post3-OSX.app/Contents/ilastik-release/run_ilastik.sh", "--headless", "--project=/Users/thomasathey/Documents/mimlab/mouselight/ailey/benchmark_formal/brain3/matt_benchmark_formal_brain3.ilp", fname], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
583
-
584
- fname_prob = str (fname ).split ("." )[0 ] + "_Probabilities.h5"
585
- with h5py .File (fname_prob , "r" ) as f :
586
- pred = f .get ("exported_data" )
587
- if object_type == "soma" :
588
- fname_results = f"image_{ c1 [0 ]} _{ c1 [1 ]} _{ c1 [2 ]} _somas.txt"
589
- fname_results = results_dir / fname_results
590
- pred = pred [0 , :, :, :]
591
- mask = pred > threshold
592
- labels = measure .label (mask )
593
- props = measure .regionprops (labels )
582
+ for attempt in range (3 ):
583
+ with h5py .File (fname , "w" ) as f :
584
+ dset = f .create_dataset ("image_3channel" , data = image_3channel )
594
585
595
- results = []
596
- for prop in props :
597
- if prop ["area" ] > area_threshold :
598
- location = list (np .add (c1 , prop ["centroid" ]))
599
- results .append (location )
600
- if len (results ) > 0 :
601
- with open (fname_results , "w" ) as f2 :
602
- for location in results :
603
- f2 .write (str (location ))
604
- f2 .write ("\n " )
605
- elif object_type == "axon" :
606
- dir_mask = volume_base_dir + "axon_mask"
607
- vol_mask = CloudVolume (
608
- dir_mask , parallel = 1 , mip = mip , fill_missing = True , compress = False
609
- )
610
- pred = pred [1 , :, :, :]
611
- mask = np .array (pred > threshold ).astype ("uint64" )
612
- vol_mask [c1 [0 ] : c2 [0 ], c1 [1 ] : c2 [1 ], c1 [2 ] : c2 [2 ]] = mask
586
+ subprocess .run (
587
+ [
588
+ f"{ self .ilastik_path } " ,
589
+ "--headless" ,
590
+ f"--project={ self .ilastik_project } " ,
591
+ fname ,
592
+ ],
593
+ stdout = subprocess .PIPE ,
594
+ stderr = subprocess .PIPE ,
595
+ )
596
+
597
+ fname_prob = str (fname ).split ("." )[0 ] + "_Probabilities.h5"
598
+ try :
599
+ with h5py .File (fname_prob , "r" ) as f :
600
+ pred = f .get ("exported_data" )
601
+ except :
602
+ if attempt >= 2 :
603
+ raise ValueError (f"Tried to evaluate thrice and failed" )
604
+ if os .path .isfile (fname_prob ):
605
+ os .remove (fname_prob )
606
+ continue
607
+
608
+ if object_type == "soma" :
609
+ fname_results = f"image_{ c1 [0 ]} _{ c1 [1 ]} _{ c1 [2 ]} _somas.txt"
610
+ fname_results = results_dir / fname_results
611
+ pred = pred [0 , :, :, :]
612
+ mask = pred > threshold
613
+ labels = measure .label (mask )
614
+ props = measure .regionprops (labels )
615
+
616
+ results = []
617
+ for prop in props :
618
+ if prop ["area" ] > area_threshold :
619
+ location = list (np .add (c1 , prop ["centroid" ]))
620
+ results .append (location )
621
+ if len (results ) > 0 :
622
+ with open (fname_results , "w" ) as f2 :
623
+ for location in results :
624
+ f2 .write (str (location ))
625
+ f2 .write ("\n " )
626
+ elif object_type == "axon" :
627
+ dir_mask = volume_base_dir + "axon_mask"
628
+ vol_mask = CloudVolume (
629
+ dir_mask , parallel = 1 , mip = mip , fill_missing = True , compress = False
630
+ )
631
+ pred = pred [1 , :, :, :]
632
+ mask = np .array (pred > threshold ).astype ("uint64" )
633
+ vol_mask [c1 [0 ] : c2 [0 ], c1 [1 ] : c2 [1 ], c1 [2 ] : c2 [2 ]] = mask
613
634
614
635
def collect_soma_results (self , brain_id : str ):
615
636
"""Combine all soma detections and post to neuroglancer. Intended for use after apply_ilastik_parallel.
0 commit comments