Skip to content

Commit

Permalink
#167 API finished
Browse files Browse the repository at this point in the history
  • Loading branch information
kripanshu committed Aug 23, 2018
1 parent 3dae8dd commit 98ab00d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 10 deletions.
3 changes: 3 additions & 0 deletions preprocess/code/col_info_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,6 @@
UPDATE_CUSTOM_STATISTICS = 'UPDATE_CUSTOM_STATISTICS'
UPDATE_TO_CUSTOM_STATISTICS = 'UPDATE_TO_CUSTOM_STATISTICS'
DELETE_CUSTOM_STATISTICS = 'DELETE_CUSTOM_STATISTICS'

PROBLEM_KEY = 'problems'
ADD_PROBLEM_SECTION = 'ADD_PROBLEM_SECTION'
81 changes: 80 additions & 1 deletion preprocess/code/problems_section_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,85 @@ def __init__(self, preprocess_json, problem_section_json):
self.error_messages = []
self.original_json = None

def add_error_message(self, err_msg):
"""Add error message"""
# print(err_msg)
self.has_error = True
self.error_messages.append(err_msg)

def get_updated_metadata(self, as_string=False):
"""Return the modified metadata--which is in the 'original_json' """
assert self.has_error is False, \
"Make sure that 'has_error' is False before using this method"

if as_string:
return json.dumps(self.original_json,
indent=4,
cls=NumpyJSONEncoder)

return self.original_json

def get_error_messages(self):
"""Return the list of error messages"""
print("Error messages ", self.error_messages)
return self.error_messages

@staticmethod
def is_major_update():
"""add or change a custom stat"""
return False

@staticmethod
def is_minor_update():
"""add or change a custom stat"""
return True

def redundent_id_check(self, id_list):
list_preprocess_problem = list(self.preprocess_json[col_const.PROBLEM_KEY])
print(list_preprocess_problem)
for item in id_list:
if item in list_preprocess_problem:
self.add_error_message('%s ID already present' % item)
return False, self.get_error_messages()
return True, None

def problem_section_update(self):
"""update the preprocess with problem section """
"""update the preprocess with problem section
Sample problem_section_json:
{
"preprocessId":24,
"version":1,
"problems":
[
{
"description":{"problem_id":"problem1","system":"auto","meaningful":"no","target":"Hits",
"predictors":["At_bats","Runs","Doubles"],"transform":0,"subsetObs":0,"subsetFeats":0,
"task":"regression","rating":3,"description":"Hits is predicted by At_bats and Runs and Doubles",
"metric":"meanSquaredError"},
"results":{}
}
]
}
"""
print(self.problem_section_json[col_const.PROBLEM_KEY])
id_list = list(self.problem_section_json[col_const.PROBLEM_KEY])

if col_const.PROBLEM_KEY not in self.preprocess_json:
self.preprocess_json[col_const.PROBLEM_KEY] = self.problem_section_json[col_const.PROBLEM_KEY]
else:
success, obj = self.redundent_id_check(id_list)
if not success:
return False
self.preprocess_json[col_const.PROBLEM_KEY].append(self.problem_section_json[col_const.PROBLEM_KEY])

self.original_json = OrderedDict(self.preprocess_json)

success, updated_or_err = VersionNumberUtil.update_version_number(
self.original_json,
self.is_major_update())

if not success:
self.add_error_message(updated_or_err)
return False


Original file line number Diff line number Diff line change
Expand Up @@ -425,4 +425,6 @@ def update_preprocess_problem_section(job_id, version, problem_section_json):
if problem_section_update.has_error:
return err_resp(get_json_error(problem_section_update))

print(problem_section_update.get_updated_metadata())

return ok_resp(problem_section_update.get_updated_metadata())
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

from ravens_metadata_apps.preprocess_jobs.models import MetadataUpdate
from custom_statistics_util import CustomStatisticsUtil

from problems_section_utils import ProblemSectionUtil
from variable_display_util import VariableDisplayUtil
from np_json_encoder import NumpyJSONEncoder
from col_info_constants import \
(UPDATE_VARIABLE_DISPLAY, UPDATE_CUSTOM_STATISTICS,DELETE_CUSTOM_STATISTICS,UPDATE_TO_CUSTOM_STATISTICS)
(UPDATE_VARIABLE_DISPLAY, UPDATE_CUSTOM_STATISTICS,DELETE_CUSTOM_STATISTICS,UPDATE_TO_CUSTOM_STATISTICS, ADD_PROBLEM_SECTION)


class MetadataUpdateUtil(object):
Expand Down Expand Up @@ -88,6 +88,11 @@ def get_update_util(self, latest_metadata_or_err):
delete_util.delete_custom_stat()
return delete_util

elif self.update_type == ADD_PROBLEM_SECTION:
add_problem_util = ProblemSectionUtil(latest_metadata_or_err, self.update_json)
add_problem_util.problem_section_update()
return add_problem_util

else:
self.add_err_msg('Unknown update type: %s' % self.update_type)
return None
Expand All @@ -105,6 +110,8 @@ def make_update(self):
else:
metadata_obj = metadata_obj_or_err # to be clear



# Retrieve the metadata from a file; returned as an OrderedDict
#
data_found, latest_metadata_or_err = metadata_obj_or_err.get_metadata()
Expand Down
27 changes: 20 additions & 7 deletions preprocess_web/code/ravens_metadata_apps/preprocess_jobs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,28 @@ def add_problems_section(request):
if job_id is None:
return JsonResponse('%s is required' % col_const.PREPROCESS_ID)

success, latest_metadata_json_or_err = JobUtil.update_preprocess_problem_section(job_id, version, update_json_or_err)
if success is False:
# success, latest_metadata_json_or_err = JobUtil.update_preprocess_problem_section(job_id, version, update_json_or_err)
# if success is False:
# user_msg = dict(success=False,
# message=latest_metadata_json_or_err)
# return JsonResponse(user_msg)
metadata_update_or_err = MetadataUpdateUtil(job_id, update_json_or_err,
col_const.ADD_PROBLEM_SECTION)
if metadata_update_or_err.has_error:
msg = get_json_error(metadata_update_or_err.get_error_messages())
user_msg = dict(success=False,
message=latest_metadata_json_or_err)
return JsonResponse(user_msg)

return JsonResponse(latest_metadata_json_or_err)

message='Problem Section',
id=job_id,
data=msg)

else:
user_msg = dict(success=True,
message='Problem Section',
id=job_id,
data=metadata_update_or_err.get_updated_metadata())
print("Updated problem metadata : ", metadata_update_or_err)
print(user_msg)
return JsonResponse(user_msg)



Expand Down

0 comments on commit 98ab00d

Please sign in to comment.