Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
finam
Manage
Activity
Members
Labels
Plan
Issues
31
Issue boards
Milestones
Code
Merge requests
3
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FINAM
finam
Commits
c070e2cf
Commit
c070e2cf
authored
2 years ago
by
Martin Lange
Browse files
Options
Downloads
Patches
Plain Diff
add tests for chapter on connect phase
parent
480d462b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!127
Doc-tests
Pipeline
#123365
passed with stages
in 4 minutes and 17 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
docs/source/finam-book/development/connect_phase.rst
+111
-17
111 additions, 17 deletions
docs/source/finam-book/development/connect_phase.rst
with
111 additions
and
17 deletions
docs/source/finam-book/development/connect_phase.rst
+
111
−
17
View file @
c070e2cf
...
...
@@ -136,20 +136,62 @@ Simple case - no dependencies
In the most simple case, all metadata is known in :meth:`.Component._initialze`, and data is pushed in :attr:`.Component._connect()`:
.. code-block:: Python
.. testcode:: simple-connect
import finam as fm
class SimpleConnect(TimeComponent):
class SimpleConnect(fm.TimeComponent):
def __init__(self, time):
super().__init__()
self.time = time
def _initialize(self):
self.inputs.add(name="A", grid=NoGrid(), units="m")
self.inputs.add(name="B", grid=NoGrid(), units="m")
self.outputs.add(name="Area", grid=NoGrid(), units="m2")
self.inputs.add(name="A",
time=self.time,
grid=
fm.
NoGrid(), units="m")
self.inputs.add(name="B",
time=self.time,
grid=
fm.
NoGrid(), units="m")
self.outputs.add(name="Area",
time=self.time,
grid=
fm.
NoGrid(), units="m2")
self.create_connector()
def _connect(self):
push_data = {"Area": 0}
self.try_connect(time=self.time, push_data=push_data)
self.try_connect(push_data=push_data)
def _validate(self):
pass
# etc...
.. testcode:: simple-connect
:hide:
from datetime import datetime, timedelta
generator = fm.modules.CallbackGenerator(
{
"Output1": (lambda t: t.day, fm.Info(time=None, grid=fm.NoGrid())),
"Output2": (lambda t: t.day, fm.Info(time=None, grid=fm.NoGrid())),
},
start=datetime(2000, 1, 1),
step=timedelta(days=30),
)
simple_conn = SimpleConnect(datetime(2000, 1, 1))
consumer = fm.modules.DebugConsumer(
{"Input": fm.Info(None, grid=fm.NoGrid())},
start=datetime(2000, 1, 1),
step=timedelta(days=1),
)
comp = fm.Composition([generator, simple_conn, consumer])
comp.initialize()
generator.outputs["Output1"] >> simple_conn.inputs["A"]
generator.outputs["Output2"] >> simple_conn.inputs["B"]
simple_conn.outputs["Area"] >> consumer.inputs["Input"]
comp.connect()
In :meth:`.Component._initialize`, we create inputs and outputs with metadata (here ``grid`` and ``units``).
Then, we create the connector with ``self.create_connector()``. No arguments required here, as there are no dependencies.
...
...
@@ -163,13 +205,19 @@ In this example, we want to get a grid specification from an input.
This grid specification should then be used for the metadata of the output,
and the initial data should be generated from it.
..
code-block:: Python
..
testcode:: complex-connect
class ComplexConnect(TimeComponent):
import finam as fm
class ComplexConnect(fm.TimeComponent):
def __init__(self, time):
super().__init__()
self.time = time
def _initialize(self):
self.inputs.add(name="A", grid=None, units="m")
self.inputs.add(name="B", grid=NoGrid(), units="m")
self.inputs.add(name="A",
time=self.time,
grid=None, units="m")
self.inputs.add(name="B",
time=self.time,
grid=
fm.
NoGrid(), units="m")
self.outputs.add(name="Area")
self.create_connector()
...
...
@@ -188,6 +236,45 @@ and the initial data should be generated from it.
push_infos=push_infos,
push_data=push_data)
def _validate(self):
pass
# etc...
.. testcode:: complex-connect
:hide:
from datetime import datetime, timedelta
def _generate_data(info):
return 0
generator = fm.modules.CallbackGenerator(
{
"Output1": (lambda t: t.day, fm.Info(time=None, grid=fm.NoGrid())),
"Output2": (lambda t: t.day, fm.Info(time=None, grid=fm.NoGrid())),
},
start=datetime(2000, 1, 1),
step=timedelta(days=30),
)
complex_conn = ComplexConnect(datetime(2000, 1, 1))
consumer = fm.modules.DebugConsumer(
{"Input": fm.Info(None, grid=fm.NoGrid())},
start=datetime(2000, 1, 1),
step=timedelta(days=1),
)
comp = fm.Composition([generator, complex_conn, consumer])
comp.initialize()
generator.outputs["Output1"] >> complex_conn.inputs["A"]
generator.outputs["Output2"] >> complex_conn.inputs["B"]
complex_conn.outputs["Area"] >> consumer.inputs["Input"]
comp.connect()
In :meth:`.Component._initialize`, we set the ``grid`` of input ``"A"`` to ``None``.
It will be filled from the connected output, and becomes available in
:attr:`connector.in_infos <.ConnectHelper.in_infos>` after successful exchange.
...
...
@@ -215,20 +302,27 @@ The process works in both directions.
Any metadata field that is initialized with ``None`` will be filled with the value from the other end of the connection.
This can happen in the initialization of inputs and outputs:
.. code-block:: Python
.. testsetup:: no-metadata
from finam import Component, Info, NoGrid
from datetime import datetime
self = Component()
.. testcode:: no-metadata
self.inputs.add(name="A", grid=None, units=None)
self.outputs.add(name="Area", grid=NoGrid(), units=None)
self.inputs.add(name="A",
time=None,
grid=None, units=None)
self.outputs.add(name="Area",
time=None,
grid=NoGrid(), units=None)
Here, ``grid`` and ``units`` of the input would be filled from a connected output.
For the output, ``units`` would be filled from a connected input.
Here,
``time``,
``grid`` and ``units`` of the input would be filled from a connected output.
For the output,
``time`` and
``units`` would be filled from a connected input.
The same mechanism can also be applied in :meth:`.Component._connect`:
.. code-block:: Python
info = Info(grid=None, units="m")
self.try_connect(
time=self.time, in
_infos={"A": info})
info = Info(
time=None,
grid=None, units="m")
self.try_connect(
exchange
_infos={"A": info})
Summary metadata initialization
-------------------------------
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment