''' Created on Apr 10, 2019 @author: Brett Paufler Copyright Brett Paufler TAKES TWO LOGFILES this_log 1.txt this_log 2.txt In Research Ration Red = First Game Black = Second Game ''' from os import listdir from os.path import join as path_join import matplotlib.pyplot as plt import numpy as np #################################################### # # Logfile IO - Get Raw Text # #################################################### dir_in = './input/' dir_out = './output/' log_list = [path_join(dir_in, log_file) for log_file in listdir(dir_in)] name_proper = ' '.join( log_list[0][8:-4].split(' ')[:-1]) save_name_base = '%s%s' % ( dir_in, '_'.join(name_proper.lower().split(' ') )) save_name_base = save_name_base.replace(dir_in, dir_out) save_name_ratio = save_name_base + '_research_ratio.png' save_name_abandoned = save_name_base + '_research_abandoned.png' save_name_rough = save_name_base + '_research_rough.png' save_name_cum_sum = save_name_base + '_research_cum_sum.png' #print log_list #print name_proper #print save_name with open(log_list[0], 'r') as f: log_text_one = f.read() with open(log_list[1], 'r') as f: log_text_two = f.read() #print log_text_one #################################################### # # Extract Research Data # #################################################### #Find Number of Turns turns_raw = [0, 0] for n, game in enumerate([log_text_one, log_text_two]): for line in game.split('\n'): #print line if line.startswith('Turn '): turn = line.split('/') turn = int(turn[0][5:]) if turns_raw[n] < turn: turns_raw[n] = turn #print n, turn #print turns_raw turns_max = 1 + max(turns_raw) turns_min = 1 + min(turns_raw) #print turns_max #print turns_min #For Per Turn Research Rate research_turn = [ [0] * turns_max, [0] * turns_max ] #print research_turn #To Track Technological Discoveries discovery_turn = [ [''] * turns_max, [''] * turns_max ] #print discovery_turn for n, game in enumerate([log_text_one, log_text_two]): turn = 0 for line in game.split('\n'): #What Turn is it? if line.startswith('Turn '): turn = line.split('/') turn = int(turn[0][5:]) continue #Per Turn Research Amount if ( (' Research: ' in line) and (' per turn' in line)): research = line.split(': ')[1][:-9] research_turn[n][turn] = int(research) #print line #print research continue #Technologies by Trade if 'Tech acquired (trade, light' in line: tech = line.split('espionage): ')[1] discovery_turn[n][turn] = tech #print line #print civ_parse_tech_tree continue if 'Tech research finished: ' in line: tech = line.split('finished: ')[1] discovery_turn[n][turn] = tech #print line #print civ_parse_tech_tree ################################################### # # Preprocess the Values a Bit # I probably did not need to do any of this # ################################################### #Chop to Minimum research_one = research_turn[0][:turns_min] research_two = research_turn[1][:turns_min] #print research_one #print research_two #But I don't think I will be using this discover_one = discovery_turn[0][:turns_min] discover_two = discovery_turn[1][:turns_min] #print discover_one #print discover_two ################################################### # # Graph: Research Rate as a Ratio # ################################################### ratio_one = [] ratio_two = [] #Reduce Absolute Values to Ratios for i in range(len(research_one)): if research_one[i] >= research_two[i]: r = research_one[i] else: r = research_two[i] r = float(r) ratio_one.append(research_one[i]/r) ratio_two.append(research_two[i]/r) plt.figure(figsize=(10,5)) plt.plot(range(turns_min), ratio_one, color='blue', label='Game One') plt.plot(range(turns_min), ratio_two, color='red', label='Game Two') plt.legend() plt.title(name_proper + '\nResearch Ratio') plt.ylabel('Ratio') plt.xlabel('Turn Number') plt.xlim(xmin=0, xmax=turns_min) #plt.show() print save_name_abandoned #plt.savefig(save_name_abandoned) plt.close() ################################################### # # Graph: as an absolute ratio to the second game # ################################################### ratio_two = [] #A Simple Ratio for i in range(len(research_one)): if research_one[i] == 0: ratio_two.append(ratio_two[-1]) else: ratio_two.append( float(research_two[i]) / float(research_one[i])) plt.figure(figsize=(10,5)) plt.plot(range(turns_min), ratio_two, color='black') plt.axhline(1.0, color='red') plt.title(name_proper + '\nResearch Ratio') plt.ylabel('Ratio') plt.xlabel('Turn Number') plt.xlim(xmin=0, xmax=turns_min) #plt.show() print save_name_rough #plt.savefig(save_name_rough) plt.close() ################################################### # # Graph: Ratio... as above but smooth # ################################################### conv = [.01]*100 ratio_one = np.array(research_one, dtype=np.float64) ratio_one = np.convolve( ratio_one, conv, mode='same') ratio_two = np.array(research_two) ratio_two = np.convolve( ratio_two, conv, mode='same') ratio_two = ratio_two/ratio_one abs_max = np.max(np.abs(ratio_one)) plt.figure(figsize=(10,5)) plt.plot(range(turns_min), ratio_two, color='black') plt.axhline(1.0, color='red') plt.title(name_proper + '\nResearch Ratio') plt.ylabel('Ratio') plt.xlabel('Turn Number') plt.xlim(xmin=0, xmax=turns_min) #plt.ylim(ymin=-abs_max, ymax=abs_max) #plt.show() print save_name_ratio plt.savefig(save_name_ratio) plt.close() ################################################### # # Graph: Cumulative Sum # ################################################### cum_sum_one = np.cumsum(research_one) cum_sum_two = np.cumsum(research_two) plt.figure(figsize=(10,5)) plt.plot(range(turns_min), cum_sum_one, color='blue', label='Game One Total: %d' % sum(research_one)) plt.plot(range(turns_min), cum_sum_two, color='red', label='Game Two Total: %d' % sum(research_two)) plt.legend() plt.title(name_proper + '\nCumulative Sum') plt.ylabel('Ratio') plt.xlabel('Turn Number') plt.xlim(xmin=0, xmax=turns_min) #plt.show() print save_name_cum_sum plt.savefig(save_name_cum_sum) plt.close() ################################################### # # Statistics # ################################################### print 'Total Research Game One: %d, Turns: %d' % ( sum(research_one), len(research_one)) print 'Total Research Game Two: %d, Turns: %d' % ( sum(research_two), len(research_one))