Skip to content

Commit

Permalink
deploy: 3ef31af
Browse files Browse the repository at this point in the history
  • Loading branch information
Karl5766 committed Nov 29, 2024
1 parent 0966335 commit 29ebadf
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions GettingStarted/result_caching.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ <h2>CachePointer<a class="headerlink" href="#cachepointer" title="Permalink to t
<span class="n">result</span> <span class="o">=</span> <span class="p">(</span><span class="n">im</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">*</span> <span class="mi">3</span>
<span class="n">cache_path</span> <span class="o">=</span> <span class="n">cptr</span><span class="o">.</span><span class="n">subpath</span><span class="p">()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">cache_path</span><span class="o">.</span><span class="n">exists</span><span class="p">:</span>
<span class="n">result</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">)</span>
<span class="k">return</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">)</span>
<span class="n">result</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
<span class="k">return</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>

<span class="c1"># implementation 2 (functionally equivalent but creates two sub-directories)</span>
<span class="k">def</span> <span class="nf">compute</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">cptr</span><span class="p">):</span>
Expand Down
18 changes: 9 additions & 9 deletions GettingStarted/segmentation_pipeline.html
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,15 @@ <h2>Extending the Pipeline<a class="headerlink" href="#extending-the-pipeline" t
closed. If provided, this cache file will persist. Within the <code class="code docutils literal notranslate"><span class="pre">forward()</span></code> method, you should use
<code class="code docutils literal notranslate"><span class="pre">self.tmpdir.cache()</span></code> and <code class="code docutils literal notranslate"><span class="pre">self.tmpdir.cache_im()</span></code> to create cache files:</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ExampleSegProcess</span><span class="p">(</span><span class="n">SegProcess</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">im</span><span class="p">,</span> <span class="n">cptr</span><span class="p">:</span> <span class="n">CachePointer</span><span class="p">,</span> <span class="n">viewer</span><span class="p">:</span> <span class="n">napari</span><span class="o">.</span><span class="n">Viewer</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">im</span><span class="p">,</span> <span class="n">cptr</span><span class="p">:</span> <span class="n">CachePointer</span><span class="p">,</span> <span class="n">viewer</span><span class="p">:</span> <span class="n">napari</span><span class="o">.</span><span class="n">Viewer</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="n">cache_path</span> <span class="o">=</span> <span class="n">cptr</span><span class="o">.</span><span class="n">subpath</span><span class="p">()</span>

<span class="c1"># in the case cache does not exists, cache_path.abs_path is an empty path we can create a folder in:</span>
<span class="c1"># in the case cache does not exists, cache_path.url is an empty path we can create a folder in:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">cache_path</span><span class="o">.</span><span class="n">exists</span><span class="p">:</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">)</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">compute_result</span><span class="p">(</span><span class="n">im</span><span class="p">)</span>
<span class="n">save</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">)</span>
<span class="n">save</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
<span class="k">return</span> <span class="n">result</span>
</pre></div>
</div>
Expand All @@ -208,11 +208,11 @@ <h2>Extending the Pipeline<a class="headerlink" href="#extending-the-pipeline" t
should use <code class="code docutils literal notranslate"><span class="pre">viewer.add_labels()</span></code>, <code class="code docutils literal notranslate"><span class="pre">lc_interpretable_napari()</span></code> or <code class="code docutils literal notranslate"><span class="pre">temp_directory.cache_im()</span></code>
while passing in <code class="code docutils literal notranslate"><span class="pre">viewer_args</span></code> argument to display your results:</p>
<div class="highlight-Python notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ExampleSegProcess</span><span class="p">(</span><span class="n">SegProcess</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">im</span><span class="p">,</span> <span class="n">cptr</span><span class="p">:</span> <span class="n">CachePointer</span><span class="p">,</span> <span class="n">viewer_args</span><span class="p">:</span> <span class="nb">dict</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="k">async</span> <span class="k">def</span> <span class="nf">forward</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">im</span><span class="p">,</span> <span class="n">cptr</span><span class="p">:</span> <span class="n">CachePointer</span><span class="p">,</span> <span class="n">viewer_args</span><span class="p">:</span> <span class="nb">dict</span> <span class="o">=</span> <span class="kc">None</span><span class="p">):</span>
<span class="k">if</span> <span class="n">viewer_args</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
<span class="n">viewer_args</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">compute_result</span><span class="p">(</span><span class="n">im</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">cptr</span><span class="o">.</span><span class="n">im</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="n">result</span><span class="p">,</span> <span class="n">viewer_args</span><span class="o">=</span><span class="n">viewer_args</span><span class="p">)</span> <span class="c1"># caching result at location pointed by cptr</span>
<span class="n">result</span> <span class="o">=</span> <span class="k">await</span> <span class="n">cptr</span><span class="o">.</span><span class="n">im</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="n">result</span><span class="p">,</span> <span class="n">viewer_args</span><span class="o">=</span><span class="n">viewer_args</span><span class="p">)</span> <span class="c1"># caching result at location pointed by cptr</span>
<span class="k">return</span> <span class="n">result</span>
<span class="c1"># ...</span>
<span class="n">viewer</span> <span class="o">=</span> <span class="n">napari</span><span class="o">.</span><span class="n">Viewer</span><span class="p">(</span><span class="n">ndisplay</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
Expand All @@ -223,7 +223,7 @@ <h2>Extending the Pipeline<a class="headerlink" href="#extending-the-pipeline" t
<span class="n">multiscale</span><span class="o">=</span><span class="mi">4</span> <span class="k">if</span> <span class="n">viewer</span> <span class="k">else</span> <span class="mi">0</span><span class="p">,</span> <span class="c1"># maximum downsampling level of ome zarr files, necessary for very large images</span>
<span class="p">)</span>
<span class="n">process</span> <span class="o">=</span> <span class="n">ExampleSegProcess</span><span class="p">()</span>
<span class="n">process</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">cptr</span> <span class="o">=</span> <span class="n">root_dir</span><span class="o">.</span><span class="n">cache</span><span class="p">(</span><span class="n">cid</span><span class="o">=</span><span class="s1">&#39;compute&#39;</span><span class="p">),</span> <span class="n">viewer_args</span><span class="o">=</span><span class="n">viewer_args</span><span class="p">)</span>
<span class="k">await</span> <span class="n">process</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">cptr</span> <span class="o">=</span> <span class="n">root_dir</span><span class="o">.</span><span class="n">cache</span><span class="p">(</span><span class="n">cid</span><span class="o">=</span><span class="s1">&#39;compute&#39;</span><span class="p">),</span> <span class="n">viewer_args</span><span class="o">=</span><span class="n">viewer_args</span><span class="p">)</span>
</pre></div>
</div>
<p><code class="code docutils literal notranslate"><span class="pre">viewer_args</span></code> is a parameter that allows us to visualize the saved results as part of the caching
Expand Down Expand Up @@ -258,7 +258,7 @@ <h2>Running the Pipeline<a class="headerlink" href="#running-the-pipeline" title
<span class="n">process</span><span class="o">.</span><span class="n">set_tmpdir</span><span class="p">(</span><span class="n">temp_directory</span><span class="p">)</span>
<span class="n">viewer</span> <span class="o">=</span> <span class="n">napari</span><span class="o">.</span><span class="n">Viewer</span><span class="p">()</span>
<span class="n">viewer_args</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">viewer</span><span class="o">=</span><span class="n">viewer</span><span class="p">)</span>
<span class="n">process</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">cid</span><span class="o">=</span><span class="s1">&#39;cell_count_cache&#39;</span><span class="p">,</span> <span class="n">viewer_args</span><span class="o">=</span><span class="n">viewer_args</span><span class="p">)</span>
<span class="k">await</span> <span class="n">process</span><span class="o">.</span><span class="n">forward</span><span class="p">(</span><span class="n">im</span><span class="p">,</span> <span class="n">cid</span><span class="o">=</span><span class="s1">&#39;cell_count_cache&#39;</span><span class="p">,</span> <span class="n">viewer_args</span><span class="o">=</span><span class="n">viewer_args</span><span class="p">)</span>

<span class="n">client</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">viewer</span><span class="o">.</span><span class="n">show</span><span class="p">(</span><span class="n">block</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
Expand Down
12 changes: 6 additions & 6 deletions GettingStarted/setting_up_the_script.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,22 @@ <h2>CacheDirectory<a class="headerlink" href="#cachedirectory" title="Permalink
<span class="c1"># Use case #1. Create a data directory for caching computation results</span>
<span class="n">cache_path</span> <span class="o">=</span> <span class="n">temp_directory</span><span class="o">.</span><span class="n">cache_subpath</span><span class="p">(</span><span class="n">cid</span><span class="o">=</span><span class="s1">&#39;some_cache_path&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">cache_path</span><span class="o">.</span><span class="n">exists</span><span class="p">:</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">,</span> <span class="n">exists_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># PUT CODE HERE: Now write your data into cache_path.abs_path and load it back later</span>
<span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="n">exists_ok</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
<span class="c1"># PUT CODE HERE: Now write your data into cache_path.url and load it back later</span>

<span class="c1"># Use case #2. Create a sub-directory and pass it to other processes for caching</span>
<span class="k">def</span> <span class="nf">multi_step_computation</span><span class="p">(</span><span class="n">cache_at</span><span class="p">:</span> <span class="n">imfs</span><span class="o">.</span><span class="n">CacheDirectory</span><span class="p">):</span>
<span class="n">cache_path</span> <span class="o">=</span> <span class="n">cache_at</span><span class="o">.</span><span class="n">cache_subpath</span><span class="p">(</span><span class="n">cid</span><span class="o">=</span><span class="s1">&#39;A&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">cache_path</span><span class="o">.</span><span class="n">exists</span><span class="p">:</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">computeA</span><span class="p">()</span>
<span class="n">save</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">,</span> <span class="n">A</span><span class="p">)</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">abs_path</span><span class="p">)</span>
<span class="n">save</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="n">A</span><span class="p">)</span>
<span class="n">A</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>

<span class="n">cache_path_B</span> <span class="o">=</span> <span class="n">cache_at</span><span class="o">.</span><span class="n">cache_subpath</span><span class="p">(</span><span class="n">cid</span><span class="o">=</span><span class="s1">&#39;B&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">cache_path_B</span><span class="o">.</span><span class="n">exists</span><span class="p">:</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">computeBFromA</span><span class="p">()</span>
<span class="n">save</span><span class="p">(</span><span class="n">cache_path_B</span><span class="o">.</span><span class="n">abs_path</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path_B</span><span class="o">.</span><span class="n">abs_path</span><span class="p">)</span>
<span class="n">save</span><span class="p">(</span><span class="n">cache_path_B</span><span class="o">.</span><span class="n">url</span><span class="p">,</span> <span class="n">B</span><span class="p">)</span>
<span class="n">B</span> <span class="o">=</span> <span class="n">load</span><span class="p">(</span><span class="n">cache_path_B</span><span class="o">.</span><span class="n">url</span><span class="p">)</span>
<span class="k">return</span> <span class="n">B</span>

<span class="n">sub_temp_directory</span> <span class="o">=</span> <span class="n">temp_directory</span><span class="o">.</span><span class="n">cache_subdir</span><span class="p">(</span><span class="n">cid</span><span class="o">=</span><span class="s1">&#39;mult_step_cache&#39;</span><span class="p">)</span>
Expand Down
4 changes: 2 additions & 2 deletions _sources/GettingStarted/result_caching.rst.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ function where the caller does not need to care whether you want a leaf node or
result = (im + 1) * 3
cache_path = cptr.subpath()
if not cache_path.exists:
result.save(cache_path.abs_path)
return load(cache_path.abs_path)
result.save(cache_path.url)
return load(cache_path.url)
# implementation 2 (functionally equivalent but creates two sub-directories)
def compute(im, cptr):
Expand Down
Loading

0 comments on commit 29ebadf

Please sign in to comment.