GalSim-developers/GalSim — pull request #133 by rmandelb (2012-05-07 17:00:25+00:00)
Title: #38 - python interface for moments and shape measurement code

I think that the work on branch #38 to create a python interface for the moments and shape measurement code is ready to be merged into master.

As in my other recent pull requests, general comments on how I have done this are quite welcome, given that I'm still getting back into C++ and am quite new to python. What I've done works (at least on my machine :) but if there are places where something could be done more optimally in future, I'm happy to hear about it. Also, I'm open to further work on improving the user interface now or at some later point.

A brief summary of what I did:
* Some updates of the C++ code as raised in issue #120, including explicit ```std``` namespace among other changes.
* I made an ```HSMShapeData``` object in C++, and wrapped it, so that it can carry around information about moments and shape measurement.
* I made some C++ functions that take ```ImageView``` as arguments, and repackage those inputs into a proper format for the original moments functions to process.
* These are wrapped to python, but at the python level I made other functions that can take either an ```Image``` or an ```ImageView```, and then call the wrapped functions appropriately (see galsim/psfcorr.py). The function to compute adaptive moments is also a member function of the ```ImageView``` and ```Image``` classes, so it can be called in several ways (see documentation in galsim/psfcorr.py).
* I removed our C++ example codes MeasMoments.cpp and MeasShape.cpp, and replaced them with similarly-behaving python scripts. (I did not spend much time making these very sophisticated in how they handle arguments, since my assumption is that most of the time, we'll be using the python interface directly without writing files out and then calling MeasMoments.py from the command-line.)
* I removed the cfitsio dependency and some defunct C++ codes that depended on it.
* I updated Demo.py to use this python interface rather than making system calls to MeasMoments and MeasShape.
* Error handling: the python functions in psfcorr.py take an argument called ```strict```, which defaults to ```True```. If ```strict==True```, then if the adaptive moments / shape measurement functions return any error message, the python function raises a runtime exception with that error message. If ```strict==False```, then no exception is raised; however, the error message is saved in the HSMShapeData object that gets returned. I think that some of the time we will want to mass-produce many objects and test their properties, flagging any failures based on the error message but without causing the script to fail. So that's why I made the error handling have this option of strict vs. not. Any thoughts on having ```strict==True``` as the default are welcome.

Questions:
* I made unit tests, but they are not very extensive. Suggestions for more would be welcome. (Or comments on whether you think what I have is already adequate!)
* One possible issue with the error handling is that, since I wanted the functions I wrote in C++ to be usable in C++ as well, any error messages get output to stdout when the C++ function is executed. This means that when calling the code in python with ```strict==True```, the error messages are announced twice (or, with ```strict==False```, they are announced once and also saved in the output data structure). Suggestions on how to handle this more cleanly would be welcome.

Once this is merged in, I will update the wiki page on example scripts to reflect the fact that MeasShape and MeasMoments executables based on C++ are no longer present.