|
############################################################################## |
|
# Copyright (c) 2015-2018 BigML, Inc |
|
# |
|
# Permission is hereby granted, free of charge, to any person obtaining |
|
# a copy of this software and associated documentation files (the |
|
# "Software"), to deal in the Software without restriction, including |
|
# without limitation the rights to use, copy, modify, merge, publish, |
|
# distribute, sublicense, and/or sell copies of the Software, and to |
|
# permit persons to whom the Software is furnished to do so, subject to |
|
# the following conditions: |
|
# |
|
# The above copyright notice and this permission notice shall be |
|
# included in all copies or substantial portions of the Software. |
|
# |
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
|
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
|
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
############################################################################## |
|
|
|
|
|
#!/usr/bin/env python |
|
# -*- coding: utf-8 -* |
|
|
|
import argparse |
|
import sys |
|
import json |
|
|
|
|
|
from bigml.api import BigML |
|
|
|
SUMMARY = ("Creates a batch prediction from a 1-click dataset for a fusion.") |
|
|
|
|
|
OPTIONS = { |
|
# fusion ID to be used |
|
'--fusion': { |
|
'required': 'True', |
|
'action': 'store', |
|
'dest': 'fusion', |
|
'help': "fusion/id"}, |
|
|
|
# delimiter in the file |
|
'--test-source': { |
|
'action': 'store', |
|
'dest': 'test_source', |
|
'default': ',', |
|
'help': "Path to the test file."} |
|
} |
|
|
|
|
|
|
|
def parser_add_options(parser, options): |
|
"""Adds the options to the parser |
|
""" |
|
for option, properties in sorted(options.items(), key=lambda x: x[0]): |
|
parser.add_argument(option, **properties) |
|
|
|
|
|
def create_parser(user_options): |
|
"""Parses the user-given parameters. |
|
""" |
|
parser = argparse.ArgumentParser( |
|
description=SUMMARY, |
|
epilog="BigML, Inc") |
|
|
|
parser_add_options(parser, user_options) |
|
return parser |
|
|
|
|
|
def main(args=sys.argv[1:]): |
|
"""Parses command-line parameters and calls the actual main function. |
|
""" |
|
|
|
# If credentials are properly set in environment variables, there's no need |
|
# to explicitly create the api object. Otherwise, use next code to set them |
|
|
|
try: |
|
api = BigML() |
|
except: |
|
sys.exit("The connection to BigML needs credentials. Please set them" |
|
" in the environment variables.") |
|
|
|
command_args = create_parser(OPTIONS).parse_args(args) |
|
# transforming args object to dictionary |
|
context = vars(command_args) |
|
|
|
source = api.create_source(context["test_source"]) |
|
api.ok(source) |
|
try: |
|
with open("./script") as file_handler: |
|
script_id = file_handler.read() |
|
except IOError: |
|
with open("./1-click-batchprediction.whizzml") as file_handler: |
|
script_code = file_handler.read() |
|
args = json.load(open("./1-click-batchprediction.json")) |
|
script_id = api.create_script(script_code, args) |
|
with open("./script", "w") as file_handler: |
|
file_handler.write(script_id["resource"]) |
|
|
|
execution = api.create_execution( \ |
|
script_id, {"inputs": [["test-source", source["resource"]], |
|
["fusion", context["fusion"]]]}) |
|
api.ok(execution) |
|
batch_prediction = execution["object"]["execution"]["results"][0] |
|
api.download_batch_prediction(batch_prediction, "./predictions.csv") |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |