Commit 9c16fbbe by gradyward

Finalized consistent Error Checking, and added myself to the AUTHORS document

parent 0a4d39d7
......@@ -4,3 +4,4 @@ John Jarvis <jarv@edx.org>
James Tauber <jtauber@jtauber.com>
Nate Aune <naune@edx.org>
John Kern <kern3020@gmail.com>
Grady Ward <gward@brandeis.edu>
\ No newline at end of file
......@@ -69,7 +69,7 @@ def create(examples, scores, prompt_string, dump_data=False):
# Create an essay set object that encapsulates all the essays and alternate representations (tokens, etc)
try:
essay_set = _create_essay_set(examples, scores, prompt_string)
except (EssaySetRequestError, ExampleCreationInternalError) as ex:
except EssaySetRequestError as ex:
msg = "Essay Set Creation failed (likely due to an error in essay cleaning/parsing) {}".format(ex)
results['errors'].append(msg)
log.exception(msg)
......
......@@ -2,20 +2,52 @@
Errors for the EASE repository
"""
class EssaySetRequestError(Exception):
class EaseError(Exception):
pass
class ExampleCreationInternalError(Exception):
class EssaySetRequestError(EaseError):
"""
There was a problem with a request sent to the Essay Set module.
"""
pass
class EaseError(Exception):
class GradingRequestError(EaseError):
"""
There was a problem with a request sent to the Grading module.
"""
pass
class GradingRequestError(Exception):
class ClassifierTrainingInternalError(EaseError):
"""
An unexpected error occurred when training a classifier.
"""
pass
class ClassifierTrainingInternalError(Exception):
class CreateRequestError(EaseError):
"""
There was a problem with a request sent to the Create Module.
"""
pass
class CreateRequestError(Exception):
class FeatureExtractionInternalError(EaseError):
"""
An unexpected error occurred while extracting features from an essay.
"""
pass
class InputError(EaseError):
"""
The user supplied an argument which was incorrect.
"""
def __init__(self, expr, msg):
self.expr = expr
self.msg = msg
def __str__(self):
"An input error occurred at '{0}': {1}".format(self.expr, self.msg)
\ No newline at end of file
......@@ -8,6 +8,7 @@ import sys
import random
import os
import logging
from ease.errors import InputError
from errors import *
base_path = os.path.dirname(__file__)
......@@ -171,7 +172,7 @@ class EssaySet(object):
if isinstance(prompt_text, basestring):
self._prompt = util_functions.sub_chars(prompt_text)
else:
raise util_functions.InputError(prompt_text, "Invalid prompt. Need to enter a string value.")
raise InputError('prompt_text', "Invalid prompt. Need to enter a string value.")
return self._prompt
def generate_additional_essays(self, original_essay, original_score, to_generate=3):
......
......@@ -11,6 +11,7 @@ import os
from itertools import chain
import operator
import logging
from errors import *
base_path = os.path.dirname(__file__)
sys.path.append(base_path)
......@@ -100,9 +101,9 @@ class FeatureExtractor(object):
# Average index of how "topical" essays are
self._mean_topical_index = feature_row_sum / float(sum([len(t) for t in essay_set._cleaned_essays]))
else:
raise util_functions.InputError(essay_set, "needs to be an essay set of the train type.")
raise InputError('essay_set', "The EssaySet provided needs to be an EssaySet of the 'train' type.")
else:
raise util_functions.InputError(essay_set, "wrong input. need an essay set object.")
raise InputError('essay_set', "Wrong input provided, it must provide an EssaySet object.")
def generate_features(self, essay_set):
"""
......@@ -117,9 +118,14 @@ class FeatureExtractor(object):
- Vocabulary Features (both Normal and Stemmed Vocabulary)
- Prompt Features
"""
try:
vocabulary_features = self._generate_vocabulary_features(essay_set)
length_features = self._generate_length_features(essay_set)
prompt_features = self._generate_prompt_features(essay_set)
except Exception as ex:
msg = "An unexpected error occurred during feature extraction: {}".format(ex)
log.exception(msg)
raise FeatureExtractionInternalError(msg)
# Lumps them all together, copies to solidify, and returns
overall_features = numpy.concatenate((length_features, prompt_features, vocabulary_features), axis=1)
......
......@@ -2,10 +2,12 @@
Functions to score specified data using specified ML models
"""
import sys
import os
import logging
import sys
# Append sys to base path to import the following modules
base_path = os.path.dirname(__file__)
sys.path.append(base_path)
......@@ -52,18 +54,17 @@ def grade(grader_data, submission):
try:
grader_set.add_essay(str(submission), 0)
grader_set.update_prompt(str(grader_data['prompt']))
except:
except (EssaySetRequestError, InputError):
error_message = "Essay could not be added to essay set:{0}".format(submission)
log.exception(error_message)
results['errors'].append(error_message)
# Tries to extract features from submission and assign score via the model
grader_features = None
try:
grader_features = extractor.generate_features(grader_set)
results['score'] = int(model.predict(grader_features)[0])
except:
error_message = "Could not extract features and score essay."
except FeatureExtractionInternalError as ex:
error_message = "Could not extract features and score essay: {ex}".format(ex=ex)
log.exception(error_message)
results['errors'].append(error_message)
......
......@@ -200,15 +200,6 @@ def get_vocab(essays, scores, max_features_pass_1=750, max_features_pass_2=200):
return vocab
class InputError(Exception):
"""
A class to report to the user that one of their inputs was incorrect.
"""
def __init__(self, expr, msg):
self.expr = expr
self.msg = msg
def gen_cv_preds(clf, arr, sel_score, num_chunks=3):
"""
Generates cross validated predictions using an input classifier and data.
......
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