def update_elo(): sys.stdout.write("Query matches...") matches = Match1on1.query().all() sys.stdout.write("\rBeginning to update %d matches" % len(matches)) print "" # constants conf = utils.get_config(args) k = conf["elo.chess.k"] func = conf["elo.chess.function"] # Query all ratings and store it in a dictionary. This is done to store # the newest rating data in memory. We do not have to commit. ratings = Rank_Elo.query().all() rdict = {} for r in ratings: rdict[r.player_id.value] = r updates = 0 for match in matches: rating1 = rdict[match.player1.value] rating2 = rdict[match.player2.value] result = elo.elo1on1(rating1.value.value, rating2.value.value, match.outcome.value, k, func) rating1.value = result[0] rating2.value = result[1] updates = updates + 1 if updates % 50 == 0: sys.stdout.write("\r" + "Updated %d matches..." % updates) sys.stdout.flush() # update table for r in ratings: r.save(commit=False) Rank_Elo.commit() print "\rUpdated", updates, "matches."
def predict(args): """ Reads match constellations from an input file and writes the results to a specified output file. :param args: A list with arguments from the argument parser :type args: namespace """ if os.path.abspath(args.ifile) == os.path.abspath(args.ofile): print "You tried to overwrite your input with your output file." print "Aborted." return if args.algorithm == "glicko": print "Not implemented yet" return try: modes = {True: "incremental", False: "non-incremental"} print "Predicting the matches in %s mode" % modes[args.incremental] print "Open %s and write into %s..." % (args.ifile, args.ofile) csvfile, reader, hasHeader = utils.get_csv(args.ifile) ofile = open(args.ofile, 'w') line = 0 if hasHeader: print "\tFirst line of csv file is ignored. It seems to be a " \ "header row.\n" # constants conf = utils.get_config(args) k = conf["elo.chess.k"] func = conf["elo.chess.function"] # Query all ratings and store it in a dictionary. This is done for # faster access and on-the-fly calculation of new elo values ratings = Rank_Elo.query().all() rdict = {} for r in ratings: player = Player.query().get(player_id=r.player_id.value) rdict[player.nickname.value] = r for row in reader: if line != 0 or (line == 0 and not hasHeader): ratings = (rdict.get(row[1], None), rdict.get(row[2], None)) if ratings[0] is None or ratings[1] is None: continue value1 = ratings[0].value.value value2 = ratings[1].value.value if (value1 > value2): outcome = 1 elif (value1 < value2): outcome = 0 else: outcome = 0.5 if args.incremental: result = elo.elo1on1(value1, value2, outcome, k, func) ratings[0].value = result[0] ratings[1].value = result[1] ofile.write("%s,%s,%s,%s\n" % (row[0], row[1], row[2], str(outcome))) elif line == 0 and hasHeader: ofile.write('"%s","%s","%s","Statistically most possible ' \ 'outcome"\n' % (row[0], row[1], row[2])) if line % 13 == 0: sys.stdout.write("\rWrote %d entries to the out file" % line) sys.stdout.flush() line = line + 1 print "\rWrote %d entries to the out file" % (line - (1 if hasHeader else 0)) csvfile.close() ofile.close() except csv.Error: print "Error importing %s in line %d" % (args.ifile, line) except IOError: print "One file is missing. Either %s or %s" % (args.ifile, args.ofile)
def predict(args): """ Reads match constellations from an input file and writes the results to a specified output file. :param args: A list with arguments from the argument parser :type args: namespace """ if os.path.abspath(args.ifile) == os.path.abspath(args.ofile): print "You tried to overwrite your input with your output file." print "Aborted." return if args.algorithm == "glicko": print "Not implemented yet" return try: modes = {True: "incremental", False: "non-incremental"} print "Predicting the matches in %s mode" % modes[args.incremental] print "Open %s and write into %s..." % (args.ifile, args.ofile) csvfile, reader, hasHeader = utils.get_csv(args.ifile) ofile = open(args.ofile, 'w') line = 0 if hasHeader: print "\tFirst line of csv file is ignored. It seems to be a " \ "header row.\n" # constants conf = utils.get_config(args) k = conf["elo.chess.k"] func = conf["elo.chess.function"] # Query all ratings and store it in a dictionary. This is done for # faster access and on-the-fly calculation of new elo values ratings = Rank_Elo.query().all() rdict = {} for r in ratings: player = Player.query().get(player_id=r.player_id.value) rdict[player.nickname.value] = r for row in reader: if line != 0 or (line == 0 and not hasHeader): ratings = (rdict.get(row[1], None), rdict.get(row[2], None)) if ratings[0] is None or ratings[1] is None: continue value1 = ratings[0].value.value value2 = ratings[1].value.value if (value1 > value2): outcome = 1 elif (value1 < value2): outcome = 0 else: outcome = 0.5 if args.incremental: result = elo.elo1on1(value1, value2, outcome, k, func) ratings[0].value = result[0] ratings[1].value = result[1] ofile.write("%s,%s,%s,%s\n" % (row[0], row[1], row[2], str(outcome))) elif line == 0 and hasHeader: ofile.write('"%s","%s","%s","Statistically most possible ' \ 'outcome"\n' % (row[0], row[1], row[2])) if line % 13 == 0: sys.stdout.write("\rWrote %d entries to the out file" % line) sys.stdout.flush() line = line + 1 print "\rWrote %d entries to the out file" % ( line - (1 if hasHeader else 0)) csvfile.close() ofile.close() except csv.Error: print "Error importing %s in line %d" % (args.ifile, line) except IOError: print "One file is missing. Either %s or %s" % (args.ifile, args.ofile)