Calling function causes failure, fine when independent

This is the place for queries that don't fit in any of the other categories.

Calling function causes failure, fine when independent

I have a function defined to convert spectrometer pixel numbers to corresponding wavelengths:

Code: Select all
`# Define pixel-to-wavelength conversion function.  def px_to_wl(numPixels,instNum):     # Define coefficients in descending order.      coef = {6:[-5.339937e-3,-1.095702e-1,-2.125161,6.740874e+1,4.124978e+2],             37:[9.082226e-3,-1.188136e-1,-2.582489,7.705425e+1,4.278468e+2]}        # Apply the Pixel Scaler (E1 in Pan Manual)    pixs = [3.46*((n/numPixels) - 0.5) for n in xrange(1,numPixels+1)]    c  = coef[instNum]    wl = [c[0]*r**4 + c[1]*r**3 + c[2]*r**2 + c[3]*r + c[4] for r in pixs]            return np.array(wl)`

I am calling the function via:
Code: Select all
`wl6  = px_to_wl(1024,6)`

However, if I look at wl6 I get:
Code: Select all
`>>> wl6array([ 290.03977671,  290.03977671,  290.03977671, ...,  290.03977671,        290.03977671,  522.13937013])`

As you can see, all values, except the last, are identical. Worse yet, the spectrometer does not see 290.039xxxx.

Here's the problem: If I take the function lines and run them outside the function, i.e.

Code: Select all
`numPixels = 1024instNum   = 6# Define coefficients in descending order.  coef = {6:[-5.339937e-3,-1.095702e-1,-2.125161,6.740874e+1,4.124978e+2],         37:[9.082226e-3,-1.188136e-1,-2.582489,7.705425e+1,4.278468e+2]}# Apply the Pixel Scaler (E1 in Pan Manual)pixs = [3.46*((n/numPixels) - 0.5) for n in xrange(1,numPixels+1)]c  = coef[instNum]wl = [c[0]*r**4 + c[1]*r**3 + c[2]*r**2 + c[3]*r + c[4] for r in pixs]`

everything works as desired. Now,

Code: Select all
`>>> wl6array([ 290.28942047,  290.53902649,  290.78859474, ...,  521.74079343,        521.94011362,  522.13937013])`

I have deleted px_to_wl, deleted wl6, deleted wl, even restarted Python to no avail. Why is the function call failing, but the same lines work outside the function?
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm

Re: Calling function causes failure, fine when independent

I'm guessing you're on Python 2. Put this at the top of your file:
Code: Select all
`from __future__ import division`

The reason that you're getting all the same values, is because, on Python 2, n/numPixels will always be 0 (apart for the last one, where n==numPixels) as it will perform integer division, dropping everything after the decimal point. Importing division from __future__ will give you proper division even when dividing two ints.
setrofim

Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Calling function causes failure, fine when independent

setrofim wrote:I'm guessing you're on Python 2.

D'oh

Correct, I am running 2.7. I can't believe I didn't catch the division rule!
Python: 2.7 via Anaconda
Numpy: 1.7
Pandas: 0.11
OS: Windows 7
IDE: Spyder/IPython

tnknepp

Posts: 153
Joined: Mon Mar 11, 2013 7:41 pm