Skip to content

Commit

Permalink
Merge c0f92a5 into 1f7398c
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff1evesque committed Nov 14, 2017
2 parents 1f7398c + c0f92a5 commit 593c4be
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 11 deletions.
11 changes: 10 additions & 1 deletion brain/session/model_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,16 @@ def generate_model(self):
self.list_error,
random_state=rand
)

if model_type == 'bagr':
rand = current_app.config.get('RANDOM_STATE')
rand = None if rand == 'None' else rand
result = baggen(
model_type,
self.collection,
payload,
self.list_error,
random_state=rand
)
# store any errors
if result and result['error']:
self.list_error.extend(result['error'])
Expand Down
21 changes: 13 additions & 8 deletions brain/session/predict/bag.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ def predict(model, collection, predictors):
decision_function = None
collection_adjusted = collection.lower().replace(' ', '_')
list_model_type = current_app.config.get('MODEL_TYPE')
rs = [list_model_type[3],
list_model_type[5],
list_model_type[7],
list_model_type[9]]
cs = [list_model_type[2],
list_model_type[4],
list_model_type[6],
list_model_type[8]]
rs = [
list_model_type[3],
list_model_type[5],
list_model_type[7],
list_model_type[9]
]
cs = [
list_model_type[2],
list_model_type[4],
list_model_type[6],
list_model_type[8]
]

clf = Model().uncache(
model + '_model',
Expand Down Expand Up @@ -50,6 +54,7 @@ def predict(model, collection, predictors):
},
'error': None
}

elif model in rs:
r2 = Hset().uncache(
model + '_r2',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"properties": {
"collection": "bagr-1",
"session_type": "model_predict",
"prediction_input[]": [
"22.22",
"96.24",
"338",
"72.55",
"0.001",
"28",
"0.678"
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"properties": {
"collection": "bgr-2",
"collection": "bagr-2",
"dataset_type": "file_upload",
"session_type": "data_append",
"model_type": "bagr",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"properties": {
"session_name": "sample_bgr_title",
"collection": "bgr-2",
"collection": "bagr-2",
"dataset_type": "file_upload",
"session_type": "data_new",
"model_type": "bagr",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"properties": {
"collection": "bagr-2",
"session_type": "model_predict",
"prediction_input[]": [
"22.22",
"96.24",
"338",
"72.55",
"0.001",
"28",
"0.678"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
'''
This file will test the following bagr sessions:
- data_new: stores supplied dataset into a SQL database.
- data_append: appends supplied dataset to an already stored dataset in an
SQL database.
- model_generate: generate an model by selecting a particular range of
dataset (session), and store it into a NoSQL cache.
- model_predict: generate a prediction by selecting a particular cached
model from the NoSQL cache.
Note: the 'pytest' instances can further be reviewed:
- https://pytest-flask.readthedocs.io/en/latest
- http://docs.pytest.org/en/latest/usage.html
'''

import json
import os.path
from flask import current_app, url_for


def get_sample_json(jsonfile, model_type):
'''
Get a sample json dataset.
'''

# local variables
root = current_app.config.get('ROOT')

# open file
json_dataset = None

with open(
os.path.join(
root,
'interface',
'static',
'data',
'json',
'programmatic_interface',
model_type,
'dataset_url',
jsonfile
),
'r'
) as json_file:
json_dataset = json.load(json_file)

return json.dumps(json_dataset)


def test_data_new(client, live_server):
'''
This method tests the 'data_new' session.
'''

@live_server.app.route('/load-data')
def get_endpoint():
return url_for('name.load_data', _external=True)

live_server.start()

res = client.post(
get_endpoint(),
headers={'Content-Type': 'application/json'},
data=get_sample_json('bagr-data-new.json', 'bagr')
)

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0


def test_data_append(client, live_server):
'''
This method tests the 'data_new' session.
'''

@live_server.app.route('/load-data')
def get_endpoint():
return url_for('name.load_data', _external=True)

live_server.start()

res = client.post(
get_endpoint(),
headers={'Content-Type': 'application/json'},
data=get_sample_json('bagr-data-append.json', 'bagr')
)

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0


def test_model_generate(client, live_server):
'''
This method tests the 'model_generate' session.
'''

@live_server.app.route('/load-data')
def get_endpoint():
return url_for('name.load_data', _external=True)

live_server.start()

res = client.post(
get_endpoint(),
headers={'Content-Type': 'application/json'},
data=get_sample_json('bagr-model-generate.json', 'bagr')
)

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0


def test_model_predict(client, live_server):
'''
This method tests the 'model_predict' session.
Note: for debugging, the following syntax will output the corresponding
json values, nested within 'json.loads()', to the travis ci:
raise ValueError(res.json['result']['key1'])
'''

@live_server.app.route('/load-data')
def get_endpoint():
return url_for('name.load_data', _external=True)

live_server.start()

res = client.post(
get_endpoint(),
headers={'Content-Type': 'application/json'},
data=get_sample_json('bagr-model-predict.json', 'bagr')
)

# assertion checks
assert res.status_code == 200
assert res.json['status'] == 0
assert res.json['result']
assert res.json['result']['confidence']
assert res.json['result']['confidence']['score'] == '0.96282488659958954'
assert res.json['result']['model'] == 'bagr'
assert res.json['result']['result'] == '33.4442'

0 comments on commit 593c4be

Please sign in to comment.