def find_best_tree(self): if self.tree_scores and self.tree_scores[0]: return if not self.performance_on_train or not self.performance_on_train[0]: self.grow() if self.print_enabled: print "\t----- PERFORMANCES FOR ALL FFTs on Training Data -----" print PERFORMANCE + " \t" + self.criteria best = [-1, float('inf')] for i in range(self.tree_cnt): all_metrics = self.performance_on_train[i][self.tree_depths[i]] if self.criteria == "LOC_AUC": score = self.loc_aucs[i] else: score = get_score(self.criteria, all_metrics[:4]) self.tree_scores[i] = score self.dist2heavens[i] = get_score("Dist2Heaven", all_metrics[:4]) if score < best[-1]: best = [i, score] if self.print_enabled: print "\t" + "\t".join( ["FFT(" + str(i) + ")"] + \ [str(x).ljust(5, "0") for x in all_metrics[4:] + \ [score if self.criteria == "Dist2Heaven" else -score]]) if self.print_enabled: print "\tThe best tree found on training data is: FFT(" + str( best[0]) + ")" self.best = best[0] return best[0]
def eval_tree(self, t_id): if self.performance_on_test[t_id]: return depth = self.tree_depths[t_id] self.node_descriptions[t_id] = [[] for _ in range(depth + 1)] TP, FP, TN, FN = 0, 0, 0, 0 data = self.test for level in range(depth + 1): cue, direction, threshold, decision = self.selected[t_id][level] undecided, metrics, loc_auc = self.eval_decision( data, cue, direction, threshold, decision) tp, fp, tn, fn = self.update_metrics(level, depth, decision, metrics) TP, FP, TN, FN = TP + tp, FP + fp, TN + tn, FN + fn if len(undecided) == 0: break data = undecided pre, rec, spec, fpr, npv, acc, f1 = get_performance([TP, FP, TN, FN]) self.performance_on_test[t_id] = [ TP, FP, TN, FN, pre, rec, spec, fpr, npv, acc, f1 ] dist2heaven = get_score("Dist2Heaven", self.performance_on_test[t_id][:4]) loc_auc = -self.get_tree_loc_auc(self.test, t_id) self.results[t_id] = { "Accuracy": self.performance_on_test[t_id][9], "Dist2Heaven": dist2heaven, "LOC_AUC": loc_auc }
def index(): #create new session for each if 'num_correct' not in session: session['num_correct'] = 0 if 'num_total' not in session: session['num_total'] = 0 db_cursor = firebase.FirebaseApplication('https://dj-183-c7447.firebaseio.com/',None) score_list = db_cursor.get('/',None) if len(score_list) >= 5: score_list = OrderedDict(sorted(score_list.items(), key=operator.itemgetter(1), reverse=True)) n_items = take(5, score_list.iteritems()) print n_items score_list = {} score_list = Convert(n_items,score_list) else: score_list = OrderedDict(sorted(score_list.items(), key=operator.itemgetter(1), reverse=True)) score_list = OrderedDict(sorted(score_list.items(), key=operator.itemgetter(1), reverse=True)) data = { "genres": GENRES_LIST, "score_list": score_list } score = get_score() return render_template("index.html", score=score, **data)
def answer(): #get user input from choice player_answer = request.args["choice"] correct_choice = request.args["correct_choice"] genre_correct = request.args["correct_genre"] #compare correct answer with player answer #if player answer is correct, add to num_correct if player_answer == correct_choice: player_is_correct = True session['num_correct'] += 1 else: player_is_correct = False #regardless of correct or not, add one to total session['num_total'] += 1 data = { "our_song": correct_choice, "song": player_answer, "choice": correct_choice, "is_correct": player_is_correct, "num_total": session['num_total'], "num_correct": session['num_correct'], "genre_correct": genre_correct } score = get_score() return render_template("answer.html", score=score, **data)
def FFT1(k, train_data, train_labels, test_data, test_labels, metric): dic = {} dic1 = {} for i in metrics: fft = FFT(max_level=5) fft.criteria = i #fft.print_enabled=True train_labels = np.reshape(train_labels, (-1, 1)) test_labels = np.reshape(test_labels, (-1, 1)) training = np.hstack((train_data, train_labels)) testing = np.hstack((test_data, test_labels)) training_df = pd.DataFrame(training) testing_df = pd.DataFrame(testing) training_df.rename(columns={training_df.columns[-1]: "bug"}, inplace=True) testing_df.rename(columns={testing_df.columns[-1]: "bug"}, inplace=True) fft.target = "bug" fft.train, fft.test = training_df, testing_df fft.build_trees() # build and get performance on TEST data t_id = fft.find_best_tree() # find the best tree on TRAIN data fft.eval_tree(t_id) # eval all the trees on TEST data description = fft.print_tree(t_id) if i != 'Dist2Heaven': dic[i] = fft.performance_on_test[t_id][metrics_dic[i]] else: dic["Dist2Heaven"] = get_score("Dist2Heaven", fft.performance_on_test[t_id][:4]) dic1[i] = description return dic[metric], [dic, dic1]
def grow(self, data, t_id, level, cur_performance): """ :param data: current data for future tree growth :param t_id: tree id :param level: level id :return: None """ if level >= self.max_depth: return if len(data) == 0: print "?????????????????????? Early Ends ???????????????????????" return # print "level, ", level self.tree_depths[t_id] = level decision = self.structures[t_id][level] structure = tuple(self.structures[t_id][:level + 1]) #print(t_id, level, structure) cur_selected = self.computed_cache.get(structure, None) Y = data.as_matrix(columns=[self.target]) if not cur_selected: for cue in list(data): if cue in self.ignore or cue == self.target: continue if (self.median_top == 1 and level == 0) or (self.median_top == 0): threshold = data[cue].median() else: threshold = data[cue] for direction in "><": undecided, metrics, loc_auc = self.eval_decision( data, cue, direction, threshold, decision) tp, fp, tn, fn = self.update_metrics( level, self.max_depth, decision, metrics) # if the decision lead to no data, punish the score if sum([tp, fp, tn, fn]) == 0: score = float('inf') elif self.criteria == "LOC_AUC": score = loc_auc else: score = get_score(self.criteria, [TP + tp, FP + fp, TN + tn, FN + fn]) # score = get_score(self.criteria, metrics) # if not cur_selected or metrics[goal] > self.performance_on_train[t_id][level][cur_selected][goal]: if not cur_selected or score < cur_selected['score']: cur_selected = {'rule': (cue, direction, threshold, decision), \ 'undecided': undecided, \ 'metrics': [TP + tp, FP + fp, TN + tn, FN + fn], \ # 'metrics': metrics, 'score': score} x = 1 self.computed_cache[structure] = cur_selected self.selected[t_id][level] = cur_selected['rule'] self.performance_on_train[t_id][level] = cur_selected[ 'metrics'] + get_performance(cur_selected['metrics']) self.grow(cur_selected['undecided'], t_id, level + 1, cur_selected['metrics'])
def eval_range_split(self, level, cur_selected, cur_performance, data, cue, indexes, interval, decision): TP, FP, TN, FN = cur_performance pos = data.iloc[indexes] neg = data.iloc[~data.index.isin(indexes)] if decision == 1: decided = pos undecided = neg else: pos, neg = neg, pos decided = neg undecided = pos # get auc for loc. if "loc" in data: sorted_data = pd.concat([ df.sort_values(by=["loc"], ascending=True) for df in [pos, neg] ]) loc_auc = get_auc(sorted_data) else: loc_auc = 0 tp = pos.loc[pos[self.target] == 1] fp = pos.loc[pos[self.target] == 0] tn = neg.loc[neg[self.target] == 0] fn = neg.loc[neg[self.target] == 1] metrics = map(len, [tp, fp, tn, fn]) tp, fp, tn, fn = self.update_metrics(level, self.max_depth, decision, metrics) # if the decision lead to no data, punish the score if sum([tp, fp, tn, fn]) == 0: score = float('inf') elif self.criteria == "LOC_AUC": score = loc_auc else: score = get_score(self.criteria, [TP + tp, FP + fp, TN + tn, FN + fn]) if not cur_selected or score < cur_selected['score']: direction = "inside" if decision else "outside" cur_selected = {'rule': (cue, direction, interval, decision),\ 'undecided': undecided,\ 'metrics': [TP + tp, FP + fp, TN + tn, FN + fn],\ 'score': score} ''' if self.sorted_cues: self.sorted_cues = [cur_selected].extend(self.sorted_cues) else: self.sorted_cues = [cur_selected] else: direction = "inside" if decision else "outside" new_cue = {'rule': (cue, direction, interval, decision), 'undecided': undecided, 'metrics': [TP + tp, FP + fp, TN + tn, FN + fn], 'score': score} self.sorted_cues.append(new_cue) ''' return cur_selected
def confirm(): final_score = get_score() if session.get('num_total') == 0: clear_score() final_score = "N/A" data = {"final_score": final_score} return render_template("confirm.html", **data)
def index(): leaderboard = firebase.FirebaseApplication( 'https://dj183-6a1a1.firebaseio.com/') clearing_score = request.form.get("choice") if clearing_score == "yes": temp_score = session["num_correct"] if request.form.get("username") == "": pass else: temp_username = request.form.get("username") leaderboard.put('/Highscores', temp_username, temp_score) clear_score() final_score = get_score() if session.get('num_total') > 0: temp = 0 else: clear_score() final_score = "N/A" highscores = leaderboard.get('/Highscores', None) sorted_highscores = sorted(highscores.items(), key=operator.itemgetter(1), reverse=True) while len(sorted_highscores) > 5: sorted_highscores.pop() if request.form.get("comment_song") != None: song_comment = request.form.get("comment_song") song_to_comment = request.form.get("answer_song") leaderboard.put('/Comments', song_to_comment, song_comment) difficulty = ["Easy", "Normal", "Difficult"] data = { "genres": GENRES_LIST, "final_score": final_score, "sorted_highscores": sorted_highscores, "difficulty": difficulty } return render_template("index.html", **data)
def free_bacon(score): """Return the points scored from rolling 0 dice (Free Bacon). score: The opponent's current score. """ assert score < 100, 'The game should be over.' score_list = to_list(score) if (len(score_list) == 1): return 1 sum = 1 for s in score_list: sum *= s return get_score(sum)
def eval_point_split(self, level, cur_selected, cur_performance, data, cue, direction, threshold, decision): TP, FP, TN, FN = cur_performance undecided, metrics, loc_auc = self.eval_decision( data, cue, direction, threshold, decision) tp, fp, tn, fn = self.update_metrics(level, self.max_depth, decision, metrics) # if the decision lead to no data, punish the score if sum([tp, fp, tn, fn]) == 0: score = float('inf') elif self.criteria == "LOC_AUC": score = loc_auc else: score = get_score(self.criteria, [TP + tp, FP + fp, TN + tn, FN + fn]) if not cur_selected or score < cur_selected['score']: cur_selected = {'rule': (cue, direction, threshold, decision),\ 'undecided': undecided,\ 'metrics': [TP + tp, FP + fp, TN + tn, FN + fn], \ 'score': score} return cur_selected
def recordscore(): db_cursor = firebase.FirebaseApplication('https://dj-183-c7447.firebaseio.com/',None) username = request.args['username'] score_list = db_cursor.get('/',None) print score_list score = get_score() score.replace(" ", "") index = score.find('/') num_correct = int(score[0:index]) print "num_correct = ", num_correct if username in score_list: user_score = db_cursor.get('/',username) prev_correct = int(user_score) if num_correct > prev_correct: print "new score > old score" db_cursor.put('/',username,num_correct) else: print "added new user + score to database" db_cursor.put('/',username,num_correct) clear_score() return render_template("recordscore.html")
def question(genre): if session.get('difficulty_level') == "Normal": choice = random.randrange(0,4) songs = get_four_songs(genre) try: url = get_preview_url(songs[choice].title, songs[choice].artist)["url"] except: songs = get_four_songs(genre) url = get_preview_url(songs[choice].title, songs[choice].artist)["url"] elif session.get('difficulty_level') == "Easy": choice = random.randrange(0,3) songs = get_three_songs(genre) try: url = get_preview_url(songs[choice].title, songs[choice].artist)["url"] except: songs = get_three_songs(genre) url = get_preview_url(songs[choice].title, songs[choice].artist)["url"] else: choice = random.randrange(0,5) songs = get_five_songs(genre) try: url = get_preview_url(songs[choice].title, songs[choice].artist)["url"] except: songs = get_five_songs(genre) url = get_preview_url(songs[choice].title, songs[choice].artist)["url"] final_score = get_score() if session.get('num_total') == 0: clear_score() final_score = "N/A" return render_template("question.html", songs=songs, url=url, choice=choice, genre=genre, final_score=final_score)
def print_tree(self, t_id): data = self.test depth = self.tree_depths[t_id] if not self.node_descriptions[t_id]: self.node_descriptions[t_id] = [[] for _ in range(depth + 1)] for i in range(depth + 1): if self.node_descriptions[t_id][i]: print self.node_descriptions[t_id][i][0] else: cue, direction, threshold, decision = self.selected[t_id][i] undecided, metrics, loc_auc = self.eval_decision( data, cue, direction, threshold, decision) description = self.describe_decision(t_id, i, metrics) self.node_descriptions[t_id][i] += [description] print description if len(undecided) == 0: break data = undecided description = self.describe_decision(t_id, i, metrics, reversed=True) self.node_descriptions[t_id][i] += [description] dist2heaven = get_score("Dist2Heaven", self.performance_on_test[t_id][:4]) loc_auc = -self.get_tree_loc_auc(self.test, t_id) self.results[t_id] = {"d2h": dist2heaven, "auc": loc_auc} if self.print_enabled: print self.node_descriptions[t_id][i][1] print "\t----- CONFUSION MATRIX -----" print MATRIX print "\t" + "\t".join(map(str, self.performance_on_test[t_id][:4])) print "\t----- PERFORMANCES ON TEST DATA -----" print PERFORMANCE + " \tD2H" + " \tLOC" print "\t" + "\t".join( ["FFT(" + str(self.best) + ")"] + \ [str(x).ljust(5, "0") for x in self.performance_on_test[t_id][4:11] + [dist2heaven, loc_auc]])
def question(): chart_title = request.args["chart_name"] chart_name = GENRES_LIST[chart_title] #initialize url to error urlin = {"error": "Song not found"} #keep getting new url until no error while "error" in urlin: songs_chosen = get_four_songs(chart_name) index = random.randint(0, 3) correct_song = songs_chosen[index] urlin = get_preview_url(correct_song.title, correct_song.artist) song_url = urlin["url"] data = { "song_answers": songs_chosen, "chartname": chart_name, "charttitle": chart_title, "songurl_correct": song_url, "correct_song": correct_song, } score = get_score() return render_template("question.html", score=score, **data)
def askname(): score = get_score() return render_template("askname.html",score=score)
def clear(): score = get_score() return render_template("clear.html", score=score)
def answer(): leaderboard = firebase.FirebaseApplication('https://dj183-6a1a1.firebaseio.com/') answer = request.args.get('answer', None) genreAgain = request.args.get('genreAgain', None) user_input = request.form['song'] if (answer == user_input): result = 'Correct!' session['num_correct'] += 1 session['num_total'] += 1 if leaderboard.get('/Percent', answer) != None: song_correct = leaderboard.get('/Correct', answer) + 1 leaderboard.put('/Correct', answer, song_correct) song_total = leaderboard.get('/Total', answer) + 1 leaderboard.put('/Total', answer, song_total) song_percent = float(song_correct) / song_total leaderboard.put('/Percent', answer, song_percent) else: song_correct = 1 song_total = 1 leaderboard.put('/Correct', answer, song_correct) leaderboard.put('/Total', answer, song_total) song_percent = float(song_correct) / song_total leaderboard.put('/Percent', answer, song_percent) else: result = 'Incorrect!' session['num_total'] += 1 if leaderboard.get('/Percent', answer) != None: song_correct = leaderboard.get('/Correct', answer) song_total = leaderboard.get('/Total', answer) + 1 leaderboard.put('/Total', answer, song_total) song_percent = float(song_correct) / song_total leaderboard.put('/Percent', answer, song_percent) else: song_correct = 0 song_total = 1 leaderboard.put('/Correct', answer, song_correct) leaderboard.put('/Total', answer, song_total) song_percent = float(song_correct) / song_total leaderboard.put('/Percent', answer, song_percent) final_score = get_score() final_song_percent = leaderboard.get('/Percent', answer) * 100 indiv_song_comment = leaderboard.get('/Comments', answer) if request.form.get('song_like') == None: pass else: prev_song_like = request.form.get('song_like') if prev_song_like == "yes": if leaderboard.get('/Likes', answer) != None: current_song_likes = leaderboard.get('/Likes', answer) current_song_likes += 1 leaderboard.put('/Likes', answer, current_song_likes) else: current_song_likes = 1 leaderboard.put('/Likes', answer, current_song_likes) if leaderboard.get('/Likes', answer) == None: current_song_likes = 0 else: current_song_likes = leaderboard.get('/Likes', answer) wrong_percent = 100 - final_song_percent data = { 'answer': answer, 'user_input': user_input, 'result': result, 'genre': genreAgain, 'final_score': final_score, 'final_song_percent': final_song_percent, 'indiv_song_comment': indiv_song_comment, 'current_song_likes': current_song_likes, 'wrong_percent': wrong_percent } # print("PRINT SOMETHING PLEASE") # print(songs[choice].title) # data = { # 'song': request.form['song'] # } # if request.form['song'] == x[choice].title: # print('yay') # else: # print('NAY') # if songs[choice].title == request.form['song']: # print("yay") # else: # print("NAY") return render_template("answer.html", **data)