Skip to content

Commit

Permalink
Updating the code to adapt to changes in backend
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerce committed May 18, 2016
1 parent 9a4d649 commit 4b8f647
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 118 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
History
-------

4.6.0 (2016-04-10)
~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion bigml/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '4.5.4'
__version__ = '4.6.0'
4 changes: 2 additions & 2 deletions bigml/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
SAMPLE_PATH, SAMPLE_RE, CORRELATION_PATH, CORRELATION_RE,
STATISTICAL_TEST_PATH, STATISTICAL_TEST_RE,
LOGISTIC_REGRESSION_PATH, LOGISTIC_REGRESSION_RE, ASSOCIATION_PATH,
ASSOCIATION_RE, ASSOCIATION_SET_PATH, ASSOCIATION_SET_RE
ASSOCIATION_RE, ASSOCIATION_SET_PATH, ASSOCIATION_SET_RE,
SCRIPT_PATH, SCRIPT_RE,
EXECUTION_PATH, EXECUTION_RE, LIBRARY_PATH, LIBRARY_RE)

Expand All @@ -99,7 +99,7 @@
get_status, check_resource, http_ok, get_project_id, get_sample_id,
get_correlation_id, get_statistical_test_id, get_logistic_regression_id,
get_association_id, get_association_set_id, get_script_id,
get_execution_id, get_library_id))
get_execution_id, get_library_id)


# Map status codes to labels
Expand Down
14 changes: 13 additions & 1 deletion bigml/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
LOGISTIC_REGRESSION_PATH = 'logisticregression'
ASSOCIATION_PATH = 'association'
ASSOCIATION_SET_PATH = 'associationset'
SCRIPT_PATH = 'script'
EXECUTION_PATH = 'execution'
LIBRARY_PATH = 'library'


# Resource Ids patterns
Expand Down Expand Up @@ -81,6 +84,12 @@
(ASSOCIATION_PATH, ID_PATTERN, ASSOCIATION_PATH, SHARED_PATTERN))
ASSOCIATION_SET_RE = re.compile(r'^%s/%s$' % \
(ASSOCIATION_SET_PATH, ID_PATTERN))
SCRIPT_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \
(SCRIPT_PATH, ID_PATTERN, SCRIPT_PATH, SHARED_PATTERN))
EXECUTION_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \
(EXECUTION_PATH, ID_PATTERN, EXECUTION_PATH, SHARED_PATTERN))
LIBRARY_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \
(LIBRARY_PATH, ID_PATTERN, LIBRARY_PATH, SHARED_PATTERN))

RESOURCE_RE = {
SOURCE_PATH: SOURCE_RE,
Expand All @@ -102,7 +111,10 @@
STATISTICAL_TEST_PATH: STATISTICAL_TEST_RE,
LOGISTIC_REGRESSION_PATH: LOGISTIC_REGRESSION_RE,
ASSOCIATION_PATH: ASSOCIATION_RE,
ASSOCIATION_SET_PATH: ASSOCIATION_SET_RE}
ASSOCIATION_SET_PATH: ASSOCIATION_SET_RE,
SCRIPT_PATH: SCRIPT_RE,
EXECUTION_PATH: EXECUTION_RE,
LIBRARY_PATH: LIBRARY_RE}


RENAMED_RESOURCES = {
Expand Down
4 changes: 2 additions & 2 deletions bigml/executionhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from bigml.resourcehandler import (check_resource_type,
get_execution_id, get_resource_type,
get_script_id, check_resource)
from bigml.resourcehandler import (EXECUTION_PATH, SCRIPT_PATH,
TINY_RESOURCE)
from bigml.constants import (EXECUTION_PATH, SCRIPT_PATH,
TINY_RESOURCE)


class ExecutionHandler(ResourceHandler):
Expand Down
24 changes: 23 additions & 1 deletion bigml/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import sys
import json


from bigml.util import invert_dictionary, python_map_type, find_locale
from bigml.util import DEFAULT_LOCALE
from bigml.api import get_resource_type
Expand All @@ -70,8 +71,12 @@
UPDATABLE_HEADERS = {"field name": "name",
"field label": "label",
"field description": "description",
<<<<<<< HEAD
"field type": "optype",
"preferred": "preferred"}
=======
"field type": "optype"}
>>>>>>> Updating the code to adapt to changes in backend

ITEM_SINGULAR = {u"categories": u"category"}

Expand Down Expand Up @@ -131,16 +136,34 @@ def get_fields_structure(resource, errors=False):
result = result + (field_errors,)
return result
else:
<<<<<<< HEAD
return (None, None, None, None, None) if errors else \
(None, None, None, None)
=======

return None, None, None, None, None if errors else \
None, None, None, None


def text_encode(text):
"""Encoding text to be written in a utf-8 file
"""
if isinstance(text, basestring):
return text.encode("utf-8")
return text
>>>>>>> Updating the code to adapt to changes in backend


def attribute_summary(attribute_value, item_type, limit=None):
"""Summarizes the information in fields attributes where content is
written as an array of arrays like tag_cloud, items, etc.
"""
<<<<<<< HEAD
if attribute_value is None:
return None
=======
>>>>>>> Updating the code to adapt to changes in backend
items = [u"%s (%s)" % (item, instances) for
item, instances in attribute_value]
items_length = len(items)
Expand Down Expand Up @@ -588,7 +611,6 @@ def new_fields_structure(self, csv_attributes_file=None,
self.field_id(int(field_attributes[0]))
new_fields_structure[field_id] = \
dict(zip(headers, field_attributes[1: 6]))

except ValueError:
raise ValueError("The first column should contain either the"
" column or ID of the fields. Failed to find"
Expand Down
2 changes: 1 addition & 1 deletion bigml/libraryhandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from bigml.resourcehandler import (check_resource_type,
get_library_id, get_resource_type,
check_resource)
from bigml.resourcehandler import (LIBRARY_PATH, TINY_RESOURCE)
from bigml.constants import LIBRARY_PATH, TINY_RESOURCE


class LibraryHandler(ResourceHandler):
Expand Down
4 changes: 3 additions & 1 deletion bigml/logistic.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ def __init__(self, logistic_regression, api=None):
self.input_fields = logistic_regression.get("input_fields", [])
self.dataset_field_types = logistic_regression.get(
"dataset_field_types", {})
objective_field = logistic_regression['objective_fields']
objective_field = logistic_regression['objective_fields'] if \
logistic_regression['objective_fields'] else \
logistic_regression['objective_field']
except KeyError:
raise ValueError("Failed to find the logistic regression expected "
"JSON structure. Check your arguments.")
Expand Down
104 changes: 3 additions & 101 deletions bigml/resourcehandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,104 +28,6 @@
from bigml.bigmlconnection import BigMLConnection


# Basic resources
SOURCE_PATH = 'source'
DATASET_PATH = 'dataset'
MODEL_PATH = 'model'
PREDICTION_PATH = 'prediction'
EVALUATION_PATH = 'evaluation'
ENSEMBLE_PATH = 'ensemble'
BATCH_PREDICTION_PATH = 'batchprediction'
CLUSTER_PATH = 'cluster'
CENTROID_PATH = 'centroid'
BATCH_CENTROID_PATH = 'batchcentroid'
ANOMALY_PATH = 'anomaly'
ANOMALY_SCORE_PATH = 'anomalyscore'
BATCH_ANOMALY_SCORE_PATH = 'batchanomalyscore'
PROJECT_PATH = 'project'
SAMPLE_PATH = 'sample'
CORRELATION_PATH = 'correlation'
STATISTICAL_TEST_PATH = 'statisticaltest'
SCRIPT_PATH = 'script'
EXECUTION_PATH = 'execution'
LIBRARY_PATH = 'library'

# Resource Ids patterns
ID_PATTERN = '[a-f0-9]{24}'
SHARED_PATTERN = '[a-zA-Z0-9]{24,30}'
SOURCE_RE = re.compile(r'^%s/%s$' % (SOURCE_PATH, ID_PATTERN))
# public resources: model and dataset
# shared resources: all except sources
DATASET_RE = re.compile(r'^(public/)?%s/%s$|^shared/%s/%s$' % (
DATASET_PATH, ID_PATTERN, DATASET_PATH, SHARED_PATTERN))
MODEL_RE = re.compile(r'^(public/)?%s/%s$|^shared/%s/%s$' % (
MODEL_PATH, ID_PATTERN, MODEL_PATH, SHARED_PATTERN))
#
PREDICTION_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
PREDICTION_PATH, ID_PATTERN, PREDICTION_PATH, SHARED_PATTERN))
EVALUATION_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
EVALUATION_PATH, ID_PATTERN, EVALUATION_PATH, SHARED_PATTERN))
ENSEMBLE_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
ENSEMBLE_PATH, ID_PATTERN, ENSEMBLE_PATH, SHARED_PATTERN))
BATCH_PREDICTION_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
BATCH_PREDICTION_PATH, ID_PATTERN, BATCH_PREDICTION_PATH, SHARED_PATTERN))
CLUSTER_RE = re.compile(r'^%s/%s$|^shared/%s/%s$' % (
CLUSTER_PATH, ID_PATTERN, CLUSTER_PATH, SHARED_PATTERN))
CENTROID_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
CENTROID_PATH, ID_PATTERN, CENTROID_PATH, SHARED_PATTERN))
BATCH_CENTROID_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
BATCH_CENTROID_PATH, ID_PATTERN, BATCH_CENTROID_PATH, SHARED_PATTERN))
ANOMALY_RE = re.compile(r'^(public/)?%s/%s$|^shared/%s/%s$' % (
ANOMALY_PATH, ID_PATTERN, ANOMALY_PATH, SHARED_PATTERN))
ANOMALY_SCORE_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
ANOMALY_SCORE_PATH, ID_PATTERN, ANOMALY_SCORE_PATH, SHARED_PATTERN))
BATCH_ANOMALY_SCORE_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
BATCH_ANOMALY_SCORE_PATH, ID_PATTERN,
BATCH_ANOMALY_SCORE_PATH, SHARED_PATTERN))
PROJECT_RE = re.compile(r'^%s/%s$' % (PROJECT_PATH, ID_PATTERN))
SAMPLE_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
SAMPLE_PATH, ID_PATTERN, SAMPLE_PATH, SHARED_PATTERN))
CORRELATION_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % (
CORRELATION_PATH, ID_PATTERN, CORRELATION_PATH, SHARED_PATTERN))
STATISTICAL_TEST_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \
(STATISTICAL_TEST_PATH, ID_PATTERN, STATISTICAL_TEST_PATH, SHARED_PATTERN))
SCRIPT_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \
(SCRIPT_PATH, ID_PATTERN, SCRIPT_PATH, SHARED_PATTERN))
EXECUTION_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \
(EXECUTION_PATH, ID_PATTERN, EXECUTION_PATH, SHARED_PATTERN))
LIBRARY_RE = re.compile(r'^%s/%s|^shared/%s/%s$' % \
(LIBRARY_PATH, ID_PATTERN, LIBRARY_PATH, SHARED_PATTERN))

RESOURCE_RE = {
'source': SOURCE_RE,
'dataset': DATASET_RE,
'model': MODEL_RE,
'prediction': PREDICTION_RE,
'evaluation': EVALUATION_RE,
'ensemble': ENSEMBLE_RE,
'batchprediction': BATCH_PREDICTION_RE,
'cluster': CLUSTER_RE,
'centroid': CENTROID_RE,
'batchcentroid': BATCH_CENTROID_RE,
'anomaly': ANOMALY_RE,
'anomalyscore': ANOMALY_SCORE_RE,
'batchanomalyscore': BATCH_ANOMALY_SCORE_RE,
'project': PROJECT_RE,
'sample': SAMPLE_RE,
'correlation': CORRELATION_RE,
'statisticaltest': STATISTICAL_TEST_RE,
'script': SCRIPT_RE,
'execution': EXECUTION_RE,
'library': LIBRARY_RE}


RENAMED_RESOURCES = {
'batchprediction': 'batch_prediction',
'batchcentroid': 'batch_centroid',
'anomalyscore': 'anomaly_score',
'batchanomalyscore': 'batch_anomaly_score',
'statisticaltest': 'statistical_test'
}

NO_QS = [c.EVALUATION_RE, c.PREDICTION_RE, c.BATCH_PREDICTION_RE,
c.CENTROID_RE, c.BATCH_CENTROID_RE, c.ANOMALY_SCORE_RE,
Expand Down Expand Up @@ -364,21 +266,21 @@ def get_script_id(script):
"""Returns a script/id.
"""
return get_resource(SCRIPT_RE, script)
return get_resource(c.SCRIPT_PATH, script)


def get_execution_id(execution):
"""Returns a execution/id.
"""
return get_resource(EXECUTION_RE, execution)
return get_resource(c.EXECUTION_PATH, execution)


def get_library_id(library):
"""Returns a library/id.
"""
return get_resource(LIBRARY_RE, library)
return get_resource(c.LIBRARY_PATH, library)


def get_resource_id(resource):
Expand Down
4 changes: 2 additions & 2 deletions bigml/scripthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
from bigml.resourcehandler import (check_resource_type,
get_script_id, get_resource_type,
get_dataset_id, check_resource)
from bigml.resourcehandler import (SCRIPT_PATH, DATASET_PATH,
TINY_RESOURCE)
from bigml.constants import (SCRIPT_PATH, DATASET_PATH,
TINY_RESOURCE)


class ScriptHandler(ResourceHandler):
Expand Down
6 changes: 6 additions & 0 deletions bigml/tests/inspect_model_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def i_check_the_model_summary_with(step, file):
output = io.BytesIO()
world.local_model.summarize(out=output)
world.output = output.getvalue()
if not os.path.exists('./tmp'):
os.mkdir('./tmp')
with open('./tmp/summary.txt', "w") as rules_file:
rules_file.write(world.output)


i_check_if_the_output_is_like_expected_file(step, file)


Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ def test_scenario1(self):
Examples:
| source_code | time_1 | time_2 | time_3 | param | param_value
| (+ 1 1) | 10 | 10 | 10 | name | my execution
| (+ 1 1) | 10 | 10 | 50 | name | my execution
"""
print self.test_scenario1.__doc__
examples = [
['(+ 1 1)', '10', '10', '10', 'name', 'my execution']]
['(+ 1 1)', '10', '10', '50', 'name', 'my execution']]
for example in examples:
print "\nTesting with:\n", example
script_create.i_create_a_script(self, example[0])
Expand All @@ -80,11 +80,11 @@ def test_scenario2(self):
Examples:
| source_code | time_1 | time_2 | time_3 | param | param_value
| (+ 1 1) | 10 | 10 | 10 | name | my execution
| (+ 1 1) | 10 | 10 | 50 | name | my execution
"""
print self.test_scenario2.__doc__
examples = [
['(+ 1 1)', '10', '10', '10', 'name', 'my execution']]
['(+ 1 1)', '10', '10', '50', 'name', 'my execution']]
for example in examples:
print "\nTesting with:\n", example
script_create.i_create_a_script(self, example[0])
Expand Down
File renamed without changes.
5 changes: 3 additions & 2 deletions bigml/tests/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
'correlation',
'statisticaltest',
'logisticregression',
'association'
'association',
'script',
'execution',
'library'
Expand All @@ -60,9 +60,10 @@
'anomalyscore': 'anomaly_scores',
'batchanomalyscore': 'batch_anomaly_scores',
'statisticaltest': 'statistical_tests',
'logisticregression': 'logistic_regressions'
'logisticregression': 'logistic_regressions',
'library': 'libraries'
}

TRANSLATED_RESOURCES = {
'batchprediction': 'batch_prediction',
'batchcentroid': 'batch_centroid',
Expand Down

0 comments on commit 4b8f647

Please sign in to comment.