This short tutorial shows how to store data into the property service with python and requests (see also Access to REST-Services in Python).

For IDL you can take a similar rout by adapting these instructions to Access to REST-Services in IDL.

The Prediction Service

The prediction service represents a web interface which allows to request, insert and modify prediction data from the database. Operations are performed by sending URL requests, whereas each operation is well defined as a so called route. The property service comes along with a graphical user interface at http://localhost:8004/ui/ which provides visual access to all available routes. Hereby, all routes involving the insertion or modification of data are enlisted under the Edit section.

For this tutorial we are using only two routes. One to add a new machine learning configuration and one to add the consequential prediction.

Each route can hold up to three different parameter types which are described in details by the following article: "Ingest property data in database (REST API)".

Implementation

Ingest

In our example we run a machine learning algorithm which produces a flare prediction to store within our database. Hereby, the algorithm consists of a training phase and a test or prediction phase. Within the training phase the algorithm learns and tunes its parameters which then can be stored within the database as a configuration for later use. Afterwards, within the test phase, we use this configuration to compute flare predictions which are also stored within the database. The following code shows the two corresponding functions.

def train_model(model, train_data, validation_data, max_epoches, batch_size, environment):
    # train model (e.g. until max_epoches are reached or validation loss increases)
	model.train(train_data, validation_data, max_epoches, batch_size)
	# store model parameters within database
    post_data = {
        "algorithm_run_id": environment.r_id,
        "config_data": model.get_parameters(),
        "description": {},
    }
    response = requests.post('http://localhost:8004/algoconfig/%s' % cfg_name, data=post_data)
    if response['has_erro'] == True:
        raise ValueError('An error occured while storing the algorithm's configuration:\n%s' % response['error'])
    return response['data']

def test_model(model, test_data, environment):
    # test model (e.g. predict the test_data)
    model.run(test_data)
    (time_start, position_hg, prediction_data) = model.get_prediction()
	# store predictions within database
    post_data = [
        {
            "algorithm_config": environment['algorithm']['cfg_name'],
            "algorithm_run_id": environment['runtime']['run_id'],
            "lat_hg": position_hg[0],
            "long_hg": position_hg[1],
            "prediction_data": prediction_data,
            "source_data": [get_fc_id(row) for row in test_data],
            "time_start": time_start
        }
    ]
    response = requests.post('http://localhost:8004/prediction/bulk', data=post_data)
    if response['has_erro'] == True:
        raise ValueError('An error occured while storing the algorithm's prediction:\n%s' % response['error'])
    return response['data']

The post_data structure is equivalent to the the algorithm_config_data or prediction_data definitions as given by the routes /algoconfig/{name} and /prediction/bulk:

  • Every algorithm_configuration_data needs at least an algorithm_run_id and
    config_data attribute.
  • Every specific configuration value has to be added to the config_data attribute and has
    to be a key-value pair.
  • Every prediction_data needs at least an algorithm_run_id, prediction_data, source_data and
    time_start attribute.
  • Every specific prediction value has to be added to the prediction_data attribute and has to be
    a key-value pair.

Source Code

Here you can download the above code fragments as python code.

ingest_prediction_data.py

A more complete example using the above functions is given in the article 'Request prediction data from database (REST API)'.

 

This page was adopted from Ingest property data in database (REST API).