8000 AttributeError: module 'numpy' has no attribute 'getbuffer' · Issue #18581 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

AttributeError: module 'numpy' has no attribute 'getbuffer' #18581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xhding1997 opened this issue Mar 8, 2021 · 3 comments
Closed

AttributeError: module 'numpy' has no attribute 'getbuffer' #18581

xhding1997 opened this issue Mar 8, 2021 · 3 comments
Labels
33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued

Comments

@xhding1997
Copy link

Reproducing code example:

I encountered an error as shown in the title while running this project's demo.bag. There seems to be a problem with NumPy's numpy.getbuffer() function. From what I've looked up, NumPy seems to have deprecated getbuffer() since version 1.13.0 of Python 3, so I'm getting this bug in the default Python 3 environment. But how to fix this bug? I haven't found a good alternative to getbuffer()

This is the code for the error in the color_pcl_generator.py file in the project.

<< def make_ros_cloud(self, stamp):
        # Assign data to ros msg
        # We should send directly in bytes, send in as a list is too slow, numpy tobytes is too slow, takes 0.3s.
        
        self.cloud_ros.data = np.getbuffer(self.ros_data.ravel())[:]
        #self.cloud_ros.data = memoryview(self.ros_data.ravel()) #I try to use memoryview to replace getbuffer but fail.
        
        self.cloud_ros.header.stamp = stamp
        return self.cloud_ros >>

Error message:

When I run the source code, the error is:

[ERROR] [1615203437.940523]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7fe0b83122e0>>
Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 76, in callback
    self.signalMessage(msg)
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 58, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 330, in add
    self.signalMessage(*msgs)
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 58, in signalMessage
    cb(*(msg + args))
  File "/home/bie/ros_ws/Sem_SLAM/src/semantic_slam/semantic_cloud/src/semantic_cloud.py", line 209, in color_depth_callback
    cloud_ros = self.cloud_generator.generate_cloud_semantic_max(color_img, depth_img, semantic_color, pred_confidence, color_img_ros.header.stamp)
  File "/home/bie/ros_ws/Sem_SLAM/src/semantic_slam/semantic_cloud/include/color_pcl_generator/color_pcl_generator.py", line 163, in generate_cloud_semantic_max
    return self.make_ros_cloud(stamp)
  File "/home/bie/ros_ws/Sem_SLAM/src/semantic_slam/semantic_cloud/include/color_pcl_generator/color_pcl_generator.py", line 139, in make_ros_cloud
    self.cloud_ros.data = np.getbuffer(self.ros_data.ravel())[:]
AttributeError: module 'numpy' has no attribute 'getbuffer'

After I use memoryview(self.ros_data.ravel()) , the error is:

[ERROR] [1615203229.414497]: bad callback: <bound method Subscriber.callback of <message_filters.Subscriber object at 0x7f3caf0b0970>>
Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/sensor_msgs/msg/_PointCloud2.py", line 168, in serialize
    buff.write(struct.Struct('<I%ss'%length).pack(length, _x))
struct.error: argument for 's' must be a bytes object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 882, in publish
    self.impl.publish(data)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 1066, in publish
    serialize_message(b, self.seq, message)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/msg.py", line 152, in serialize_message
    msg.serialize(b)
  File "/opt/ros/noetic/lib/python3/dist-packages/sensor_msgs/msg/_PointCloud2.py", line 171, in serialize
    except struct.error as se: self._check_types(struct.error("%s: '%s' when writing '%s'" % (type(se), str(se), str(locals().get('_x', self)))))
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 383, in _check_types
    check_type(n, t, getattr(self, n))
  File "/opt/ros/noetic/lib/python3/dist-packages/genpy/message.py", line 302, in check_type
    raise SerializationError('field %s must be a list or tuple type' % field_name)
genpy.message.SerializationError: field data must be a list or tuple type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 750, in _invoke_callback
    cb(msg)
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 76, in callback
    self.signalMessage(msg)
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 58, in signalMessage
    cb(*(msg + args))
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 330, in add
    self.signalMessage(*msgs)
  File "/opt/ros/noetic/lib/python3/dist-packages/message_filters/__init__.py", line 58, in signalMessage
    cb(*(msg + args))
  File "/home/bie/ros_ws/Sem_SLAM/src/semantic_slam/semantic_cloud/src/semantic_cloud.py", line 225, in color_depth_callback
    self.pcl_pub.publish(cloud_ros)
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/topics.py", line 886, in publish
    raise ROSSerializationException(str(e))
rospy.exceptions.ROSSerializationException: field data must be a list or tuple type 

NumPy/Python version information:

1.17.4 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0]

@eric-wieser
Copy link
Member

getbuffer has never been supported on python 3, because there is no such thing as a buffer object in Python 3. It was removed from numpy entirely in #15239, but only because we stopped supporting Python 2 at all, so that if was never run.

Are you looking for one of bytes, bytearray, or memoryview? (all part of Python itself)

@eric-wieser
Copy link
Member
eric-wieser commented Mar 8, 2021

I imagine that ros wants you to use list(memoryview(self.ros_data.ravel())), although I'd expect that to be rather slow (but probably not any slower than what was there before).

@eric-wieser eric-wieser added 33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued labels Mar 8, 2021
@rgommers
Copy link
Member

No follow-up and the question was answered, so closing this issue. Thanks all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
33 - Question Question about NumPy usage or development 57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

3 participants
0