crobar
crobar

Reputation: 2929

AttributeError: exp when using numpy on data loaded using scipy.io.loadmat

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.

Upvotes: 0

Views: 58

Answers (1)

crobar
crobar

Reputation: 2929

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()

Upvotes: 1

Related Questions