-->
Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Thursday, March 22, 2012

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

Thursday, January 12, 2012

LaTeX: VIM + Skim

macvim-skim-install.sh is my install script for using MacVim.app with Skim.app.

The agpy wiki page has instructions that are probably more clear; I don't really like the colorscheme / layout of this blog.

You can use synctex to make an editor and viewer work together, but it is far from easy and far harder than it should be. Forward-search is pretty easy, but the latex-suite \ls only works intermittently and is not easily customizable.

I had to do the following:

For VIM->Skim.app (Skim.app is necessary for any of this to work), add these commands to .vimrc:
" Activate skim
map ,v :w<CR>:silent !/Applications/Skim.app/Contents/SharedSupport/displayline -r <C-r>=line('.')<CR> %<.pdf %<CR><CR>
map ,p :w<CR>:silent !pdflatex -synctex=1 --interaction=nonstopmode %:p <CR>:silent !/Applications/Skim.app/Contents/SharedSupport/displayline -r <C-r>=line('.')<CR> %<.pdf %<CR><CR>
map ,m :w<CR>:silent !make <CR>:silent !/Applications/Skim.app/Contents/SharedSupport/displayline -r <C-r>=line('.')<CR> %<.pdf %<CR><CR>
" Reactivate VIM
map ,r :w<CR>:silent !/Applications/Skim.app/Contents/SharedSupport/displayline -r <C-r>=line('.')<CR> %<.pdf %<CR>:silent !osascript -e "tell application \"MacVim\" to activate" <CR><CR>
map ,t :w<CR>:silent !pdflatex -synctex=1 --interaction=nonstopmode %:p <CR>:silent !/Applications/Skim.app/Contents/SharedSupport/displayline -r <C-r>=line('.')<CR> %<.pdf %<CR>:silent !osascript -e "tell application \"MacVim\" to activate" <CR><CR>


The ,m command will reload the file and put your cursor where the text is. ,t will return VIM to the front afterwards.


Going the other way (reverse-search / inverse-search) was MUCH more challenging. The code that does this is on agpy . Reproduced here for posterity (I hope to update the agpy version to deal with tabs). [A few hours later, I HAVE replaced the code. Below are the old applescript version, then the new, vim-based version



#!/bin/bash

file="$1"
line="$2"

[ "${file:0:1}" == "/" ] || file="${PWD}/$file"

# Use Applescript to activate VIM, find file, and load it
# the 'delay' command is needed to prevent command/control/shift from sticking when this
# is activated (e.g., from Skim, where the command is command-shift-click)
#
# key code 53 is "escape" to escape to command mode in VIM
exec osascript \
-e "delay 0.2" \
-e "tell application \"MacVim\" to activate" \
-e "tell application \"System Events\"" \
-e " tell process \"MacVim\"" \
-e " key code 53 "\
-e " keystroke \":set hidden\" & return " \
-e " keystroke \":if bufexists(bufname('$file'))\" & return " \
-e " keystroke \":exe \\\":buffer \\\" . bufnr(bufname('$file'))\" & return " \
-e " keystroke \":else \" & return " \
-e " keystroke \":echo \\\"Could not load file\\\" \" & return " \
-e " keystroke \":endif\" & return " \
-e " keystroke \":$line\" & return " \
-e " end tell" \
-e "end tell"


New code: download link
#!/bin/bash

# Install directions:
# Put this file somewhere in your path and make it executable
# To set up in Skim, go to Preferences:Sync
# Change Preset: to Custom
# Change Command: to macvim-load-line
# Change Arguments: to "%file" %line

file="$1"
line="$2"
debug="$3"

echo file: $file
echo line: $line
echo debug: $debug

for server in `mvim --serverlist`
do
foundfile=`mvim --servername $server --remote-expr "WhichTab('$file')"`
if [[ $foundfile > 0 ]]
then
mvim --servername $server --remote-expr "foreground()"
if [[ $debug ]] ; then echo mvim --servername $server --remote-send ":exec \"tabnext $foundfile\" "; fi
mvim --servername $server --remote-send ":exec \"tabnext $foundfile\" "
if [[ $debug ]] ; then echo mvim --servername $server --remote-send ":$line "; fi
mvim --servername $server --remote-send ":$line "
fi
done

Save that as an executable in your default path (e.g., /usr/local/bin/macvim-load-line) and open Skim.app, go to Preferences:Sync and make the command look like this:

You need to have mvim on your path. mvim comes with MacVim.app, but is NOT installed by default. Install it by doing something like:

cp /Users/adam/Downloads/MacVim-7_3-53/mvim /usr/local/bin/mvim

You'll also need to install WhichTab.vim in your ~/.vim/plugins/ directory. It's available here ( download link ). Here's the source:

function! WhichTab(filename)
" Try to determine whether file is open in any tab.
" Return number of tab it's open in
let buffername = bufname(a:filename)
if buffername == ""
return 0
endif
let buffernumber = bufnr(buffername)

" tabdo will loop through pages and leave you on the last one;
" this is to make sure we don't leave the current page
let currenttab = tabpagenr()
let tab_arr = []
tabdo let tab_arr += tabpagebuflist()

" return to current page
exec "tabnext ".currenttab

" Start checking tab numbers for matches
let i = 0
for tnum in tab_arr
let i += 1
echo "tnum: ".tnum." buff: ".buffernumber." i: ".i
if tnum == buffernumber
return i
endif
endfor

endfunction

function! WhichWindow(filename)
" Try to determine whether the file is open in any GVIM *window*
let serverlist = split(serverlist(),"\n")

"let currentserver = ????
for server in serverlist
let remotetabnum = remote_expr(server,
\"WhichTab('".a:filename."')")
if remotetabnum != 0
return server
endif
endfor

endfunction

Sunday, January 08, 2012

API documentation on agpy

I finally processed agpy through sphinx and made some nice html documentation.

http://agpy.googlecode.com/svn/trunk/doc/html/agpy.html

Saturday, April 30, 2011

specanpy

AG

There are now 2 clones of PySpecKit:
sPecAnPy
Spectroscopic Toolkit (Astronomy)

Friday, March 18, 2011

pyspeckit: an astronomical spectroscopic toolkit

Jordan and I have been working on our python-based spectroscopic analysis tool for a while now:
pyspeckit is a pretty awesome, now functional but incomplete (and incompletely documented) tool.

Thursday, August 05, 2010

Testing trackbacks?

I just want to see if "trackbacks" work at all. I recently posted about histograms on google spreadsheets .

While I'm at it, might as well throw up a link to my google code site .

Thursday, April 15, 2010

Montage wrapper

(I'm going to try to gradually shift my blogging to this one...)

I wrote a bash wrapper for Tom Robitaille's montage wrapper to allow fits wildcards.


#!/bin/bash

origdir=`pwd`

#echo $# $*

if [ $# -gt 0 ]
then
for ii in $*
do
if [ ${ii%=*} == 'header' ]
then
/usr/local/bin/montage/mGetHdr ${ii#*=} mosaic.hdr
elif [ ${ii%=*} == 'outfile' ]
then
outfile=${ii#*=}
elif [ `echo $ii | grep =` ]
then
params="$params,${ii%=*}='${ii#*=}'"
elif [ `echo $ii | grep ".fits"` ]
then
files=( ${files[@]} $ii )
fi
done
fi
echo ${files[@]} ${#files}
if [ ${#files} -gt 0 ]
then
mkdir tmp
cp ${files[@]} tmp/
cp mosaic.hdr tmp/
cd tmp/
fi

if [ -f mosaic.hdr ]
then
echo "mosaic.hdr exists, continuing"

dir=`pwd`
echo python -c "import montage; montage.wrappers.mosaic('$dir','$dir/mosaic',header='$dir/mosaic.hdr'$params)"
python -c "import montage; montage.wrappers.mosaic('$dir','$dir/mosaic',header='$dir/mosaic.hdr'$params)"

cd $origdir
if [ -d tmp ]
then
if [ $outfile ]
then
mv tmp/mosaic/mosaic.fits $outfile
else
mv tmp/mosaic mosaic
fi
rm -r tmp
fi

else
echo "mosaic.hdr does not exist. Quitting."
cd $origdir
fi