0

I have a python script NeoprobeApp.py which calls a fitting function in Parameter.py Parameter.py is given below

from scipy import optimize
import numpy as np

class Parameter:
    def __init__(self, value):
            self.value = value

    def set(self, value):
            self.value = value

    def __call__(self):
            return self.value

def fit(function, parameters, y, x = None):
    def f(params):
        i = 0
        for p in parameters:
            p.set(params[i])
            i += 1
        return y - function(x)

    if x is None: x = arange(y.shape[0])
    p = [param() for param in parameters]
    optimize.leastsq(f, p)

I import the fit function successfully from Parameter import fit. However, when I try to initialise my parameters

# Define initial parameters of Gaussian fit
        mu = Parameter(0)
        sigma = Parameter(20)
        height = Parameter(1)

        #define Gaussian fit function
        def f(angles): return height() * exp(-((angles-mu())/sigma())**2)

        fit(f, [mu, sigma, height], n_col_cnts)

I get the error message

Traceback (most recent call last):
  File "NeoprobeApp.py", line 228, in OnPlot
    mu = Parameter(0)
NameError: global name 'Parameter' is not defined

What am I doing wrong?

2
  • I'm not sure why this question deserves a down vote Commented Jul 30, 2013 at 8:53
  • Downvoted because this is entry level stuff that is handled in every python tutorial that deals with the import statement. The error should also be very clear - your script doesn't know anything named Parameter. Any good IDE (for example, vim + pyflakes) would have notified you of this error before running the code. Also, you seem to understand that you need to import your fit function, why do you think the Parameter class is special and doesn't need to be imported? Commented Jul 30, 2013 at 9:12

2 Answers 2

7

You just import the fit function, but not the Parameter class.

Use

from Parameter import fit, Parameter

instead of just

from Parameter import fit
Sign up to request clarification or add additional context in comments.

Comments

2

Both fit and Parameter are just names to denote things in the Parameter module. These names could be bound to anything. In this case, fit is bound to a function and Parameter is bound to a class. The list of names accessible at any point in code is defined by the scope of the names. The import statement brings names from other scopes into your current scope.

BTW: In python, modules should be named lowercase. A "Module" is generally the contents of a python file, so you should really name the file parameter.py and then your import statement would be:

from parameter import fit, Parameter

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.