From ebddbbcbc9eb44e60ab1340ea488ed60d23c89ca Mon Sep 17 00:00:00 2001 From: Nirkan Date: Wed, 14 Jan 2026 17:38:53 +0100 Subject: [PATCH 1/2] Handle empty mask in obj_size_ecdf. --- plantcv/plantcv/visualize/obj_size_ecdf.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/plantcv/plantcv/visualize/obj_size_ecdf.py b/plantcv/plantcv/visualize/obj_size_ecdf.py index 52c4f0c05..9b235f280 100644 --- a/plantcv/plantcv/visualize/obj_size_ecdf.py +++ b/plantcv/plantcv/visualize/obj_size_ecdf.py @@ -27,6 +27,16 @@ def obj_size_ecdf(mask): # Remove objects with areas < 1px areas = [i for i in areas if i >= 1.0] + # If the mask is empty, return empty chart + if len(areas) == 0: + ecdf_df = pd.DataFrame({'object_area': [], 'cumulative_probability': []}) + chart = alt.Chart(ecdf_df).mark_circle(size=10).encode( + x=alt.X("object area:Q").scale(type='log'), + y="cumulative probability:Q", + tooltip=['object area', 'cumulative probability'] + ) + return chart.interactive() + ecdf = ECDF(areas, side='right') ecdf_df = pd.DataFrame({'object area': ecdf.x[1:], 'cumulative probability': ecdf.y[1:]}) From e6244729554f203ef9ba420b99bd34e8757135c4 Mon Sep 17 00:00:00 2001 From: Nirkan Date: Wed, 14 Jan 2026 18:03:29 +0100 Subject: [PATCH 2/2] Test for empty mask. --- tests/plantcv/visualize/test_obj_size_ecdf.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/plantcv/visualize/test_obj_size_ecdf.py b/tests/plantcv/visualize/test_obj_size_ecdf.py index 5dff0750b..c2d61ccb5 100644 --- a/tests/plantcv/visualize/test_obj_size_ecdf.py +++ b/tests/plantcv/visualize/test_obj_size_ecdf.py @@ -1,5 +1,7 @@ """Tests for pcv.visualize.obj_size_ecdf.""" import cv2 +import numpy as np +import pandas as pd from altair.vegalite.v5.api import Chart from plantcv.plantcv.visualize import obj_size_ecdf @@ -9,3 +11,11 @@ def test_obj_size_ecdf(visualize_test_data): mask = cv2.imread(visualize_test_data.small_bin_img, -1) fig_ecdf = obj_size_ecdf(mask=mask) assert isinstance(fig_ecdf, Chart) + + +def test_obj_size_ecdf_empty_mask(): + """Test for empty mask""" + mask = np.zeros((100, 100), dtype=np.uint8) + chart = obj_size_ecdf(mask=mask) + assert isinstance(chart.data, pd.DataFrame) + assert chart.data.shape[0] == 0