perwin / pyimfit

Python wrapper for Imfit (https://github.com/perwin/imfit)
GNU General Public License v3.0
33 stars 4 forks source link

Model image pixel offset #4

Closed larrybradley closed 1 year ago

larrybradley commented 1 year ago

Generating a model image results in a 1-pixel offset in both x and y. I assumed that (x, y) pixel coordinates are 0-indexed (i.e., Python convention). Are they actually 1-indexed? I couldn't find anything in documentation about the pixel coordinate convention.

import pyimfit

shape = (101, 101)
x0 = 50
y0 = 50
pa = 90.0
ell = 0.2
n = 4
I_e = 1
r_e = 25 

model = pyimfit.SimpleModelDescription()
model.x0.setValue(x0, [x0 - 5, x0 + 5])
model.y0.setValue(y0, [y0 - 5, y0 + 5])
gal = pyimfit.make_imfit_function('Sersic', label='galaxy')
gal.PA.setValue(pa, [0, 180])
gal.ell.setValue(ell, [0, 1])
gal.n.setValue(n, [0.5, 5])
gal.I_e.setValue(I_e, [0, 10*I_e])
gal.r_e.setValue(r_e, [0, 10*r_e])

model.addFunction(gal)
imfitter = pyimfit.Imfit(model)
data = imfitter.getModelImage(shape=shape)

I expected this model image to be centered at (x, y) = (50, 50) (the input x0, y0), but it's actually centered at (49, 49):

>>> np.unravel_index(np.argmax(data), data.shape)
(49, 49)

plt.imshow(data)
ls = 'dashed'
color = 'w'
alpha = 0.5
plt.axhline(49, ls=ls, color=color, alpha=alpha)
plt.axvline(49, ls=ls, color=color, alpha=alpha)

sersic_offset

perwin commented 1 year ago

Yes, the pixel convention in Imfit is 1-based (like e.g. IRAF and SAOimage DS9); PyImfit inherits this (so that an Imfit-compatible configuration file will produce the same output for both Imfit and PyImfit).

This is explained at several points in the Imfit manual -- but evidently not (enough) in the PyImfit documentation, and it obviously should be, since it conflicts with the Python/NumPy default. It's mentioned at one point in the PyImfit overview page -- in the "Specify the model (and its parameters)" section -- but that's only inside the comments, where it's easy to miss.

I should probably elevate that comment tosomething boldfaced in the main text of that page; any suggestions for other places to mention this? E.g., where did you look to see if it was mentioned?

larrybradley commented 1 year ago

I only looked in the pyimfit documentation. Sorry, I missed it in the code comment. I think it would be good to document this somewhere more prominent. For photutils, I have a page about the pixel coordinate conventions (https://photutils.readthedocs.io/en/latest/pixel_conventions.html).

perwin commented 1 year ago

Oh, absolutely this should be more clearly documented.

Thanks for the link to your coordinate-conventions page; that's an excellent idea!

perwin commented 1 year ago

I've (finally) updated the documentation to include a pixel-coordinate convention file similar to your photutils example, as well as some extra (and hopefully more explicit/obvious) discussion in the overview. (Thanks again for bringing this to my attention!) https://pyimfit.readthedocs.io/en/latest/pixel_conventions.html