intro.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. .. _intro:
  2. 1. Introduction
  3. ===============
  4. The fundamental abstractions in ThingFlow are 1) *sensors*, which provide a means
  5. to sample a changing value representing some quanity in the physical world, 2)
  6. *event streams*, which are
  7. push-based sequences of sensor data readings, and 3) *things*, which are
  8. reusable components to generate, transform, or consume the events on these
  9. streams. Things can have simple, stateless logic (e.g. filter events based
  10. on a predicate) or implement more complex, stateful algorithms, such as
  11. Kalman filters or machine learning. Using ThingFlow, you describe the flow of
  12. data through these things rather than programming low-level behaviors.
  13. Although ThingFlow presents a simple dataflow model to the user, internally it
  14. uses an event-driven programming model, building on
  15. Python's ``asyncio`` module. In addition to being a natural programming model for
  16. realtime sensor data, it reduces the potential resource consumption of Ant
  17. Events programs. The details of event scheduling are handled by the framework.
  18. Separate threads may be used on the "edges" of a dataflow, where elements
  19. frequently interact with external components that have blocking APIs.
  20. ThingFlow integrates with standard Python
  21. data analytics frameworks, including NumPy_, Pandas_, and scikit-learn_. This
  22. allows dataflows involving complex elements to be developed and refined offline
  23. and then deployed in an IoT environment using the same code base.
  24. We call the implementation described here "ThingFlow-Python", as it should be
  25. possible to port the ideas of ThingFlow to other languages. Currently, one such
  26. port exists: "ThingFlow-MicroPython". This is a port of ThingFlow to MicroPython,
  27. a limited version of Python 3 that runs "bare metal" on embadded devices. The
  28. ThingFlow-MicroPython port is included in the ThingFlow-Python repository
  29. under the subdirectory ``micropython``. It is documented in
  30. :ref:`Section 7 <micropython_port>` of this document.
  31. .. _NumPy: http://www.numpy.org/
  32. .. _Pandas: http://pandas.pydata.org/
  33. .. _scikit-learn: http://scikit-learn.org/stable/
  34. Example
  35. -------
  36. To give the flavor of ThingFlow, below is a short code snippet for the
  37. Raspberry Pi that reads a light sensor and then turns on an LED if the running
  38. average of the last five readings is greater than some threshold:
  39. .. code-block:: python
  40. lux = SensorAsOutputThing(LuxSensor())
  41. lux.map(lambda e: e.val).running_avg(5).map(lambda v: v > THRESHOLD)\
  42. .GpioPinOut()
  43. scheduler.schedule_periodic(lux, 60.0)
  44. scheduler.run_forever()
  45. The first line instantiates a light sensor object and wraps it in an *output thing*
  46. to handle sampling and progagation of events.
  47. The next two lines
  48. create a pipeline of things to process the data from the sensor. We call things
  49. which have a single input and output *filters*, as they can be composed to process
  50. a stream of events.
  51. The ``map`` filter extracts the data value from the sensor event, the
  52. ``running_avg`` filter averages the last five values, and the next ``map`` filter converts
  53. the value to a a boolean based on the threshold. The ``GpioPinOut`` thing is
  54. an *adapter* to the outside world. It turns on the LED based on the value of
  55. its input boolean value.
  56. Finally, the last two lines of the example schedule the sensor to be sampled
  57. at a sixty second interval and then start the scheduler's main loop.
  58. Platforms
  59. ---------
  60. ThingFlow does not have any required external dependendencies, so, in theory
  61. at least, it can be run just about anywhere you can run Python 3. It has been
  62. tested on the Raspberry Pi (Rasbian distribution), Desktop Linux, and MacOSX.
  63. In a desktop environment, you might find the
  64. Anaconda_ Python distribution helpful, as it comes with many data analytics
  65. tools (e.g. Jupyter, NumPy, Pandas, and scikit-learn) pre-installed.
  66. ThingFlow has been ported to Micropython_, so that it can run on very small
  67. devices, like the ESP8266_. Since these devices have stringent memory
  68. requirements, the code base has been stripped down to a core for the
  69. Micropython port. The port is in this repository under the ``micropython``
  70. directory.
  71. .. _Micropython: http://www.micropython.org
  72. .. _ESP8266: http://docs.micropython.org/en/latest/esp8266/esp8266/quickref.html
  73. .. _Anaconda: https://docs.continuum.io/anaconda/index
  74. Installing ThingFlow
  75. ---------------------
  76. We recommend installing into a ``virtualenv`` rather than directly into the
  77. system's Python. To do so, first run the ``activate`` script of your chosen
  78. virtual environment. Next, you can either install from the Python Package
  79. Index website (pypi.python.org) or from the ThingFlow source
  80. tree.
  81. Installing from the Python Package Index
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. The package name of thingflow-python on PyPi is ``thingflow``. You
  84. can use the ``pip`` utility to install, as follows::
  85. pip3 install thingflow
  86. If you have activated your virtual environment, this should pick up the
  87. version of ``pip3`` associated with your environment, and install ThingFlow
  88. into your environment rather than into the system's Python install.
  89. Installing from the source tree
  90. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91. Go to the ``thingflow-python`` directory and then run::
  92. python3 setup.py install
  93. If you have activated your virtual environment, this should pick up the
  94. version of ``python3`` associated with your environment, and install ThingFlow
  95. into your environment rather than into the system's Python install.
  96. Using ThingFlow without installation
  97. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  98. You can also run the ThingFlow code in-place from the git repository by adding
  99. the full path to the ``thingflow-python`` directory to your ``PYTHONPATH``. This
  100. is how the tests and the examples are run.
  101. Directory Layout
  102. ----------------
  103. The layout of the files in the ThingFlow code repository (the ``thingflow-python``
  104. directory) is as follows:
  105. + ``README.RST`` - a short introduction and pointer to resources
  106. + ``Makefile`` - builds the source distribution and documentation; can run the tests
  107. + ``setup.py`` - used to install the core code into a python environment
  108. + ``thingflow/`` - the core code. This is all that will get installed in a
  109. production system
  110. + ``thingflow/base.py`` - the core definitions and base classes of thingflow
  111. + ``thingflow/adapters`` - reader and writer things that talk to the outside world
  112. + ``thingflow/filters`` - elements for filter pipelines, in the style of
  113. Microsoft's Linq_ framework
  114. + ``docs/`` - this documentation, build using Sphinx
  115. + ``tests/`` - the tests. These can be run in-place.
  116. + ``examples/`` - examples and other documentation.
  117. + ``micropython/`` - port of the ThingFlow core to MicroPython
  118. .. _Linq: https://en.wikipedia.org/wiki/Language_Integrated_Query
  119. More Examples
  120. -------------
  121. Additional can be found throughout this document, particularly in
  122. :ref:`Section 6: More Examples <more-examples>`.
  123. There is also a separate repository with larger ThingFlow applications. It is at
  124. https://github.com/mpi-sws-rse/thingflow-examples. This repository includes an automated
  125. lighting application and a vehicle traffic analysis.
  126. Next, we will go into more detail on ThingFlow with a :ref:`tutorial <tutorial>`.