hello,
I have been working to automate some tasks for a set of CT scans which otherwise would take a long time. The code I wrote worked perfectly and in reasonable amount of time until now when I wrote a function where multiple segments (overall four colors) belonging to one category (one color) are merged together using the segment editor and logical operators. This function (if required could post it here) is now the bottleneck with the code not even finishing - I tried couple of things - but in vain - I also optimized my code where possible I thought I could - reducing time by 10-12 seconds, but still it takes a long time -and eventually 3d slicer crashes. I tried this on both - Mac (8 gigs RAM) and Windows (16 gigs RAM).
To figure out the bottleneck in more detail, I profiled my code and the attached snapshot shows the total time taken. I am a bit confused as to how this merging process can take so much time programmatically although when I do manually it is smooth and super fast. As can be seen from the profile report/analysis, the SegmentEditorThresholdEffect and SegmentEditorLogicalEffect are the two main functions that take most of the time. I have disabled volume rendering, display etc. but not much effect.
The function does this: There is a segmentation comprising of multiple RGBY segments and I have to merge them into just these four colors. So i add a new segment to my segmentation (empty) and then perform logical operations (union) on each segment of a particular color (modifier segment) and this new target segment and then remove the individual ones.
Can someone please share some insight as to what I could be doing wrong or some advice/tip? I can gladly share the code if required.
Edit 1: I added the code, should give more idea as to what I am doing. Let me know please if some info is required
CODE:
# segment editor part
def combine_same_colors(folder_num, bone_obj_seg_list, final_segmentation_node, final_segmentation):
# naming and color vals
vol_name = folder_num + 'HRimage'
color_dict = {'blue': (0, 0, 1),
'red': (1, 0, 0),
'green': (0, 1, 0),
'yellow': (1, 1, 0)}
# set up the segment editor widget and node
vol_node = slicer.util.getNode(vol_name)
seg_edit_wid = slicer.qMRMLSegmentEditorWidget()
seg_edit_wid.setMRMLScene(slicer.mrmlScene)
seg_edit_node = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLSegmentEditorNode")
seg_edit_wid.setMRMLSegmentEditorNode(seg_edit_node)
seg_edit_wid.setSegmentationNode(final_segmentation_node)
seg_edit_wid.setMasterVolumeNode(vol_node)
seg_edit_wid.setActiveEffectByName("Logical operators")
effect = seg_edit_wid.activeEffect()
effect.setParameter("Operation", "UNION")
segments_list = [i[0].get_segment() for i in bone_obj_seg_list]
colors = ['blue', 'red', 'green', 'yellow']
# code to check if there are multiple segments belonging to same color and if yes, merge them
def func(color):
return lambda x: True if color in x else False
for i in colors:
check_multiple_seg = Counter(map(func(i), segments_list))[True]
if check_multiple_seg > 1:
color_seg_list = [j for j in segments_list if i in j]
segment_name = folder_num + 'lego' + i
final_segmentation.AddEmptySegment(segment_name)
final_segmentation.GetSegment(segment_name).SetColor(color_dict[i][0],
color_dict[i][1],
color_dict[i][2])
# target_seg_id = final_segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(segment_name)
target_seg_id = final_segmentation.GetSegmentIdBySegmentName(segment_name)
# source_seg_id = [final_segmentation_node.GetSegmentation().GetSegmentIdBySegmentName(i) for i in color_seg_list]
source_seg_id = [final_segmentation.GetSegmentIdBySegmentName(i) for i in color_seg_list]
seg_edit_node.SetSelectedSegmentID(target_seg_id)
for i in source_seg_id:
effect.setParameter("ModifierSegmentID", i)
effect.self().onApply()
final_segmentation.RemoveSegment(i)
return final_segmentation_node, final_segmentation
Thank you very much.
Windows Errors: (I also increased the virtual memory in Win 10, but no improvement)