Line Profiling¶
Only available on the github page: https://apn-pucky.github.io/smpl/
tests/test_animation.py¶
test_animation¶
Timer unit: 1e-09 s
Total time: 0 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: fit at line 237
Line # Hits Time Per Hit % Time Line Contents
==============================================================
237 def fit(func, *adata, **kwargs):
238 """
239 Fit and plot function to datax and datay.
240
241 Parameters
242 ----------
243 datax : array_like
244 X data either as ``unp.uarray`` or ``np.array`` or ``list``
245 datay : array_like
246 Y data either as ``unp.uarray`` or ``np.array`` or ``list``
247 function : func
248 Fit function with parameters: ``x``, ``params``
249 **kwargs : optional
250 see :func:`plot_kwargs`.
251 Fit parameters can be fixed via ``kwargs`` eg. ``a=5``.
252
253 Returns
254 -------
255 array_like
256 Optimized fit parameters of ``function`` to ``datax`` and ``datay``.
257 If ``datay`` is complex, both the real and imaginary part are returned.
258
259 Examples
260 --------
261
262 .. plot::
263 :include-source:
264
265 >>> from smpl import functions as f
266 >>> from smpl import plot
267 >>> param = plot.fit([0,1,2],[0,1,2],f.line)
268 >>> plot.unv(param).round()[0]
269 1.0
270
271 """
272 function = func
273 if "function" in kwargs:
274 function = kwargs["function"]
275 del kwargs["function"]
276 adata = [func, *adata]
277 # Fix parameter order if necessary
278 elif isinstance(function, (list, tuple, np.ndarray)):
279 adata = [adata[-1], function, *adata[:-1]]
280 function = adata[0]
281 adata = adata[1:]
282 if util.true("bins", kwargs):
283 # yvalue will be overwritten
284 ndata = [*adata, *adata]
285 for i, o in enumerate(adata):
286 ndata[2 * i] = o
287 ndata[2 * i + 1] = o * 0
288 adata = ndata
289
290 assert len(adata) % 2 == 0, "data must be pairs of x and y data"
291 if len(adata) == 2:
292 datax, datay = adata
293 else:
294 rs = []
295 for i in range(0, len(adata), 2):
296 datax, datay = adata[i], adata[i + 1]
297 if util.true("bins", kwargs):
298 rs.append(fit(function, datax, **kwargs))
299 else:
300 rs.append(fit(function, datax, datay, **kwargs))
301 return rs
302
303 kwargs = plot_kwargs(kwargs)
304
305 if np.any(np.iscomplex(datay)):
306 label = util.get("label", kwargs, "")
307 kwargs["label"] = label + "(real)"
308 r = fit(datax, datay.real, function=function, **kwargs)
309 kwargs["label"] = label + "(imag)"
310 i = fit(datax, datay.imag, function=function, **kwargs)
311 return r, i
312 if kwargs["auto_fit"]:
313 best_f, best_ff, lambda_f = ffit.auto(datax, datay, function, **kwargs)
314 if best_f is not None:
315 del kwargs["auto_fit"]
316 fit(datax, datay, best_f, **kwargs)
317 return best_f, best_ff, lambda_f
318 if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
319 kwargs["lpos"] = -1
320 return _fit_impl(datax, datay, function, **kwargs)
Total time: 7.71617 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: function at line 469
Line # Hits Time Per Hit % Time Line Contents
==============================================================
469 def function(func, *args, **kwargs):
470 """
471 Plot function ``func`` between ``xmin`` and ``xmax``
472
473 Parameters
474 ----------
475 func : function
476 Function to be plotted between ``xmin`` and ``xmax``, only taking `array_like` ``x`` as parameter
477 *args : optional
478 arguments for ``func``
479 **kwargs : optional
480 see :func:`plot_kwargs`.
481 """
482 203 985086.0 4852.6 0.0 if not util.has("xmin", kwargs) or not util.has("xmax", kwargs):
483 kwargs["xmin"], kwargs["xmax"] = stat.get_interesting_domain(func)
484 # raise Exception("xmin or xmax missing.")
485
486 # if not util.has('lpos', kwargs) and not util.has('label', kwargs):
487 # kwargs['lpos'] = -1
488 203 185400.0 913.3 0.0 if not util.has("fmt", kwargs):
489 203 95998.0 472.9 0.0 kwargs["fmt"] = "-"
490
491 203 101399.0 499.5 0.0 if "label" not in kwargs:
492 203 8946830.0 44073.1 0.1 kwargs = plot_kwargs(kwargs)
493 203 549170506.0 2705273.4 7.1 kwargs["label"] = get_fnc_legend(func, args, **kwargs)
494 else:
495 kwargs = plot_kwargs(kwargs)
496
497 203 19305935.0 95103.1 0.3 xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
498 203 191229349.0 942016.5 2.5 init_plot(kwargs)
499
500 # kwargs['lpos'] = 0
501 # _plot(xfit, func(xfit, *args), **kwargs)
502 203 6291023607.0 30990264.1 81.5 _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
503 203 224099.0 1103.9 0.0 if kwargs["ss"]:
504 203 654905252.0 3226134.2 8.5 save_plot(**kwargs)
Total time: 0.188101 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: init_plot at line 750
Line # Hits Time Per Hit % Time Line Contents
==============================================================
750 def init_plot(kwargs):
751 203 235797.0 1161.6 0.1 fig = None
752 203 635698.0 3131.5 0.3 if util.has("axes", kwargs) and kwargs["axes"] is not None:
753 plt.sca(kwargs["axes"])
754 fig = kwargs["axes"].get_figure()
755 203 108900.0 536.5 0.1 if kwargs["init"] or util.true("residue", kwargs):
756 203 129798.0 639.4 0.1 if kwargs["size"] is None:
757 203 185432997.0 913463.0 98.6 fig = plt.figure()
758 else:
759 fig = plt.figure(figsize=kwargs["size"])
760 203 250099.0 1232.0 0.1 if kwargs["residue"]:
761 fig.add_axes((0.1, 0.3, 0.8, 0.6))
762 203 495296.0 2439.9 0.3 if util.has("xlabel", kwargs) and kwargs["xlabel"] != "":
763 plt.xlabel(kwargs["xlabel"])
764 203 230596.0 1135.9 0.1 if util.has("ylabel", kwargs) and kwargs["ylabel"] != "":
765 plt.ylabel(kwargs["ylabel"])
766 203 172396.0 849.2 0.1 if util.has("xaxis", kwargs) and kwargs["xaxis"] != "":
767 plt.xlabel(kwargs["xaxis"])
768 203 152599.0 751.7 0.1 if util.has("yaxis", kwargs) and kwargs["yaxis"] != "":
769 plt.ylabel(kwargs["yaxis"])
770 203 201598.0 993.1 0.1 if util.has("next_color", kwargs) and not kwargs["next_color"]:
771 it1, it2 = itertools.tee(iter(plt.gca()._get_lines.prop_cycler))
772 plt.gca()._get_lines.prop_cycler = it2
773 tmp_color = next(it1)["color"]
774 if kwargs["data_color"] is None:
775 kwargs["data_color"] = tmp_color
776 if kwargs["fit_color"] is None:
777 kwargs["fit_color"] = tmp_color
778 if kwargs["function_color"] is None:
779 kwargs["function_color"] = tmp_color
780 203 54998.0 270.9 0.0 return fig
test_histogram¶
Timer unit: 1e-09 s
Total time: 0.0802105 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: fit at line 237
Line # Hits Time Per Hit % Time Line Contents
==============================================================
237 def fit(func, *adata, **kwargs):
238 """
239 Fit and plot function to datax and datay.
240
241 Parameters
242 ----------
243 datax : array_like
244 X data either as ``unp.uarray`` or ``np.array`` or ``list``
245 datay : array_like
246 Y data either as ``unp.uarray`` or ``np.array`` or ``list``
247 function : func
248 Fit function with parameters: ``x``, ``params``
249 **kwargs : optional
250 see :func:`plot_kwargs`.
251 Fit parameters can be fixed via ``kwargs`` eg. ``a=5``.
252
253 Returns
254 -------
255 array_like
256 Optimized fit parameters of ``function`` to ``datax`` and ``datay``.
257 If ``datay`` is complex, both the real and imaginary part are returned.
258
259 Examples
260 --------
261
262 .. plot::
263 :include-source:
264
265 >>> from smpl import functions as f
266 >>> from smpl import plot
267 >>> param = plot.fit([0,1,2],[0,1,2],f.line)
268 >>> plot.unv(param).round()[0]
269 1.0
270
271 """
272 2 3100.0 1550.0 0.0 function = func
273 2 1900.0 950.0 0.0 if "function" in kwargs:
274 2 900.0 450.0 0.0 function = kwargs["function"]
275 2 1000.0 500.0 0.0 del kwargs["function"]
276 2 2100.0 1050.0 0.0 adata = [func, *adata]
277 # Fix parameter order if necessary
278 elif isinstance(function, (list, tuple, np.ndarray)):
279 adata = [adata[-1], function, *adata[:-1]]
280 function = adata[0]
281 adata = adata[1:]
282 2 10900.0 5450.0 0.0 if util.true("bins", kwargs):
283 # yvalue will be overwritten
284 ndata = [*adata, *adata]
285 for i, o in enumerate(adata):
286 ndata[2 * i] = o
287 ndata[2 * i + 1] = o * 0
288 adata = ndata
289
290 2 3000.0 1500.0 0.0 assert len(adata) % 2 == 0, "data must be pairs of x and y data"
291 2 1000.0 500.0 0.0 if len(adata) == 2:
292 2 1700.0 850.0 0.0 datax, datay = adata
293 else:
294 rs = []
295 for i in range(0, len(adata), 2):
296 datax, datay = adata[i], adata[i + 1]
297 if util.true("bins", kwargs):
298 rs.append(fit(function, datax, **kwargs))
299 else:
300 rs.append(fit(function, datax, datay, **kwargs))
301 return rs
302
303 2 32100.0 16050.0 0.0 kwargs = plot_kwargs(kwargs)
304
305 2 89099.0 44549.5 0.1 if np.any(np.iscomplex(datay)):
306 label = util.get("label", kwargs, "")
307 kwargs["label"] = label + "(real)"
308 r = fit(datax, datay.real, function=function, **kwargs)
309 kwargs["label"] = label + "(imag)"
310 i = fit(datax, datay.imag, function=function, **kwargs)
311 return r, i
312 2 1400.0 700.0 0.0 if kwargs["auto_fit"]:
313 best_f, best_ff, lambda_f = ffit.auto(datax, datay, function, **kwargs)
314 if best_f is not None:
315 del kwargs["auto_fit"]
316 fit(datax, datay, best_f, **kwargs)
317 return best_f, best_ff, lambda_f
318 2 1800.0 900.0 0.0 if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
319 2 1200.0 600.0 0.0 kwargs["lpos"] = -1
320 2 80059275.0 40029637.5 99.8 return _fit_impl(datax, datay, function, **kwargs)
Total time: 0.193241 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: function at line 469
Line # Hits Time Per Hit % Time Line Contents
==============================================================
469 def function(func, *args, **kwargs):
470 """
471 Plot function ``func`` between ``xmin`` and ``xmax``
472
473 Parameters
474 ----------
475 func : function
476 Function to be plotted between ``xmin`` and ``xmax``, only taking `array_like` ``x`` as parameter
477 *args : optional
478 arguments for ``func``
479 **kwargs : optional
480 see :func:`plot_kwargs`.
481 """
482 2 6300.0 3150.0 0.0 if not util.has("xmin", kwargs) or not util.has("xmax", kwargs):
483 kwargs["xmin"], kwargs["xmax"] = stat.get_interesting_domain(func)
484 # raise Exception("xmin or xmax missing.")
485
486 # if not util.has('lpos', kwargs) and not util.has('label', kwargs):
487 # kwargs['lpos'] = -1
488 2 1800.0 900.0 0.0 if not util.has("fmt", kwargs):
489 2 1300.0 650.0 0.0 kwargs["fmt"] = "-"
490
491 2 1100.0 550.0 0.0 if "label" not in kwargs:
492 kwargs = plot_kwargs(kwargs)
493 kwargs["label"] = get_fnc_legend(func, args, **kwargs)
494 else:
495 2 83100.0 41550.0 0.0 kwargs = plot_kwargs(kwargs)
496
497 2 172498.0 86249.0 0.1 xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
498 2 46800.0 23400.0 0.0 init_plot(kwargs)
499
500 # kwargs['lpos'] = 0
501 # _plot(xfit, func(xfit, *args), **kwargs)
502 2 2859789.0 1429894.5 1.5 _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
503 2 1800.0 900.0 0.0 if kwargs["ss"]:
504 2 190066427.0 95033213.5 98.4 save_plot(**kwargs)
Total time: 0.0625349 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: init_plot at line 750
Line # Hits Time Per Hit % Time Line Contents
==============================================================
750 def init_plot(kwargs):
751 4 16300.0 4075.0 0.0 fig = None
752 4 7300.0 1825.0 0.0 if util.has("axes", kwargs) and kwargs["axes"] is not None:
753 plt.sca(kwargs["axes"])
754 fig = kwargs["axes"].get_figure()
755 2 5200.0 2600.0 0.0 if kwargs["init"] or util.true("residue", kwargs):
756 2 800.0 400.0 0.0 if kwargs["size"] is None:
757 2 1825492.0 912746.0 2.9 fig = plt.figure()
758 else:
759 fig = plt.figure(figsize=kwargs["size"])
760 2 1600.0 800.0 0.0 if kwargs["residue"]:
761 fig.add_axes((0.1, 0.3, 0.8, 0.6))
762 2 3800.0 1900.0 0.0 if util.has("xlabel", kwargs) and kwargs["xlabel"] != "":
763 2 60466455.0 30233227.5 96.7 plt.xlabel(kwargs["xlabel"])
764 2 8400.0 4200.0 0.0 if util.has("ylabel", kwargs) and kwargs["ylabel"] != "":
765 2 187699.0 93849.5 0.3 plt.ylabel(kwargs["ylabel"])
766 4 4600.0 1150.0 0.0 if util.has("xaxis", kwargs) and kwargs["xaxis"] != "":
767 plt.xlabel(kwargs["xaxis"])
768 4 3000.0 750.0 0.0 if util.has("yaxis", kwargs) and kwargs["yaxis"] != "":
769 plt.ylabel(kwargs["yaxis"])
770 4 3100.0 775.0 0.0 if util.has("next_color", kwargs) and not kwargs["next_color"]:
771 it1, it2 = itertools.tee(iter(plt.gca()._get_lines.prop_cycler))
772 plt.gca()._get_lines.prop_cycler = it2
773 tmp_color = next(it1)["color"]
774 if kwargs["data_color"] is None:
775 kwargs["data_color"] = tmp_color
776 if kwargs["fit_color"] is None:
777 kwargs["fit_color"] = tmp_color
778 if kwargs["function_color"] is None:
779 kwargs["function_color"] = tmp_color
780 4 1200.0 300.0 0.0 return fig
test_subplots¶
Timer unit: 1e-09 s
Total time: 0 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: fit at line 237
Line # Hits Time Per Hit % Time Line Contents
==============================================================
237 def fit(func, *adata, **kwargs):
238 """
239 Fit and plot function to datax and datay.
240
241 Parameters
242 ----------
243 datax : array_like
244 X data either as ``unp.uarray`` or ``np.array`` or ``list``
245 datay : array_like
246 Y data either as ``unp.uarray`` or ``np.array`` or ``list``
247 function : func
248 Fit function with parameters: ``x``, ``params``
249 **kwargs : optional
250 see :func:`plot_kwargs`.
251 Fit parameters can be fixed via ``kwargs`` eg. ``a=5``.
252
253 Returns
254 -------
255 array_like
256 Optimized fit parameters of ``function`` to ``datax`` and ``datay``.
257 If ``datay`` is complex, both the real and imaginary part are returned.
258
259 Examples
260 --------
261
262 .. plot::
263 :include-source:
264
265 >>> from smpl import functions as f
266 >>> from smpl import plot
267 >>> param = plot.fit([0,1,2],[0,1,2],f.line)
268 >>> plot.unv(param).round()[0]
269 1.0
270
271 """
272 function = func
273 if "function" in kwargs:
274 function = kwargs["function"]
275 del kwargs["function"]
276 adata = [func, *adata]
277 # Fix parameter order if necessary
278 elif isinstance(function, (list, tuple, np.ndarray)):
279 adata = [adata[-1], function, *adata[:-1]]
280 function = adata[0]
281 adata = adata[1:]
282 if util.true("bins", kwargs):
283 # yvalue will be overwritten
284 ndata = [*adata, *adata]
285 for i, o in enumerate(adata):
286 ndata[2 * i] = o
287 ndata[2 * i + 1] = o * 0
288 adata = ndata
289
290 assert len(adata) % 2 == 0, "data must be pairs of x and y data"
291 if len(adata) == 2:
292 datax, datay = adata
293 else:
294 rs = []
295 for i in range(0, len(adata), 2):
296 datax, datay = adata[i], adata[i + 1]
297 if util.true("bins", kwargs):
298 rs.append(fit(function, datax, **kwargs))
299 else:
300 rs.append(fit(function, datax, datay, **kwargs))
301 return rs
302
303 kwargs = plot_kwargs(kwargs)
304
305 if np.any(np.iscomplex(datay)):
306 label = util.get("label", kwargs, "")
307 kwargs["label"] = label + "(real)"
308 r = fit(datax, datay.real, function=function, **kwargs)
309 kwargs["label"] = label + "(imag)"
310 i = fit(datax, datay.imag, function=function, **kwargs)
311 return r, i
312 if kwargs["auto_fit"]:
313 best_f, best_ff, lambda_f = ffit.auto(datax, datay, function, **kwargs)
314 if best_f is not None:
315 del kwargs["auto_fit"]
316 fit(datax, datay, best_f, **kwargs)
317 return best_f, best_ff, lambda_f
318 if kwargs["also_fit"] == False and kwargs["label"] is None and kwargs["lpos"] == 0:
319 kwargs["lpos"] = -1
320 return _fit_impl(datax, datay, function, **kwargs)
Total time: 89.1861 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: function at line 469
Line # Hits Time Per Hit % Time Line Contents
==============================================================
469 def function(func, *args, **kwargs):
470 """
471 Plot function ``func`` between ``xmin`` and ``xmax``
472
473 Parameters
474 ----------
475 func : function
476 Function to be plotted between ``xmin`` and ``xmax``, only taking `array_like` ``x`` as parameter
477 *args : optional
478 arguments for ``func``
479 **kwargs : optional
480 see :func:`plot_kwargs`.
481 """
482 1200 5179989.0 4316.7 0.0 if not util.has("xmin", kwargs) or not util.has("xmax", kwargs):
483 kwargs["xmin"], kwargs["xmax"] = stat.get_interesting_domain(func)
484 # raise Exception("xmin or xmax missing.")
485
486 # if not util.has('lpos', kwargs) and not util.has('label', kwargs):
487 # kwargs['lpos'] = -1
488 1200 1295697.0 1079.7 0.0 if not util.has("fmt", kwargs):
489 1200 740298.0 616.9 0.0 kwargs["fmt"] = "-"
490
491 1200 575797.0 479.8 0.0 if "label" not in kwargs:
492 1200 57287843.0 47739.9 0.1 kwargs = plot_kwargs(kwargs)
493 1200 3360027084.0 2800022.6 3.8 kwargs["label"] = get_fnc_legend(func, args, **kwargs)
494 else:
495 kwargs = plot_kwargs(kwargs)
496
497 1200 111865764.0 93221.5 0.1 xlin = kwargs["xspace"](kwargs["xmin"], kwargs["xmax"], kwargs["steps"])
498 1200 80932896.0 67444.1 0.1 init_plot(kwargs)
499
500 # kwargs['lpos'] = 0
501 # _plot(xfit, func(xfit, *args), **kwargs)
502 1200 1667285762.0 1389404.8 1.9 _function(wrap.get_lambda_argd(func, kwargs["xvar"], *args), xlin, **kwargs)
503 1200 1153798.0 961.5 0.0 if kwargs["ss"]:
504 1200 83899795950.0 69916496.6 94.1 save_plot(**kwargs)
Total time: 0.0664021 s
File: /github/home/.cache/pypoetry/virtualenvs/smpl-animation-VRc_1PH3-py3.8/lib/python3.8/site-packages/smpl/plot.py
Function: init_plot at line 750
Line # Hits Time Per Hit % Time Line Contents
==============================================================
750 def init_plot(kwargs):
751 1200 1416099.0 1180.1 2.1 fig = None
752 1200 4308489.0 3590.4 6.5 if util.has("axes", kwargs) and kwargs["axes"] is not None:
753 1200 45193592.0 37661.3 68.1 plt.sca(kwargs["axes"])
754 1200 4305788.0 3588.2 6.5 fig = kwargs["axes"].get_figure()
755 1200 3502587.0 2918.8 5.3 if kwargs["init"] or util.true("residue", kwargs):
756 if kwargs["size"] is None:
757 fig = plt.figure()
758 else:
759 fig = plt.figure(figsize=kwargs["size"])
760 if kwargs["residue"]:
761 fig.add_axes((0.1, 0.3, 0.8, 0.6))
762 1200 1493598.0 1244.7 2.2 if util.has("xlabel", kwargs) and kwargs["xlabel"] != "":
763 plt.xlabel(kwargs["xlabel"])
764 1200 2100294.0 1750.2 3.2 if util.has("ylabel", kwargs) and kwargs["ylabel"] != "":
765 plt.ylabel(kwargs["ylabel"])
766 1200 1199096.0 999.2 1.8 if util.has("xaxis", kwargs) and kwargs["xaxis"] != "":
767 plt.xlabel(kwargs["xaxis"])
768 1200 1021899.0 851.6 1.5 if util.has("yaxis", kwargs) and kwargs["yaxis"] != "":
769 plt.ylabel(kwargs["yaxis"])
770 1200 1366297.0 1138.6 2.1 if util.has("next_color", kwargs) and not kwargs["next_color"]:
771 it1, it2 = itertools.tee(iter(plt.gca()._get_lines.prop_cycler))
772 plt.gca()._get_lines.prop_cycler = it2
773 tmp_color = next(it1)["color"]
774 if kwargs["data_color"] is None:
775 kwargs["data_color"] = tmp_color
776 if kwargs["fit_color"] is None:
777 kwargs["fit_color"] = tmp_color
778 if kwargs["function_color"] is None:
779 kwargs["function_color"] = tmp_color
780 1200 494399.0 412.0 0.7 return fig