# [AstroPy] Python spectral fitting package - ala Sherpa

Andrew Ptak ptak at pha.jhu.edu
Wed Feb 4 10:00:24 CST 2004

```Hello,

Sorry if this is getting a bit off-topic, but concerning these two, first
the latest verison of XSPEC is object-oriented (written in C++) but hasn't
been released yet.  This opens the possibility a little more of wrapping
higher-level objects in python.  Second, I have a program that does 2-d
fitting on images using Numeric (see http://xassist.pha.jhu.edu/ximgfit).
It works fine for my purposes however I suspect it is a bit inefficient at
the moment due to the creation of temporaries.  For example, since
everything is in python the models are simply .py files (this of course
was one of the design goals, to allow models to be added without
recompiling).  Here is the code for computing a 2-d Gaussian model:
def Eval(self, x, y):
xc = self.params[0].value
yc = self.params[1].value
norm = self.params[2].value
sigmax = self.params[3].value
sigmay = self.params[4].value
theta = self.params[5].value
dx = x-xc
dy = y-yc
rx = dx*cost + dy*sint
ry = -dx*sint + dy*cost
z2 = rx*rx/(sigmax*sigmax)+ry*ry/(sigmay*sigmay)
# Avoid overflow errors
z2 = self.min(z2, 100.)
result = norm*exp(-z2/2)
return(result)

In this design, x and y are 2-D arrays.  If I am not mistaken, in the line
with z2 = rx* ... 7 temporaries are created.  From what I have heard
weave.blitz() may be a solution but I haven't had a chance to play with
that.  But my main point is that in general it may be tricky to be as
efficient as the case where most of the computation is done at the C/C++
level since the programmer will have some insight, and with scripting
languages only being used for the UI.

Andy Ptak

On Wed, 4 Feb 2004, Paul Barrett wrote:

>
> 3. they only handle 1 dimensional response matrices (see e.g. FUSE,
>     which could use a 2D response matrix), and
>
> 4. they are primarily implemented using compiled languages, so
>     development is slow.  (Sherpa's implementation is simply
>     bass-ackwards, i.e. imbeding an interpreted language in a compiled
>     language.  In addition, Numeric/numarray syntax is more powerful than
>     SLANG and hence, numerically more efficient.)
>

_____________________________________________________
AstroPy mailing list  -  astropy at stsci.edu
http://lheawww.gsfc.nasa.gov/~bridgman/AstroPy/
```