pyplati / platipy

Processing Library and Analysis Toolkit for Medical Imaging in Python
https://pyplati.github.io/platipy/
Apache License 2.0
103 stars 24 forks source link

Code gets stuck in generate_valve_using_cylinder in valve.py #251

Open MarkGardnerUSyd opened 5 months ago

MarkGardnerUSyd commented 5 months ago

Hey guys. I have been generating some cardiac substructures and the code has been really helpful and easy to use.

One minor issue I have found is that in the generate_valve_using_cylinder function in valve.py in some rare occasions the code gets stuck in the following loop:

`# Define the overlap region (using binary dilation)

Increment overlap to make sure we have enough voxels

dilation = 1
overlap_vol = 0
while overlap_vol <= 2000:
    dilation_img = [int(dilation / i) for i in label_ventricle.GetSpacing()]
    overlap = sitk.BinaryDilate(label_atrium, dilation_img) & sitk.BinaryDilate(
        label_ventricle, dilation_img
    )
    overlap_vol = np.sum(sitk.GetArrayFromImage(overlap) * np.prod(overlap.GetSpacing()))
    dilation += 1

`

This was caused by me inputting a garbage input image into the cardiac segmentation code, but it meant that the dilation value kept increasing but the while loop was never escaped, most likely due to ventricle image not being found or in the image space. This may require a way to escape the loop to avoid an infinite loop? Could also be something that is found by the code that generates the ventricle segmentations as I am generating my own ventricle segmentations.

pchlap commented 5 months ago

Hi @MarkGardnerUSyd,

thanks for flagging this issue. Yes, this would be good to resolve. I remember seeing issues (not sure if it was this one or something else) when the whole heart segmentation step would fail. My thinking was that we could probably do some sanity checks after the wholeheart seg to enure that the resulting structure at least looks something like a heart that we expect (based on volume and shape etc). If not then we skip the sub-structure seg part and don't return those structures.

That solution would probably take a bit of work to exactly determine whether the whole heart segmentation was successful or not. So in the interim, putting a condition to escape that loop as you suggest would be useful. I think we would basically just need to check that dilation<some_value in the while clause. I can put that in before we next make a release for platipy. But if you'd be keen to put in a PR with that change that would also be appreciated :)

MarkGardnerUSyd commented 5 months ago

I have added a change in a new branch, named MarkEdits. I'm not sure how you wanted to deal with how you want to exit the loop, so I went for:

if dilation > max(label_ventricle.GetSize()): print('Failure to generate cylinder') return -1