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.0899357 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     239786.0   9991.1      0.3      kwargs = fit_kwargs(kwargs)
   147        24    3370957.0 140456.5      3.7      x, y, xerr, yerr = fit_split(datax, datay, **kwargs)
   148
   149        24    1339029.0  55792.9      1.5      tmp, params, fixed, Ntot = _wrap_func_and_param(function, **kwargs)
   150
   151        24       8188.0    341.2      0.0      fitter = kwargs["fitter"]
   152        24      24627.0   1026.1      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       5835.0    364.7      0.0      if fitter is Fitter.MINUIT_LEASTSQUARES:
   158         8   60726739.0 7590842.4     67.5          fitt = _fit_minuit_leastsquares(x, y, tmp, params=params, yerr=yerr)
   159         8       2877.0    359.6      0.0      elif fitter is Fitter.SCIPY_CURVEFIT:
   160         8   11058815.0 1382351.9     12.3          fitt = _fit_curvefit(x, y, tmp, params=params, yerr=yerr)
   161         8       2446.0    305.8      0.0      elif fitter is Fitter.SCIPY_ODR:
   162         8   13053116.0 1631639.5     14.5          fitt = _fit_odr(x, y, tmp, params=params, xerr=xerr, yerr=yerr)
   163                                               else:
   164                                                   raise ValueError("Unknown fitter: {}".format(fitter))
   165
   166        24     103303.0   4304.3      0.1      return _unwrap_param(fitt, fixed, Ntot)

Total time: 0.0606545 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      34214.0   4276.8      0.1      from iminuit import Minuit
   276
   277                                               # we also need a cost function to fit and import the LeastSquares function
   278         8      32510.0   4063.8      0.1      from iminuit.cost import LeastSquares
   279         8      12143.0   1517.9      0.0      from iminuit.util import Matrix
   280
   281                                               # TODO check/add params
   282         8       1803.0    225.4      0.0      if params is None:
   283                                                   params = []
   284         8    1272206.0 159025.8      2.1      least_squares = LeastSquares(
   285         8       2025.0    253.1      0.0          datax, datay, yerr if yerr is not None else 1, function
   286                                               )
   287         8    1707338.0 213417.2      2.8      m = Minuit(least_squares, *params)
   288         8   51163134.0 6395391.8     84.4      m.migrad()
   289         8    4797422.0 599677.8      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    1631724.0 203965.5      2.7      return unc.correlated_values(m.values, super(Matrix, m.covariance))

Total time: 0.010973 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       1964.0    245.5      0.0      try:
   314         8   10003656.0 1250457.0     91.2          pfit, pcov = optimize.curve_fit(
   315         8       1874.0    234.2      0.0              function,
   316         8        911.0    113.9      0.0              datax,
   317         8        984.0    123.0      0.0              datay,
   318         8       1070.0    133.8      0.0              p0=params,
   319         8       1063.0    132.9      0.0              sigma=yerr,
   320         8      12865.0   1608.1      0.1              epsfcn=util.get("epsfcn", kwargs, 0.0001),
   321         8       1082.0    135.2      0.0              **kwargs,
   322         8       8205.0   1025.6      0.1              maxfev=util.get("maxfev", kwargs, 10000)
   323                                                   )
   324                                               except RuntimeError as e:
   325                                                   debug.msg(str(e))
   326                                                   return params
   327         8       2695.0    336.9      0.0      error = []
   328        12      10991.0    915.9      0.1      for i in range(len(pfit)):
   329        12       1593.0    132.8      0.0          try:
   330        12      38642.0   3220.2      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     885443.0 110680.4      8.1      return unc.correlated_values(pfit, pcov)

Total time: 0.0129876 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      24195.0   3024.4      0.2      model = Model(lambda p, x: function(x, *p))
   344         8      97711.0  12213.9      0.8      realdata = RealData(datax, datay, sy=yerr, sx=xerr)
   345         8     580162.0  72520.2      4.5      odr = ODR(realdata, model, beta0=params)
   346         8   11402825.0 1425353.1     87.8      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     882708.0 110338.5      6.8      return unc.correlated_values(out.beta, out.cov_beta)

Total time: 0.0915296 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     748888.0 249629.3      0.8      data = np.loadtxt("tests/test_linear.txt")
    52         3       5320.0   1773.3      0.0      datax, datay = data[:, 0], data[:, 1]
    53         3    3607873.0 1202624.3      3.9      _test_fit_linear(fitter, datax, datay)
    54         3   19378918.0 6459639.3     21.2      _test_fit_exponential(fitter, datax, datay)
    55
    56         3     173845.0  57948.3      0.2      datax, datay = unp.uarray(data[:, 0], data[:, 2]), data[:, 1]
    57         3    3444537.0 1148179.0      3.8      _test_fit_linear(fitter, datax, datay)
    58         3   18922635.0 6307545.0     20.7      _test_fit_exponential(fitter, datax, datay)
    59
    60         3     181699.0  60566.3      0.2      datax, datay = data[:, 0], unp.uarray(data[:, 1], data[:, 3])
    61         3    3453344.0 1151114.7      3.8      _test_fit_linear(fitter, datax, datay)
    62         3   19091112.0 6363704.0     20.9      _test_fit_exponential(fitter, datax, datay)
    63
    64         3     166200.0  55400.0      0.2      datax, datay = unp.uarray(data[:, 0], data[:, 2]), unp.uarray(
    65         3       2824.0    941.3      0.0          data[:, 1], data[:, 3]
    66                                               )
    67         3    3490543.0 1163514.3      3.8      _test_fit_linear(fitter, datax, datay)
    68         3   18861905.0 6287301.7     20.6      _test_fit_exponential(fitter, datax, datay)

Total time: 0.0916901 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   63105867.0 63105867.0     68.8      _test_fit(Fitter.MINUIT_LEASTSQUARES)
    73         1   13301990.0 13301990.0     14.5      _test_fit(Fitter.SCIPY_CURVEFIT)
    74         1   15282214.0 15282214.0     16.7      _test_fit(Fitter.SCIPY_ODR)

tests/plot/test_fit.py

test_fit

Timer unit: 1e-09 s

Total time: 0.402254 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        872.0    436.0      0.0      function = func
   277         2        772.0    386.0      0.0      if "function" in kwargs:
   278         2        481.0    240.5      0.0          function = kwargs["function"]
   279         2        482.0    241.0      0.0          del kwargs["function"]
   280         2       1142.0    571.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       4820.0   2410.0      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       1783.0    891.5      0.0      assert len(adata) % 2 == 0, "data must be pairs of x and y data"
   295         2        922.0    461.0      0.0      if len(adata) == 2:
   296         2        771.0    385.5      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      48520.0  24260.0      0.0      kwargs = plot_kwargs(kwargs)
   308
   309         2      51306.0  25653.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        810.0    405.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        620.0    310.0      0.0      if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
   323                                                   kwargs["lpos"] = -1
   324         2  402140259.0 201070129.5    100.0      return _fit_impl(datax, datay, function, **kwargs)

Total time: 0.00668915 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       2775.0   2775.0      0.0      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        922.0    922.0      0.0      if not util.has("fmt", kwargs):
   515         1        281.0    281.0      0.0          kwargs["fmt"] = "-"
   516
   517         1        190.0    190.0      0.0      if "label" not in kwargs:
   518         1      25979.0  25979.0      0.4          kwargs = plot_kwargs(kwargs)
   519         1    2912965.0 2912965.0     43.5          kwargs["label"] = get_fnc_legend(func, args, **kwargs)
   520                                               else:
   521                                                   kwargs = plot_kwargs(kwargs)
   522
   523         1      51776.0  51776.0      0.8      xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
   524         1     190866.0 190866.0      2.9      init_plot(kwargs)
   525
   526                                               # kwargs['lpos'] = 0
   527                                               # _plot(xfit, func(xfit, *args), **kwargs)
   528         1     928423.0 928423.0     13.9      _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
   529         1        331.0    331.0      0.0      if kwargs["ss"]:
   530         1    2574644.0 2574644.0     38.5          save_plot(**kwargs)

Total time: 0.0502576 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        891.0    297.0      0.0      fig = None
   810         3       4719.0   1573.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       3166.0   1583.0      0.0      if kwargs["init"] or util.true("residue", kwargs):
   814         2        430.0    215.0      0.0          if kwargs["size"] is None:
   815         2    1178138.0 589069.0      2.3              fig = plt.figure()
   816                                                   else:
   817                                                       fig = plt.figure(figsize=kwargs["size"])
   818         2        851.0    425.5      0.0          if kwargs["residue"]:
   819         2   48581647.0 24290823.5     96.7              fig.add_axes((0.1, 0.3, 0.8, 0.6))
   820         3       5340.0   1780.0      0.0      if util.has("xlabel", kwargs) and kwargs["xlabel"] != "":
   821                                                   plt.xlabel(kwargs["xlabel"])
   822         3       2536.0    845.3      0.0      if util.has("ylabel", kwargs) and kwargs["ylabel"] != "":
   823                                                   plt.ylabel(kwargs["ylabel"])
   824         3       2455.0    818.3      0.0      if util.has("xaxis", kwargs) and kwargs["xaxis"] != "":
   825         3     256349.0  85449.7      0.5          plt.xlabel(kwargs["xaxis"])
   826         3       4028.0   1342.7      0.0      if util.has("yaxis", kwargs) and kwargs["yaxis"] != "":
   827         3     213208.0  71069.3      0.4          plt.ylabel(kwargs["yaxis"])
   828         3       3446.0   1148.7      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        442.0    147.3      0.0      return fig

tests/test_wrap.py

test_wrap

Timer unit: 1e-09 s

Total time: 0.0046538 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       2224.0    741.3      0.0      if isinstance(function, str):
    54                                                   l = "$" + sympy.latex(str_get_expr(function)) + "$"
    55                                               else:
    56         3        811.0    270.3      0.0          l = function.__name__
    57         2        592.0    296.0      0.0      if l == "<lambda>":
    58         1        120.0    120.0      0.0          try:
    59         1        120.0    120.0      0.0              try:
    60         1      56666.0  56666.0      1.2                  cc, li = inspect.findsource(function)
    61         1        130.0    130.0      0.0                  f = (
    62         1       1052.0   1052.0      0.0                      "".join(cc[li:])
    63         1        861.0    861.0      0.0                      .split("lambda")[1]
    64         1        511.0    511.0      0.0                      .split(":")[1]
    65         1        521.0    521.0      0.0                      .split(",")[0]
    66         1        581.0    581.0      0.0                      .replace("\n", "")
    67                                                           )
    68         1    2334176.0 2334176.0     50.2                  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        891.0    445.5      0.0      elif not isinstance(function, str):
    76         1        320.0    320.0      0.0          if function.__doc__ is not None:
    77         1       1623.0   1623.0      0.0              l = function.__doc__.split("\n")[0]
    78                                                   else:
    79         1        130.0    130.0      0.0              try:
    80         1      59992.0  59992.0      1.3                  cc, li = inspect.findsource(function)
    81         1      13225.0  13225.0      0.3                  s = "".join(cc[li:]).split("return")[1].split("\n")[0]
    82         1    2178525.0 2178525.0     46.8                  l = "$" + sympy.latex(str_get_expr(s)) + "$"
    83                                                       except OSError:
    84                                                           l = function.__name__
    85
    86         3        733.0    244.3      0.0      return l

tests/tests/test_line_profiling.py

test_as_mark

Timer unit: 1e-09 s

Total time: 1.454e-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       1454.0    145.4    100.0      return i * 10

Total time: 1.3435e-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      13435.0  13435.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: 1.423e-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       1423.0    142.3    100.0      return i * 10

Total time: 1.3716e-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      13716.0  13716.0    100.0      return sum(f(i) for i in range(n))