0

I get the following output from the unit test below:

[[array([[-1.57079633]])]]
[[array([[0.+1.57079633j]])]]
<module 'numpy' from '/usr/local/lib/python2.7/dist-packages/numpy/__init__.pyc'>
E
======================================================================
ERROR: test_TestWECTrain_BasicEnv_SetupAndStepping (__main__.Test_exp)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "Test_exp.py", line 34, in test_TestWECTrain_BasicEnv_SetupAndStepping
    expsigmatphase = np.exp(tmp)
AttributeError: exp

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)

Here is the unit test

import unittest
import os
import scipy.io as sio
import numpy as np
from pprint import pprint

class Test_exp (unittest.TestCase):

    def test_exp (self):

        data_file = "test_buoysimoptions.mat"

        buoysimoptions = sio.loadmat (data_file)

        t = 0.0
        phase = buoysimoptions['SeaParameters']['phase']
        sigma = buoysimoptions['SeaParameters']['sigma']

        sigmatminusphase = sigma * t - phase; print (sigmatminusphase)
        tmp = -1.0j * sigmatminusphase; print (tmp)
        print (np)
        tmp = np.asarray(tmp)
        expsigmatphase = np.exp(tmp)


if __name__ == '__main__':
    unittest.main()

The input file (2.9kB) can be downloaded here: https://www.dropbox.com/s/psq1gq8xpjivrim/test_buoysimoptions.mat?dl=0

Why do I get the error AttributeError: exp?

Note this is identical to "AttributeError: exp" while using numpy.exp() on an apparently ordinary array but this question was never answered and provides no minimal example like I do.

This is in Python 2.7, In Python 3.5 I get:

[[array([[-1.57079633]])]]
[[array([[0.+1.57079633j]])]]
E
======================================================================
ERROR: test_exp (__main__.Test_exp)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "Test_exp.py", line 25, in test_exp
    expsigmatphase = np.exp(tmp)
AttributeError: 'numpy.ndarray' object has no attribute 'exp'

----------------------------------------------------------------------
Ran 1 test in 0.002s

FAILED (errors=1)

Edit: some further information on the loaded data

I expected buoysimoptions['SeaParameters']['phase'] to just be a numpy array, but it seems not, see below, which ultimately causes the error

>>> phase = buoysimoptions['SeaParameters']['phase']
>>> phase
array([[array([[1.57079633]])]], dtype=object)
>>> phase = buoysimoptions['SeaParameters']['phase'][0]
>>> phase
array([array([[1.57079633]])], dtype=object)
>>> phase = buoysimoptions['SeaParameters']['phase'][0][0]
>>> phase
array([[1.57079633]])

do I need to index [0][0] always to just get the actual array? What is the right thing to do here? If I use the last one, the exp error goes away.

9
  • What's the output of print(type(tmp), dir(tmp))? The issue seems not to be about np but tmp. Commented Sep 20, 2018 at 9:54
  • Uhm. On the command line I can reproduce it. But I see that tmp.dtype is O... if I try to create an array as a = np.array([[0.+1.57079633j]], dtype=object) then np.exp(tmp) fails with AttributeError, but if I specify dtype=complex then I get the correct result... maybe you only need to specify the dtype somewhere? Re why on the command line works but not from the script: you probably use a different python and hence different library versions. Maybe in some cases it ends up with dtype=O vs dtype=complex and you get that error Commented Sep 20, 2018 at 10:00
  • I think the problem is that the fields of the loaded structure are like the following: array([[array([[1.57079633]])]], dtype=object) Commented Sep 20, 2018 at 10:07
  • @NilsWerner, nope, definitely not, did you see the output of the print (np) statement? Commented Sep 20, 2018 at 10:08
  • This seems more a problem with your file... if somebody sent you a file that was created in a weird way you'll obtain a weird result. Can you change how the file is created? Maybe it's there that you have to fix something. Commented Sep 20, 2018 at 10:15

1 Answer 1

1

It turns out the answer is simple, these loaded variables were themselves oringinally matlab structures, and I was omitting the index when retrieving them, the correct thing to do is the following (note the extra [0,0]s when retrieving phase and sigma):

import unittest
import os
import scipy.io as sio
import numpy as np
from pprint import pprint

class Test_exp (unittest.TestCase):

    def test_exp (self):

        data_file = "test_buoysimoptions.mat"

        buoysimoptions = sio.loadmat (data_file)

        t = 0.0
        phase = buoysimoptions['SeaParameters'][0,0]['phase']
        sigma = buoysimoptions['SeaParameters'][0,0]['sigma']

        sigmatminusphase = sigma * t - phase; print (sigmatminusphase)
        tmp = -1.0j * sigmatminusphase; print (tmp)
        print (np)
        tmp = np.asarray(tmp)
        expsigmatphase = np.exp(tmp)


if __name__ == '__main__':
    unittest.main()
Sign up to request clarification or add additional context in comments.

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.