Skip to content

Commit

Permalink
Update to new adapter API.
Browse files Browse the repository at this point in the history
  • Loading branch information
orenbenkiki committed Jun 11, 2024
1 parent fdff258 commit 014979d
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 82 deletions.
41 changes: 19 additions & 22 deletions daf/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
from typing import Optional

from .copies import EmptyData
from .data import DafReadOnly
from .copies import copy_all
from .data import DafWriter
from .formats import MemoryDaf
from .julia_import import _jl_pairs
from .julia_import import jl
from .formats import chain_writer
from .views import ViewAxes
from .views import ViewData
from .views import viewer

__all__ = [
"adapter",
Expand All @@ -33,30 +33,27 @@

@contextmanager
def adapter(
view: DafWriter | DafReadOnly,
name: Optional[str] = None,
dset: DafWriter,
*,
input_axes: Optional[ViewAxes] = None,
input_data: Optional[ViewData] = None,
capture: Callable[..., DafWriter] = MemoryDaf,
axes: Optional[ViewAxes] = None,
data: Optional[ViewData] = None,
output_axes: Optional[ViewAxes] = None,
output_data: Optional[ViewData] = None,
empty: Optional[EmptyData] = None,
relayout: bool = True,
overwrite: bool = False,
) -> Iterator[DafWriter]:
"""
Invoke a computation on a ``view`` data set; copy a ``viewer`` of the updated data set into the base ``Daf`` data
set of the view. See the Julia
Invoke a computation on a view of some ``dset`` and return the result; copy a view of the results into the
base ``dset``. See the Julia
`documentation <https://tanaylab.github.io/Daf.jl/v0.1.0/adapters.html#Daf.Adapters.adapter>`__ for details.
"""
writer = capture(name=jl.Daf.Adapters.get_adapter_capture_name(view, name=name))
adapted = jl.Daf.Adapters.get_adapter_input(view, name=name, writer=writer)
yield DafWriter(adapted)
jl.Daf.Adapters.copy_adapter_output(
view,
adapted,
name=name,
axes=axes,
data=jl._pairify_data(_jl_pairs(data)),
empty=empty,
relayout=relayout,
overwrite=overwrite,
)
base_name = dset.name
input_dset = viewer(dset, axes=input_axes, data=input_data, name=f"{base_name}.input")
captured_dset = capture(name=f"{base_name}.capture")
adapted_dset = chain_writer([input_dset, captured_dset], name=f"{base_name}.adapted")
result = yield adapted_dset
output_dset = viewer(adapted_dset, axes=output_axes, data=output_data, name=f"{base_name}.output")
copy_all(source=output_dset, destination=dset, empty=empty, relayout=relayout, overwrite=overwrite)
return result
2 changes: 1 addition & 1 deletion daf/julia_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
jl_version = (jl.VERSION.major, jl.VERSION.minor, jl.VERSION.patch)

jl.seval("using Pkg")
jl.seval("Pkg.update()")
# jl.seval("Pkg.update()")

jl.seval("using Daf")

Expand Down
41 changes: 19 additions & 22 deletions docs/v0.1.0/html/_modules/daf/adapters.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ <h1>Source code for daf.adapters</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="n">Optional</span>

<span class="kn">from</span> <span class="nn">.copies</span> <span class="kn">import</span> <span class="n">EmptyData</span>
<span class="kn">from</span> <span class="nn">.data</span> <span class="kn">import</span> <span class="n">DafReadOnly</span>
<span class="kn">from</span> <span class="nn">.copies</span> <span class="kn">import</span> <span class="n">copy_all</span>
<span class="kn">from</span> <span class="nn">.data</span> <span class="kn">import</span> <span class="n">DafWriter</span>
<span class="kn">from</span> <span class="nn">.formats</span> <span class="kn">import</span> <span class="n">MemoryDaf</span>
<span class="kn">from</span> <span class="nn">.julia_import</span> <span class="kn">import</span> <span class="n">_jl_pairs</span>
<span class="kn">from</span> <span class="nn">.julia_import</span> <span class="kn">import</span> <span class="n">jl</span>
<span class="kn">from</span> <span class="nn">.formats</span> <span class="kn">import</span> <span class="n">chain_writer</span>
<span class="kn">from</span> <span class="nn">.views</span> <span class="kn">import</span> <span class="n">ViewAxes</span>
<span class="kn">from</span> <span class="nn">.views</span> <span class="kn">import</span> <span class="n">ViewData</span>
<span class="kn">from</span> <span class="nn">.views</span> <span class="kn">import</span> <span class="n">viewer</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span>
<span class="s2">&quot;adapter&quot;</span><span class="p">,</span>
Expand All @@ -114,33 +114,30 @@ <h1>Source code for daf.adapters</h1><div class="highlight"><pre>
<a class="viewcode-back" href="../../adapters.html#daf.adapters.adapter">[docs]</a>
<span class="nd">@contextmanager</span>
<span class="k">def</span> <span class="nf">adapter</span><span class="p">(</span>
<span class="n">view</span><span class="p">:</span> <span class="n">DafWriter</span> <span class="o">|</span> <span class="n">DafReadOnly</span><span class="p">,</span>
<span class="n">name</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">dset</span><span class="p">:</span> <span class="n">DafWriter</span><span class="p">,</span>
<span class="o">*</span><span class="p">,</span>
<span class="n">input_axes</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ViewAxes</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">input_data</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ViewData</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">capture</span><span class="p">:</span> <span class="n">Callable</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">DafWriter</span><span class="p">]</span> <span class="o">=</span> <span class="n">MemoryDaf</span><span class="p">,</span>
<span class="n">axes</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ViewAxes</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">data</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ViewData</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">output_axes</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ViewAxes</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">output_data</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">ViewData</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">empty</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="n">EmptyData</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span><span class="p">,</span>
<span class="n">relayout</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span>
<span class="n">overwrite</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">,</span>
<span class="p">)</span> <span class="o">-&gt;</span> <span class="n">Iterator</span><span class="p">[</span><span class="n">DafWriter</span><span class="p">]:</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Invoke a computation on a ``view`` data set; copy a ``viewer`` of the updated data set into the base ``Daf`` data</span>
<span class="sd"> set of the view. See the Julia</span>
<span class="sd"> Invoke a computation on a view of some ``dset`` and return the result; copy a view of the results into the</span>
<span class="sd"> base ``dset``. See the Julia</span>
<span class="sd"> `documentation &lt;https://tanaylab.github.io/Daf.jl/v0.1.0/adapters.html#Daf.Adapters.adapter&gt;`__ for details.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">writer</span> <span class="o">=</span> <span class="n">capture</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">jl</span><span class="o">.</span><span class="n">Daf</span><span class="o">.</span><span class="n">Adapters</span><span class="o">.</span><span class="n">get_adapter_capture_name</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">))</span>
<span class="n">adapted</span> <span class="o">=</span> <span class="n">jl</span><span class="o">.</span><span class="n">Daf</span><span class="o">.</span><span class="n">Adapters</span><span class="o">.</span><span class="n">get_adapter_input</span><span class="p">(</span><span class="n">view</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span> <span class="n">writer</span><span class="o">=</span><span class="n">writer</span><span class="p">)</span>
<span class="k">yield</span> <span class="n">DafWriter</span><span class="p">(</span><span class="n">adapted</span><span class="p">)</span>
<span class="n">jl</span><span class="o">.</span><span class="n">Daf</span><span class="o">.</span><span class="n">Adapters</span><span class="o">.</span><span class="n">copy_adapter_output</span><span class="p">(</span>
<span class="n">view</span><span class="p">,</span>
<span class="n">adapted</span><span class="p">,</span>
<span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">,</span>
<span class="n">axes</span><span class="o">=</span><span class="n">axes</span><span class="p">,</span>
<span class="n">data</span><span class="o">=</span><span class="n">jl</span><span class="o">.</span><span class="n">_pairify_data</span><span class="p">(</span><span class="n">_jl_pairs</span><span class="p">(</span><span class="n">data</span><span class="p">)),</span>
<span class="n">empty</span><span class="o">=</span><span class="n">empty</span><span class="p">,</span>
<span class="n">relayout</span><span class="o">=</span><span class="n">relayout</span><span class="p">,</span>
<span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">,</span>
<span class="p">)</span></div>
<span class="n">base_name</span> <span class="o">=</span> <span class="n">dset</span><span class="o">.</span><span class="n">name</span>
<span class="n">input_dset</span> <span class="o">=</span> <span class="n">viewer</span><span class="p">(</span><span class="n">dset</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="n">input_axes</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">input_data</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">base_name</span><span class="si">}</span><span class="s2">.input&quot;</span><span class="p">)</span>
<span class="n">captured_dset</span> <span class="o">=</span> <span class="n">capture</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">base_name</span><span class="si">}</span><span class="s2">.capture&quot;</span><span class="p">)</span>
<span class="n">adapted_dset</span> <span class="o">=</span> <span class="n">chain_writer</span><span class="p">([</span><span class="n">input_dset</span><span class="p">,</span> <span class="n">captured_dset</span><span class="p">],</span> <span class="n">name</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">base_name</span><span class="si">}</span><span class="s2">.adapted&quot;</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="k">yield</span> <span class="n">adapted_dset</span>
<span class="n">output_dset</span> <span class="o">=</span> <span class="n">viewer</span><span class="p">(</span><span class="n">adapted_dset</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="n">output_axes</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="n">output_data</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="sa">f</span><span class="s2">&quot;</span><span class="si">{</span><span class="n">base_name</span><span class="si">}</span><span class="s2">.output&quot;</span><span class="p">)</span>
<span class="n">copy_all</span><span class="p">(</span><span class="n">source</span><span class="o">=</span><span class="n">output_dset</span><span class="p">,</span> <span class="n">destination</span><span class="o">=</span><span class="n">dset</span><span class="p">,</span> <span class="n">empty</span><span class="o">=</span><span class="n">empty</span><span class="p">,</span> <span class="n">relayout</span><span class="o">=</span><span class="n">relayout</span><span class="p">,</span> <span class="n">overwrite</span><span class="o">=</span><span class="n">overwrite</span><span class="p">)</span>
<span class="k">return</span> <span class="n">result</span></div>

</pre></div>

Expand Down
2 changes: 1 addition & 1 deletion docs/v0.1.0/html/_modules/daf/julia_import.html
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ <h1>Source code for daf.julia_import</h1><div class="highlight"><pre>
<span class="n">jl_version</span> <span class="o">=</span> <span class="p">(</span><span class="n">jl</span><span class="o">.</span><span class="n">VERSION</span><span class="o">.</span><span class="n">major</span><span class="p">,</span> <span class="n">jl</span><span class="o">.</span><span class="n">VERSION</span><span class="o">.</span><span class="n">minor</span><span class="p">,</span> <span class="n">jl</span><span class="o">.</span><span class="n">VERSION</span><span class="o">.</span><span class="n">patch</span><span class="p">)</span>

<span class="n">jl</span><span class="o">.</span><span class="n">seval</span><span class="p">(</span><span class="s2">&quot;using Pkg&quot;</span><span class="p">)</span>
<span class="n">jl</span><span class="o">.</span><span class="n">seval</span><span class="p">(</span><span class="s2">&quot;Pkg.update()&quot;</span><span class="p">)</span>
<span class="c1"># jl.seval(&quot;Pkg.update()&quot;)</span>

<span class="n">jl</span><span class="o">.</span><span class="n">seval</span><span class="p">(</span><span class="s2">&quot;using Daf&quot;</span><span class="p">)</span>

Expand Down
96 changes: 80 additions & 16 deletions docs/v0.1.0/html/adapters.html
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,88 @@ <h1>adapters
<span class="sig-paren">(
</span>
<em class="sig-param">
<span class="pre">view:
<span class="pre">dset:
</span>
<span class="pre">~daf.data.DafWriter
<span class="pre">~daf.data.DafWriter,
</span>
<span class="pre">*,
</span>
<span class="pre">input_axes:
</span>
<span class="pre">~typing.Mapping[str,
</span>
<span class="pre">str
</span>
<span class="pre">|
</span>
<span class="pre">~daf.queries.Axis
</span>
<span class="pre">|
</span>
<span class="pre">~daf.queries.Lookup
</span>
<span class="pre">|
</span>
<span class="pre">~daf.queries.Names
</span>
<span class="pre">|
</span>
<span class="pre">~daf.data.DafReadOnly,
<span class="pre">~daf.operations.QuerySequence
</span>
<span class="pre">|
</span>
<span class="pre">None]
</span>
<span class="pre">|
</span>
<span class="pre">None
</span>
<span class="pre">name:
<span class="pre">=
</span>
<span class="pre">None,
</span>
<span class="pre">input_data:
</span>
<span class="pre">~typing.Mapping[str
</span>
<span class="pre">|
</span>
<span class="pre">~typing.Tuple[str,
</span>
<span class="pre">str]
</span>
<span class="pre">|
</span>
<span class="pre">~typing.Tuple[str,
</span>
<span class="pre">str,
</span>
<span class="pre">str],
</span>
<span class="pre">str
</span>
<span class="pre">|
</span>
<span class="pre">~daf.queries.Axis
</span>
<span class="pre">|
</span>
<span class="pre">~daf.queries.Lookup
</span>
<span class="pre">|
</span>
<span class="pre">~daf.queries.Names
</span>
<span class="pre">|
</span>
<span class="pre">~daf.operations.QuerySequence
</span>
<span class="pre">|
</span>
<span class="pre">None]
</span>
<span class="pre">|
</span>
<span class="pre">None
</span>
<span class="pre">=
Expand All @@ -336,7 +404,7 @@ <h1>adapters
</span>
<span class="pre">'daf.formats.MemoryDaf'&gt;,
</span>
<span class="pre">axes:
<span class="pre">output_axes:
</span>
<span class="pre">~typing.Mapping[str,
</span>
Expand Down Expand Up @@ -370,7 +438,7 @@ <h1>adapters
</span>
<span class="pre">None,
</span>
<span class="pre">data:
<span class="pre">output_data:
</span>
<span class="pre">~typing.Mapping[str
</span>
Expand Down Expand Up @@ -548,20 +616,16 @@ <h1>adapters
</dt>

<dd>
<p>Invoke a computation on a
<p>Invoke a computation on a view of some
<code class="docutils literal notranslate">
<span class="pre">view
<span class="pre">dset
</span>
</code> data set; copy a
</code> and return the result; copy a view of the results into the
base
<code class="docutils literal notranslate">
<span class="pre">viewer
<span class="pre">dset
</span>
</code> of the updated data set into the base
<code class="docutils literal notranslate">
<span class="pre">Daf
</span>
</code> data
set of the view. See the Julia
</code>. See the Julia

<a class="reference external" href="https://tanaylab.github.io/Daf.jl/v0.1.0/adapters.html#Daf.Adapters.adapter">documentation
</a> for details.
Expand Down
2 changes: 1 addition & 1 deletion docs/v0.1.0/html/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
def test_adapters() -> None: # pylint: disable=too-many-statements
dset = MemoryDaf(name="memory!")
dset.set_scalar("INPUT", 1)
with adapter(viewer(dset, data={"input": ": INPUT"}), data={"OUTPUT": ": output"}) as adapted:
with adapter(dset, input_data={"input": ": INPUT"}, output_data={"OUTPUT": ": output"}) as adapted:
adapted.set_scalar("output", adapted.get_scalar("input"))
assert dset.get_scalar("OUTPUT") == 1
Loading

0 comments on commit 014979d

Please sign in to comment.