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.179831 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     435106.0  18129.4      0.2      kwargs = fit_kwargs(kwargs)
   147        24    6411359.0 267140.0      3.6      x, y, xerr, yerr = fit_split(datax, datay, **kwargs)
   148
   149        24    3483730.0 145155.4      1.9      tmp, params, fixed, Ntot = _wrap_func_and_param(function, **kwargs)
   150
   151        24      18800.0    783.3      0.0      fitter = kwargs["fitter"]
   152        24      61102.0   2545.9      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      11000.0    687.5      0.0      if fitter is Fitter.MINUIT_LEASTSQUARES:
   158         8  122422829.0 15302853.6     68.1          fitt = _fit_minuit_leastsquares(x, y, tmp, params=params, yerr=yerr)
   159         8       8200.0   1025.0      0.0      elif fitter is Fitter.SCIPY_CURVEFIT:
   160         8   21316296.0 2664537.0     11.9          fitt = _fit_curvefit(x, y, tmp, params=params, yerr=yerr)
   161         8       5000.0    625.0      0.0      elif fitter is Fitter.SCIPY_ODR:
   162         8   25458234.0 3182279.2     14.2          fitt = _fit_odr(x, y, tmp, params=params, xerr=xerr, yerr=yerr)
   163                                               else:
   164                                                   raise ValueError("Unknown fitter: {}".format(fitter))
   165
   166        24     199803.0   8325.1      0.1      return _unwrap_param(fitt, fixed, Ntot)

Total time: 0.122243 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      78202.0   9775.2      0.1      from iminuit import Minuit
   276
   277                                               # we also need a cost function to fit and import the LeastSquares function
   278         8      65701.0   8212.6      0.1      from iminuit.cost import LeastSquares
   279         8      27901.0   3487.6      0.0      from iminuit.util import Matrix
   280
   281                                               # TODO check/add params
   282         8       2800.0    350.0      0.0      if params is None:
   283                                                   params = []
   284         8    3265130.0 408141.2      2.7      least_squares = LeastSquares(
   285         8       3700.0    462.5      0.0          datax, datay, yerr if yerr is not None else 1, function
   286                                               )
   287         8    3259531.0 407441.4      2.7      m = Minuit(least_squares, *params)
   288         8  102428742.0 12803592.8     83.8      m.migrad()
   289         8    9861191.0 1232648.9      8.1      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    3250331.0 406291.4      2.7      return unc.correlated_values(m.values, super(Matrix, m.covariance))

Total time: 0.0211313 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       4801.0    600.1      0.0      try:
   314         8   19043776.0 2380472.0     90.1          pfit, pcov = optimize.curve_fit(
   315         8       2400.0    300.0      0.0              function,
   316         8       2300.0    287.5      0.0              datax,
   317         8       1900.0    237.5      0.0              datay,
   318         8       2600.0    325.0      0.0              p0=params,
   319         8       2200.0    275.0      0.0              sigma=yerr,
   320         8      24300.0   3037.5      0.1              epsfcn=util.get("epsfcn", kwargs, 0.0001),
   321         8       1700.0    212.5      0.0              **kwargs,
   322         8      13500.0   1687.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       6000.0    750.0      0.0      error = []
   328        12      26500.0   2208.3      0.1      for i in range(len(pfit)):
   329        12       3800.0    316.7      0.0          try:
   330        12      85801.0   7150.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    1909717.0 238714.6      9.0      return unc.correlated_values(pfit, pcov)

Total time: 0.025312 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      58401.0   7300.1      0.2      model = Model(lambda p, x: function(x, *p))
   344         8     173502.0  21687.8      0.7      realdata = RealData(datax, datay, sy=yerr, sx=xerr)
   345         8    1187009.0 148376.1      4.7      odr = ODR(realdata, model, beta0=params)
   346         8   22072604.0 2759075.5     87.2      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    1820517.0 227564.6      7.2      return unc.correlated_values(out.beta, out.cov_beta)

Total time: 0.183583 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    1886817.0 628939.0      1.0      data = np.loadtxt("tests/test_linear.txt")
    52         3      10301.0   3433.7      0.0      datax, datay = data[:, 0], data[:, 1]
    53         3    8859181.0 2953060.3      4.8      _test_fit_linear(fitter, datax, datay)
    54         3   37260143.0 12420047.7     20.3      _test_fit_exponential(fitter, datax, datay)
    55
    56         3     370804.0 123601.3      0.2      datax, datay = unp.uarray(data[:, 0], data[:, 2]), data[:, 1]
    57         3    6890964.0 2296988.0      3.8      _test_fit_linear(fitter, datax, datay)
    58         3   37134242.0 12378080.7     20.2      _test_fit_exponential(fitter, datax, datay)
    59
    60         3     343604.0 114534.7      0.2      datax, datay = data[:, 0], unp.uarray(data[:, 1], data[:, 3])
    61         3    7016264.0 2338754.7      3.8      _test_fit_linear(fitter, datax, datay)
    62         3   37469245.0 12489748.3     20.4      _test_fit_exponential(fitter, datax, datay)
    63
    64         3     336104.0 112034.7      0.2      datax, datay = unp.uarray(data[:, 0], data[:, 2]), unp.uarray(
    65         3       6100.0   2033.3      0.0          data[:, 1], data[:, 3]
    66                                               )
    67         3    7539170.0 2513056.7      4.1      _test_fit_linear(fitter, datax, datay)
    68         3   38459755.0 12819918.3     20.9      _test_fit_exponential(fitter, datax, datay)

Total time: 0.183939 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  127460775.0 127460775.0     69.3      _test_fit(Fitter.MINUIT_LEASTSQUARES)
    73         1   25977940.0 25977940.0     14.1      _test_fit(Fitter.SCIPY_CURVEFIT)
    74         1   30500181.0 30500181.0     16.6      _test_fit(Fitter.SCIPY_ODR)

tests/plot/test_fit.py

test_fit

Timer unit: 1e-09 s

Total time: 0.465069 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       1700.0    850.0      0.0      function = func
   277         2       1300.0    650.0      0.0      if "function" in kwargs:
   278         2        700.0    350.0      0.0          function = kwargs["function"]
   279         2       1000.0    500.0      0.0          del kwargs["function"]
   280         2       1700.0    850.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       9200.0   4600.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       3900.0   1950.0      0.0      assert len(adata) % 2 == 0, "data must be pairs of x and y data"
   295         2       1400.0    700.0      0.0      if len(adata) == 2:
   296         2        900.0    450.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      98801.0  49400.5      0.0      kwargs = plot_kwargs(kwargs)
   308
   309         2     112601.0  56300.5      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       1200.0    600.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       1400.0    700.0      0.0      if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
   323                                                   kwargs["lpos"] = -1
   324         2  464832886.0 232416443.0     99.9      return _fit_impl(datax, datay, function, **kwargs)

Total time: 0.012303 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       4200.0   4200.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        900.0    900.0      0.0      if not util.has("fmt", kwargs):
   515         1        600.0    600.0      0.0          kwargs["fmt"] = "-"
   516
   517         1        500.0    500.0      0.0      if "label" not in kwargs:
   518         1      47001.0  47001.0      0.4          kwargs = plot_kwargs(kwargs)
   519         1    5008246.0 5008246.0     40.7          kwargs["label"] = get_fnc_legend(func, args, **kwargs)
   520                                               else:
   521                                                   kwargs = plot_kwargs(kwargs)
   522
   523         1     111801.0 111801.0      0.9      xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
   524         1     410704.0 410704.0      3.3      init_plot(kwargs)
   525
   526                                               # kwargs['lpos'] = 0
   527                                               # _plot(xfit, func(xfit, *args), **kwargs)
   528         1    1895517.0 1895517.0     15.4      _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
   529         1        800.0    800.0      0.0      if kwargs["ss"]:
   530         1    4822745.0 4822745.0     39.2          save_plot(**kwargs)

Total time: 0.0931407 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       1700.0    566.7      0.0      fig = None
   810         3       8800.0   2933.3      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       5200.0   2600.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    2231720.0 1115860.0      2.4              fig = plt.figure()
   816                                                   else:
   817                                                       fig = plt.figure(figsize=kwargs["size"])
   818         2       1200.0    600.0      0.0          if kwargs["residue"]:
   819         2   89914629.0 44957314.5     96.5              fig.add_axes((0.1, 0.3, 0.8, 0.6))
   820         3      15000.0   5000.0      0.0      if util.has("xlabel", kwargs) and kwargs["xlabel"] != "":
   821                                                   plt.xlabel(kwargs["xlabel"])
   822         3       3500.0   1166.7      0.0      if util.has("ylabel", kwargs) and kwargs["ylabel"] != "":
   823                                                   plt.ylabel(kwargs["ylabel"])
   824         3       3500.0   1166.7      0.0      if util.has("xaxis", kwargs) and kwargs["xaxis"] != "":
   825         3     523004.0 174334.7      0.6          plt.xlabel(kwargs["xaxis"])
   826         3       7600.0   2533.3      0.0      if util.has("yaxis", kwargs) and kwargs["yaxis"] != "":
   827         3     416705.0 138901.7      0.4          plt.ylabel(kwargs["yaxis"])
   828         3       6600.0   2200.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        800.0    266.7      0.0      return fig

tests/test_wrap.py

test_wrap

Timer unit: 1e-09 s

Total time: 0.00750977 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       4200.0   1400.0      0.1      if isinstance(function, str):
    54                                                   l = "$" + sympy.latex(str_get_expr(function)) + "$"
    55                                               else:
    56         3       1700.0    566.7      0.0          l = function.__name__
    57         2       1200.0    600.0      0.0      if l == "<lambda>":
    58         1        300.0    300.0      0.0          try:
    59         1        300.0    300.0      0.0              try:
    60         1      84601.0  84601.0      1.1                  cc, li = inspect.findsource(function)
    61         1        200.0    200.0      0.0                  f = (
    62         2       3600.0   1800.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        500.0    250.0      0.0                      .split(",")[0]
    66         1        200.0    200.0      0.0                      .replace("\n", "")
    67                                                           )
    68         1    3854535.0 3854535.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       1700.0    850.0      0.0      elif not isinstance(function, str):
    76         1        700.0    700.0      0.0          if function.__doc__ is not None:
    77         1       2900.0   2900.0      0.0              l = function.__doc__.split("\n")[0]
    78                                                   else:
    79         1        300.0    300.0      0.0              try:
    80         1     118502.0 118502.0      1.6                  cc, li = inspect.findsource(function)
    81         1      24100.0  24100.0      0.3                  s = "".join(cc[li:]).split("return")[1].split("\n")[0]
    82         1    3408231.0 3408231.0     45.4                  l = "$" + sympy.latex(str_get_expr(s)) + "$"
    83                                                       except OSError:
    84                                                           l = function.__name__
    85
    86         3       1200.0    400.0      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.23e-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      22300.0  22300.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.2401e-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      22401.0  22401.0    100.0      return sum(f(i) for i in range(n))