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.
print(type(tmp), dir(tmp))? The issue seems not to be aboutnpbuttmp.tmp.dtypeisO... if I try to create an array asa = np.array([[0.+1.57079633j]], dtype=object)thennp.exp(tmp)fails withAttributeError, but if I specifydtype=complexthen I get the correct result... maybe you only need to specify thedtypesomewhere? 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 withdtype=Ovsdtype=complexand you get that errorarray([[array([[1.57079633]])]], dtype=object)