Python Forum
[Solved] TypeError when calling function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] TypeError when calling function
#1
Hey,

I'm trying to plot a multi-gaussian function to some data, but the code runs a TypeError:

import numpy as np
import matplotlib.pyplot as plt
import pathlib
import os
from scipy.optimize import curve_fit

#Data
data = np.loadtxt('file.hst')
x = data[:,0]
y1 = data[:,1] # sync1
y2 = data[:,2] # sync2

n_gauss = 2

#Mean & width for y1 & y2
center = sum(x * y1) / sum(y1)
#mean = sum(x * y2) / sum(y2)
width = np.sqrt(sum(y1 * (x - center)**2) / sum(y1))
#width = np.sqrt(sum(y2 * (x - center)**2) / sum(y2))
#Functions:

def multi_gaussian(x, *gaussians):
    y = gaussians[0] * np.ones_like(x)
    n_gauss = (len(gaussians)-1) // 3
    for gauss in range(n_gauss):
        y += gaussian(x, gaussians[3*gauss+1], gaussians[3*gauss+2],
                      gaussians[3*gauss+3])
    return y

def gaussian(x, offset, area, center, width):
    y = offset + area * np.exp(-(x - center)**2 / (2 * width**2))
    return y
    
#Plots:
plt.plot(x, y1, label="sync1")
plt.plot(x, y2, label="sync2")
popt, pcov = curve_fit(gaussian, x, y1, p0=[5000, max(y1), center, width])
#popt, pcov = curve_fit(gaussian, x, y2, p0=[5000, max(y2), center, width])
#plt.plot(x, gaussian(y1, -2100, 3200, center, width),
#    lw=1, c='m', ls='--', label='Gaussian')
plt.plot(x, multi_gaussian(y1, -200, 3200, center, width),
    lw=1, c='r', ls='--', label='multi-Gaussian')
#plt.plot(x, gaussian(y2, 0, 2250, center, width),
#    lw=1, c='r', ls='--', label='Gaussian')
#plt.xlim([850, 980])
#plt.ylim([30, 600])
plt.legend()
The error I get is this:

    gaussians[3*gauss+3])

TypeError: gaussian() missing 1 required positional argument: 'width'
Fitting the single gaussian function works perfectly, but multi_gaussian doesn't work, no matter which n_gauss I use (the same error comes with n_gauss = 1 or 3, f.ex.). Am I not giving the multi_gaussian function enough values, or what could be wrong here? I tried removing offset in case it causes the error, but there is no difference.
Reply
#2
The gaussian () function requires five parameters. On line 26/27 you call it with only four.
Reply
#3
(Jun-16-2021, 01:54 PM)BashBedlam Wrote: The gaussian () function requires five parameters. On line 26/27 you call it with only four.

Oh man, I should've realized this Big Grin the original gaussian I used for another code didn't have offset in it so I didn't put it in multi-gaussian either. Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Looking for documentation on Reportlab's canvas.transform() function NeilUK 1 552 Aug-23-2023, 01:21 PM
Last Post: NeilUK
  calling external function with arguments Wimpy_Wellington 7 1,344 Jul-05-2023, 06:33 PM
Last Post: deanhystad
  Calling a function (which accesses a library) from another file mouse9095 4 767 Jun-07-2023, 08:55 PM
Last Post: deanhystad
  [Solved] unkown (to me) function def parm "name1:name2" syntax. MvGulik 5 986 Nov-11-2022, 11:21 AM
Last Post: MvGulik
Sad Iterate randint() multiple times when calling a function Jake123 2 1,979 Feb-15-2022, 10:56 PM
Last Post: deanhystad
  Calling a class from a function jc4d 5 1,754 Dec-17-2021, 09:04 PM
Last Post: ndc85430
  Using curve_fit to optimize function (TypeError) Laplace12 4 2,453 Aug-30-2021, 11:15 AM
Last Post: Larz60+
  calling a function and argument in an input phillup7 3 2,552 Oct-25-2020, 02:12 PM
Last Post: jefsummers
  [Solved]TypeError: list indices must be integers or slices, not str NectDz 3 3,867 Jun-02-2020, 08:21 AM
Last Post: DreamingInsanity
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,208 Apr-06-2020, 09:14 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020