Help with gaussian2d and data formats?

17 views
Skip to first unread message

Tom Caradoc-Davies

unread,
Jun 25, 2024, 6:53:10 AM (5 days ago) Jun 25
to lmfit-py
Hi,

I have detector data that I have as a 2D numpy array (shape of 4362x4148 and typeof uInt32 intensity values).


What I am confused by is that in the example the x, y and z data are all 1D arrays with the same length? In my case I have x of 4362, y of 4148 but I have 18093576 values for z (one intensity value for each pixel). It seems all 3 need to be the same length for gaussian2d to run but I don't understand how to correctly represent my data in this format.

I want to model the 2D gaussian and then calculate the difference between the value of each pixel and the calculated value. I have 96 frames and want to generate a list of pixels that are consistently producing values that are different to the calculated value.

Any help would be appreciated!

Cheers,

Tomdata.png

Matthew Newville

unread,
Jun 25, 2024, 11:53:42 AM (5 days ago) Jun 25
to lmfi...@googlegroups.com

Hi Tom,

 

Yeah, I know that example is somewhat confusing, and we should probably add another.  Or maybe `gaussian2d` needs better documentation:  it assumes that x and y will hold the x and y values for every pixel in the image.  This then handles the case that it is not a regularly gridded image, or you have already transformed the pixels to some other set of values like diffraction q_x, q_y values or something like that where the pixel indices are not how you want to model the data.

 

For regularly gridded data (as straight from a camera), using `np.meshgrid` is almost certainly what you want, using something like:

 

import numpy as np

from lmfit.lineshapes import gaussian2d

import matplotlib.pyplot as plt

 

nx = 200

ny = 250

 

x, y = np.meshgrid(np.arange(nx), np.arange(ny))

 

z = gaussian2d(y, x, amplitude=30, centerx=88, centery=121, sigmax=9.5, sigmay=12.0)

 

print(x.shape, y.shape, z.shape)

plt.imshow(z)

plt.show()

 

 

Hope that gets you going down the right path….

 

--Matt

Tom

 

--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/a69b6cc8-e9b0-4aab-8c85-301f6b7578a9n%40googlegroups.com.

Laurence Lurio

unread,
Jun 27, 2024, 7:35:46 AM (3 days ago) Jun 27
to lmfi...@googlegroups.com
I think you want to replace your 1d x and y arrays with meshgrids.  These would be square arrays, the x-array would have identical x-values for all y-coordinates, and the y array would have identical y values for all x-coordinates.  Then you can write your gaussian as exp(-((x-x0)**2 +(y-y0)**2) /sigma**2).  This is a pretty common was to do things, so there are already numpy functions to create the arrays.  Look here for more information https://numpy.org/doc/stable/reference/generated/numpy.meshgrid.html 

Reply all
Reply to author
Forward
0 new messages