[Tutor] Request for advice on Python code

Martin A. Brown martin at linux-ip.net
Tue Feb 14 03:08:50 CET 2012

Greetings Elaina,

I will echo Alan's remarks--it sounds like you are using a quite
specialized (specialised?) module.  It is certainly not a module 
with which I am familiar, not in the standard library, and not a 
commonly encountered problem.  I would classify this module as 

It is for this reason, that Alan (and possibly others on this list) 
would recommend finding a domain-specific mailing list on which to 
ask the question--IFF that question is really domain specific.

If the question is more general (and part of your question is 
general), then this is an appropriate forum.  With that, said, 
though, you have asked the question quite reasonably, and I have one 
other observation to make, based on the traceback and comments you 
have provided.

 : I'm pretty sure that the problem is that the module expects a 
 : scalar and I want it to read and return a vector from a table.  
 : This is a general-ish problem since I haven't had any real 
 : experience operating vectors, although the code I'm working on 
 : now should give me that.  I had a moment of puzzelment earlier 
 : when I realized that .math doesn't operate on vectors either so 
 : you have to do np.sin() instead of math.sin(). 

I note that you say 'np' here.  I will make an educated guess that 
this means numpy.  There is a package, numpy [0], which operates on 
arrays/vectors rather than the Python standard library math, which 
operates on individual values.

So, yes, you can use the standard library for calculating a sine:

  >>> import math
  >>> math.sin(0)

But, the numpy library lets you operate on an array.

  >>> import numpy
  >>> l = [ x/1000.0 for x in range(1,1001) ]
  >>> len(numpy.sin(l))

It is up to you whether you treat the data as an array or a vector.

Now...going back to your original posting, I see:

 : dustmap='SFD_dust_4096_ngp.fits'
 : EB_V=astropysics.obstools.get_SFD_dust(l,b,dustmap,interpolate=True)
 : -------
 : this gives the error 'Only length-1 arrays can be converted to 
 : Python scalars' ... however, I cannot do:

First, I have no idea what a dustmap is.  Second thing, it seems 
that the error you are seeing is 'Only length-1 arrays [...]', which 
suggests that you are treating an array (a numpy thing) as a scalar 
(a native Python data type) without knowing how you wish to 
summarize your data into a single value.

Who is responsible for the namespace 'astrophysics' here?  The 
documentation there may be able to help you.....maybe.

 : I am thinking/hoping this might be a similarly easy problem....  
 : Anyone performing a module operation on a column from a file 
 : would encounter a similar message (I have in other instances as 
 : well, some tasks don't seem to care, other's do...).  If I do 
 : figure it out and it's a general thing shall I post the solution 
 : here? The entire error is: Traceback (most recent call last):

It is good form to post the traceback, so thank you for posting 
that.  I snipped your traceback, because the error message suggested 
to me exactly what you observed--that some part of that operates 
only on values, not arrays/vectors.

Good luck sorting this out!


 [0] http://numpy.scipy.org/

More information about the Tutor mailing list