9

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?

4
  • 1
    What is enums.DataPoints? Commented Nov 12, 2013 at 17:42
  • The code in your traceback does not match the code you posted. In the traceback, dtype is specified (as the type @Jaime has asked about), but not in the code you posted at the top. Be sure you've saved (and reloaded if interactive) your source file if you've made changes. Commented Nov 12, 2013 at 23:11
  • I added enums.DataPoints, thanks Commented Nov 13, 2013 at 9:25
  • this results in this error: _p = np.empty(1, dtype=[('p', np.uint16), ('pr', np.uint16)]) then _p[:]=1 but not this: _p = np.empty(2, dtype=[('p', np.uint16), ('pr', np.uint16)]) then _p[:]=1, looks like a bug to me (1.8.2) Commented Jul 16, 2015 at 0:01

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

The code runs fine on the machines (Windows 7) of my colleagues. From askewchan's answer I'm not clear why the issue is arising especially since we're using numpy.zeros_like() -- From the documentation, this should basically replicate the prior array?
Yeah, it runs fine on my computer (python 2.7, numpy 1.7) too, now that I've set DataPoints as you added. What version of numpy/python are you running?
I am using numpy version 1.6.1 and python 2.7.2
Thanks for this help, I found out that my numpy was out of date, I updated to 1.8.0 and the error is now gone. Thanks
Great, @Ben. If that answers your question, please either accept mine or add your own and accept it, to mark the question as answered. Thanks!
1

The answer to this was that I was not working under the same version of Numpy that my colleagues were using, as pointed out by @askewchan in the comments to his answer.

  • Failed: numpy 1.6.1
  • Works: numpy 1.7
  • Works: numpy 1.8.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.