Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagepy
import json
import requests


# Setupsetup environment
environment = {}
with open("params.json") as params_file:
    environment = json.loads(params_file.read())
  
# Downloaddownload required regions and properties
response = requests.get(
    'http://localhost:8002/region/%s/list?cadence=%s&time_start=between(%s,%s)'
    % (
        environment['algorithm']['dataset'],
        environment['algorithm']['cadence'],
        environment['algorithm']['time_start'],
        environment['algorithm']['time_end']
    )
).json()
if response['has_error'] == True or response['result-count'] == 0:
    raise ValueError('No data found!')
else:
    # FIXME: Bad Implementation
    # if we already have an algorithm configuration stored within the database,
    # we do not need to extract any traintraining notnor validation data.
    (train_data, validation_data, test_data) = createDataPartitionscreate_data_partitions(response['data'])
  
# Setupsetup model
model = MyMLAlgorithmDemoMLAlgorithm(envirnomentenvironment['algorithm']['params'])
  
# Checkcheck wherever we have to train our algorithm or if we can download an already existing configuration
response = requests.get(
    'http://localhost:8004/algoconfig/list?algorithm_config_name=%s&algorithm_config_version=%s'
    % (environment['algorithm']['cfg_name'], 'latest')
).json()
if response['has_error'] == False and response['result-count'] > 0:
    # as we requested the latest configuration we expect only one result within 'data'
    algo_cfg = response['data'][0]['config_data']
    model.set_parameters(algo_cfg)
else:
    train_model(
        model, train_data, validation_data,
        envirnomentenvironment['algorithm']['max_epoches'], envirnomentenvironment['algorithm']['batch_size'],
        environment
    )

# run algorithm
prediction = test_model(model, test_data, environment)
prediction_ids = [get_fc_id(row) for row in prediction]

# check wherever the new prediction was successfully stored within the database
response = requests.get(
    'http://localhost:8004/prediction/list?prediction_fc_id=eq(%s)'
    % (','.join(prediction_ids))
).json()
if response['has_error'] == True or response['result-count'] == 0:
    raise AssertionError('No predictions found!')
else:
    for prediction in response['data']:
        print(prediction)

Source Code

Here you can download the full python source code.

...