Skip to content

Commit c2ba78d

Browse files
committed
converted tabs to spaces, truncated lines to <= 80 chars
1 parent 1eb68d2 commit c2ba78d

File tree

4 files changed

+135
-130
lines changed

4 files changed

+135
-130
lines changed

ros_numpy/image.py

Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -7,114 +7,114 @@
77
from numpy.lib.stride_tricks import as_strided
88

99
name_to_dtypes = {
10-
"rgb8": (np.uint8, 3),
11-
"rgba8": (np.uint8, 4),
12-
"rgb16": (np.uint16, 3),
13-
"rgba16": (np.uint16, 4),
14-
"bgr8": (np.uint8, 3),
15-
"bgra8": (np.uint8, 4),
16-
"bgr16": (np.uint16, 3),
17-
"bgra16": (np.uint16, 4),
18-
"mono8": (np.uint8, 1),
19-
"mono16": (np.uint16, 1),
10+
"rgb8": (np.uint8, 3),
11+
"rgba8": (np.uint8, 4),
12+
"rgb16": (np.uint16, 3),
13+
"rgba16": (np.uint16, 4),
14+
"bgr8": (np.uint8, 3),
15+
"bgra8": (np.uint8, 4),
16+
"bgr16": (np.uint16, 3),
17+
"bgra16": (np.uint16, 4),
18+
"mono8": (np.uint8, 1),
19+
"mono16": (np.uint16, 1),
2020

2121
# for bayer image (based on cv_bridge.cpp)
22-
"bayer_rggb8": (np.uint8, 1),
23-
"bayer_bggr8": (np.uint8, 1),
24-
"bayer_gbrg8": (np.uint8, 1),
25-
"bayer_grbg8": (np.uint8, 1),
26-
"bayer_rggb16": (np.uint16, 1),
27-
"bayer_bggr16": (np.uint16, 1),
28-
"bayer_gbrg16": (np.uint16, 1),
29-
"bayer_grbg16": (np.uint16, 1),
22+
"bayer_rggb8": (np.uint8, 1),
23+
"bayer_bggr8": (np.uint8, 1),
24+
"bayer_gbrg8": (np.uint8, 1),
25+
"bayer_grbg8": (np.uint8, 1),
26+
"bayer_rggb16": (np.uint16, 1),
27+
"bayer_bggr16": (np.uint16, 1),
28+
"bayer_gbrg16": (np.uint16, 1),
29+
"bayer_grbg16": (np.uint16, 1),
3030

3131
# OpenCV CvMat types
32-
"8UC1": (np.uint8, 1),
33-
"8UC2": (np.uint8, 2),
34-
"8UC3": (np.uint8, 3),
35-
"8UC4": (np.uint8, 4),
36-
"8SC1": (np.int8, 1),
37-
"8SC2": (np.int8, 2),
38-
"8SC3": (np.int8, 3),
39-
"8SC4": (np.int8, 4),
40-
"16UC1": (np.uint16, 1),
41-
"16UC2": (np.uint16, 2),
42-
"16UC3": (np.uint16, 3),
43-
"16UC4": (np.uint16, 4),
44-
"16SC1": (np.int16, 1),
45-
"16SC2": (np.int16, 2),
46-
"16SC3": (np.int16, 3),
47-
"16SC4": (np.int16, 4),
48-
"32SC1": (np.int32, 1),
49-
"32SC2": (np.int32, 2),
50-
"32SC3": (np.int32, 3),
51-
"32SC4": (np.int32, 4),
52-
"32FC1": (np.float32, 1),
53-
"32FC2": (np.float32, 2),
54-
"32FC3": (np.float32, 3),
55-
"32FC4": (np.float32, 4),
56-
"64FC1": (np.float64, 1),
57-
"64FC2": (np.float64, 2),
58-
"64FC3": (np.float64, 3),
59-
"64FC4": (np.float64, 4)
32+
"8UC1": (np.uint8, 1),
33+
"8UC2": (np.uint8, 2),
34+
"8UC3": (np.uint8, 3),
35+
"8UC4": (np.uint8, 4),
36+
"8SC1": (np.int8, 1),
37+
"8SC2": (np.int8, 2),
38+
"8SC3": (np.int8, 3),
39+
"8SC4": (np.int8, 4),
40+
"16UC1": (np.uint16, 1),
41+
"16UC2": (np.uint16, 2),
42+
"16UC3": (np.uint16, 3),
43+
"16UC4": (np.uint16, 4),
44+
"16SC1": (np.int16, 1),
45+
"16SC2": (np.int16, 2),
46+
"16SC3": (np.int16, 3),
47+
"16SC4": (np.int16, 4),
48+
"32SC1": (np.int32, 1),
49+
"32SC2": (np.int32, 2),
50+
"32SC3": (np.int32, 3),
51+
"32SC4": (np.int32, 4),
52+
"32FC1": (np.float32, 1),
53+
"32FC2": (np.float32, 2),
54+
"32FC3": (np.float32, 3),
55+
"32FC4": (np.float32, 4),
56+
"64FC1": (np.float64, 1),
57+
"64FC2": (np.float64, 2),
58+
"64FC3": (np.float64, 3),
59+
"64FC4": (np.float64, 4)
6060
}
6161

6262
@converts_to_numpy(Image)
6363
def image_to_numpy(msg):
64-
if not msg.encoding in name_to_dtypes:
65-
raise TypeError('Unrecognized encoding {}'.format(msg.encoding))
64+
if not msg.encoding in name_to_dtypes:
65+
raise TypeError('Unrecognized encoding {}'.format(msg.encoding))
6666

67-
dtype_class, channels = name_to_dtypes[msg.encoding]
68-
dtype = np.dtype(dtype_class)
69-
dtype = dtype.newbyteorder('>' if msg.is_bigendian else '<')
70-
shape = (msg.height, msg.width, channels)
67+
dtype_class, channels = name_to_dtypes[msg.encoding]
68+
dtype = np.dtype(dtype_class)
69+
dtype = dtype.newbyteorder('>' if msg.is_bigendian else '<')
70+
shape = (msg.height, msg.width, channels)
7171

72-
data = np.frombuffer(msg.data, dtype=dtype).reshape(shape)
73-
data.strides = (
74-
msg.step,
75-
dtype.itemsize * channels,
76-
dtype.itemsize
77-
)
72+
data = np.frombuffer(msg.data, dtype=dtype).reshape(shape)
73+
data.strides = (
74+
msg.step,
75+
dtype.itemsize * channels,
76+
dtype.itemsize
77+
)
7878

79-
if channels == 1:
80-
data = data[...,0]
81-
return data
79+
if channels == 1:
80+
data = data[...,0]
81+
return data
8282

8383

8484
@converts_from_numpy(Image)
8585
def numpy_to_image(arr, encoding):
86-
if not encoding in name_to_dtypes:
87-
raise TypeError('Unrecognized encoding {}'.format(encoding))
88-
89-
im = Image(encoding=encoding)
90-
91-
# extract width, height, and channels
92-
dtype_class, exp_channels = name_to_dtypes[encoding]
93-
dtype = np.dtype(dtype_class)
94-
if len(arr.shape) == 2:
95-
im.height, im.width, channels = arr.shape + (1,)
96-
elif len(arr.shape) == 3:
97-
im.height, im.width, channels = arr.shape
98-
else:
99-
raise TypeError("Array must be two or three dimensional")
100-
101-
# check type and channels
102-
if exp_channels != channels:
103-
raise TypeError("Array has {} channels, {} requires {}".format(
104-
channels, encoding, exp_channels
105-
))
106-
if dtype_class != arr.dtype.type:
107-
raise TypeError("Array is {}, {} requires {}".format(
108-
arr.dtype.type, encoding, dtype_class
109-
))
110-
111-
# make the array contiguous in memory, as mostly required by the format
112-
contig = np.ascontiguousarray(arr)
113-
im.data = contig.tostring()
114-
im.step = contig.strides[0]
115-
im.is_bigendian = (
116-
arr.dtype.byteorder == '>' or
117-
arr.dtype.byteorder == '=' and sys.byteorder == 'big'
118-
)
119-
120-
return im
86+
if not encoding in name_to_dtypes:
87+
raise TypeError('Unrecognized encoding {}'.format(encoding))
88+
89+
im = Image(encoding=encoding)
90+
91+
# extract width, height, and channels
92+
dtype_class, exp_channels = name_to_dtypes[encoding]
93+
dtype = np.dtype(dtype_class)
94+
if len(arr.shape) == 2:
95+
im.height, im.width, channels = arr.shape + (1,)
96+
elif len(arr.shape) == 3:
97+
im.height, im.width, channels = arr.shape
98+
else:
99+
raise TypeError("Array must be two or three dimensional")
100+
101+
# check type and channels
102+
if exp_channels != channels:
103+
raise TypeError("Array has {} channels, {} requires {}".format(
104+
channels, encoding, exp_channels
105+
))
106+
if dtype_class != arr.dtype.type:
107+
raise TypeError("Array is {}, {} requires {}".format(
108+
arr.dtype.type, encoding, dtype_class
109+
))
110+
111+
# make the array contiguous in memory, as mostly required by the format
112+
contig = np.ascontiguousarray(arr)
113+
im.data = contig.tostring()
114+
im.step = contig.strides[0]
115+
im.is_bigendian = (
116+
arr.dtype.byteorder == '>' or
117+
arr.dtype.byteorder == '=' and sys.byteorder == 'big'
118+
)
119+
120+
return im

ros_numpy/registry.py

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,44 @@
55
_from_numpy = {}
66

77
def converts_to_numpy(msgtype, plural=False):
8-
def decorator(f):
9-
_to_numpy[msgtype, plural] = f
10-
#_to_numpy[numpy_msg(msgtype), plural] = f
11-
return f
12-
return decorator
8+
def decorator(f):
9+
_to_numpy[msgtype, plural] = f
10+
return f
11+
return decorator
1312

1413
def converts_from_numpy(msgtype, plural=False):
15-
def decorator(f):
16-
_from_numpy[msgtype, plural] = f
17-
#_from_numpy[numpy_msg(msgtype), plural] = f
18-
return f
19-
return decorator
14+
def decorator(f):
15+
_from_numpy[msgtype, plural] = f
16+
return f
17+
return decorator
2018

2119
def numpify(msg, *args, **kwargs):
22-
if msg is None:
23-
return
20+
if msg is None:
21+
return
2422

25-
conv = _to_numpy.get((msg.__class__, False))
26-
if not conv and isinstance(msg, collections.Sequence):
27-
if not msg:
28-
raise ValueError("Cannot determine the type of an empty Collection")
29-
conv = _to_numpy.get((msg[0].__class__, True))
23+
conv = _to_numpy.get((msg.__class__, False))
24+
if not conv and isinstance(msg, collections.Sequence):
25+
if not msg:
26+
raise ValueError("Cannot determine the type of an empty Collection")
27+
conv = _to_numpy.get((msg[0].__class__, True))
3028

3129

32-
if not conv:
33-
raise ValueError("Unable to convert message {} - only supports {}".format(
34-
msg.__class__.__name__,
35-
', '.join(cls.__name__ + ("[]" if pl else '') for cls, pl in _to_numpy.keys())
36-
))
30+
if not conv:
31+
raise ValueError(
32+
"Unable to convert message {} - only supports {}".format(
33+
msg.__class__.__name__,
34+
', '.join(cls.__name__ + ("[]" if pl else '')
35+
for cls, pl in _to_numpy.keys())
36+
))
3737

38-
return conv(msg, *args, **kwargs)
38+
return conv(msg, *args, **kwargs)
3939

4040
def msgify(msg_type, numpy_obj, *args, **kwargs):
41-
conv = _from_numpy.get((msg_type, kwargs.pop('plural', False)))
42-
if not conv:
43-
raise ValueError("Unable to build message {} - only supports {}".format(
44-
msg_type.__name__,
45-
', '.join(cls.__name__ + ("[]" if pl else '') for cls, pl in _to_numpy.keys())
46-
))
47-
return conv(numpy_obj, *args, **kwargs)
41+
conv = _from_numpy.get((msg_type, kwargs.pop('plural', False)))
42+
if not conv:
43+
raise ValueError("Unable to build message {} - only supports {}".format(
44+
msg_type.__name__,
45+
', '.join(cls.__name__ + ("[]" if pl else '')
46+
for cls, pl in _to_numpy.keys())
47+
))
48+
return conv(numpy_obj, *args, **kwargs)

test/test_geometry.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ def test_transform(self):
4545

4646
t_mat = ros_numpy.numpify(t)
4747

48-
np.testing.assert_allclose(t_mat.dot([0, 0, 1, 1]), [1.0, 2.0, 2.0, 1.0])
48+
np.testing.assert_allclose(
49+
t_mat.dot([0, 0, 1, 1]), [1.0, 2.0, 2.0, 1.0])
4950

5051
msg = ros_numpy.msgify(Transform, t_mat)
5152

@@ -68,7 +69,8 @@ def test_pose(self):
6869

6970
t_mat = ros_numpy.numpify(t)
7071

71-
np.testing.assert_allclose(t_mat.dot([0, 0, 1, 1]), [1.0, 2.0, 2.0, 1.0])
72+
np.testing.assert_allclose(
73+
t_mat.dot([0, 0, 1, 1]), [1.0, 2.0, 2.0, 1.0])
7274

7375
msg = ros_numpy.msgify(Pose, t_mat)
7476

test/test_images.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ def test_roundtrip_little_endian(self):
3737

3838
def test_bad_encodings(self):
3939
mono_arr = np.random.randint(0, 256, size=(240, 360)).astype(np.uint8)
40-
mono_arrf = np.random.randint(0, 256, size=(240, 360)).astype(np.float32)
40+
mono_arrf = \
41+
np.random.randint(0, 256, size=(240, 360)).astype(np.float32)
4142
rgb_arr = np.random.randint(0, 256, size=(240, 360, 3)).astype(np.uint8)
42-
rgb_arrf = np.random.randint(0, 256, size=(240, 360, 3)).astype(np.float32)
43+
rgb_arrf = \
44+
np.random.randint(0, 256, size=(240, 360, 3)).astype(np.float32)
4345

4446
with self.assertRaises(TypeError):
4547
msg = ros_numpy.msgify(Image, rgb_arr, encoding='mono8')

0 commit comments

Comments
 (0)