Skip to content

Commit c58467a

Browse files
authored
[Fix] Fix off-by-one in Covobs pos bounds check (#288)
* [Fix] Fix off-by-one in Covobs pos bounds check * [Fix] Also reject negative pos values
1 parent 470e2c5 commit c58467a

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

pyerrors/covobs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def __init__(self, mean, cov, name, pos=None, grad=None):
3131
else:
3232
raise ValueError('Have to specify position of cov-element belonging to mean!')
3333
else:
34-
if pos > self.N:
35-
raise ValueError(f'pos {pos} too large for covariance matrix with dimension {self.N}x{self.N}!')
34+
if pos < 0 or pos >= self.N:
35+
raise ValueError(f'pos {pos} not valid for covariance matrix with dimension {self.N}x{self.N}!')
3636
self._grad = np.zeros((self.N, 1))
3737
self._grad[pos] = 1.
3838
else:

tests/covobs_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import autograd.numpy as np
22
import pyerrors as pe
33
import pytest
4+
from pyerrors.covobs import Covobs
45

56
np.random.seed(0)
67

@@ -108,3 +109,15 @@ def test_covobs_exceptions():
108109
covobs = pe.cov_Obs([1.5, 0.1], [[1., .2,], [.3, .5]] , 'test')
109110
with pytest.raises(Exception):
110111
covobs = pe.cov_Obs([1.5, 0.1], [[8, 4,], [4, -2]] , 'test')
112+
113+
114+
def test_covobs_pos_too_large():
115+
cov = [[1, 0], [0, 1]]
116+
with pytest.raises(ValueError):
117+
Covobs(1.0, cov, 'test', pos=2)
118+
119+
120+
def test_covobs_pos_negative():
121+
cov = [[1, 0], [0, 1]]
122+
with pytest.raises(ValueError):
123+
Covobs(1.0, cov, 'test', pos=-1)

0 commit comments

Comments
 (0)