Advertisement
Guest User

LSC graph

a guest
Jan 14th, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.07 KB | None | 0 0
  1. import string
  2. import re
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import codecs
  6.  
  7. print "---- LSC log parser ----"
  8.  
  9. ## input
  10.  
  11. input_files = [
  12.     #"input/log_01.txt","input/log_02.txt","input/log_03.txt",
  13.     #"input/log_04.txt","input/log_05.txt",
  14.     #"input/log_06.txt","input/log_07.txt",
  15.     "input/log_08.txt","input/log_09.txt","input/log_10.txt",
  16.     "input/log_11.txt","input/log_12.txt","input/log_13.txt",
  17.     "input/log_14.txt","input/log_15.txt","input/log_16.txt",
  18.     "input/log_17.txt","input/log_18.txt",
  19.     "input/log_19.txt",
  20.     "input/log_20.txt","input/log_21.txt",
  21.     "input/actual.txt"
  22.     ]
  23.  
  24. ship_plot_groups = [
  25.      {'jap':[u'大鳳',u'大凰'], 'eng':'Taihou', 'color':'#acd8bd'}
  26.     ,{'jap':u'大和', 'eng':'Yamato', 'color':'#b66572'}
  27.     ,{'jap':[u'あきつ丸',u'あきつ'], 'eng':'Akitsumaru', 'color':'#c7c6c7'}
  28.     ,{'jap':[u'まるゆ',u'まゆる'], 'eng':'Maruyu', 'color':'#fcf1d6'}
  29.  
  30.     ,{'jap':u'矢矧', 'eng':'Yahagi', 'color':'#5b6268'},{'jap':u'阿賀野', 'eng':'Agano', 'color':'#aa2845'},{'jap':u'能代', 'eng':'Noshiro', 'color':'#6a4143'}
  31.     #,{'jap':[u'矢矧',u'阿賀野',u'能代'], 'eng':'Agano class', 'color':'#aa2845'}
  32.  
  33.     ,{'jap':u'三隈', 'eng':'Mikuma', 'color':'#86362d'},{'jap':u'鈴谷', 'eng':'Suzuya', 'color':'#b3cbc4'},{'jap':u'熊野', 'eng':'Kumano', 'color':'#d2c1aa'},{'jap':u'最上', 'eng':'Mogami', 'color':'#666666'}
  34.     #,{'jap':[u'三隈',u'鈴谷',u'熊野',u'最上'], 'eng':'Mogami class', 'color':'#86362d'}
  35.  
  36.     ,{'jap':u'長門', 'eng':'Nagato', 'color':'#605044'},{'jap':[u'陸奥',u'陸奧'], 'eng':'Mutsu', 'color':'#b5916c'}
  37.     #,{'jap':[u'長門',u'陸奥',u'陸奧'], 'eng':'Nagato class', 'color':'#b5916c'}
  38.  
  39.     #,{'jap':u'赤城', 'eng':'Akagi', 'color':'#666666'}
  40.     ,{'jap':u'加賀', 'eng':'Kaga', 'color':'#473f8a'}
  41.    
  42.     #,{'jap':[u'赤城',u'加賀'], 'eng':'1kou', 'color':'#473f8a'}
  43.  
  44.     ,{'jap':u'翔鶴', 'eng':'Shoukaku', 'color':'#dee8ed'}
  45.     ,{'jap':u'瑞鶴', 'eng':'Zuikaku', 'color':'#99948e'}
  46.    
  47.     #,{'jap':[u'翔鶴',u'瑞鶴'], 'eng':'5kou', 'color':'#dee8ed'}
  48.  
  49.     #,{'jap':u'蒼龍', 'eng':'Souryuu', 'color':'#556959'}
  50.     #,{'jap':u'飛龍', 'eng':'Hiryuu', 'color':'#555969'}
  51.  
  52.     #,{'jap':[u'蒼龍',u'飛龍'], 'eng':'2kou', 'color':'#666666'}
  53.    
  54.     ,{'jap':[u'赤城',u'蒼龍',u'飛龍'], 'eng':'Other CV', 'color':'#667766'}
  55.  
  56.     ,{'jap':[u'瑞鳳',u'飛鷹',u'隼鷹',u'準鷹',u'祥鳳',u'龍驤',u'翔鳳',u'龍嬢',u'出雲丸',u'龍穣'], 'eng':'CVL', 'color':'#776666'}
  57.  
  58.     ,{'jap':[u'金剛',u'比叡',u'榛名',u'霧島'], 'eng':'Kongou class', 'color':'#595555'}
  59.  
  60.     #,{'jap':[u'扶桑',u'山城'], 'eng':'Fusou class', 'color':'#666666'}
  61.     #,{'jap':[u'伊勢',u'日向'], 'eng':'Ise class', 'color':'#666666'}
  62.     ,{'jap':[u'扶桑',u'山城',u'伊勢',u'日向'], 'eng':'BBV', 'color':'#555955'}
  63.  
  64.     #,{'jap':[u'金剛',u'比叡',u'榛名',u'霧島',u'扶桑',u'山城',u'伊勢',u'日向'], 'eng':'Other BB', 'color':'#666677'}
  65.  
  66.     ,{'jap':[u'利根',u'筑摩'], 'eng':'Tone class', 'color':'#444444'}
  67.     ]
  68.  
  69. all_ship = [
  70.     u"長門",u"陸奥",u"伊勢",u"日向",u"雪風",u"赤城",u"加賀",u"蒼龍",u"飛龍",u"島風"
  71.    ,u"吹雪",u"白雪",u"初雪",u"深雪",u"叢雲",u"磯波",u"綾波",u"敷波",u"大井",u"北上"
  72.    ,u"金剛",u"比叡",u"榛名",u"霧島",u"鳳翔",u"扶桑",u"山城",u"天龍",u"龍田",u"龍驤"
  73.    ,u"睦月",u"如月",u"皐月",u"文月",u"長月",u"菊月",u"三日月",u"望月",u"球磨",u"多摩"
  74.    ,u"木曾",u"長良",u"五十鈴",u"名取",u"由良",u"川内",u"神通",u"那珂",u"千歳",u"千代田"
  75.    ,u"最上",u"古鷹",u"加古",u"青葉",u"妙高",u"那智",u"足柄",u"羽黒",u"高雄",u"愛宕"
  76.    ,u"摩耶",u"鳥海",u"利根",u"筑摩",u"飛鷹",u"隼鷹",u"朧",u"曙",u"漣",u"潮"
  77.    ,u"暁",u"響",u"雷",u"電",u"初春",u"子日",u"若葉",u"初霜",u"白露",u"時雨"
  78.    ,u"村雨",u"夕立",u"五月雨",u"涼風",u"朝潮",u"大潮",u"満潮",u"荒潮",u"霰",u"霞"
  79.    ,u"陽炎",u"不知火",u"黒潮",u"祥鳳"
  80.    ,u"翔鶴",u"瑞鶴",u"鬼怒"
  81.    ,u"阿武隈",u"夕張",u"瑞鳳",u"三隈",u"初風",u"舞風"
  82.    ,u"衣笠",u"伊19",u"鈴谷",u"熊野",u"伊168",u"伊58",u"伊8"
  83.    ,u"大和",u"秋雲",u"夕雲",u"巻雲",u"長波",u"阿賀野",u"能代",u"矢矧"
  84.    ,u"武蔵"
  85.    ,u"大鳳",u"伊401",u"あきつ丸",u"まるゆ"
  86.    # name variants
  87.    ,u'大凰',u'あきつ',u'まゆる',u'準鷹',u'陸奧',u'翔鳳', u'龍嬢',u'龍穣',u'出雲丸'
  88.    ]
  89.  
  90. ## plotting config
  91.  
  92. #Taihou min 3000,2000,2000,3000
  93. #CV only (4000,1500,2000,5000,1)
  94.  
  95. #Yamato min 3000,2000,3500,1000
  96. #No Yamato 4000,1500,2000,5000
  97.  
  98. min_resources = (1500,1500,2000,1000,1)
  99. max_resources = (7000,7000,7000,7000,100)
  100.  
  101. min_samples = 100
  102. legend = 1
  103.  
  104. label_dist = -0.3
  105. label_min_chance = 2.5
  106. label_name_min_chance = 6.0
  107. label_short_name_min_chance = 5.0
  108. short_name_length = 8
  109.  
  110. x_max = 100
  111. bar_y_start_pos = 0
  112. bar_spacing = 1.25
  113. bar_height = 1
  114.  
  115. yellow_rate = 0.25
  116.  
  117. figure_size = (16, 6)
  118.  
  119. font = 'Segoe UI'
  120. font_size = 'xx-small'
  121.  
  122. legend_columns = 12
  123.  
  124. subplots_adjustment = {'left':0.16, 'right':0.99, 'bottom':0.11, 'top':0.94}
  125.  
  126. lsc_regex = u".*\D+(\d{4})\D*(\d{4})\D*(\d{4})\D*(\d{4}).*[開発資材財源料費」]+\s*[::・]?\s*(\d+).*結果\s*[::・]?\s*([^-]*) --"
  127.  
  128. ## functions
  129.  
  130. def fix_rec(rec):
  131.     m = {u"0":u"0", u"1":u"1", u"2":u"2", u"3":u"3", u"4":u"4", u"5":u"5", u"6":u"6", u"7":u"7", u"8":u"8", u"9":u"9"}
  132.     for k,v in m.items():
  133.         rec = rec.replace(k,v)
  134.     return rec
  135.  
  136. def parse_ships(ships_str):
  137.     res = re.findall("[\w]+", ships_str, re.UNICODE)
  138.     filtered_res = [s for s in res if s in all_ship]
  139.     #bad_res = [s for s in res if s not in all_ship]
  140.     #for s in bad_res:
  141.     #    print "bad ship report: " + s
  142.     return filtered_res
  143.  
  144. def cmp_rec(a, b):
  145.     return a[0] < b[0] or a[1] < b[1] or a[2] < b[2] or a[3] < b[3] or a[4] < b[4]
  146.  
  147. ## main
  148.  
  149. all_possible = []
  150. for ship in ship_plot_groups:
  151.     s = ship['jap']
  152.     if isinstance(s, list):
  153.         all_possible.extend(s)
  154.     else:
  155.         all_possible.append(s)
  156.  
  157. print "---- Reading - start ----"
  158.  
  159. all_input = ""
  160. for file in input_files:
  161.     print "Reading - " + file
  162.     f = codecs.open(file, 'r', encoding='utf-8')
  163.     all_input += f.read()
  164.  
  165. print "---- Reading - end ----"
  166.  
  167. print "---- Parsing - start ----"
  168.  
  169. all_input_splitted = string.split(all_input, "\n")
  170. data = {}
  171. total = 0
  172. bad_count = 0
  173. times = []
  174. for line in all_input_splitted:
  175.     time_match = re.match(".*(\d{4}-\d{2}-\d{2}).*(\d{2}:\d{2}:\d{2})", line, re.UNICODE)
  176.     if time_match is not None:
  177.         time = time_match.group(1) + " " + time_match.group(2)
  178.         times.append(time)
  179.  
  180.     m = re.match(lsc_regex, line, re.UNICODE)
  181.     if m is None:
  182.         if line.find(u"結果") != -1:
  183.             #print "no match: " + line
  184.             bad_count += 1
  185.         continue
  186.     rec = m.group(1) + "/" + m.group(2) + "/" + m.group(3) + "/" + m.group(4) + "/" + m.group(5)
  187.     rec = fix_rec(rec)
  188.     rec_val = (int(fix_rec(m.group(1))), int(fix_rec(m.group(2))), int(fix_rec(m.group(3))), int(fix_rec(m.group(4))), int(fix_rec(m.group(5))))
  189.  
  190.     if cmp_rec(rec_val,min_resources) or cmp_rec(max_resources,rec_val):
  191.         continue
  192.  
  193.     ship_str = m.group(6)
  194.     rec_ships = parse_ships(ship_str)
  195.     for ship in rec_ships:
  196.         #if ship == u"大和" and rec_val[3] > rec_val[1]:
  197.         #    print rec + u" 大和"
  198.         total += 1
  199.         if rec not in data:
  200.             data[rec] = {'total':0,'rec_val':rec_val}
  201.         data[rec]['total'] += 1
  202.         if ship not in data[rec]:
  203.             data[rec][ship] = 0
  204.         data[rec][ship] += 1
  205.  
  206. print "---- Parsing - end ----"
  207.  
  208. total_stat_str = "Reports: " + str(total) + " (" + ("%.1lf"%(100*float(total) / float(total + bad_count))) + "%) - Unparsable: " + str(bad_count) + " (" + ("%.1lf"%(100*(float(bad_count) / float(total + bad_count)))) + "%)"
  209. print total_stat_str
  210. times_sorted = sorted(times)
  211. time_span_str = times_sorted[0] + " - " + times_sorted[len(times_sorted)-1]
  212. print "Time span: " + time_span_str
  213.  
  214. print "---- Generating - start ----"
  215.  
  216. ## ordering
  217. to_order = {}
  218. max_count = 0
  219. for rec, value in data.items():
  220.     if value['total'] > max_count:
  221.         max_count = value['total']
  222.     order_key = value['total'] #value['rec_val'][3]
  223.     if order_key not in to_order:
  224.         to_order[order_key] = []
  225.     to_order[order_key].append({'rec':rec, 'val':value})
  226. ordered = sorted(to_order)
  227.  
  228. recs = []
  229. chances = {}
  230. colors = []
  231. for key in ordered:
  232.     for data in to_order[key]:
  233.         rec = data['rec']
  234.         value = data['val']
  235.         rec_str = rec + u" (" + str(value['total']) + u")"
  236.  
  237.         if value['total'] < min_samples:
  238.             #print u"too few data: " + rec_str
  239.             continue
  240.  
  241.         for k in value.keys():
  242.             if k not in ['total','rec_val'] and k not in all_possible:
  243.                 print "bad ship: " + k + " cnt: " + str(value[k]) + " rec: " + rec
  244.  
  245.         rec_rate = float(value['total']) / float(max_count)
  246.         green_rate = 0.0
  247.         red_rate = 0.0
  248.         if rec_rate < yellow_rate:
  249.             green_rate = rec_rate / yellow_rate
  250.             red_rate = 1.0
  251.         else:
  252.             green_rate = 1.0
  253.             red_rate = (1.0 - rec_rate) / (1.0 - yellow_rate)
  254.         color = '#' + ('%02x'%(red_rate*225)) + ('%02x'%(green_rate*225)) + '00'
  255.  
  256.         for i in range(0,len(ship_plot_groups)):
  257.             ship_names = ship_plot_groups[i]['jap']
  258.             if not isinstance(ship_names, list):
  259.                 ship_names = [ship_names]
  260.             chance = 0.0
  261.             for ship_name in ship_names:
  262.                 if ship_name not in value:
  263.                     value[ship_name] = 0
  264.                     #print u"no " + ship_name + " with: " + rec_str
  265.                 #print u"" + ship_name + " get: " + rec_str
  266.                 chance += float(value[ship_name]) / float(value['total']) * 100
  267.             if i not in chances:
  268.                 chances[i] = []
  269.             chances[i].append(chance)
  270.  
  271.         recs.append(rec_str)
  272.         colors.append(color)
  273.  
  274. print "---- Generating - end ----"
  275. print "---- Plotting - start ----"
  276.  
  277. plt.rc('font', family=font)
  278.  
  279. y_pos = np.arange(len(recs)) * bar_spacing + bar_y_start_pos
  280. fig, ax1 = plt.subplots(figsize=figure_size)
  281.  
  282. bars = []
  283. prev_chance = []
  284. for v in chances.values()[0]:
  285.     prev_chance.append(0)
  286.  
  287. for i, chance in chances.items():
  288.     rects = ax1.barh(y_pos, chance, align='center', alpha=0.8, color=ship_plot_groups[i]['color'], left=prev_chance, height=bar_height)
  289.     for j in range(0,len(chance)):
  290.         prev_chance[j] += chance[j]
  291.         if chance[j] > label_min_chance:
  292.             label = ('%.1lf'%chance[j]) + '%'
  293.             if chance[j] > label_name_min_chance or (chance[j] > label_short_name_min_chance and len(ship_plot_groups[i]['eng']) < short_name_length):
  294.                 label = ship_plot_groups[i]['eng'] + " " + label
  295.             ax1.text(label_dist + prev_chance[j], rects[j].get_y()+rects[j].get_height()/2., label, ha='right', va='center', fontsize=font_size)
  296.     bars.append(rects[0])
  297.  
  298. ax1.set_xticklabels([])
  299.  
  300. plt.yticks(y_pos, recs, fontsize='small')
  301. for i in range(0,len(colors)):
  302.     ticks = ax1.get_yticklabels()
  303.     ticks[i].set_color(colors[i])
  304. plt.tick_params(axis='y',which='both',left='off',right='off')
  305. plt.tick_params(axis='x',which='both',top='off',bottom='off')
  306.  
  307. titles = []
  308. for v in ship_plot_groups:
  309.     titles.append(v['eng'])
  310.  
  311. plt.xlim(0, x_max)
  312. plt.ylim(-1,y_pos[len(y_pos)-1]+1)
  313.  
  314. fig.canvas.set_window_title('LSC')
  315. plt.title("LSC report (" + time_span_str + ") - Total: " + str(total))
  316. if legend != 0:
  317.     plt.legend(bars, titles, fontsize='x-small', loc='lower center', mode="expand", ncol=legend_columns, bbox_to_anchor=(0, -0.1, 1, 0))
  318. plt.subplots_adjust(left=subplots_adjustment['left'], right=subplots_adjustment['right'], bottom=subplots_adjustment['bottom'], top=subplots_adjustment['top'])
  319.  
  320. print "---- Plotting - end ----"
  321.  
  322. print "---- Display - start ----"
  323.  
  324. plt.show()
  325.  
  326. print "---- Display - end ----"
  327.  
  328. print "Bye."
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement