Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the code necessary for review bidding webservice #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!flask/bin/python
from flask import Flask
import flask
from flask import Flask, jsonify, abort, request, make_response, url_for
import json_unpacker
import matching_model
from user import User
from team import Team
import user
import json
import clustering as clst
import top_trading_cycles as ttc

def extract_users(req):
exper_data,users = ([],[])
Expand All @@ -21,12 +23,12 @@ def extract_users(req):
def send_teams_as_json(teams): #this method currently uses the classes defined for bidding
json_obj = [[user.pid for user in team.members] for team in teams]
return flask.Response(json.dumps({"teams":json_obj,"users":flask.request.json['users']}), mimetype='application/json')

def extract_task_data(req):
#extract json data and convert to python object here
#do not necessarily have to use user class here, it is already defined if you would like to use it
return req

def send_assigned_tasks_as_json(tasks):
#convert python objects to simple maps and lists
return flask.Response(json.dumps({"info":tasks}))
Expand All @@ -41,14 +43,16 @@ def clstbuild():
teams,users = clst.kmeans_assignment(data,users, flask.request.json['max_team_size'])
return send_teams_as_json(teams)

@app.route('/assign_tasks',methods=['POST']) #Add topic code here
def ttctrading():
if not 'users' in flask.request.json or not 'teams' in flask.request.json or sum([not 'history' in user or not 'ranks' in user or not 'pid' in user for user in flask.request.json['users']]) > 0: #check for required fields in json request here
flask.abort(400)
users = extract_task_data(flask.request.json) #extract json data into necessary format

assignments = ttc.team_swap(users) #method where assignment algorithm is run
return send_assigned_tasks_as_json(assignments) #returning a flask response object
@app.route("/match", methods=['POST']) #using the post method with /match in the url to get the required app route
def matching():
if not request.json: #will abort the request if it fails to load the json
abort(400) #will have a return status of 400 in case of failure
bidding_data = json_unpacker.JsonUnpacker(request.json) #calles the json_unpacker to get the necessary bidding_data
model = matching_model.MatchingModel(bidding_data.student_ids,
bidding_data.topic_ids,
bidding_data.student_preferences_map,
bidding_data.topic_preferences_map, bidding_data.q_S) #model to get the student_ids,topic_ids,student_preference_map,topic_prefernce_map
return jsonify(model.get_matching()) #returns a json object

if __name__ == "__main__":
app.run(debug=True)
app.run(debug=True)
94 changes: 94 additions & 0 deletions app/json_unpacker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import json
import operator
from random import shuffle
import random
from copy import deepcopy

class JsonUnpacker:
"""
Class for unpacking bidding data from JSON Object.

ATTRIBUTES:

----------

json_dict : dict
The JSON object represented as a python dictionary.

topic_ids : list
{String, String, ....}
A list of the topic_ids of all the topics in the assignment.

student_ids : list
{String, String, ....}
A list containing all the Student IDs.

student_preferences_map : dict
{String : [String, String, ....], String : [String, String, ....], ....}
A dictionary that maps a student ID to a list of topic IDs in the linear
order of the student's preference.
key : Student ID
value : list of topic IDs in the linear order of student's preference.

topic_preferences_map : dict
{String : [String, String, ....], String : [String, String, ....], ....}
A dictionary that maps a topic ID to a list of students in the linear
order of the topic's preference.
key : Topic ID
value : list of student IDs in the linear order of topic's preference.
"""
def __init__(self,data):
self.json_dict = data
self.topic_ids = self.json_dict['tids']
self.student_ids = list(self.json_dict['users'].keys())
self.student_preferences_map = self.gen_stud_pref_map(self.json_dict)
self.topic_preferences_map = self.gen_topic_pref_map(self.json_dict)
self.q_S = int(self.json_dict['q_S'])

def gen_stud_pref_map(self,json_dict):
student_preferences_map = dict()
for student_id in self.student_ids:
chosen_topic_ids = json_dict['users'][student_id]['tid']
if(len(chosen_topic_ids) == 0):
self_topic = json_dict['users'][student_id]['otid']
student_preferences_map[student_id] = deepcopy(self.topic_ids)
shuffle(student_preferences_map[student_id])
if self_topic in student_preferences_map[student_id]:
student_preferences_map[student_id].remove(self_topic)
student_preferences_map[student_id].append(self_topic)
self.json_dict['users'][student_id]['priority'] = random.sample(range(1, len(student_preferences_map[student_id])+1), len(student_preferences_map[student_id]))
self.json_dict['users'][student_id]['time'] = [0]
else:
chosen_topic_priorities = json_dict['users'][student_id]['priority']
student_preferences_map[student_id] = [x for x,_ in
sorted(zip(chosen_topic_ids,
chosen_topic_priorities))]
unchosen_topic_ids = list(set(self.topic_ids).difference(set(
chosen_topic_ids)))
self_topic = json_dict['users'][student_id]['otid']
if self_topic in unchosen_topic_ids:
unchosen_topic_ids.remove(self_topic)
student_preferences_map[student_id] += unchosen_topic_ids
student_preferences_map[student_id].append(self_topic)
return student_preferences_map

def gen_topic_pref_map(self,json_dict):
topic_preferences_map = dict()
for topic_id in self.topic_ids:
topic_preferences_map[topic_id] = []
for student_id in self.student_ids:
if(topic_id in self.student_preferences_map[student_id]):
timestamp = max(json_dict['users'][student_id]['time'])
topic_priority = self.student_preferences_map[
student_id].index(topic_id)
num_chosen_topics = len(json_dict['users'][student_id][
'tid'])
topic_preferences_map[topic_id].append((student_id,
topic_priority,
num_chosen_topics,
timestamp))
topic_preferences_map[topic_id].sort(key = operator.itemgetter(1,2,
3))
topic_preferences_map[topic_id] = [x for x,_,_,_ in
topic_preferences_map[topic_id]]
return topic_preferences_map
Loading