|
require 'bundler' |
|
Bundler.require |
|
|
|
module CountBy |
|
def count_by &block |
|
reduce(Hash.new {|h,k| h[k] = 0}) do |prev, i| |
|
key = yield i |
|
prev[key] += 1 |
|
prev |
|
end |
|
end |
|
end |
|
|
|
module MapWith |
|
def map_with with, &block |
|
memo = with.to_proc.call self |
|
map {|i| yield i, memo} |
|
end |
|
end |
|
|
|
module MRound |
|
refine Numeric do |
|
def m_round n |
|
x = n * div(n) |
|
return x if self == x |
|
return [x, x + n].max_by(&:abs) if self + self == x + x + n |
|
[x, x + n].min_by {|i| (i - self).abs} |
|
end |
|
end |
|
end |
|
|
|
using MRound |
|
|
|
class TeamIO |
|
include Enumerable |
|
SCORES = %w(score death).map {|indicator| (1..5).map {|i| :"#{indicator}_#{i}"}}.flatten |
|
Team = Struct.new :round, *SCORES do |
|
def win? |
|
round == 7 |
|
end |
|
|
|
def total_score |
|
[score_1, score_2, score_3, score_4, score_5].sum |
|
end |
|
|
|
def top1_rate |
|
return 100 if total_score == 0 |
|
(score_1 * 100 / total_score).to_i |
|
end |
|
|
|
def top2_rate |
|
return 100 if total_score == 0 |
|
((score_1 + score_2) * 100 / total_score).to_i |
|
end |
|
end |
|
|
|
def each &block |
|
open 'data/match_score_after_assist.csv', ?r do |f| |
|
f.gets # ignore header |
|
while f.gets |
|
yield Team.new *$_.chomp.split(?,).map(&:to_i) |
|
end |
|
end |
|
end |
|
end |
|
|
|
teams = TeamIO.new.map &:itself |
|
|
|
Gnuplot.open do |g| |
|
Gnuplot::Plot.new g do |p| |
|
p.set 'term png truecolor font "Mikachan"' |
|
p.set %(out "data/#{File.basename __FILE__, '.rb'}_top1_rate.png") |
|
|
|
p.title '(1位のスコア/チームスコア)%の分布' |
|
p.ylabel '出現率(%)' |
|
p.xlabel '(1位のスコア/チームスコア)%' |
|
|
|
p.set 'style fill transparent solid 0.75 noborder' |
|
p.set 'xrange [0:100]' |
|
|
|
# winner |
|
table = teams |
|
.select(&:win?) |
|
.extend(CountBy) |
|
.count_by(&:top1_rate) |
|
.extend(MapWith) |
|
.map_with(->(x){x.values.sum}) {|(k, v), total| [k, v * 100 / total.to_f]} |
|
.to_h |
|
x = table.keys.sort |
|
y = x.map &table.method(:fetch) |
|
p.data << Gnuplot::DataSet.new([x,y]) do |d| |
|
d.title = '勝利チーム' |
|
d.with = 'boxes' |
|
end |
|
|
|
# loser |
|
table = teams |
|
.reject(&:win?) |
|
.extend(CountBy) |
|
.count_by(&:top1_rate) |
|
.extend(MapWith) |
|
.map_with(->(x){x.values.sum}) {|(k, v), total| [k, v * 100 / total.to_f]} |
|
.to_h |
|
x = table.keys.sort |
|
y = x.map &table.method(:fetch) |
|
p.data << Gnuplot::DataSet.new([x,y]) do |d| |
|
d.title = '敗北チーム' |
|
d.with = 'boxes' |
|
end |
|
end |
|
end |
|
|
|
Gnuplot.open do |g| |
|
Gnuplot::Plot.new g do |p| |
|
p.set 'term png truecolor font "Mikachan"' |
|
p.set %(out "data/#{File.basename __FILE__, '.rb'}_top2_rate.png") |
|
|
|
p.title '(1位+2位のスコア/チームスコア)%の分布' |
|
p.ylabel '出現率(%)' |
|
p.xlabel '(1位+2位のスコア/チームスコア)%' |
|
|
|
p.set 'style fill transparent solid 0.75 noborder' |
|
p.set 'xrange [0:100]' |
|
|
|
# winner |
|
table = teams |
|
.select(&:win?) |
|
.extend(CountBy) |
|
.count_by(&:top2_rate) |
|
.extend(MapWith) |
|
.map_with(->(x){x.values.sum}) {|(k, v), total| [k, v * 100 / total.to_f]} |
|
.to_h |
|
x = table.keys.sort |
|
y = x.map &table.method(:fetch) |
|
p.data << Gnuplot::DataSet.new([x,y]) do |d| |
|
d.title = '勝利チーム' |
|
d.with = 'boxes' |
|
end |
|
|
|
# loser |
|
table = teams |
|
.reject(&:win?) |
|
.extend(CountBy) |
|
.count_by(&:top2_rate) |
|
.extend(MapWith) |
|
.map_with(->(x){x.values.sum}) {|(k, v), total| [k, v * 100 / total.to_f]} |
|
.to_h |
|
x = table.keys.sort |
|
y = x.map &table.method(:fetch) |
|
p.data << Gnuplot::DataSet.new([x,y]) do |d| |
|
d.title = '敗北チーム' |
|
d.with = 'boxes' |
|
end |
|
end |
|
end |
|
|
|
out = "data/#{File.basename __FILE__, '.rb'}.png" |
|
files = ['top1_rate', 'top2_rate'].map do |rank| |
|
"data/#{File.basename __FILE__, '.rb'}_#{rank}.png" |
|
end |
|
|
|
`convert +append #{files.join(' ')} #{out}` |
|
|
|
files.each &FileUtils.method(:rm) |