-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
120 lines (103 loc) · 4.04 KB
/
handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import json
import numpy as np
from src.catsimUtils import *
## NOTE: Difficulty map (add more if required)
DIFFICULTY_MAP = {
'Easy': -1,
'Medium': 0,
'Hard': 1
}
## NOTE: Score multiplication factor (remove if not required)
SCORE_MUL_FACTOR = {
'Easy': 0.25,
'Medium': 0.5,
'Hard': 1
}
def getNextTestItem(event, context):
# print("\n\nRequest Body:\n", event["body"], "\n\n")
# initialize response object
response = {
"headers": {
"Access-Control-Allow-Origin": "*"
},
"statusCode": 500,
"body": json.dumps({ "error": "empty function" })
}
try:
# extract body object from json
body = json.loads(event["body"])
except Exception as e:
print(e)
response.update({ "body": json.dumps({ "error": "Failed to parse POST data" }) })
return response
try:
# fetch testItemsMap (super set) from json
with open('./src/testItemsData.json') as f:
rawTestItems = json.load(f)
testItems = [
{
**i,
'score': i['score'] * SCORE_MUL_FACTOR[i['difficulty']],
'arrayValues': [
i['score'] * SCORE_MUL_FACTOR[i['difficulty']],
DIFFICULTY_MAP[i['difficulty']],
0,
1
]
} for i in rawTestItems
]
testItemIds = [i['testItemId'] for i in testItems]
visitedItemIds, responses = [], []
estimatedProficiency = getInitialProficiency()
nextTestItemId, nextCorrectProbability = '', None
if 'testItemIds' in body:
testItemIds = body['testItemIds']
if len(testItemIds):
# testItems: curate testItems subset using testItemIds from request body
testItems = [i for i in testItems if i['testItemId'] in testItemIds]
# testItemsArray: generate numpy 2D-array for testItems
testItemsArray = np.array([i['arrayValues'] for i in testItems])
if 'visitedItemIds' in body:
visitedItemIds = body['visitedItemIds']
# visitedItemIndices: get indices for visitedItemIds from testItems
visitedItemIndices = [i for (i, j) in enumerate(testItems) if j['testItemId'] in visitedItemIds]
if 'visitedItemResponses' in body:
responses = body['visitedItemResponses']
# currentScore: get the current cumulative score for visitedItems
currentScore = sum([testItems[j]['score'] for (i, j) in enumerate(visitedItemIndices) if responses[i] == True])
# update currentProficiency if there are valid visitedItems
if 'currentProficiency' in body and len(visitedItemIndices):
currentProficiency = body['currentProficiency']
estimatedProficiency = getEstimatedProficiency(testItemsArray, visitedItemIndices, responses, currentProficiency)
if estimatedProficiency == float('inf'):
estimatedProficiency = 100
elif estimatedProficiency == float('-inf'):
estimatedProficiency = -100
# get max count for visited items
maxVisitedItemsCount = len(testItemsArray)
if 'maxVisitedItemsCount' in body:
maxVisitedItemsCount = body['maxVisitedItemsCount']
# check for stopFlag before calculating next item
stopFlag = getStopFlag(testItemsArray, visitedItemIndices, currentProficiency, maxVisitedItemsCount)
if stopFlag == False:
nextTestItemIndex = getNextItemIndex(testItemsArray, visitedItemIndices, currentProficiency)
# nextTestItemId: get testItemId for testItemIndex from testItems
nextTestItemId = testItems[nextTestItemIndex]['testItemId']
nextItemScore = testItems[nextTestItemIndex]['score']
nextTestItemArray = np.array(testItems[nextTestItemIndex]['arrayValues'])
# nextCorrectProbability = getCorrectProbability(nextTestItemArray)
# update and return response
response.update({
"statusCode": 200,
"body": json.dumps({
"testItemId": nextTestItemId,
"itemScore": nextItemScore,
"currentProficiency": estimatedProficiency,
"currentScore": currentScore
})
})
return response
except Exception as e:
print(e)
response.update({ "body": json.dumps({ "error": str(e) }) })
return response