def generate_results_page(study_id,final_rankings): print "generate_results_page" studyObj = Database.getStudy(study_id) results = { "study_id": study_id, "question": studyObj['study_question'], "ranking": [] } # TODO: Optimize this so that it doesn't run one query per # location just to fetch the place_id. study_places = set(map(str,studyObj['places_id'])) places = defaultdict(list) for rankedLocationID in final_rankings: locationObj = Database.getLocation(rankedLocationID) # Get the place_ids shared (set intersection) between the study and this location. # I'm assuming there'll only be one in common, since a location should appear # in a study only once. # place_id = list(study_places&set(locationObj['place_id']))[0] place_id = list(study_places&set(locationObj['place_id']))[0] score = final_rankings[rankedLocationID] places[place_id].append({ "location_id": rankedLocationID, "score": score, "loc": locationObj['loc'], "heading": locationObj['heading'], "pitch": locationObj['pitch'], "study_rank": len(places[place_id])+1 }) for place_id in places.keys(): places[place_id].sort(key=lambda x:x['score']) placeObj = Database.getPlace(place_id) placeRanking = { "name": placeObj.get('name'), "name_slug": slugify(placeObj.get('name')), "place_id": place_id, "rankings": places[place_id] } results['ranking'].append(placeRanking) Database.results.update({ "study_id": study_id, }, { "study_id": study_id, "results": results },True)
def generate_results_page(study_id, final_rankings): print "generate_results_page" studyObj = Database.getStudy(study_id) results = { "study_id": study_id, "question": studyObj['study_question'], "ranking": [] } # TODO: Optimize this so that it doesn't run one query per # location just to fetch the place_id. study_places = set(map(str, studyObj['places_id'])) places = defaultdict(list) for rankedLocationID in final_rankings: locationObj = Database.getLocation(rankedLocationID) # Get the place_ids shared (set intersection) between the study and this location. # I'm assuming there'll only be one in common, since a location should appear # in a study only once. # place_id = list(study_places&set(locationObj['place_id']))[0] place_id = list(study_places & set(locationObj['place_id']))[0] score = final_rankings[rankedLocationID] places[place_id].append({ "location_id": rankedLocationID, "score": score, "loc": locationObj['loc'], "heading": locationObj['heading'], "pitch": locationObj['pitch'], "study_rank": len(places[place_id]) + 1 }) for place_id in places.keys(): places[place_id].sort(key=lambda x: x['score']) placeObj = Database.getPlace(place_id) placeRanking = { "name": placeObj.get('name'), "name_slug": slugify(placeObj.get('name')), "place_id": place_id, "rankings": places[place_id] } results['ranking'].append(placeRanking) Database.results.update({ "study_id": study_id, }, { "study_id": study_id, "results": results }, True)
def rank_mongo(study_id=None): if study_id: study = Database.getStudy(study_id) else: study = Database.getRandomStudy() study_id = study.get('_id') print "processing study: %s" % study_id votes_selected = load_from_db(study_id) if len(votes_selected) == 0: print "no votes eligible, exiting" return output_file = "data/db.csv" print str(len(votes_selected)) + " eligible votes" #Set Image Hash images = set_image_hash(votes_selected) print str(len(images)) + " images in total" #Rank all images final_rankings = calculate_win_loss(images, votes_selected) # print "" # print "*** FINAL RANKINGS ***" # print final_rankings #Load Results to db print len(final_rankings) for location_id, q in final_rankings.iteritems(): if Database.updateQS(str(study_id),location_id,q) is None: print "Could not update score for %s" % location_id # Generate results page data generate_results_page(study_id, final_rankings) return 1
def rank_mongo(study_id=None): if study_id: study = Database.getStudy(study_id) else: study = Database.getRandomStudy() study_id = study.get('_id') print "processing study: %s" % study_id votes_selected = load_from_db(study_id) if len(votes_selected) == 0: print "no votes eligible, exiting" return output_file = "data/db.csv" print str(len(votes_selected)) + " eligible votes" #Set Image Hash images = set_image_hash(votes_selected) print str(len(images)) + " images in total" #Rank all images final_rankings = calculate_win_loss(images, votes_selected) # print "" # print "*** FINAL RANKINGS ***" # print final_rankings #Load Results to db print len(final_rankings) for location_id, q in final_rankings.iteritems(): if Database.updateQS(str(study_id), location_id, q) is None: print "Could not update score for %s" % location_id # Generate results page data generate_results_page(study_id, final_rankings) return 1
def load_from_db(study_id): # load votes from db votesCursor = Database.getVotes(str(study_id)) votes = [vote for vote in votesCursor] votes_selected = [] for vote in votes: reformatted_vote = {} reformatted_vote['id_left'] = vote.get('left') reformatted_vote['id_right'] = vote.get('right') if vote.get('choice') == 'left': reformatted_vote['winner'] = vote.get('left') elif vote.get('choice') == 'right': reformatted_vote['winner'] = vote.get('right') elif vote.get('choice') == 'equal': reformatted_vote['winner'] = '0' votes_selected.append(reformatted_vote) return votes_selected