@@ -232,7 +232,7 @@ def test_odd_num_random_features_error(self):
232232 )
233233
234234 def test_rbf_weight_generator_shape_error (self ):
235- """Test shape validation error in RBF weight generator"""
235+ """Test shape validation in RBF weight generator"""
236236 from unittest .mock import patch
237237
238238 from botorch .sampling .pathwise .features .generators import (
@@ -242,38 +242,24 @@ def test_rbf_weight_generator_shape_error(self):
242242 config = TestCaseConfig (seed = 0 , device = self .device , num_inputs = 2 )
243243 kernel = gen_module (kernels .RBFKernel , config )
244244
245- # Mock draw_sobol_normal_samples to trigger the shape error
246- def mock_weight_gen (shape ):
247- if len (shape ) != 2 :
248- raise ValueError ("Wrong shape dimensions" )
249- return torch .randn (shape , device = kernel .device , dtype = kernel .dtype )
250-
251- # Trigger the internal weight generator with wrong shape
245+ # Patch _gen_fourier_features to call weight generator with invalid shape
252246 with patch (
253- "botorch.sampling.pathwise.features.generators.draw_sobol_normal_samples" ,
254- side_effect = mock_weight_gen ,
255- ):
256- # This should call the weight generator with a 1D shape to trigger the error
257- with patch (
258- "botorch.sampling.pathwise.features.generators._gen_fourier_features"
259- ) as mock_fourier :
260-
261- def mock_fourier_call (* args , ** kwargs ):
262- # Call the weight generator with malformed shape to trigger lines
263- weight_gen = kwargs ["weight_generator" ]
264- try :
265- weight_gen (
266- torch .Size ([10 ])
267- ) # 1D shape should trigger the error
268- except UnsupportedError :
269- pass
270- return torch .nn .Identity () # Return dummy
271-
272- mock_fourier .side_effect = mock_fourier_call
273- _gen_kernel_feature_map_rbf (kernel , num_random_features = 64 )
247+ "botorch.sampling.pathwise.features.generators._gen_fourier_features"
248+ ) as mock_fourier :
249+
250+ def mock_fourier_call (weight_generator , ** kwargs ):
251+ # Call the weight generator with 1D shape to trigger ValueError
252+ with self .assertRaisesRegex (
253+ UnsupportedError , "Expected.*2-dimensional"
254+ ):
255+ weight_generator (torch .Size ([10 ])) # 1D shape
256+ return None
257+
258+ mock_fourier .side_effect = mock_fourier_call
259+ _gen_kernel_feature_map_rbf (kernel , num_random_features = 64 )
274260
275261 def test_matern_weight_generator_shape_error (self ):
276- """Test shape validation error in Matern weight generator"""
262+ """Test shape validation in Matern weight generator"""
277263 from unittest .mock import patch
278264
279265 from botorch .sampling .pathwise .features .generators import (
@@ -283,35 +269,21 @@ def test_matern_weight_generator_shape_error(self):
283269 config = TestCaseConfig (seed = 0 , device = self .device , num_inputs = 2 )
284270 kernel = gen_module (kernels .MaternKernel , config )
285271
286- # Mock draw_sobol_normal_samples to trigger the shape error
287- def mock_weight_gen (shape ):
288- if len (shape ) != 2 :
289- raise ValueError ("Wrong shape dimensions" )
290- return torch .randn (shape , device = kernel .device , dtype = kernel .dtype )
291-
292- # Trigger the internal weight generator with wrong shape
272+ # Patch _gen_fourier_features to call weight generator with invalid shape
293273 with patch (
294- "botorch.sampling.pathwise.features.generators.draw_sobol_normal_samples" ,
295- side_effect = mock_weight_gen ,
296- ):
297- # This should call the weight generator with a 1D shape to trigger the error
298- with patch (
299- "botorch.sampling.pathwise.features.generators._gen_fourier_features"
300- ) as mock_fourier :
301-
302- def mock_fourier_call (* args , ** kwargs ):
303- # Call the weight generator with malformed shape to trigger lines
304- weight_gen = kwargs ["weight_generator" ]
305- try :
306- weight_gen (
307- torch .Size ([10 ])
308- ) # 1D shape should trigger the error
309- except UnsupportedError :
310- pass
311- return torch .nn .Identity () # Return dummy
312-
313- mock_fourier .side_effect = mock_fourier_call
314- _gen_kernel_feature_map_matern (kernel , num_random_features = 64 )
274+ "botorch.sampling.pathwise.features.generators._gen_fourier_features"
275+ ) as mock_fourier :
276+
277+ def mock_fourier_call (weight_generator , ** kwargs ):
278+ # Call the weight generator with 1D shape to trigger ValueError
279+ with self .assertRaisesRegex (
280+ UnsupportedError , "Expected.*2-dimensional"
281+ ):
282+ weight_generator (torch .Size ([10 ])) # 1D shape
283+ return None
284+
285+ mock_fourier .side_effect = mock_fourier_call
286+ _gen_kernel_feature_map_matern (kernel , num_random_features = 64 )
315287
316288 def test_scale_kernel_coverage (self ):
317289 """Test ScaleKernel condition - active_dims different from base kernel"""
0 commit comments