Skip to content

Commit

Permalink
Merge pull request #51 from UBC-MDS/func_util_empty_arr
Browse files Browse the repository at this point in the history
Func util empty arr
  • Loading branch information
InderKhera authored Jan 16, 2025
2 parents 28f8420 + e24b3e3 commit 71a8761
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 2 deletions.
138 changes: 138 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ readme = "README.md"

[tool.poetry.dependencies]
python = "^3.11"
numpy = "^2.2.1"

[tool.poetry.group.dev.dependencies]
pytest = "^8.3.4"

[tool.semantic_release]
version_toml = [
Expand Down
2 changes: 1 addition & 1 deletion src/sharpedge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# read version from installed package
from importlib.metadata import version
__version__ = version("sharpedge")
__version__ = version("sharpedge")
8 changes: 7 additions & 1 deletion src/sharpedge/_utils/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ def _input_checker(img_array):
raise ValueError(
f"Invalid image shape: {img_array.shape}. "
"Expected 2D (grayscale) or 3D with 3 channels (RGB)."
)
)
# Check if the array is empty or contains zero-sized dimensions
if img_array.size == 0 or any(dim == 0 for dim in img_array.shape):
raise ValueError("Image size must not be zero in any dimension.")
# Check if the image is 2D or 3D
if img_array.ndim not in (2, 3):
raise ValueError("Image array must be 2D or 3D.")
61 changes: 61 additions & 0 deletions tests/test_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,64 @@ def test_edge_image_format(edge_array):
def test_erroneous_image_format(invalid_input, expected_error):
with pytest.raises(TypeError, match=expected_error):
Utility._input_checker(invalid_input)


# Part 3 of the input_checker: Empty Array & Missing Dimensions
# Valid Cases: Test the utility function with valid edge cases
@pytest.mark.parametrize("edge_array", [
np.array([[255]]), # 1 pixel 2D array
np.array([[[255, 0, 0]]]), # 1 pixel 3D array
np.array([[1.5, 2.5]]), # 2D array with float values
np.array([[[2.35, 3.45, 4.55]]]), # 1 pixel 3D array with float values
np.array([[np.nan, np.nan], [np.nan, np.nan]]), # 2D array with NaN values to simulate "empty"
np.array([[[np.nan, np.nan, np.nan], [np.nan, np.nan, np.nan]]]) # 3D array with NaN values to simulate "empty"
])
def test_edge_image_format(edge_array):
result = Utility._input_checker(edge_array)
assert result is None # Expect no error for valid arrays

# Erroneous Cases: Test with invalid input and check if expected error is raised
@pytest.mark.parametrize("invalid_input, expected_error", [
(np.array([1, 2, 3]), "Image array must be 2D or 3D."), # 1D array
(np.random.rand(2, 2, 2, 2), "Image array must be 2D or 3D."), # 4D array
(np.array([]), "Image size must not be zero in any dimension."), # Empty array
(np.empty((0, 10)), "Image size must not be zero in any dimension."), # Zero-sized dimension in 2D array
(np.empty((0, 10, 3)), "Image size must not be zero in any dimension."), # Zero-sized dimension in 3D array
(np.empty((0, 0, 3)), "Image size must not be zero in any dimension."), # Zero-sized dimensions in multiple axes
(np.empty((1, 0)), "Image size must not be zero in any dimension."), # Zero-sized column in 2D
(np.empty((0, 0)), "Image size must not be zero in any dimension.") # Zero-sized 2D array
])
def test_erroneous_image_format(invalid_input, expected_error):
with pytest.raises(ValueError, match=expected_error):
Utility._input_checker(invalid_input)

# Valid Case: Test for 2D array
@pytest.mark.parametrize("valid_input", [
np.array([[1, 2], [3, 4]]), # Standard 2D array
np.array([[1.1, 2.2], [3.3, 4.4]]), # 2D array with float values
np.array([[255, 255], [0, 0]]) # 2D array with image-like values
])
def test_valid_2d_array(valid_input):
result = Utility._input_checker(valid_input)
assert result is None # Expect no error for valid arrays

# Valid Case: Test for 3D array
@pytest.mark.parametrize("valid_input", [
np.array([[[1, 2, 3]]]), # 1 pixel 3D array
np.array([[[1.1, 2.2, 3.3]]]), # 1 pixel 3D array with float values
np.array([[[255, 0, 0]]]) # 1 pixel 3D array with image-like values
])
def test_valid_3d_array(valid_input):
result = Utility._input_checker(valid_input)
assert result is None # Expect no error for valid arrays

# Erroneous Case: Check for ValueError on zero-sized dimension
@pytest.mark.parametrize("invalid_input", [
np.empty((0, 10)), # Zero-sized row in 2D array
np.empty((10, 0)), # Zero-sized column in 2D array
np.empty((0, 10, 3)), # Zero-sized dimension in 3D array
np.empty((0, 0)), # Fully empty 2D array
np.empty((0, 0, 0)) # Fully empty 3D array
])
def test_zero_size_dimensions(invalid_input):
with pytest.raises(ValueError, match="Image size must not be zero in any dimension."):

0 comments on commit 71a8761

Please sign in to comment.