Skip to content

Commit 7fe9237

Browse files
committed
rebuild
1 parent c2ed4d6 commit 7fe9237

File tree

5 files changed

+38
-104
lines changed

5 files changed

+38
-104
lines changed

test/sampling/pathwise/features/test_generators.py

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -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"""

test/sampling/pathwise/features/test_maps.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ def forward(self, x):
547547
map_3d = MixedDimFeatureMap(Size([2, 3, 5])) # 3D: max_ndim will be 3
548548
map_2d = MixedDimFeatureMap(Size([4, 6])) # 2D: will be expanded to 3D
549549

550-
# This should trigger lines where ndim < max_ndim and we're in the else branch
550+
# This should trigger code where ndim < max_ndim and we're in the else branch
551551
# for i in range(max_ndim - 1), specifically the else part where
552552
# idx = i - (max_ndim - ndim)
553553
mixed_direct_sum_2 = maps.DirectSumFeatureMap([map_3d, map_2d])
@@ -558,7 +558,7 @@ def forward(self, x):
558558
# map_2d has ndim = 2, so ndim < max_ndim
559559
# For i in range(2): i=0,1
560560
# For map_2d: when i >= max_ndim - ndim (i.e., i >= 3-2=1), we go to else branch
561-
# So when i=1, we execute lines 179-180: idx = 1 - (3-2) = 0,
561+
# So when i=1, we execute: idx = 1 - (3-2) = 0,
562562
# result_shape[1] = max(result_shape[1], shape[0])
563563
self.assertEqual(len(shape_2), 3) # Should have 3 dimensions
564564
self.assertEqual(shape_2[-1], 5 + 6) # Concatenation: last dims added

test/sampling/pathwise/test_prior_samplers.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ def test_model_lists(self):
128128

129129
def test_weight_generator_custom(self):
130130
"""Test custom weight generator in prior_samplers.py"""
131-
import torch
132131
from botorch.sampling.pathwise.prior_samplers import (
133132
_draw_kernel_feature_paths_fallback,
134133
)
@@ -184,30 +183,8 @@ def custom_weight_generator(shape):
184183
)
185184
self.assertTrue(torch.allclose(result.weight, torch.ones_like(result.weight)))
186185

187-
def test_weight_generator_device_handling(self):
188-
"""Test weight generator with proper device handling."""
189-
from botorch.sampling.pathwise.prior_samplers import (
190-
_draw_kernel_feature_paths_fallback,
191-
)
192-
from gpytorch.kernels import RBFKernel
193-
194-
kernel = RBFKernel(ard_num_dims=2)
195-
196-
def custom_weight_generator(shape):
197-
return torch.zeros(shape)
198-
199-
result = _draw_kernel_feature_paths_fallback(
200-
mean_module=None,
201-
covar_module=kernel,
202-
sample_shape=Size([2]),
203-
weight_generator=custom_weight_generator,
204-
)
205-
206-
# This should exercise the device handling code
207-
self.assertTrue(torch.allclose(result.weight, torch.zeros_like(result.weight)))
208-
209186
def test_approximategp_dispatcher(self):
210-
"""Test ApproximateGP dispatcher registration (line 193)."""
187+
"""Test ApproximateGP dispatcher registration."""
211188
from botorch.sampling.pathwise.prior_samplers import DrawKernelFeaturePaths
212189
from gpytorch.models import ApproximateGP
213190
from gpytorch.variational import VariationalStrategy

test/sampling/pathwise/test_update_strategies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def test_error_branches(self):
240240
from botorch.models import SingleTaskVariationalGP
241241
from linear_operator.operators import DiagLinearOperator
242242

243-
# Test exact model with non-Gaussian likelihood (lines 195-196)
243+
# Test exact model with non-Gaussian likelihood
244244
config = TestCaseConfig(device=self.device)
245245
model = gen_module(models.SingleTaskGP, config)
246246
model.likelihood = BernoulliLikelihood()
@@ -250,7 +250,7 @@ def test_error_branches(self):
250250
with self.assertRaises(NotImplementedError):
251251
gaussian_update(model=model, sample_values=sample_values)
252252

253-
# Test variational model with non-zero noise covariance (lines 203-204)
253+
# Test variational model with non-zero noise covariance
254254
variational_model = SingleTaskVariationalGP(
255255
train_X=torch.rand(5, 2),
256256
train_Y=torch.rand(5, 1),
@@ -264,7 +264,7 @@ def test_error_branches(self):
264264
noise_covariance=DiagLinearOperator(torch.ones(5)),
265265
)
266266

267-
# Test the tensor splitting with None target_values (line 217)
267+
# Test the tensor splitting with None target_values
268268
config = TestCaseConfig(device=self.device)
269269
model_list = gen_module(models.ModelListGP, config)
270270

test/sampling/pathwise/test_utils.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_sparse_block_diag_with_linear_operator(self):
205205
self.assertEqual(dense_result.shape, expected_shape)
206206

207207
def test_untransform_shape_with_input_transform(self):
208-
"""Test untransform_shape with InputTransform - covers line 142"""
208+
"""Test untransform_shape with InputTransform."""
209209
from botorch.models.transforms.input import Normalize
210210
from botorch.sampling.pathwise.utils.helpers import untransform_shape
211211

@@ -223,7 +223,7 @@ def test_untransform_shape_with_input_transform(self):
223223
self.assertEqual(result_shape, shape)
224224

225225
def test_get_kernel_num_inputs_error_case(self):
226-
"""Test get_kernel_num_inputs error case - covers lines 209-214"""
226+
"""Test get_kernel_num_inputs error case."""
227227
from botorch.sampling.pathwise.utils.helpers import get_kernel_num_inputs
228228
from gpytorch.kernels import RBFKernel
229229

@@ -390,18 +390,3 @@ def untransform(self, Y, Yvar=None):
390390
result_shape = untransform_shape(transform, shape)
391391
# Should return the transformed shape (doubled last dimension)
392392
self.assertEqual(result_shape, torch.Size([10, 4]))
393-
394-
def test_get_train_inputs_branch_coverage(self):
395-
"""Test specific branch in _get_train_inputs_SingleTaskVariationalGP"""
396-
from botorch.sampling.pathwise.utils.helpers import get_train_inputs
397-
398-
# Create a variational model
399-
model = self.models[2] # Use a SingleTaskVariationalGP
400-
if not isinstance(model, SingleTaskVariationalGP):
401-
return # Skip if not the right model type
402-
403-
# Test with training=False and transformed=False to hit specific branch
404-
model.eval() # Set to eval mode
405-
result = get_train_inputs(model, transformed=False)
406-
self.assertIsInstance(result, tuple)
407-
self.assertEqual(len(result), 1)

0 commit comments

Comments
 (0)