I had a go at optimizing some code this past week, and ended up learning to use both cython and f2py. f2py is much easier to use. If you want to write a function in fortran and use it in python, all you do is write the code and add specifications using comments in the fortran code. cython is more natural to code. The code style is C/fortran-like: think in terms of loops instead of arrays. The syntax is python-like, which makes coding somewhat clearer and simpler. For my code, I found that cython was ~10% slower than fortran. Check out the plfits in: http://code.google.com/p/agpy/source/browse/#svn/trunk/
DS9 gaussian fitting
One thing DS9 desperately needs is an interactive gaussian fitter. I have NOT implemented one yet, but it is high on my to do list. Has anyone else (googlers?) tried or succeeded in implementing such a thing? Ideally, something with NO dependencies: if I write one, it will require python, numpy, and probably pyds9 - ick. Straight-up TCL would be very much preferable.
Filled step plots in matplotlib
It's not possible to do a simple filled step plot in matplotlib using default commands. Workaround:
def steppify(arr,isX=False,interval=0): """ Converts an array to double-length for step plotting """ if isX and interval==0: interval = abs(arr[1]-arr[0]) / 2.0 newarr = array(zip(arr-interval,arr+interval)).ravel() return newarrplot(xx,yy,linestyle='steps-mid',color='b',linewidth=1.5)fill_between(steppify(xx[x1:x2],isX=True), steppify(yy[x1:x2])*0, steppify(yy[x1:x2]), facecolor='b',alpha=0.2)
IDL-to-Python
astrobetter started up an idl-to-python guide on their wiki.
Installing 64 bit tcl/tk on Mac OS X
Everything is described in this post: http://www.nabble.com/Error-compiling-tk-8.5.7-on-Mac-OS-X-10.5-td23790967.html
But here's a script too: curl -O 'http://osdn.dl.sourceforge.net/sourceforge/tcl/t{cl,k}8.5.7-src.tar.gz'for f in t*8.5.7*.gz; do tar zxf $f; done cd tcl8.5.7/unix/./configure --enable-framework --enable-64-bitcd tk8.5.7/unix/./configure --enable-framework --enable-64-bitmake -j 4 -C tcl8.5.7/unix make -j 4 -C tk8.5.7/unixsudo make install -C tcl8.5.7/unix sudo make install -C tk8.5.7/unix
Concerns:
- might be necessary to do this in the macosx directory for some reason, though Aqua doesn't support 64 bits
- have to recompile python to get _tkinter to work (see a later post)
Listing variables (e.g IDL help) in Python
Again, IDL has the simple 'help' command to tell you all variables in your namespace. Python has the same thing, but the namespace tends to be cluttered with imported functions. The commands who, who_ls, and whos are meant for interactive use. They are a hell of a lot more useful than var, locals, globals, and dir. examples: whos floatwhos ndarraywho modulefloat_vars = %who_ls floatgrep('x',float_vars) I'm afraid I don't know how to make the last two lines into a one-liner, as would be desirable.
Logarithmic Colormap / Other Colormap in Matplotlib
This is kind of a pain to find out: from matplotlib.colors import LogNormim = imshow(.... cmap=... , norm=LogNorm(vmin=clevs[0], vmax=clevs[-1])) `` It also works for contours, and can be particularly useful if you only want to display contours at a few levels, but you want the colormap to start at a different point. e.g.: ``contour(xx,levels=[2,3,4,5,6,7,8,9,10],norm=matplotlib.colors.Normalize(vmin=0,vmax=10)) will start at light blue instead of dark blue in the default colormap
my scipy install...
As far as I was able to reconstruct, my scipy install looked like this when it went well: mkdir scipy-bincp ../scipy-svn/site.cfg .export PATH=/Users/adam/repos/scipy.git/scipy-bin:$PATHln -s /usr/bin/g++-4.0 scipy-bin/g++-4.0ln -s /usr/bin/g++-4.0 scipy-bin/c++export CC=/usr/bin/gcc-4.0 ln -s /usr/bin/gcc-4.0 scipy-bin/ln -s /usr/bin/gcc-4.0 scipy-bin/gccln -s /usr/local/bin/gfortran-4.0 scipy-bin/gfortran-4.0ln -s /usr/local/bin/gfortran-4.0 scipy-bin/gfortranln -s /usr/local/bin/g95 scipy-bin/g95ln -s /usr/local/bin/i686-apple-darwin8-gfortran-4.2 scipy-bin/python2.7 setup.py buildpython2.7 setup.py install However, site.cfg included pointers to AMD and UMFPACK that were installed via the incredibly complicated series of steps listed here: http://blog.hyperjeff.net/?p=160 AG
NaN-friendly convolution
NaN-friendly convolution is important for, e.g., masked data sets in which you want to interpolate across the masked region.
Astropy has gained this functionality with pull request 155: https://github.com/astropy/astropy/pull/155 but this is a "direct" convolution parallel to IDL's 'convol' routine.
My FFT-based version now works in N dimensions and is a little cleaner: http://code.google.com/p/agpy/source/browse/trunk/AG_fft_tools/convolve_nd.py
I'm still working on writing unit tests, and I'm really not sure what the "correct" behavior at the edges is for the different cases... right now, it seems counterintuitive to me, but the code is doing what I expect it to.
Also, Boxcar kernels always result in shifts for me... they're never supposed to. This is a bug.
Currently, other links to these codes: http://stackoverflow.com/questions/1100100/fft-based-2d-convolution-and-correlation-in-python/8454010#8454010
Observing 10/20
I don't have a better place to post this one, so here it is:
My automated fitter (Gaussfitting Cube Collapser) has come a long way. I now adaptively choose to fit 1, 2, or 3 Gaussian components to output to a data cube. The purpose of that code is primarily to find a two-dimensional way to display information about the 3D structure, specifically about the presence/absence of outflows. Outflows will inevitably be confused with multiple velocity components, but they are also likely to be convolved with them.