Skip to content

Commit

Permalink
Add missing documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
tshead committed Dec 3, 2020
1 parent 382f4c5 commit ce40f36
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 12 deletions.
7 changes: 7 additions & 0 deletions docs/graphcat.common.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
graphcat.common module
======================

.. automodule:: graphcat.common
:members:
:undoc-members:
:show-inheritance:
7 changes: 7 additions & 0 deletions docs/graphcat.dynamic.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
graphcat.dynamic module
=======================

.. automodule:: graphcat.dynamic
:members:
:undoc-members:
:show-inheritance:
1 change: 0 additions & 1 deletion docs/graphcat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ graphcat module
:members:
:undoc-members:
:show-inheritance:
:imported-members:
7 changes: 7 additions & 0 deletions docs/graphcat.static.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
graphcat.static module
======================

.. automodule:: graphcat.static
:members:
:undoc-members:
:show-inheritance:
16 changes: 8 additions & 8 deletions docs/image-processing-case-study.ipynb

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ Contents:
:maxdepth: 2

graphcat.rst
graphcat.common.rst
graphcat.dynamic.rst
graphcat.notebook.rst
graphcat.static.rst
109 changes: 106 additions & 3 deletions graphcat/dynamic.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,7 @@ def _update(self, name):
if task["state"] != graphcat.common.TaskState.FINISHED:
try:
# Get the task inputs.
edges = self._graph.out_edges(name, data="input")
inputs = NamedInputs(self, edges)
inputs = NamedInputs(self, name)

# Execute the function and store the output.
self._on_execute.send(self, name=name, inputs=inputs)
Expand Down Expand Up @@ -588,21 +587,58 @@ def update(self, name):


class NamedInputs(object):
def __init__(self, graph, edges):
"""Access named inputs for a graph task.
Parameters
----------
graph: :class:`DynamicGraph`, required
Graph containing a task.
name: hashable object, required
Existing task unique name.
"""
def __init__(self, graph, name):
if not isinstance(graph, DynamicGraph):
raise ValueError("Graph input must be an instance of DynamicGraph")

edges = graph._graph.out_edges(name, data="input")
self._keys = [input for target, source, input in edges]
self._values = [functools.partial(graph._output, source) for target, source, input in edges]

def __contains__(self, name):
"""Return :any:`True` if `name` matches a named input for this task."""
return name in self._keys

def __len__(self):
"""Return the number of named inputs for this task."""
return len(self._keys)

def __repr__(self):
inputs = ", ".join([repr(key) for key in self._keys])
return f"{{{inputs}}}"

def get(self, name, default=None):
"""Return a single input value.
Use this method to return a value when you expect to have either zero
or one input that matches `name`.
Parameters
----------
name: hashable object, required
Name of the input value to return.
default: any Python value, optional
If an input matching `name` doesn't exist, this value will be
returned instead. Defaults to :any:`None`.
Returns
-------
value: any Python value
The value of input `name`, or `default`.
Raises
------
:class:`KeyError`: if more than one input matches `name`.
"""
values = [value for key, value in zip(self._keys, self._values) if key == name]
if len(values) == 0:
return default
Expand All @@ -612,9 +648,43 @@ def get(self, name, default=None):
raise KeyError(f"More than one input {name!r}")

def getall(self, name):
"""Return multiple input values.
Use this method to return every input value that matches `name`.
Parameters
----------
name: hashable object, required
Name of the input value to return.
Returns
-------
values: list of Python values
Values from every input that matches `name`. Returns an empty list
if there are none.
"""
return [value() for key, value in zip(self._keys, self._values) if key == name]

def getone(self, name):
"""Return a single input value.
Use this method to return a value when you expect to have exactly one
input that matches `name`.
Parameters
----------
name: hashable object, required
Name of the input value to return.
Returns
-------
value: any Python value
The value of input `name`.
Raises
------
:class:`KeyError`: if more or less than one input matches `name`.
"""
values = [value for key, value in zip(self._keys, self._values) if key == name]
if len(values) == 0:
raise KeyError(name)
Expand All @@ -624,12 +694,45 @@ def getone(self, name):
raise KeyError(f"More than one input {name!r}")

def items(self):
"""Return names and values for every input attached to this task.
Note
----
For each (name, value) pair returned by this method, the value is a
callable that returns the actual value from the upstream task.
Returns
-------
values: sequence of (hashable object, callable) tuples
The name and value of every input attached to this task.
"""
return zip(self._keys, self._values)

def keys(self):
"""Return names for every input attached to this task.
Returns
-------
names: sequence of hashable objects
The name of every input attached to this task. Note that the same
name may appear more than once in the sequence.
"""
return self._keys

def values(self):
"""Return values for every input attached to this task.
Note
----
Each value returned by this method is a callable that returns the
actual value from the upstream task.
Returns
-------
values: sequence of callables
The value of every input attached to this task, in the same
order as :meth:`keys`.
"""
return self._values


100 changes: 100 additions & 0 deletions graphcat/static.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,15 @@ def update(self, name):


class NamedInputs(object):
"""Access named inputs for a graph task.
Parameters
----------
graph: :class:`StaticGraph`, required
Graph containing a task.
name: hashable object, required
Existing task unique name.
"""
def __init__(self, graph, name):
def constant(value):
def implementation():
Expand All @@ -607,6 +616,7 @@ def implementation():
self._values = [constant(graph._graph.nodes[source]["output"]) for target, source, input in edges]

def __contains__(self, name):
"""Return :any:`True` if `name` matches a named input for this task."""
return name in self._keys

def __getitem__(self, key): # pragma: no cover
Expand All @@ -615,13 +625,36 @@ def __getitem__(self, key): # pragma: no cover
return self.getall(key)

def __len__(self):
"""Return the number of named inputs for this task."""
return len(self._keys)

def __repr__(self):
inputs = ", ".join([f"{key}: {value()}" for key, value in zip(self._keys, self._values)])
return f"{{{inputs}}}"

def get(self, name, default=None):
"""Return a single input value.
Use this method to return a value when you expect to have either zero
or one input that matches `name`.
Parameters
----------
name: hashable object, required
Name of the input value to return.
default: any Python value, optional
If an input matching `name` doesn't exist, this value will be
returned instead. Defaults to :any:`None`.
Returns
-------
value: any Python value
The value of input `name`, or `default`.
Raises
------
:class:`KeyError`: if more than one input matches `name`.
"""
values = [value for key, value in zip(self._keys, self._values) if key == name]
if len(values) == 0:
return default
Expand All @@ -631,9 +664,43 @@ def get(self, name, default=None):
raise KeyError(f"More than one input {name!r}")

def getall(self, name):
"""Return multiple input values.
Use this method to return every input value that matches `name`.
Parameters
----------
name: hashable object, required
Name of the input value to return.
Returns
-------
values: list of Python values
Values from every input that matches `name`. Returns an empty list
if there are none.
"""
return [value() for key, value in zip(self._keys, self._values) if key == name]

def getone(self, name):
"""Return a single input value.
Use this method to return a value when you expect to have exactly one
input that matches `name`.
Parameters
----------
name: hashable object, required
Name of the input value to return.
Returns
-------
value: any Python value
The value of input `name`.
Raises
------
:class:`KeyError`: if more or less than one input matches `name`.
"""
values = [value for key, value in zip(self._keys, self._values) if key == name]
if len(values) == 0:
raise KeyError(name)
Expand All @@ -643,12 +710,45 @@ def getone(self, name):
raise KeyError(f"More than one input {name!r}")

def items(self):
"""Return names and values for every input attached to this task.
Note
----
For each (name, value) pair returned by this method, the value is a
callable that returns the actual value from the upstream task.
Returns
-------
values: sequence of (hashable object, callable) tuples
The name and value of every input attached to this task.
"""
return zip(self._keys, self._values)

def keys(self):
"""Return names for every input attached to this task.
Returns
-------
names: sequence of hashable objects
The name of every input attached to this task. Note that the same
name may appear more than once in the sequence.
"""
return self._keys

def values(self):
"""Return values for every input attached to this task.
Note
----
Each value returned by this method is a callable that returns the
actual value from the upstream task.
Returns
-------
values: sequence of callables
The value of every input attached to this task, in the same
order as :meth:`keys`.
"""
return self._values


0 comments on commit ce40f36

Please sign in to comment.