/usr/share/doc/python-dask-doc/html/custom-collections.html is in python-dask-doc 0.16.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 | <!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Collections — dask 0.16.0 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/style.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="dask 0.16.0 documentation" href="index.html"/>
<link rel="next" title="Citations" href="cite.html"/>
<link rel="prev" title="Remote Data Services" href="remote-data-services.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> dask
</a>
<div class="version">
0.16.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="install.html">Install Dask</a></li>
<li class="toctree-l1"><a class="reference internal" href="use-cases.html">Use Cases</a></li>
<li class="toctree-l1"><a class="reference internal" href="examples-tutorials.html">Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="cheatsheet.html">Dask Cheat Sheet</a></li>
</ul>
<p class="caption"><span class="caption-text">Collections</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="array.html">Array</a></li>
<li class="toctree-l1"><a class="reference internal" href="bag.html">Bag</a></li>
<li class="toctree-l1"><a class="reference internal" href="dataframe.html">DataFrame</a></li>
<li class="toctree-l1"><a class="reference internal" href="delayed.html">Delayed</a></li>
<li class="toctree-l1"><a class="reference internal" href="futures.html">Futures</a></li>
<li class="toctree-l1"><a class="reference internal" href="machine-learning.html">Machine Learning</a></li>
</ul>
<p class="caption"><span class="caption-text">Scheduling</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="distributed.html">Distributed Scheduling</a></li>
<li class="toctree-l1"><a class="reference internal" href="scheduler-overview.html">Scheduler Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="scheduler-choice.html">Choosing between Schedulers</a></li>
<li class="toctree-l1"><a class="reference internal" href="shared.html">Shared Memory</a></li>
<li class="toctree-l1"><a class="reference internal" href="scheduling-policy.html">Scheduling in Depth</a></li>
</ul>
<p class="caption"><span class="caption-text">Diagnostics</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="inspect.html">Inspecting Dask objects</a></li>
<li class="toctree-l1"><a class="reference internal" href="diagnostics.html">Diagnostics</a></li>
</ul>
<p class="caption"><span class="caption-text">Graphs</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="graphs.html">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="spec.html">Specification</a></li>
<li class="toctree-l1"><a class="reference internal" href="custom-graphs.html">Custom Graphs</a></li>
<li class="toctree-l1"><a class="reference internal" href="optimize.html">Optimization</a></li>
</ul>
<p class="caption"><span class="caption-text">Help & reference</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="debugging.html">Debugging</a></li>
<li class="toctree-l1"><a class="reference internal" href="support.html">Contact and Support</a></li>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="presentations.html">Presentations On Dask</a></li>
<li class="toctree-l1"><a class="reference internal" href="develop.html">Development Guidelines</a></li>
<li class="toctree-l1"><a class="reference internal" href="faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="spark.html">Comparison to PySpark</a></li>
<li class="toctree-l1"><a class="reference internal" href="caching.html">Opportunistic Caching</a></li>
<li class="toctree-l1"><a class="reference internal" href="bytes.html">Internal Data Ingestion</a></li>
<li class="toctree-l1"><a class="reference internal" href="remote-data-services.html">Remote Data Services</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Custom Collections</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#the-dask-collection-interface">The Dask Collection Interface</a></li>
<li class="toctree-l2"><a class="reference internal" href="#internals-of-the-core-dask-methods">Internals of the Core Dask Methods</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#compute">Compute</a></li>
<li class="toctree-l3"><a class="reference internal" href="#persist">Persist</a></li>
<li class="toctree-l3"><a class="reference internal" href="#visualize">Visualize</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#adding-the-core-dask-methods-to-your-class">Adding the Core Dask Methods to Your Class</a></li>
<li class="toctree-l2"><a class="reference internal" href="#example-dask-collection">Example Dask Collection</a></li>
<li class="toctree-l2"><a class="reference internal" href="#checking-if-an-object-is-a-dask-collection">Checking if an object is a dask collection</a></li>
<li class="toctree-l2"><a class="reference internal" href="#implementing-deterministic-hashing">Implementing Deterministic Hashing</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#example">Example</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="cite.html">Citations</a></li>
<li class="toctree-l1"><a class="reference internal" href="funding.html">Funding</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">dask</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Custom Collections</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/custom-collections.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="custom-collections">
<h1>Custom Collections<a class="headerlink" href="#custom-collections" title="Permalink to this headline">¶</a></h1>
<p>For many problems the built-in dask collections (<code class="docutils literal"><span class="pre">dask.array</span></code>,
<code class="docutils literal"><span class="pre">dask.dataframe</span></code>, <code class="docutils literal"><span class="pre">dask.bag</span></code>, and <code class="docutils literal"><span class="pre">dask.delayed</span></code>) are sufficient. For
cases where they aren’t it’s possible to create your own dask collections. Here
we describe the required methods to fullfill the dask collection interface.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The custom collection API is experimental and subject to change
without going through a deprecation cycle.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This is considered an advanced feature. For most cases the built-in
collections are probably sufficient.</p>
</div>
<p>Before reading this you should read and underestand:</p>
<ul class="simple">
<li><a class="reference internal" href="graphs.html"><span class="doc">overview</span></a></li>
<li><a class="reference internal" href="spec.html"><span class="doc">graph specification</span></a></li>
<li><a class="reference internal" href="custom-graphs.html"><span class="doc">custom graphs</span></a></li>
</ul>
<p><strong>Contents</strong></p>
<ul class="simple">
<li><a class="reference internal" href="#collection-interface"><span class="std std-ref">Description of the dask collection interface</span></a></li>
<li><a class="reference internal" href="#core-method-internals"><span class="std std-ref">How this interface is used to implement the core dask
methods</span></a></li>
<li><a class="reference internal" href="#adding-methods-to-class"><span class="std std-ref">How to add the core methods to your class</span></a></li>
<li><a class="reference internal" href="#example-dask-collection"><span class="std std-ref">Example Dask Collection</span></a></li>
<li><a class="reference internal" href="#is-dask-collection"><span class="std std-ref">How to check if something is a dask collection</span></a></li>
<li><a class="reference internal" href="#deterministic-hashing"><span class="std std-ref">How to make tokenize work with your collection</span></a></li>
</ul>
<div class="section" id="the-dask-collection-interface">
<span id="collection-interface"></span><h2>The Dask Collection Interface<a class="headerlink" href="#the-dask-collection-interface" title="Permalink to this headline">¶</a></h2>
<p>To create your own dask collection, you need to fullfill the following
interface. Note that there is no required base class.</p>
<p>It’s recommended to also read <a class="reference internal" href="#core-method-internals"><span class="std std-ref">Internals of the Core Dask Methods</span></a> to see how this
interface is used inside dask.</p>
<dl class="method">
<dt id="__dask_graph__">
<code class="descname">__dask_graph__</code><span class="sig-paren">(</span><em>self</em><span class="sig-paren">)</span><a class="headerlink" href="#__dask_graph__" title="Permalink to this definition">¶</a></dt>
<dd><p>The dask graph.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>dsk</strong> : MutableMapping, None</p>
<blockquote class="last">
<div><p>The dask graph. If <code class="docutils literal"><span class="pre">None</span></code>, this instance will not be interpreted as a
dask collection, and none of the remaining interface methods will be
called.</p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="__dask_keys__">
<code class="descname">__dask_keys__</code><span class="sig-paren">(</span><em>self</em><span class="sig-paren">)</span><a class="headerlink" href="#__dask_keys__" title="Permalink to this definition">¶</a></dt>
<dd><p>The output keys for the dask graph.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>keys</strong> : list</p>
<blockquote class="last">
<div><p>A possibly nested list of keys that represent the outputs of the graph.
After computation, the results will be returned in the same layout,
with the keys replaced with their corresponding outputs.</p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="staticmethod">
<dt id="__dask_optimize__">
<em class="property">static </em><code class="descname">__dask_optimize__</code><span class="sig-paren">(</span><em>dsk</em>, <em>keys</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#__dask_optimize__" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a graph and keys, return a new optimized graph.</p>
<p>This method can be either a <code class="docutils literal"><span class="pre">staticmethod</span></code> or a <code class="docutils literal"><span class="pre">classmethod</span></code>, but not
an instancemethod.</p>
<p>Note that graphs and keys are merged before calling <code class="docutils literal"><span class="pre">__dask_optimize__</span></code>;
as such the graph and keys passed to this method may represent more than
one collection sharing the same optimize method.</p>
<p>If not implemented, defaults to returning the graph unchanged.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><p class="first"><strong>dsk</strong> : MutableMapping</p>
<blockquote>
<div><p>The merged graphs from all collections sharing the same
<code class="docutils literal"><span class="pre">__dask_optimize__</span></code> method.</p>
</div></blockquote>
<p><strong>keys</strong> : list</p>
<blockquote>
<div><p>A list of the outputs from <code class="docutils literal"><span class="pre">__dask_keys__</span></code> from all collections
sharing the same <code class="docutils literal"><span class="pre">__dask_optimize__</span></code> method.</p>
</div></blockquote>
<p><strong>**kwargs</strong></p>
<blockquote>
<div><p>Extra keyword arguments forwarded from the call to <code class="docutils literal"><span class="pre">compute</span></code> or
<code class="docutils literal"><span class="pre">persist</span></code>. Can be used or ignored as needed.</p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>optimized_dsk</strong> : MutableMapping</p>
<blockquote class="last">
<div><p>The optimized dask graph.</p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="staticmethod">
<dt id="__dask_scheduler__">
<em class="property">static </em><code class="descname">__dask_scheduler__</code><span class="sig-paren">(</span><em>dsk</em>, <em>keys</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#__dask_scheduler__" title="Permalink to this definition">¶</a></dt>
<dd><p>The default scheduler <code class="docutils literal"><span class="pre">get</span></code> to use for this object.</p>
<p>Usually attached to the class as a staticmethod, e.g.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">dask.threaded</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">MyCollection</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="c1"># Use the threaded scheduler by default</span>
<span class="gp">... </span> <span class="n">__dask_scheduler__</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">dask</span><span class="o">.</span><span class="n">threaded</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="__dask_postcompute__">
<code class="descname">__dask_postcompute__</code><span class="sig-paren">(</span><em>self</em><span class="sig-paren">)</span><a class="headerlink" href="#__dask_postcompute__" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the finalizer and (optional) extra arguments to convert the computed
results into their in-memory representation.</p>
<p>Used to implement <code class="docutils literal"><span class="pre">dask.compute</span></code>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>finalize</strong> : callable</p>
<blockquote>
<div><p>A function with the signature <code class="docutils literal"><span class="pre">finalize(results,</span> <span class="pre">*extra_args)</span></code>.
Called with the computed results in the same structure as the
corresponding keys from <code class="docutils literal"><span class="pre">__dask_keys__</span></code>, as well as any extra
arguments as specified in <code class="docutils literal"><span class="pre">extra_args</span></code>. Should perform any necessary
finalization before returning the corresponding in-memory collection
from <code class="docutils literal"><span class="pre">compute</span></code>. For example, the <code class="docutils literal"><span class="pre">finalize</span></code> function for
<code class="docutils literal"><span class="pre">dask.array.Array</span></code> concatenates all the individual array chunks into
one large numpy array, which is then the result of <code class="docutils literal"><span class="pre">compute</span></code>.</p>
</div></blockquote>
<p><strong>extra_args</strong> : tuple</p>
<blockquote class="last">
<div><p>Any extra arguments to pass to <code class="docutils literal"><span class="pre">finalize</span></code> after <code class="docutils literal"><span class="pre">results</span></code>. If no
extra arguments should be an empty tuple.</p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="__dask_postpersist__">
<code class="descname">__dask_postpersist__</code><span class="sig-paren">(</span><em>self</em><span class="sig-paren">)</span><a class="headerlink" href="#__dask_postpersist__" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the rebuilder and (optional) extra arguments to rebuild an equivalent
dask collection from a persisted graph.</p>
<p>Used to implement <code class="docutils literal"><span class="pre">dask.persist</span></code>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>rebuild</strong> : callable</p>
<blockquote>
<div><p>A function with the signature <code class="docutils literal"><span class="pre">rebuild(dsk,</span> <span class="pre">*extra_args)</span></code>. Called
with a persisted graph containing only the keys and results from
<code class="docutils literal"><span class="pre">__dask_keys__</span></code>, as well as any extra arguments as specified in
<code class="docutils literal"><span class="pre">extra_args</span></code>. Should return an equivalent dask collection with the
same keys as <code class="docutils literal"><span class="pre">self</span></code>, but with the results already computed. For
example, the <code class="docutils literal"><span class="pre">rebuild</span></code> function for <code class="docutils literal"><span class="pre">dask.array.Array</span></code> is just the
<code class="docutils literal"><span class="pre">__init__</span></code> method called with the new graph but the same metadata.</p>
</div></blockquote>
<p><strong>extra_args</strong> : tuple</p>
<blockquote class="last">
<div><p>Any extra arguments to pass to <code class="docutils literal"><span class="pre">rebuild</span></code> after <code class="docutils literal"><span class="pre">dsk</span></code>. If no extra
arguments should be an empty tuple.</p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">It’s also recommended to define <code class="docutils literal"><span class="pre">__dask_tokenize__</span></code>,
see <a class="reference internal" href="#deterministic-hashing"><span class="std std-ref">Implementing Deterministic Hashing</span></a>.</p>
</div>
</div>
<div class="section" id="internals-of-the-core-dask-methods">
<span id="core-method-internals"></span><h2>Internals of the Core Dask Methods<a class="headerlink" href="#internals-of-the-core-dask-methods" title="Permalink to this headline">¶</a></h2>
<p>Dask has a few <em>core</em> functions (and corresponding methods) that implement
common operations:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">compute</span></code>: convert one or more dask collections into their in-memory
counterparts</li>
<li><code class="docutils literal"><span class="pre">persist</span></code>: convert one or more dask collections into equivalent dask
collections with their results already computed and cached in memory.</li>
<li><code class="docutils literal"><span class="pre">visualize</span></code>: given one or more dask collections, draw out the graph that
would be passed to the scheduler during a call to <code class="docutils literal"><span class="pre">compute</span></code> or <code class="docutils literal"><span class="pre">persist</span></code></li>
</ul>
<p>Here we briefly describe the internals of these functions to illustrate how
they relate to the above interface.</p>
<div class="section" id="compute">
<h3>Compute<a class="headerlink" href="#compute" title="Permalink to this headline">¶</a></h3>
<p>The operation of <code class="docutils literal"><span class="pre">compute</span></code> can be broken into three stages:</p>
<ol class="arabic">
<li><p class="first"><strong>Graph Merging & Optimization</strong></p>
<p>First the individual collections are converted to a single large graph and
nested list of keys. How this happens depends on the value of the
<code class="docutils literal"><span class="pre">optimize_graph</span></code> keyword, which each function takes:</p>
<ul class="simple">
<li>If <code class="docutils literal"><span class="pre">optimize_graph</span></code> is <code class="docutils literal"><span class="pre">True</span></code> (default) then the collections are first
grouped by their <code class="docutils literal"><span class="pre">__dask_optimize__</span></code> methods. All collections with the
same <code class="docutils literal"><span class="pre">__dask_optimize__</span></code> method have their graphs merged and keys
concatenated, and then a single call to each respective
<code class="docutils literal"><span class="pre">__dask_optimize__</span></code> is made with the merged graphs and keys. The
resulting graphs are then merged.</li>
<li>If <code class="docutils literal"><span class="pre">optimize_graph</span></code> is <code class="docutils literal"><span class="pre">False</span></code> then all the graphs are merged and all
the keys concatenated.</li>
</ul>
<p>After this stage there is a single large graph and nested list of keys which
represents all the collections.</p>
</li>
<li><p class="first"><strong>Computation</strong></p>
<p>After the graphs are merged and any optimizations performed, the resulting
large graph and nested list of keys are passed on to the scheduler. The
scheduler to use is chosen as follows:</p>
<ul class="simple">
<li>If a <code class="docutils literal"><span class="pre">get</span></code> function is specified directly as a keyword, use that.</li>
<li>Otherwise, if a global scheduler is set, use that.</li>
<li>Otherwise fall back to the default scheduler for the given collections.
Note that if all collections don’t share the same <code class="docutils literal"><span class="pre">__dask_scheduler__</span></code>
then an error will be raised.</li>
</ul>
<p>Once the appropriate scheduler <code class="docutils literal"><span class="pre">get</span></code> function is determined, it’s called
with the merged graph, keys, and extra keyword arguments. After this stage
<code class="docutils literal"><span class="pre">results</span></code> is a nested list of values. The structure of this list mirrors
that of <code class="docutils literal"><span class="pre">keys</span></code>, with each key substituted with its corresponding result.</p>
</li>
<li><p class="first"><strong>Postcompute</strong></p>
<p>After the results are generated the output values of <code class="docutils literal"><span class="pre">compute</span></code> need to be
built. This is what the <code class="docutils literal"><span class="pre">__dask_postcompute__</span></code> method is for.
<code class="docutils literal"><span class="pre">__dask_postcompute__</span></code> returns two things:</p>
<ul class="simple">
<li>A <code class="docutils literal"><span class="pre">finalize</span></code> function, which takes in the results for the corresponding
keys</li>
<li>A tuple of extra arguments to pass to <code class="docutils literal"><span class="pre">finalize</span></code> after the results</li>
</ul>
<p>To build the outputs, the list of collections and results is iterated over,
and the finalizer for each collection is called on its respective results.</p>
</li>
</ol>
<p>In pseudocode this process looks like:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">compute</span><span class="p">(</span><span class="o">*</span><span class="n">collections</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># 1. Graph Merging & Optimization</span>
<span class="c1"># -------------------------------</span>
<span class="k">if</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="s1">'optimize_graph'</span><span class="p">,</span> <span class="kc">True</span><span class="p">):</span>
<span class="c1"># If optimization is turned on, group the collections by</span>
<span class="c1"># optimization method, and apply each method only once to the merged</span>
<span class="c1"># sub-graphs.</span>
<span class="n">optimization_groups</span> <span class="o">=</span> <span class="n">groupby_optimization_methods</span><span class="p">(</span><span class="n">collections</span><span class="p">)</span>
<span class="n">graphs</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">optimize_method</span><span class="p">,</span> <span class="n">cols</span> <span class="ow">in</span> <span class="n">optimization_groups</span><span class="p">:</span>
<span class="c1"># Merge the graphs and keys for the subset of collections that</span>
<span class="c1"># share this optimization method</span>
<span class="n">sub_graph</span> <span class="o">=</span> <span class="n">merge_graphs</span><span class="p">([</span><span class="n">x</span><span class="o">.</span><span class="n">__dask_graph__</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">cols</span><span class="p">])</span>
<span class="n">sub_keys</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">__dask_keys__</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">cols</span><span class="p">]</span>
<span class="c1"># kwargs are forwarded to ``__dask_optimize__`` from compute</span>
<span class="n">optimized_graph</span> <span class="o">=</span> <span class="n">optimize_method</span><span class="p">(</span><span class="n">sub_graph</span><span class="p">,</span> <span class="n">sub_keys</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="n">graphs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">optimized_graph</span><span class="p">)</span>
<span class="n">graph</span> <span class="o">=</span> <span class="n">merge_graphs</span><span class="p">(</span><span class="n">graphs</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">graph</span> <span class="o">=</span> <span class="n">merge_graphs</span><span class="p">([</span><span class="n">x</span><span class="o">.</span><span class="n">__dask_graph__</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">collections</span><span class="p">])</span>
<span class="c1"># Keys are always the same</span>
<span class="n">keys</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">.</span><span class="n">__dask_keys__</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">collections</span><span class="p">]</span>
<span class="c1"># 2. Computation</span>
<span class="c1"># --------------</span>
<span class="c1"># Determine appropriate get function based on collections, global</span>
<span class="c1"># settings, and keyword arguments</span>
<span class="n">get</span> <span class="o">=</span> <span class="n">determine_get_function</span><span class="p">(</span><span class="n">collections</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="c1"># Pass the merged graph, keys, and kwargs to ``get``</span>
<span class="n">results</span> <span class="o">=</span> <span class="n">get</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="n">keys</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
<span class="c1"># 3. Postcompute</span>
<span class="c1"># --------------</span>
<span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># Iterate over the results and collections</span>
<span class="k">for</span> <span class="n">res</span><span class="p">,</span> <span class="n">collection</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">collections</span><span class="p">):</span>
<span class="n">finalize</span><span class="p">,</span> <span class="n">extra_args</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">__dask_postcompute__</span><span class="p">()</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">finalize</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="o">**</span><span class="n">extra_args</span><span class="p">)</span>
<span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">out</span><span class="p">)</span>
<span class="c1"># `dask.compute` always returns tuples</span>
<span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="persist">
<h3>Persist<a class="headerlink" href="#persist" title="Permalink to this headline">¶</a></h3>
<p>Persist is very similar to <code class="docutils literal"><span class="pre">compute</span></code>, except for how the return values are
created. It too has three stages:</p>
<ol class="arabic">
<li><p class="first"><strong>Graph Merging & Optimization</strong></p>
<p>Same as in <code class="docutils literal"><span class="pre">compute</span></code>.</p>
</li>
<li><p class="first"><strong>Computation</strong></p>
<p>Same as in <code class="docutils literal"><span class="pre">compute</span></code>, except in the case of the distributed scheduler,
where the values in <code class="docutils literal"><span class="pre">results</span></code> are futures instead of values.</p>
</li>
<li><p class="first"><strong>Postpersist</strong></p>
<p>Similar to <code class="docutils literal"><span class="pre">__dask_postcompute__</span></code>, <code class="docutils literal"><span class="pre">__dask_postpersist__</span></code> is used to
rebuild values in a call to <code class="docutils literal"><span class="pre">persist</span></code>. <code class="docutils literal"><span class="pre">__dask_postpersist__</span></code> returns
two things:</p>
<ul class="simple">
<li>A <code class="docutils literal"><span class="pre">rebuild</span></code> function, which takes in a persisted graph. The keys of
this graph are the same as <code class="docutils literal"><span class="pre">__dask_keys__</span></code> for the corresponding
collection, and the values are computed results (for the single machine
scheduler) or futures (for the distributed scheduler).</li>
<li>A tuple of extra arguments to pass to <code class="docutils literal"><span class="pre">rebuild</span></code> after the graph</li>
</ul>
<p>To build the outputs of <code class="docutils literal"><span class="pre">persist</span></code>, the list of collections and results is
iterated over, and the rebuilder for each collection is called on the graph
for its respective results.</p>
</li>
</ol>
<p>In pseudocode this looks like:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">persist</span><span class="p">(</span><span class="o">*</span><span class="n">collections</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># 1. Graph Merging & Optimization</span>
<span class="c1"># -------------------------------</span>
<span class="c1"># **Same as in compute**</span>
<span class="n">graph</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">keys</span> <span class="o">=</span> <span class="o">...</span>
<span class="c1"># 2. Computation</span>
<span class="c1"># --------------</span>
<span class="c1"># **Same as in compute**</span>
<span class="n">results</span> <span class="o">=</span> <span class="o">...</span>
<span class="c1"># 3. Postpersist</span>
<span class="c1"># --------------</span>
<span class="n">output</span> <span class="o">=</span> <span class="p">[]</span>
<span class="c1"># Iterate over the results and collections</span>
<span class="k">for</span> <span class="n">res</span><span class="p">,</span> <span class="n">collection</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">results</span><span class="p">,</span> <span class="n">collections</span><span class="p">):</span>
<span class="c1"># res has the same structure as keys</span>
<span class="n">keys</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">__dask_keys__</span><span class="p">()</span>
<span class="c1"># Get the computed graph for this collection.</span>
<span class="c1"># Here flatten converts a nested list into a single list</span>
<span class="n">graph</span> <span class="o">=</span> <span class="p">{</span><span class="n">k</span><span class="p">:</span> <span class="n">r</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">flatten</span><span class="p">(</span><span class="n">keys</span><span class="p">),</span> <span class="n">flatten</span><span class="p">(</span><span class="n">res</span><span class="p">))}</span>
<span class="c1"># Rebuild the output dask collection with the computed graph</span>
<span class="n">rebuild</span><span class="p">,</span> <span class="n">extra_args</span> <span class="o">=</span> <span class="n">collection</span><span class="o">.</span><span class="n">__dask_postpersist__</span><span class="p">()</span>
<span class="n">out</span> <span class="o">=</span> <span class="n">rebuild</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="o">*</span><span class="n">extra_args</span><span class="p">)</span>
<span class="n">output</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">out</span><span class="p">)</span>
<span class="c1"># dask.persist always returns tuples</span>
<span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">output</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="visualize">
<h3>Visualize<a class="headerlink" href="#visualize" title="Permalink to this headline">¶</a></h3>
<p>Visualize is the simplest of the 3 core methods. It only has two stages:</p>
<ol class="arabic">
<li><p class="first"><strong>Graph Merging & Optimization</strong></p>
<p>Same as in <code class="docutils literal"><span class="pre">compute</span></code></p>
</li>
<li><p class="first"><strong>Graph Drawing</strong></p>
<p>The resulting merged graph is drawn using <code class="docutils literal"><span class="pre">graphviz</span></code> and output to the
specified file.</p>
</li>
</ol>
<p>In pseudocode this looks like:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">visualize</span><span class="p">(</span><span class="o">*</span><span class="n">collections</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># 1. Graph Merging & Optimization</span>
<span class="c1"># -------------------------------</span>
<span class="c1"># **Same as in compute**</span>
<span class="n">graph</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">keys</span> <span class="o">=</span> <span class="o">...</span>
<span class="c1"># 2. Graph Drawing</span>
<span class="c1"># ----------------</span>
<span class="c1"># Draw the graph with graphviz's `dot` tool and return the result.</span>
<span class="k">return</span> <span class="n">dot_graph</span><span class="p">(</span><span class="n">graph</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="adding-the-core-dask-methods-to-your-class">
<span id="adding-methods-to-class"></span><h2>Adding the Core Dask Methods to Your Class<a class="headerlink" href="#adding-the-core-dask-methods-to-your-class" title="Permalink to this headline">¶</a></h2>
<p>Defining the above interface will allow your object to used by the core dask
functions (<code class="docutils literal"><span class="pre">dask.compute</span></code>, <code class="docutils literal"><span class="pre">dask.persist</span></code>, <code class="docutils literal"><span class="pre">dask.visualize</span></code>, etc…). To
add corresponding method versions of these subclass from
<code class="docutils literal"><span class="pre">dask.base.DaskMethodsMixin</span></code>, which adds implementations of <code class="docutils literal"><span class="pre">compute</span></code>,
<code class="docutils literal"><span class="pre">persist</span></code>, and <code class="docutils literal"><span class="pre">visualize</span></code> based on the interface above.</p>
</div>
<div class="section" id="example-dask-collection">
<span id="id1"></span><h2>Example Dask Collection<a class="headerlink" href="#example-dask-collection" title="Permalink to this headline">¶</a></h2>
<p>Here we create a dask collection representing a tuple. Every element in the
tuple is represented as a task in the graph. Note that this is for illustration
purposes only - the same user experience could be done using normal tuples with
elements of <code class="docutils literal"><span class="pre">dask.delayed</span></code>.</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="c1"># Saved as dask_tuple.py</span>
<span class="kn">from</span> <span class="nn">dask.base</span> <span class="k">import</span> <span class="n">DaskMethodsMixin</span>
<span class="kn">from</span> <span class="nn">dask.optimize</span> <span class="k">import</span> <span class="n">cull</span>
<span class="c1"># We subclass from DaskMethodsMixin to add common dask methods to our</span>
<span class="c1"># class. This is nice but not necessary for creating a dask collection.</span>
<span class="k">class</span> <span class="nc">Tuple</span><span class="p">(</span><span class="n">DaskMethodsMixin</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">dsk</span><span class="p">,</span> <span class="n">keys</span><span class="p">):</span>
<span class="c1"># The init method takes in a dask graph and a set of keys to use</span>
<span class="c1"># as outputs.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_dsk</span> <span class="o">=</span> <span class="n">dsk</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_keys</span> <span class="o">=</span> <span class="n">keys</span>
<span class="k">def</span> <span class="nf">__dask_graph__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_dsk</span>
<span class="k">def</span> <span class="nf">__dask_keys__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_keys</span>
<span class="nd">@staticmethod</span>
<span class="k">def</span> <span class="nf">__dask_optimize__</span><span class="p">(</span><span class="n">dsk</span><span class="p">,</span> <span class="n">keys</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="c1"># We cull unnecessary tasks here. Note that this isn't necessary,</span>
<span class="c1"># dask will do this automatically, this just shows one optimization</span>
<span class="c1"># you could do.</span>
<span class="n">dsk2</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">cull</span><span class="p">(</span><span class="n">dsk</span><span class="p">,</span> <span class="n">keys</span><span class="p">)</span>
<span class="k">return</span> <span class="n">dsk2</span>
<span class="c1"># Use the threaded scheduler by default.</span>
<span class="n">__dask_scheduler__</span> <span class="o">=</span> <span class="nb">staticmethod</span><span class="p">(</span><span class="n">dask</span><span class="o">.</span><span class="n">threaded</span><span class="o">.</span><span class="n">get</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">__dask_postcompute__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c1"># We want to return the results as a tuple, so our finalize</span>
<span class="c1"># function is `tuple`. There are no extra arguments, so we also</span>
<span class="c1"># return an empty tuple.</span>
<span class="k">return</span> <span class="nb">tuple</span><span class="p">,</span> <span class="p">()</span>
<span class="k">def</span> <span class="nf">__dask_postpersist__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c1"># Since our __init__ takes a graph as its first argument, our</span>
<span class="c1"># rebuild function can just be the class itself. For extra</span>
<span class="c1"># arguments we also return a tuple containing just the keys.</span>
<span class="k">return</span> <span class="n">Tuple</span><span class="p">,</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_keys</span><span class="p">,)</span>
<span class="k">def</span> <span class="nf">__dask_tokenize__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="c1"># For tokenize to work we want to return a value that fully</span>
<span class="c1"># represents this object. In this case it's the list of keys</span>
<span class="c1"># to be computed.</span>
<span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_keys</span><span class="p">)</span>
</pre></div>
</div>
<p>Demonstrating this class:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dask_tuple</span> <span class="k">import</span> <span class="n">Tuple</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">operator</span> <span class="k">import</span> <span class="n">add</span><span class="p">,</span> <span class="n">mul</span>
<span class="go"># Define a dask graph</span>
<span class="gp">>>> </span><span class="n">dsk</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="gp">... </span> <span class="s1">'b'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="gp">... </span> <span class="s1">'c'</span><span class="p">:</span> <span class="p">(</span><span class="n">add</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">),</span>
<span class="gp">... </span> <span class="s1">'d'</span><span class="p">:</span> <span class="p">(</span><span class="n">mul</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="gp">... </span> <span class="s1">'e'</span><span class="p">:</span> <span class="p">(</span><span class="n">add</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">)}</span>
<span class="go"># The output keys for this graph</span>
<span class="gp">>>> </span><span class="n">keys</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'b'</span><span class="p">,</span> <span class="s1">'c'</span><span class="p">,</span> <span class="s1">'d'</span><span class="p">,</span> <span class="s1">'e'</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">Tuple</span><span class="p">(</span><span class="n">dsk</span><span class="p">,</span> <span class="n">keys</span><span class="p">)</span>
<span class="go"># Compute turns Tuple into a tuple</span>
<span class="gp">>>> </span><span class="n">x</span><span class="o">.</span><span class="n">compute</span><span class="p">()</span>
<span class="go">(2, 3, 4, 5)</span>
<span class="go"># Persist turns Tuple into a Tuple, with each task already computed</span>
<span class="gp">>>> </span><span class="n">x2</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">persist</span><span class="p">()</span>
<span class="gp">>>> </span><span class="nb">isinstance</span><span class="p">(</span><span class="n">x2</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">x2</span><span class="o">.</span><span class="n">__dask_graph__</span><span class="p">()</span>
<span class="go">{'b': 2,</span>
<span class="go"> 'c': 3,</span>
<span class="go"> 'd': 4,</span>
<span class="go"> 'e': 5}</span>
<span class="gp">>>> </span><span class="n">x2</span><span class="o">.</span><span class="n">compute</span><span class="p">()</span>
<span class="go">(2, 3, 4, 5)</span>
</pre></div>
</div>
</div>
<div class="section" id="checking-if-an-object-is-a-dask-collection">
<span id="is-dask-collection"></span><h2>Checking if an object is a dask collection<a class="headerlink" href="#checking-if-an-object-is-a-dask-collection" title="Permalink to this headline">¶</a></h2>
<p>To check if an object is a dask collection, use
<code class="docutils literal"><span class="pre">dask.base.is_dask_collection</span></code>:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dask.base</span> <span class="k">import</span> <span class="n">is_dask_collection</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dask</span> <span class="k">import</span> <span class="n">delayed</span>
<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">delayed</span><span class="p">(</span><span class="nb">sum</span><span class="p">)([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">is_dask_collection</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">is_dask_collection</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</div>
<div class="section" id="implementing-deterministic-hashing">
<span id="deterministic-hashing"></span><h2>Implementing Deterministic Hashing<a class="headerlink" href="#implementing-deterministic-hashing" title="Permalink to this headline">¶</a></h2>
<p>Dask implements its own deterministic hash function to generate keys based on
the value of arguments. This function is available as <code class="docutils literal"><span class="pre">dask.base.tokenize</span></code>.
Many common types already have implementations of <code class="docutils literal"><span class="pre">tokenize</span></code>, which can be
found in <code class="docutils literal"><span class="pre">dask/base.py</span></code>.</p>
<p>When creating your own custom classes you may need to register a <code class="docutils literal"><span class="pre">tokenize</span></code>
implementation. There are two ways to do this:</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Both dask collections and normal python objects can have
implementations of <code class="docutils literal"><span class="pre">tokenize</span></code> using either of the methods
described below.</p>
</div>
<ol class="arabic">
<li><p class="first">The <code class="docutils literal"><span class="pre">__dask_tokenize__</span></code> method</p>
<p>Where possible, it’s recommended to define the <code class="docutils literal"><span class="pre">__dask_tokenize__</span></code> method.
This method takes no arguments and should return a value fully
representative of the object.</p>
</li>
<li><p class="first">Register a function with <code class="docutils literal"><span class="pre">dask.base.normalize_token</span></code></p>
<p>If defining a method on the class isn’t possible, you can register a tokenize
function with the <code class="docutils literal"><span class="pre">normalize_token</span></code> dispatch. The function should have the
same signature as described above.</p>
</li>
</ol>
<p>In both cases the implementation should be the same, only the location of the
definition is different.</p>
<div class="section" id="example">
<h3>Example<a class="headerlink" href="#example" title="Permalink to this headline">¶</a></h3>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">dask.base</span> <span class="k">import</span> <span class="n">tokenize</span><span class="p">,</span> <span class="n">normalize_token</span>
<span class="go"># Define a tokenize implementation using a method.</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">Foo</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span> <span class="o">=</span> <span class="n">a</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span> <span class="o">=</span> <span class="n">b</span>
<span class="gp">...</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__dask_tokenize__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span> <span class="c1"># This tuple fully represents self</span>
<span class="gp">... </span> <span class="k">return</span> <span class="p">(</span><span class="n">Foo</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">a</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">b</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">x</span> <span class="o">=</span> <span class="n">Foo</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">tokenize</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="go">'5988362b6e07087db2bc8e7c1c8cc560'</span>
<span class="gp">>>> </span><span class="n">tokenize</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">==</span> <span class="n">tokenize</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c1"># token is deterministic</span>
<span class="go">True</span>
<span class="go"># Register an implementation with normalize_token</span>
<span class="gp">>>> </span><span class="k">class</span> <span class="nc">Bar</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">x</span>
<span class="gp">... </span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>
<span class="gp">>>> </span><span class="nd">@normalize_token</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Bar</span><span class="p">)</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">tokenize_bar</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="p">(</span><span class="n">Bar</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span><span class="o">.</span><span class="n">x</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">y</span> <span class="o">=</span> <span class="n">Bar</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">tokenize</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">'5a7e9c3645aa44cf13d021c14452152e'</span>
<span class="gp">>>> </span><span class="n">tokenize</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">==</span> <span class="n">tokenize</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">tokenize</span><span class="p">(</span><span class="n">y</span><span class="p">)</span> <span class="o">==</span> <span class="n">tokenize</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="c1"># tokens for different objects aren't equal</span>
<span class="go">False</span>
</pre></div>
</div>
<p>For more examples please see <code class="docutils literal"><span class="pre">dask/base.py</span></code> or any of the built-in dask
collections.</p>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="cite.html" class="btn btn-neutral float-right" title="Citations" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="remote-data-services.html" class="btn btn-neutral" title="Remote Data Services" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2017, Anaconda.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'0.16.0',
COLLAPSE_INDEX:false,
FILE_SUFFIX:'.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="file:///usr/share/javascript/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>
|