Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-ora2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-ora2
Commits
e424c439
Commit
e424c439
authored
May 21, 2014
by
Will Daly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor training API call to move logic into AITrainingWorkflow model
parent
44ad63d8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
88 additions
and
39 deletions
+88
-39
apps/openassessment/assessment/api/ai.py
+5
-1
apps/openassessment/assessment/api/ai_worker.py
+21
-37
apps/openassessment/assessment/models/ai.py
+57
-1
apps/openassessment/assessment/test/test_ai.py
+5
-0
No files found.
apps/openassessment/assessment/api/ai.py
View file @
e424c439
...
@@ -8,7 +8,9 @@ from openassessment.assessment.serializers import (
...
@@ -8,7 +8,9 @@ from openassessment.assessment.serializers import (
from
openassessment.assessment.errors
import
(
from
openassessment.assessment.errors
import
(
AITrainingRequestError
,
AITrainingInternalError
AITrainingRequestError
,
AITrainingInternalError
)
)
from
openassessment.assessment.models
import
AITrainingWorkflow
,
InvalidOptionSelection
from
openassessment.assessment.models
import
(
AITrainingWorkflow
,
InvalidOptionSelection
,
NoTrainingExamples
)
from
openassessment.assessment.worker
import
training
as
training_tasks
from
openassessment.assessment.worker
import
training
as
training_tasks
...
@@ -89,6 +91,8 @@ def train_classifiers(rubric_dict, examples, algorithm_id):
...
@@ -89,6 +91,8 @@ def train_classifiers(rubric_dict, examples, algorithm_id):
# Create the workflow model
# Create the workflow model
try
:
try
:
workflow
=
AITrainingWorkflow
.
start_workflow
(
examples
,
algorithm_id
)
workflow
=
AITrainingWorkflow
.
start_workflow
(
examples
,
algorithm_id
)
except
NoTrainingExamples
as
ex
:
raise
AITrainingRequestError
(
ex
)
except
:
except
:
msg
=
(
msg
=
(
u"An unexpected error occurred while creating "
u"An unexpected error occurred while creating "
...
...
apps/openassessment/assessment/api/ai_worker.py
View file @
e424c439
...
@@ -7,7 +7,7 @@ from django.db import DatabaseError
...
@@ -7,7 +7,7 @@ from django.db import DatabaseError
from
openassessment.assessment.models
import
(
from
openassessment.assessment.models
import
(
AITrainingWorkflow
,
AIClassifierSet
,
AITrainingWorkflow
,
AIClassifierSet
,
ClassifierUploadError
,
ClassifierSerializeError
,
ClassifierUploadError
,
ClassifierSerializeError
,
IncompleteClassifierSet
IncompleteClassifierSet
,
NoTrainingExamples
)
)
from
openassessment.assessment.errors
import
(
from
openassessment.assessment.errors
import
(
AITrainingRequestError
,
AITrainingInternalError
AITrainingRequestError
,
AITrainingInternalError
...
@@ -185,8 +185,7 @@ def create_classifiers(training_workflow_uuid, classifier_set):
...
@@ -185,8 +185,7 @@ def create_classifiers(training_workflow_uuid, classifier_set):
Args:
Args:
training_workflow_uuid (str): The UUID of the training workflow.
training_workflow_uuid (str): The UUID of the training workflow.
classifier_set (dict): Mapping of criterion names to serialized classifiers.
classifier_set (dict): Mapping of criteria names to serialized classifiers.
(binary classifiers should be base-64 encoded).
Returns:
Returns:
None
None
...
@@ -201,47 +200,32 @@ def create_classifiers(training_workflow_uuid, classifier_set):
...
@@ -201,47 +200,32 @@ def create_classifiers(training_workflow_uuid, classifier_set):
# If the task is executed multiple times, the classifier set may already
# If the task is executed multiple times, the classifier set may already
# have been created. If so, log a warning then return immediately.
# have been created. If so, log a warning then return immediately.
if
workflow
.
classifier_set
is
not
Non
e
:
if
workflow
.
is_complet
e
:
msg
=
u"AI training workflow with UUID {} already has trained classifiers."
msg
=
u"AI training workflow with UUID {} already has trained classifiers."
logger
.
warning
(
msg
)
logger
.
warning
(
msg
)
return
else
:
workflow
.
complete
(
classifier_set
)
# Retrieve the rubric model
rubric
=
workflow
.
rubric
if
rubric
is
None
:
msg
=
(
u"The AI training workflow with UUID {} does not have "
u"a rubric associated with it, which means it has no "
u"training examples."
)
.
format
(
training_workflow_uuid
)
logger
.
exception
(
msg
)
raise
AITrainingInternalError
(
msg
)
try
:
workflow
.
classifier_set
=
AIClassifierSet
.
create_classifier_set
(
classifier_set
,
rubric
,
workflow
.
algorithm_id
)
except
IncompleteClassifierSet
as
ex
:
msg
=
(
u"An error occurred while creating the classifier set "
u"for the training workflow with UUID {uuid}: {ex}"
)
.
format
(
uuid
=
training_workflow_uuid
,
ex
=
ex
)
raise
AITrainingRequestError
(
msg
)
except
(
ClassifierSerializeError
,
ClassifierUploadError
,
DatabaseError
)
as
ex
:
msg
=
(
u"An unexpected error occurred while creating the classifier "
u"set for training workflow UUID {uuid}: {ex}"
)
.
format
(
uuid
=
training_workflow_uuid
,
ex
=
ex
)
logger
.
exception
(
msg
)
raise
AITrainingInternalError
(
msg
)
workflow
.
completed_at
=
now
()
workflow
.
save
()
except
AITrainingWorkflow
.
DoesNotExist
:
except
AITrainingWorkflow
.
DoesNotExist
:
msg
=
(
msg
=
(
u"Could not retrieve AI training workflow with UUID {}"
u"Could not retrieve AI training workflow with UUID {}"
)
.
format
(
training_workflow_uuid
)
)
.
format
(
training_workflow_uuid
)
raise
AITrainingRequestError
(
msg
)
raise
AITrainingRequestError
(
msg
)
except
NoTrainingExamples
as
ex
:
logger
.
exception
(
ex
)
raise
AITrainingInternalError
(
ex
)
except
IncompleteClassifierSet
as
ex
:
msg
=
(
u"An error occurred while creating the classifier set "
u"for the training workflow with UUID {uuid}: {ex}"
)
.
format
(
uuid
=
training_workflow_uuid
,
ex
=
ex
)
raise
AITrainingRequestError
(
msg
)
except
(
ClassifierSerializeError
,
ClassifierUploadError
,
DatabaseError
)
as
ex
:
msg
=
(
u"An unexpected error occurred while creating the classifier "
u"set for training workflow UUID {uuid}: {ex}"
)
.
format
(
uuid
=
training_workflow_uuid
,
ex
=
ex
)
logger
.
exception
(
msg
)
raise
AITrainingInternalError
(
msg
)
except
DatabaseError
:
except
DatabaseError
:
msg
=
(
msg
=
(
u"An unexpected error occurred while creating the classifier set "
u"An unexpected error occurred while creating the classifier set "
...
...
apps/openassessment/assessment/models/ai.py
View file @
e424c439
...
@@ -40,6 +40,19 @@ class ClassifierSerializeError(Exception):
...
@@ -40,6 +40,19 @@ class ClassifierSerializeError(Exception):
pass
pass
class
NoTrainingExamples
(
Exception
):
"""
No training examples were provided to the workflow.
"""
def
__init__
(
self
,
workflow_uuid
=
None
):
msg
=
u"No training examples were provided"
if
workflow_uuid
is
not
None
:
msg
=
u"{msg} to the training workflow with UUID {uuid}"
.
format
(
msg
=
msg
,
uuid
=
workflow_uuid
)
super
(
NoTrainingExamples
,
self
)
.
__init__
(
msg
)
class
AIClassifierSet
(
models
.
Model
):
class
AIClassifierSet
(
models
.
Model
):
"""
"""
A set of trained classifiers (immutable).
A set of trained classifiers (immutable).
...
@@ -242,7 +255,13 @@ class AITrainingWorkflow(models.Model):
...
@@ -242,7 +255,13 @@ class AITrainingWorkflow(models.Model):
Returns:
Returns:
AITrainingWorkflow
AITrainingWorkflow
Raises:
NoTrainingExamples
"""
"""
if
len
(
examples
)
==
0
:
raise
NoTrainingExamples
()
workflow
=
AITrainingWorkflow
.
objects
.
create
(
algorithm_id
=
algorithm_id
)
workflow
=
AITrainingWorkflow
.
objects
.
create
(
algorithm_id
=
algorithm_id
)
workflow
.
training_examples
.
add
(
*
examples
)
workflow
.
training_examples
.
add
(
*
examples
)
workflow
.
save
()
workflow
.
save
()
...
@@ -256,6 +275,9 @@ class AITrainingWorkflow(models.Model):
...
@@ -256,6 +275,9 @@ class AITrainingWorkflow(models.Model):
Returns:
Returns:
Rubric or None (if no training examples are available)
Rubric or None (if no training examples are available)
Raises:
NoTrainingExamples
"""
"""
# We assume that all the training examples we have been provided are using
# We assume that all the training examples we have been provided are using
# the same rubric (this is enforced by the API call that deserializes
# the same rubric (this is enforced by the API call that deserializes
...
@@ -264,4 +286,38 @@ class AITrainingWorkflow(models.Model):
...
@@ -264,4 +286,38 @@ class AITrainingWorkflow(models.Model):
if
first_example
:
if
first_example
:
return
first_example
[
0
]
.
rubric
return
first_example
[
0
]
.
rubric
else
:
else
:
return
None
raise
NoTrainingExamples
(
workflow_uuid
=
self
.
uuid
)
@property
def
is_complete
(
self
):
"""
Check whether the workflow is complete (classifiers have been trained).
Returns:
bool
"""
return
self
.
completed_at
is
not
None
def
complete
(
self
,
classifier_set
):
"""
Add a classifier set to the workflow and mark it complete.
Args:
classifier_set (dict): Mapping of criteria names to serialized classifiers.
Returns:
None
Raises:
NoTrainingExamples
IncompleteClassifierSet
ClassifierSerializeError
ClassifierUploadError
DatabaseError
"""
self
.
classifier_set
=
AIClassifierSet
.
create_classifier_set
(
classifier_set
,
self
.
rubric
,
self
.
algorithm_id
)
self
.
completed_at
=
now
()
self
.
save
()
apps/openassessment/assessment/test/test_ai.py
View file @
e424c439
...
@@ -108,6 +108,11 @@ class AITrainingTest(CacheResetTest):
...
@@ -108,6 +108,11 @@ class AITrainingTest(CacheResetTest):
with
self
.
assertRaises
(
AITrainingRequestError
):
with
self
.
assertRaises
(
AITrainingRequestError
):
ai_api
.
train_classifiers
(
RUBRIC
,
mutated_examples
,
self
.
ALGORITHM_ID
)
ai_api
.
train_classifiers
(
RUBRIC
,
mutated_examples
,
self
.
ALGORITHM_ID
)
def
test_train_classifiers_no_examples
(
self
):
# Empty list of training examples
with
self
.
assertRaises
(
AITrainingRequestError
):
ai_api
.
train_classifiers
(
RUBRIC
,
[],
self
.
ALGORITHM_ID
)
@override_settings
(
ORA2_AI_ALGORITHMS
=
AI_ALGORITHMS
)
@override_settings
(
ORA2_AI_ALGORITHMS
=
AI_ALGORITHMS
)
@mock.patch.object
(
AITrainingWorkflow
.
objects
,
'create'
)
@mock.patch.object
(
AITrainingWorkflow
.
objects
,
'create'
)
def
test_start_workflow_database_error
(
self
,
mock_create
):
def
test_start_workflow_database_error
(
self
,
mock_create
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment