Fix divide-by-zero RuntimeWarning in PeelColor.get_green_yellow_values#66
Open
loren-honaas wants to merge 4 commits into
Open
Fix divide-by-zero RuntimeWarning in PeelColor.get_green_yellow_values#66loren-honaas wants to merge 4 commits into
loren-honaas wants to merge 4 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix divide-by-zero RuntimeWarning in PeelColor.get_green_yellow_values
Problem
When processing images where no pixels pass the LAB color thresholds,
get_green_yellow_valuesraises aRuntimeWarning: invalid value encountered in divideon lines 135-137:This occurs when
th123— the combined LAB threshold mask — excludes all pixels, causingnp.count_nonzeroto return 0. The resultingnanvalues propagate silently through the downstream spherical coordinate normalization, producing meaningless scores.This was observed when running the color analysis on Granny-segmented images of blush apples. Sun-side (blush-facing) images have no green/yellow pixels and correctly produce an empty
th123mask — but instead of being flagged, they were silently assigned bin=1 with no score, which is misleading.A secondary issue is that each channel used its own independent
np.count_nonzero, meaning the three means could in principle be computed over different pixel counts — this is inconsistent.Fix
Compute a single shared
pixel_count = np.count_nonzero(th123)and use it as the denominator for all three channels. Add an explicit guard that returns(nan, nan, nan)when no pixels pass the threshold, making it clear in the output that the image could not be scored rather than propagating silent errors.Impact
RuntimeWarningentirelynanexplicitly rather than silently producing invalid results