File: fit_erf.py

package info (click to toggle)
vedo 2025.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 10,404 kB
  • sloc: python: 64,792; javascript: 1,932; xml: 437; sh: 139; makefile: 6
file content (49 lines) | stat: -rw-r--r-- 1,126 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from scipy import special
from scipy.optimize import curve_fit
from vedo import np, settings, Marker
from vedo.pyplot import plot

settings.default_font = 'Calco'
settings.remember_last_figure_format = True

xdata = [230, 234, 240, 243, 246, 249, 252]
ydata = [0,   0,    11,  62,  15,  21, 100]
tdata = [100, 31,   34, 80,   21,  21, 100]

yerrs = np.sqrt(ydata) /np.array(tdata) + 0.1
ydata = np.array(ydata) /np.array(tdata)

def func(x, a, x0):
    return (1 + special.erf(a*(x-x0))) / 2

p0 = [1/25, 240] # initial guess
popt, _ = curve_fit(func, xdata, ydata, p0)

x = np.linspace(225, 255, 50)
y = func(x, *popt)
x0, y0 = popt[1], func(popt[1], *popt)

fig = plot(
    xdata,
    ydata,
    'o',
    yerrors=yerrs,
    ylim=(-0.1,1.3),
    title="ERF(x) fit to data",
    ytitle='Embryos with visible HL',
    xtitle='Hind Limb age (h)',
    mc='blue2',
    ms=0.3,
    lwe=2,
    label='data',
)

fig += plot(x, y, lw=5, label='fit')
fig += Marker('*', s=0.05, c='r4').pos(x0,y0, 0.1)

fig.add_label(':mu', marker='*', mc="r4")
fig.add_legend("top-left", vspace=2.5)

fig.show(size=(900, 650), zoom='tight').close()