def write_la_liga_on_brasileiro_format(gameFilePath,year): def player_score(score): return score[1] with open(abspath(gameFilePath),'r') as filestream: open_csv = csv.reader(filestream) players = get_players(open_csv) #print(year,len(players)) with open(abspath(gameFilePath),'r') as filestream: open_csv = csv.reader(filestream) game_rounds = get_game_rounds(open_csv,players) #print(year,len(game_rounds)) for round_ in game_rounds: round_.sort(reverse=True,key=player_score) with open(abspath('data/espanhol/simples'+year),'w') as filestream: filestream.write("[") number_of_game_rounds = len(game_rounds) for count in range(0,number_of_game_rounds): game_round = game_rounds[count] filestream.write("[") sz = len(game_round) for index in range(0,sz): score = game_round[index] filestream.write("[") filestream.write('"') filestream.write(score[0]) filestream.write('"') filestream.write(",") filestream.write(str(score[1])) filestream.write("]") if(index < sz-1): filestream.write(",") filestream.write("]") if count < number_of_game_rounds-1 : filestream.write(",") filestream.write("]")
def calculaESalvaIncerteza(games, indiceVariante): incertezaEntropia = [] incertezaPDD = [] for game in games: incertezaEntropia.append(calculaIncertezaEntropia(game)) incertezaPDD.append(calculaIncertezaPDD(game)) salvaArquivoValores(incertezaEntropia, abspath('code_pac/dncertezaentropia_grupo'+ indiceVariante +'.txt')) salvaArquivoValores(incertezaPDD, abspath('code_pac/dncertezapdd_grupo'+ indiceVariante +'.txt'))
def calculaESalvaDramas(games, indiceVariante): dramasPontos = [] dramasPosicao = [] dramasCaminho = [] for game in games: dramasPontos.append(calculaDramaPorPontos(game)) dramasPosicao.append(calculaDramaPorPosicao(game)) dramasCaminho.append(calculaDramaPorCaminho(game)) salvaArquivoValores(dramasPontos, abspath('code_pac/dramaporpontos_grupo'+ indiceVariante +'.txt')) salvaArquivoValores(dramasPosicao, abspath('code_pac/dramaporposicao_grupo'+ indiceVariante +'.txt')) salvaArquivoValores(dramasCaminho, abspath('code_pac/dramaporcaminho_grupo'+ indiceVariante +'.txt'))
def listPremierLeagueGames(self): r = [] folder = abspath(self.parser.get('folder ingles', 'folder')) for f in os.listdir(folder): if f.startswith("simples"): r.append(folder + os.sep + f) return r
def listPrimeiraLigaGames(self): r = [] folder = abspath(self.parser.get('folder portugal', 'folder')) for f in os.listdir(folder): if f.startswith("simples"): r.append(folder + os.sep + f) return r
def listLegaNazionaleGames(self): r = [] folder = abspath(self.parser.get('folder italiano', 'folder')) for f in os.listdir(folder): if f.startswith("simples"): r.append(folder + os.sep + f) return r
def listBunsligaGames(self): r = [] folder = abspath(self.parser.get('folder alemao', 'folder')) for f in os.listdir(folder): if f.startswith("simples"): r.append(folder + os.sep + f) return r
def listPontosCorridosGames(self): r = [] countries = [ 'brasileiro', 'ingles', 'espanhol', 'italiano', 'alemao', 'portugal' ] for country in countries: folder = abspath(self.parser.get('folder ' + country, 'folder')) for f in os.listdir(folder): if f.startswith("simples"): r.append(folder + os.sep + f) return r
if __name__ == '__main__': import matplotlib.pyplot as plt from matplotlib.backends.backend_pdf import PdfPages from openpyxl import Workbook wb = Workbook() for players in range(3, 7): for gRounds in range(3, 9): for target in range(1, 5): if target > players - 1: break identiS = str(players) + '_r' + str(gRounds) + '_t' + str( target) pp = PdfPages( abspath('multi_testes/envio/graficos_p' + identiS + '.pdf')) planilha = wb.create_sheet(title=identiS) paths = pathMaker(players, gRounds, target) values = [] for path in paths: #print path drama = pathDrama(path, players, target) values.append(drama) planilha.append([drama] + path) values = sorted(values) #print values[:] titleGr = 'Players=' + str(players) + " | Rounds=" + str( gRounds) + " | target=" + str(target) plt.figure() plt.title(titleGr) #plt.subplot(131)
def salvarDados(metrica, listaValores, indiceVariante): with open(abspath("dados_grupo" + indiceVariante + ".txt"), 'a') as fp: fp.write('{} {} {} {}\n'.format(metrica, len(listaValores), np.mean(listaValores), np.std(listaValores)))
''' Created on 11/07/2015 @author: mangeli ''' from __future__ import division import math import matplotlib.pyplot as plt from openpyxl import Workbook from openpyxl.reader.excel import load_workbook from GameQualityAssessment.project_path import make_absolute_path as abspath if __name__ == '__main__': wb = load_workbook(filename=abspath('multi_testes/envio/dados.xlsx'), read_only=True) ws = wb['5_r5_t1'] clean = [] durty = [] for row in ws.rows: if row[1].value == 1 or row[2].value == 1 or row[3].value == 1 or row[ 4].value == 1: durty.append(row[0].value) else: clean.append(row[0].value) print(len(clean), len(durty)) plt.figure('clean') plt.hist(clean) plt.xlim((0, 1)) plt.figure('durty') plt.hist(durty) plt.xlim((0, 1))
def __init__(self): self.parser = configparser.ConfigParser() f = open(abspath('code_pac/gameAnalyzer.ini'), 'r') self.parser.readfp(f) f.close()
def listDiceGames(self): r = [] folder = abspath(self.parser.get('folder dado', 'folder')) for f in os.listdir(folder): r.append(folder + os.sep + f) return r
#data = data.decode("utf-8") # ... and reencode it into the target encoding #data = self.encoder.encode(data) # write to the target stream self.stream.write(data) # empty queue self.queue.truncate(0) def writerows(self, rows): for row in rows: self.writerow(row) if __name__ == '__main__': anos = range(2003, 2015) f = open(abspath('code_pac/brasileiro/tabela_resultados_full.csv'), 'w') #f2 = open(abspath('code_pac/brasileiro/tabela_resultados.csv'), 'w') arq = UnicodeWriter(f) arq_r = UnicodeWriter(f) header = [ 'Edition', 'Teams No.', 'Rounds No.', 'Winner', 'Winner final score', 'Winner effectiveness' ] arq.writerow(header) for ano in anos: entrada = open(abspath('data/brasileiro/raw_data/full' + str(ano)), 'r') parser = BeautifulSoup(entrada.read(), features="html.parser") entrada.close() rodadas = parser.find_all('div', 'rodada-tabela')
round_.sort(reverse=True,key=player_score) with open(abspath('data/ingles/simples'+year),'w') as filestream: filestream.write("[") number_of_game_rounds = len(game_rounds) for count in range(0,number_of_game_rounds): game_round = game_rounds[count] filestream.write("[") sz = len(game_round) for index in range(0,sz): score = game_round[index] filestream.write("[") filestream.write('"') filestream.write(score[0]) filestream.write('"') filestream.write(",") filestream.write(str(score[1])) filestream.write("]") if(index < sz-1): filestream.write(",") filestream.write("]") if count < number_of_game_rounds-1 : filestream.write(",") filestream.write("]") if __name__ == "__main__": configreader = configReader.ConfigReader() folder = abspath(configreader.parser.get('folder ingles','folder')) for fil in os.listdir(folder): if fil.startswith("premier-league-") : path = folder + os.sep + fil write_premierleague_on_brasileiro_format(path,path[-8:-4]) pass
dramaPoints = DramaByPointsUp2First(game=genGame, ignored=0, normScores=True).getMeasureValue() dramaPosition = DramaByPositionUp2First(game=genGame, ignored=0).getMeasureValue() planilha.append([game.year, dramaPath, dramaPoints, dramaPosition]) winner = genGame.getWinner() nPlayers = len(genGame.getPlayers()) nRounds = genGame.getNumberRounds() winnerPath = [] for i in range(1, nRounds + 1): gameRound = genGame.getRound(i)[1] for r in gameRound: if r.playerCode == winner: winnerPath.append(gameRound.index(r)+1) break plt.figure(game.year) plt.plot(np.arange(1,nRounds+1), winnerPath, '-b', linewidth=1.5) plt.xlim(1,nRounds) plt.ylim(0, nPlayers) plt.yticks(np.arange(1, nPlayers, 3)) plt.gca().invert_yaxis() plt.gca().set_title(str(game.year)) fString = '{0:.4f}' plt.gca().text(0.63,0.02,'Drama by Path: ' + fString.format(dramaPath) + '\n' + 'Drama by Points: ' + fString.format(dramaPoints) + '\n' + 'Drama by Position: ' + fString.format(dramaPosition) ,transform=plt.gca().transAxes, verticalalignment='bottom', horizontalalignment='left') plt.show() wb.save(abspath('dadosDrama.xlsx'))
#1 - 10Rd6 #2 - 10Rd10 #3 - 50R2d5 #4 - 50Rd10 #5 - 10R2d5 #6 - 50Rd6 #7 - 50Rd50 variantes = [ "10Rd6", "10Rd10", "50R2d5", "50Rd10", "10R2d5", "50Rd6", "50Rd50" ] indiceVariante = "1" nomeMetrica = "Drama por Caminho" plot.figure() ax = plot.subplot(111) plot.ylabel('Frequência de Simulações') plot.title('Distribuição do ' + nomeMetrica + ' nas variantes do DicePoints') plot.xlabel(nomeMetrica) for indiceVariante in range(7): dramasPontos = leArquivo( abspath("code_pac/dramaporcaminho_grupo" + str(indiceVariante + 1) + ".txt")) print(indiceVariante) #salvarDados("Drama por Pontos", dramasPontos, indiceVariante) plotaHistograma(dramasPontos, nomeMetrica, 0, 50, variantes[indiceVariante]) ax.legend() plot.savefig(abspath('dist_dramaporcaminho.png')) plot.show()
#anos = range(2003, 2014) anos = ['2013-2013', '2014'] def montaRanking(tabelaDados): #pegando o ranking da rodada ranking = [] for linha in tabelaDados.find_all('tr', 'linha-classificacao'): ranking.append([ linha.find('td', 'time').text.strip(), int(linha.find('td', rel='jogos-pontos').find_next().text) ]) return ranking for ano in anos: campeonato = [] file_to_be_open = 'data/raw_data/full' + str(ano) entrada = open(abspath(file_to_be_open), 'r') parser = BeautifulSoup(entrada.read()) entrada.close() saida = open(abspath('data/raw_data/simples_' + str(ano)), 'w') rodadas = parser.find_all('div', 'rodada-tabela') print(ano, len(rodadas)) for rodada in rodadas: campeonato.append(montaRanking(rodada)) #print ' \r', rodada.find_next().get('data-rodada'), " ", int(rodada.text) / len(rodadas) * 100, '%' json.dump(campeonato, saida) saida.close()
l.release() class myList: it = [] def __len__(self): import sys return sys.getsizeof(myList) if __name__ == "__main__": #abre o arqruivo print("Abrindo dados.xlsx ....") parser = Parser(abspath('data/desafio/raw_data/dados.xlsx')) print("Planilha carregada") #le o arquivo linhas = parser.getValues(0, 0) #planilha 0 com zero linhas de cabecalho #linhas = [] print("Número de linhas da planilha:") print(len(linhas)) #imprime a associacao entre índice e nome do campo #for i in range(len(linhas[0])): # print str(i) + ' - ' + linhas[0][i] #for i in linhas: # print i #for i in range(len(linhas)): # print "linha ", str(i+1), " de ", str(len(linhas)) + "\r",
import matplotlib.pyplot as plt import numpy as np import csv import codecs from GameQualityAssessment.project_path import make_absolute_path as abspath def normalizeData(dados): maxPoints= max(dados) minPoints= min(dados) retorno=[] for dado in dados: retorno.append((dado-minPoints)/(maxPoints-minPoints)) return retorno if __name__ == "__main__": arq = csv.reader(codecs.open(abspath("code_pac/brasileiro/tabela_resultados_full.csv"), "r", 'ansi')) tabela = [] for linha in arq: tabela.append(linha) #only data pre_dados = [] edition = [] for i in range(1, len(tabela)): print(tabela[i]) edition.append(int(tabela[i][0])) pre_dados.append([float(tabela[i][1]), float(tabela[i][2]), float(tabela[i][3])]) dados = np.array(pre_dados) #print edition, dados[:,0]
#data = data.decode("utf-8") # ... and reencode it into the target encoding #data = self.encoder.encode(data) # write to the target stream self.stream.write(data.replace(chr(0), '').replace("\n", "")) # empty queue self.queue.truncate(0) def writerows(self, rows): for row in rows: self.writerow(row) if __name__ == '__main__': #anos = xrange(2003, 2015) f = open(abspath('code_pac/brasileiro/tabela_resultados_full.csv'), 'w') #f2 = open('tabela_resultados.csv', 'w') arq = UnicodeWriter(f) arq_r = UnicodeWriter(f) fString = '{0:.4f}' valuesW = [[ 'Edition', 'Drama by Points', 'Drama by Position', 'Drama by Path' ]] valuesC = [] games = Game.retrieveList() for game in games: genGame = BrasileiroGame(game) dramaPath = DramaByPaths(game=genGame, ignored=0).getMeasureValue() dramaPoints = DramaByPointsUp2First(game=genGame,
#reading valid games print ("Reading valid games csv...") ''' tournamentsCodes = set() #set of tournamentcodes with valid games seriesCodes = set() #set of series ([tournamentCode, seriesCode]) with valid games games =[] #list of all valid games detailedGames=[] #list of all valid games and its drama measure values ''' #retrieving drama information from db print ("Retrieving drama information from db...") targets = [(123,272), (123,264), (160,296), (160,303), (166,313), (166,324), (127,281), (127,291)] for target in targets: arq = csv.reader(open(abspath("data/desafio/raw_data/valid_games.csv"), "r"), delimiter=";") t = Tournament.retrieve(target[0], conn) games = getValidGames(Series.retrieve(t, target[1], conn), arq) values = getMeasureValues(games) plotHist(values) plt.show() '''for linha in arq: if (not arq.line_num == 1): #ignores first line tournamentsCodes.add(linha[0]) seriesCodes.add((linha[0], linha[1])) tournament = Tournament.retrieve(linha[0], conn) series = Series.retrieve(tournament, linha[1], conn) groupCode = linha[2] game = Game(series, groupCode)
def calculaESalvaMudancaLideranca(games, indiceVariante): mudancaLiderancao = [] for game in games: mudancaLiderancao.append(calculaMudancasLideranca(game)) salvaArquivoValores(mudancaLiderancao, abspath('code_pac/deadchange_grupo'+ indiceVariante +'.txt'))
''' Created on 28/05/2015 @author: mangeli ''' import requests from GameQualityAssessment.project_path import make_absolute_path as abspath #for ano in xrange(2003, 2014): for ano in ['2013-2013', '2014']: f = open(abspath('../data/raw_data/full' + str(ano)), 'w') f.write( requests.get( 'http://futpedia.globo.com/campeonato/campeonato-brasileiro/' + ano).text.encode('utf-8')) f.close()
round_.sort(reverse=True,key=player_score) with open(abspath('data/espanhol/simples'+year),'w') as filestream: filestream.write("[") number_of_game_rounds = len(game_rounds) for count in range(0,number_of_game_rounds): game_round = game_rounds[count] filestream.write("[") sz = len(game_round) for index in range(0,sz): score = game_round[index] filestream.write("[") filestream.write('"') filestream.write(score[0]) filestream.write('"') filestream.write(",") filestream.write(str(score[1])) filestream.write("]") if(index < sz-1): filestream.write(",") filestream.write("]") if count < number_of_game_rounds-1 : filestream.write(",") filestream.write("]") if __name__ == "__main__": configreader = configReader.ConfigReader() folder = abspath(configreader.parser.get('folder espanhol','folder')) for fil in os.listdir(folder): if fil.startswith("la-liga-") : path = folder + os.sep + fil write_la_liga_on_brasileiro_format(path,path[-8:-4]) pass
if __name__ == '__main__': #1 - 10Rd6 #2 - 10Rd10 #3 - 50R2d5 #4 - 50Rd10 #5 - 10R2d5 #6 - 50Rd6 #7 - 50Rd50 indiceVariante = "1" variante = "5 Rodadas" dramasPontos = leArquivo( abspath("code_pac/dramaporpontos_grupo" + indiceVariante + ".txt")) salvarDados("Drama por Pontos", dramasPontos, indiceVariante) plotaHistograma(dramasPontos, "Drama por Pontos", 0, 100, variante) dramasPosicao = leArquivo( abspath("code_pac/dramaporposicao_grupo" + indiceVariante + ".txt")) salvarDados("Drama por Posicao", dramasPosicao, indiceVariante) plotaHistograma(dramasPosicao, "Drama por Posição", 0, 50, variante) dramasCaminho = leArquivo( abspath("code_pac/dramaporcaminho_grupo" + indiceVariante + ".txt")) salvarDados("Drama por Caminho", dramasCaminho, indiceVariante) plotaHistograma(dramasCaminho, "Drama por Caminho", 1000, 50, variante) leadChange = leArquivo( abspath("code_pac/leadchange_grupo" + indiceVariante + ".txt"))
from __future__ import division from bs4 import BeautifulSoup from openpyxl import Workbook import datetime from GameQualityAssessment.project_path import make_absolute_path as abspath #<div class="rodada-tabela"> #<tr class="linha-classificacao" rel="juventude" data-escudo="http://s.glbimg.com/es/sde/f/organizacoes/2011/01/03/juventude30.png"> if __name__ == '__main__': anos = range(2003, 2015) wb = Workbook() for ano in anos: entrada = open(abspath('data/brasileiro/raw_data/full') + str(ano), 'r') planilha = wb.create_sheet(title=str(ano)) planilha.append(['rodada', 'data-hora', 'local', 'mandante', 'gols-mandante', 'gols-visitante', 'visitante']) parser = BeautifulSoup(entrada.read()) entrada.close() rodadas = parser.find_all('div',id='lista-jogos') for rodada in rodadas: for linha in rodada.find_all('li', class_='lista-classificacao-jogo'): numRodada = linha['data-rodada'] dia = linha.find('time', itemprop='startDate')['datetime'].split('/') hora = linha.find('div', class_='data-local').find('span', class_='horario').text.split('h') dados = [int(numRodada), datetime.datetime(int(dia[2]), int(dia[1]), int(dia[0]), int(hora[0]), int(hora[1])), linha.find('div', class_='data-local').find('span', itemprop='name').text,
import csv from GameQualityAssessment.project_path import make_absolute_path as abspath def normalizeData(dados): maxPoints = max(dados) minPoints = min(dados) retorno = [] for dado in dados: retorno.append((dado - minPoints) / (maxPoints - minPoints)) return retorno if __name__ == "__main__": arq = csv.reader( open(abspath("code_pac/brasileiro/tabela_resultados_full.csv"), "r")) tabela = [] for linha in arq: tabela.append(linha) #only data pre_dados = [] edition = [] for i in range(1, len(tabela)): edition.append(int(tabela[i][0])) pre_dados.append( [float(tabela[i][1]), float(tabela[i][2]), float(tabela[i][3])])
from openpyxl import Workbook from openpyxl.reader.excel import load_workbook import scipy.stats import scipy.spatial.distance from GameQualityAssessment.project_path import make_absolute_path as abspath def assignValues(row): retorno = [] for i in range(0, len(row)): retorno.append(row[i].value) return retorno if __name__ == '__main__': wb = load_workbook(filename=abspath('code_pac/brasileiro/vetores.xlsx'), read_only=True) ws = wb['Sheet1'] path = [] points = [] positions = [] judges = [] for row in ws.rows: if row[0].value == "path": path = assignValues(row[1:]) if row[0].value == "points": points = assignValues(row[1:]) if row[0].value == "positions": positions = assignValues(row[1:]) if row[0].value == "judges": judges = assignValues(row[1:])
with open(abspath('data/alemao/simples'+year),'w') as filestream: filestream.write("[") number_of_game_rounds = len(game_rounds) for count in range(0,number_of_game_rounds): game_round = game_rounds[count] filestream.write("[") sz = len(game_round) for index in range(0,sz): score = game_round[index] filestream.write("[") filestream.write('"') filestream.write(score[0]) filestream.write('"') filestream.write(",") filestream.write(str(score[1])) filestream.write("]") if(index < sz-1): filestream.write(",") filestream.write("]") if count < number_of_game_rounds-1 : filestream.write(",") filestream.write("]") if __name__ == "__main__": configreader = configReader.ConfigReader() folder = abspath(configreader.parser.get('folder alemao','folder')) for fil in os.listdir(folder): if fil.startswith("bundesliga-") : path = folder + os.sep + fil write_bundesliga_on_brasileiro_format(path,path[-8:-4]) pass