Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to deal with expected a readable buffer object in Python

Tags:

python

numpy

I am quitenew to python and I am working on a python script I inherited from someone I work with, and I am getting this error:

expected a readable buffer object

The code causing this is:

self.y_NoShock_data = np.zeros((self.a_count+1,1,self.numberOfTags+1,lookback+forward,),dtype=enums.    
self.y_data = np.zeros((self.a_count+1,len(self.SCL)+1,self.numberOfTags+1,lookback+forward,),dtype=enums.DataPoints)

self.y_NoShock_cum_data = np.zeros_like(self.y_NoShock_data)
self.y_cum_data = np.zeros_like(self.y_data)

enums.DataPoints looks like this:

enums.DataPoints = dtype([
        ('Amount','float32'),
        ])

The stack trace is as follows:

Internal Server 

Error: /smCore/entity/1/runScenarioManager/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/views/createScenario.py", line 445, in runScenarioManager
    a = ScenarioExecutionController(sEA)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecutionController.py", line 176, in __init__
    shockEventDataSet=[], lookback=self.lookback, forward=self.forward, period=self.period) #,
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 307, in buildSeedScenarioObject
    cls.updateScenarioParameters(shockContainerList,shockEventDataSet, shockEventDateList)
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 130, in updateScenarioParameters
    self.initialiseResultArrays()
  File "/Users/bentaliadoros/Documents/workspace/LivingSvnAmmar/trunk/ScenarioManagerStandAlone/smCore/models/scenarioExecution.py", line 154, in initialiseResultArrays
    self.y_NoShock_cum_data = np.zeros_like(self.y_NoShock_data,dtype=enums.DataPoints)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/numeric.py", line 116, in zeros_like
    res.fill(0)
TypeError: expected a readable buffer object

He was working on a pc and I am on a Mac. I've been looking around but can't find a solution for this, can anyone point me in the right direction?

like image 473
Ben Taliadoros Avatar asked Nov 12 '13 16:11

Ben Taliadoros


1 Answers

It's hard to respond without knowing what the enums.DataPoints dtype object looks like, but I'll try to explain where you see that error message.

When you try to set (an element of) an array to some value that doesn't align properly with its dtype, you will see this. Here is an example:

In [133]: data = np.zeros((3,2), dtype="int, int")

In [134]: data
Out[134]: 
array([[(0, 0), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

In [135]: data[0, 0]
Out[135]: (0, 0)

In [136]: data[0, 0] = [1,2]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-136-399e78675be4> in <module>()
----> 1 data[0, 0] = [1,2]

TypeError: expected a readable buffer object

This gave an error because it can't handle two values being assigned to one element of your array. Your dtype has two values in it, so it seems reasonable to expect, but the array wants one object of type given by the dtype:

In [137]: data[0,0] = (1,2)

In [138]: data
Out[138]: 
array([[(1, 2), (0, 0)],
       [(0, 0), (0, 0)],
       [(0, 0), (0, 0)]], 
      dtype=[('f0', '<i8'), ('f1', '<i8')])

It's likely that a single set of zeros of the same shape of your array won't align with the dtype.

like image 67
askewchan Avatar answered Oct 29 '22 08:10

askewchan