WebSockets

Overview

Description

This describe the Websocket interface into Algorithms.io's systems.

Websocket URI:

 ws://ws.algorithms.io

We are using a clustered Node.js and Socket.io for the websocket interface. There is good support for websockets clients in various languages which you can find lots of examples here.

Examples

Javascript

Using Javascript to make a websocket connection and then a prediction using the random forest algorithm. This will connect to http://ws.algorithms.io and then switch to the websockets protocol. Once the connection is established it will send a request to run the algorithm. The results will be place in the div with the id "results".

Remember to place your "authToken" in the call.

 <script src="http://ws.algorithms.io/socket.io/socket.io.js"></script>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
 <script>
   var socket = io.connect('http://ws.algorithms.io');

   // Sending client data through the websocket
   $(document).ready(function(){
       socket.emit('algorithm_random_forest', {
                  'authToken': '<AUTH_TOKEN>',
                  'train': '1234',
                  'test': '{"Accelerometer.X":-5.6,"Gyroscope.Y":-132.2}', 
                  'dependentVariable': 'action'
                  });
   });

   socket.on('algorithm_random_forest_result', function(data){
         $('#results').text(data.data);
       });

 </script>

 <div id="results"></div>

Here is a live example of all the calls working: All Calls

Methods

Event Data

Save

Save an event or a data point to Algorithms.io. This is the generic saving method where the "data" is in your own pre-defined json method.

Parameter(s) Description Type Example Value Required
authToken Your auth_token string abce1234 yes
device_id A user defined device id string dev_1234 yes
data Data you want to save. Data has to be in a flat json structure containing no arrays json {"a":1.2,"b":"b1"} yes
label Lable for this data point string running yes
 socket.emit('event_save', {
           'authToken': '<AUTH_TOKEN>',
           'device_id': '<USER_DEFINED_ID>',
           'data': '<DATA>'
           'label': '<STRING>'
 });

Emit Event name: event_save_result

Save Motion

Save an event or a data point to Algorithms.io. This saving method saves specifically motion sensor specific values such as accelerometer, gyroscope, and rotation.

Saving motion data with this method is the preferable way. It allows you to use this Random Forest algorithm to predict movement which we have achieved a 99% prediction accuracy rate.

Parameter(s) Description Type Example Value Required
authToken Your auth_token string abce1234 yes
device_id A user defined device id string dev_1234 yes
accelerometer_x accelerometer_x number -5.6 yes
accelerometer_y accelerometer_y number -2.5 yes
accelerometer_z accelerometer_z number 3.7 yes
gyroscope_x gyroscope_x number -84.2 yes
gyroscope_y gyroscope_y number -132.4 yes
gyroscope_z gyroscope_z number -80 yes
rotation_x rotation_x number -5.6 yes
rotation_y rotation_y number -5.6 yes
rotation_z rotation_z number -5.6 yes
label Lable for this data point string running yes
 socket.emit('event_save_motion', {
           'authToken': '<AUTH_TOKEN>',
           'device_id': '<USER_DEFINED_ID>',
           'accelerometer_x': <NUMBER>,
           'accelerometer_y': <NUMBER>,
           'accelerometer_z': <NUMBER>,
           'gyroscope_x': <NUMBER>,
           'gyroscope_y': <NUMBER>,
           'gyroscope_z': <NUMBER>,
           'rotation_x': <NUMBER>,
           'rotation_y': <NUMBER>,
           'rotation_z': <NUMBER>,
           'label': '<STRING>'
 });

Emit Event name: event_save_motion_result

Training

To use any of the algorithms a training set has to be created for it. After creating a training set, an id will be returned which is used when calling the algorithms. Creating the training set is an easy process that is done through our web gui.

Algorithms

Random Forest

Parameter(s) Description Type Example Value Required
authToken Your auth token string abcde1234 yes
train Training set to use number 1234 yes
test Test data json {"Accelerometer.X":-5.6,"Gyroscope.Y":-132.2} yes
dependentVariable Dependent Variable in the training set string action yes
 socket.emit('algorithm_random_forest', {
           'authToken': '<AUTH_TOKEN>',
           'train': '1234',
           'test': '{"Accelerometer.X":-5.6,"Gyroscope.Y":-132.2}',
           'dependentVariable': 'action'
 });

Emit Event name: algorithm_random_forest_result

Random Forest with Rolling Samples For Motion Prediction

This random forest differs from the other Random Forest only by the input data that you supply it. For this method, the call to run the algorithm does not accept values for motion data. Instead it will use the data that the sensor has been passing in for the last second. It will retrieve the standard deviation and avgerage for the x, y, z for the accelerometer, gyroscope, and rotation data in the last second and pass that into the algorithm for you. By using this technique we have found that you can get a 99% accuracy rate on prediction.

If you are preforming motion related prediction this should be your preferred method to use.

Parameter(s) Description Type Example Value Required
authToken Your auth token string abcde1234 yes
train Training set to use number 1234 yes
dependentVariable Dependent Variable in the training set string action yes
 socket.emit('algorithm_random_forest_rolling_samples', {
           'authToken': '<AUTH_TOKEN>',
           'train': '1234',
           'dependentVariable': 'action'
 });

Emit Event name: algorithm_random_forest_rolling_samples_result

SVM

Parameter(s) Description Type Example Value Required
authToken Your auth token string abcde1234 yes
train Training set to use number 1234 yes
test Test data json {"Accelerometer.X":-5.6,"Gyroscope.Y":-132.2} yes
dependentVariable Dependent Variable in the training set string action yes
 socket.emit('algorithm_svm', {
           'authToken': '<AUTH_TOKEN>',
           'train': '{"Accelerometer.X":-5.6,"Gyroscope.Y":-132.2}',
           'dependentVariable': 'item'
 });

Emit Event name: algorithm_svm_result

Querying

Get Data By Time Range

Parameter(s) Description Type Example Value Required
authToken Your auth_token string abce1234 yes
device_id A user defined device id string dev_1234 yes
start_time start time datetime 082113T18:15 yes
end_time end time datetime 082113T18:20 yes
 socket.emit('query_get_data_by_range', {
           'authToken': '<AUTH_TOKEN>',
           'device_id': '<USER_DEFINED_ID>',
           'start_time': '<TIME>',
           'end_time': '<TIME>'
 });

Emit Event name: query_get_data_by_range

Statistics

Total EPS Per Device

This will return a continuous stream of total events that were sent to the system in the last second by this authtoken and deviceid.

Parameter(s) Description Type Example Value Required
authToken Your auth_token string abce1234 yes
device_id A user defined device id string dev_1234 yes
 socket.emit('statistic_eps_stat_device', {
           'authToken': '<AUTH_TOKEN>',
           'device_id': '<USER_DEFINED_ID>'
 });

Emit Event name: statistic_eps_stat_device

Total EPS Per Auth Token

This will return a continuous stream of total events that were sent to the system in the last second by this auth_token. It will be an aggregate of the EPS for all devices.

Parameter(s) Description Type Example Value Required
authToken Your auth_token string abce1234 yes
 socket.emit('statistic_eps_stat_auth_token', {
           'authToken': '<AUTH_TOKEN>'
 });

Emit Event name: statistic_eps_stat_auth_token

Get Rolling Avg and Standard Deviation

Retrieve a value of the last second's rolling average and standard deviations values for a device_id.

Parameter(s) Description Type Example Value Required
authToken Your auth_token string abce1234 yes
device_id A user defined device id string dev_1234 yes
 socket.emit('statistic_get_rolling_avg_and_std', {
           'authToken': '<AUTH_TOKEN>',
           'device_id': '<USER_DEFINED_ID>'
 });

Emit Event name: statistic_get_rolling_avg_and_std