Commit 6c1aa5f9 by gradyward

Cleaned up exception handling in the create.py method.

Created two new exceptions to describe the behavior of their errors.
parent 6c38578b
...@@ -7,10 +7,9 @@ import sys ...@@ -7,10 +7,9 @@ import sys
import logging import logging
import numpy import numpy
# Define base path and add to sys path # Constructs a log
from ease import feature_extractor log = logging.getLogger(__name__)
from ease.essay_set import EssaySet # Setup base path so that we can import modules who are dependent on it
base_path = os.path.dirname(__file__) base_path = os.path.dirname(__file__)
sys.path.append(base_path) sys.path.append(base_path)
one_up_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..//')) one_up_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..//'))
...@@ -22,9 +21,8 @@ from errors import * ...@@ -22,9 +21,8 @@ from errors import *
from datetime import datetime from datetime import datetime
import json import json
import sklearn.ensemble import sklearn.ensemble
from ease import feature_extractor
#Make a log from ease.essay_set import EssaySet
log = logging.getLogger(__name__)
def create(examples, scores, prompt_string, dump_data=False): def create(examples, scores, prompt_string, dump_data=False):
...@@ -72,7 +70,7 @@ def create(examples, scores, prompt_string, dump_data=False): ...@@ -72,7 +70,7 @@ def create(examples, scores, prompt_string, dump_data=False):
try: try:
essay_set = _create_essay_set(examples, scores, prompt_string) essay_set = _create_essay_set(examples, scores, prompt_string)
except (ExampleCreationRequestError, ExampleCreationInternalError) as ex: except (ExampleCreationRequestError, ExampleCreationInternalError) as ex:
msg = "essay set creation failed due to an error in the create_essay_set method. {}".format(ex) msg = "Essay Set Creation failed (likely due to an error in essay cleaning/parsing) {}".format(ex)
results['errors'].append(msg) results['errors'].append(msg)
log.exception(msg) log.exception(msg)
return results return results
...@@ -88,11 +86,13 @@ def create(examples, scores, prompt_string, dump_data=False): ...@@ -88,11 +86,13 @@ def create(examples, scores, prompt_string, dump_data=False):
results['classifier'] = classifier results['classifier'] = classifier
results['algorithm'] = algorithm results['algorithm'] = algorithm
results['success'] = True results['success'] = True
except Exception as ex:
msg = "feature extraction and model creation failed." # We cannot be sure what kind of errors .fit could throw at us. Memory, Type, Interrupt, etc.
except ClassifierTrainingInternalError as ex:
msg = "Feature extraction and Model Creation failed with the following error {ex}".format(ex=ex)
results['errors'].append(msg) results['errors'].append(msg)
log.exception(msg) log.exception(msg)
log.exception(ex) results['success'] = False
return results return results
...@@ -159,6 +159,9 @@ def _extract_features_and_generate_model(essay_set): ...@@ -159,6 +159,9 @@ def _extract_features_and_generate_model(essay_set):
- The Trained Feature extractor - The Trained Feature extractor
- The Trained Classifier - The Trained Classifier
- Any Cross Validation results - Any Cross Validation results
Raises:
ClassifierTrainingInternalError
""" """
feat_extractor = feature_extractor.FeatureExtractor(essay_set) feat_extractor = feature_extractor.FeatureExtractor(essay_set)
...@@ -173,11 +176,14 @@ def _extract_features_and_generate_model(essay_set): ...@@ -173,11 +176,14 @@ def _extract_features_and_generate_model(essay_set):
try: try:
predict_classifier.fit(features, set_scores) predict_classifier.fit(features, set_scores)
except:
log.exception("Not enough classes (0,1,etc) in sample.") # We cannot be sure what kind of errors .fit could throw at us. Memory, Type, Interrupt, etc.
set_scores[0] = 1 except Exception as ex:
set_scores[1] = 0 str = (
predict_classifier.fit(features, set_scores) "predict_classifier.fit raised an exception in _extract_features_and_generate_model: {}"
).format(ex)
log.exception(str)
raise ClassifierTrainingInternalError(str)
return feat_extractor, predict_classifier, cv_error_results return feat_extractor, predict_classifier, cv_error_results
...@@ -243,23 +249,30 @@ def _get_cv_error(classifier, features, scores): ...@@ -243,23 +249,30 @@ def _get_cv_error(classifier, features, scores):
# TODO Figure out why this error would occur in the first place. # TODO Figure out why this error would occur in the first place.
msg = u"Not enough classes (0,1,etc) in each cross validation fold: {ex}".format(ex=ex) msg = u"Not enough classes (0,1,etc) in each cross validation fold: {ex}".format(ex=ex)
log.debug(msg) log.debug(msg)
except:
log.exception("Error getting cv error estimates.")
return results return results
def _dump_input_data(text, score): def _dump_input_data(essays, scores):
try: """
Dumps input data using json serialized objects of the form {'text': essay, 'score': score}
Args:
essays (list of str): A list of essays to dump
scores (list of int): An associated list of scores
"""
file_path = base_path + "/tests/data/json_data/" file_path = base_path + "/tests/data/json_data/"
time_suffix = datetime.now().strftime("%H%M%S%d%m%Y") time_suffix = datetime.now().strftime("%H%M%S%d%m%Y")
prefix = "test-case-" prefix = "test-case-"
filename = prefix + time_suffix + ".json" filename = prefix + time_suffix + ".json"
json_data = [] json_data = []
for i in xrange(0, len(text)): try:
json_data.append({'text': text[i], 'score': score[i]}) for i in xrange(0, len(essays)):
json_data.append({'text': essays[i], 'score': scores[i]})
with open(file_path + filename, 'w+') as outfile: with open(file_path + filename, 'w+') as outfile:
json.dump(json_data, outfile) json.dump(json_data, outfile)
except: except IOError as ex:
error = "Could not dump data to file." error = "An IO error occurred while trying to dump JSON data to a file: {ex}".format(ex=ex)
log.exception(error) log.exception(error)
raise CreateRequestError(error)
...@@ -13,3 +13,8 @@ class EaseError(Exception): ...@@ -13,3 +13,8 @@ class EaseError(Exception):
class GradingRequestError(Exception): class GradingRequestError(Exception):
pass pass
class ClassifierTrainingInternalError(Exception):
pass
class CreateRequestError(Exception):
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment