Line Profiling
Only available on the github page: https://apn-pucky.github.io/smpl/
tests/fit/test_fit_algos.py
test_fit_algos
Timer unit: 1e-09 s
Total time: 0.127743 s
File: /__w/smpl/smpl/smpl/fit.py
Function: fit at line 130
Line # Hits Time Per Hit % Time Line Contents
==============================================================
130 def fit(datax, datay, function, **kwargs):
131 """
132 Returns a fit of ``function`` to ``datax`` and ``datay``.
133
134 Parameters
135 ----------
136 datax : array_like
137 X data either as ``unp.uarray`` or ``np.array`` or ``list``
138 datay : array_like
139 Y data either as ``unp.uarray`` or ``np.array`` or ``list``
140 function : func
141 Fit function with parameters: ``x``, ``params``
142 **kwargs : optional
143 see :func:`fit_kwargs`.
144
145 """
146 24 364509.0 15187.9 0.3 kwargs = fit_kwargs(kwargs)
147 24 5097429.0 212392.9 4.0 x, y, xerr, yerr = fit_split(datax, datay, **kwargs)
148
149 24 2556663.0 106527.6 2.0 tmp, params, fixed, Ntot = _wrap_func_and_param(function, **kwargs)
150
151 24 13600.0 566.7 0.0 fitter = kwargs["fitter"]
152 24 42700.0 1779.2 0.0 if fitter is Fitter.AUTO:
153 if xerr is not None:
154 fitter = Fitter.SCIPY_ODR
155 else:
156 fitter = Fitter.SCIPY_CURVEFIT
157 16 10301.0 643.8 0.0 if fitter is Fitter.MINUIT_LEASTSQUARES:
158 8 84341921.0 10542740.1 66.0 fitt = _fit_minuit_leastsquares(x, y, tmp, params=params, yerr=yerr)
159 8 4600.0 575.0 0.0 elif fitter is Fitter.SCIPY_CURVEFIT:
160 8 16122907.0 2015363.4 12.6 fitt = _fit_curvefit(x, y, tmp, params=params, yerr=yerr)
161 8 3900.0 487.5 0.0 elif fitter is Fitter.SCIPY_ODR:
162 8 19000378.0 2375047.2 14.9 fitt = _fit_odr(x, y, tmp, params=params, xerr=xerr, yerr=yerr)
163 else:
164 raise ValueError("Unknown fitter: {}".format(fitter))
165
166 24 184104.0 7671.0 0.1 return _unwrap_param(fitt, fixed, Ntot)
Total time: 0.0842218 s
File: /__w/smpl/smpl/smpl/fit.py
Function: _fit_minuit_leastsquares at line 273
Line # Hits Time Per Hit % Time Line Contents
==============================================================
273 def _fit_minuit_leastsquares(datax, datay, function, yerr, params=None, **kwargs):
274 # everything in iminuit is done through the Minuit object, so we import it
275 8 62401.0 7800.1 0.1 from iminuit import Minuit
276
277 # we also need a cost function to fit and import the LeastSquares function
278 8 58201.0 7275.1 0.1 from iminuit.cost import LeastSquares
279 8 20802.0 2600.2 0.0 from iminuit.util import Matrix
280
281 # TODO check/add params
282 8 3100.0 387.5 0.0 if params is None:
283 params = []
284 8 2247054.0 280881.8 2.7 least_squares = LeastSquares(
285 8 3400.0 425.0 0.0 datax, datay, yerr if yerr is not None else 1, function
286 )
287 8 2405361.0 300670.1 2.9 m = Minuit(least_squares, *params)
288 8 70448472.0 8806059.0 83.6 m.migrad()
289 8 6620466.0 827558.2 7.9 m.hesse()
290 # print(m.values)
291 # print(m.covariance)
292 # print("chi2 = ", m.fval)
293 # print("ndof = ", len(datax) - m.nfit)
294
295 # fix slice issue from iminuites rewritten __getitem__ by using super
296 8 2352559.0 294069.9 2.8 return unc.correlated_values(m.values, super(Matrix, m.covariance))
Total time: 0.0159672 s
File: /__w/smpl/smpl/smpl/fit.py
Function: _fit_curvefit at line 312
Line # Hits Time Per Hit % Time Line Contents
==============================================================
312 def _fit_curvefit(datax, datay, function, params=None, yerr=None, **kwargs):
313 8 3100.0 387.5 0.0 try:
314 8 14480665.0 1810083.1 90.7 pfit, pcov = optimize.curve_fit(
315 8 1700.0 212.5 0.0 function,
316 8 1800.0 225.0 0.0 datax,
317 8 1500.0 187.5 0.0 datay,
318 8 1800.0 225.0 0.0 p0=params,
319 8 1800.0 225.0 0.0 sigma=yerr,
320 8 19800.0 2475.0 0.1 epsfcn=util.get("epsfcn", kwargs, 0.0001),
321 8 1900.0 237.5 0.0 **kwargs,
322 8 12300.0 1537.5 0.1 maxfev=util.get("maxfev", kwargs, 10000)
323 )
324 except RuntimeError as e:
325 debug.msg(str(e))
326 return params
327 8 5500.0 687.5 0.0 error = []
328 12 18000.0 1500.0 0.1 for i in range(len(pfit)):
329 12 3700.0 308.3 0.0 try:
330 12 63301.0 5275.1 0.4 error.append(np.absolute(pcov[i][i]) ** 0.5)
331 except Exception as e:
332 warnings.warn(str(e))
333 error.append(0.00)
334 # print(pcov)
335 # restore cov: unc.covariance_matrix([*ff])
336 8 1350333.0 168791.6 8.5 return unc.correlated_values(pfit, pcov)
Total time: 0.0188936 s
File: /__w/smpl/smpl/smpl/fit.py
Function: _fit_odr at line 342
Line # Hits Time Per Hit % Time Line Contents
==============================================================
342 def _fit_odr(datax, datay, function, params=None, yerr=None, xerr=None):
343 8 48103.0 6012.9 0.3 model = Model(lambda p, x: function(x, *p))
344 8 134902.0 16862.8 0.7 realdata = RealData(datax, datay, sy=yerr, sx=xerr)
345 8 861022.0 107627.8 4.6 odr = ODR(realdata, model, beta0=params)
346 8 16429813.0 2053726.6 87.0 out = odr.run()
347 # This was the old wrong way! Now use correct co. matrix through unc-package!
348 # Note Issues on scipy odr and curve_fit, regarding different definitions/namings of standard deviation or error and covaraince matrix
349 # https://github.com/scipy/scipy/issues/6842
350 # https://github.com/scipy/scipy/pull/12207
351 # https://stackoverflow.com/questions/62460399/comparison-of-curve-fit-and-scipy-odr-absolute-sigma
352 8 1419736.0 177467.0 7.5 return unc.correlated_values(out.beta, out.cov_beta)
Total time: 0.130797 s
File: /__w/smpl/smpl/tests/fit/test_fit_algos.py
Function: _test_fit at line 50
Line # Hits Time Per Hit % Time Line Contents
==============================================================
50 def _test_fit(fitter):
51 3 1587240.0 529080.0 1.2 data = np.loadtxt("tests/test_linear.txt")
52 3 7900.0 2633.3 0.0 datax, datay = data[:, 0], data[:, 1]
53 3 5806046.0 1935348.7 4.4 _test_fit_linear(fitter, datax, datay)
54 3 27181184.0 9060394.7 20.8 _test_fit_exponential(fitter, datax, datay)
55
56 3 303407.0 101135.7 0.2 datax, datay = unp.uarray(data[:, 0], data[:, 2]), data[:, 1]
57 3 5145531.0 1715177.0 3.9 _test_fit_linear(fitter, datax, datay)
58 3 25805147.0 8601715.7 19.7 _test_fit_exponential(fitter, datax, datay)
59
60 3 292708.0 97569.3 0.2 datax, datay = data[:, 0], unp.uarray(data[:, 1], data[:, 3])
61 3 5028026.0 1676008.7 3.8 _test_fit_linear(fitter, datax, datay)
62 3 26936677.0 8978892.3 20.6 _test_fit_exponential(fitter, datax, datay)
63
64 3 273507.0 91169.0 0.2 datax, datay = unp.uarray(data[:, 0], data[:, 2]), unp.uarray(
65 3 4000.0 1333.3 0.0 data[:, 1], data[:, 3]
66 )
67 3 5933749.0 1977916.3 4.5 _test_fit_linear(fitter, datax, datay)
68 3 26492267.0 8830755.7 20.3 _test_fit_exponential(fitter, datax, datay)
Total time: 0.131068 s
File: /__w/smpl/smpl/tests/fit/test_fit_algos.py
Function: _test_fit_algos at line 71
Line # Hits Time Per Hit % Time Line Contents
==============================================================
71 def _test_fit_algos():
72 1 88257419.0 88257419.0 67.3 _test_fit(Fitter.MINUIT_LEASTSQUARES)
73 1 19757697.0 19757697.0 15.1 _test_fit(Fitter.SCIPY_CURVEFIT)
74 1 23052580.0 23052580.0 17.6 _test_fit(Fitter.SCIPY_ODR)
tests/plot/test_fit.py
test_fit
Timer unit: 1e-09 s
Total time: 0.393133 s
File: /__w/smpl/smpl/smpl/plot.py
Function: fit at line 241
Line # Hits Time Per Hit % Time Line Contents
==============================================================
241 def fit(func, *adata, **kwargs):
242 """
243 Fit and plot function to datax and datay.
244
245 Parameters
246 ----------
247 datax : array_like
248 X data either as ``unp.uarray`` or ``np.array`` or ``list``
249 datay : array_like
250 Y data either as ``unp.uarray`` or ``np.array`` or ``list``
251 function : func
252 Fit function with parameters: ``x``, ``params``
253 **kwargs : optional
254 see :func:`plot_kwargs`.
255 Fit parameters can be fixed via ``kwargs`` eg. ``a=5``.
256
257 Returns
258 -------
259 array_like
260 Optimized fit parameters of ``function`` to ``datax`` and ``datay``.
261 If ``datay`` is complex, both the real and imaginary part are returned.
262
263 Examples
264 --------
265
266 .. plot::
267 :include-source:
268
269 >>> from smpl import functions as f
270 >>> from smpl import plot
271 >>> param = plot.fit([0,1,2],[0,1,2],f.line)
272 >>> plot.unv(param).round()[0]
273 1.0
274
275 """
276 2 1400.0 700.0 0.0 function = func
277 2 1200.0 600.0 0.0 if "function" in kwargs:
278 2 700.0 350.0 0.0 function = kwargs["function"]
279 2 900.0 450.0 0.0 del kwargs["function"]
280 2 1400.0 700.0 0.0 adata = [func, *adata]
281 # Fix parameter order if necessary
282 elif isinstance(function, (list, tuple, np.ndarray)):
283 adata = [adata[-1], function, *adata[:-1]]
284 function = adata[0]
285 adata = adata[1:]
286 2 11701.0 5850.5 0.0 if util.true("bins", kwargs):
287 # yvalue will be overwritten
288 ndata = [*adata, *adata]
289 for i, o in enumerate(adata):
290 ndata[2 * i] = o
291 ndata[2 * i + 1] = o * 0
292 adata = ndata
293
294 2 3800.0 1900.0 0.0 assert len(adata) % 2 == 0, "data must be pairs of x and y data"
295 2 1300.0 650.0 0.0 if len(adata) == 2:
296 2 1000.0 500.0 0.0 datax, datay = adata
297 else:
298 rs = []
299 for i in range(0, len(adata), 2):
300 datax, datay = adata[i], adata[i + 1]
301 if util.true("bins", kwargs):
302 rs.append(fit(function, datax, **kwargs))
303 else:
304 rs.append(fit(function, datax, datay, **kwargs))
305 return rs
306
307 2 91802.0 45901.0 0.0 kwargs = plot_kwargs(kwargs)
308
309 2 98102.0 49051.0 0.0 if np.any(np.iscomplex(datay)):
310 label = util.get("label", kwargs, "")
311 kwargs["label"] = label + "(real)"
312 r = fit(datax, datay.real, function=function, **kwargs)
313 kwargs["label"] = label + "(imag)"
314 i = fit(datax, datay.imag, function=function, **kwargs)
315 return r, i
316 2 1000.0 500.0 0.0 if kwargs["auto_fit"]:
317 best_f, best_ff, lambda_f = ffit.auto(datax, datay, function, **kwargs)
318 if best_f is not None:
319 del kwargs["auto_fit"]
320 fit(datax, datay, best_f, **kwargs)
321 return best_f, best_ff, lambda_f
322 2 1100.0 550.0 0.0 if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
323 kwargs["lpos"] = -1
324 2 392917680.0 196458840.0 99.9 return _fit_impl(datax, datay, function, **kwargs)
Total time: 0.0104181 s
File: /__w/smpl/smpl/smpl/plot.py
Function: function at line 495
Line # Hits Time Per Hit % Time Line Contents
==============================================================
495 def function(func, *args, **kwargs):
496 """
497 Plot function ``func`` between ``xmin`` and ``xmax``
498
499 Parameters
500 ----------
501 func : function
502 Function to be plotted between ``xmin`` and ``xmax``, only taking `array_like` ``x`` as parameter
503 *args : optional
504 arguments for ``func``
505 **kwargs : optional
506 see :func:`plot_kwargs`.
507 """
508 1 5300.0 5300.0 0.1 if not util.has("xmin", kwargs) or not util.has("xmax", kwargs):
509 kwargs["xmin"], kwargs["xmax"] = stat.get_interesting_domain(func)
510 # raise Exception("xmin or xmax missing.")
511
512 # if not util.has('lpos', kwargs) and not util.has('label', kwargs):
513 # kwargs['lpos'] = -1
514 1 1600.0 1600.0 0.0 if not util.has("fmt", kwargs):
515 1 500.0 500.0 0.0 kwargs["fmt"] = "-"
516
517 1 500.0 500.0 0.0 if "label" not in kwargs:
518 1 54902.0 54902.0 0.5 kwargs = plot_kwargs(kwargs)
519 1 4254407.0 4254407.0 40.8 kwargs["label"] = get_fnc_legend(func, args, **kwargs)
520 else:
521 kwargs = plot_kwargs(kwargs)
522
523 1 87002.0 87002.0 0.8 xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
524 1 333509.0 333509.0 3.2 init_plot(kwargs)
525
526 # kwargs['lpos'] = 0
527 # _plot(xfit, func(xfit, *args), **kwargs)
528 1 1488337.0 1488337.0 14.3 _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
529 1 600.0 600.0 0.0 if kwargs["ss"]:
530 1 4191405.0 4191405.0 40.2 save_plot(**kwargs)
Total time: 0.0768737 s
File: /__w/smpl/smpl/smpl/plot.py
Function: init_plot at line 808
Line # Hits Time Per Hit % Time Line Contents
==============================================================
808 def init_plot(kwargs):
809 3 1300.0 433.3 0.0 fig = None
810 3 7500.0 2500.0 0.0 if util.has("axes", kwargs) and kwargs["axes"] is not None:
811 plt.sca(kwargs["axes"])
812 fig = kwargs["axes"].get_figure()
813 2 5600.0 2800.0 0.0 if kwargs["init"] or util.true("residue", kwargs):
814 2 700.0 350.0 0.0 if kwargs["size"] is None:
815 2 2001350.0 1000675.0 2.6 fig = plt.figure()
816 else:
817 fig = plt.figure(figsize=kwargs["size"])
818 2 2100.0 1050.0 0.0 if kwargs["residue"]:
819 2 74032262.0 37016131.0 96.3 fig.add_axes((0.1, 0.3, 0.8, 0.6))
820 3 11100.0 3700.0 0.0 if util.has("xlabel", kwargs) and kwargs["xlabel"] != "":
821 plt.xlabel(kwargs["xlabel"])
822 3 4501.0 1500.3 0.0 if util.has("ylabel", kwargs) and kwargs["ylabel"] != "":
823 plt.ylabel(kwargs["ylabel"])
824 3 15200.0 5066.7 0.0 if util.has("xaxis", kwargs) and kwargs["xaxis"] != "":
825 3 461112.0 153704.0 0.6 plt.xlabel(kwargs["xaxis"])
826 3 6500.0 2166.7 0.0 if util.has("yaxis", kwargs) and kwargs["yaxis"] != "":
827 3 318508.0 106169.3 0.4 plt.ylabel(kwargs["yaxis"])
828 3 5100.0 1700.0 0.0 if util.has("next_color", kwargs) and not kwargs["next_color"]:
829 it1, it2 = itertools.tee(iter(plt.gca()._get_lines.prop_cycler))
830 plt.gca()._get_lines.prop_cycler = it2
831 tmp_color = next(it1)["color"]
832 if kwargs["data_color"] is None:
833 kwargs["data_color"] = tmp_color
834 if kwargs["fit_color"] is None:
835 kwargs["fit_color"] = tmp_color
836 if kwargs["function_color"] is None:
837 kwargs["function_color"] = tmp_color
838 3 901.0 300.3 0.0 return fig
tests/test_wrap.py
test_wrap
Timer unit: 1e-09 s
Total time: 0.00686027 s
File: /__w/smpl/smpl/smpl/wrap.py
Function: get_latex at line 31
Line # Hits Time Per Hit % Time Line Contents
==============================================================
31 def get_latex(function):
32 """
33 Return a latex string for passed function.
34
35
36 Parameters
37 ----------
38 function : function_like
39 function as str lambda or (oneline) function
40
41 Examples
42 --------
43 >>> get_latex(lambda a,b,c,x : (a+b+c)*x,)
44 '$x \\\\left(a + b + c\\\\right)$'
45 >>> get_latex("(a+b+c)*x")
46 '$x \\\\left(a + b + c\\\\right)$'
47 >>> def fun(a,b,x,c):
48 ... return (a+b+c)*x
49 >>> get_latex(fun)
50 '$x \\\\left(a + b + c\\\\right)$'
51
52 """
53 3 3700.0 1233.3 0.1 if isinstance(function, str):
54 l = "$" + sympy.latex(str_get_expr(function)) + "$"
55 else:
56 3 1800.0 600.0 0.0 l = function.__name__
57 2 900.0 450.0 0.0 if l == "<lambda>":
58 1 100.0 100.0 0.0 try:
59 1 200.0 200.0 0.0 try:
60 1 84902.0 84902.0 1.2 cc, li = inspect.findsource(function)
61 1 300.0 300.0 0.0 f = (
62 2 3100.0 1550.0 0.0 "".join(cc[li:])
63 2 400.0 200.0 0.0 .split("lambda")[1]
64 2 400.0 200.0 0.0 .split(":")[1]
65 2 400.0 200.0 0.0 .split(",")[0]
66 1 200.0 200.0 0.0 .replace("\n", "")
67 )
68 1 3520388.0 3520388.0 51.3 l = "$" + sympy.latex(str_get_expr(f)) + "$"
69 except TokenError:
70 raise Exception(
71 "Make sure there is a ',' behind the lambda expression and no commas are in the lambda expression and no newlines"
72 )
73 except OSError:
74 l = "$\\lambda$(" + ",".join(function.__code__.co_varnames) + ")"
75 2 1600.0 800.0 0.0 elif not isinstance(function, str):
76 1 600.0 600.0 0.0 if function.__doc__ is not None:
77 1 2400.0 2400.0 0.0 l = function.__doc__.split("\n")[0]
78 else:
79 1 300.0 300.0 0.0 try:
80 1 97402.0 97402.0 1.4 cc, li = inspect.findsource(function)
81 1 22201.0 22201.0 0.3 s = "".join(cc[li:]).split("return")[1].split("\n")[0]
82 1 3117978.0 3117978.0 45.4 l = "$" + sympy.latex(str_get_expr(s)) + "$"
83 except OSError:
84 l = function.__name__
85
86 3 1000.0 333.3 0.0 return l
tests/tests/test_line_profiling.py
test_as_mark
Timer unit: 1e-09 s
Total time: 2.1e-06 s
File: /__w/smpl/smpl/tests/tests/test_line_profiling.py
Function: f at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 def f(i):
5 10 2100.0 210.0 100.0 return i * 10
Total time: 2.2001e-05 s
File: /__w/smpl/smpl/tests/tests/test_line_profiling.py
Function: g at line 8
Line # Hits Time Per Hit % Time Line Contents
==============================================================
8 def g(n=10):
9 1 22001.0 22001.0 100.0 return sum(f(i) for i in range(n))
tests/tests/test_line_profiling_doctests.py
test_as_mark
Timer unit: 1e-09 s
Total time: 2.2e-06 s
File: /__w/smpl/smpl/tests/tests/test_line_profiling_doctests.py
Function: f at line 4
Line # Hits Time Per Hit % Time Line Contents
==============================================================
4 def f(i):
5 """
6 >>> f(0)
7 0
8 """
9 10 2200.0 220.0 100.0 return i * 10
Total time: 2.4401e-05 s
File: /__w/smpl/smpl/tests/tests/test_line_profiling_doctests.py
Function: g at line 12
Line # Hits Time Per Hit % Time Line Contents
==============================================================
12 def g(n=10):
13 """
14 >>> g(10)
15 450
16
17 """
18 1 24401.0 24401.0 100.0 return sum(f(i) for i in range(n))