jrkerns / pylinac

An image analysis library for medical physics
https://pylinac.readthedocs.io/en/latest/
MIT License
153 stars 98 forks source link

Catphan Modules without background_roi_settings result in nan background_median & background_std values #354

Closed randlet closed 3 years ago

randlet commented 3 years ago

Describe the bug

For Catphan modules without background_roi_settings (CTP486) the background_median/background_std get set to nan in _setup_rois because np.mean([]) == np.std([]) == nan. This throws RuntimeWarnings and / or FloatingPointError's. This nan gets propagated through to cnr calculations for the module rois

To Reproduce


from pylinac import CatPhan604

cbct = CatPhan604.from_zip("CatPhan604.zip")
cbct.analyze()
print([roi.cnr for roi in cbct.ctp486.rois.values()])

# /lib/python3.6/site-packages/numpy/core/fromnumeric.py:3373: RuntimeWarning: Mean of empty slice.
# /lib/python3.6/site-packages/numpy/core/_methods.py:170: RuntimeWarning: invalid value encountered in double_scalars
# /lib/python3.6/site-packages/numpy/core/_methods.py:234: RuntimeWarning: Degrees of freedom <= 0 for slice
# /lib/python3.6/site-packages/numpy/core/_methods.py:195: RuntimeWarning: invalid value encountered in true_divide
# /lib/python3.6/site-packages/numpy/core/_methods.py:226: RuntimeWarning: invalid value encountered in double_scalars

# [nan, nan, nan, nan, nan]
randlet commented 3 years ago

Also, somewhat related, should

            background_median = np.mean([roi.pixel_value for roi in self.background_rois.values()])

be

            background_mean = np.mean([roi.pixel_value for roi in self.background_rois.values()])

or

            background_median = np.median([roi.pixel_value for roi in self.background_rois.values()])

?

jrkerns commented 3 years ago

Quantitatively, mean or median doesn't make a big difference in this context, but for consistency, let's go with mean (middle).