@@ -354,6 +354,7 @@ def normalizer(x):
354354 continue
355355 if (loop == 1 ) and ("label_elem" not in locals ()):
356356 if "bone_n" in locals () and "skin_n" in locals ():
357+ print ("decoupling bone and skin surfaces" )
357358 bone_n , bone_f = surfboolean (
358359 bone_n [:, :3 ],
359360 bone_f [:, :3 ],
@@ -362,14 +363,17 @@ def normalizer(x):
362363 skin_f [:, :3 ],
363364 )
364365 if "bone_n" in locals () and "csf_n" in locals ():
366+ print ("decoupling csf and bone surfaces" )
365367 csf_n , csf_f = surfboolean (
366368 csf_n [:, :3 ], csf_f [:, :3 ], "decouple" , bone_n [:, :3 ], bone_f [:, :3 ]
367369 )
368370 if "pial_n" in locals () and "csf_n" in locals ():
371+ print ("decoupling pial and csf surfaces" )
369372 pial_n , pial_f = surfboolean (
370373 pial_n [:, :3 ], pial_f [:, :3 ], "decouple" , csf_n [:, :3 ], csf_f [:, :3 ]
371374 )
372375 if "pial_n" in locals () and "wm_n" in locals ():
376+ print ("decoupling wm and pial surfaces" )
373377 wm_n , wm_f = surfboolean (
374378 wm_n [:, :3 ], wm_f [:, :3 ], "decouple" , pial_n [:, :3 ], pial_f [:, :3 ]
375379 )
@@ -452,18 +456,27 @@ def normalizer(x):
452456 return brain_n , brain_el , brain_f
453457
454458 # Generates a coarse tetrahedral mesh of the combined tissues
459+ final_surf_n , final_surf_f = meshcheckrepair (final_surf_n , final_surf_f , "dup" )
455460 try :
456461 final_n , final_e , _ = s2m (
457- final_surf_n , final_surf_f , 1.0 , maxvol , "tetgen1.5" , None , None , "-A"
462+ final_surf_n ,
463+ final_surf_f ,
464+ 1.0 ,
465+ maxvol ,
466+ "tetgen1.5" ,
467+ None ,
468+ None ,
469+ "-YY -A" ,
458470 )
459- except :
471+ except RuntimeError as e :
460472 print (
461- "volumetric mesh generation failed, returning the intermediate surface model only"
473+ f "volumetric mesh generation failed with error: { e } , returning the intermediate surface model only"
462474 )
463- brain_n = final_surf_n
464- brain_f = final_surf_f
465- brain_el = np .array ([])
466- return brain_n , brain_el , brain_f
475+ continue
476+ # brain_n = final_surf_n
477+ # brain_f = final_surf_f
478+ # brain_el = np.array([])
479+ # return brain_n, brain_el, brain_f
467480
468481 # Removes the elements that are part of the box, but not the brain/head
469482 if dotruncate == 1 or isinstance (dotruncate , str ):
@@ -1761,7 +1774,7 @@ def brain1020(
17611774
17621775
17631776def label2tpm (
1764- vol : np .ndarray , names : Optional [Union [List [str ], Dict [int , str ]]] = None
1777+ vol : np .ndarray , names : Optional [Union [List [str ], Dict [int , str ]]] = None , ** kwargs
17651778) -> Dict [str , np .ndarray ]:
17661779 """
17671780 Converting a multi-label volume to binary tissue probabilistic maps (TPMs)
@@ -1824,6 +1837,17 @@ def label2tpm(
18241837 # MATLAB: seg.(nm) = uint8(vol == val(i))
18251838 seg [nm ] = (vol == val [i ]).astype (np .uint8 )
18261839
1840+ if kwargs .get ("sigma" , 0 ) > 0 :
1841+ from scipy .ndimage import gaussian_filter
1842+
1843+ segsum = np .zeros_like (seg [next (iter (seg ))], dtype = np .float32 )
1844+ for key in seg :
1845+ seg [key ] = gaussian_filter (seg [key ].astype (np .float32 ), sigma = 1 )
1846+ segsum += seg [key ]
1847+
1848+ for key in seg :
1849+ np .divide (seg [key ], segsum , out = seg [key ], where = segsum != 0 )
1850+
18271851 return seg
18281852
18291853
0 commit comments