Skip to content

Commit 1471197

Browse files
committed
Github # 112 replace asserts with exceptions
1 parent e63ebee commit 1471197

13 files changed

+76
-55
lines changed

filterpy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
for more information.
1515
"""
1616

17-
__version__ = "1.2.5"
17+
__version__ = "1.3"

filterpy/changelog.txt

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version 1.2.6
1+
Version 1.3
22
=============
33

44
* #113 added plotting of 3D covariance ellipsoid with plot_3d_covariance
@@ -11,7 +11,6 @@ Version 1.2.6
1111

1212

1313

14-
1514
Version 1.2.5
1615
=============
1716

filterpy/common/discretization.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ def Q_discrete_white_noise(dim, dt=1., var=1., block_size=1):
7373
John Wiley & Sons, 2001. Page 274.
7474
"""
7575

76-
assert dim == 2 or dim == 3 or dim == 4
76+
if not (dim == 2 or dim == 3 or dim == 4):
77+
assert ValueError("dim must be between 2 and 4")
78+
7779
if dim == 2:
7880
Q = [[.25*dt**4, .5*dt**3],
7981
[ .5*dt**3, dt**2]]
@@ -130,7 +132,9 @@ def Q_continuous_white_noise(dim, dt=1., spectral_density=1.,
130132
[0. , 0. , 0. , 0. , 0.005 , 0.1 ]])
131133
"""
132134

133-
assert dim == 2 or dim == 3 or dim == 4
135+
if not (dim == 2 or dim == 3 or dim == 4):
136+
assert ValueError("dim must be between 2 and 4")
137+
134138
if dim == 2:
135139
Q = [[(dt**3)/3., (dt**2)/2.],
136140
[(dt**2)/2., dt]]

filterpy/common/kinematic.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def kinematic_state_transition(order, dt):
3030
step `dt`.
3131
"""
3232

33-
assert order >= 0 and int(order) == order, "order must be an int >= 0"
33+
if not(order >= 0 and int(order) == order):
34+
raise ValueError("order must be an int >= 0")
3435

3536
# hard code common cases for computational efficiency
3637
if order == 0:
@@ -136,9 +137,12 @@ def kinematic_kf(dim, order, dt=1., dim_z=1, order_by_dim=True):
136137
[x y z x' y' z' x'' y'' z'']
137138
"""
138139

139-
assert dim >= 1
140-
assert order >= 0
141-
assert dim_z >= 1
140+
if dim < 1:
141+
raise ValueError("dim must be >= 1")
142+
if order < 0:
143+
raise ValueError("order must be >= 0")
144+
if dim_z < 1:
145+
raise ValueError("dim_z must be >= 1")
142146

143147
dim_x = order + 1
144148

filterpy/gh/gh_filter.py

+5-14
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,8 @@ def __init__(self, x0, dt, order, g, h=None, k=None):
9393
difference between the measurement and the prediction
9494
"""
9595

96-
97-
assert order >= 0
98-
assert order <= 2
96+
if order < 0 or order > 2:
97+
raise ValueError('order must be between 0 and 2')
9998

10099
if np.isscalar(x0):
101100
self.x = np.zeros(order+1)
@@ -241,10 +240,6 @@ def __init__(self, x, dx, dt, g, h):
241240
filter h gain parameter.
242241
"""
243242

244-
assert np.isscalar(dt)
245-
assert np.isscalar(g)
246-
assert np.isscalar(h)
247-
248243
self.x = x
249244
self.dx = dx
250245
self.dt = dt
@@ -498,10 +493,6 @@ def __init__(self, x, dx, ddx, dt, g, h, k):
498493
filter k gain parameter.
499494
"""
500495

501-
assert np.isscalar(dt)
502-
assert np.isscalar(g)
503-
assert np.isscalar(h)
504-
505496
self.x = x
506497
self.dx = dx
507498
self.ddx = ddx
@@ -899,16 +890,16 @@ def critical_damping_parameters(theta, order=2):
899890
July, 1975
900891
901892
"""
902-
assert theta >= 0
903-
assert theta <= 1
893+
if theta < 0 or theta > 1:
894+
raise ValueError('theta must be between 0 and 1')
904895

905896
if order == 2:
906897
return (1. - theta**2, (1. - theta)**2)
907898

908899
if order == 3:
909900
return (1. - theta**3, 1.5*(1.-theta**2)*(1.-theta), .5*(1 - theta)**3)
910901

911-
raise Exception('bad order specified: {}'.format(order))
902+
raise ValueError('bad order specified: {}'.format(order))
912903

913904

914905
def benedict_bornder_constants(g, critical=False):

filterpy/kalman/IMM.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def __init__(self, filters, mu, M):
7070
7171
"""
7272

73-
assert len(filters) > 1
73+
if len(filters) < 1:
74+
raise ValueError('filters must contain at least one filter')
7475

7576
self.filters = filters
7677
self.mu = mu

filterpy/kalman/ensemble_kalman_filter.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,11 @@ def fx(x, dt):
118118
f.update(np.asarray([z]))
119119
"""
120120

121-
assert dim_z > 0
121+
if dim_z <= 0:
122+
raise ValueError('dim_z must be greater than zero')
123+
124+
if N <= 0:
125+
raise ValueError('N must be greater than zero')
122126

123127
self.dim_x = len(x)
124128
self.dim_z = dim_z
@@ -152,9 +156,10 @@ def initialize(self, x, P):
152156
covariance of the state
153157
"""
154158

155-
assert x.ndim == 1
156-
self.sigmas = multivariate_normal(mean=x, cov=P, size=self.N)
159+
if x.ndim != 1:
160+
raise ValueError('x must be a 1D array')
157161

162+
self.sigmas = multivariate_normal(mean=x, cov=P, size=self.N)
158163
self.x = x
159164
self.P = P
160165

filterpy/kalman/information_filter.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ class InformationFilter(object):
101101

102102
def __init__(self, dim_x, dim_z, dim_u=0, compute_log_likelihood=True):
103103

104-
assert dim_x > 0
105-
assert dim_z > 0
106-
assert dim_u >= 0
104+
if dim_z < 1:
105+
raise ValueError('dim_x must be 1 or greater')
106+
if dim_z < 1:
107+
raise ValueError('dim_x must be 1 or greater')
108+
if dim_u < 0:
109+
raise ValueError('dim_x must be 0 or greater')
107110

108111
self.dim_x = dim_x
109112
self.dim_z = dim_z

filterpy/kalman/kalman_filter.py

+15-11
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,12 @@ def __init__(self, dim_x, dim_z, dim_u=0, compute_log_likelihood=True):
207207
off.
208208
"""
209209

210-
assert dim_x > 0
211-
assert dim_z > 0
212-
assert dim_u >= 0
210+
if dim_z < 1:
211+
raise ValueError('dim_x must be 1 or greater')
212+
if dim_z < 1:
213+
raise ValueError('dim_x must be 1 or greater')
214+
if dim_u < 0:
215+
raise ValueError('dim_x must be 0 or greater')
213216

214217
self.dim_x = dim_x
215218
self.dim_z = dim_z
@@ -297,9 +300,6 @@ def update(self, z, R=None, H=None):
297300
# predict new x with residual scaled by the kalman gain
298301
self.x = self.x + dot(self.K, self.y)
299302

300-
if self.x.ndim == 2:
301-
assert self.x.shape[0] == self.dim_x and self.x.shape[1] == 1
302-
303303
# P = (I-KH)P(I-KH)' + KRK'
304304
I_KH = self._I - dot(self.K, H)
305305
self.P = dot(dot(I_KH, self.P), I_KH.T) + dot(dot(self.K, R), self.K.T)
@@ -490,7 +490,6 @@ def test_matrix_dimensions(self, z=None, H=None, R=None, F=None, Q=None):
490490
"Shape of F must be ({},{}), but is {}".format(
491491
self.dim_x, self.dim_x, F.shape)
492492

493-
494493
assert np.ndim(H) == 2, \
495494
"Shape of H must be (dim_z, {}), but is {}".format(
496495
P.shape[0], shape(H))
@@ -816,7 +815,9 @@ def rts_smoother(self, Xs, Ps, Fs=None, Qs=None):
816815
817816
"""
818817

819-
assert len(Xs) == len(Ps)
818+
if len(Xs) != len(Ps):
819+
raise ValueError('length of Xs and Ps must be the same')
820+
820821
n = Xs.shape[0]
821822
dim_x = Xs.shape[1]
822823

@@ -992,8 +993,9 @@ def log_likelihood_of(self, z):
992993

993994
@alpha.setter
994995
def alpha(self, value):
995-
assert np.isscalar(value)
996-
assert value > 0.
996+
997+
if not np.isscalar(value) or value <= 0:
998+
raise ValueError('alpha must be a float greater than 0')
997999

9981000
self._alpha_sq = value**2
9991001

@@ -1457,7 +1459,9 @@ def rts_smoother(Xs, Ps, Fs, Qs):
14571459
(x, P, K, pP) = rts_smoother(mu, cov, kf.F, kf.Q)
14581460
"""
14591461

1460-
assert len(Xs) == len(Ps)
1462+
if len(Xs) != len(Ps):
1463+
raise ValueError('length of Xs and Ps must be the same')
1464+
14611465
n = Xs.shape[0]
14621466
dim_x = Xs.shape[1]
14631467

filterpy/kalman/mmae.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,14 @@ def __init__(self, filters, p, dim_x, H=None):
7070
dim_x : float
7171
number of random variables in the state X
7272
73-
H :
73+
H : Measurement matrix
7474
"""
7575

76-
assert len(filters) == len(p)
77-
assert dim_x > 0
76+
if len(filters) != len(p):
77+
raise ValueError('length of filters and p must be the same')
78+
79+
if dim_x < 1:
80+
raise ValueError('dim_x must be >= 1')
7881

7982
self.filters = filters
8083
self.p = np.asarray(p)

filterpy/kalman/sigma_points.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,9 @@ def sigma_points(self, x, P):
139139
Ordered by Xi_0, Xi_{1..n}, Xi_{n+1..2n}
140140
"""
141141

142-
assert self.n == np.size(x), "expected size {}, but size is {}".format(
143-
self.n, np.size(x))
142+
if self.n != np.size(x):
143+
raise ValueError("expected size(x) {}, but size is {}".format(
144+
self.n, np.size(x)))
144145

145146
n = self.n
146147

@@ -314,7 +315,10 @@ def sigma_points(self, x, P):
314315
315316
"""
316317

317-
assert self.n == np.size(x)
318+
if self.n != np.size(x):
319+
raise ValueError("expected size(x) {}, but size is {}".format(
320+
self.n, np.size(x)))
321+
318322
n = self.n
319323

320324
if np.isscalar(x):
@@ -461,8 +465,9 @@ def sigma_points(self, x, P):
461465
Ordered by Xi_0, Xi_{1..n}
462466
"""
463467

464-
assert self.n == np.size(x), "expected size {}, but size is {}".format(
465-
self.n, np.size(x))
468+
if self.n != np.size(x):
469+
raise ValueError("expected size(x) {}, but size is {}".format(
470+
self.n, np.size(x)))
466471

467472
n = self.n
468473

filterpy/kalman/square_root.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ def __init__(self, dim_x, dim_z, dim_u=0):
9494
Kalman Filtering. Wiley and sons, 2012.
9595
"""
9696

97-
assert dim_x > 0
98-
assert dim_z > 0
99-
assert dim_u >= 0
97+
if dim_z < 1:
98+
raise ValueError('dim_x must be 1 or greater')
99+
if dim_z < 1:
100+
raise ValueError('dim_x must be 1 or greater')
101+
if dim_u < 0:
102+
raise ValueError('dim_x must be 0 or greater')
100103

101104
self.dim_x = dim_x
102105
self.dim_z = dim_z

filterpy/memory/fading_memory.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,8 @@ class FadingMemoryFilter(object):
101101

102102
def __init__(self, x0, dt, order, beta):
103103

104-
105-
assert order >= 0
106-
assert order <= 2
104+
if order < 0 or order > 2:
105+
raise ValueError('order must be between 0 and 2')
107106

108107
if np.isscalar(x0):
109108
self.x = np.zeros(order+1)

0 commit comments

Comments
 (0)