You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 18KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. # OpenViBE Python
  2. This Project aims to bring modifications to OpenVibe and to widen its data-oriented functionnalities. OpenVibe is a signal processing software allowing the use of machine learning algorithms, however their number is reduced. Thus, we woud like to benefit from the Openvibe Python scripting box (which allows the use of python scripts in OV) in order to allow users to use Scikit-learn Machine Learning algorithms.
  3. ## Dependancies
  4. `pip install pyqt5 pandas numpy natsort pygame sklearn matplotlib pyriemann`
  5. ## What OpenViBE Python allows
  6. - The use of components already implemented in OpenVibe that will allow you to :
  7. - The use of ML algorithms from Scikit-learn and Pyriemann (Library centered on the use of Riemannian geometry) that will allow you to train and store models.
  8. - Visualization of your data in 2D/3D via a PCA or LDA.
  9. - Easily create datasets compatible with OpenVibe's operation
  10. - The use of a manager that allows you to simply :
  11. - Create new boxes in OpenVibe
  12. - Create new types of openvibe settings that can be used in your new boxes.
  13. - The reuse of our scripts in order to simply implement your own python data management
  14. 1. The new boxes
  15. TrainerML / ML Class Boxes
  16. Box ProcessML
  17. Box DataViz
  18. DatasetCreator Box
  19. 2. The Pybox Manager
  20. Box Manager
  21. Stimulations / Labels Manager
  22. Custom Settings Manager
  23. 3. Internal functioning and details
  24. Our box model: PolyBox
  25. Input management with PolyBox, two modes possible
  26. Duplicating the Python Scripting Box
  27. PolyBox: Automatic data storage
  28. Managing Custom Settings
  29. Translated with www.DeepL.com/Translator (free version)
  30. ## Table des matières
  31. - [OpenViBE Python](#openvibe-python)
  32. - [Dependancies](#dependancies)
  33. - [What OpenViBE Python allows](#what-openvibe-python-allows)
  34. - [Table des matières](#table-des-mati%c3%a8res)
  35. - [1. The new boxes](#1-the-new-boxes)
  36. - [TrainerML Class / ML Boxes](#trainerml-class--ml-boxes)
  37. - [Scikit-learn](#scikit-learn)
  38. - [Pyriemann](#pyriemann)
  39. - [Box ProcessML](#box-processml)
  40. - [Box DataViz](#box-dataviz)
  41. - [Box DatasetCreator](#box-datasetcreator)
  42. - [2. The Pybox Manager](#2-the-pybox-manager)
  43. - [Box Manager](#box-manager)
  44. - [Stimulations / Labels Manager](#stimulations--labels-manager)
  45. - [Custom Settings Manager](#custom-settings-manager)
  46. - [3. Internal Functioning and Details](#3-internal-functioning-and-details)
  47. - [Our Box model : PolyBox](#our-box-model--polybox)
  48. - [Input management with PolyBox, two possible modes](#input-management-with-polybox-two-possible-modes)
  49. - [Duplicating the Python Scripting Box](#duplicating-the-python-scripting-box)
  50. - [Automatic data storage](#automatic-data-storage)
  51. - [Managing Custom Settings](#managing-custom-settings)
  52. ## 1. The new boxes
  53. ### TrainerML Class / ML Boxes
  54. The TrainerML class defined in TrainerML.py is a class that inherits from [PolyBox](#our-box-model-polybox), its purpose is to be used in boxes that will be configured to allow the use of certain learning machine algorithms. The following parameters can be passed to it:
  55. - **Filename to save model to** : Path to the file in which to save the model. If no file is indicated, then the model will not be saved.
  56. - **Filename to load model from** : Path to the file in which the model to be loaded is saved. If a file is specified and exists, no model will be created and the model contained in the file will be loaded and used for the current session.
  57. - **classifier** : Algorithm you wish to use. The various Algorithms are from Scikit-learn or Pyriemann.
  58. - **discriminator** : In case the chosen classifier is `TangentSpace`, it is necessary to provide a second algorithm which will be used to classify after the projection on the tangent space. All the previous algorithms can be used except `TangentSpace` and `MDM`. If no algorithm is specified, `LinearDiscriminantAnalysis` will be used by default.
  59. Similarly for `MDM`, a second algorithm may be used, but is not mandatory.
  60. - **labels**: If the read mode is Poly-Mode, you must specify the list of labels as `my label1, my label2, my label3`, otherwise, the labels will be as follows `1, 2, 3, 4, ...`. If the read mode is ov-mode, you can leave the field blank, the labels will be extracted from the stimuli.
  61. - **Test set share**: float between 0 and 1 (1 not included) which represents the proportion of the input dataset transformed into a test set, allowing us to evaluate our model once the training on the set train is done. If 0 is filled in, then all the data will constitute the train set and no metrics will be displayed.
  62. Here are the new algorithms / boxes implemented in OV :
  63. #### Scikit-learn
  64. <https://scikit-learn.org/stable/>
  65. > | Box Name | Algorithm |
  66. > | :-: | :-: |
  67. > | Nearest Centroid | NearestCentroid |
  68. > | Nearest Neighbors Classifier | KNeighborsClassifier |
  69. > | Gaussian Naive Bayes | GaussianNB |
  70. > | Stochastic Gradient Descent | SGDClassifier |
  71. > | Logistic Regression | LogisticRegression |
  72. > | Decision Tree Classifier | DecisionTreeClassifier |
  73. > | Extra Trees | ExtraTreesClassifier |
  74. > | Bagging | BaggingClassifier |
  75. > | Random Forest | RandomForestClassifier |
  76. > | Support Vector Machine | LinearSVC |
  77. > | Linear Discriminant Analysis | LinearDiscriminantAnalysis |
  78. > | AdaBoost | AdaBoostClassifier |
  79. > | Multi Layer Perceptron | MLPClassifier |
  80. > | Linear SVC | LinearSVC |
  81. #### Pyriemann
  82. <https://pyriemann.readthedocs.io/en/latest/index.html>
  83. > | String | Algorithm |
  84. > | :-: | :-: |
  85. > | Riemann Minimum Distance to Mean | MDM |
  86. > | Riemann Tangent Space | TangentSpace |
  87. You can easily find information on each of these methods in the docs of their library.
  88. For each of these classifiers, an Openvibe box using TrainerML with the appropriate parameters has been created. So, if you want to train with the Random Forest algorithm of Scikit-learn for example, you just have to look for the associated box in Openvibe and you will be able to use it directly, and modify the parameters related to the algorithm. You will find the information concerning all these parameters on the respective pages of the algorithms, ex: <https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html.>
  89. > ![random-forest.PNG](Assets/Doc/random-forest.PNG)
  90. >
  91. > Example of parameterization of a box implementing Random Forest
  92. ### Box ProcessML
  93. This box allows you to make classification predictions on new data using a previously trained model (via TrainerML or the boxes that inherit it, e.g. SVM, LDA, RandomForest ...).
  94. - **Model Filename**: Path where the model you want to use is stored.
  95. - **Filename to save predictions** : Path to the file where to save the predictions. If no file is specified, then the predictions will not be saved. The predictions are saved as a string where each prediction is separated from the others by a comma: `pred1,pred2,pred3 ...`.
  96. Once the model is loaded, the predictions will be made on each chunk of data received, they can be used in real time, and if a path is given, they can be saved for later use.
  97. ### Box DataViz
  98. The DataViz Box allows the visualization of our data. To do this, it applies a dimension reduction via an LDA or PCA and then displays our data using the matplotlib library. It inherits from PolyBox. It requires the following parameters:
  99. - **Path to save the model**: Path to the file in which to save the model. If no file is specified, then the model will not be saved.
  100. - **Path to load the model** : Path to the file in which is saved the model you want to load. If a file is specified and exists, then no model will be created and the model contained in the file will be loaded and used for the current session.
  101. - **Algorithm (PCA or LDA)** : Name of the algorithm to be used to reduce dimensions. Accepted values are `PCA` or `LDA`. The default algorithm used is LDA.
  102. - **Dimension reduction**: Number of dimensions to display. The different possible values are 2 and 3. By default, if no number is given or if the field is filled incorrectly, the number of dimension is 2.
  103. - **labels** : List of labels to indicate as `my label1, my label2, my label3` if the read mode is poly-mode. Otherwise, the labels will be in the form `1, 2, 3, 4, ...`.
  104. ### Box DatasetCreator
  105. To facilitate data acquisition during our experiments, we created the python box `DatasetCreator`. This box takes a signal as an input and outputs a `OVTK_StimulationId_ExperimentStop' stimulation when it has finished creating the dataset.
  106. It works in the following way: the user first chooses some labels, then the box will randomly determine an order between them. It will then record the user's brain activity while verbally indicating the current action. In this way, we will be able to create a labeled dataset that can be used for learning.
  107. The recording of an action takes place in the following way:
  108. 1. Audible warning of the beginning of the action.
  109. 2. 2 second wait.
  110. 3. Recording for 10 seconds.
  111. 4. Audible warning of the end of the action.
  112. 5. Wait for 3 seconds.
  113. It can be configured by indicating :
  114. - The path to the directory that will contain the data.
  115. - The number of folds you wish to obtain.
  116. - The number of actions you wish to record. That is to say 30 if you wish to obtain 30 recordings of 10 seconds distributed among the different labels.
  117. - The names of the labels you want to record (/!\ Attention, these names must have a corresponding mp3 file in `pybox-manager/Assets/Sounds/`, you can create new labels with the manager).
  118. - A boolean indicating whether you want several CSVs or only one CSV. If you enter "true", then the data will be split into as many CSVs as there are actions, one CSV per action. If you enter "false", then the data will all be recorded in a single CSV, in which the start of a new recording for a label will be indicated in the stimuli.
  119. ## 2. The Pybox Manager
  120. The Pybox Manager allows you to simply create and incorporate new boxes running a Python script of your choice, new labels/stimulations and new Custom Settings for your python boxes into OpenVibe.
  121. To run it: `python pybox_manager.py`.
  122. (Python 2.7 and python 3.X compatible.)
  123. An option is available to enable the so-called "developer settings" i.e. the ones you created.
  124. To do this, run the manager with the `-mode=developer` option.
  125. ### Box Manager
  126. The Box Manager looks like this:
  127. > ![box_manager.png](Assets/Doc/box_manager.png)
  128. >
  129. > PyBox Manager.
  130. - New: Create a new box
  131. - Duplicate: duplicates the currently selected box.
  132. - Reset Box: resets the selected box, cancels all changes made since the last compilation.
  133. - Category : category in which the box will be stored in OV.
  134. - Author : add the author's name in the box's references.
  135. - Settings: Allows you to manage all the settings necessary to use the box. These can be types from Openvibe (String, Float etc.) or types created by yourself with the [Custom settings Manager](#custom-settings-manager)
  136. - Inputs: Used to fill in the different inputs that your box will receive.
  137. - Outputs : Used to fill in the different outputs that your box will send.
  138. - Enable settings / inputs / outputs modifications : Allows you to prevent the users of your box from modifying these elements later on.
  139. - Mode : allows you to quickly configure the inputs of your box according to the mode you want to use. [Inputs Polybox](#input-management-with-polybox-two-possible-modes)
  140. Once your modifications are finished you can press Build to have the modifications taken into account, and the compilation is done (mandatory to have your modifications and new boxes). The compilation.log file contains information from the last compilation.
  141. Delete Box deletes the currently selected box (a compilation is necessary to take the deletion into account).
  142. ### Stimulations / Labels Manager
  143. When reading and processing our signals, OpenViBE can handle stimulations. These can be used to label our data during a classification/training procedure.
  144. All these stimulations are indicated in the file `pybox-manager/share/PolyStimulations.py`.
  145. The DatasetCreator box we have created allows you to monitor the creation of a labeled dataset for the user. To do this, the box plays a sound during each action, indicating the action the user should think about. Basically, the user will have at his disposal about ten different labels.
  146. We allow the user to add labels (stimulations) via the `Stimulations/Labels Manager` interface present in the manager.
  147. To do this, the manager must provide the name of the label, and a corresponding `.mp3` file, which will be played by the DatasetCreator.
  148. > ![add_label.jpeg](Assets/Doc/add_label.jpeg)
  149. >
  150. > Add a label/stimulation to OpenViBE.
  151. ### Custom Settings Manager
  152. The `Custom Settings Manager` allows you to create or delete special types of settings, as well as possible values for these settings, according to your needs.
  153. This is especially useful to allow easy use on OpenVibe using a drop-down list. For example, if you want to be able to choose one algorithm among several, directly in your OpenVibe box configuration, you just have to create this new type as well as the associated values via the manager as shown in the following illustration.
  154. To be able to use them with the manager when defining the parameters of a box, you have to launch the manager with the option `mode=developer`.
  155. > ![manage_custom_settings.png](Assets/Doc/manage_custom_settings.png)
  156. >
  157. > Add/Remove/Manage Custom Settings.
  158. Result in OpenVibe :
  159. > ![classifiers.png](Assets/Doc/classifiers.png)
  160. >
  161. > Example with a new type 'classifier'.
  162. ## 3. Internal Functioning and Details
  163. ### Our Box model : PolyBox
  164. To facilitate our development phases, we have added in `pybox-manager/share/PolyBox.py` a class called `PolyBox`. This class inherits from the OVBox class. It automates the reception and storage of an input signal and allows the development of simple methods called at key moments:
  165. - **on_initialize(self) :** called at initialization, this method aims at allowing the user to define a particular behavior during initialization.
  166. - **on_header_received(self, header) :** called at each reception of a header, this method aims to allow the user to define a particular behavior when receiving a header.
  167. - **on_chunk_received(self, chunk, label, shape) :** called at each reception of a chunk, this method aims to allow the user to define a particular behavior when receiving a chunk.
  168. - **on_end_box(self) :** called just before the box finishes its work, this method aims to allow the user to define a particular behavior when the box finishes.
  169. We have created this box in order to be able to attribute behaviors adapted to our use cases, in particular to easily create boxes proposing Machine Learning algorithms based on its architecture.
  170. #### Input management with PolyBox, two possible modes
  171. Any box inheriting the PolyBox automatically has two possible read modes to retrieve input data.
  172. The first mode (`ov-mode`) corresponds to the classic OpenViBE reading mode: the different classes are all included in the same .csv file, and we use input stimuli to separate our data into different classes. To use this mode, you just have to give the box only two inputs: 1 StreamedMatrix and 1 Stimulation.
  173. For example:
  174. > ![ov-mode.jpeg](Assets/Doc/ov-mode.jpeg)
  175. >
  176. > **Figure 1** - ov-mode
  177. The second mode (`poly-mode`) consists in considering as many files as there are classes, i.e. one .csv file per class. To read all these files, the box then needs to have at least as many StreamedMatrix inputs as there are different classes. To use this mode, you just have to create only StreamedMatrix inputs.
  178. (Often, this mode requires a `label` parameter in which to fill in the labels of our different classes in the form: `my label1, my label2, my label3`).
  179. For example:
  180. > ![poly-mode.jpeg](Assets/Doc/poly-mode.jpeg)
  181. > **Figure 2** - poly-mode
  182. Thus, these two reading modes are transparent for the user and allow him to operate the `PolyBoxes` either with a single file containing all the actions, or with several files: one per action.
  183. The user doesn't need to indicate anything for the box to choose the right behavior to adopt, the box chooses its behavior according to its inputs.
  184. ### Duplicating the Python Scripting Box
  185. Toutes les modifications ont été faite, on peut maintenant compiler. Toutes ces étapes sont implémentées dans `pybox-manager/ov-manager.py`.
  186. All the modifications we have made to the software are based on the duplication of the Python Scripting box. Since the latter allows the use of a python script, we decided to create a manager that allows us to automatically make changes in the OV code to duplicate the C++ files needed to duplicate the Python Scripting Box. This way, we can definitively associate a script to a box, and let the user configure it and integrate it into OpenViBE.
  187. Each time a python box is created, the manager performs the following tasks:
  188. 1. Go to the root directory of the python boxes: `pybox-manager/src/`.
  189. 2. We duplicate `pybox-manager/Assets/BoxManager/NewBoxPattern-skeletton.h` into `ovpBoxName.h`
  190. 3. Insert the CIdentifier declaration into `src/defines.hpp`.
  191. 4. Add to `src/main.cpp` the imports of the newly created files for the box creation as well as the declarations.
  192. 5. In `src/box-algorithms/ovpBoxName.h`, replace the box name and includes/declarations.
  193. 6. Change the box name and description in `src/box-algorithms/ovpBoxName.h`.
  194. 7. Set the path of the script to be executed in `src/box-algorithms/ovpBoxName.h`.
  195. 8. Remove the possibility to change the path.
  196. 9. Add the parameters of our box to `src/box-algorithms/ovpBoxName.h`.
  197. 10. Add the box's inputs and outputs to `src/box-algorithms/ovpBoxName.h`.
  198. All changes have been made, we can now compile. All these steps are implemented in `pybox-manager/ov-manager.py`.
  199. ### Automatic data storage
  200. Automatically, the box stores all received chunk in `self.data`. This can be handy if the user wants to use all the data at the end of the box (to train a learning machine model for example).
  201. However, it is possible to prevent this behavior voluntarily. To do this, simply give as a parameter when creating the PolyBox `record=False`. By default, record is set to True.
  202. ### Managing Custom Settings
  203. We add to `meta/sdk/toolkit/include/toolkit/ovtk_defines.hpp` the declaration of our new Custom Settings and to `meta/sdk/toolkit/src/ovtk_main.cpp` the creation of the custom setting and the declaration of its different values.