For a script that I am working on I want to make it optional to pass on an array to a function. The way in which I have attempted to do this is by making the variable in question (residue
) a kwarg
.
The problem is that when I do it in this way, python changes de dtype of the kwarg from a numpy.ndarray
to dict
. The simplest solution is to convert the variable back to a np.array
using:
residue = np.array(residue.values())
But I do not find this a very elegant solution. So I was wondering if someone could show me a "prettier" way to accomplish this and possibly explain to my why python does this?
The function in question is:
#Returns a function for a 2D Gaussian model
def Gaussian_model2D(data,x_box,y_box,amplitude,x_stddev,y_stddev,theta,**residue):
if not residue:
x_mean, y_mean = max_pixel(data) # Returns location of maximum pixel value
else:
x_mean, y_mean = max_pixel(residue) # Returns location of maximum pixel value
g_init = models.Gaussian2D(amplitude,x_mean,y_mean,x_stddev,y_stddev,theta)
return g_init
# end of Gaussian_model2D
The function is called with the following command:
g2_init = Gaussian_model2D(cut_out,x_box,y_box,amp,x_stddev,y_stddev,theta,residue=residue1)
The version of Python that I am working in is 2.7.15