-
Notifications
You must be signed in to change notification settings - Fork 8
/
sanity.py
86 lines (58 loc) · 2.3 KB
/
sanity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
def assert_numpy(x, name=None):
if not isinstance(x, np.ndarray):
maybe_name = f" {name}" if name is not None else ""
raise ValueError(f"Expected{maybe_name} to be numpy array, but got {type(x)}.")
def assert_K(K):
if K.shape != (3, 3):
raise ValueError(f"K must has shape (3, 3), but got {K} of shape {K.shape}.")
def assert_T(T):
if T.shape != (4, 4):
raise ValueError(f"T must has shape (4, 4), but got {T} of shape {T.shape}.")
is_valid = np.allclose(T[3, :], np.array([0, 0, 0, 1]))
if not is_valid:
raise ValueError(f"T must has [0, 0, 0, 1] the bottom row, but got {T}.")
def assert_pose(pose):
if pose.shape != (4, 4):
raise ValueError(
f"pose must has shape (4, 4), but got {pose} of shape {pose.shape}."
)
is_valid = np.allclose(pose[3, :], np.array([0, 0, 0, 1]))
if not is_valid:
raise ValueError(f"pose must has [0, 0, 0, 1] the bottom row, but got {pose}.")
def assert_shape(x, shape, name=None):
shape_valid = True
if shape_valid and x.ndim != len(shape):
shape_valid = False
if shape_valid:
for i, s in enumerate(shape):
if s is not None:
if x.shape[i] != s:
shape_valid = False
break
if not shape_valid:
name_must = f"{name} must" if name is not None else "Must"
raise ValueError(f"{name_must} has shape {shape}, but got shape {x.shape}.")
def assert_shape_ndim(x, ndim, name=None):
"""
Assert that x is a tensor with nd dimensions.
Args:
x: tensor
nd: number of dimensions
name: name of tensor
"""
if x.ndim != ndim:
name_must = f"{name} must" if name is not None else "Must"
raise ValueError(f"{name_must} have {ndim} dimensions, but got {x.ndim}.")
def assert_shape_nx3(x, name=None):
assert_shape(x, (None, 3), name=name)
def assert_shape_nx2(x, name=None):
assert_shape(x, (None, 2), name=name)
def assert_shape_4x4(x, name=None):
assert_shape(x, (4, 4), name=name)
def assert_shape_3x4(x, name=None):
assert_shape(x, (3, 4), name=name)
def assert_shape_3x3(x, name=None):
assert_shape(x, (3, 3), name=name)
def assert_shape_3(x, name=None):
assert_shape(x, (3,), name=name)