"Kernel Appears to have died" problem when using mathematical constraints on a parameter

786 views
Skip to first unread message

Kiyan Kheradvar

unread,
Apr 27, 2021, 1:33:43 PM4/27/21
to lmfit-py
Hi everyone, 

I have created code to fit the sum of "n" gaussians to my data. Now, I was trying to keep the width or sigma of all gaussians equal using Lmfit expressions, but it caused the kernel that contains the minimize function to die. Here is my code; I've split it into kernels using dashes and indicated which kernel keeps dying. 

import scipy
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from numpy import exp, loadtxt, pi, sqrt
from lmfit import Model
from numpy import exp, linspace, pi, random, sign, sin
from lmfit import Parameters, fit_report, minimize

data = np.loadtxt("MyDataFile.csv", delimiter=',', skiprows=1)
x = data[:, 0]
y = data[:, 1]

n=3
--------------------------------------------
def gaussian (x, amp, cen, wid): 
    return (amp / (sqrt(2*pi) * wid)) * exp(-(x-cen)**2 / (2*wid**2))
  
def gaussiangenerator(params, i, x): 
    amp = params['amp_%d' % (i)]
    cen = params['cen_%d' % (i)]
    wid = params['wid_%d' % (i)]
    return gaussian (x, amp, cen, wid)

def objective(params, x, data, n):
    individual_fits=np.ones([n, len(x)])
    for i in range(1, n+1):
        individual_fits[i-1, :] = gaussiangenerator(params, i, x)
    resid = data - np.sum(individual_fits, axis=0)
    return resid.flatten()
------------------------------------------------------
fit_params = Parameters()
for i in range(1, n+1):
    fit_params.add('amp_%d' % (i), value = 1, min = 0.5, max = 10, vary = True)
    fit_params.add('cen_%d' % (i), value = 9, min = 0, max = 20, vary = True)
    fit_params.add('wid_%d' % (i), value = 1, min = 0, max = 2, vary = True)
    
for iy in range(1, n+1):
    fit_params['wid_%d' % (iy)].expr='wid_%d' % (1)
 --------------------------------------------------------   
out = minimize(objective, fit_params, args=(x, y, n)) #This is the Kernel that keeps dying when I run it. 
----------------------------------------------------------
print(fit_report(out))
----------------------------------------------------------

Thank you for your kind attention. 

Sincerely, 

Kiyan

Matt Newville

unread,
Apr 27, 2021, 4:57:44 PM4/27/21
to lmfit-py
"Kernel died" suggests that you are running from Jupyter.   That's fine, at least to the extent that "Jupyter is Python".   And that is true right up until the kernel dies (or you have problems with "magics" or any other junk fine things from Jupyter).

OTOH, if you were to run with actual Python, you might get a useful error message, and then you would have something that we might be able to use to help you.  

If "Kernel died" is all you can give us to go on, I think it's probably a problem with Jupyter.

Kiyan Kheradvar

unread,
May 1, 2021, 2:48:38 PM5/1/21
to lmfi...@googlegroups.com
I've fixed the issue. The following line has a mistake:

for iy in range(1, n+1):
    fit_params['wid_%d' % (iy)].expr='wid_%d' % (1)

the range should be (2,n+1). 


--
You received this message because you are subscribed to a topic in the Google Groups "lmfit-py" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/lmfit-py/T-jrOiwG4zI/unsubscribe.
To unsubscribe from this group and all its topics, 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/dbfad081-b070-4489-8634-aa52a7437d9cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages