@@ -248,6 +248,46 @@ def test_morphology_val_structure_grad(
248
248
check_grads (op , modes = ["rev" ], order = 1 )(x , structure = k , mode = mode )
249
249
250
250
251
+ class TestMorphology1D :
252
+ """Test morphological operations with 1D-like structuring elements."""
253
+
254
+ @pytest .mark .parametrize ("h, w" , [(1 , 3 ), (3 , 1 ), (1 , 5 ), (5 , 1 )])
255
+ def test_1d_structuring_elements (self , rng , h , w ):
256
+ """Test grey dilation with 1D-like structuring elements on 2D arrays."""
257
+ x = rng .random ((8 , 8 ))
258
+
259
+ # Test with size parameter
260
+ size_tuple = (h , w )
261
+ result_size = grey_dilation (x , size = size_tuple )
262
+
263
+ # Verify output shape matches input
264
+ assert result_size .shape == x .shape
265
+
266
+ # Verify that dilation actually increases values (or keeps them the same)
267
+ assert np .all (result_size >= x )
268
+
269
+ # Test that we can also use structure parameter with 1D-like arrays
270
+ structure = np .ones ((h , w ))
271
+ result_struct = grey_dilation (x , structure = structure )
272
+ assert result_struct .shape == x .shape
273
+
274
+ def test_1d_gradient_flow (self , rng ):
275
+ """Test gradient flow through 1D-like structuring elements."""
276
+ x = rng .random ((6 , 6 ))
277
+
278
+ # Test horizontal 1D structure
279
+ check_grads (lambda x : grey_dilation (x , size = (1 , 3 )), modes = ["rev" ], order = 1 )(x )
280
+
281
+ # Test vertical 1D structure
282
+ check_grads (lambda x : grey_dilation (x , size = (3 , 1 )), modes = ["rev" ], order = 1 )(x )
283
+
284
+ # Test with structure parameter
285
+ struct_h = np .ones ((1 , 3 ))
286
+ struct_v = np .ones ((3 , 1 ))
287
+ check_grads (lambda x : grey_dilation (x , structure = struct_h ), modes = ["rev" ], order = 1 )(x )
288
+ check_grads (lambda x : grey_dilation (x , structure = struct_v ), modes = ["rev" ], order = 1 )(x )
289
+
290
+
251
291
class TestMorphologyExceptions :
252
292
"""Test exceptions in morphological operations."""
253
293
@@ -264,6 +304,13 @@ def test_even_structure_dimensions(self, rng):
264
304
with pytest .raises (ValueError , match = "Structuring element dimensions must be odd" ):
265
305
grey_dilation (x , structure = k_even )
266
306
307
+ def test_both_size_and_structure (self , rng ):
308
+ """Test that an exception is raised when both size and structure are provided."""
309
+ x = rng .random ((5 , 5 ))
310
+ k = np .ones ((3 , 3 ))
311
+ with pytest .raises (ValueError , match = "Cannot specify both size and structure" ):
312
+ grey_dilation (x , size = 3 , structure = k )
313
+
267
314
268
315
@pytest .mark .parametrize (
269
316
"array, out_min, out_max, in_min, in_max, expected" ,
0 commit comments