7

I am trying to write a feature extractor by gathering essentia's (a MIR library) functions. The flow chart is like: individual feature extraction, pool, PoolAggregator, concatenate to form the whole feature list from poolAggregator using np.concatenate

The script runs well under ipython notebook even without importing numpy. I'm just congregating the array or float number I got from the previous stage but the error message: "NameError: global name 'numpy' is not defined" shows.

I've tried to put "import numpy as np" at the top of the module:

import numpy as np
def featureExtractor(path):

or in the function:

def featureExtractor(path):
    import numpy as np

or outside the module in the main file:

import numpy as np
from featureExtractor import featureExtractor

None of these can solve it, please help me.

The following is the script:

from essentia.standard import *
import essentia

def featureExtractor(path):
    loader = MonoLoader(filename = path)
    x = loader()

    pool = essentia.Pool()
    spectrum = Spectrum()
    w = Windowing(type = 'hann')
    # Create needed objects
    mfcc = MFCC()
    centroid = Centroid()

    for frame in FrameGenerator(x, frameSize = 1024, hopSize = 512):    

        mfcc_bands, mfcc_coeffs = mfcc(spectrum(w(frame))) # output: vector_real
        spec_centroid = centroid(spectrum(w(frame))) # output: real

        pool.add('lowlevel.mfcc', mfcc_coeffs)
        pool.add('lowlevel.centroid', spec_centroid)

    aggrPool = PoolAggregator(defaultStats = [ 'mean', 'var' ])(pool) 
    # calculate mean and var for each feature

    # build a feature vector of type array
    list = ['lowlevel.centroid.mean', 'lowlevel.centroid.var',
            'lowlevel.mfcc.mean', 'lowlevel.mfcc.var']

    feature_vec = []

    for name in list:
        feature = aggrPool[name]
        if type(feature) != float:  # for those type == array
           feature_vec = np.concatenate([feature_vec,feature], axis = 0)
        else: # for those type == float
           feature_vec.append(feature)
    return feature_vec

Then I command in the main file:

path = "/~/Downloads/~.wav"
from featureExtractor import featureExtractor
featureExtractor(path)

I got the error:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-109-40b5bbac9b17> in <module>()
      1 from featureExtractor import featureExtractor
      2 
----> 3 featureExtractor(path)

/~/ipython_notebook/featureExtractor.py in featureExtractor(path)
     66         for name in list:
     67                 feature = aggrPool[name]
---> 68         if type(feature) != float:  # for those type == array
     69                 feature_vec = np.concatenate([feature_vec,feature], axis = 0)
     70         else: # for those type == float

NameError: global name 'numpy' is not defined

And I got the same error no matter where I put the command (as described in above)

import numpy as np
Francis
  • 6,416
  • 5
  • 24
  • 32
  • 5
    Do you have numpy installed? – EdChum Aug 11 '14 at 08:39
  • 5
    [Strip your file down to the minimum that runs, includes the `import numpy as np` line, and produces the error when you run it](http://stackoverflow.com/help/mcve), then post what remains. Also, post the complete stack trace your stripped-down code produces. Otherwise, we'll have to look at bits and pieces of your situation and guess at how those pieces fit together. – user2357112 Aug 11 '14 at 08:46
  • @EdChum I run the script directly in ipython notebook and it's fine. The error emerges when I tries to use it as function. – Francis Aug 11 '14 at 09:20
  • 1
    That could mean your ipython environment has numpy installed but your command line environment does not, if you tried to run a script which just had `import numpy` does this even run? You'll need to install numpy into your python environment – EdChum Aug 11 '14 at 09:22
  • I just tried to run `import numpy` directly under python prompt and there wasn't any error. – Francis Aug 11 '14 at 09:31
  • Do you have ipython notebook in a different virtual environment? –  Aug 11 '14 at 09:50
  • I also got the error when calling the function in python prompt. But I can import numpy directly, that's weird. – Francis Aug 11 '14 at 10:55
  • The stack trace refers to file `featureExtract.py`, but it appears you are editing file `featureExtractor.py`. – pv. Aug 11 '14 at 11:17
  • not sure if it is a typo, but I run again and confirm the error. It is `featureExtractor.py`. – Francis Aug 11 '14 at 11:34
  • Then it seems you may be adding the `import numpy as np` lines to the wrong file. – pv. Aug 11 '14 at 12:01
  • I am sure that I added the line in the correct file `featureExtractor.py` – Francis Aug 11 '14 at 12:04
  • That stack trace makes no sense. The line it points to doesn't use the name `numpy` at all. – user2357112 Aug 11 '14 at 22:34

1 Answers1

16

Try simply

import numpy

at the top of the file /~/ipython_notebook/featureExtractor.py

It seems that your code expect numpy and not np as the module name.

Pierre de Buyl
  • 7,074
  • 2
  • 16
  • 22
  • If you did not have numpy, you would get a ImportError instead. – Pierre de Buyl Aug 11 '14 at 12:25
  • 1
    The fact that you can make it work with IPython and not the regular Python interpreter is weird. Do you have a custom installation? – Pierre de Buyl Aug 11 '14 at 12:30
  • 2
    Also, did you reset IPython's kernel? Else, you may have "leftover" imports. – Pierre de Buyl Aug 11 '14 at 12:31
  • 3
    Restarting the kernel works! I thought the only thing making difference is the script in module `featureExtractor.py`. I didn't expect restarting kernel to fix the problem! Thank you very much. So I have `import numpy as np` at the top of my module, and it works after restarting the kernel. I thought the only thing that restart does is like `clear all` in Matlab. – Francis Aug 11 '14 at 12:50
  • 1
    Python (and IPython) do not reload modules without a restart (there are [ways](http://ipython.org/ipython-doc/stable/api/generated/IPython.lib.deepreload.html#IPython.lib.deepreload.reload) around it, but it is not practical. – Pierre de Buyl Aug 11 '14 at 12:59
  • I thought that re-command the cell could reload the module. I got it, thank you. – Francis Aug 11 '14 at 13:14