{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "# High Performance Python\n", "\n", "Python is slower than compiled languages for a variety of reasons:\n", "\n", "### Python is Dynamically Typed rather than Statically Typed.\n", "\n", "What this means is that at the time the program executes, the interpreter doesn't know the type of the variables that are defined. For example, the difference between a C variable and a Python variable is summarized by this diagram:\n", "\n", "![](http://jakevdp.github.io/images/cint_vs_pyint.png)\n", "\n", "For a variable in C, the compiler knows the type by its very definition. For a variable in Python, all you know at the time the program executes is that it's some sort of Python object.\n", "\n", "So if you write the following in C:\n", "\n", "C\n", "int a = 1;\n", "int b = 2;\n", "int c = a + b;\n", "\n", "\n", "the C compiler knows from the start that a and b are integers: they simply can't be anything else! With this knowledge, it can call the routine which adds two integers, returning another integer which is just a simple value in memory. As a rough schematic, the sequence of events looks like this:\n", "\n", "**C Addition**\n", "\n", "1. Assign 1 to a\n", "2. Assign 2 to b\n", "3. call binary_add(a, b)\n", "4. Assign the result to c\n", "\n", "The equivalent code in Python looks like this:\n", "\n", "python\n", "a = 1\n", "b = 2\n", "c = a + b\n", "\n", "\n", "here the interpreter knows only that 1 and 2 are objects, but not what type of object they are. So the The interpreter must inspect PyObject_HEAD for each variable to find the type information, and then call the appropriate summation routine for the two types. Finally it must create and initialize a new Python object to hold the return value. The sequence of events looks roughly like this:\n", "\n", "**Python Addition**\n", "\n", "1. Assign 1 to a\n", " - Set a->PyObject_HEAD->typecode to integer\n", " - Set a->val = 1\n", "2. Assign 2 to b\n", " - Set b->PyObject_HEAD->typecode to integer\n", " - Set b->val = 2\n", "3. call binary_add(a, b)\n", " - find typecode in a->PyObject_HEAD\n", " - a is an integer; value is a->val\n", " - find typecode in b->PyObject_HEAD\n", " - b is an integer; value is b->val\n", " - call binary_add(a->val, b->val)\n", " - result of this is result, and is an integer.\n", "4. Create a Python object c\n", " - set c->PyObject_HEAD->typecode to integer\n", " - set c->val to result\n", "\n", "The dynamic typing means that there are a lot more steps involved with any operation. This is a primary reason that Python is slow compared to C for operations on numerical data.\n", "\n", "### Python is interpreted rather than compiled.\n", "\n", "We saw above one difference between interpreted and compiled code. A smart compiler can look ahead and optimize for repeated or unneeded operations, which can result in speed-ups. Compiler optimization is its own beast, and I'm personally not qualified to say much about it, so I'll stop there. \n", "\n", "### Python's object model can lead to inefficient memory access\n", "\n", "We saw above the extra type info layer when moving from a C integer to a Python integer. Now imagine you have many such integers and want to do some sort of batch operation on them. In Python you might use the standard List object, while in C you would likely use some sort of buffer-based array.\n", "\n", "A NumPy array in its simplest form is a Python object build around a C array. That is, it has a pointer to a contiguous data buffer of values. A Python list, on the other hand, has a pointer to a contiguous buffer of pointers, each of which points to a Python object which in turn has references to its data (in this case, integers). This is a schematic of what the two might look like:\n", "\n", "![](http://jakevdp.github.io/images/array_vs_list.png)\n", "\n", "You can see that if you're doing some operation which steps through data in sequence, the numpy layout will be much more efficient than the Python layout, both in the cost of storage and the cost of access." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Speeding up statistical computations in Python\n", "\n", "In the age of \"big data\" and sophisitcated Bayesian and statistical learning algorithms, many are interested in optimizing the performance of the high-level languages that we use to analyse data.\n", "\n", "[NumPy](http://numpy.scipy.org/) gets us part of the way there on Python:\n", "\n", "* Storage of multidimensional data\n", "* Efficient data access\n", "* Efficient in-memory storage\n", "* Fast methods and functions for data manipulation\n", "\n", "Ffor many applications, this is sufficient to drastically improve performance. However, there is plenty of scope for improving Python's performance in situations where speed matters.\n", "\n", "Pure Python and Python with NumPy are not particularly fast. Below are some recent performance benchmarks comparing several computing languages (taken directly from the [Julia website](http://julialang.org)):\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Fortran Julia Python R Matlab Octave Mathematica JavaScript Go LuaJIT Java fib gcc 5.1.1\n", " 0.4.0\n", " 3.4.3\n", " 3.2.2\n", " R2015b\n", " 4.0.0\n", " 10.2.0\n", " V8 3.28.71.19\n", " go1.5\n", " gsl-shell 2.3.1\n", " 1.8.0_45\n", " 0.70 2.11 77.76 533.52 26.89 9324.35 118.53 3.36 1.86 1.71 1.21 5.05 1.45 17.02 45.73 802.52 9581.44 15.02 6.06 1.20 5.77 3.35 1.31 1.15 32.89 264.54 4.92 1866.01 43.23 2.70 1.29 2.03 2.60 0.81 0.79 15.32 53.16 7.58 451.81 5.13 0.66 1.11 0.67 1.35 1.00 1.00 21.99 9.56 1.00 299.31 1.69 1.01 1.00 1.00 1.00 1.45 1.66 17.93 14.56 14.52 30.93 5.95 2.30 2.96 3.27 3.92 3.48 1.02 1.14 1.57 1.12 1.12 1.30 15.07 1.42 1.16 2.36
\n", "\n", "

Figure:\n", "benchmark times relative to C (smaller is better, C performance = 1.0).\n", "

\n", "
\n", "\n", "So, while fast relative to some scientific compution choices (*e.g.* R, Matlab), Python sometimes needs to be tweaked in order to make it a competitive choice for implementing modern statistical methods. We will cover two approachable ways of improving the performance of Python.\n", "\n", "> One does have to be careful when interpreting benchmarks. They involve many assumptions about how algorithms are implemented that may not be applicable to real-world applications in a particular language. \n", "\n", "\n", "## Profiling\n", "\n", "Before you barrel ahead and prematurely optimize your Python code, it is important to understand **why** and **where** your code is slow. This is achieved by systematically accounting for the resources that your code is using, such as memory, CPU time or data transfer. This process is broadly referred to as ***Profiling***, and it allows you to identify where the performance bottlenecks in your code lie.\n", "\n", "Here, we will concentrate on optimizing performance for **CPU-bound** problems.\n", "\n", "There are a number of tools to help you profile your code.\n", "\n", "### time\n", "\n", "For those of you on UNIX platforms, the built-in utility time can be used to assess how long your code takes to run." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/bin/sh: 1: time: not found\n" ] } ], "source": [ "!time python ../examples/abc.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output from time can be interpreted as:\n", "\n", "* real: elapsed (wall) time\n", "* user: time spent in your code\n", "* sys: time spent in system (kernel) functions\n", "\n", "The last 2 quantities account for the cycles used by your program. The remaining real time is often due to waiting for information either from disk or a network connection (I/O).\n", "\n", "Python also has a time module (and function) that is more rudimentary; it simply returns the time, in seconds from the Epoch (1/1/1970)." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1545190399.5904741" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import time\n", "time.time()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use this for profiling by differencing the times before and after running some code of interest:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.03713417053222656" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "start_time = time.time()\n", "np.product(range(1, 100000))\n", "end_time = time.time()\n", "\n", "end_time - start_time" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note, however that it does not provide a breakdown of where the code spends its time.\n", "\n", "### IPython magic: %timeit, %run and %prun\n", "\n", "IPython has three built-in \"magic\" functions that are useful for profiling your code. \n", "\n", "The %timeit magic executes a Python statement or expressions in a loop to see how long we expect it to take for any given call. Additionally, it repeats the loop a certain number of times, and returns the best result.\n", "\n", "As an example, consider a Python implementation of the **trapezoidal rule**, a method from numerical analysis for approximating a definite integral. Specifically, it allows us to approximate:\n", "\n", "$$\\int_a^b f(x) dx$$\n", "\n", "using the approximation:\n", "\n", "$$\\int_a^b f(x) dx \\approx (b-a) \\frac{f(b) + f(a)}{2}$$\n", "\n", "Rather than use a single interval for this estimate, we break the interval down into $n$ subintervals, to obtain a more accurate approximation." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def f(x):\n", " return 2*x*x + 3*x + 1\n", " \n", "def trapez(a, b, n):\n", " h = (b-a)/n \n", " sumy = 0\n", " x = a\n", " for i in range(n):\n", " x += h\n", " sumy += f(x)\n", " sumy += 0.5*(f(a) + f(b))\n", " return sumy*h" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "122.6930668799846" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "trapez(1, 5, 10000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To confirm that this works, we can compare this to the symbolic solution, using Sympy:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "122.666666666667" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sympy as sym\n", "\n", "xs = sym.symbols('xs')\n", "\n", "fx = 2*xs*xs + 3*xs + 1\n", "\n", "ifx = sym.integrate(fx, (xs, 1, 5))\n", "ifx.evalf()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3.25 ms ± 338 µs per loop (mean ± std. dev. of 5 runs, 50 loops each)\n" ] } ], "source": [ "%timeit -n 50 -r 5 trapez(1, 5, 10000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "%timeit tries to pick suitable values for the number of loops and repeats; these values can be overriden by specifying -n and -r values, respectively.\n", "\n", "The %run command with a -p option allows you to run complete programs under the control of the Python profiler. It writes the output to the help pane, which opens at the bottom of the page." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# This code redirects pager output to a regular cell\n", "from IPython.core import page\n", "page.page = print" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mu 4.617512\n", "sigma 1.699296\n", "dtype: float64\n", " 1748732 function calls (1744952 primitive calls) in 6.083 seconds\n", "\n", " Ordered by: internal time\n", "\n", " ncalls tottime percall cumtime percall filename:lineno(function)\n", " 48742 1.228 0.000 2.467 0.000 _methods.py:91(_var)\n", " 146233 0.594 0.000 0.594 0.000 {method 'reduce' of 'numpy.ufunc' objects}\n", " 48742 0.476 0.000 1.236 0.000 _methods.py:58(_mean)\n", " 48742 0.389 0.000 2.951 0.000 _methods.py:138(_std)\n", " 97484 0.369 0.000 0.458 0.000 _methods.py:48(_count_reduce_items)\n", " 48743 0.320 0.000 0.320 0.000 {method 'normal' of 'mtrand.RandomState' objects}\n", " 1 0.305 0.305 5.157 5.157 abc.py:4(abc)\n", "299336/299215 0.304 0.000 0.312 0.000 {built-in method builtins.isinstance}\n", " 146226 0.256 0.000 0.432 0.000 numeric.py:504(asanyarray)\n", "197736/197604 0.191 0.000 0.201 0.000 {built-in method builtins.issubclass}\n", " 146235 0.176 0.000 0.176 0.000 {built-in method numpy.core.multiarray.array}\n", " 149840 0.162 0.000 0.162 0.000 {built-in method builtins.hasattr}\n", " 48742 0.108 0.000 3.059 0.000 {method 'std' of 'numpy.ndarray' objects}\n", " 48742 0.108 0.000 1.343 0.000 {method 'mean' of 'numpy.ndarray' objects}\n", " 170 0.075 0.000 0.075 0.000 {built-in method marshal.loads}\n", " 48805 0.072 0.000 0.072 0.000 {built-in method builtins.max}\n", " 213 0.058 0.000 0.062 0.000 :318(__exit__)\n", " 24371 0.056 0.000 0.056 0.000 {method 'uniform' of 'mtrand.RandomState' objects}\n", " 48742 0.047 0.000 0.047 0.000 {built-in method builtins.abs}\n", " 43/27 0.046 0.001 0.134 0.005 {built-in method _imp.create_dynamic}\n", " 613 0.034 0.000 0.125 0.000 textwrap.py:414(dedent)\n", " 883 0.033 0.000 0.033 0.000 {method 'sub' of '_sre.SRE_Pattern' objects}\n", " 613/593 0.033 0.000 0.203 0.000 {built-in method builtins.__build_class__}\n", "28817/28680 0.031 0.000 0.031 0.000 {built-in method builtins.len}\n", " 18015 0.030 0.000 0.030 0.000 {method 'startswith' of 'str' objects}\n", " 613 0.025 0.000 0.025 0.000 {method 'findall' of '_sre.SRE_Pattern' objects}\n", " 958 0.019 0.000 0.019 0.000 {built-in method posix.stat}\n", " 170 0.015 0.000 0.015 0.000 {method 'read' of '_io.FileIO' objects}\n", " 420 0.015 0.000 0.072 0.000 :1233(find_spec)\n", " 149/58 0.011 0.000 0.035 0.001 sre_parse.py:470(_parse)\n", " 2129 0.011 0.000 0.031 0.000 :57(_path_join)\n", " 333 0.011 0.000 0.109 0.000 :870(_find_spec)\n", " 170 0.011 0.000 0.025 0.000 :830(get_data)\n", " 2129 0.010 0.000 0.017 0.000 :59()\n", " 4126 0.010 0.000 0.010 0.000 {built-in method builtins.getattr}\n", " 399/2 0.009 0.000 0.923 0.461 :966(_find_and_load)\n", " 209/1 0.008 0.000 6.084 6.084 {built-in method builtins.exec}\n", " 765 0.007 0.000 0.014 0.000 :157(_get_module_lock)\n", " 4601 0.007 0.000 0.007 0.000 {method 'rstrip' of 'str' objects}\n", "3167/3163 0.007 0.000 0.010 0.000 {method 'join' of 'str' objects}\n", "1246/1035 0.006 0.000 0.366 0.000 :997(_handle_fromlist)\n", " 399/2 0.006 0.000 0.922 0.461 :936(_find_and_load_unlocked)\n", " 765 0.006 0.000 0.007 0.000 :78(acquire)\n", " 261/53 0.006 0.000 0.018 0.000 sre_compile.py:64(_compile)\n", " 430 0.006 0.000 0.019 0.000 copy.py:66(copy)\n", " 170 0.005 0.000 0.008 0.000 :430(_validate_bytecode_header)\n", " 765 0.005 0.000 0.007 0.000 :103(release)\n", " 4367 0.005 0.000 0.005 0.000 {method 'append' of 'list' objects}\n", " 547 0.005 0.000 0.122 0.000 _decorators.py:300(__call__)\n", " 900 0.005 0.000 0.035 0.000 __init__.py:829(__setitem__)\n", " 340 0.005 0.000 0.016 0.000 :263(cache_from_source)\n", " 333 0.005 0.000 0.084 0.000 :1117(_get_spec)\n", " 213 0.005 0.000 0.023 0.000 :504(_init_module_attrs)\n", " 170 0.005 0.000 0.131 0.001 :743(get_code)\n", " 2273 0.005 0.000 0.005 0.000 :222(_verbose_message)\n", " 1113 0.005 0.000 0.005 0.000 {method 'format' of 'str' objects}\n", " 213/1 0.005 0.000 0.921 0.921 :651(_load_unlocked)\n", " 1543 0.004 0.000 0.004 0.000 {built-in method builtins.setattr}\n", " 677 0.004 0.000 0.072 0.000 re.py:286(_compile)\n", " 1910 0.004 0.000 0.004 0.000 {method 'rpartition' of 'str' objects}\n", " 1769 0.004 0.000 0.004 0.000 sre_parse.py:232(__next)\n", " 1391 0.004 0.000 0.007 0.000 sre_parse.py:253(get)\n", " 2279 0.003 0.000 0.003 0.000 {built-in method _imp.acquire_lock}\n", " 2026 0.003 0.000 0.003 0.000 {method 'get' of 'dict' objects}\n", " 429 0.003 0.000 0.008 0.000 copy.py:268(_reconstruct)\n", " 2279 0.003 0.000 0.003 0.000 {built-in method _imp.release_lock}\n", " 126 0.003 0.000 0.006 0.000 expr.py:152()\n", " 1119 0.003 0.000 0.005 0.000 :847(__exit__)\n", " 982 0.003 0.000 0.005 0.000 sre_parse.py:163(__getitem__)\n", " 925 0.003 0.000 0.021 0.000 :75(_path_stat)\n", " 395 0.003 0.000 0.005 0.000 :176(cb)\n", " 1119 0.003 0.000 0.005 0.000 :843(__enter__)\n", " 122 0.003 0.000 0.005 0.000 sre_compile.py:250(_optimize_charset)\n", " 366 0.003 0.000 0.012 0.000 :194(_lock_unlock_module)\n", " 399/192 0.003 0.000 0.004 0.000 sre_parse.py:173(getwidth)\n", " 112/14 0.003 0.000 0.008 0.001 abc.py:196(__subclasscheck__)\n", " 188/7 0.003 0.000 0.010 0.001 pyparsing.py:1380(_parseNoCache)\n", " 107 0.003 0.000 0.005 0.000 functools.py:44(update_wrapper)\n", " 1530 0.002 0.000 0.002 0.000 {built-in method _thread.get_ident}\n", " 213 0.002 0.000 0.004 0.000 :524(spec_from_file_location)\n", " 395 0.002 0.000 0.004 0.000 :58(__init__)\n", " 170 0.002 0.000 0.079 0.000 :485(_compile_bytecode)\n", " 1 0.002 0.002 0.005 0.005 __init__.py:978(_rc_params_in_file)\n", " 399 0.002 0.000 0.016 0.000 :147(__enter__)\n", " 5 0.002 0.000 0.042 0.008 _collections_abc.py:824(update)\n", " 68 0.002 0.000 0.018 0.000 ops.py:1486(_arith_method_FRAME)\n", " 1274 0.002 0.000 0.002 0.000 {method 'strip' of 'str' objects}\n", " 213/190 0.002 0.000 0.159 0.001 :564(module_from_spec)\n", " 1 0.002 0.002 0.002 0.002 {method 'readlines' of '_io._IOBase' objects}\n", " 115/53 0.002 0.000 0.036 0.001 sre_parse.py:407(_parse_sub)\n", " 1206 0.002 0.000 0.002 0.000 sre_parse.py:248(match)\n", " 29 0.002 0.000 0.002 0.000 {built-in method posix.listdir}\n", " 235 0.002 0.000 0.003 0.000 {built-in method builtins.any}\n", " 213 0.002 0.000 0.011 0.000 :361(_get_cached)\n", " 340 0.002 0.000 0.003 0.000 :63(_path_split)\n", " 170/1 0.002 0.000 0.921 0.921 :672(exec_module)\n", " 232 0.002 0.000 0.002 0.000 pyparsing.py:1154(__init__)\n", " 399 0.002 0.000 0.006 0.000 :151(__exit__)\n", " 11 0.002 0.000 0.008 0.001 ops.py:861(add_methods)\n", " 619 0.002 0.000 0.002 0.000 {method 'update' of 'dict' objects}\n", " 429 0.002 0.000 0.021 0.000 pyparsing.py:1177(copy)\n", " 547 0.002 0.000 0.002 0.000 _decorators.py:293(__init__)\n", " 158 0.002 0.000 0.002 0.000 _decorators.py:247(__call__)\n", " 213 0.002 0.000 0.006 0.000 :1228(_get_spec)\n", " 587 0.002 0.000 0.002 0.000 {built-in method __new__ of type object at 0x7c1b3b1589e0}\n", " 265 0.002 0.000 0.017 0.000 re.py:184(sub)\n", " 11 0.002 0.000 0.041 0.004 ops.py:800(_create_methods)\n", " 335 0.002 0.000 0.009 0.000 :85(_path_is_mode_type)\n", " 333 0.002 0.000 0.086 0.000 :1149(find_spec)\n", " 3 0.001 0.000 0.003 0.001 expr.py:216(disallowed)\n", " 263 0.001 0.000 0.009 0.000 abc.py:180(__instancecheck__)\n", " 790 0.001 0.000 0.001 0.000 {built-in method _thread.allocate_lock}\n", " 442 0.001 0.000 0.003 0.000 generic.py:7(_check)\n", " 354 0.001 0.000 0.006 0.000 re.py:169(match)\n", " 440 0.001 0.000 0.004 0.000 :1080(_path_importer_cache)\n", " 429 0.001 0.000 0.002 0.000 {method '__reduce_ex__' of 'object' objects}\n", " 364/2 0.001 0.000 0.921 0.461 :211(_call_with_frames_removed)\n", " 383 0.001 0.000 0.012 0.000 :403(cached)\n", " 694 0.001 0.000 0.001 0.000 {method 'split' of 'str' objects}\n", " 5 0.001 0.000 0.002 0.000 textwrap.py:233(_wrap_chunks)\n", " 541 0.001 0.000 0.001 0.000 _weakrefset.py:70(__contains__)\n", " 53 0.001 0.000 0.006 0.000 sre_compile.py:482(_compile_info)\n", " 248 0.001 0.000 0.002 0.000 ops.py:326(_get_op_name)\n", " 53 0.001 0.000 0.065 0.001 sre_compile.py:557(compile)\n", " 122 0.001 0.000 0.007 0.000 sre_compile.py:223(_compile_charset)\n", " 83 0.001 0.000 0.004 0.000 pyparsing.py:3326(__init__)\n", " 635 0.001 0.000 0.001 0.000 {built-in method builtins.min}\n", " 333 0.001 0.000 0.001 0.000 :369(__init__)\n", " 429 0.001 0.000 0.002 0.000 copyreg.py:87(__newobj__)\n", " 852 0.001 0.000 0.001 0.000 :321()\n", " 108 0.001 0.000 0.009 0.000 rcsetup.py:358(validate_color)\n", " 340 0.001 0.000 0.002 0.000 :52(_r_long)\n", " 414 0.001 0.000 0.002 0.000 sre_parse.py:171(append)\n", " 170 0.001 0.000 0.001 0.000 {built-in method _imp._fix_co_filename}\n", " 939 0.001 0.000 0.001 0.000 {built-in method builtins.ord}\n", " 251 0.001 0.000 0.002 0.000 :416(parent)\n", " 389 0.001 0.000 0.001 0.000 {method 'match' of '_sre.SRE_Pattern' objects}\n", " 351 0.001 0.000 0.002 0.000 __init__.py:217(bind_method)\n", " 22 0.001 0.000 0.007 0.000 traceback.py:319(extract)\n", " 133 0.001 0.000 0.002 0.000 ops.py:274(_get_opstr)\n", " 306 0.001 0.000 0.009 0.000 :94(_path_isfile)\n", " 1 0.001 0.001 0.014 0.014 expr.py:2()\n", " 333 0.001 0.000 0.002 0.000 :780(find_spec)\n", " 48 0.001 0.000 0.003 0.000 config.py:414(register_option)\n", " 106 0.001 0.000 0.003 0.000 enum.py:803(__and__)\n", " 192 0.001 0.000 0.004 0.000 colors.py:116(_is_nth_color)\n", " 381 0.001 0.000 0.001 0.000 sre_parse.py:159(__len__)\n", " 351 0.001 0.000 0.001 0.000 sre_parse.py:285(tell)\n", " 23 0.001 0.000 0.001 0.000 generic.py:6(create_pandas_abc_type)\n", " 231 0.001 0.000 0.002 0.000 enum.py:267(__call__)\n", " 568 0.001 0.000 0.001 0.000 {built-in method posix.fspath}\n", " 399 0.001 0.000 0.001 0.000 :143(__init__)\n", " 113/42 0.001 0.000 0.629 0.015 {built-in method builtins.__import__}\n", " 1 0.001 0.001 0.126 0.126 rcsetup.py:15()\n", " 87 0.001 0.000 0.011 0.000 rcsetup.py:69(f)\n", " 403 0.001 0.000 0.001 0.000 {method 'replace' of 'str' objects}\n", " 170 0.001 0.000 0.001 0.000 :393(_check_name_wrapper)\n", " 214 0.001 0.000 0.001 0.000 rcsetup.py:118(validate_bool)\n", " 87 0.001 0.000 0.005 0.000 rcsetup.py:90()\n", " 170 0.001 0.000 0.004 0.000 :840(path_stats)\n", " 157/155 0.001 0.000 0.001 0.000 pyparsing.py:382(__init__)\n", " 1 0.001 0.001 0.096 0.096 pyparsing.py:76()\n", " 34 0.001 0.000 0.001 0.000 groupby.py:3348(_whitelist_method_generator)\n", " 1 0.001 0.001 0.194 0.194 __init__.py:101()\n", " 333 0.001 0.000 0.001 0.000 {built-in method _imp.is_frozen}\n", " 53 0.001 0.000 0.038 0.001 sre_parse.py:844(parse)\n", " 84 0.001 0.000 0.002 0.000 ops.py:623(_make_flex_doc)\n", " 226 0.001 0.000 0.001 0.000 {method 'extend' of 'list' objects}\n", " 340 0.001 0.000 0.001 0.000 {built-in method from_bytes}\n", " 121/59 0.001 0.000 0.017 0.000 pyparsing.py:3429(copy)\n", " 170 0.001 0.000 0.001 0.000 ops.py:854()\n", " 1 0.001 0.001 0.010 0.010 generic.py:102(NDFrame)\n", " 33/5 0.001 0.000 0.003 0.001 pyparsing.py:3385(streamline)\n", " 66 0.001 0.000 0.002 0.000 pyparsing.py:3785(__init__)\n", " 404 0.001 0.000 0.001 0.000 {method 'find' of 'bytearray' objects}\n", " 24 0.001 0.000 0.004 0.000 deprecation.py:178(deprecate)\n", " 1 0.001 0.001 0.001 0.001 stata.py:720(StataMissingValue)\n", " 1 0.001 0.001 0.012 0.012 frame.py:248(DataFrame)\n", " 67 0.001 0.000 0.004 0.000 pyparsing.py:3456(__init__)\n", " 270 0.001 0.000 0.001 0.000 {method 'endswith' of 'str' objects}\n", " 170 0.001 0.000 0.001 0.000 :35(_new_module)\n", " 231 0.001 0.000 0.001 0.000 enum.py:517(__new__)\n", " 265 0.001 0.000 0.001 0.000 sre_parse.py:111(__init__)\n", " 103 0.001 0.000 0.001 0.000 ops.py:182(_gen_eval_kwargs)\n", " 157 0.001 0.000 0.001 0.000 pyparsing.py:373(__new__)\n", " 1 0.001 0.001 0.024 0.024 parsers.py:3()\n", " 42 0.001 0.000 0.012 0.000 ops.py:1367(_flex_method_SERIES)\n", " 80 0.001 0.000 0.002 0.000 _weakrefset.py:58(__iter__)\n", " 74 0.001 0.000 0.001 0.000 _weakrefset.py:36(__init__)\n", " 213 0.001 0.000 0.001 0.000 :311(__enter__)\n", " 39/7 0.001 0.000 0.009 0.001 pyparsing.py:3463(parseImpl)\n", " 29 0.001 0.000 0.003 0.000 :1067(_path_hooks)\n", " 121/59 0.001 0.000 0.013 0.000 pyparsing.py:3431()\n", " 11 0.001 0.000 0.002 0.000 ops.py:857()\n", " 137 0.001 0.000 0.001 0.000 expr.py:145()\n", " 144 0.001 0.000 0.001 0.000 __init__.py:293(set_function_name)\n", " 3 0.001 0.000 0.011 0.004 generic.py:8747(_add_numeric_operations)\n", " 420 0.001 0.000 0.001 0.000 :41(_relax_case)\n", " 58/10 0.001 0.000 0.018 0.002 pyparsing.py:3354(leaveWhitespace)\n", " 333 0.001 0.000 0.001 0.000 :707(find_spec)\n", " 1 0.001 0.001 0.011 0.011 series.py:126(Series)\n", " 1 0.001 0.001 0.001 0.001 __init__.py:933()\n", " 45/1 0.001 0.000 0.001 0.001 copy.py:132(deepcopy)\n", " 120 0.001 0.000 0.001 0.000 :861(_find_spec_legacy)\n", " 14 0.001 0.000 0.003 0.000 abc.py:132(__new__)\n", " 93 0.001 0.000 0.001 0.000 sre_compile.py:388(_simple)\n", " 1 0.001 0.001 0.006 0.006 groupby.py:1171(GroupBy)\n", " 25 0.001 0.000 0.001 0.000 {method 'split' of '_sre.SRE_Pattern' objects}\n", " 200 0.001 0.000 0.001 0.000 sre_parse.py:81(groups)\n", " 43/27 0.001 0.000 0.135 0.005 :919(create_module)\n", " 361 0.001 0.000 0.001 0.000 {method 'lower' of 'str' objects}\n", " 23 0.001 0.000 0.008 0.000 pyparsing.py:1049(_trim_arity)\n", " 105 0.001 0.000 0.007 0.000 colors.py:121(is_color_like)\n", " 1 0.001 0.001 0.004 0.004 base.py:183(Index)\n", " 170 0.001 0.000 0.001 0.000 :800(__init__)\n", " 80 0.001 0.000 0.001 0.000 ops.py:249(_get_frame_op_default_axis)\n", " 213 0.001 0.000 0.001 0.000 :307(__init__)\n", " 27 0.001 0.000 0.035 0.001 pyparsing.py:2816(__init__)\n", " 87 0.000 0.000 0.003 0.000 colors.py:150(to_rgba)\n", " 66/50 0.000 0.000 0.001 0.000 sre_compile.py:414(_get_literal_prefix)\n", " 66 0.000 0.000 0.005 0.000 pyparsing.py:1843(__add__)\n", " 151 0.000 0.000 0.001 0.000 {built-in method builtins.all}\n", " 22/10 0.000 0.000 0.004 0.000 pyparsing.py:3613(parseImpl)\n", " 54 0.000 0.000 0.001 0.000 pyparsing.py:2870(__str__)\n", " 9 0.000 0.000 0.002 0.000 accessor.py:56(_add_delegate_accessors)\n", " 14 0.000 0.000 0.006 0.000 expr.py:150(_filter_nodes)\n", " 1 0.000 0.000 0.002 0.002 strings.py:1882(StringMethods)\n", " 29 0.000 0.000 0.002 0.000 :1281(_fill_cache)\n", " 6 0.000 0.000 0.021 0.004 ops.py:875(add_special_arithmetic_methods)\n", " 158 0.000 0.000 0.000 0.000 _decorators.py:241(__init__)\n", " 53 0.000 0.000 0.024 0.000 sre_compile.py:542(_code)\n", " 14 0.000 0.000 0.002 0.000 colors.py:184(_to_rgba_no_colorcycle)\n", " 12 0.000 0.000 0.002 0.000 ops.py:1566(_flex_comp_method_FRAME)\n", " 29 0.000 0.000 0.001 0.000 :1196(__init__)\n", " 75 0.000 0.000 0.001 0.000 sre_parse.py:342(_escape)\n", " 1 0.000 0.000 0.000 0.000 {built-in method builtins.compile}\n", " 54 0.000 0.000 0.001 0.000 sre_parse.py:223(__init__)\n", " 101 0.000 0.000 0.000 0.000 functools.py:74(wraps)\n", " 159 0.000 0.000 0.000 0.000 expr.py:197(_node_not_implemented)\n", " 5 0.000 0.000 0.005 0.001 __init__.py:357(namedtuple)\n", " 2 0.000 0.000 0.921 0.461 __init__.py:5()\n", " 66 0.000 0.000 0.003 0.000 linecache.py:15(getline)\n", " 63 0.000 0.000 0.001 0.000 _weakrefset.py:26(__exit__)\n", " 1 0.000 0.000 0.555 0.555 groupby.py:1()\n", " 30 0.000 0.000 0.000 0.000 deprecation.py:23(_generate_deprecation_message)\n", " 1 0.000 0.000 0.048 0.048 pyparsing.py:5558(pyparsing_common)\n", " 97 0.000 0.000 0.001 0.000 ops.py:223(_gen_fill_zeros)\n", " 1 0.000 0.000 0.025 0.025 pytables.py:4()\n", " 10 0.000 0.000 0.013 0.001 pyparsing.py:2675(__init__)\n", " 224 0.000 0.000 0.000 0.000 {method 'add' of 'set' objects}\n", " 301 0.000 0.000 0.001 0.000 __init__.py:907(__iter__)\n", " 46 0.000 0.000 0.001 0.000 sre_parse.py:84(opengroup)\n", " 138 0.000 0.000 0.001 0.000 expr.py:147()\n", " 1 0.000 0.000 0.236 0.236 _core.py:3()\n", " 36 0.000 0.000 0.001 0.000 pyparsing.py:2434(__init__)\n", " 29 0.000 0.000 0.001 0.000 sre_compile.py:376(_mk_bitmap)\n", " 29 0.000 0.000 0.001 0.000 pyparsing.py:2852(parseImpl)\n", " 49/13 0.000 0.000 0.006 0.000 typing.py:1145(__subclasscheck__)\n", " 1 0.000 0.000 0.000 0.000 expr.py:266(BaseExprVisitor)\n", " 96 0.000 0.000 0.000 0.000 ops.py:162(make_invalid_op)\n", " 88 0.000 0.000 0.004 0.000 traceback.py:283(line)\n", " 93 0.000 0.000 0.000 0.000 pyparsing.py:1361(preParse)\n", " 19 0.000 0.000 0.003 0.000 generic.py:9598(_make_stat_function)\n", " 166 0.000 0.000 0.000 0.000 pyparsing.py:3336()\n", " 86 0.000 0.000 0.001 0.000 pyparsing.py:3505()\n", " 213 0.000 0.000 0.000 0.000 :424(has_location)\n", " 23 0.000 0.000 0.001 0.000 rcsetup.py:57()\n", " 56 0.000 0.000 0.001 0.000 base.py:128(_make_arithmetic_op)\n", " 6 0.000 0.000 0.169 0.028 api.py:3()\n", " 32/16 0.000 0.000 0.001 0.000 typing.py:1164(__setattr__)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:1078()\n", " 54 0.000 0.000 0.000 0.000 accessor.py:72(_create_delegator_property)\n", " 148 0.000 0.000 0.000 0.000 rcsetup.py:52(func)\n", " 1 0.000 0.000 0.015 0.015 offsets.py:2()\n", " 22 0.000 0.000 0.007 0.000 traceback.py:200(extract_stack)\n", " 1 0.000 0.000 0.000 0.000 _color_data.py:992()\n", " 1 0.000 0.000 0.000 0.000 __init__.py:1120()\n", " 1 0.000 0.000 0.003 0.003 panel.py:107(Panel)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2214(Tick)\n", " 68 0.000 0.000 0.000 0.000 _weakrefset.py:81(add)\n", " 90 0.000 0.000 0.000 0.000 rcsetup.py:59(__call__)\n", " 2 0.000 0.000 0.072 0.036 base.py:1()\n", " 1 0.000 0.000 0.004 0.004 window.py:1713(Expanding)\n", " 1 0.000 0.000 0.015 0.015 internals.py:1()\n", " 29 0.000 0.000 0.000 0.000 sre_compile.py:378()\n", " 232 0.000 0.000 0.000 0.000 :1202()\n", " 196 0.000 0.000 0.000 0.000 {method 'pop' of 'list' objects}\n", " 1 0.000 0.000 0.298 0.298 series.py:3()\n", " 13 0.000 0.000 0.000 0.000 nanops.py:60(__init__)\n", " 1 0.000 0.000 0.000 0.000 excel.py:1568(_XlsxStyler)\n", " 1 0.000 0.000 0.000 0.000 rrule.py:1308(_genitem)\n", " 115 0.000 0.000 0.000 0.000 pyparsing.py:2175(__str__)\n", " 83 0.000 0.000 0.001 0.000 pyparsing.py:2390(__init__)\n", " 1 0.000 0.000 0.382 0.382 frame.py:10()\n", " 1 0.000 0.000 0.013 0.013 datetimes.py:2()\n", " 106 0.000 0.000 0.000 0.000 sre_compile.py:539(isstring)\n", " 43 0.000 0.000 0.001 0.000 :927(exec_module)\n", " 9 0.000 0.000 0.002 0.000 generic.py:9618(_make_stat_function_ddof)\n", " 236 0.000 0.000 0.000 0.000 {built-in method builtins.chr}\n", " 58 0.000 0.000 0.013 0.000 pyparsing.py:3358()\n", " 87 0.000 0.000 0.000 0.000 rcsetup.py:399(validate_string)\n", " 1 0.000 0.000 0.006 0.006 excel.py:3()\n", " 3 0.000 0.000 0.020 0.007 common.py:1()\n", " 53 0.000 0.000 0.000 0.000 {built-in method _sre.compile}\n", " 1 0.000 0.000 0.004 0.004 window.py:1428(Rolling)\n", " 14 0.000 0.000 0.001 0.000 abc.py:135()\n", " 1 0.000 0.000 0.019 0.019 frame.py:4()\n", " 37 0.000 0.000 0.000 0.000 sre_parse.py:294(_class_escape)\n", " 1 0.000 0.000 0.001 0.001 rrule.py:7()\n", " 22 0.000 0.000 0.007 0.000 pyparsing.py:1057(extract_stack)\n", " 30 0.000 0.000 0.004 0.000 rcsetup.py:342(validate_color_for_prop_cycle)\n", " 188 0.000 0.000 0.000 0.000 pyparsing.py:2678()\n", " 1 0.000 0.000 0.047 0.047 generic.py:2()\n", " 20 0.000 0.000 0.001 0.000 deprecation.py:237(finalize)\n", " 1 0.000 0.000 0.004 0.004 nanops.py:1()\n", " 120 0.000 0.000 0.000 0.000 six.py:184(find_module)\n", " 63 0.000 0.000 0.000 0.000 _weakrefset.py:20(__enter__)\n", " 1 0.000 0.000 0.653 0.653 api.py:5()\n", " 135 0.000 0.000 0.000 0.000 rcsetup.py:144(validate_float)\n", " 1 0.000 0.000 0.227 0.227 _converter.py:1()\n", " 170 0.000 0.000 0.000 0.000 :825(get_filename)\n", " 1 0.000 0.000 0.026 0.026 dates.py:134()\n", " 1 0.000 0.000 0.012 0.012 config_init.py:11()\n", " 1 0.000 0.000 0.000 0.000 parsers.py:2015(PythonParser)\n", " 1 0.000 0.000 0.007 0.007 groupby.py:3409(SeriesGroupBy)\n", " 1 0.000 0.000 0.017 0.017 ticker.py:165()\n", " 14 0.000 0.000 0.002 0.000 ops.py:1687(_flex_method_PANEL)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:143(DateOffset)\n", " 1 0.000 0.000 0.002 0.002 multi.py:131(MultiIndex)\n", " 53 0.000 0.000 0.000 0.000 sre_parse.py:828(fix_flags)\n", " 170 0.000 0.000 0.000 0.000 :669(create_module)\n", " 29 0.000 0.000 0.002 0.000 :1322(path_hook_for_FileFinder)\n", " 8 0.000 0.000 0.003 0.000 base.py:4839(_make_logical_function)\n", " 34/26 0.000 0.000 0.001 0.000 pyparsing.py:3500(__str__)\n", " 1 0.000 0.000 0.044 0.044 reshape.py:3()\n", " 1 0.000 0.000 0.000 0.000 ticker.py:481(ScalarFormatter)\n", " 1 0.000 0.000 0.001 0.001 array.py:130(SparseArray)\n", " 21 0.000 0.000 0.008 0.000 pyparsing.py:1260(setParseAction)\n", " 39 0.000 0.000 0.001 0.000 sre_compile.py:441(_get_charset_prefix)\n", " 1 0.000 0.000 0.006 0.006 __init__.py:7()\n", " 7 0.000 0.000 0.000 0.000 {built-in method builtins.dir}\n", " 6 0.000 0.000 0.002 0.000 generic.py:9673(_make_logical_function)\n", " 4 0.000 0.000 0.001 0.000 base.py:4710(_add_numeric_methods_binary)\n", " 3 0.000 0.000 0.005 0.002 rcsetup.py:825(validate_cycler)\n", " 16 0.000 0.000 0.000 0.000 getlimits.py:507(__init__)\n", " 19 0.000 0.000 0.000 0.000 inspect.py:2450(__init__)\n", " 97 0.000 0.000 0.000 0.000 pyparsing.py:4934()\n", " 5 0.000 0.000 0.001 0.000 base.py:4634(_add_numeric_methods_disabled)\n", " 1 0.000 0.000 0.003 0.003 strings.py:1()\n", " 3 0.000 0.000 0.000 0.000 {pandas._libs.lib.maybe_convert_objects}\n", " 12 0.000 0.000 0.002 0.000 generic.py:9638(_make_cum_function)\n", " 122 0.000 0.000 0.000 0.000 {method 'items' of 'dict' objects}\n", " 66 0.000 0.000 0.000 0.000 linecache.py:147(lazycache)\n", " 58 0.000 0.000 0.067 0.001 re.py:231(compile)\n", " 8 0.000 0.000 0.000 0.000 {built-in method _warnings.warn}\n", " 1 0.000 0.000 0.000 0.000 _core.py:75(MPLPlot)\n", " 4 0.000 0.000 0.000 0.000 {built-in method io.open}\n", " 21 0.000 0.000 0.037 0.002 __init__.py:1()\n", " 1 0.000 0.000 0.004 0.004 testing.py:1()\n", " 1 0.000 0.000 0.000 0.000 colors.py:89()\n", " 11 0.000 0.000 0.001 0.000 ops.py:741(_get_method_wrappers)\n", " 1 0.000 0.000 0.003 0.003 ops.py:5()\n", " 137 0.000 0.000 0.000 0.000 pyparsing.py:2083(setWhitespaceChars)\n", " 66 0.000 0.000 0.003 0.000 linecache.py:37(getlines)\n", " 1 0.000 0.000 0.008 0.008 transforms.py:30()\n", " 1 0.000 0.000 0.002 0.002 category.py:32(CategoricalIndex)\n", " 20 0.000 0.000 0.000 0.000 version.py:307(parse)\n", " 20 0.000 0.000 0.001 0.000 _decorators.py:135(_deprecate_kwarg)\n", " 1 0.000 0.000 0.013 0.013 categorical.py:3()\n", " 48 0.000 0.000 0.003 0.000 config.py:726(inner)\n", " 3 0.000 0.000 0.000 0.000 cycler.py:349(by_key)\n", " 158 0.000 0.000 0.000 0.000 pyparsing.py:3458()\n", " 33/10 0.000 0.000 0.022 0.002 pyparsing.py:3809(leaveWhitespace)\n", " 2 0.000 0.000 0.001 0.000 lazy.py:139(__new__)\n", " 1 0.000 0.000 0.000 0.000 twodim_base.py:140(eye)\n", " 2 0.000 0.000 0.005 0.003 numeric.py:1()\n", " 38 0.000 0.000 0.000 0.000 {method 'strftime' of 'datetime.date' objects}\n", " 3 0.000 0.000 0.000 0.000 generic.py:236(_setup_axes)\n", " 45 0.000 0.000 0.000 0.000 pyparsing.py:1200(setName)\n", " 1 0.000 0.000 0.011 0.011 packers.py:39()\n", " 1 0.000 0.000 0.000 0.000 ops.py:44(Term)\n", " 1 0.000 0.000 0.003 0.003 series.py:39(SparseSeries)\n", " 1 0.000 0.000 0.022 0.022 window.py:7()\n", " 1 0.000 0.000 0.001 0.001 merge.py:3()\n", " 6 0.000 0.000 0.001 0.000 groupby.py:1404(groupby_function)\n", " 1 0.000 0.000 0.013 0.013 panel.py:3()\n", " 1 0.000 0.000 0.002 0.002 timedeltas.py:1()\n", " 8 0.000 0.000 0.002 0.000 typing.py:875(__extrahook__)\n", " 115 0.000 0.000 0.000 0.000 expr.py:157()\n", " 23 0.000 0.000 0.001 0.000 rcsetup.py:47(__init__)\n", " 1 0.000 0.000 0.000 0.000 expr.py:252(f)\n", " 98 0.000 0.000 0.000 0.000 pyparsing.py:2181(streamline)\n", " 1 0.000 0.000 0.009 0.009 format.py:5()\n", " 5 0.000 0.000 0.029 0.006 ops.py:927(add_flex_arithmetic_methods)\n", " 22 0.000 0.000 0.001 0.000 linecache.py:53(checkcache)\n", " 6/1 0.000 0.000 0.001 0.001 copy.py:236(_deepcopy_dict)\n", " 42 0.000 0.000 0.000 0.000 re.py:249(escape)\n", " 23/7 0.000 0.000 0.002 0.000 pyparsing.py:3828(streamline)\n", " 36 0.000 0.000 0.000 0.000 accessor.py:86(_create_delegator_method)\n", " 46 0.000 0.000 0.002 0.000 sre_parse.py:96(closegroup)\n", " 2 0.000 0.000 0.001 0.000 concat.py:3()\n", " 2 0.000 0.000 0.001 0.000 _decorators.py:184(decorate)\n", " 1 0.000 0.000 0.004 0.004 groupby.py:4584(DataFrameGroupBy)\n", " 3 0.000 0.000 0.003 0.001 _decorators.py:10(deprecate)\n", " 88 0.000 0.000 0.000 0.000 traceback.py:290(walk_stack)\n", " 53 0.000 0.000 0.000 0.000 sre_parse.py:76(__init__)\n", " 4 0.000 0.000 0.000 0.000 {built-in method builtins.sorted}\n", " 1 0.000 0.000 0.001 0.001 _color_data.py:1()\n", " 1 0.000 0.000 0.001 0.001 period.py:125(PeriodIndex)\n", " 108 0.000 0.000 0.000 0.000 {built-in method _struct.pack}\n", " 37 0.000 0.000 0.000 0.000 codecs.py:318(decode)\n", " 22 0.000 0.000 0.001 0.000 compilerop.py:137(check_linecache_ipython)\n", " 15 0.000 0.000 0.000 0.000 sre_parse.py:266(getuntil)\n", " 1 0.000 0.000 0.003 0.003 stata.py:11()\n", " 1 0.000 0.000 0.007 0.007 array.py:3()\n", " 1 0.000 0.000 0.008 0.008 pyparsing.py:5028(_makeTags)\n", " 2 0.000 0.000 0.001 0.000 lazy.py:84(__new__)\n", " 21 0.000 0.000 0.000 0.000 pyparsing.py:2764(__str__)\n", " 28 0.000 0.000 0.001 0.000 pyparsing.py:1079(wrapper)\n", " 27 0.000 0.000 0.000 0.000 rcsetup.py:939(_validate_linestyle)\n", " 37 0.000 0.000 0.000 0.000 typing.py:1019(_abc_negative_cache)\n", " 12 0.000 0.000 0.001 0.000 ops.py:1604(_comp_method_FRAME)\n", " 1 0.000 0.000 0.007 0.007 base.py:3()\n", " 1 0.000 0.000 0.023 0.023 algorithms.py:4()\n", " 112 0.000 0.000 0.000 0.000 {built-in method _struct.unpack}\n", " 1 0.000 0.000 0.000 0.000 internals.py:3363(_rebuild_blknos_and_blklocs)\n", " 1 0.000 0.000 0.002 0.002 window.py:1980(EWM)\n", " 16 0.000 0.000 0.001 0.000 offsets.py:59(apply_wraps)\n", " 15 0.000 0.000 0.001 0.000 ops.py:996(_arith_method_SERIES)\n", " 1 0.000 0.000 0.026 0.026 interval.py:1()\n", " 142 0.000 0.000 0.000 0.000 {method '__contains__' of 'frozenset' objects}\n", " 1 0.000 0.000 0.002 0.002 frame.py:34(SparseDataFrame)\n", " 52 0.000 0.000 0.000 0.000 colors.py:204()\n", " 1 0.000 0.000 0.001 0.001 categorical.py:170(Categorical)\n", " 1 0.000 0.000 0.008 0.008 fontconfig_pattern.py:65(__init__)\n", " 63 0.000 0.000 0.000 0.000 _weakrefset.py:16(__init__)\n", " 16 0.000 0.000 0.000 0.000 pyparsing.py:2729(parseImpl)\n", " 1 0.000 0.000 0.003 0.003 multi.py:3()\n", " 93 0.000 0.000 0.000 0.000 sre_parse.py:167(__setitem__)\n", " 12 0.000 0.000 0.005 0.000 pyparsing.py:4012(parseImpl)\n", " 12 0.000 0.000 0.000 0.000 pyparsing.py:1589(resetCache)\n", " 2 0.000 0.000 0.029 0.014 generic.py:8893(_add_series_or_dataframe_operations)\n", " 2 0.000 0.000 0.000 0.000 inspect.py:2730(__init__)\n", " 63 0.000 0.000 0.000 0.000 _weakrefset.py:52(_commit_removals)\n", " 70 0.000 0.000 0.000 0.000 {method '__subclasses__' of 'type' objects}\n", " 1 0.000 0.000 0.001 0.001 ops.py:2()\n", " 37 0.000 0.000 0.000 0.000 {built-in method _codecs.utf_8_decode}\n", " 49 0.000 0.000 0.000 0.000 pyparsing.py:219(__init__)\n", " 37 0.000 0.000 0.000 0.000 pyparsing.py:421(__getitem__)\n", " 11 0.000 0.000 0.000 0.000 pyparsing.py:4718(_escapeRegexRangeChars)\n", " 48 0.000 0.000 0.000 0.000 rcsetup.py:201(validate_int)\n", " 1 0.000 0.000 0.003 0.003 indexing.py:2()\n", " 1 0.000 0.000 0.004 0.004 colors.py:61()\n", " 95 0.000 0.000 0.000 0.000 pyparsing.py:213()\n", " 112 0.000 0.000 0.000 0.000 ops.py:856()\n", " 58 0.000 0.000 0.000 0.000 {built-in method builtins.id}\n", " 1 0.000 0.000 0.015 0.015 series.py:4()\n", " 36 0.000 0.000 0.000 0.000 {method 'setter' of 'property' objects}\n", " 16 0.000 0.000 0.001 0.000 pyparsing.py:3606(__init__)\n", " 1 0.000 0.000 0.002 0.002 period.py:2()\n", " 1 0.000 0.000 0.003 0.003 category.py:1()\n", " 7 0.000 0.000 0.013 0.002 pyparsing.py:1630(parseString)\n", " 66 0.000 0.000 0.000 0.000 traceback.py:243(__init__)\n", " 44 0.000 0.000 0.000 0.000 pyparsing.py:3647()\n", " 17 0.000 0.000 0.000 0.000 {built-in method posix.getcwd}\n", " 1 0.000 0.000 0.007 0.007 json.py:2()\n", " 32 0.000 0.000 0.000 0.000 nanops.py:62()\n", " 25 0.000 0.000 0.000 0.000 common.py:1835(_get_dtype_type)\n", " 15 0.000 0.000 0.000 0.000 rcsetup.py:288(__call__)\n", " 12 0.000 0.000 0.001 0.000 ops.py:1654(_comp_method_PANEL)\n", " 18 0.000 0.000 0.001 0.000 contextlib.py:129(contextmanager)\n", " 1 0.000 0.000 0.119 0.119 api.py:1()\n", " 43 0.000 0.000 0.000 0.000 :908(__init__)\n", " 1 0.000 0.000 0.000 0.000 format.py:1038(format_values_with)\n", " 96 0.000 0.000 0.000 0.000 pyparsing.py:1376(postParse)\n", " 31 0.000 0.000 0.000 0.000 pyparsing.py:2453(parseImpl)\n", " 40 0.000 0.000 0.000 0.000 typing.py:1089(__eq__)\n", " 1 0.000 0.000 0.001 0.001 datetimes.py:175(DatetimeIndex)\n", " 62 0.000 0.000 0.000 0.000 lazy.py:96(lazy)\n", " 25 0.000 0.000 0.000 0.000 typing.py:1033(_abc_negative_cache_version)\n", " 1 0.000 0.000 0.000 0.000 path.py:21(Path)\n", " 25 0.000 0.000 0.001 0.000 pyparsing.py:4150(__init__)\n", " 10 0.000 0.000 0.022 0.002 pyparsing.py:4404(__init__)\n", " 1 0.000 0.000 0.001 0.001 {pandas._libs.lib.clean_index_list}\n", " 1 0.000 0.000 0.002 0.002 range.py:1()\n", " 1 0.000 0.000 0.000 0.000 frequencies.py:2()\n", " 2 0.000 0.000 0.000 0.000 missing.py:3()\n", " 13 0.000 0.000 0.000 0.000 pyparsing.py:3847(__str__)\n", " 27 0.000 0.000 0.001 0.000 pyparsing.py:4383(__init__)\n", " 16 0.000 0.000 0.001 0.000 pyparsing.py:1970(__or__)\n", " 13 0.000 0.000 0.000 0.000 printing.py:153(pprint_thing)\n", " 9 0.000 0.000 0.000 0.000 rcsetup.py:906(validate_animation_writer_path)\n", " 1 0.000 0.000 0.015 0.015 cast.py:1()\n", " 33 0.000 0.000 0.000 0.000 rcsetup.py:667(__call__)\n", " 94 0.000 0.000 0.000 0.000 pyparsing.py:5043()\n", " 1 0.000 0.000 0.003 0.003 datetimelike.py:4()\n", " 1 0.000 0.000 0.006 0.006 __init__.py:9()\n", " 1 0.000 0.000 0.002 0.002 window.py:446(Window)\n", " 18 0.000 0.000 0.000 0.000 rcsetup.py:429(validate_fontsize)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:792()\n", " 6 0.000 0.000 0.000 0.000 generic.py:280(set_axis)\n", " 14 0.000 0.000 0.000 0.000 copyreg.py:96(_slotnames)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:334(ParseResults)\n", " 10 0.000 0.000 0.002 0.000 pyparsing.py:4092(parseImpl)\n", " 1 0.000 0.000 0.000 0.000 sorting.py:1()\n", " 15 0.000 0.000 0.000 0.000 config.py:78(_get_single_key)\n", " 9 0.000 0.000 0.000 0.000 os.py:664(__getitem__)\n", " 5 0.000 0.000 0.000 0.000 base.py:4732(_add_numeric_methods_unary)\n", " 1 0.000 0.000 0.002 0.002 groupby.py:563(_GroupBy)\n", " 13 0.000 0.000 0.000 0.000 contextlib.py:59(__init__)\n", " 1 0.000 0.000 0.001 0.001 interval.py:135(IntervalIndex)\n", " 29 0.000 0.000 0.001 0.000 :99(_path_isdir)\n", " 3 0.000 0.000 0.000 0.000 pathlib.py:51(parse_parts)\n", " 21 0.000 0.000 0.000 0.000 ops.py:1756(_arith_method_SPARSE_SERIES)\n", " 74 0.000 0.000 0.000 0.000 pyparsing.py:2074(leaveWhitespace)\n", " 12 0.000 0.000 0.000 0.000 pyparsing.py:4910()\n", " 6 0.000 0.000 0.001 0.000 generic.py:9575(_make_min_count_stat_function)\n", " 1 0.000 0.000 0.017 0.017 pytables.py:1()\n", " 48 0.000 0.000 0.000 0.000 :12(__new__)\n", " 84 0.000 0.000 0.000 0.000 lazy.py:149(lazy)\n", " 43 0.000 0.000 0.000 0.000 {built-in method _imp.exec_dynamic}\n", " 1 0.000 0.000 0.001 0.001 melt.py:3()\n", " 1 0.000 0.000 0.002 0.002 __init__.py:25()\n", " 1 0.000 0.000 0.001 0.001 pivot.py:4()\n", " 1 0.000 0.000 0.002 0.002 base.py:698(IndexOpsMixin)\n", " 60 0.000 0.000 0.000 0.000 ops.py:891(_wrap_inplace_method)\n", " 23 0.000 0.000 0.000 0.000 base.py:61(is_dtype)\n", " 63 0.000 0.000 0.000 0.000 {method 'remove' of 'set' objects}\n", " 1 0.000 0.000 0.001 0.001 timedeltas.py:99(TimedeltaIndex)\n", " 1 0.000 0.000 0.002 0.002 sql.py:5()\n", " 14 0.000 0.000 0.000 0.000 pyparsing.py:673(__iadd__)\n", " 51 0.000 0.000 0.000 0.000 {built-in method sys._getframe}\n", " 2 0.000 0.000 0.250 0.125 __init__.py:3()\n", " 12 0.000 0.000 0.003 0.000 pyparsing.py:4156(parseImpl)\n", " 26 0.000 0.000 0.000 0.000 calendar.py:58(__getitem__)\n", " 5 0.000 0.000 0.000 0.000 getlimits.py:518(min)\n", " 12 0.000 0.000 0.000 0.000 rcsetup.py:68(_listify_validator)\n", " 1 0.000 0.000 0.002 0.002 accessors.py:3()\n", " 1 0.000 0.000 0.003 0.003 window.py:893(_Rolling_and_Expanding)\n", " 4 0.000 0.000 0.004 0.001 base.py:4757(_add_logical_methods)\n", " 1 0.000 0.000 0.001 0.001 groupby.py:1400(_add_numeric_operations)\n", " 1 0.000 0.000 0.001 0.001 zipfile.py:5()\n", " 1 0.000 0.000 0.000 0.000 {pandas._libs.lib.to_object_array}\n", " 59 0.000 0.000 0.000 0.000 {method 'isidentifier' of 'str' objects}\n", " 32 0.000 0.000 0.000 0.000 {built-in method builtins.repr}\n", " 11 0.000 0.000 0.000 0.000 sre_compile.py:393(_generate_overlap_table)\n", " 1 0.000 0.000 0.000 0.000 datetimes.py:1()\n", " 33 0.000 0.000 0.000 0.000 cycler.py:227()\n", " 1 0.000 0.000 0.000 0.000 cycler.py:41()\n", " 13 0.000 0.000 0.001 0.000 nanops.py:68(__call__)\n", " 4 0.000 0.000 0.000 0.000 __init__.py:1161(getLogger)\n", " 24 0.000 0.000 0.000 0.000 config.py:695(config_prefix)\n", " 2 0.000 0.000 0.000 0.000 weakref.py:102(__init__)\n", " 29 0.000 0.000 0.000 0.000 {method 'translate' of 'bytearray' objects}\n", " 1 0.000 0.000 0.000 0.000 colors.py:73(__init__)\n", " 18 0.000 0.000 0.007 0.000 rcsetup.py:446(validate_font_properties)\n", " 1 0.000 0.000 0.001 0.001 __init__.py:286(Verbose)\n", " 1 0.000 0.000 0.001 0.001 function.py:19()\n", " 1 0.000 0.000 0.114 0.114 fontconfig_pattern.py:7()\n", " 4 0.000 0.000 0.000 0.000 _decorators.py:308(indent)\n", " 52 0.000 0.000 0.000 0.000 _collections_abc.py:392(__subclasshook__)\n", " 1 0.000 0.000 0.001 0.001 html.py:4()\n", " 1 0.000 0.000 0.001 0.001 align.py:2()\n", " 16 0.000 0.000 0.001 0.000 pyparsing.py:3642(__str__)\n", " 1 0.000 0.000 0.001 0.001 generic.py:1()\n", " 8 0.000 0.000 0.000 0.000 typing.py:1025(_abc_negative_cache)\n", " 1 0.000 0.000 0.001 0.001 path.py:10()\n", " 22 0.000 0.000 0.000 0.000 traceback.py:273(__getitem__)\n", " 1 0.000 0.000 0.000 0.000 plotting.py:1()\n", " 18/4 0.000 0.000 0.004 0.001 pyparsing.py:3803(parseImpl)\n", " 1 0.000 0.000 6.083 6.083 interactiveshell.py:2637(safe_execfile)\n", " 12/2 0.000 0.000 0.001 0.000 pyparsing.py:1948(makeOptionalList)\n", " 26 0.000 0.000 0.000 0.000 pyparsing.py:516(haskeys)\n", " 1 0.000 0.000 0.000 0.000 internals.py:4880(form_blocks)\n", " 40 0.000 0.000 0.000 0.000 pyparsing.py:468(__bool__)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:289(JointFilterBinOp)\n", " 2 0.000 0.000 0.000 0.000 __init__.py:175(compare_versions)\n", " 1 0.000 0.000 0.001 0.001 range.py:28(RangeIndex)\n", " 26 0.000 0.000 0.000 0.000 ops.py:1795(_arith_method_SPARSE_ARRAY)\n", " 1 0.000 0.000 0.006 0.006 config.py:49()\n", " 50 0.000 0.000 0.000 0.000 __init__.py:262(u_safe)\n", " 26 0.000 0.000 0.000 0.000 {built-in method builtins.next}\n", " 1 0.000 0.000 0.005 0.005 engines.py:3()\n", " 1 0.000 0.000 0.031 0.031 __init__.py:1056(rc_params_from_file)\n", " 1 0.000 0.000 0.001 0.001 numeric.py:292(Float64Index)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2335(Day)\n", " 3 0.000 0.000 0.000 0.000 socket.py:334(send)\n", " 13 0.000 0.000 0.000 0.000 printing.py:182(as_escaped_unicode)\n", " 6 0.000 0.000 0.000 0.000 nanops.py:95(__call__)\n", " 15 0.000 0.000 0.000 0.000 config.py:96(_get_option)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:244(BboxBase)\n", " 15 0.000 0.000 0.000 0.000 pyparsing.py:4170(__str__)\n", " 20 0.000 0.000 0.001 0.000 version.py:302(__init__)\n", " 32 0.000 0.000 0.000 0.000 {built-in method builtins.iter}\n", " 1 0.000 0.000 0.003 0.003 linecache.py:82(updatecache)\n", " 13 0.000 0.000 0.000 0.000 contextlib.py:85(__exit__)\n", " 10 0.000 0.000 0.000 0.000 pyparsing.py:4004(__init__)\n", " 24 0.000 0.000 0.000 0.000 deprecation.py:115(deprecated)\n", " 5 0.000 0.000 0.000 0.000 {method 'translate' of 'str' objects}\n", " 7 0.000 0.000 0.000 0.000 rrule.py:70(__init__)\n", " 5 0.000 0.000 0.007 0.001 fontconfig_pattern.py:113(parse)\n", " 5 0.000 0.000 0.000 0.000 textwrap.py:160(_split)\n", " 6 0.000 0.000 0.000 0.000 copy.py:252(_keep_alive)\n", " 5 0.000 0.000 0.000 0.000 _depr_module.py:26(__init__)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:752(Bbox)\n", " 1 0.000 0.000 0.009 0.009 dtypes.py:1()\n", " 4 0.000 0.000 0.000 0.000 posixpath.py:75(join)\n", " 1 0.000 0.000 0.000 0.000 scope.py:3()\n", " 2 0.000 0.000 0.000 0.000 __init__.py:961(_open_file_or_url)\n", " 1 0.000 0.000 0.000 0.000 common.py:3()\n", " 8 0.000 0.000 0.000 0.000 typing.py:1039(_abc_negative_cache_version)\n", " 20 0.000 0.000 0.000 0.000 {built-in method _imp.is_builtin}\n", " 1 0.000 0.000 0.001 0.001 _core.py:2911(FramePlotMethods)\n", " 39 0.000 0.000 0.000 0.000 copy.py:190(_deepcopy_atomic)\n", " 44 0.000 0.000 0.000 0.000 pyparsing.py:3609()\n", " 6 0.000 0.000 0.001 0.000 pyparsing.py:4934()\n", " 1 0.000 0.000 0.003 0.003 _misc.py:3()\n", " 1 0.000 0.000 0.000 0.000 internals.py:88(Block)\n", " 6 0.000 0.000 0.000 0.000 deprecation.py:54(warn_deprecated)\n", " 1 0.000 0.000 0.000 0.000 internals.py:3210(BlockManager)\n", " 8 0.000 0.000 0.000 0.000 datetimelike.py:280(_join_i8_wrapper)\n", " 11 0.000 0.000 0.000 0.000 re.py:324(_subx)\n", " 5 0.000 0.000 0.000 0.000 pyparsing.py:4098(__str__)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:178(validate_axisbelow)\n", " 3 0.000 0.000 0.000 0.000 missing.py:189(_isna_ndarraylike)\n", " 20 0.000 0.000 0.000 0.000 _decorators.py:66(deprecate_kwarg)\n", " 6 0.000 0.000 0.000 0.000 warnings.py:449(__enter__)\n", " 5 0.000 0.000 0.000 0.000 warnings.py:159(_add_filter)\n", " 1 0.000 0.000 0.001 0.001 __init__.py:783(RcParams)\n", " 4 0.000 0.000 0.000 0.000 generic.py:9003(_doc_parms)\n", " 12 0.000 0.000 0.000 0.000 period.py:53(_field_accessor)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2340(Hour)\n", " 25 0.000 0.000 0.000 0.000 __init__.py:422()\n", " 1 0.000 0.000 0.000 0.000 pickle_compat.py:3()\n", " 22 0.000 0.000 0.000 0.000 expr.py:227(_op_maker)\n", " 3 0.000 0.000 0.000 0.000 excel.py:245(register_writer)\n", " 6 0.000 0.000 0.000 0.000 rcsetup.py:221(validate_fonttype)\n", " 2 0.000 0.000 0.000 0.000 fontconfig_pattern.py:152(_property)\n", " 1 0.000 0.000 0.001 0.001 format.py:1023(get_result_as_array)\n", " 15 0.000 0.000 0.000 0.000 config.py:550(_get_root)\n", " 1 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.putmask}\n", " 1 0.000 0.000 0.000 0.000 tile.py:3()\n", " 1 0.000 0.000 0.002 0.002 frame.py:334(__init__)\n", " 1 0.000 0.000 0.000 0.000 units.py:43()\n", " 5 0.000 0.000 0.000 0.000 fontconfig_pattern.py:132(_family)\n", " 2 0.000 0.000 0.000 0.000 internals.py:237(mgr_locs)\n", " 20 0.000 0.000 0.000 0.000 ops.py:1638(_arith_method_PANEL)\n", " 32 0.000 0.000 0.000 0.000 offsets.py:2455()\n", " 25 0.000 0.000 0.000 0.000 __init__.py:420()\n", " 1 0.000 0.000 0.000 0.000 base.py:255(__new__)\n", " 4/2 0.000 0.000 0.000 0.000 common.py:1879(_get_dtype_from_object)\n", " 13 0.000 0.000 0.000 0.000 contextlib.py:157(helper)\n", " 8 0.000 0.000 0.000 0.000 {method 'rfind' of 'str' objects}\n", " 1 0.000 0.000 0.000 0.000 groupby.py:3950(NDFrameGroupBy)\n", " 1 0.000 0.000 0.001 0.001 nanops.py:342(nanmean)\n", " 1 0.000 0.000 6.083 6.083 abc.py:1()\n", " 12 0.000 0.000 0.000 0.000 getlimits.py:532(max)\n", " 4 0.000 0.000 0.000 0.000 pathlib.py:385(wrapped)\n", " 1 0.000 0.000 0.008 0.008 eval.py:4()\n", " 1 0.000 0.000 0.000 0.000 cycler.py:468(cycler)\n", " 14 0.000 0.000 0.000 0.000 pyparsing.py:2773(charsAsStr)\n", " 9 0.000 0.000 0.000 0.000 __init__.py:870(__getitem__)\n", " 1 0.000 0.000 0.002 0.002 csv.py:4()\n", " 5 0.000 0.000 0.000 0.000 textwrap.py:146(_munge_whitespace)\n", " 12 0.000 0.000 0.000 0.000 rcsetup.py:300()\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:86(CategoricalDtype)\n", " 30 0.000 0.000 0.000 0.000 config.py:565(_get_deprecated_option)\n", " 13 0.000 0.000 0.000 0.000 inference.py:447(is_sequence)\n", " 20 0.000 0.000 0.000 0.000 version.py:312()\n", " 2 0.000 0.000 0.000 0.000 __init__.py:302(loads)\n", " 3 0.000 0.000 0.000 0.000 functools.py:479(decorating_function)\n", " 1 0.000 0.000 0.000 0.000 plotting.py:9()\n", " 1 0.000 0.000 0.001 0.001 __init__.py:602(_get_config_or_cache_dir)\n", " 6 0.000 0.000 0.000 0.000 base.py:89(_make_comparison_op)\n", " 13 0.000 0.000 0.000 0.000 version.py:331(_cmp)\n", " 14 0.000 0.000 0.000 0.000 calendar.py:77(__getitem__)\n", " 1 0.000 0.000 0.000 0.000 sre_parse.py:876(parse_template)\n", " 9 0.000 0.000 0.000 0.000 os.py:742(encode)\n", " 1 0.000 0.000 0.002 0.002 dates.py:581(DateFormatter)\n", " 1 0.000 0.000 0.000 0.000 cycler.py:77(Cycler)\n", " 15 0.000 0.000 0.000 0.000 cycler.py:138(keys)\n", " 1 0.000 0.000 0.000 0.000 datetimelike.py:246(DatetimeIndexOpsMixin)\n", " 5 0.000 0.000 0.001 0.000 base.py:4752(_add_numeric_methods)\n", " 1 0.000 0.000 0.000 0.000 {built-in method pandas._libs.lib.is_datetime_array}\n", " 1 0.000 0.000 0.000 0.000 lazy.py:172()\n", " 2 0.000 0.000 0.000 0.000 pathlib.py:631(_parse_args)\n", " 7 0.000 0.000 0.002 0.000 abc.py:151(register)\n", " 1 0.000 0.000 0.000 0.000 stata.py:959(StataReader)\n", " 3 0.000 0.000 0.000 0.000 pytables.py:1506(__init__)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:468(validate_whiskers)\n", " 1 0.000 0.000 0.000 0.000 base.py:4614(_add_comparison_methods)\n", " 7 0.000 0.000 0.000 0.000 base.py:4862(_add_logical_methods_disabled)\n", " 4 0.000 0.000 0.000 0.000 function_base.py:1912(__init__)\n", " 3 0.000 0.000 0.002 0.001 textwrap.py:368(wrap)\n", " 1 0.000 0.000 0.000 0.000 parquet.py:1()\n", " 1 0.000 0.000 0.005 0.005 table_schema.py:5()\n", " 1 0.000 0.000 0.001 0.001 window.py:60(_Window)\n", " 1 0.000 0.000 0.000 0.000 {built-in method posix.mkdir}\n", " 1 0.000 0.000 0.000 0.000 _compat.py:3()\n", " 1 0.000 0.000 0.000 0.000 numeric.py:215(UInt64Index)\n", " 6 0.000 0.000 0.000 0.000 category.py:798(_make_compare)\n", " 36 0.000 0.000 0.000 0.000 config.py:725(wrap)\n", " 1 0.000 0.000 6.083 6.083 py3compat.py:184(execfile)\n", " 5 0.000 0.000 0.003 0.001 textwrap.py:342(wrap)\n", " 6 0.000 0.000 0.000 0.000 warnings.py:468(__exit__)\n", " 1 0.000 0.000 0.000 0.000 {method 'readline' of '_io.BufferedReader' objects}\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2982(Table)\n", " 1 0.000 0.000 0.000 0.000 excel.py:788(ExcelWriter)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:1718(AffineBase)\n", " 1 0.000 0.000 0.000 0.000 colors.py:632(LinearSegmentedColormap)\n", " 2 0.000 0.000 0.006 0.003 pyparsing.py:4916(srange)\n", " 1 0.000 0.000 0.001 0.001 _style.py:3()\n", " 1 0.000 0.000 0.000 0.000 internals.py:2293(ObjectBlock)\n", " 5 0.000 0.000 0.000 0.000 generic.py:2456(_create_indexer)\n", " 1 0.000 0.000 0.000 0.000 base.py:174(SelectionMixin)\n", " 7 0.000 0.000 0.000 0.000 genericpath.py:16(exists)\n", " 6 0.000 0.000 0.000 0.000 ops.py:1126(_comp_method_SERIES)\n", " 1 0.000 0.000 0.001 0.001 groupby.py:2768(BinGrouper)\n", " 5 0.000 0.000 0.004 0.001 config.py:769(is_instance_factory)\n", " 1 0.000 0.000 0.000 0.000 lazy.py:118()\n", " 5 0.000 0.000 0.000 0.000 textwrap.py:179()\n", " 1 0.000 0.000 0.000 0.000 scipy_sparse.py:5()\n", " 4 0.000 0.000 0.001 0.000 __init__.py:734(gen_candidates)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2355(Milli)\n", " 2 0.000 0.000 0.000 0.000 base.py:4624(_add_numeric_methods_add_sub_disabled)\n", " 43 0.000 0.000 0.000 0.000 {built-in method builtins.callable}\n", " 25 0.000 0.000 0.000 0.000 {method 'end' of '_sre.SRE_Match' objects}\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:1113(ParserElement)\n", " 5 0.000 0.000 0.000 0.000 fontconfig_pattern.py:144(_families)\n", " 1 0.000 0.000 0.000 0.000 _core.py:2712(SeriesPlotMethods)\n", " 1 0.000 0.000 0.000 0.000 {pandas._libs.tslibs.offsets._get_calendar}\n", " 7 0.000 0.000 0.000 0.000 {pandas._libs.tslibs.offsets.apply_index_wraps}\n", " 1 0.000 0.000 0.001 0.001 accessor.py:7()\n", " 15 0.000 0.000 0.000 0.000 config.py:226(__call__)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:389(wrapper)\n", " 3 0.000 0.000 0.000 0.000 __init__.py:1212(_fixupParents)\n", " 1 0.000 0.000 0.000 0.000 tokenize.py:448(open)\n", " 22 0.000 0.000 0.000 0.000 {method 'group' of '_sre.SRE_Match' objects}\n", " 21 0.000 0.000 0.000 0.000 {method 'groupdict' of '_sre.SRE_Match' objects}\n", " 8 0.000 0.000 0.000 0.000 rrule.py:77()\n", " 2 0.000 0.000 0.001 0.000 pyparsing.py:4603(delimitedList)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:921(validate_webagg_address)\n", " 8 0.000 0.000 0.000 0.000 __init__.py:390(_logged_cached)\n", " 4 0.000 0.000 0.041 0.010 __init__.py:826(__init__)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2345(Minute)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2350(Second)\n", " 1 0.000 0.000 0.124 0.124 index.py:2()\n", " 2 0.000 0.000 0.000 0.000 cast.py:971(maybe_cast_to_datetime)\n", " 4 0.000 0.000 0.000 0.000 _collections_abc.py:664(__contains__)\n", " 2 0.000 0.000 0.000 0.000 _version.py:7()\n", " 7 0.000 0.000 0.000 0.000 pyparsing.py:4978(tokenMap)\n", " 6 0.000 0.000 0.001 0.000 pyparsing.py:1214(setResultsName)\n", " 9 0.000 0.000 0.000 0.000 pyparsing.py:3994(__str__)\n", " 1 0.000 0.000 0.001 0.001 datetimelike.py:99(TimelikeOps)\n", " 21 0.000 0.000 0.000 0.000 datetimes.py:63(_field_accessor)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2365(Nano)\n", " 1 0.000 0.000 0.001 0.001 nanops.py:69(_f)\n", " 12 0.000 0.000 0.000 0.000 typing.py:889(__extrahook__)\n", " 2 0.000 0.000 0.002 0.001 textwrap.py:381(fill)\n", " 7 0.000 0.000 0.000 0.000 __init__.py:1286(debug)\n", " 7 0.000 0.000 0.000 0.000 __init__.py:1544(isEnabledFor)\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:430(__setitem__)\n", " 1 0.000 0.000 0.001 0.001 __init__.py:703(matplotlib_fname)\n", " 1 0.000 0.000 0.000 0.000 _tools.py:3()\n", " 6 0.000 0.000 0.000 0.000 period.py:77(_period_index_cmp)\n", " 20 0.000 0.000 0.000 0.000 ops.py:945()\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2360(Micro)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:521(__init__)\n", " 3 0.000 0.000 0.000 0.000 __init__.py:1268(__init__)\n", " 30 0.000 0.000 0.000 0.000 {method 'reverse' of 'list' objects}\n", " 1 0.000 0.000 0.000 0.000 series.py:365(_set_axis)\n", " 12 0.000 0.000 0.000 0.000 ops.py:1296(_bool_method_SERIES)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1319(Week)\n", " 20 0.000 0.000 0.000 0.000 base.py:4736(_make_evaluate_unary)\n", " 11 0.000 0.000 0.000 0.000 version.py:51(__lt__)\n", " 3 0.000 0.000 0.000 0.000 iostream.py:195(schedule)\n", " 3 0.000 0.000 0.000 0.000 posixpath.py:154(dirname)\n", " 15 0.000 0.000 0.000 0.000 {built-in method sys.intern}\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:1863(__radd__)\n", " 3 0.000 0.000 0.001 0.000 pyparsing.py:1899(__mul__)\n", " 1 0.000 0.000 0.000 0.000 numeric.py:157(Int64Index)\n", " 18 0.000 0.000 0.000 0.000 strings.py:1874(do_copy)\n", " 5 0.000 0.000 0.000 0.000 missing.py:112(_isna_new)\n", " 1 0.000 0.000 0.000 0.000 common.py:21()\n", " 1 0.000 0.000 0.000 0.000 numeric.py:2171(identity)\n", " 14 0.000 0.000 0.000 0.000 {method 'get' of 'mappingproxy' objects}\n", " 1 0.000 0.000 0.001 0.001 pickle.py:1()\n", " 7 0.000 0.000 0.000 0.000 pyparsing.py:3164(__init__)\n", " 3 0.000 0.000 0.000 0.000 series.py:4019(_sanitize_array)\n", " 1 0.000 0.000 0.000 0.000 range.py:574(_add_numeric_methods_binary)\n", " 15 0.000 0.000 0.000 0.000 strings.py:1835(_noarg_wrapper)\n", " 15 0.000 0.000 0.000 0.000 config.py:606(_warn_if_deprecated)\n", " 7 0.000 0.000 0.000 0.000 config.py:799()\n", " 1 0.000 0.000 0.000 0.000 tzinfo.py:1()\n", " 5 0.000 0.000 0.000 0.000 textwrap.py:115(__init__)\n", " 5 0.000 0.000 0.000 0.000 warnings.py:143(simplefilter)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:419(HDFStore)\n", " 5 0.000 0.000 0.000 0.000 pyparsing.py:3269(parseImpl)\n", " 7 0.000 0.000 0.000 0.000 pyparsing.py:4088(__init__)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:268(validate_toolbar)\n", " 1 0.000 0.000 0.002 0.002 series.py:1221(__unicode__)\n", " 1 0.000 0.000 0.000 0.000 frozen.py:9()\n", " 1 0.000 0.000 0.000 0.000 groupby.py:2173(BaseGrouper)\n", " 1 0.000 0.000 0.001 0.001 _decorators.py:1()\n", " 7 0.000 0.000 0.000 0.000 config.py:802(inner)\n", " 7 0.000 0.000 0.000 0.000 config.py:797(is_one_of_factory)\n", " 13 0.000 0.000 0.000 0.000 contextlib.py:79(__enter__)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2084(IdentityTransform)\n", " 1 0.000 0.000 0.000 0.000 cycler.py:191(_from_iter)\n", " 1 0.000 0.000 0.000 0.000 gzip.py:4()\n", " 1 0.000 0.000 0.000 0.000 numeric.py:26(NumericIndex)\n", " 1 0.000 0.000 0.000 0.000 timedeltas.py:182(_add_comparison_methods)\n", " 24 0.000 0.000 0.000 0.000 config.py:761(inner)\n", " 7 0.000 0.000 0.000 0.000 config.py:800()\n", " 4 0.000 0.000 0.000 0.000 __init__.py:1838(getLogger)\n", " 1 0.000 0.000 0.000 0.000 parsers.py:713(TextFileReader)\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:3982(__init__)\n", " 1 0.000 0.000 0.000 0.000 format.py:169(__init__)\n", " 1 0.000 0.000 0.000 0.000 category.py:794(_add_comparison_methods)\n", " 23 0.000 0.000 0.000 0.000 function.py:31(__init__)\n", " 12 0.000 0.000 0.000 0.000 {built-in method pandas._libs.lib.is_scalar}\n", " 5 0.000 0.000 0.001 0.000 textwrap.py:336(_split_chunks)\n", " 2 0.000 0.000 0.000 0.000 decoder.py:334(decode)\n", " 2 0.000 0.000 0.000 0.000 weakref.py:288(update)\n", " 5 0.000 0.000 0.000 0.000 _collections_abc.py:657(get)\n", " 6 0.000 0.000 0.000 0.000 {method 'remove' of 'list' objects}\n", " 14 0.000 0.000 0.000 0.000 {method 'lstrip' of 'str' objects}\n", " 1 0.000 0.000 0.000 0.000 expr.py:717(Expr)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2344(CompositeGenericTransform)\n", " 3 0.000 0.000 0.000 0.000 cycler.py:225(__iter__)\n", " 1 0.000 0.000 0.001 0.001 pyparsing.py:5864(pyparsing_unicode)\n", " 4 0.000 0.000 0.000 0.000 timedeltas.py:163(_join_i8_wrapper)\n", " 7 0.000 0.000 0.001 0.000 base.py:4914(_ensure_index)\n", " 9 0.000 0.000 0.000 0.000 common.py:513(is_categorical_dtype)\n", " 12 0.000 0.000 0.000 0.000 {method 'expandtabs' of 'str' objects}\n", " 1 0.000 0.000 0.000 0.000 {method 'read' of '_io.BufferedReader' objects}\n", " 1 0.000 0.000 0.000 0.000 pytables.py:3805(LegacyTable)\n", " 11 0.000 0.000 0.000 0.000 _compat.py:9(_mpl_version)\n", " 1 0.000 0.000 0.000 0.000 format.py:206(_get_footer)\n", " 1 0.000 0.000 0.000 0.000 timedeltas.py:3()\n", " 15 0.000 0.000 0.000 0.000 _decorators.py:60()\n", " 1 0.000 0.000 0.000 0.000 cast.py:1207(construct_1d_object_array_from_listlike)\n", " 15 0.000 0.000 0.000 0.000 config.py:593(_translate_key)\n", " 1 0.000 0.000 0.000 0.000 printing.py:12(adjoin)\n", " 4 0.000 0.000 0.000 0.000 numeric.py:2491(seterr)\n", " 6 0.000 0.000 0.000 0.000 warnings.py:428(__init__)\n", " 1 0.000 0.000 0.000 0.000 normalize.py:4()\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2242(Fixed)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:1095(LockableBbox)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:312(__call__)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:88(_NDFrameIndexer)\n", " 1 0.000 0.000 0.000 0.000 period.py:226(_add_comparison_methods)\n", " 1 0.000 0.000 0.000 0.000 {pandas._libs.missing.isnaobj}\n", " 15 0.000 0.000 0.000 0.000 config.py:532(_select_options)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:3()\n", " 1 0.000 0.000 0.001 0.001 lazy.py:1()\n", " 19 0.000 0.000 0.000 0.000 inspect.py:2500(name)\n", " 8 0.000 0.000 0.000 0.000 posixpath.py:41(_get_sep)\n", " 10 0.000 0.000 0.000 0.000 {method 'encode' of 'str' objects}\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2409(GenericFixed)\n", " 1 0.000 0.000 0.000 0.000 parsers.py:1234(ParserBase)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:1224(Transform)\n", " 1 0.000 0.000 0.000 0.000 fontconfig_pattern.py:30(FontconfigPatternParser)\n", " 1 0.000 0.000 0.000 0.000 series.py:166(__init__)\n", " 1 0.000 0.000 0.000 0.000 frame.py:7547(_list_to_arrays)\n", " 1 0.000 0.000 0.001 0.001 hashing.py:3()\n", " 1 0.000 0.000 0.000 0.000 base.py:16(ExtensionArray)\n", " 7 0.000 0.000 0.000 0.000 common.py:369(is_datetime64tz_dtype)\n", " 1 0.000 0.000 0.000 0.000 inference.py:1()\n", " 1 0.000 0.000 0.000 0.000 posixpath.py:331(normpath)\n", " 1 0.000 0.000 0.001 0.001 pytables.py:4334(AppendableMultiFrameTable)\n", " 6 0.000 0.000 0.000 0.000 timedeltas.py:56(_td_index_cmp)\n", " 18 0.000 0.000 0.000 0.000 strings.py:1871(copy)\n", " 1 0.000 0.000 0.000 0.000 :5(Components)\n", " 2 0.000 0.000 0.000 0.000 iostream.py:382(write)\n", " 3 0.000 0.000 0.000 0.000 pathlib.py:1343(is_dir)\n", " 19 0.000 0.000 0.000 0.000 inspect.py:2504(default)\n", " 2 0.000 0.000 0.000 0.000 six.py:91(__get__)\n", " 7 0.000 0.000 0.000 0.000 __init__.py:1530(getEffectiveLevel)\n", " 3 0.000 0.000 0.000 0.000 codecs.py:308(__init__)\n", " 17 0.000 0.000 0.000 0.000 {built-in method _warnings._filters_mutated}\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2826(SparseSeriesFixed)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:1488(IndexCol)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:1829(__init__)\n", " 1 0.000 0.000 0.000 0.000 clipboards.py:1()\n", " 1 0.000 0.000 0.000 0.000 window.py:772(_GroupByMixin)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2131(BlendedGenericTransform)\n", " 3 0.000 0.000 0.000 0.000 pyparsing.py:3230(__init__)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:607(validate_hinting)\n", " 2 0.000 0.000 0.000 0.000 pyparsing.py:678()\n", " 1 0.000 0.000 0.000 0.000 internals.py:4631(SingleBlockManager)\n", " 4 0.000 0.000 0.000 0.000 generic.py:9005()\n", " 1 0.000 0.000 0.000 0.000 indexing.py:1361(_IXIndexer)\n", " 3 0.000 0.000 0.000 0.000 datetimelike.py:817(_add_datetimelike_methods)\n", " 1 0.000 0.000 0.002 0.002 dtypes.py:383(DatetimeTZDtype)\n", " 1 0.000 0.000 0.002 0.002 printing.py:3()\n", " 1 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.zeros}\n", " 7 0.000 0.000 0.000 0.000 _common.py:9(__init__)\n", " 2 0.000 0.000 0.000 0.000 pathlib.py:998(__new__)\n", " 2 0.000 0.000 0.000 0.000 parsers.py:538(_make_parser_function)\n", " 1 0.000 0.000 0.000 0.000 util.py:1()\n", " 8 0.000 0.000 0.000 0.000 rcsetup.py:284(__init__)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:890(validate_hist_bins)\n", " 7 0.000 0.000 0.000 0.000 pyparsing.py:470(__iter__)\n", " 3 0.000 0.000 0.000 0.000 nanops.py:64(check)\n", " 5 0.000 0.000 0.000 0.000 dtypes.py:707(is_dtype)\n", " 1 0.000 0.000 0.000 0.000 {built-in method pandas._libs.lib.is_bool}\n", " 7 0.000 0.000 0.000 0.000 pathlib.py:691(__str__)\n", " 19 0.000 0.000 0.000 0.000 inspect.py:2512(kind)\n", " 2 0.000 0.000 0.000 0.000 decoder.py:345(raw_decode)\n", " 2 0.000 0.000 0.000 0.000 util.py:73(compose)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:1621(TransformWrapper)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:108(validate_path_exists)\n", " 4 0.000 0.000 0.000 0.000 fontconfig_pattern.py:138(_name)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2990(SparseBlock)\n", " 7 0.000 0.000 0.000 0.000 offsets.py:2219(__init__)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:616(BusinessHourMixin)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:202(_get_values)\n", " 4 0.000 0.000 0.000 0.000 function_base.py:241(iterable)\n", " 2 0.000 0.000 0.000 0.000 pathlib.py:651(_from_parts)\n", " 5 0.000 0.000 0.000 0.000 {built-in method _struct.calcsize}\n", " 1 0.000 0.000 0.000 0.000 feather_format.py:1()\n", " 1 0.000 0.000 0.000 0.000 _print_versions.py:1()\n", " 5 0.000 0.000 0.000 0.000 pyparsing.py:3098(__str__)\n", " 8 0.000 0.000 0.001 0.000 pyparsing.py:4936()\n", " 16 0.000 0.000 0.000 0.000 pyparsing.py:327(__getitem__)\n", " 1 0.000 0.000 0.001 0.001 _core.py:1558(BoxPlot)\n", " 3 0.000 0.000 0.000 0.000 {built-in method _csv.register_dialect}\n", " 6 0.000 0.000 0.000 0.000 datetimes.py:103(_dt_index_cmp)\n", " 1 0.000 0.000 0.000 0.000 datetimes.py:292(_add_comparison_methods)\n", " 1 0.000 0.000 0.000 0.000 category.py:849(_add_accessors)\n", " 3 0.000 0.000 0.000 0.000 common.py:1688(is_extension_array_dtype)\n", " 4 0.000 0.000 0.000 0.000 __init__.py:205(iteritems)\n", " 4 0.000 0.000 0.000 0.000 pathlib.py:282(splitroot)\n", " 1 0.000 0.000 0.000 0.000 shutil.py:1048(get_terminal_size)\n", " 4 0.000 0.000 0.000 0.000 __init__.py:219(_acquireLock)\n", " 3 0.000 0.000 0.000 0.000 threading.py:1062(_wait_for_tstate_lock)\n", " 1 0.000 0.000 0.000 0.000 _depr_module.py:4()\n", " 11 0.000 0.000 0.000 0.000 rcsetup.py:661(__init__)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:65(register)\n", " 1 0.000 0.000 0.001 0.001 format.py:861(format_array)\n", " 1 0.000 0.000 0.001 0.001 frame.py:6845(_reduce)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:513(BusinessDay)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1110(SemiMonthOffset)\n", " 11 0.000 0.000 0.000 0.000 base.py:641(__len__)\n", " 6 0.000 0.000 0.000 0.000 accessor.py:125(__init__)\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:11(PandasExtensionDtype)\n", " 2 0.000 0.000 0.000 0.000 dtypes.py:401(__new__)\n", " 5 0.000 0.000 0.000 0.000 {method 'any' of 'numpy.ndarray' objects}\n", " 5 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.empty}\n", " 1 0.000 0.000 0.000 0.000 tokenize.py:385(find_cookie)\n", " 4 0.000 0.000 0.000 0.000 __init__.py:228(_releaseLock)\n", " 5 0.000 0.000 0.000 0.000 {method 'keys' of 'dict' objects}\n", " 1 0.000 0.000 0.000 0.000 window.py:1682(RollingGroupby)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2253(BlendedAffine2D)\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:2042(__invert__)\n", " 2 0.000 0.000 0.001 0.000 __init__.py:409(wrapper)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:656(_get_data_path)\n", " 1 0.000 0.000 0.000 0.000 internals.py:3265(__init__)\n", " 1 0.000 0.000 0.000 0.000 frame.py:7644(_homogenize)\n", " 1 0.000 0.000 0.000 0.000 groupby.py:2911(Grouping)\n", " 6 0.000 0.000 0.000 0.000 common.py:477(is_interval_dtype)\n", " 2 0.000 0.000 0.000 0.000 posixpath.py:232(expanduser)\n", " 12 0.000 0.000 0.000 0.000 {method 'clear' of 'dict' objects}\n", " 1 0.000 0.000 0.000 0.000 date_converters.py:1()\n", " 1 0.000 0.000 0.000 0.000 excel.py:332(ExcelFile)\n", " 1 0.000 0.000 0.000 0.000 colors.py:1186(PowerNorm)\n", " 11 0.000 0.000 0.000 0.000 _color_data.py:31()\n", " 9 0.000 0.000 0.000 0.000 rcsetup.py:336(validate_color_or_auto)\n", " 2 0.000 0.000 0.000 0.000 format.py:997(base_formatter)\n", " 2 0.000 0.000 0.000 0.000 internals.py:116(__init__)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2545(CategoricalBlock)\n", " 4 0.000 0.000 0.000 0.000 timedeltas.py:42(_field_accessor)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1777(FY5253)\n", " 1 0.000 0.000 0.000 0.000 base.py:2382(_format_with_header)\n", " 2 0.000 0.000 0.000 0.000 common.py:1936(_validate_date_like_dtype)\n", " 3 0.000 0.000 0.000 0.000 {built-in method _socket.inet_aton}\n", " 1 0.000 0.000 0.000 0.000 pytables.py:3930(AppendableTable)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:4227(AppendableSeriesTable)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:1790(DataCol)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:1019(TransformedBbox)\n", " 1 0.000 0.000 0.000 0.000 :5(Boxplot)\n", " 2 0.000 0.000 0.000 0.000 pyparsing.py:1298(addParseAction)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3127(__init__)\n", " 3 0.000 0.000 0.000 0.000 pyparsing.py:4445(__init__)\n", " 2 0.000 0.000 0.000 0.000 format.py:1404()\n", " 1 0.000 0.000 0.000 0.000 internals.py:5017(_stack_arrays)\n", " 1 0.000 0.000 0.559 0.559 __init__.py:2()\n", " 1 0.000 0.000 0.001 0.001 dtypes.py:497(PeriodDtype)\n", " 1 0.000 0.000 0.000 0.000 {built-in method pandas._libs.algos.ensure_object}\n", " 1 0.000 0.000 0.000 0.000 pathlib.py:814(with_name)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:1()\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1810(MaxNLocator)\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:3081(parseImpl)\n", " 1 0.000 0.000 0.001 0.001 format.py:262(to_string)\n", " 1 0.000 0.000 0.000 0.000 internals.py:4986(_multi_blockify)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1513(LastWeekOfMonth)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1968(FY5253Quarter)\n", " 1 0.000 0.000 0.001 0.001 dtypes.py:614(IntervalDtype)\n", " 1 0.000 0.000 0.000 0.000 numeric.py:630(require)\n", " 4 0.000 0.000 0.000 0.000 numeric.py:2592(geterr)\n", " 2 0.000 0.000 0.000 0.000 six.py:159(_resolve)\n", " 3 0.000 0.000 0.000 0.000 threading.py:1104(is_alive)\n", " 3 0.000 0.000 0.000 0.000 os.py:746(decode)\n", " 1 0.000 0.000 0.002 0.002 {built-in method builtins.print}\n", " 2 0.000 0.000 0.000 0.000 {built-in method _functools.reduce}\n", " 1 0.000 0.000 0.000 0.000 sasreader.py:3()\n", " 1 0.000 0.000 0.000 0.000 pytables.py:69(BinOp)\n", " 2 0.000 0.000 0.000 0.000 expr.py:135(_is_type)\n", " 1 0.000 0.000 0.000 0.000 excel.py:992(_OpenpyxlWriter)\n", " 12 0.000 0.000 0.000 0.000 plotting.py:11(outer)\n", " 1 0.000 0.000 0.000 0.000 rrule.py:1300(rruleset)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:808(LogFormatter)\n", " 2/1 0.000 0.000 0.000 0.000 deprecation.py:248(wrapper)\n", " 4 0.000 0.000 0.000 0.000 rcsetup.py:501(validate_ps_distiller)\n", " 5 0.000 0.000 0.000 0.000 fontconfig_pattern.py:145()\n", " 1 0.000 0.000 0.002 0.002 series.py:1240(to_string)\n", " 4 0.000 0.000 0.000 0.000 internals.py:3148(get_block_type)\n", " 2 0.000 0.000 0.000 0.000 frame.py:7615(convert)\n", " 1 0.000 0.000 0.000 0.000 range.py:68(__new__)\n", " 4 0.000 0.000 0.000 0.000 strings.py:1849(_pat_wrapper)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:924(CustomBusinessHour)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:698(_get_counts)\n", " 4 0.000 0.000 0.000 0.000 common.py:195(is_categorical)\n", " 1 0.000 0.000 0.000 0.000 tzfile.py:4()\n", " 1 0.000 0.000 0.000 0.000 pathlib.py:1241(mkdir)\n", " 1 0.000 0.000 0.000 0.000 posixpath.py:369(abspath)\n", " 3 0.000 0.000 0.000 0.000 _collections_abc.py:72(_check_methods)\n", " 1 0.000 0.000 0.000 0.000 {built-in method posix.get_terminal_size}\n", " 1 0.000 0.000 0.000 0.000 _tester.py:3()\n", " 1 0.000 0.000 0.000 0.000 sql.py:515(SQLTable)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:1777(Affine2DBase)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:1865(Affine2D)\n", " 6 0.000 0.000 0.000 0.000 cycler.py:371()\n", " 11 0.000 0.000 0.000 0.000 cycler.py:212()\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5885(Japanese)\n", " 3 0.000 0.000 0.000 0.000 pyparsing.py:2067(suppress)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3057(__init__)\n", " 2 0.000 0.000 0.000 0.000 __init__.py:666(get_candidate_paths)\n", " 6 0.000 0.000 0.000 0.000 format.py:306(len)\n", " 1 0.000 0.000 0.000 0.000 internals.py:3488(_verify_integrity)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:1519(_LocIndexer)\n", " 1 0.000 0.000 0.000 0.000 internals.py:1717(NonConsolidatableMixIn)\n", " 3 0.000 0.000 0.000 0.000 internals.py:3307(shape)\n", " 1 0.000 0.000 0.001 0.001 generic.py:9599(stat_func)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1692(YearOffset)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:878(__init__)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1011(_CustomBusinessMonth)\n", " 2 0.000 0.000 0.000 0.000 nanops.py:493(_nanminmax)\n", " 4 0.000 0.000 0.000 0.000 common.py:227(is_datetimetz)\n", " 7 0.000 0.000 0.000 0.000 config.py:789(inner)\n", " 2 0.000 0.000 0.001 0.001 textwrap.py:356(fill)\n", " 1 0.000 0.000 0.000 0.000 tokenize.py:355(detect_encoding)\n", " 6 0.000 0.000 0.000 0.000 {method 'pop' of 'dict' objects}\n", " 1 0.000 0.000 0.000 0.000 excel.py:1442(_XlwtWriter)\n", " 3 0.000 0.000 0.000 0.000 pytables.py:1530(set_name)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1399(Locator)\n", " 1 0.000 0.000 0.000 0.000 cycler.py:55(_process_keys)\n", " 1 0.000 0.000 0.000 0.000 deprecation.py:1()\n", " 6 0.000 0.000 0.001 0.000 rcsetup.py:329(validate_color_or_inherit)\n", " 1 0.000 0.000 0.000 0.000 _core.py:933(LinePlot)\n", " 2 0.000 0.000 0.000 0.000 _style.py:119(__init__)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:1063(ZipFile)\n", " 1 0.000 0.000 0.000 0.000 format.py:1402(_cond)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2125(DatetimeLikeBlockMixin)\n", " 2 0.000 0.000 0.000 0.000 internals.py:3191(make_block)\n", " 2 0.000 0.000 0.000 0.000 generic.py:124(__init__)\n", " 4 0.000 0.000 0.000 0.000 datetimes.py:282(_join_i8_wrapper)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1585(QuarterOffset)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:953(MonthOffset)\n", " 3 0.000 0.000 0.000 0.000 base.py:654(_dispatch)\n", " 1 0.000 0.000 0.002 0.002 base.py:7()\n", " 2 0.000 0.000 0.000 0.000 dtypes.py:459(construct_from_string)\n", " 12 0.000 0.000 0.000 0.000 algorithms.py:1253(_view_wrapper)\n", " 2 0.000 0.000 0.000 0.000 {built-in method pandas._libs.missing.checknull}\n", " 11 0.000 0.000 0.000 0.000 __init__.py:259(u)\n", " 3 0.000 0.000 0.000 0.000 pathlib.py:1153(stat)\n", " 3 0.000 0.000 0.000 0.000 __init__.py:685(__init__)\n", " 3 0.000 0.000 0.000 0.000 _collections_abc.py:252(__subclasshook__)\n", " 6 0.000 0.000 0.000 0.000 {method 'insert' of 'list' objects}\n", " 8 0.000 0.000 0.000 0.000 {method 'values' of 'dict' objects}\n", " 1 0.000 0.000 0.000 0.000 :5(ExtType)\n", " 1 0.000 0.000 0.000 0.000 sql.py:916(SQLDatabase)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2747(TransformedPath)\n", " 1 0.000 0.000 0.000 0.000 rrule.py:1105(_iterinfo)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1169(EngFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1283(PercentFormatter)\n", " 1 0.000 0.000 0.000 0.000 rrule.py:93(rrulebase)\n", " 4 0.000 0.000 0.000 0.000 deprecation.py:189(finalize)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:644(validate_sketch)\n", " 1 0.000 0.000 0.033 0.033 __init__.py:936(rc_params)\n", " 1 0.000 0.000 0.000 0.000 internals.py:3922(as_array)\n", " 1 0.000 0.000 0.000 0.000 internals.py:4639(__init__)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2778(DatetimeTZBlock)\n", " 9 0.000 0.000 0.000 0.000 internals.py:3309()\n", " 2 0.000 0.000 0.000 0.000 generic.py:4378(__setattr__)\n", " 1 0.000 0.000 0.000 0.000 range.py:131(_simple_new)\n", " 5 0.000 0.000 0.000 0.000 range.py:481(__len__)\n", " 8 0.000 0.000 0.000 0.000 range.py:578(_make_evaluate_binop)\n", " 1 0.000 0.000 0.000 0.000 groupby.py:355(Grouper)\n", " 1 0.000 0.000 0.000 0.000 _validators.py:4()\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:278(validate_ordered)\n", " 3 0.000 0.000 0.000 0.000 common.py:1170(is_datetime_or_timedelta_dtype)\n", " 4 0.000 0.000 0.000 0.000 common.py:1527(is_float_dtype)\n", " 2 0.000 0.000 0.000 0.000 config.py:481(deprecate_option)\n", " 1 0.000 0.000 0.000 0.000 :5(RegisteredOption)\n", " 1 0.000 0.000 0.000 0.000 terminal.py:13()\n", " 2 0.000 0.000 0.000 0.000 {method 'sum' of 'numpy.ndarray' objects}\n", " 2 0.000 0.000 0.000 0.000 pathlib.py:674(_format_parsed_parts)\n", " 3 0.000 0.000 0.000 0.000 functools.py:448(lru_cache)\n", " 4 0.000 0.000 0.000 0.000 {method 'acquire' of '_thread.RLock' objects}\n", " 1 0.000 0.000 0.000 0.000 {built-in method posix.access}\n", " 1 0.000 0.000 0.000 0.000 stata.py:1908(StataWriter)\n", " 1 0.000 0.000 0.000 0.000 parsers.py:1680(CParserWrapper)\n", " 1 0.000 0.000 0.000 0.000 merge.py:484(_MergeOperation)\n", " 1 0.000 0.000 0.000 0.000 engines.py:41(AbstractEngine)\n", " 1 0.000 0.000 0.000 0.000 align.py:44(_filter_special_cases)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:231(TickHelper)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1031(RRuleLocator)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5844(unicode_set)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:1308(addCondition)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:484(update_savefig_format)\n", " 1 0.000 0.000 0.000 0.000 format.py:373(DataFrameFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1368(_make_fixed_width)\n", " 1 0.000 0.000 0.001 0.001 internals.py:4869(create_block_manager_from_arrays)\n", " 1 0.000 0.000 0.000 0.000 internals.py:1852(ExtensionBlock)\n", " 1 0.000 0.000 0.000 0.000 groupby.py:4928(PanelGroupBy)\n", " 1 0.000 0.000 0.000 0.000 groupby.py:5099(NDFrameSplitter)\n", " 1 0.000 0.000 0.000 0.000 base.py:1976(is_all_dates)\n", " 5 0.000 0.000 0.000 0.000 common.py:89(is_object_dtype)\n", " 2 0.000 0.000 0.000 0.000 common.py:1405(needs_i8_conversion)\n", " 3 0.000 0.000 0.000 0.000 common.py:1578(is_bool_dtype)\n", " 1 0.000 0.000 0.000 0.000 :5(DeprecatedOption)\n", " 1 0.000 0.000 0.000 0.000 terminal.py:24(get_terminal_size)\n", " 3 0.000 0.000 0.000 0.000 __init__.py:190(_checkLevel)\n", " 1 0.000 0.000 0.000 0.000 sre_parse.py:885(addgroup)\n", " 2 0.000 0.000 0.000 0.000 _bootlocale.py:23(getpreferredencoding)\n", " 1 0.000 0.000 0.006 0.006 testing.py:5()\n", " 1 0.000 0.000 0.000 0.000 sql.py:1342(SQLiteDatabase)\n", " 1 0.000 0.000 0.000 0.000 expr.py:698(PandasExprVisitor)\n", " 1 0.000 0.000 0.000 0.000 scope.py:80(Scope)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1527(FixedLocator)\n", " 1 0.000 0.000 0.000 0.000 colors.py:1510(LightSource)\n", " 1 0.000 0.000 0.000 0.000 cycler.py:112(__init__)\n", " 3 0.000 0.000 0.000 0.000 pyparsing.py:2048(__call__)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2798(Regex)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:631(validate_bbox)\n", " 1 0.000 0.000 0.000 0.000 gzip.py:110(GzipFile)\n", " 1 0.000 0.000 0.000 0.000 _style.py:107(_Options)\n", " 3 0.000 0.000 0.000 0.000 internals.py:4718(dtype)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:465(__init__)\n", " 6 0.000 0.000 0.000 0.000 nanops.py:834(make_nancomp)\n", " 6 0.000 0.000 0.000 0.000 categorical.py:63(_cat_compare_op)\n", " 1 0.000 0.000 0.000 0.000 categorical.py:2349(CategoricalAccessor)\n", " 5 0.000 0.000 0.000 0.000 common.py:1784(_get_dtype)\n", " 1 0.000 0.000 0.000 0.000 {pandas._libs.lib.values_from_object}\n", " 1 0.000 0.000 0.032 0.032 __init__.py:4()\n", " 1 0.000 0.000 0.000 0.000 {method 'transpose' of 'numpy.ndarray' objects}\n", " 2 0.000 0.000 0.000 0.000 locale.py:379(normalize)\n", " 2 0.000 0.000 0.000 0.000 locale.py:565(getlocale)\n", " 2 0.000 0.000 0.000 0.000 six.py:80(_import_module)\n", " 2 0.000 0.000 0.000 0.000 sre_parse.py:452()\n", " 3 0.000 0.000 0.000 0.000 codecs.py:259(__init__)\n", " 4 0.000 0.000 0.000 0.000 {method 'decode' of 'bytes' objects}\n", " 2 0.000 0.000 0.000 0.000 {built-in method _locale.nl_langinfo}\n", " 1 0.000 0.000 0.000 0.000 stata.py:2666(StataWriter117)\n", " 1 0.000 0.000 0.000 0.000 json.py:582(Parser)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:4160(AppendableFrameTable)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:4285(GenericTable)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:379(ExprVisitor)\n", " 1 0.000 0.000 0.000 0.000 ops.py:156(Constant)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1591(LinearLocator)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:55(TransformNode)\n", " 6 0.000 0.000 0.000 0.000 pyparsing.py:4449(postParse)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:324()\n", " 1 0.000 0.000 0.000 0.000 __init__.py:687(get_data_path)\n", " 3 0.000 0.000 0.000 0.000 __init__.py:1876(_str_equal)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:53(get_pairs)\n", " 3 0.000 0.000 0.000 0.000 series.py:412(dtype)\n", " 3 0.000 0.000 0.000 0.000 series.py:4036(_try_cast)\n", " 1 0.000 0.000 0.000 0.000 format.py:189(_chk_truncate)\n", " 1 0.000 0.000 0.001 0.001 format.py:915(get_result)\n", " 3 0.000 0.000 0.000 0.000 internals.py:3473(__len__)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2627(DatetimeBlock)\n", " 1 0.000 0.000 0.000 0.000 internals.py:3351(_is_single_block)\n", " 1 0.000 0.000 0.001 0.001 frame.py:7349(_arrays_to_mgr)\n", " 1 0.000 0.000 0.000 0.000 frame.py:7453(_to_arrays)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1441(WeekOfMonth)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:855(CustomBusinessDay)\n", " 1 0.000 0.000 0.000 0.000 base.py:473(_simple_new)\n", " 6 0.000 0.000 0.000 0.000 nanops.py:92(__init__)\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:156(_finalize)\n", " 5 0.000 0.000 0.000 0.000 missing.py:32(isna)\n", " 1 0.000 0.000 0.000 0.000 common.py:301(_asarray_tuplesafe)\n", " 1 0.000 0.000 0.000 0.000 _decorators.py:212(Substitution)\n", " 3 0.000 0.000 0.000 0.000 common.py:444(is_period_dtype)\n", " 3 0.000 0.000 0.000 0.000 common.py:546(is_string_dtype)\n", " 1 0.000 0.000 0.000 0.000 console.py:14(detect_console_encoding)\n", " 1 0.000 0.000 0.001 0.001 console.py:3()\n", " 6 0.000 0.000 0.000 0.000 __init__.py:275(strlen)\n", " 8 0.000 0.000 0.000 0.000 basic.py:1902(_preorder_traversal)\n", " 8 0.000 0.000 0.000 0.000 {built-in method numpy.core.umath.geterrobj}\n", " 2 0.000 0.000 0.000 0.000 numeric.py:2887(__init__)\n", " 2 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.arange}\n", " 2 0.000 0.000 0.000 0.000 version.py:69(__ge__)\n", " 2 0.000 0.000 0.000 0.000 locale.py:462(_parse_localename)\n", " 1 0.000 0.000 0.000 0.000 locale.py:631(getpreferredencoding)\n", " 7 0.000 0.000 0.000 0.000 {method 'copy' of 'dict' objects}\n", " 2 0.000 0.000 0.000 0.000 {method 'title' of 'str' objects}\n", " 1 0.000 0.000 0.000 0.000 testing.py:59(set_testing_mode)\n", " 1 0.000 0.000 0.000 0.000 json.py:159(JSONTableWriter)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:4371(AppendablePanelTable)\n", " 1 0.000 0.000 0.000 0.000 reshape.py:35(_Unstacker)\n", " 1 0.000 0.000 0.000 0.000 rrule.py:1409(_rrulestr)\n", " 1 0.000 0.000 0.000 0.000 dates.py:951(DateLocator)\n", " 1 0.000 0.000 0.000 0.000 _version.py:20(get_versions)\n", " 1 0.000 0.000 0.000 0.000 colors.py:427(Colormap)\n", " 2 0.000 0.000 0.000 0.000 pyparsing.py:3265(__init__)\n", " 4 0.000 0.000 0.000 0.000 rcsetup.py:249(validate_backend)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:361(PandasAutoDateFormatter)\n", " 2 0.000 0.000 0.000 0.000 format.py:343(_get_adjustment)\n", " 1 0.000 0.000 0.000 0.000 internals.py:5755(JoinUnit)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2150(TimeDeltaBlock)\n", " 3 0.000 0.000 0.000 0.000 generic.py:255()\n", " 1 0.000 0.000 0.000 0.000 frozen.py:17(FrozenList)\n", " 6 0.000 0.000 0.000 0.000 offsets.py:2207(_tick_comp)\n", " 1 0.000 0.000 0.000 0.000 easter.py:5()\n", " 1 0.000 0.000 0.000 0.000 groupby.py:5073(FrameSplitter)\n", " 1 0.000 0.000 0.000 0.000 base.py:2394()\n", " 4 0.000 0.000 0.000 0.000 nanops.py:72()\n", " 1 0.000 0.000 0.000 0.000 base.py:8(_DtypeOpsMixin)\n", " 4 0.000 0.000 0.000 0.000 common.py:122(is_sparse)\n", " 3 0.000 0.000 0.000 0.000 common.py:811(is_integer_dtype)\n", " 1 0.000 0.000 0.000 0.000 tzinfo.py:18(memorized_timedelta)\n", " 1 0.000 0.000 0.000 0.000 chainmap.py:1()\n", " 5 0.000 0.000 0.000 0.000 _methods.py:42(_any)\n", " 2 0.000 0.000 0.000 0.000 numeric.py:2891(__enter__)\n", " 2 0.000 0.000 0.000 0.000 iostream.py:307(_is_master_process)\n", " 2 0.000 0.000 0.000 0.000 traitlets.py:526(get)\n", " 3 0.000 0.000 0.000 0.000 abc.py:9(abstractmethod)\n", " 2 0.000 0.000 0.000 0.000 {built-in method _locale.setlocale}\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2789(SeriesFixed)\n", " 1 0.000 0.000 0.000 0.000 ops.py:178(Op)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2474(CompositeAffine2D)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:2541(AutoMinorLocator)\n", " 1 0.000 0.000 0.000 0.000 cycler.py:529(_cycler)\n", " 1 0.000 0.000 0.000 0.000 units.py:87(ConversionInterface)\n", " 1 0.000 0.000 0.000 0.000 colors.py:870(Normalize)\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:4536(postParse)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:153(validate_float_or_None)\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:325(__init__)\n", " 2 0.000 0.000 0.000 0.000 pyparsing.py:467(__len__)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:308(__init__)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:578(_get_xdg_config_dir)\n", " 1 0.000 0.000 0.001 0.001 __init__.py:625(get_configdir)\n", " 1 0.000 0.000 0.000 0.000 _core.py:1164(BarPlot)\n", " 3 0.000 0.000 0.000 0.000 series.py:401(name)\n", " 1 0.000 0.000 0.000 0.000 format.py:1396(_trim_zeros)\n", " 1 0.000 0.000 0.000 0.000 generic.py:8873(_add_series_only_operations)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:2162(Easter)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:837(BusinessHour)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1092(CustomBusinessMonthEnd)\n", " 1 0.000 0.000 0.000 0.000 groupby.py:5019(DataSplitter)\n", " 2 0.000 0.000 0.000 0.000 base.py:2067(__getitem__)\n", " 1 0.000 0.000 0.000 0.000 accessor.py:13(DirNamesMixin)\n", " 1 0.000 0.000 0.000 0.000 base.py:85(PandasObject)\n", " 1 0.000 0.000 0.001 0.001 nanops.py:103(f)\n", " 3 0.000 0.000 0.000 0.000 dtypes.py:584(is_dtype)\n", " 1 0.000 0.000 0.000 0.000 common.py:123(_default_index)\n", " 3 0.000 0.000 0.000 0.000 cast.py:853(maybe_castable)\n", " 3 0.000 0.000 0.000 0.000 common.py:407(is_timedelta64_dtype)\n", " 2 0.000 0.000 0.000 0.000 inference.py:251(is_list_like)\n", " 4 0.000 0.000 0.000 0.000 {built-in method numpy.core.umath.seterrobj}\n", " 4 0.000 0.000 0.000 0.000 numeric.py:433(asarray)\n", " 2 0.000 0.000 0.000 0.000 numeric.py:701()\n", " 1 0.000 0.000 0.000 0.000 syspathcontext.py:48(__enter__)\n", " 1 0.000 0.000 0.000 0.000 pathlib.py:664(_from_parsed_parts)\n", " 2 0.000 0.000 0.000 0.000 traitlets.py:545(__get__)\n", " 1 0.000 0.000 0.000 0.000 tokenize.py:344(_get_normal_name)\n", " 1 0.000 0.000 0.000 0.000 re.py:314(_compile_repl)\n", " 1 0.000 0.000 0.000 0.000 posixpath.py:64(isabs)\n", " 3 0.000 0.000 0.000 0.000 {method 'update' of 'set' objects}\n", " 3 0.000 0.000 0.000 0.000 {method 'acquire' of '_thread.lock' objects}\n", " 1 0.000 0.000 0.000 0.000 _version.py:22(get_versions)\n", " 1 0.000 0.000 0.000 0.000 html.py:147(_HtmlFrameParser)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:3912(LegacyFrameTable)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:4270(AppendableMultiSeriesTable)\n", " 1 0.000 0.000 0.000 0.000 merge.py:1254(_AsOfMerge)\n", " 3 0.000 0.000 0.000 0.000 util.py:68(_compose2)\n", " 1 0.000 0.000 0.000 0.000 window.py:807(_Rolling)\n", " 1 0.000 0.000 0.000 0.000 window.py:1942(ExpandingGroupby)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2654(BboxTransformToMaxOnly)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1144(LogitFormatter)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1136(AutoDateLocator)\n", " 4 0.000 0.000 0.000 0.000 rrule.py:80(_invalidates_cache)\n", " 1 0.000 0.000 0.000 0.000 colors.py:306(ColorConverter)\n", " 1 0.000 0.000 0.000 0.000 colors.py:94()\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4067(__str__)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:596(validate_svg_fonttype)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:867(Grouper)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:1769(Locked)\n", " 1 0.000 0.000 0.000 0.000 _core.py:804(PlanePlot)\n", " 1 0.000 0.000 0.000 0.000 _core.py:1492(PiePlot)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:403(MilliSecondLocator)\n", " 1 0.000 0.000 0.000 0.000 csv.py:81(DictReader)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:1915(_iLocIndexer)\n", " 1 0.000 0.000 0.000 0.000 generic.py:4436(f)\n", " 1 0.000 0.000 0.001 0.001 frame.py:6856(f)\n", " 1 0.000 0.000 0.000 0.000 frame.py:7604(_convert_object_array)\n", " 3 0.000 0.000 0.000 0.000 generic.py:258()\n", " 2 0.000 0.000 0.000 0.000 range.py:84(_ensure_int)\n", " 2 0.000 0.000 0.000 0.000 base.py:672(values)\n", " 1 0.000 0.000 0.000 0.000 base.py:2367(format)\n", " 2 0.000 0.000 0.000 0.000 dtypes.py:266(construct_from_string)\n", " 1 0.000 0.000 0.000 0.000 common.py:250(groupby)\n", " 4 0.000 0.000 0.000 0.000 config.py:222(__init__)\n", " 2 0.000 0.000 0.000 0.000 printing.py:52()\n", " 2 0.000 0.000 0.000 0.000 iostream.py:320(_schedule_flush)\n", " 1 0.000 0.000 0.000 0.000 syspathcontext.py:55(__exit__)\n", " 3 0.000 0.000 0.000 0.000 pathlib.py:1008(_init)\n", " 1 0.000 0.000 0.000 0.000 threading.py:74(RLock)\n", " 1 0.000 0.000 0.000 0.000 sre_parse.py:784(_parse_flags)\n", " 2 0.000 0.000 0.000 0.000 {built-in method builtins.delattr}\n", " 2 0.000 0.000 0.000 0.000 {method 'search' of '_sre.SRE_Pattern' objects}\n", " 1 0.000 0.000 6.083 6.083 :1()\n", " 1 0.000 0.000 0.000 0.000 json.py:431(JsonReader)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2850(SparseFrameFixed)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2883(BlockManagerFixed)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:30(Term)\n", " 3 0.000 0.000 0.000 0.000 expr.py:208(disallow)\n", " 1 0.000 0.000 0.000 0.000 ops.py:539(FuncNode)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:205(_DummyAxis)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:338(FixedFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1498(IndexLocator)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1669(Base)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:2057(LogLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:846(rrulewrapper)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1574(MicrosecondLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1766(DateConverter)\n", " 1 0.000 0.000 0.000 0.000 rrule.py:304(rrule)\n", " 1 0.000 0.000 0.000 0.000 colors.py:1068(SymLogNorm)\n", " 4 0.000 0.000 0.000 0.000 pyparsing.py:5833(__init__)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:1477(_UnboundedCache)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3322(ParseExpression)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3434(And)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4115(Optional)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:168(validate_string_or_None)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:209(validate_int_or_None)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:422(validate_fontsize_None)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:215(ParseBaseException)\n", " 2 0.000 0.000 0.000 0.000 pyparsing.py:654(__getattr__)\n", " 1 0.000 0.000 0.001 0.001 __init__.py:932(copy)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:956(is_url)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:629(Stack)\n", " 1 0.000 0.000 0.000 0.000 _core.py:1288(BarhPlot)\n", " 1 0.000 0.000 0.000 0.000 _core.py:1310(HistPlot)\n", " 1 0.000 0.000 0.000 0.000 csv.py:166(Sniffer)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:760(ZipExtFile)\n", " 1 0.000 0.000 0.000 0.000 format.py:245(_get_formatted_index)\n", " 3 0.000 0.000 0.000 0.000 format.py:309(justify)\n", " 1 0.000 0.000 0.000 0.000 format.py:312(adjoin)\n", " 1 0.000 0.000 0.000 0.000 format.py:1049()\n", " 1 0.000 0.000 0.000 0.000 internals.py:3784(_consolidate_check)\n", " 2 0.000 0.000 0.000 0.000 internals.py:5020(_asarray_compat)\n", " 6 0.000 0.000 0.000 0.000 internals.py:233(mgr_locs)\n", " 1 0.000 0.000 0.000 0.000 internals.py:1694(ScalarBlock)\n", " 1 0.000 0.000 0.000 0.000 generic.py:4423(_protect_consolidate)\n", " 1 0.000 0.000 0.000 0.000 generic.py:364(_get_axis_number)\n", " 1 0.000 0.000 0.000 0.000 datetimelike.py:57(DatelikeOps)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1419(_WeekOfMonthMixin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:478(BusinessMixin)\n", " 4 0.000 0.000 0.000 0.000 missing.py:458(_interp_wrapper)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:808(_ensure_numeric)\n", " 1 0.000 0.000 0.000 0.000 base.py:98(ExtensionDtype)\n", " 2 0.000 0.000 0.000 0.000 missing.py:259(notna)\n", " 2 0.000 0.000 0.000 0.000 common.py:332(is_datetime64_dtype)\n", " 1 0.000 0.000 0.000 0.000 {pandas._libs.lib.infer_dtype}\n", " 1 0.000 0.000 0.000 0.000 printing.py:56()\n", " 3 0.000 0.000 0.000 0.000 printing.py:47(justify)\n", " 3 0.000 0.000 0.000 0.000 printing.py:59(_join_unicode)\n", " 1 0.000 0.000 0.000 0.000 terminal.py:52(is_terminal)\n", " 1 0.000 0.000 0.000 0.000 tzinfo.py:156(DstTzInfo)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:124(lrange)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:192(UTC)\n", " 2 0.000 0.000 0.000 0.000 numeric.py:2896(__exit__)\n", " 3 0.000 0.000 0.000 0.000 iostream.py:93(_event_pipe)\n", " 1 0.000 0.000 0.000 0.000 builtin_trap.py:46(__exit__)\n", " 1 0.000 0.000 0.000 0.000 tokenize.py:379(read_or_stop)\n", " 1 0.000 0.000 0.000 0.000 {method 'copy' of 'mappingproxy' objects}\n", " 2 0.000 0.000 0.000 0.000 {method 'setdefault' of 'dict' objects}\n", " 4 0.000 0.000 0.000 0.000 {method 'release' of '_thread.RLock' objects}\n", " 2 0.000 0.000 0.000 0.000 {built-in method posix.getpid}\n", " 1 0.000 0.000 0.000 0.000 {built-in method _codecs.lookup}\n", " 3 0.000 0.000 0.000 0.000 {built-in method _stat.S_ISDIR}\n", " 1 0.000 0.000 0.000 0.000 {method 'seek' of '_io.BufferedReader' objects}\n", " 1 0.000 0.000 0.000 0.000 testing.py:2560(RNGContext)\n", " 1 0.000 0.000 0.000 0.000 parquet.py:174(FastParquetImpl)\n", " 3 0.000 0.000 0.000 0.000 api.py:69(outer)\n", " 1 0.000 0.000 0.000 0.000 html.py:425(_BeautifulSoupHtml5LibFrameParser)\n", " 1 0.000 0.000 0.000 0.000 html.py:532(_LxmlFrameParser)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2967(FrameFixed)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2972(PanelFixed)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:3784(WORMTable)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:4688(Selection)\n", " 1 0.000 0.000 0.000 0.000 excel.py:1711(_XlsxWriter)\n", " 3 0.000 0.000 0.000 0.000 pytables.py:1545(set_pos)\n", " 1 0.000 0.000 0.000 0.000 parsers.py:3338(FixedWidthReader)\n", " 1 0.000 0.000 0.000 0.000 ops.py:478(UnaryOp)\n", " 1 0.000 0.000 0.000 0.000 concat.py:229(_Concatenator)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:252(Formatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:327(NullFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1711(MultipleLocator)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:2256(SymmetricalLogLocator)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:2604(OldAutoLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:338(bytespdate2num)\n", " 1 0.000 0.000 0.000 0.000 dates.py:747(AutoDateFormatter)\n", " 1 0.000 0.000 0.000 0.000 units.py:52(AxisInfo)\n", " 1 0.000 0.000 0.000 0.000 units.py:136(__init__)\n", " 3 0.000 0.000 0.000 0.000 cycler.py:145(change_key)\n", " 1 0.000 0.000 0.008 0.008 pyparsing.py:5057(makeHTMLTags)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2398(__init__)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2461(Keyword)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2567(CloseMatch)\n", " 3 0.000 0.000 0.000 0.000 pyparsing.py:3130()\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3217(__init__)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3279(WordStart)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3589(MatchFirst)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3781(ParseElementEnhance)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4294(Forward)\n", " 1 0.000 0.000 0.000 0.000 rcsetup.py:46(ValidateInStrings)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:190(validate_dpi)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:256(validate_qt4)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:413(validate_aspect)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:1424(is_interactive)\n", " 1 0.000 0.000 0.000 0.000 _core.py:837(ScatterPlot)\n", " 1 0.000 0.000 0.000 0.000 _core.py:1092(AreaPlot)\n", " 1 0.000 0.000 0.000 0.000 _core.py:1436(KdePlot)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:158(TimeConverter)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:231(PeriodConverter)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:291(DatetimeConverter)\n", " 1 0.000 0.000 0.000 0.000 accessors.py:114(DatetimeProperties)\n", " 1 0.000 0.000 0.000 0.000 accessors.py:189(TimedeltaProperties)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:318(ZipInfo)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:534(_ZipDecrypter)\n", " 1 0.000 0.000 0.001 0.001 format.py:257(_get_formatted_values)\n", " 2 0.000 0.000 0.000 0.000 format.py:303(__init__)\n", " 1 0.000 0.000 0.000 0.000 format.py:901(__init__)\n", " 1 0.000 0.000 0.000 0.000 format.py:972(__init__)\n", " 3 0.000 0.000 0.000 0.000 format.py:1078()\n", " 1 0.000 0.000 0.001 0.001 format.py:1099(_format_strings)\n", " 3 0.000 0.000 0.000 0.000 format.py:1376()\n", " 1 0.000 0.000 0.000 0.000 format.py:1391()\n", " 1 0.000 0.000 0.000 0.000 internals.py:3785()\n", " 4 0.000 0.000 0.000 0.000 internals.py:4684(_block)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:1460(_LocationIndexer)\n", " 1 0.000 0.000 0.000 0.000 internals.py:356(ftype)\n", " 1 0.000 0.000 0.000 0.000 internals.py:3266()\n", " 1 0.000 0.000 0.000 0.000 generic.py:4563(values)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:29(get_indexers_list)\n", " 1 0.000 0.000 0.000 0.000 frame.py:7621()\n", " 1 0.000 0.000 0.000 0.000 frozen.py:84(FrozenNDArray)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1099(CustomBusinessMonthBegin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1201(SemiMonthEnd)\n", " 2 0.000 0.000 0.000 0.000 base.py:635(_reset_identity)\n", " 1 0.000 0.000 0.002 0.002 base.py:52(__str__)\n", " 1 0.000 0.000 0.000 0.000 base.py:37(StringMixin)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:58(disallow)\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:137(__init__)\n", " 2 0.000 0.000 0.000 0.000 _decorators.py:183(rewrite_axis_style_signature)\n", " 1 0.000 0.000 0.000 0.000 common.py:907(is_unsigned_integer_dtype)\n", " 1 0.000 0.000 0.000 0.000 common.py:995(is_int_or_datetime_dtype)\n", " 5 0.000 0.000 0.000 0.000 config.py:747(is_type_factory)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:371(_FixedOffset)\n", " 1 0.000 0.000 0.000 0.000 {method 'copy' of 'numpy.ndarray' objects}\n", " 2 0.000 0.000 0.000 0.000 {built-in method numpy.core.multiarray.datetime_data}\n", " 1 0.000 0.000 0.000 0.000 pathlib.py:777(name)\n", " 1 0.000 0.000 0.000 0.000 {built-in method sys.getdefaultencoding}\n", " 1 0.000 0.000 0.000 0.000 {method 'fileno' of '_io.TextIOWrapper' objects}\n", " 1 0.000 0.000 0.000 0.000 testing.py:2053(optional_args)\n", " 1 0.000 0.000 0.000 0.000 testing.py:2688(SubclassedSparseDataFrame)\n", " 1 0.000 0.000 0.000 0.000 stata.py:595(StataValueLabel)\n", " 1 0.000 0.000 0.000 0.000 stata.py:2479(StataStrLWriter)\n", " 1 0.000 0.000 0.000 0.000 json.py:817(FrameParser)\n", " 1 0.000 0.000 0.000 0.000 sql.py:1230(SQLiteTable)\n", " 1 0.000 0.000 0.000 0.000 parquet.py:43(BaseImpl)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:226(FilterBinOp)\n", " 1 0.000 0.000 0.000 0.000 json.py:75(Writer)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:1391(TableIterator)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:1769(GenericIndexCol)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2212(DataIndexableCol)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2760(LegacyFixed)\n", " 1 0.000 0.000 0.000 0.000 ops.py:315(BinOp)\n", " 1 0.000 0.000 0.000 0.000 ops.py:523(MathCall)\n", " 1 0.000 0.000 0.000 0.000 _depr_module.py:10(_DeprecatedModule)\n", " 1 0.000 0.000 0.000 0.000 engines.py:101(NumExprEngine)\n", " 1 0.000 0.000 0.000 0.000 engines.py:133(PythonEngine)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2563(BboxTransform)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2611(BboxTransformTo)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2675(BboxTransformFrom)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2715(ScaledTranslation)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:391(FormatStrFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1767(_Edge_integer)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:2429(LogitLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:723(IndexDateFormatter)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1370(YearLocator)\n", " 1 0.000 0.000 0.000 0.000 colors.py:781(ListedColormap)\n", " 1 0.000 0.000 0.000 0.000 colors.py:1252(BoundaryNorm)\n", " 1 0.000 0.000 0.000 0.000 colors.py:72(_ColorMapping)\n", " 3 0.000 0.000 0.000 0.000 pyparsing.py:3132()\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3112(White)\n", " 1 0.000 0.000 0.001 0.001 pyparsing.py:3419(setResultsName)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3510(Or)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4488(__init__)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:103(validate_any)\n", " 3 0.000 0.000 0.000 0.000 rcsetup.py:262(validate_qt5)\n", " 2 0.000 0.000 0.000 0.000 pyparsing.py:676()\n", " 1 0.000 0.000 0.000 0.000 __init__.py:292()\n", " 1 0.000 0.000 0.000 0.000 __init__.py:313(set_level)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:1264(rc_context)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:70(_StrongRef)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:88(CallbackRegistry)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:2020(_OrderedSet)\n", " 1 0.000 0.000 0.000 0.000 gzip.py:69(_PaddedFile)\n", " 1 0.000 0.000 0.000 0.000 gzip.py:377(_GzipReader)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:187(TimeFormatter)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:1065(TimeSeries_DateFormatter)\n", " 1 0.000 0.000 0.000 0.000 csv.py:24(Dialect)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:989(_ZipWriteFile)\n", " 3 0.000 0.000 0.000 0.000 series.py:111(_coerce_method)\n", " 1 0.000 0.000 0.000 0.000 format.py:351(TableFormatter)\n", " 2 0.000 0.000 0.000 0.000 format.py:1385(just)\n", " 1 0.000 0.000 0.000 0.000 format.py:1411()\n", " 1 0.000 0.000 0.000 0.000 format.py:1421(EngFormatter)\n", " 1 0.000 0.000 0.000 0.000 common.py:453(MMapWrapper)\n", " 2 0.000 0.000 0.000 0.000 internals.py:3490()\n", " 1 0.000 0.000 0.000 0.000 internals.py:4748(formatting_values)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:2126(_ScalarAccessIndexer)\n", " 2 0.000 0.000 0.000 0.000 internals.py:127(_check_ndim)\n", " 1 0.000 0.000 0.000 0.000 internals.py:209(formatting_values)\n", " 4 0.000 0.000 0.000 0.000 internals.py:352(dtype)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2037(FloatBlock)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2088(ComplexBlock)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2267(BoolBlock)\n", " 2 0.000 0.000 0.000 0.000 internals.py:3311(ndim)\n", " 4 0.000 0.000 0.000 0.000 internals.py:3384(_get_items)\n", " 1 0.000 0.000 0.000 0.000 generic.py:4362(__getattr__)\n", " 4 0.000 0.000 0.000 0.000 interval.py:1394(_setop)\n", " 1 0.000 0.000 0.000 0.000 sorting.py:269(_KeyMapper)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1646(BQuarterEnd)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:451(SingleConstructorOffset)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1260(SemiMonthBegin)\n", " 1 0.000 0.000 0.000 0.000 groupby.py:531(GroupByPlot)\n", " 1 0.000 0.000 0.000 0.000 base.py:651(GroupByMixin)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:266(_wrap_results)\n", " 1 0.000 0.000 0.000 0.000 _decorators.py:344(docstring_wrapper)\n", " 1 0.000 0.000 0.000 0.000 algorithms.py:1070(SelectN)\n", " 1 0.000 0.000 0.000 0.000 common.py:858(is_signed_integer_dtype)\n", " 1 0.000 0.000 0.000 0.000 config.py:178(__init__)\n", " 1 0.000 0.000 0.000 0.000 inference.py:119(is_iterator)\n", " 1 0.000 0.000 0.000 0.000 tzinfo.py:76(StaticTzInfo)\n", " 1 0.000 0.000 0.000 0.000 tzfile.py:13(_byte_string)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:211(itervalues)\n", " 2 0.000 0.000 0.000 0.000 _methods.py:34(_sum)\n", " 2 0.000 0.000 0.000 0.000 {method 'fill' of 'numpy.ndarray' objects}\n", " 1 0.000 0.000 0.000 0.000 syspathcontext.py:45(__init__)\n", " 1 0.000 0.000 0.000 0.000 {built-in method utcfromtimestamp}\n", " 1 0.000 0.000 0.000 0.000 platform.py:1147(_sys_version)\n", " 1 0.000 0.000 0.000 0.000 platform.py:1254(python_implementation)\n", " 4 0.000 0.000 0.000 0.000 {method 'ljust' of 'str' objects}\n", " 1 0.000 0.000 0.000 0.000 {built-in method builtins.sum}\n", " 1 0.000 0.000 0.000 0.000 packers.py:759(Unpacker)\n", " 1 0.000 0.000 0.000 0.000 gbq.py:1()\n", " 1 0.000 0.000 0.000 0.000 testing.py:2376(_AssertRaisesContextmanager)\n", " 1 0.000 0.000 0.000 0.000 testing.py:2652(SubclassedSeries)\n", " 1 0.000 0.000 0.000 0.000 testing.py:2700(SubclassedCategorical)\n", " 1 0.000 0.000 0.000 0.000 json.py:773(SeriesParser)\n", " 1 0.000 0.000 0.000 0.000 parquet.py:73(PyArrowImpl)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2813(SparseFixed)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:3923(LegacyPanelTable)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:20(Scope)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:298(ConditionBinOp)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:482(Expr)\n", " 1 0.000 0.000 0.000 0.000 expr.py:709(PythonExprVisitor)\n", " 1 0.000 0.000 0.000 0.000 json.py:116(SeriesWriter)\n", " 1 0.000 0.000 0.000 0.000 json.py:134(FrameWriter)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:1871(set_metadata)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2413()\n", " 1 0.000 0.000 0.000 0.000 parsers.py:3436(FixedWidthFieldParser)\n", " 1 0.000 0.000 0.000 0.000 ops.py:445(Div)\n", " 1 0.000 0.000 0.000 0.000 ops.py:32(UndefinedVariableError)\n", " 1 0.000 0.000 0.000 0.000 panel.py:1552(WidePanel)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:305(IndexFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:371(FuncFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:431(OldScalarFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1059(LogFormatterMathtext)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1124(LogFormatterSciNotation)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1571(NullLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:322(strpdate2num)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1434(MonthLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1459(WeekdayLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1487(DayLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1514(HourLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1554(SecondLocator)\n", " 1 0.000 0.000 0.000 0.000 rrule.py:66(weekday)\n", " 1 0.000 0.000 0.000 0.000 units.py:132(Registry)\n", " 1 0.000 0.000 0.000 0.000 colors.py:997(LogNorm)\n", " 1 0.000 0.000 0.000 0.000 colors.py:1338(NoNorm)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5904(Thai)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2405(NoMatch)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2420(Literal)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2526(CaselessLiteral)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2549(CaselessKeyword)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2628(Word)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2904(QuotedString)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3041(CharsNotIn)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3196(LineStart)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3226(LineEnd)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3250(__init__)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3657(Each)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3858(FollowedBy)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3887(PrecededBy)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3960(NotAny)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4003(_MultipleMatch)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4041(OneOrMore)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4076(ZeroOrMore)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4107(_NullToken)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4387(Combine)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4543(OnlyOnce)\n", " 1 0.000 0.000 0.000 0.000 rcsetup.py:283(validate_nseq_float)\n", " 1 0.000 0.000 0.000 0.000 rcsetup.py:309(__init__)\n", " 1 0.000 0.000 0.000 0.000 rcsetup.py:308(validate_nseq_int)\n", " 1 0.000 0.000 0.000 0.000 rcsetup.py:657(ValidateInterval)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:196()\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:316(RecursiveGrammarException)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:324(_ParseResultsWithOffset)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:265(_parse_commandline)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:225(silent_list)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:1274()\n", " 1 0.000 0.000 0.000 0.000 _core.py:901(HexBinPlot)\n", " 1 0.000 0.000 0.000 0.000 _core.py:2703(BasePlotMethods)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:981(TimeSeries_DateLocator)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:1135(TimeSeries_TimedeltaFormatter)\n", " 1 0.000 0.000 0.000 0.000 series.py:470(_formatting_values)\n", " 2 0.000 0.000 0.000 0.000 series.py:562(__len__)\n", " 1 0.000 0.000 0.000 0.000 accessors.py:24(Properties)\n", " 1 0.000 0.000 0.000 0.000 series.py:391(_set_subtyp)\n", " 1 0.000 0.000 0.000 0.000 format.py:113(CategoricalFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:167(SeriesFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:317(EastAsianTextAdjustment)\n", " 1 0.000 0.000 0.000 0.000 format.py:982(_value_formatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1403()\n", " 1 0.000 0.000 0.000 0.000 common.py:431(BytesZipFile)\n", " 1 0.000 0.000 0.000 0.000 internals.py:4101(_consolidate_inplace)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:2162(_AtIndexer)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:2233(_iAtIndexer)\n", " 1 0.000 0.000 0.000 0.000 internals.py:213(get_values)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2107(IntBlock)\n", " 1 0.000 0.000 0.000 0.000 generic.py:4433(_consolidate_inplace)\n", " 1 0.000 0.000 0.000 0.000 frame.py:7047(_get_agg_axis)\n", " 1 0.000 0.000 0.000 0.000 multi.py:50(MultiIndexUIntEngine)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1669(QuarterEnd)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1744(BYearEnd)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1760(YearEnd)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:987(MonthEnd)\n", " 1 0.000 0.000 0.000 0.000 base.py:615(is_)\n", " 1 0.000 0.000 0.000 0.000 base.py:2445(equals)\n", " 1 0.000 0.000 0.000 0.000 accessor.py:43(PandasDelegate)\n", " 1 0.000 0.000 0.000 0.000 base.py:131(NoNewAttributesMixin)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:90(bottleneck_switch)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:256(_na_ok_dtype)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:23(UnsortedIndexError)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:33(ParserError)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:164(AbstractMethodError)\n", " 1 0.000 0.000 0.000 0.000 common.py:1043(is_datetime64_any_dtype)\n", " 1 0.000 0.000 0.000 0.000 config.py:220(CallableDynamicDoc)\n", " 1 0.000 0.000 0.000 0.000 printing.py:33()\n", " 1 0.000 0.000 0.000 0.000 inference.py:388(is_named_tuple)\n", " 1 0.000 0.000 0.000 0.000 api.py:72(TimeGrouper)\n", " 2 0.000 0.000 0.000 0.000 lazy.py:91(LazyList)\n", " 1 0.000 0.000 0.000 0.000 lazy.py:71(LazyList)\n", " 2 0.000 0.000 0.000 0.000 lazy.py:144(LazySet)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:387(add_metaclass)\n", " 1 0.000 0.000 0.000 0.000 {method 'astype' of 'numpy.ndarray' objects}\n", " 3 0.000 0.000 0.000 0.000 {method 'ravel' of 'numpy.ndarray' objects}\n", " 2 0.000 0.000 0.000 0.000 {method 'view' of 'numpy.ndarray' objects}\n", " 1 0.000 0.000 0.000 0.000 builtin_trap.py:39(__enter__)\n", " 3 0.000 0.000 0.000 0.000 threading.py:506(is_set)\n", " 1 0.000 0.000 0.000 0.000 sre_parse.py:98(checkgroup)\n", " 1 0.000 0.000 0.000 0.000 {method 'startswith' of 'bytes' objects}\n", " 2 0.000 0.000 0.000 0.000 {method 'rjust' of 'str' objects}\n", " 3 0.000 0.000 0.000 0.000 {method 'append' of 'collections.deque' objects}\n", " 1 0.000 0.000 0.000 0.000 pickle_compat.py:129(Unpickler)\n", " 1 0.000 0.000 0.000 0.000 packers.py:743(Packer)\n", " 1 0.000 0.000 0.000 0.000 packers.py:777(Iterator)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:9(ExtType)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:1(UnpackException)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:9(OutOfData)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:13(UnpackValueError)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:17(ExtraData)\n", " 1 0.000 0.000 0.000 0.000 testing.py:2047(TestSubDict)\n", " 1 0.000 0.000 0.000 0.000 testing.py:2278(SimpleMock)\n", " 1 0.000 0.000 0.000 0.000 testing.py:2664(SubclassedDataFrame)\n", " 1 0.000 0.000 0.000 0.000 testing.py:2676(SubclassedSparseSeries)\n", " 1 0.000 0.000 0.000 0.000 stata.py:471(PossiblePrecisionLoss)\n", " 1 0.000 0.000 0.000 0.000 stata.py:481(ValueLabelTypeMismatch)\n", " 1 0.000 0.000 0.000 0.000 stata.py:840(StataParser)\n", " 1 0.000 0.000 0.000 0.000 sql.py:30(SQLAlchemyRequired)\n", " 1 0.000 0.000 0.000 0.000 sql.py:34(DatabaseError)\n", " 1 0.000 0.000 0.000 0.000 sql.py:902(PandasSQL)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:59(Constant)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:355(UnaryOp)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:571(TermValue)\n", " 1 0.000 0.000 0.000 0.000 expr.py:250(add_ops)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:117(PossibleDataLossError)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:121(ClosedFileError)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:125(IncompatibilityWarning)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:136(AttributeConflictWarning)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:146(DuplicateWarning)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:1856(set_data)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2234(GenericDataIndexableCol)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2779(LegacyFrameFixed)\n", " 1 0.000 0.000 0.000 0.000 merge.py:1151(_OrderedMerge)\n", " 1 0.000 0.000 0.000 0.000 engines.py:19(NumExprClobberingError)\n", " 1 0.000 0.000 0.000 0.000 common.py:23(NameResolutionError)\n", " 1 0.000 0.000 0.000 0.000 panel.py:1562(LongPanel)\n", " 1 0.000 0.000 0.000 0.000 transforms.py:2821(TransformedPatchPath)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:410(StrMethodFormatter)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:1043(LogFormatterExponent)\n", " 1 0.000 0.000 0.000 0.000 ticker.py:2521(AutoLocator)\n", " 1 0.000 0.000 0.000 0.000 dates.py:1534(MinuteLocator)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5832(_lazyclassproperty)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5867(Latin1)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5872(Greek)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5882(Chinese)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5888(Kanji)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5891(Hiragana)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5894(Katakana)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5897(Korean)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5907(Arabic)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5910(Hebrew)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2386(Token)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2394(Empty)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:2787(Char)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3163(_PositionToken)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3170(GoToColumn)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3246(StringStart)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3261(StringEnd)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3299(WordEnd)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:3450(_ErrorStop)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4179(SkipTo)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4379(TokenConverter)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4431(Group)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4452(Dict)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4517(Suppress)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:271(ParseException)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:297(ParseSyntaxException)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:491(checkdep_ps_distiller)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:315(Bunch)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:505(GetRealpathAndStat)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:609(maxdict)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:1784(TimeoutError)\n", " 1 0.000 0.000 0.000 0.000 deprecation.py:6(MatplotlibDeprecationWarning)\n", " 1 0.000 0.000 0.000 0.000 _converter.py:377(PandasAutoDateLocator)\n", " 1 0.000 0.000 0.000 0.000 accessors.py:266(PeriodProperties)\n", " 1 0.000 0.000 0.000 0.000 accessors.py:291(CombinedDatetimelikeProperties)\n", " 1 0.000 0.000 0.000 0.000 csv.py:55(excel)\n", " 1 0.000 0.000 0.000 0.000 csv.py:70(unix_dialect)\n", " 1 0.000 0.000 0.000 0.000 csv.py:131(DictWriter)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:43(BadZipFile)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:595(LZMACompressor)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:618(LZMADecompressor)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:740(_Tellable)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:1819(PyZipFile)\n", " 1 0.000 0.000 0.000 0.000 format.py:301(TextAdjustment)\n", " 1 0.000 0.000 0.000 0.000 format.py:899(GenericArrayFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:967(FloatArrayFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1115(Datetime64Formatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1141(IntervalArrayFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1299(Datetime64TZFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1314(Timedelta64Formatter)\n", " 1 0.000 0.000 0.000 0.000 common.py:61(BaseIterator)\n", " 1 0.000 0.000 0.000 0.000 common.py:496(UTF8Recoder)\n", " 2 0.000 0.000 0.000 0.000 internals.py:3776(is_consolidated)\n", " 1 0.000 0.000 0.000 0.000 internals.py:4085(consolidate)\n", " 2 0.000 0.000 0.000 0.000 internals.py:4990()\n", " 1 0.000 0.000 0.000 0.000 internals.py:5026(_shape_compat)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2021(NumericBlock)\n", " 1 0.000 0.000 0.000 0.000 internals.py:2027(FloatOrComplexBlock)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:45(_IndexSlice)\n", " 1 0.000 0.000 0.000 0.000 indexing.py:84(IndexingError)\n", " 1 0.000 0.000 0.000 0.000 frame.py:320(_constructor)\n", " 1 0.000 0.000 0.000 0.000 multi.py:89(MultiIndexPyIntEngine)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1660(BQuarterBegin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1681(QuarterBegin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1752(BYearBegin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1767(YearBegin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:460(_CustomMixin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:993(MonthBegin)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:999(BusinessMonthEnd)\n", " 1 0.000 0.000 0.000 0.000 offsets.py:1005(BusinessMonthBegin)\n", " 1 0.000 0.000 0.000 0.000 groupby.py:5067(SeriesSplitter)\n", " 1 0.000 0.000 0.000 0.000 base.py:163(InvalidIndexError)\n", " 1 0.000 0.000 0.000 0.000 base.py:162(GroupByError)\n", " 1 0.000 0.000 0.000 0.000 base.py:166(DataError)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:48(set_use_bottleneck)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:260(_view_if_needed)\n", " 1 0.000 0.000 0.000 0.000 function.py:38(__call__)\n", " 1 0.000 0.000 0.000 0.000 function.py:29(CompatValidator)\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:376(DatetimeTZDtypeType)\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:490(PeriodDtypeType)\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:607(IntervalDtypeType)\n", " 1 0.000 0.000 0.000 0.000 generic.py:64(_ABCGeneric)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:10(PerformanceWarning)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:16(UnsupportedFunctionCall)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:39(DtypeWarning)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:97(EmptyDataError)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:104(ParserWarning)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:145(MergeError)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:152(NullFrequencyError)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:160(AccessorRegistrationWarning)\n", " 1 0.000 0.000 0.000 0.000 common.py:23(SettingWithCopyError)\n", " 1 0.000 0.000 0.000 0.000 _decorators.py:273(Appender)\n", " 1 0.000 0.000 0.000 0.000 algorithms.py:1096(SelectNSeries)\n", " 1 0.000 0.000 0.000 0.000 algorithms.py:1157(SelectNFrame)\n", " 1 0.000 0.000 0.000 0.000 common.py:1490(is_string_like_dtype)\n", " 2 0.000 0.000 0.000 0.000 {built-in method pandas._libs.lib.is_integer}\n", " 1 0.000 0.000 0.000 0.000 config.py:69(OptionError)\n", " 1 0.000 0.000 0.000 0.000 config.py:175(DictWrapper)\n", " 1 0.000 0.000 0.000 0.000 config.py:377(option_context)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:288(_CountryTimezoneDict)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:11(UnknownTimeZoneError)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:42(NonExistentTimeError)\n", " 1 0.000 0.000 0.000 0.000 lazy.py:16(LazyDict)\n", " 1 0.000 0.000 0.000 0.000 lazy.py:121(LazySet)\n", " 1 0.000 0.000 0.000 0.000 tzinfo.py:66(BaseTzInfo)\n", " 1 0.000 0.000 0.000 0.000 chainmap.py:7(DeepChainMap)\n", " 1 0.000 0.000 0.000 0.000 {method 'reshape' of 'numpy.ndarray' objects}\n", " 2 0.000 0.000 0.000 0.000 calendar.py:53()\n", " 1 0.000 0.000 0.000 0.000 interactiveshell.py:690(get_ipython)\n", " 1 0.000 0.000 0.000 0.000 {method 'toordinal' of 'datetime.date' objects}\n", " 1 0.000 0.000 0.000 0.000 {method 'items' of 'collections.OrderedDict' objects}\n", " 1 0.000 0.000 0.000 0.000 {method 'discard' of 'set' objects}\n", " 1 0.000 0.000 0.000 0.000 {method 'write' of '_io.StringIO' objects}\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:5(BufferFull)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:27(PackException)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:31(PackValueError)\n", " 1 0.000 0.000 0.000 0.000 _version.py:1()\n", " 1 0.000 0.000 0.000 0.000 stata.py:492(InvalidColumnName)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:346(JointConditionBinOp)\n", " 1 0.000 0.000 0.000 0.000 pytables.py:2770(LegacySeriesFixed)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5879(Cyrillic)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5900(CJK)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:5913(Devanagari)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:1500(_FifoCache)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:4375(_ForwardNoRecurse)\n", " 1 0.000 0.000 0.000 0.000 pyparsing.py:292(ParseFatalException)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:525(checkdep_usetex)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:250(IgnoredKeywordWarning)\n", " 1 0.000 0.000 0.000 0.000 series.py:405(name)\n", " 1 0.000 0.000 0.000 0.000 csv.py:65(excel_tab)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:47(LargeZipFile)\n", " 1 0.000 0.000 0.000 0.000 zipfile.py:714(_SharedFile)\n", " 1 0.000 0.000 0.000 0.000 format.py:1107(IntArrayFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1152(PeriodArrayFormatter)\n", " 1 0.000 0.000 0.000 0.000 format.py:1167(CategoricalArrayFormatter)\n", " 1 0.000 0.000 0.000 0.000 internals.py:203(internal_values)\n", " 1 0.000 0.000 0.000 0.000 internals.py:348(shape)\n", " 1 0.000 0.000 0.000 0.000 range.py:158(_validate_dtype)\n", " 1 0.000 0.000 0.000 0.000 accessor.py:113(CachedAccessor)\n", " 1 0.000 0.000 0.000 0.000 base.py:170(SpecificationError)\n", " 1 0.000 0.000 0.000 0.000 nanops.py:179(_get_fill_value)\n", " 1 0.000 0.000 0.000 0.000 dtypes.py:79(CategoricalDtypeType)\n", " 1 0.000 0.000 0.000 0.000 common.py:27(SettingWithCopyWarning)\n", " 1 0.000 0.000 0.000 0.000 common.py:154(_all_none)\n", " 1 0.000 0.000 0.000 0.000 __init__.py:346(_CountryNameDict)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:27(InvalidTimeError)\n", " 1 0.000 0.000 0.000 0.000 exceptions.py:31(AmbiguousTimeError)\n", " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n", " 1 0.000 0.000 0.000 0.000 sre_parse.py:101(checklookbehindgroup)\n", " 1 0.000 0.000 0.000 0.000 {method 'upper' of 'str' objects}\n", " 1 0.000 0.000 0.000 0.000 {built-in method _stat.S_ISREG}\n", " 1 0.000 0.000 0.000 0.000 {method 'getvalue' of '_io.StringIO' objects}\n", " " ] } ], "source": [ "%run -p ../examples/abc.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The profiling information includes the following information:\n", "\n", "* ncalls: number of calls to function\n", "* tottime: total time spent in the given function (excluding time in calls to sub-functions)\n", "* percall: time per call\n", "* cumtime: cumulative time spent in this and all subfunctions \n", "\n", "We can see that most of the time in this example is spent inside of core NumPy functions and methods.\n", "\n", "The %prun command does a similar job for single Python expressions (like function calls)." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 100006 function calls in 0.235 seconds\n", "\n", " Ordered by: internal time\n", "\n", " ncalls tottime percall cumtime percall filename:lineno(function)\n", " 1 0.122 0.122 0.235 0.235 :4(trapez)\n", " 100002 0.113 0.000 0.113 0.000 :1(f)\n", " 1 0.000 0.000 0.235 0.235 {built-in method builtins.exec}\n", " 1 0.000 0.000 0.235 0.235 :1()\n", " 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}\n", " " ] } ], "source": [ "%prun trapez(2, 6, 100000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For even more fine-grained profiling information, we can use a line profiler to see how long it takes each line of a function to run." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/bin/sh: 1: pprofile: not found\n" ] } ], "source": [ "!pprofile ../examples/bisection.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This output makes it clear that the biggest cost is in the repeated calling of the function $f$ for which the root is being found. If we could improve the speed of this function, it would be the easiest single way of improving the performance of the code.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Snakeviz\n", "\n", "SnakeViz is a browser based graphical viewer for the output of Python’s cProfile module. Though it is ostensibly a command-line tool, \n", "SnakeViz includes IPython line and cell magics for going straight from code to a visualization. " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "%load_ext snakeviz" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's load the ABC example from the source, so that we can run it in the notebook." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "mu 4.031276\n", "sigma 2.042091\n", "dtype: float64\n" ] } ], "source": [ "# %load ../examples/abc.py\n", "import numpy as np\n", "import pandas as pd\n", "\n", "def abc(y, N, epsilon):\n", "\n", " trace = []\n", "\n", " while len(trace) < N:\n", "\n", " # Simulate from priors\n", " mu = np.random.normal(0, 10)\n", " sigma = np.random.uniform(0, 20)\n", "\n", " x = np.random.normal(mu, sigma, 50)\n", "\n", " #if (np.linalg.norm(y - x) < epsilon):\n", " if ((abs(x.mean() - y.mean()) < epsilon[0]) &\n", " (abs(x.std() - y.std()) < epsilon[1])):\n", " trace.append([mu, sigma])\n", "\n", " return pd.DataFrame(trace, columns=['mu', 'sigma'])\n", "\n", "if __name__ == '__main__':\n", " y = np.random.normal(4, 2, 50)\n", " N = 20\n", " epsilon = [0.2, 0.8]\n", " print(abc(y, N, epsilon).mean(0))" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n", "*** Profile stats marshalled to file '/tmp/tmp41fk4j6q'. \n" ] } ], "source": [ "%snakeviz abc(y=np.random.normal(4, 2, 50), N=20, epsilon=[0.2, 0.8]).mean(0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The default SnakeViz visualization displays profiles as a *sunburst* that represent function calls as a series of nested arcs, expanding outward. Thus, the root function is represented as a circle at the center, with the functions it calls displayed as arcs wrapping around it, and so forth. The amount of time spent inside a function is represented by the angular width of the arc. Hence, an arc that wraps most of the way around the circle represents a function that is taking up most of the time of its calling function, while a narrow arc represents a function that is using relatively little time.\n", "\n", "Functions don’t just spend time calling other functions, they also have their own internal time. SnakeViz shows this by putting a special child on each node that represents internal time. Only functions that call other functions will have this, functions with no calls are entirely internal time.\n", "\n", "SnakeViz is useful for a global overview of the runtime of your project because it might uncover functions that do not seem (at first) to be worth optimizing because of their short runtime, but are in fact called from other functions so that their total runtime is significant.\n", "\n", "![snakeviz](images/snakeviz.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise: Profiling EM\n", "\n", "Use one or more of the above methods to profile your expectation maximization code, and locate any bottlenecks." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "# Write your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Speeding up Python by being Pythonic\n", "\n", "When you have decided that your code is unacceptably slow, and have gone through the process of profiling to see if and where your program is experiencing a bottleneck, it can be easy to jump ahead and try speeding it up using external tools. There are several packages that will certainly improve Python's performance (and we will introduce some of them later), but the first place to look for better performance is in **refactoring** your implementation of whichever algorithm you happen to be using. \n", "\n", "Effective Python programming involves applying particular Python **idioms** effectively; these are idiosyncratic expressions that may only exist in Python (if you are coming from another languate), but when used appropriately they can make your code more readable, faster, or both. You have seen some of these already -- for example, the **list comprehension** as a means for succinctly implementing a for loop.\n", "\n", "### Comprehensions" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "def do_math(x):\n", " return 3 + x**3" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "963 ms ± 246 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%%timeit\n", "squares = np.empty(1000000)\n", "for i in range(1000000):\n", " squares[i] = do_math(i)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "820 ms ± 176 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit squares = [do_math(i) for i in range(1000000)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, not only is the list comprehension easier to write and read, it is also slightly faster.\n", "\n", "### Generators\n", "\n", "When you are dealing with a large number of elements that you do not need all at once, you can also consider another Python expression we have already seen, a **generator**. For example, if we enclose the comprehension in parentheses instead of square brackets, we get a **generator expression** object:" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ " at 0x7c1b342d2a98>" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(i**2 for i in range(int(1e20)))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, rather than storing 100,000,000,000,000,000,000,000 elements in memory, we can produce values as needed:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "squares = (i**2 for i in range(int(1e10)))\n", "next(squares)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "next(squares)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Built-in functions\n", "\n", "Before you go about coding your own functions, make sure that it isn't already provided as a **built-in function**. These are typically highly optimized, and written in C! [Here is a list of built-in functions](https://docs.python.org/3/library/functions.html).\n", "\n", "### String concatenation\n", "\n", "Just as you should avoid growing lists or arrays by concatenation or appending, iterating over strings and concatenating them manually is very inefficient. For example, let's say we want to concatente a list of strings into a single string:" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "words = [\"Six\",\n", "\"days\",\n", "\"in\",\n", "\"to\",\n", "\"what\",\n", "\"should\",\n", "\"be\",\n", "\"a\",\n", "\"greatest\",\n", "\"two\",\n", "\"months\",\n", "\"of\",\n", "\"my\",\n", "\"life\",\n", "\"and\",\n", "\"it’s\",\n", "\"turned\",\n", "\"in\",\n", "\"to\",\n", "\"a\",\n", "\"nightmare\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "One might be tempted to code the following:" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.11 µs ± 625 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%%timeit\n", "sentence = \"\"\n", "for word in words:\n", " sentence += \" \" + word" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "' Six days in to what should be a greatest two months of my life and it’s turned in to a nightmare'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sentence = \"\"\n", "for word in words:\n", " sentence += \" \" + word\n", "sentence" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, this is inefficient; since strings is immutable in Python, every + operation involves creating a new string and copying the old content. Instead, we can use the string method join, which is not only faster, but more flexible. Here, we would like to separate the words by spaces, which is easily done:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Six days in to what should be a greatest two months of my life and it’s turned in to a nightmare'" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "' '.join(words)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "921 ns ± 178 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" ] } ], "source": [ "%timeit ' '.join(words)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Avoid loops\n", "\n", "As we have seen, for loops in Python are slow. Wherever possible, avoid looping by using alternative strategies or vectorized operations. For example, say we wanted to return the common elements between two arrays. We might naively loop over both lists, comparing them elementwise to return their intersection:" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "np.random.seed(1)\n", "list1 = np.random.choice(np.arange(20), replace=False, size=10)\n", "list2 = np.random.choice(np.arange(20), replace=False, size=10)\n", "\n", "def common_elements(a, b):\n", " for i in a:\n", " for j in b:\n", " if i==j:\n", " yield i" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "32.4 µs ± 1.93 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" ] } ], "source": [ "%timeit list(common_elements(list1, list2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "However, this involves two Python for loops and a conditional statement. Instead, we can use set operations on the built-in set type provided by Python:" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.03 µs ± 1.32 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit set(list1) & set(list2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Use NumPy\n", "\n", "Often, considerable performance gains can be achieved by replacing Python data structures and functions with corresponding NumPy versions. It provides a high-performance multidimensional array object, and tools for working with these arrays. \n", "\n", "This example, borrowed from NumPy creator Travis Oliphant, solves Laplace's equation over a 2-d rectangular grid using a simple iterative method. The code finds a two-dimensional function, u, where ∇2 u = 0, given some fixed boundary conditions." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "dx = 0.1\n", "dy = 0.1\n", "dx2 = dx*dx\n", "dy2 = dy*dy\n", "\n", "def py_update(u):\n", " nx, ny = u.shape\n", " for i in range(1,nx-1):\n", " for j in range(1, ny-1):\n", " u[i,j] = ((u[i+1, j] + u[i-1, j]) * dy2 +\n", " (u[i, j+1] + u[i, j-1]) * dx2) / (2*(dx2+dy2))" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "def calc(N, Niter=100, func=py_update, args=()):\n", " u = np.zeros([N, N])\n", " u[0] = 1\n", " for i in range(Niter):\n", " func(u,*args)\n", " return u" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This code takes a very long time to run in order to converge to the correct solution. For a 100x100 grid, visually-indistinguishable convergence occurs after about 8000 iterations." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "20.9 ms ± 1.86 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" ] } ], "source": [ "%timeit calc(10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using NumPy, we can speed this code up significantly by using slicing and vectorized (automatic looping) calculations that replace the explicit loops in the Python-only solution. " ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "def num_update(u):\n", " u[1:-1,1:-1] = ((u[2:,1:-1]+u[:-2,1:-1])*dy2 + \n", " (u[1:-1,2:] + u[1:-1,:-2])*dx2) / (2*(dx2+dy2))" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.49 ms ± 89.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%timeit calc(10, func=num_update)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Such speed-ups are not uncommon when using NumPy to replace Python loops where the inner loop is doing simple math on basic data-types." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Memoize repeated function calls\n", "\n", "The fibonacci sequence benchmark cited in the table above on the Julia website uses the following implementation:" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [], "source": [ "def fib(n):\n", " if n < 2:\n", " return n\n", " return fib(n-1) + fib(n-2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a simple recursive function, and as such, grows quickly with the size of n passed to it." ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fib(5)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6765" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fib(20)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "102334155" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fib(40)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice how long the last call took. We can use the timeit magic to quantify the speed of this naive implementation." ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.18 ms ± 412 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%timeit fib(20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first thing you should notice is that, being a recursive function, the same values will tend to be calculated over and over again. What if, instead of re-calculating values that have been calculated before, we instead store calculations so that they can be retrieved when needed the next time. Almost always, looking up a value stored in an array or hash table is going to be faster than performing an expensive calculation. \n", "\n", "This optimization technique is called **memoization**.\n", "\n", "Here is a memoization function that is implemented as a **decorator**." ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "def memoize(f):\n", " memo = {}\n", " def func(arg):\n", " if arg not in memo: \n", " memo[arg] = f(arg)\n", " return memo[arg]\n", " return func" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can simply annotate our function with this decorator, and run it the same way as the original function." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "@memoize\n", "def fib_memo(n):\n", " if n < 2:\n", " return n\n", " return fib_memo(n - 1) + fib_memo(n - 2)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6765" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fib_memo(20)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "171 ns ± 8.78 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n" ] } ], "source": [ "%timeit fib_memo(20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is a massive speedup.\n", "\n", "The built-in functools library includes an efficient memoization function called lru_cache (LRU: Least Recently Used)." ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "from functools import lru_cache\n", "\n", "@lru_cache(maxsize=None)\n", "def fib_memo(n):\n", " if n < 2:\n", " return n\n", " return fib_memo(n - 1) + fib_memo(n - 2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The only constraint on lru_cache is that the arguments to the function must be hashable." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fast array expression evaluation with numexpr\n", "\n", "[numexpr](http://code.google.com/p/numexpr/) allows array expressions to be evaluated far faster that what can be achieved in Python using Numpy arrays. numexpr parses a string expression and optimizes and compiles the code on the fly, using a virtual machine that includes a [Just-in-time (JIT) compiler](http://en.wikipedia.org/wiki/Just-in-time_compilation). In addition, numexpr offers direct support for parallel multi-threaded computations, since Python's global interpreter lock is bypassed.\n", "\n", "> Python's global interpreter lock (GIL) ensures that only one thread runs in the interpreter at once. This simplifies many of the low-level activities, such as memory management, and allows for co-operative multi-tasking. But, since the currently-running thread holds onto the interpreter, it makes multi-core parallelization difficult.\n", "\n", "Part the reason Python can be slow for array calculations is that it creates temporary arrays to store intermediate results from array element calculations, which wastes memory and cache. numexpr handles such calculations in manageable chunks, which accellerates computation.\n", "\n", "The speedup over NumPy by using numexpr can be as high as 20x, but is typically in the range of 2-4x.\n", "\n", "### Example: Computing a polynomial" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.0000000e+00, -4.5000045e-06, -9.0000090e-06, ...,\n", " -2.5000030e+00, -2.5000015e+00, -2.5000000e+00])" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "x = np.linspace(-1, 1, 1000000)\n", "\n", "0.25*x**3 + 0.75*x**2 - 1.5*x - 2" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "82.1 ms ± 2.94 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" ] } ], "source": [ "%timeit 0.25*x**3 + 0.75*x**2 - 1.5*x - 2" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numexpr as ne\n", "\n", "ne.set_num_threads(1)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.0000000e+00, -4.5000045e-06, -9.0000090e-06, ...,\n", " -2.5000030e+00, -2.5000015e+00, -2.5000000e+00])" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ne.evaluate('0.25*x**3 + 0.75*x**2 - 1.5*x - 2')" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "7.34 ms ± 644 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%timeit ne.evaluate('0.25*x**3 + 0.75*x**2 - 1.5*x - 2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "numexpr actually expands the polynomial terms so that it does not need to use a transcendental function.\n", "\n", "We can achieve further gains in performance by multithreading the calculations:" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ne.set_num_threads(4)" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5.05 ms ± 1.89 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%timeit ne.evaluate('0.25*x**3 + 0.75*x**2 - 1.5*x - 2')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since the performance of processors has outpaced that of memory in the past several decades, the CPU spends a lot of time waiting for memory to give it computations; this is the ***processor-memory performance gap***.\n", "\n", "![performance gap](http://www.techdesignforums.com/practice/files/2013/02/tdf-snps-ARMcc-feb13-fig1lg.jpg)\n", "(graph courtesy http://www.techdesignforums.com)\n", "\n", "CPU caches are often used to make up for this difference. CPU caches are more effective when the data are optimally located in memory to take advantage of cache performance. numexpr does this by moving contiguous blocks of data from memory to the CPU cache, reusing them as much as possible within the cache to more quickly give the CPU access to the data.\n", "\n", "### Limitations\n", "\n", "numexpr only implements element-wise operations. So, a * b becomes:\n", "\n", " for i in range(N):\n", " c[i] = a[i] * b[i]\n", "\n", "Similarly, it cannot index other parts of arrays in the same expression:\n", "\n", " for i in range(N):\n", " c[i] = a[i-1] + a[i] * b[i]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Cython\n", "\n", "Python developers typically solve performance constraints by building Python extensions by wrapping code written in other languages (for example, SciPy contains more lines of C/C++/Fortran than Python). However, programming with the Python/C API is not straightforward for most users.\n", "\n", "Cython is a language that allows Python programmers to write fast code without having to write C/C++/Fortran directly. It looks much like Python code, but with type declarations. Cython code is translated it to C (or C++ or others), which is then compiled to create a Python extension that we can import and use. \n", "\n", "Using Cython, we can achieve speedups of several orders of magnitude, often *faster than hand-coded C code*. In addtion, Cython is compatible with core scientific programming tools like NumPy and IPython.\n", "\n", "Cython has built-in support for multicore processing.\n", "\n", "Cython is used to varying degrees by other packages in the Python scientific stack, such as pandas, sympy, scikit-learn and SciPy.\n", "\n", "### Example: Numerical integration\n", "\n", "Recall from above the function trapez for performing numerical integration using the trapezoidal rule. As a benchmark, let's time the execution of the pure-Python version of the trapezoidal rule:" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "440 µs ± 16.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%timeit trapez(1, 5, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Perhaps the easiest way to use Cython, is via the IPython cythonmagic, which allows us to run Cython interactively:" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "%load_ext cython" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "%%cython\n", "\n", "def f(x):\n", " return 2*x*x + 3*x + 1\n", "\n", "def trapez2(a, b, n):\n", " h = (b-a)/float(n) \n", " sumy = 0\n", " x=a\n", " for i in range(n):\n", " x += h\n", " sumy += f(x)\n", " sumy += 0.5*(f(a) + f(b))\n", " return sumy*h" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The Cython magic is doing a lot of work for you: it compiles the code into an extension module, and loads it into the notebook. This allows us to ignore all of the compilation details of building Cython extensions. \n", "\n", "If we run trapez2, we can see a reasonable speedup simply by compiling it, unchanged, using Cython." ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "180 µs ± 10 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%timeit trapez2(1, 5, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Under the hood, several things are happening in order to deliver this improved performance. The Cython source code is translated into C source code by cython. Then, this C source is compiled, using the appropriate compiler, flags and associated library files (if any), into a Python extension. This extension is then loaded by IPython into the current session.\n", "\n", "![cython flow](images/cython.png)\n", "\n", "C extensions can also be compiled manually, using a setup file. Here is an example for an extension called dist within a package called probability:\n", "\n", "python\n", "from distutils.core import setup\n", "from distutils.extension import Extension\n", "from Cython.Distutils import build_ext\n", "\n", "import numpy as np\n", "\n", "setup(\n", " cmdclass = {'build_ext': build_ext},\n", " ext_modules = [Extension(\"dist\", [\"probability/src/dist.pyx\"], include_dirs=[np.get_include()])]\n", ")\n", "\n", " \n", "It mainly uses machinery from a core Python package distutils that manages the build process." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a closer look at where Cython is improving our unchanged Python code, we can add an --annotate flag to the %%cython magic declaration:" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " Cython: _cython_magic_7c2f66cc05b706df58359bfb0c2282d5.pyx\n", " \n", "\n", "\n", "

Generated by Cython 0.29.2

\n", "

\n", " Yellow lines hint at Python interaction.
\n", " Click on a line that starts with a \"+\" to see the C code that Cython generated for it.\n", "

\n", "
 01:
\n", "
+02: def f(x):
\n", "
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_1f(PyObject *__pyx_self, PyObject *__pyx_v_x); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_1f = {\"f\", (PyCFunction)__pyx_pw_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_1f, METH_O, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_1f(PyObject *__pyx_self, PyObject *__pyx_v_x) {\n",
"  PyObject *__pyx_r = 0;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"f (wrapper)\", 0);\n",
"  __pyx_r = __pyx_pf_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_f(__pyx_self, ((PyObject *)__pyx_v_x));\n",
"\n",
"  /* function exit code */\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_f(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_x) {\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"f\", 0);\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_1);\n",
"  __Pyx_XDECREF(__pyx_t_2);\n",
"  __Pyx_XDECREF(__pyx_t_3);\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"/* … */\n",
"  __pyx_tuple_ = PyTuple_Pack(1, __pyx_n_s_x); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 2, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_tuple_);\n",
"  __Pyx_GIVEREF(__pyx_tuple_);\n",
"/* … */\n",
"  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_1f, NULL, __pyx_n_s_cython_magic_7c2f66cc05b706df58); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  if (PyDict_SetItem(__pyx_d, __pyx_n_s_f, __pyx_t_1) < 0) __PYX_ERR(0, 2, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __pyx_codeobj__2 = (PyObject*)__Pyx_PyCode_New(1, 0, 1, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple_, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_fonnesbeck_cache_ipython_c, __pyx_n_s_f, 2, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__2)) __PYX_ERR(0, 2, __pyx_L1_error)\n",
"
+03:     return 2*x*x + 3*x + 1
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_1 = PyNumber_Multiply(__pyx_int_2, __pyx_v_x); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __pyx_t_2 = PyNumber_Multiply(__pyx_t_1, __pyx_v_x); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_2);\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __pyx_t_1 = PyNumber_Multiply(__pyx_int_3, __pyx_v_x); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __pyx_t_3 = PyNumber_Add(__pyx_t_2, __pyx_t_1); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_3);\n",
"  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __pyx_t_1 = __Pyx_PyInt_AddObjC(__pyx_t_3, __pyx_int_1, 1, 0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
"  __pyx_r = __pyx_t_1;\n",
"  __pyx_t_1 = 0;\n",
"  goto __pyx_L0;\n",
"
 04:
\n", "
+05: def trapez2(a, b, n):
\n", "
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_3trapez2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_3trapez2 = {\"trapez2\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_3trapez2, METH_VARARGS|METH_KEYWORDS, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_3trapez2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
"  PyObject *__pyx_v_a = 0;\n",
"  PyObject *__pyx_v_b = 0;\n",
"  PyObject *__pyx_v_n = 0;\n",
"  PyObject *__pyx_r = 0;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"trapez2 (wrapper)\", 0);\n",
"  {\n",
"    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_a,&__pyx_n_s_b,&__pyx_n_s_n,0};\n",
"    PyObject* values[3] = {0,0,0};\n",
"    if (unlikely(__pyx_kwds)) {\n",
"      Py_ssize_t kw_args;\n",
"      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n",
"      switch (pos_args) {\n",
"        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  0: break;\n",
"        default: goto __pyx_L5_argtuple_error;\n",
"      }\n",
"      kw_args = PyDict_Size(__pyx_kwds);\n",
"      switch (pos_args) {\n",
"        case  0:\n",
"        if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_a)) != 0)) kw_args--;\n",
"        else goto __pyx_L5_argtuple_error;\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1:\n",
"        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_b)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"trapez2\", 1, 3, 3, 1); __PYX_ERR(0, 5, __pyx_L3_error)\n",
"        }\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  2:\n",
"        if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_n)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"trapez2\", 1, 3, 3, 2); __PYX_ERR(0, 5, __pyx_L3_error)\n",
"        }\n",
"      }\n",
"      if (unlikely(kw_args > 0)) {\n",
"        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"trapez2\") < 0)) __PYX_ERR(0, 5, __pyx_L3_error)\n",
"      }\n",
"    } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n",
"      goto __pyx_L5_argtuple_error;\n",
"    } else {\n",
"      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
"    }\n",
"    __pyx_v_a = values[0];\n",
"    __pyx_v_b = values[1];\n",
"    __pyx_v_n = values[2];\n",
"  }\n",
"  goto __pyx_L4_argument_unpacking_done;\n",
"  __pyx_L5_argtuple_error:;\n",
"  __Pyx_RaiseArgtupleInvalid(\"trapez2\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 5, __pyx_L3_error)\n",
"  __pyx_L3_error:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return NULL;\n",
"  __pyx_L4_argument_unpacking_done:;\n",
"  __pyx_r = __pyx_pf_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_2trapez2(__pyx_self, __pyx_v_a, __pyx_v_b, __pyx_v_n);\n",
"\n",
"  /* function exit code */\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_2trapez2(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_a, PyObject *__pyx_v_b, PyObject *__pyx_v_n) {\n",
"  PyObject *__pyx_v_h = NULL;\n",
"  PyObject *__pyx_v_sumy = NULL;\n",
"  PyObject *__pyx_v_x = NULL;\n",
"  CYTHON_UNUSED PyObject *__pyx_v_i = NULL;\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"trapez2\", 0);\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_1);\n",
"  __Pyx_XDECREF(__pyx_t_2);\n",
"  __Pyx_XDECREF(__pyx_t_3);\n",
"  __Pyx_XDECREF(__pyx_t_6);\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_XDECREF(__pyx_v_h);\n",
"  __Pyx_XDECREF(__pyx_v_sumy);\n",
"  __Pyx_XDECREF(__pyx_v_x);\n",
"  __Pyx_XDECREF(__pyx_v_i);\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"/* … */\n",
"  __pyx_tuple__3 = PyTuple_Pack(7, __pyx_n_s_a, __pyx_n_s_b, __pyx_n_s_n, __pyx_n_s_h, __pyx_n_s_sumy, __pyx_n_s_x, __pyx_n_s_i); if (unlikely(!__pyx_tuple__3)) __PYX_ERR(0, 5, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_tuple__3);\n",
"  __Pyx_GIVEREF(__pyx_tuple__3);\n",
"/* … */\n",
"  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_7c2f66cc05b706df58359bfb0c2282d5_3trapez2, NULL, __pyx_n_s_cython_magic_7c2f66cc05b706df58); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 5, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  if (PyDict_SetItem(__pyx_d, __pyx_n_s_trapez2, __pyx_t_1) < 0) __PYX_ERR(0, 5, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"
+06:     h = (b-a)/float(n)
\n", "
  __pyx_t_1 = PyNumber_Subtract(__pyx_v_b, __pyx_v_a); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 6, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __pyx_t_2 = __Pyx_PyNumber_Float(__pyx_v_n); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 6, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_2);\n",
"  __pyx_t_3 = __Pyx_PyNumber_Divide(__pyx_t_1, __pyx_t_2); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 6, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_3);\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n",
"  __pyx_v_h = __pyx_t_3;\n",
"  __pyx_t_3 = 0;\n",
"
+07:     sumy = 0
\n", "
  __Pyx_INCREF(__pyx_int_0);\n",
"  __pyx_v_sumy = __pyx_int_0;\n",
"
+08:     x=a
\n", "
  __Pyx_INCREF(__pyx_v_a);\n",
"  __pyx_v_x = __pyx_v_a;\n",
"
+09:     for i in range(n):
\n", "
  __pyx_t_3 = __Pyx_PyObject_CallOneArg(__pyx_builtin_range, __pyx_v_n); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_3);\n",
"  if (likely(PyList_CheckExact(__pyx_t_3)) || PyTuple_CheckExact(__pyx_t_3)) {\n",
"    __pyx_t_2 = __pyx_t_3; __Pyx_INCREF(__pyx_t_2); __pyx_t_4 = 0;\n",
"    __pyx_t_5 = NULL;\n",
"  } else {\n",
"    __pyx_t_4 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_2);\n",
"    __pyx_t_5 = Py_TYPE(__pyx_t_2)->tp_iternext; if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"  }\n",
"  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
"  for (;;) {\n",
"    if (likely(!__pyx_t_5)) {\n",
"      if (likely(PyList_CheckExact(__pyx_t_2))) {\n",
"        if (__pyx_t_4 >= PyList_GET_SIZE(__pyx_t_2)) break;\n",
"        #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
"        __pyx_t_3 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"        #else\n",
"        __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"        __Pyx_GOTREF(__pyx_t_3);\n",
"        #endif\n",
"      } else {\n",
"        if (__pyx_t_4 >= PyTuple_GET_SIZE(__pyx_t_2)) break;\n",
"        #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS\n",
"        __pyx_t_3 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_4); __Pyx_INCREF(__pyx_t_3); __pyx_t_4++; if (unlikely(0 < 0)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"        #else\n",
"        __pyx_t_3 = PySequence_ITEM(__pyx_t_2, __pyx_t_4); __pyx_t_4++; if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"        __Pyx_GOTREF(__pyx_t_3);\n",
"        #endif\n",
"      }\n",
"    } else {\n",
"      __pyx_t_3 = __pyx_t_5(__pyx_t_2);\n",
"      if (unlikely(!__pyx_t_3)) {\n",
"        PyObject* exc_type = PyErr_Occurred();\n",
"        if (exc_type) {\n",
"          if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear();\n",
"          else __PYX_ERR(0, 9, __pyx_L1_error)\n",
"        }\n",
"        break;\n",
"      }\n",
"      __Pyx_GOTREF(__pyx_t_3);\n",
"    }\n",
"    __Pyx_XDECREF_SET(__pyx_v_i, __pyx_t_3);\n",
"    __pyx_t_3 = 0;\n",
"/* … */\n",
"  }\n",
"  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n",
"
+10:         x += h
\n", "
    __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_v_x, __pyx_v_h); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_3);\n",
"    __Pyx_DECREF_SET(__pyx_v_x, __pyx_t_3);\n",
"    __pyx_t_3 = 0;\n",
"
+11:         sumy += f(x)
\n", "
    __Pyx_GetModuleGlobalName(__pyx_t_1, __pyx_n_s_f); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 11, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_1);\n",
"    __pyx_t_6 = NULL;\n",
"    if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_1))) {\n",
"      __pyx_t_6 = PyMethod_GET_SELF(__pyx_t_1);\n",
"      if (likely(__pyx_t_6)) {\n",
"        PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_1);\n",
"        __Pyx_INCREF(__pyx_t_6);\n",
"        __Pyx_INCREF(function);\n",
"        __Pyx_DECREF_SET(__pyx_t_1, function);\n",
"      }\n",
"    }\n",
"    __pyx_t_3 = (__pyx_t_6) ? __Pyx_PyObject_Call2Args(__pyx_t_1, __pyx_t_6, __pyx_v_x) : __Pyx_PyObject_CallOneArg(__pyx_t_1, __pyx_v_x);\n",
"    __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
"    if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 11, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_3);\n",
"    __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"    __pyx_t_1 = PyNumber_InPlaceAdd(__pyx_v_sumy, __pyx_t_3); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 11, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_1);\n",
"    __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
"    __Pyx_DECREF_SET(__pyx_v_sumy, __pyx_t_1);\n",
"    __pyx_t_1 = 0;\n",
"
+12:     sumy += 0.5*(f(a) + f(b))
\n", "
  __Pyx_GetModuleGlobalName(__pyx_t_1, __pyx_n_s_f); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 12, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __pyx_t_3 = NULL;\n",
"  if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_1))) {\n",
"    __pyx_t_3 = PyMethod_GET_SELF(__pyx_t_1);\n",
"    if (likely(__pyx_t_3)) {\n",
"      PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_1);\n",
"      __Pyx_INCREF(__pyx_t_3);\n",
"      __Pyx_INCREF(function);\n",
"      __Pyx_DECREF_SET(__pyx_t_1, function);\n",
"    }\n",
"  }\n",
"  __pyx_t_2 = (__pyx_t_3) ? __Pyx_PyObject_Call2Args(__pyx_t_1, __pyx_t_3, __pyx_v_a) : __Pyx_PyObject_CallOneArg(__pyx_t_1, __pyx_v_a);\n",
"  __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
"  if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 12, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_2);\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __Pyx_GetModuleGlobalName(__pyx_t_3, __pyx_n_s_f); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 12, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_3);\n",
"  __pyx_t_6 = NULL;\n",
"  if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_3))) {\n",
"    __pyx_t_6 = PyMethod_GET_SELF(__pyx_t_3);\n",
"    if (likely(__pyx_t_6)) {\n",
"      PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);\n",
"      __Pyx_INCREF(__pyx_t_6);\n",
"      __Pyx_INCREF(function);\n",
"      __Pyx_DECREF_SET(__pyx_t_3, function);\n",
"    }\n",
"  }\n",
"  __pyx_t_1 = (__pyx_t_6) ? __Pyx_PyObject_Call2Args(__pyx_t_3, __pyx_t_6, __pyx_v_b) : __Pyx_PyObject_CallOneArg(__pyx_t_3, __pyx_v_b);\n",
"  __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
"  if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 12, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
"  __pyx_t_3 = PyNumber_Add(__pyx_t_2, __pyx_t_1); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 12, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_3);\n",
"  __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0;\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __pyx_t_1 = PyNumber_Multiply(__pyx_float_0_5, __pyx_t_3); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 12, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;\n",
"  __pyx_t_3 = PyNumber_InPlaceAdd(__pyx_v_sumy, __pyx_t_1); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 12, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_3);\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __Pyx_DECREF_SET(__pyx_v_sumy, __pyx_t_3);\n",
"  __pyx_t_3 = 0;\n",
"
+13:     return sumy*h
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_3 = PyNumber_Multiply(__pyx_v_sumy, __pyx_v_h); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 13, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_3);\n",
"  __pyx_r = __pyx_t_3;\n",
"  __pyx_t_3 = 0;\n",
"  goto __pyx_L0;\n",
"
" ], "text/plain": [ "" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%cython --annotate\n", "\n", "def f(x):\n", " return 2*x*x + 3*x + 1\n", " \n", "def trapez2(a, b, n):\n", " h = (b-a)/float(n) \n", " sumy = 0\n", " x=a\n", " for i in range(n):\n", " x += h\n", " sumy += f(x)\n", " sumy += 0.5*(f(a) + f(b))\n", " return sumy*h" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the above, the line color indicates the \"typedness\" of the extension, where yellower lines are closer to Python, and therefore require calls to the Python C API, while whiter lines indicate code that is closer to pure C, hence requiring few, if any, Python API calls.\n", "\n", "If you click on a line, it unravels to show you the C code that results from the call to cython.\n", "\n", "The goal in speeding up code with Cython is to turn as many lines to white as we can. The easiest way to do this is to add type declarations to the Python code:" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " Cython: _cython_magic_edb24a3ae69e1a29821d036c6f188980.pyx\n", " \n", "\n", "\n", "

Generated by Cython 0.29.2

\n", "

\n", " Yellow lines hint at Python interaction.
\n", " Click on a line that starts with a \"+\" to see the C code that Cython generated for it.\n", "

\n", "
 01:
\n", "
 02: # Add type to argument
\n", "
+03: def ff(double x):
\n", "
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_1ff(PyObject *__pyx_self, PyObject *__pyx_arg_x); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_1ff = {\"ff\", (PyCFunction)__pyx_pw_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_1ff, METH_O, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_1ff(PyObject *__pyx_self, PyObject *__pyx_arg_x) {\n",
"  double __pyx_v_x;\n",
"  PyObject *__pyx_r = 0;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"ff (wrapper)\", 0);\n",
"  assert(__pyx_arg_x); {\n",
"    __pyx_v_x = __pyx_PyFloat_AsDouble(__pyx_arg_x); if (unlikely((__pyx_v_x == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 3, __pyx_L3_error)\n",
"  }\n",
"  goto __pyx_L4_argument_unpacking_done;\n",
"  __pyx_L3_error:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return NULL;\n",
"  __pyx_L4_argument_unpacking_done:;\n",
"  __pyx_r = __pyx_pf_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_ff(__pyx_self, ((double)__pyx_v_x));\n",
"\n",
"  /* function exit code */\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_ff(CYTHON_UNUSED PyObject *__pyx_self, double __pyx_v_x) {\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"ff\", 0);\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_1);\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"/* … */\n",
"  __pyx_tuple_ = PyTuple_Pack(2, __pyx_n_s_x, __pyx_n_s_x); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_tuple_);\n",
"  __Pyx_GIVEREF(__pyx_tuple_);\n",
"/* … */\n",
"  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_1ff, NULL, __pyx_n_s_cython_magic_edb24a3ae69e1a2982); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  if (PyDict_SetItem(__pyx_d, __pyx_n_s_ff, __pyx_t_1) < 0) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"  __pyx_codeobj__2 = (PyObject*)__Pyx_PyCode_New(1, 0, 2, 0, CO_OPTIMIZED|CO_NEWLOCALS, __pyx_empty_bytes, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_tuple_, __pyx_empty_tuple, __pyx_empty_tuple, __pyx_kp_s_home_fonnesbeck_cache_ipython_c, __pyx_n_s_ff, 3, __pyx_empty_bytes); if (unlikely(!__pyx_codeobj__2)) __PYX_ERR(0, 3, __pyx_L1_error)\n",
"
+04:     return 2*x*x + 3*x + 1
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_1 = PyFloat_FromDouble(((((2.0 * __pyx_v_x) * __pyx_v_x) + (3.0 * __pyx_v_x)) + 1.0)); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __pyx_r = __pyx_t_1;\n",
"  __pyx_t_1 = 0;\n",
"  goto __pyx_L0;\n",
"
 05:
\n", "
 06: # Add types to arguments
\n", "
+07: def trapez3(double a, double b, int n):
\n", "
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_3trapez3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_3trapez3 = {\"trapez3\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_3trapez3, METH_VARARGS|METH_KEYWORDS, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_3trapez3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
"  double __pyx_v_a;\n",
"  double __pyx_v_b;\n",
"  int __pyx_v_n;\n",
"  PyObject *__pyx_r = 0;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"trapez3 (wrapper)\", 0);\n",
"  {\n",
"    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_a,&__pyx_n_s_b,&__pyx_n_s_n,0};\n",
"    PyObject* values[3] = {0,0,0};\n",
"    if (unlikely(__pyx_kwds)) {\n",
"      Py_ssize_t kw_args;\n",
"      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n",
"      switch (pos_args) {\n",
"        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  0: break;\n",
"        default: goto __pyx_L5_argtuple_error;\n",
"      }\n",
"      kw_args = PyDict_Size(__pyx_kwds);\n",
"      switch (pos_args) {\n",
"        case  0:\n",
"        if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_a)) != 0)) kw_args--;\n",
"        else goto __pyx_L5_argtuple_error;\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1:\n",
"        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_b)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"trapez3\", 1, 3, 3, 1); __PYX_ERR(0, 7, __pyx_L3_error)\n",
"        }\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  2:\n",
"        if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_n)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"trapez3\", 1, 3, 3, 2); __PYX_ERR(0, 7, __pyx_L3_error)\n",
"        }\n",
"      }\n",
"      if (unlikely(kw_args > 0)) {\n",
"        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"trapez3\") < 0)) __PYX_ERR(0, 7, __pyx_L3_error)\n",
"      }\n",
"    } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n",
"      goto __pyx_L5_argtuple_error;\n",
"    } else {\n",
"      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
"    }\n",
"    __pyx_v_a = __pyx_PyFloat_AsDouble(values[0]); if (unlikely((__pyx_v_a == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 7, __pyx_L3_error)\n",
"    __pyx_v_b = __pyx_PyFloat_AsDouble(values[1]); if (unlikely((__pyx_v_b == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 7, __pyx_L3_error)\n",
"    __pyx_v_n = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 7, __pyx_L3_error)\n",
"  }\n",
"  goto __pyx_L4_argument_unpacking_done;\n",
"  __pyx_L5_argtuple_error:;\n",
"  __Pyx_RaiseArgtupleInvalid(\"trapez3\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 7, __pyx_L3_error)\n",
"  __pyx_L3_error:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return NULL;\n",
"  __pyx_L4_argument_unpacking_done:;\n",
"  __pyx_r = __pyx_pf_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_2trapez3(__pyx_self, __pyx_v_a, __pyx_v_b, __pyx_v_n);\n",
"\n",
"  /* function exit code */\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_2trapez3(CYTHON_UNUSED PyObject *__pyx_self, double __pyx_v_a, double __pyx_v_b, int __pyx_v_n) {\n",
"  double __pyx_v_h;\n",
"  double __pyx_v_x;\n",
"  double __pyx_v_sumy;\n",
"  CYTHON_UNUSED int __pyx_v_i;\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"trapez3\", 0);\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_5);\n",
"  __Pyx_XDECREF(__pyx_t_6);\n",
"  __Pyx_XDECREF(__pyx_t_7);\n",
"  __Pyx_XDECREF(__pyx_t_8);\n",
"  __Pyx_XDECREF(__pyx_t_9);\n",
"  __Pyx_XDECREF(__pyx_t_10);\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"/* … */\n",
"  __pyx_tuple__3 = PyTuple_Pack(7, __pyx_n_s_a, __pyx_n_s_b, __pyx_n_s_n, __pyx_n_s_h, __pyx_n_s_x, __pyx_n_s_sumy, __pyx_n_s_i); if (unlikely(!__pyx_tuple__3)) __PYX_ERR(0, 7, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_tuple__3);\n",
"  __Pyx_GIVEREF(__pyx_tuple__3);\n",
"/* … */\n",
"  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_edb24a3ae69e1a29821d036c6f188980_3trapez3, NULL, __pyx_n_s_cython_magic_edb24a3ae69e1a2982); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 7, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  if (PyDict_SetItem(__pyx_d, __pyx_n_s_trapez3, __pyx_t_1) < 0) __PYX_ERR(0, 7, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"
 08:     # Declare types of variables
\n", "
 09:     cdef double h, x, sumy
\n", "
 10:     cdef int i
\n", "
+11:     h = (b-a)/float(n)
\n", "
  __pyx_t_1 = (__pyx_v_b - __pyx_v_a);\n",
"  if (unlikely(((double)__pyx_v_n) == 0)) {\n",
"    PyErr_SetString(PyExc_ZeroDivisionError, \"float division\");\n",
"    __PYX_ERR(0, 11, __pyx_L1_error)\n",
"  }\n",
"  __pyx_v_h = (__pyx_t_1 / ((double)__pyx_v_n));\n",
"
+12:     sumy = 0
\n", "
  __pyx_v_sumy = 0.0;\n",
"
+13:     x=a
\n", "
  __pyx_v_x = __pyx_v_a;\n",
"
+14:     for i in range(n):
\n", "
  __pyx_t_2 = __pyx_v_n;\n",
"  __pyx_t_3 = __pyx_t_2;\n",
"  for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {\n",
"    __pyx_v_i = __pyx_t_4;\n",
"
+15:         x += h
\n", "
    __pyx_v_x = (__pyx_v_x + __pyx_v_h);\n",
"
+16:         sumy += ff(x)
\n", "
    __pyx_t_5 = PyFloat_FromDouble(__pyx_v_sumy); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_5);\n",
"    __Pyx_GetModuleGlobalName(__pyx_t_7, __pyx_n_s_ff); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_7);\n",
"    __pyx_t_8 = PyFloat_FromDouble(__pyx_v_x); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_8);\n",
"    __pyx_t_9 = NULL;\n",
"    if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_7))) {\n",
"      __pyx_t_9 = PyMethod_GET_SELF(__pyx_t_7);\n",
"      if (likely(__pyx_t_9)) {\n",
"        PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_7);\n",
"        __Pyx_INCREF(__pyx_t_9);\n",
"        __Pyx_INCREF(function);\n",
"        __Pyx_DECREF_SET(__pyx_t_7, function);\n",
"      }\n",
"    }\n",
"    __pyx_t_6 = (__pyx_t_9) ? __Pyx_PyObject_Call2Args(__pyx_t_7, __pyx_t_9, __pyx_t_8) : __Pyx_PyObject_CallOneArg(__pyx_t_7, __pyx_t_8);\n",
"    __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;\n",
"    __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n",
"    if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_6);\n",
"    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;\n",
"    __pyx_t_7 = PyNumber_InPlaceAdd(__pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_7);\n",
"    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
"    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
"    __pyx_t_1 = __pyx_PyFloat_AsDouble(__pyx_t_7); if (unlikely((__pyx_t_1 == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;\n",
"    __pyx_v_sumy = __pyx_t_1;\n",
"  }\n",
"
+17:     sumy += 0.5*(ff(a) + ff(b))
\n", "
  __pyx_t_7 = PyFloat_FromDouble(__pyx_v_sumy); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_7);\n",
"  __Pyx_GetModuleGlobalName(__pyx_t_5, __pyx_n_s_ff); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_5);\n",
"  __pyx_t_8 = PyFloat_FromDouble(__pyx_v_a); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_8);\n",
"  __pyx_t_9 = NULL;\n",
"  if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_5))) {\n",
"    __pyx_t_9 = PyMethod_GET_SELF(__pyx_t_5);\n",
"    if (likely(__pyx_t_9)) {\n",
"      PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_5);\n",
"      __Pyx_INCREF(__pyx_t_9);\n",
"      __Pyx_INCREF(function);\n",
"      __Pyx_DECREF_SET(__pyx_t_5, function);\n",
"    }\n",
"  }\n",
"  __pyx_t_6 = (__pyx_t_9) ? __Pyx_PyObject_Call2Args(__pyx_t_5, __pyx_t_9, __pyx_t_8) : __Pyx_PyObject_CallOneArg(__pyx_t_5, __pyx_t_8);\n",
"  __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;\n",
"  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n",
"  if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_6);\n",
"  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
"  __Pyx_GetModuleGlobalName(__pyx_t_8, __pyx_n_s_ff); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_8);\n",
"  __pyx_t_9 = PyFloat_FromDouble(__pyx_v_b); if (unlikely(!__pyx_t_9)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_9);\n",
"  __pyx_t_10 = NULL;\n",
"  if (CYTHON_UNPACK_METHODS && unlikely(PyMethod_Check(__pyx_t_8))) {\n",
"    __pyx_t_10 = PyMethod_GET_SELF(__pyx_t_8);\n",
"    if (likely(__pyx_t_10)) {\n",
"      PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_8);\n",
"      __Pyx_INCREF(__pyx_t_10);\n",
"      __Pyx_INCREF(function);\n",
"      __Pyx_DECREF_SET(__pyx_t_8, function);\n",
"    }\n",
"  }\n",
"  __pyx_t_5 = (__pyx_t_10) ? __Pyx_PyObject_Call2Args(__pyx_t_8, __pyx_t_10, __pyx_t_9) : __Pyx_PyObject_CallOneArg(__pyx_t_8, __pyx_t_9);\n",
"  __Pyx_XDECREF(__pyx_t_10); __pyx_t_10 = 0;\n",
"  __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0;\n",
"  if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_5);\n",
"  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n",
"  __pyx_t_8 = PyNumber_Add(__pyx_t_6, __pyx_t_5); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_8);\n",
"  __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
"  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
"  __pyx_t_5 = PyNumber_Multiply(__pyx_float_0_5, __pyx_t_8); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_5);\n",
"  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n",
"  __pyx_t_8 = PyNumber_InPlaceAdd(__pyx_t_7, __pyx_t_5); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_8);\n",
"  __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;\n",
"  __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
"  __pyx_t_1 = __pyx_PyFloat_AsDouble(__pyx_t_8); if (unlikely((__pyx_t_1 == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 17, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_8); __pyx_t_8 = 0;\n",
"  __pyx_v_sumy = __pyx_t_1;\n",
"
+18:     return sumy*h
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_8 = PyFloat_FromDouble((__pyx_v_sumy * __pyx_v_h)); if (unlikely(!__pyx_t_8)) __PYX_ERR(0, 18, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_8);\n",
"  __pyx_r = __pyx_t_8;\n",
"  __pyx_t_8 = 0;\n",
"  goto __pyx_L0;\n",
"
" ], "text/plain": [ "" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%cython --annotate\n", "\n", "# Add type to argument\n", "def ff(double x):\n", " return 2*x*x + 3*x + 1\n", "\n", "# Add types to arguments\n", "def trapez3(double a, double b, int n):\n", " # Declare types of variables\n", " cdef double h, x, sumy\n", " cdef int i\n", " h = (b-a)/float(n) \n", " sumy = 0\n", " x=a\n", " for i in range(n):\n", " x += h\n", " sumy += ff(x)\n", " sumy += 0.5*(ff(a) + ff(b))\n", " return sumy*h" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "51.1 µs ± 1.13 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" ] } ], "source": [ "%timeit trapez3(1, 5, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This gives us a considerable speedup. The next thing we might try is to *inline* the polynomial function. By inlining, we mean that we ask the compiler to perform an inline expansion of said function; that is, it will insert a copy of the function itself wherever the function is called, instead of calling the function wherever it is defined.\n", "\n", "We do three things to the specification of ff:\n", "\n", "* change def to cdef\n", "* add a return type to the function\n", "* add an inline keyword" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " Cython: _cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a.pyx\n", " \n", "\n", "\n", "

Generated by Cython 0.29.2

\n", "

\n", " Yellow lines hint at Python interaction.
\n", " Click on a line that starts with a \"+\" to see the C code that Cython generated for it.\n", "

\n", "
 01:
\n", "
+02: cdef inline double ff(double x):
\n", "
static CYTHON_INLINE double __pyx_f_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_ff(double __pyx_v_x) {\n",
"  double __pyx_r;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"ff\", 0);\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L0:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"
+03:     return 2*x*x + 3*x + 1
\n", "
  __pyx_r = ((((2.0 * __pyx_v_x) * __pyx_v_x) + (3.0 * __pyx_v_x)) + 1.0);\n",
"  goto __pyx_L0;\n",
"
 04:
\n", "
+05: cpdef trapez4(double a, double b, int n):
\n", "
static PyObject *__pyx_pw_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_1trapez4(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyObject *__pyx_f_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_trapez4(double __pyx_v_a, double __pyx_v_b, int __pyx_v_n, CYTHON_UNUSED int __pyx_skip_dispatch) {\n",
"  double __pyx_v_h;\n",
"  double __pyx_v_x;\n",
"  double __pyx_v_sumy;\n",
"  CYTHON_UNUSED int __pyx_v_i;\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"trapez4\", 0);\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_5);\n",
"  __pyx_r = 0;\n",
"  __pyx_L0:;\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_1trapez4(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyObject *__pyx_pw_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_1trapez4(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
"  double __pyx_v_a;\n",
"  double __pyx_v_b;\n",
"  int __pyx_v_n;\n",
"  PyObject *__pyx_r = 0;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"trapez4 (wrapper)\", 0);\n",
"  {\n",
"    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_a,&__pyx_n_s_b,&__pyx_n_s_n,0};\n",
"    PyObject* values[3] = {0,0,0};\n",
"    if (unlikely(__pyx_kwds)) {\n",
"      Py_ssize_t kw_args;\n",
"      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n",
"      switch (pos_args) {\n",
"        case  3: values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  0: break;\n",
"        default: goto __pyx_L5_argtuple_error;\n",
"      }\n",
"      kw_args = PyDict_Size(__pyx_kwds);\n",
"      switch (pos_args) {\n",
"        case  0:\n",
"        if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_a)) != 0)) kw_args--;\n",
"        else goto __pyx_L5_argtuple_error;\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1:\n",
"        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_b)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"trapez4\", 1, 3, 3, 1); __PYX_ERR(0, 5, __pyx_L3_error)\n",
"        }\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  2:\n",
"        if (likely((values[2] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_n)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"trapez4\", 1, 3, 3, 2); __PYX_ERR(0, 5, __pyx_L3_error)\n",
"        }\n",
"      }\n",
"      if (unlikely(kw_args > 0)) {\n",
"        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"trapez4\") < 0)) __PYX_ERR(0, 5, __pyx_L3_error)\n",
"      }\n",
"    } else if (PyTuple_GET_SIZE(__pyx_args) != 3) {\n",
"      goto __pyx_L5_argtuple_error;\n",
"    } else {\n",
"      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"      values[2] = PyTuple_GET_ITEM(__pyx_args, 2);\n",
"    }\n",
"    __pyx_v_a = __pyx_PyFloat_AsDouble(values[0]); if (unlikely((__pyx_v_a == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 5, __pyx_L3_error)\n",
"    __pyx_v_b = __pyx_PyFloat_AsDouble(values[1]); if (unlikely((__pyx_v_b == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 5, __pyx_L3_error)\n",
"    __pyx_v_n = __Pyx_PyInt_As_int(values[2]); if (unlikely((__pyx_v_n == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 5, __pyx_L3_error)\n",
"  }\n",
"  goto __pyx_L4_argument_unpacking_done;\n",
"  __pyx_L5_argtuple_error:;\n",
"  __Pyx_RaiseArgtupleInvalid(\"trapez4\", 1, 3, 3, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 5, __pyx_L3_error)\n",
"  __pyx_L3_error:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return NULL;\n",
"  __pyx_L4_argument_unpacking_done:;\n",
"  __pyx_r = __pyx_pf_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_trapez4(__pyx_self, __pyx_v_a, __pyx_v_b, __pyx_v_n);\n",
"\n",
"  /* function exit code */\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_trapez4(CYTHON_UNUSED PyObject *__pyx_self, double __pyx_v_a, double __pyx_v_b, int __pyx_v_n) {\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"trapez4\", 0);\n",
"  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_1 = __pyx_f_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_trapez4(__pyx_v_a, __pyx_v_b, __pyx_v_n, 0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 5, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  __pyx_r = __pyx_t_1;\n",
"  __pyx_t_1 = 0;\n",
"  goto __pyx_L0;\n",
"\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_1);\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"
 06:     cdef double h, x, sumy
\n", "
 07:     cdef int i
\n", "
+08:     h = (b-a)/n
\n", "
  __pyx_t_1 = (__pyx_v_b - __pyx_v_a);\n",
"  if (unlikely(__pyx_v_n == 0)) {\n",
"    PyErr_SetString(PyExc_ZeroDivisionError, \"float division\");\n",
"    __PYX_ERR(0, 8, __pyx_L1_error)\n",
"  }\n",
"  __pyx_v_h = (__pyx_t_1 / ((double)__pyx_v_n));\n",
"
+09:     sumy = 0
\n", "
  __pyx_v_sumy = 0.0;\n",
"
+10:     x=a
\n", "
  __pyx_v_x = __pyx_v_a;\n",
"
+11:     for i in range(n):
\n", "
  __pyx_t_2 = __pyx_v_n;\n",
"  __pyx_t_3 = __pyx_t_2;\n",
"  for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) {\n",
"    __pyx_v_i = __pyx_t_4;\n",
"
+12:         x += h
\n", "
    __pyx_v_x = (__pyx_v_x + __pyx_v_h);\n",
"
+13:         sumy += ff(x)
\n", "
    __pyx_v_sumy = (__pyx_v_sumy + __pyx_f_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_ff(__pyx_v_x));\n",
"  }\n",
"
+14:     sumy += 0.5*(ff(a) + ff(b))
\n", "
  __pyx_v_sumy = (__pyx_v_sumy + (0.5 * (__pyx_f_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_ff(__pyx_v_a) + __pyx_f_46_cython_magic_bc2a61d8ea0fe6a9b418b8ea7c73bd3a_ff(__pyx_v_b))));\n",
"
+15:     return sumy*h
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_5 = PyFloat_FromDouble((__pyx_v_sumy * __pyx_v_h)); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 15, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_5);\n",
"  __pyx_r = __pyx_t_5;\n",
"  __pyx_t_5 = 0;\n",
"  goto __pyx_L0;\n",
"
" ], "text/plain": [ "" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%cython --annotate\n", "\n", "cdef inline double ff(double x):\n", " return 2*x*x + 3*x + 1\n", "\n", "cpdef trapez4(double a, double b, int n):\n", " cdef double h, x, sumy\n", " cdef int i\n", " h = (b-a)/n \n", " sumy = 0\n", " x=a\n", " for i in range(n):\n", " x += h\n", " sumy += ff(x)\n", " sumy += 0.5*(ff(a) + ff(b))\n", " return sumy*h" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The cdef keyword declares a C object. Everything that follows it is therefore specified in terms of C; we are essentially writing C, but using a subset of Python's syntax rules. So, when we are creating a function cdef ff it is a C function, and is not available to you in Python.\n", "\n", "cpdef is a hybrid declaration that creates both a C interface and a Python interface to the function.\n", "\n", "Let's see how this performs." ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.78 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit trapez4(1, 5, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Woof! That's a big speedup, and there's not much yellow left in the annotated code. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you would like a very simple way of injecting types into your code with Cython, without modifying any of the code itelf, you can use the @cython.locals decorator. Note that you don't get as fast of a speedup as we have just achieved." ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "%%cython\n", "import cython\n", "\n", "@cython.locals(x=cython.double)\n", "def f(x):\n", " return 2*x*x + 3*x + 1\n", " \n", "@cython.locals(a=cython.double, b=cython.double, n=cython.int,\n", " h=cython.double, sumy=cython.double, i=cython.int,\n", " x=cython.double, func=cython.double)\n", "def trapez5(a, b, n):\n", " h = (b-a)/float(n) \n", " sumy = 0\n", " x=a\n", " \n", " for i in range(n):\n", " x += h\n", " sumy += f(x)\n", " sumy += 0.5*(f(a) + f(b))\n", " return sumy*h" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "56.3 µs ± 2.71 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n" ] } ], "source": [ "%timeit trapez5(1, 5, 1000)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you can stand to look at it, you can peek at all the C code that is generated by Cython just to optimize this short function." ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [], "source": [ "# %load ../examples/trapezoid.c" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Due to conveneince, running Cython from IPython is a preferred way of using the language. However, if we have some **legacy C/C++ code** that we wish to use in Python, we can do that by writing a wrapper and calling cython from the terminal.\n", "\n", "Here is the C code:" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [], "source": [ "# %load ../examples/fact.h\n", "int fact(int n) {\n", " if(n <= 1)\n", " {\n", " return 1;\n", " }\n", " return n * fact(n-1);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And here is the Cython wrapper. Cython code is stored in files with a .pyx extension." ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [], "source": [ "# %load ../examples/fact.pyx\n", "cdef extern from \"fact.h\":\n", " int _fact \"fact\"(int)\n", "\n", "def fact(int n):\n", " return _fact(n)" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/fonnesbeck/anaconda3/envs/dev/lib/python3.6/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/fonnesbeck/repos/Bios8366/examples/fact.pyx\n", " tree = Parsing.p_module(s, pxd, full_module_name)\n" ] } ], "source": [ "!cython ../examples/fact.pyx" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can compile the extension." ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [], "source": [ "import os, sys\n", "os.chdir('../examples')\n", "\n", "if sys.platform == \"darwin\":\n", " !gcc -Wall -fno-strict-aliasing -static -undefined dynamic_lookup \\\n", " -bundle -arch x86_64 \\\n", " -I$HOME/anaconda3/include/python3.6m \\\n", " -o fact.so fact.c\n", "else:\n", " !gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing \\\n", " -I$HOME/anaconda3/include/python3.7m \\\n", " -o fact.so fact.c\n", "\n", "os.chdir('../notebooks')" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [], "source": [ "!cp ../examples/fact.so ." ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [ { "ename": "ImportError", "evalue": "/home/fonnesbeck/repos/Bios8366/notebooks/fact.so: undefined symbol: PyInterpreterState_GetID", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mfact\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mfact\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfact\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mImportError\u001b[0m: /home/fonnesbeck/repos/Bios8366/notebooks/fact.so: undefined symbol: PyInterpreterState_GetID" ] } ], "source": [ "import fact\n", "\n", "fact.fact(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Using lists and arrays in Cython\n", "\n", "The above example used only scalar variables. When we have vector-valued data, we need to declare the appropriate types. Here's a simple example, using a function that calculates the Euclidean distance between two arrays:" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [], "source": [ "def euclidean(x, y):\n", " x = np.array(x)\n", " y = np.array(y)\n", " return np.sqrt(((x - y) ** 2).sum())" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13.3 µs ± 671 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit euclidean(np.random.randn(10), np.random.randn(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In order to get a speedup under Cython, we need to iterate over the elements of each passed array, and aggregate them manually." ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " Cython: _cython_magic_0a9b7f37e79624f414a3fce164851071.pyx\n", " \n", "\n", "\n", "

Generated by Cython 0.29.2

\n", "

\n", " Yellow lines hint at Python interaction.
\n", " Click on a line that starts with a \"+\" to see the C code that Cython generated for it.\n", "

\n", "
 01:
\n", "
+02: import cython
\n", "
  __pyx_t_1 = __Pyx_PyDict_NewPresized(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 2, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 2, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"
 03: cimport numpy as np
\n", "
 04: from libc.math cimport sqrt
\n", "
 05:
\n", "
 06: @cython.boundscheck(False)
\n", "
 07: @cython.wraparound(False)
\n", "
+08: def euclidean2(np.ndarray[np.float64_t, ndim=1] x,
\n", "
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_0a9b7f37e79624f414a3fce164851071_1euclidean2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_0a9b7f37e79624f414a3fce164851071_1euclidean2 = {\"euclidean2\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_0a9b7f37e79624f414a3fce164851071_1euclidean2, METH_VARARGS|METH_KEYWORDS, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_0a9b7f37e79624f414a3fce164851071_1euclidean2(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
"  PyArrayObject *__pyx_v_x = 0;\n",
"  PyArrayObject *__pyx_v_y = 0;\n",
"  PyObject *__pyx_r = 0;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"euclidean2 (wrapper)\", 0);\n",
"  {\n",
"    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_x,&__pyx_n_s_y,0};\n",
"    PyObject* values[2] = {0,0};\n",
"    if (unlikely(__pyx_kwds)) {\n",
"      Py_ssize_t kw_args;\n",
"      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n",
"      switch (pos_args) {\n",
"        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  0: break;\n",
"        default: goto __pyx_L5_argtuple_error;\n",
"      }\n",
"      kw_args = PyDict_Size(__pyx_kwds);\n",
"      switch (pos_args) {\n",
"        case  0:\n",
"        if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_x)) != 0)) kw_args--;\n",
"        else goto __pyx_L5_argtuple_error;\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1:\n",
"        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_y)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"euclidean2\", 1, 2, 2, 1); __PYX_ERR(0, 8, __pyx_L3_error)\n",
"        }\n",
"      }\n",
"      if (unlikely(kw_args > 0)) {\n",
"        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"euclidean2\") < 0)) __PYX_ERR(0, 8, __pyx_L3_error)\n",
"      }\n",
"    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {\n",
"      goto __pyx_L5_argtuple_error;\n",
"    } else {\n",
"      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"    }\n",
"    __pyx_v_x = ((PyArrayObject *)values[0]);\n",
"    __pyx_v_y = ((PyArrayObject *)values[1]);\n",
"  }\n",
"  goto __pyx_L4_argument_unpacking_done;\n",
"  __pyx_L5_argtuple_error:;\n",
"  __Pyx_RaiseArgtupleInvalid(\"euclidean2\", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 8, __pyx_L3_error)\n",
"  __pyx_L3_error:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return NULL;\n",
"  __pyx_L4_argument_unpacking_done:;\n",
"  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_x), __pyx_ptype_5numpy_ndarray, 1, \"x\", 0))) __PYX_ERR(0, 8, __pyx_L1_error)\n",
"  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_y), __pyx_ptype_5numpy_ndarray, 1, \"y\", 0))) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"  __pyx_r = __pyx_pf_46_cython_magic_0a9b7f37e79624f414a3fce164851071_euclidean2(__pyx_self, __pyx_v_x, __pyx_v_y);\n",
"\n",
"  /* function exit code */\n",
"  goto __pyx_L0;\n",
"  __pyx_L1_error:;\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_0a9b7f37e79624f414a3fce164851071_euclidean2(CYTHON_UNUSED PyObject *__pyx_self, PyArrayObject *__pyx_v_x, PyArrayObject *__pyx_v_y) {\n",
"  double __pyx_v_diff;\n",
"  int __pyx_v_i;\n",
"  __Pyx_LocalBuf_ND __pyx_pybuffernd_x;\n",
"  __Pyx_Buffer __pyx_pybuffer_x;\n",
"  __Pyx_LocalBuf_ND __pyx_pybuffernd_y;\n",
"  __Pyx_Buffer __pyx_pybuffer_y;\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"euclidean2\", 0);\n",
"  __pyx_pybuffer_x.pybuffer.buf = NULL;\n",
"  __pyx_pybuffer_x.refcount = 0;\n",
"  __pyx_pybuffernd_x.data = NULL;\n",
"  __pyx_pybuffernd_x.rcbuffer = &__pyx_pybuffer_x;\n",
"  __pyx_pybuffer_y.pybuffer.buf = NULL;\n",
"  __pyx_pybuffer_y.refcount = 0;\n",
"  __pyx_pybuffernd_y.data = NULL;\n",
"  __pyx_pybuffernd_y.rcbuffer = &__pyx_pybuffer_y;\n",
"  {\n",
"    __Pyx_BufFmt_StackElem __pyx_stack[1];\n",
"    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_x.rcbuffer->pybuffer, (PyObject*)__pyx_v_x, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float64_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 8, __pyx_L1_error)\n",
"  }\n",
"  __pyx_pybuffernd_x.diminfo[0].strides = __pyx_pybuffernd_x.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_x.diminfo[0].shape = __pyx_pybuffernd_x.rcbuffer->pybuffer.shape[0];\n",
"  {\n",
"    __Pyx_BufFmt_StackElem __pyx_stack[1];\n",
"    if (unlikely(__Pyx_GetBufferAndValidate(&__pyx_pybuffernd_y.rcbuffer->pybuffer, (PyObject*)__pyx_v_y, &__Pyx_TypeInfo_nn___pyx_t_5numpy_float64_t, PyBUF_FORMAT| PyBUF_STRIDES, 1, 0, __pyx_stack) == -1)) __PYX_ERR(0, 8, __pyx_L1_error)\n",
"  }\n",
"  __pyx_pybuffernd_y.diminfo[0].strides = __pyx_pybuffernd_y.rcbuffer->pybuffer.strides[0]; __pyx_pybuffernd_y.diminfo[0].shape = __pyx_pybuffernd_y.rcbuffer->pybuffer.shape[0];\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_6);\n",
"  { PyObject *__pyx_type, *__pyx_value, *__pyx_tb;\n",
"    __Pyx_ErrFetch(&__pyx_type, &__pyx_value, &__pyx_tb);\n",
"    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x.rcbuffer->pybuffer);\n",
"    __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y.rcbuffer->pybuffer);\n",
"  __Pyx_ErrRestore(__pyx_type, __pyx_value, __pyx_tb);}\n",
"  __pyx_r = NULL;\n",
"  goto __pyx_L2;\n",
"  __pyx_L0:;\n",
"  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_x.rcbuffer->pybuffer);\n",
"  __Pyx_SafeReleaseBuffer(&__pyx_pybuffernd_y.rcbuffer->pybuffer);\n",
"  __pyx_L2:;\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"/* … */\n",
"  __pyx_tuple__8 = PyTuple_Pack(4, __pyx_n_s_x, __pyx_n_s_y, __pyx_n_s_diff, __pyx_n_s_i); if (unlikely(!__pyx_tuple__8)) __PYX_ERR(0, 8, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_tuple__8);\n",
"  __Pyx_GIVEREF(__pyx_tuple__8);\n",
"/* … */\n",
"  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_0a9b7f37e79624f414a3fce164851071_1euclidean2, NULL, __pyx_n_s_cython_magic_0a9b7f37e79624f414); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 8, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  if (PyDict_SetItem(__pyx_d, __pyx_n_s_euclidean2, __pyx_t_1) < 0) __PYX_ERR(0, 8, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"
 09:                np.ndarray[np.float64_t, ndim=1] y):
\n", "
 10:     cdef:
\n", "
 11:         double diff
\n", "
 12:         int i
\n", "
+13:     diff = 0
\n", "
  __pyx_v_diff = 0.0;\n",
"
+14:     for i in range(x.shape[0]):
\n", "
  __pyx_t_1 = (__pyx_v_x->dimensions[0]);\n",
"  __pyx_t_2 = __pyx_t_1;\n",
"  for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {\n",
"    __pyx_v_i = __pyx_t_3;\n",
"
+15:         diff += (x[i] - y[i])**2
\n", "
    __pyx_t_4 = __pyx_v_i;\n",
"    __pyx_t_5 = __pyx_v_i;\n",
"    __pyx_v_diff = (__pyx_v_diff + pow(((*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float64_t *, __pyx_pybuffernd_x.rcbuffer->pybuffer.buf, __pyx_t_4, __pyx_pybuffernd_x.diminfo[0].strides)) - (*__Pyx_BufPtrStrided1d(__pyx_t_5numpy_float64_t *, __pyx_pybuffernd_y.rcbuffer->pybuffer.buf, __pyx_t_5, __pyx_pybuffernd_y.diminfo[0].strides))), 2.0));\n",
"  }\n",
"
+16:     return sqrt(diff)
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_6 = PyFloat_FromDouble(sqrt(__pyx_v_diff)); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 16, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_6);\n",
"  __pyx_r = __pyx_t_6;\n",
"  __pyx_t_6 = 0;\n",
"  goto __pyx_L0;\n",
"
" ], "text/plain": [ "" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%cython --annotate\n", "\n", "import cython\n", "cimport numpy as np\n", "from libc.math cimport sqrt\n", "\n", "@cython.boundscheck(False)\n", "@cython.wraparound(False)\n", "def euclidean2(np.ndarray[np.float64_t, ndim=1] x, \n", " np.ndarray[np.float64_t, ndim=1] y):\n", " cdef: \n", " double diff\n", " int i\n", " diff = 0\n", " for i in range(x.shape[0]):\n", " diff += (x[i] - y[i])**2\n", " return sqrt(diff)\n" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.69 µs ± 287 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit euclidean2(np.random.randn(10), np.random.randn(10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The decorators for trapez5 are **compiler directives** that alter the behavior of Cython code. Setting boundscheck to False removes boundary checking for indexing operations, forcing us to ensure that we do not try to index arrays using index vlaues that are out of bounds. When we set wraparound to False, Cython will not support negative indexes, as is the case with Python. While these directives may increase the speed of our code, it can be dangerous; if we do not ensure that we index our arrays properly, it may cause segmentation faults or data corruption.\n", "\n", "The full set of compiler directives are described in the [Cython docs](http://docs.cython.org/src/reference/compilation.html#compiler-directives)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is the same code using lists instead of NumPy arrays:" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", " \n", " Cython: _cython_magic_7e5c94ff564e970b46eec387ab4af637.pyx\n", " \n", "\n", "\n", "

Generated by Cython 0.29.2

\n", "

\n", " Yellow lines hint at Python interaction.
\n", " Click on a line that starts with a \"+\" to see the C code that Cython generated for it.\n", "

\n", "
 01:
\n", "
 02: from libc.math cimport sqrt
\n", "
 03:
\n", "
+04: def euclidean3(list x, list y):
\n", "
/* Python wrapper */\n",
"static PyObject *__pyx_pw_46_cython_magic_7e5c94ff564e970b46eec387ab4af637_1euclidean3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/\n",
"static PyMethodDef __pyx_mdef_46_cython_magic_7e5c94ff564e970b46eec387ab4af637_1euclidean3 = {\"euclidean3\", (PyCFunction)(void*)(PyCFunctionWithKeywords)__pyx_pw_46_cython_magic_7e5c94ff564e970b46eec387ab4af637_1euclidean3, METH_VARARGS|METH_KEYWORDS, 0};\n",
"static PyObject *__pyx_pw_46_cython_magic_7e5c94ff564e970b46eec387ab4af637_1euclidean3(PyObject *__pyx_self, PyObject *__pyx_args, PyObject *__pyx_kwds) {\n",
"  PyObject *__pyx_v_x = 0;\n",
"  PyObject *__pyx_v_y = 0;\n",
"  PyObject *__pyx_r = 0;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"euclidean3 (wrapper)\", 0);\n",
"  {\n",
"    static PyObject **__pyx_pyargnames[] = {&__pyx_n_s_x,&__pyx_n_s_y,0};\n",
"    PyObject* values[2] = {0,0};\n",
"    if (unlikely(__pyx_kwds)) {\n",
"      Py_ssize_t kw_args;\n",
"      const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args);\n",
"      switch (pos_args) {\n",
"        case  2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  0: break;\n",
"        default: goto __pyx_L5_argtuple_error;\n",
"      }\n",
"      kw_args = PyDict_Size(__pyx_kwds);\n",
"      switch (pos_args) {\n",
"        case  0:\n",
"        if (likely((values[0] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_x)) != 0)) kw_args--;\n",
"        else goto __pyx_L5_argtuple_error;\n",
"        CYTHON_FALLTHROUGH;\n",
"        case  1:\n",
"        if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_y)) != 0)) kw_args--;\n",
"        else {\n",
"          __Pyx_RaiseArgtupleInvalid(\"euclidean3\", 1, 2, 2, 1); __PYX_ERR(0, 4, __pyx_L3_error)\n",
"        }\n",
"      }\n",
"      if (unlikely(kw_args > 0)) {\n",
"        if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, \"euclidean3\") < 0)) __PYX_ERR(0, 4, __pyx_L3_error)\n",
"      }\n",
"    } else if (PyTuple_GET_SIZE(__pyx_args) != 2) {\n",
"      goto __pyx_L5_argtuple_error;\n",
"    } else {\n",
"      values[0] = PyTuple_GET_ITEM(__pyx_args, 0);\n",
"      values[1] = PyTuple_GET_ITEM(__pyx_args, 1);\n",
"    }\n",
"    __pyx_v_x = ((PyObject*)values[0]);\n",
"    __pyx_v_y = ((PyObject*)values[1]);\n",
"  }\n",
"  goto __pyx_L4_argument_unpacking_done;\n",
"  __pyx_L5_argtuple_error:;\n",
"  __Pyx_RaiseArgtupleInvalid(\"euclidean3\", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 4, __pyx_L3_error)\n",
"  __pyx_L3_error:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return NULL;\n",
"  __pyx_L4_argument_unpacking_done:;\n",
"  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_x), (&PyList_Type), 1, \"x\", 1))) __PYX_ERR(0, 4, __pyx_L1_error)\n",
"  if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_y), (&PyList_Type), 1, \"y\", 1))) __PYX_ERR(0, 4, __pyx_L1_error)\n",
"  __pyx_r = __pyx_pf_46_cython_magic_7e5c94ff564e970b46eec387ab4af637_euclidean3(__pyx_self, __pyx_v_x, __pyx_v_y);\n",
"\n",
"  /* function exit code */\n",
"  goto __pyx_L0;\n",
"  __pyx_L1_error:;\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"\n",
"static PyObject *__pyx_pf_46_cython_magic_7e5c94ff564e970b46eec387ab4af637_euclidean3(CYTHON_UNUSED PyObject *__pyx_self, PyObject *__pyx_v_x, PyObject *__pyx_v_y) {\n",
"  double __pyx_v_diff;\n",
"  int __pyx_v_i;\n",
"  PyObject *__pyx_r = NULL;\n",
"  __Pyx_RefNannyDeclarations\n",
"  __Pyx_RefNannySetupContext(\"euclidean3\", 0);\n",
"/* … */\n",
"  /* function exit code */\n",
"  __pyx_L1_error:;\n",
"  __Pyx_XDECREF(__pyx_t_4);\n",
"  __Pyx_XDECREF(__pyx_t_5);\n",
"  __Pyx_XDECREF(__pyx_t_6);\n",
"  __Pyx_XDECREF(__pyx_t_7);\n",
"  __pyx_r = NULL;\n",
"  __pyx_L0:;\n",
"  __Pyx_XGIVEREF(__pyx_r);\n",
"  __Pyx_RefNannyFinishContext();\n",
"  return __pyx_r;\n",
"}\n",
"/* … */\n",
"  __pyx_tuple_ = PyTuple_Pack(4, __pyx_n_s_x, __pyx_n_s_y, __pyx_n_s_diff, __pyx_n_s_i); if (unlikely(!__pyx_tuple_)) __PYX_ERR(0, 4, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_tuple_);\n",
"  __Pyx_GIVEREF(__pyx_tuple_);\n",
"/* … */\n",
"  __pyx_t_1 = PyCFunction_NewEx(&__pyx_mdef_46_cython_magic_7e5c94ff564e970b46eec387ab4af637_1euclidean3, NULL, __pyx_n_s_cython_magic_7e5c94ff564e970b46); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 4, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_1);\n",
"  if (PyDict_SetItem(__pyx_d, __pyx_n_s_euclidean3, __pyx_t_1) < 0) __PYX_ERR(0, 4, __pyx_L1_error)\n",
"  __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0;\n",
"
 05:     cdef:
\n", "
 06:         double diff
\n", "
 07:         int i
\n", "
+08:     diff = 0
\n", "
  __pyx_v_diff = 0.0;\n",
"
+09:     for i in range(len(x)):
\n", "
  if (unlikely(__pyx_v_x == Py_None)) {\n",
"    PyErr_SetString(PyExc_TypeError, \"object of type 'NoneType' has no len()\");\n",
"    __PYX_ERR(0, 9, __pyx_L1_error)\n",
"  }\n",
"  __pyx_t_1 = PyList_GET_SIZE(__pyx_v_x); if (unlikely(__pyx_t_1 == ((Py_ssize_t)-1))) __PYX_ERR(0, 9, __pyx_L1_error)\n",
"  __pyx_t_2 = __pyx_t_1;\n",
"  for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) {\n",
"    __pyx_v_i = __pyx_t_3;\n",
"
+10:         diff += (x[i] - y[i])**2
\n", "
    __pyx_t_4 = PyFloat_FromDouble(__pyx_v_diff); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_4);\n",
"    if (unlikely(__pyx_v_x == Py_None)) {\n",
"      PyErr_SetString(PyExc_TypeError, \"'NoneType' object is not subscriptable\");\n",
"      __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    }\n",
"    __pyx_t_5 = __Pyx_GetItemInt_List(__pyx_v_x, __pyx_v_i, int, 1, __Pyx_PyInt_From_int, 1, 1, 1); if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_5);\n",
"    if (unlikely(__pyx_v_y == Py_None)) {\n",
"      PyErr_SetString(PyExc_TypeError, \"'NoneType' object is not subscriptable\");\n",
"      __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    }\n",
"    __pyx_t_6 = __Pyx_GetItemInt_List(__pyx_v_y, __pyx_v_i, int, 1, __Pyx_PyInt_From_int, 1, 1, 1); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_6);\n",
"    __pyx_t_7 = PyNumber_Subtract(__pyx_t_5, __pyx_t_6); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_7);\n",
"    __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0;\n",
"    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
"    __pyx_t_6 = PyNumber_Power(__pyx_t_7, __pyx_int_2, Py_None); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_6);\n",
"    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;\n",
"    __pyx_t_7 = PyNumber_InPlaceAdd(__pyx_t_4, __pyx_t_6); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_GOTREF(__pyx_t_7);\n",
"    __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0;\n",
"    __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0;\n",
"    __pyx_t_8 = __pyx_PyFloat_AsDouble(__pyx_t_7); if (unlikely((__pyx_t_8 == (double)-1) && PyErr_Occurred())) __PYX_ERR(0, 10, __pyx_L1_error)\n",
"    __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0;\n",
"    __pyx_v_diff = __pyx_t_8;\n",
"  }\n",
"
+11:     return sqrt(diff)
\n", "
  __Pyx_XDECREF(__pyx_r);\n",
"  __pyx_t_7 = PyFloat_FromDouble(sqrt(__pyx_v_diff)); if (unlikely(!__pyx_t_7)) __PYX_ERR(0, 11, __pyx_L1_error)\n",
"  __Pyx_GOTREF(__pyx_t_7);\n",
"  __pyx_r = __pyx_t_7;\n",
"  __pyx_t_7 = 0;\n",
"  goto __pyx_L0;\n",
"
" ], "text/plain": [ "" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%cython --annotate\n", "\n", "from libc.math cimport sqrt\n", "\n", "def euclidean3(list x, list y):\n", " cdef: \n", " double diff\n", " int i\n", " diff = 0\n", " for i in range(len(x)):\n", " diff += (x[i] - y[i])**2\n", " return sqrt(diff)\n" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6.87 µs ± 2.63 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit euclidean3(np.random.randn(10).tolist(), np.random.randn(10).tolist())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pyximport\n", "\n", "If we have some Cython source code, we can use pyximport to directly import it as if it were a Python module." ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/fonnesbeck/anaconda3/envs/dev/lib/python3.6/site-packages/Cython/Compiler/Main.py:367: FutureWarning: Cython directive 'language_level' not set, using 2 for now (Py2). This will change in a later release! File: /home/fonnesbeck/repos/Bios8366/notebooks/trapezoid.pyx\n", " tree = Parsing.p_module(s, pxd, full_module_name)\n" ] }, { "data": { "text/plain": [ "1033.8299560546875" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyximport\n", "\n", "# Move source into current directory\n", "!cp ../examples/trapezoid.pyx .\n", "\n", "# Allow it to use Python's import mechanism\n", "pyximport.install()\n", "\n", "from trapezoid import trapez as trapez_pyx\n", "trapez_pyx(1, 10, 10) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In other words, it treats .pyx files as if they were .py files. This includes detecting changes in the source file,, if any, and recompiling it as necessary before importing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Benchmark example: Gibbs sampling\n", "\n", "Let's see if we can use Cython to speed up MCMC.\n", "\n", "Gibbs sampler for function:\n", "\n", "$$f(x,y) = x x^2 \\exp(-xy^2 - y^2 + 2y - 4x)$$\n", "\n", "using conditional distributions:\n", " \n", "$$x|y \\sim Gamma(3, y^2 +4)$$\n", "$$y|x \\sim Normal(\\frac{1}{1+x}, \\frac{1}{2(1+x)})$$\n", "\n", "Here is the pure Python implementation:" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "from numpy import zeros, random, sqrt\n", "gamma = random.gamma\n", "normal = random.normal\n", "\n", "def pygibbs(N=20000, thin=200):\n", " mat = zeros((N,2))\n", " x,y = mat[0]\n", " for i in range(N):\n", " for j in range(thin):\n", " x = gamma(3, y**2 + 4)\n", " y = normal(1./(x+1), 1./sqrt(2*(x+1)))\n", " mat[i] = x,y\n", "\n", " return mat" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "86.7 ms ± 9.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" ] } ], "source": [ "%timeit pygibbs(1000, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unchanged, compiling this code with Cython results in a slight improvement in speed." ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "%%cython\n", "\n", "from numpy import zeros, random, sqrt\n", "gamma = random.gamma\n", "normal = random.normal\n", "\n", "def cygibbs(N=20000, thin=200):\n", " mat = zeros((N,2))\n", " x,y = mat[0]\n", " for i in range(N):\n", " for j in range(thin):\n", " x = gamma(3, y**2 + 4)\n", " y = normal(1./(x+1), 1./sqrt(2*(x+1)))\n", " mat[i] = x,y\n", "\n", " return mat" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "76.3 ms ± 2.92 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" ] } ], "source": [ "%timeit cygibbs(1000, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, for some type declarations:" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "%%cython\n", "\n", "from numpy import zeros, random, sqrt\n", "from numpy cimport *\n", "gamma = random.gamma\n", "normal = random.normal\n", "\n", "def cygibbs2(int N=20000, int thin=200):\n", " cdef: \n", " ndarray[float64_t, ndim=2] mat = zeros((N,2))\n", " float64_t x,y = 0\n", " int i,j\n", " for i in range(N):\n", " for j in range(thin):\n", " x = gamma(3, y**2 + 4)\n", " y = normal(1./(x+1), 1./sqrt(2*(x+1)))\n", " mat[i] = x,y\n", "\n", " return mat" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "80.7 ms ± 6.31 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" ] } ], "source": [ "%timeit cygibbs2(1000, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A full-blown \"Cythonization\" involves using GSL's random number generators, and giving Cython a few more instructions:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you are using Homebrew on OS X, you can install GSL using \"brew\"\n", "\n", " !brew install gsl" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "%%cython -lm -lgsl -lgslcblas\n", "\n", "cimport cython\n", "import numpy as np\n", "from numpy cimport *\n", "\n", "cdef extern from \"math.h\":\n", " double sqrt(double) \n", " \n", "cdef extern from \"gsl/gsl_rng.h\":\n", " ctypedef struct gsl_rng_type\n", " ctypedef struct gsl_rng\n", "\n", " gsl_rng_type *gsl_rng_mt19937\n", " gsl_rng *gsl_rng_alloc(gsl_rng_type * T) nogil\n", " \n", "cdef extern from \"gsl/gsl_randist.h\":\n", " double gamma \"gsl_ran_gamma\"(gsl_rng * r,double,double)\n", " double gaussian \"gsl_ran_gaussian\"(gsl_rng * r,double)\n", " \n", "cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)\n", "\n", "@cython.wraparound(False)\n", "@cython.boundscheck(False)\n", "def gibbs(int N=20000,int thin=500):\n", " cdef: \n", " double x=0\n", " double y=0\n", " int i, j\n", " ndarray[float64_t, ndim=2] samples\n", "\n", " samples = np.empty((N,thin))\n", " for i from 0 <= i < N:\n", " for j from 0 <= j < thin:\n", " x = gamma(r,3,1.0/(y*y+4))\n", " y = gaussian(r,1.0/sqrt(x+1))\n", " samples[i,0] = x\n", " samples[i,1] = y\n", " return samples" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.52 ms ± 95.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%timeit gibbs(1000, 10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numba\n", "\n", "Cython precompiles parts of Python code before running. Another approach is **Just-in-Time (JIT)** compilation. Numba is a compiler that runs Python code through an LLVM compiler to produce optimized bytecode for fast execution. Numba does not require a C/C++ compiler on your machine.\n", "\n", "Numba's lone API is a **decorator**.\n", "\n", "The @jit decorator runs the decorated function through bytecode analysis and the function arguments through a type inference engine, and generates an intermediate representation of your code, which is then passed to LLVM for compilation to bytecode." ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [], "source": [ "from numba import jit, autojit" ] }, { "cell_type": "code", "execution_count": 92, "metadata": {}, "outputs": [], "source": [ "def nfibonacci(size):\n", " F = np.empty(size, 'int')\n", " a, b = 0, 1\n", " for i in range(size):\n", " F[i] = a\n", " a, b = b, a + b\n", " return F" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "13.2 µs ± 2.35 µs per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit nfibonacci(50)" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [], "source": [ "@jit\n", "def nfibonacci(size):\n", " F = np.empty(size, 'int')\n", " a, b = 0, 1\n", " for i in range(size):\n", " F[i] = a\n", " a, b = b, a + b\n", " return F" ] }, { "cell_type": "code", "execution_count": 95, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 9.44 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "19.7 µs ± 22.4 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit nfibonacci(50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numba is able to compile separate specializations depending on the input types." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want fine-grained control over types chosen by the compiler, you can tell Numba the function signature (types) to expect." ] }, { "cell_type": "code", "execution_count": 96, "metadata": {}, "outputs": [], "source": [ "from numba import int32\n", "\n", "@jit(int32[:](int32))\n", "def nfibonacci(size):\n", " F = np.empty(size, 'int')\n", " a, b = 0, 1\n", " for i in range(size):\n", " F[i] = a\n", " a, b = b, a + b\n", " return F" ] }, { "cell_type": "code", "execution_count": 97, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.61 µs ± 290 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit nfibonacci(50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compilation is deferred until the first function execution. Numba will infer the argument types at call time, and generate optimized code based on this information. " ] }, { "cell_type": "code", "execution_count": 98, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6.49 s ± 605 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "def pairwise_python(X):\n", " M = X.shape[0]\n", " N = X.shape[1]\n", " D = np.empty((M, M), dtype=np.float)\n", " for i in range(M):\n", " for j in range(M):\n", " d = 0.0\n", " for k in range(N):\n", " tmp = X[i, k] - X[j, k]\n", " d += tmp * tmp\n", " D[i, j] = np.sqrt(d)\n", " return D\n", "\n", "X = np.random.random((1000, 3))\n", "\n", "%timeit pairwise_python(X)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4.22 ms ± 161 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "@jit\n", "def npairwise_python(X):\n", " M = X.shape[0]\n", " N = X.shape[1]\n", " D = np.empty((M, M), dtype=np.float)\n", " for i in range(M):\n", " for j in range(M):\n", " d = 0.0\n", " for k in range(N):\n", " tmp = X[i, k] - X[j, k]\n", " d += tmp * tmp\n", " D[i, j] = np.sqrt(d)\n", " return D\n", "\n", "%timeit npairwise_python(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numba-compiled functions can call other compiled functions. In some situations, the optimizer may even inline the function in the machine code code." ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [], "source": [ "def square(x):\n", " return x ** 2\n", "\n", "def hypot(x, y):\n", " return np.sqrt(square(x) + square(y))" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.88 µs ± 261 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n" ] } ], "source": [ "%timeit hypot(10, 8)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [], "source": [ "@jit\n", "def nsquare(x):\n", " return x ** 2\n", "\n", "@jit\n", "def nhypot(x, y):\n", " return np.sqrt(nsquare(x) + nsquare(y))" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The slowest run took 8.08 times longer than the fastest. This could mean that an intermediate result is being cached.\n", "3.17 µs ± 3.78 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n" ] } ], "source": [ "%timeit nhypot(10, 8)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Numba **can** compile *most* NumPy functions, as well as generators.\n", "\n", "Numba **cannot** compile things like lists, sets, dictionaries (tuples are compiled), comprehensions, and string operations, so there will be no speedup for these.\n", "\n", "As with all performance tools, the best strategy is not to apply the @jit decorator all over your code, but to use Python's profiling tools to identify \"hotspots\" in your program, and selectively apply @jit." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercise: Fixed-point algorithm speedup\n", "\n", "Use the method of your choice to speed up the execution of the fixed-point algorithm that was implemented in the optimization lecture (you will find it in the examples directory)." ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [], "source": [ "# Write your answer here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "## References\n", "\n", "van der Plas, J. (2014) [Why Python is Slow: Looking Under the Hood](https://jakevdp.github.io/blog/2014/05/09/why-python-is-slow/)\n", "\n", "van der Plas, J. (2015) [Optimizing Python in the Real World: NumPy, Numba, and the NUFFT](https://jakevdp.github.io/blog/2015/02/24/optimizing-python-with-numpy-and-numba/)\n", "\n", "[A guide to analyzing Python performance](http://www.huyng.com/posts/python-performance-analysis/)\n", "\n", "[Kurt Smith's Cython tutorial from SciPy 2013](https://www.youtube.com/watch?v=JKCjsRDffXo)\n" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.7" }, "latex_envs": { "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 0 }, "nav_menu": {}, "toc": { "navigate_menu": true, "number_sections": false, "sideBar": false, "threshold": "3", "toc_cell": true, "toc_section_display": "block", "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }