Hi again, sorry if that wasnt clear. I am using the ipython interpreter to debug scripts. I have a function:-) saved as part of a module called daves_modules.py.

   import numpy as np
   def find_nearest(array,value):

        idx=(np.abs(array-value)).argmin()
        return array[idx], idx


If I run the module from the interpreter as follows,


   In [610]: %run daves_modules.py

   In [611]: a = find_nearest(pos_time, highTime)

   In [612]: a
   Out[612]: (20.009998091697867, 200)


it works fine. However, if I use the same function in a script like this,

   import numpy as np
   import pylab as py
   from daves_modules import dist, find_nearest
   .
   .
   .
   a = find_nearest(pos_time, highTime)


I get the following error,

   %run SNR_UFAN_UCRUI.py
   ---------------------------------------------------------------------------
   IndexError                                Traceback (most recent
   call last)

   /home/davcra/python_scripts/SNR_UFAN_UCRUI.py in <module>()
         88 b = find_nearest(pos_time, lowTime)
         89
   ---> 90 pos_noise =
   (np.sum(pos_signal[0:a[1]])+np.sum(pos_signal[b[1]:-1])) / (a[1] +
   (len(pos_signal)-b[1]))
         91 neg_noise =
   (np.sum(neg_signal[0:-a[1]])+np.sum(neg_signal[-b[1]:-1])) / (a[1] +
   (len(neg_signal)-b[1]))
         92

   IndexError: invalid index to scalar variable.
   WARNING: Failure executing file: <SNR_UFAN_UCRUI.py>


If I then try to use the function in ipython again,

   In [614]: a = find_nearest(pos_time, highTime)

   In [615]: a
   Out[615]: 20.009998091697867


Note: the function originally only returned array[idx], so it seems to have reverted to this somehow.





On 02/24/2012 04:28 PM, Evert Rol wrote:
   Hi David,

Hi,
I am new to python and have made a couple of definitions. I imported them and they worked ok. I 
they worked except for one which gave me the error "NameError: global name 'np' is not 
defined". I then edited my script for the def to include "import numpy as np" saved 
it and imported the def again. However, it still gives me the same error. I know I have to be doing 
something basic wrong but cant figure it out, anyone know what I am doing wrong. The def is below.
thanks
Minor thing first: in Python terminology, most of the time your 'definitions' are simply 
called functions, although you're correct that "def" refers to definition. But 
thatt's more about where the function is defined, in contrast to where in the code it is 
called (or perhaps even declared, though I don't think that applies to Python).


When you say "I imported the def again", it sounds like you're working on the 
Python interpreter, and doing
import myscript
or similar.
If that's how you run things, you would have to use the reload() function to 
reload the new function definition, which has your correction.

However, you also talk about 'my script'. A script is something I would run 
from the shell command line, like
$>  python myscript.py

If you do things that way, you would always be ensured that python uses the 
latest edits in your script.
It does mean that any command you would normally type into the Python 
interpreter, you would now have to enter in the script. And while the 
interpreter always shows the evaluation result of the last command entered, a 
script would require a print for that. Compare:
a=1
a
1

versus (inside a script):
a = 1
a  # this doesn't show anything
print a  # this does

Perhaps this is too basic, but I have to guess a bit what you are doing from 
your text.

A few tips to get more practical help:
- Python normally shows a stack trace when there is an error. It is good to 
copy-paste the whole thing in your emails. Just typing the last bit often 
doesn't help.
- Copy-paste (again, don't type) whatever you're doing in the Python 
interpreter, if that's what you are using. So we can how you do things 
(examples are clearer than descriptions). If needs be, intersperse with 
comments.
Compare eg:
import myscript
NameError: global name 'np' is not defined".
# editing myscript.py
import myscript
NameError: global name 'np' is not defined".

And we can immediately see you don't reload() the script.

Hope this gets you further.

Have fun,

   Evert



D

import numpy as np

def find_nearest(array,value):
    idx=(np.abs(array-value)).argmin()
    return array[idx]
_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to