human
I am working on the file "Sessions/Session11/Day2/MeasuringCentroidsAndProperMotion.ipynb".
The first task is:
(b) Write a function aperture_centroid(img,R) that will return the values of $(y0,x0)$ that satisfy 
$$ M_x = \sum_{xy} I_{xy} W(x-x0,y-y0) (x-x0)=0$$
(and the same for y0), where $W$ is a weight function. I provide for you below a class GaussAp which you can use to create a Gaussian-aperture weight function with some $\sigma_w$:
$$ W(x,y) = \exp{\left(-\frac{x^2+y^2}{2\sigma_w^2}\right)}$$
To do this, you'll need to start with an initial guess for $(y0,x0),$ and then calculate the values of $M_f, M_x$ and $M_y$.  Then move the aperture's $x_0,y_0$ by $(M_x/M_f, M_y/M_f)$ and try again.  Iterate until the centroid doesn't move any more (or until you have done 10 iterations and need to quit!).
Can you write Python code for it?

Write me a Python code to calculate the values of $M_f, M_x$ and $M_y$?
assistant
#Here is Python code to calculate the values of $M_f, M_x$ and $M_y$.

# Here's my weight function class
# Remember, to use a class, you first create an "instance"
# of it, e.g.:  weight = GaussAp(sigma=2.6)
# and then you can call this instance like a function of dx,dy

class GaussAp:
    # A tophat weight function of radius sigma
    def __init__(self,sigma):
        self.sigma = sigma
        return
    def __call__(self,dx,dy):
        # Given equal-shaped arrays dx=x-x0, dy=y-y0,
        # returns an array of the same shape giving weight function.

        # Calculate distance of a pixel from the center
        rsq = np.square(dx) + np.square(dy)
    
        # Now return the weight
        return np.exp(-rsq/(2.*self.sigma*self.sigma))

# Your work here...