2 # This generates graphs of evaluation values from comments in CSA files.
3 # Ruby libraries that are required:
4 # - RubyGems: http://rubyforge.org/projects/rubygems/
5 # On Debian, $ sudo install rubygems1.8
6 # - gnuplot: http://rubyforge.org/projects/rgplot/
7 # On Debian, $ sudo gem install gnuplot
8 # OS librariles that is required:
9 # - Gnuplot: http://www.gnuplot.info/
10 # On Debian, $ sudo apt-get install gnuplot
12 # Copyright (C) 2008 Daigo Moriwaki <daigo@debian.org>
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 $log = Logger.new(STDERR)
40 $log.level = Logger::WARN
44 attr_reader :ext, :size
51 class LargeFormat < Format
59 plot.format 'x "%y/%m/%d"'
67 class LargePngFormat < LargeFormat
73 def to_image_file(name)
74 return File.join(@root, "#{name}-large.#{ext}")
78 class SmallPngFormat < Format
84 def to_image_file(name)
85 return File.join(@root, "#{name}-small.#{ext}")
89 plot.terminal "png size 320,200 crop"
96 class SvgFormat < LargeFormat
102 def to_image_file(name)
103 return File.join(@root, "#{name}.#{ext}")
107 def plot(format, name, dates, rates, rdates, rrates)
109 Gnuplot::Plot.new( gp ) do |plot|
112 plot.output format.to_image_file(name)
114 #plot.size "ratio #{1/1.618}"
116 plot.timefmt '"%Y-%m-%d"'
117 plot.xrange "[\"%s\":\"%s\"]" %
118 [dates.first.strftime("%Y-%m-%d"),
119 dates.last.strftime("%Y-%m-%d")]
120 ymin = ((rates + rrates).min/50) * 50
121 ymax = ((rates + rrates).max/50 + 1) * 50
122 plot.yrange "[%s:%s]" % [ymin, ymax]
125 data << Gnuplot::DataSet.new([dates, rates]) do |ds|
128 ds.title = "original"
131 data << Gnuplot::DataSet.new([rdates, rrates]) do |ds|
134 ds.title = "relative (rate24)"
142 def load_file(file_name)
143 if /^.*-(\d{8}).yaml$/ =~ file_name
144 date = Date::parse($1)
146 $log.error("Invalid file name: %s" % [file_name])
149 db = YAML::load_file(file_name)
150 unless db['players'] && db['players'][0]
151 $log.error("Invalid file format: %s" % [file_name])
154 db['players'][0].each do |name, hash|
155 $players[name] ||= {}
156 $players[name][date] = hash['rate'].to_i
160 def empty_file?(file)
161 if !FileTest.exists?(file)
162 $log.error("Could not find the file: %s" % [file])
165 if FileTest.zero?(file)
166 $log.error("Empty file: %s" % [file])
175 puts "Usage: #{$0} --output-dir dir <players_yaml_file> <players_yaml_file>..."
177 puts " --output-dir dir Images will be located in the dir."
183 parser = GetoptLong.new
184 parser.set_options(['--output-dir', '-o', GetoptLong::REQUIRED_ARGUMENT])
186 parser.each_option do |name, arg|
187 eval "$OPT_#{name.sub(/^--/, '').gsub(/-/, '_').upcase} = '#{arg}'"
192 usage if !$OPT_OUTPUT_DIR || ARGV.size < 2
194 while file = ARGV.shift
195 next if empty_file?(file)
199 if !$players || $players.empty?
203 formats = [LargePngFormat.new($OPT_OUTPUT_DIR),
204 SmallPngFormat.new($OPT_OUTPUT_DIR),
205 SvgFormat.new($OPT_OUTPUT_DIR)]
207 $players.each do |name, hash|
208 dates, rates = hash.sort.transpose
209 rdates = dates.find_all do |date|
210 $players["YSS+707d4f98d9d2620cdaab58f19d02a2e4"] &&
211 $players["YSS+707d4f98d9d2620cdaab58f19d02a2e4"][date]
213 rrates = rdates.map do |date|
214 2300 - $players["YSS+707d4f98d9d2620cdaab58f19d02a2e4"][date] + hash[date]
216 formats.each do |format|
217 plot(format, name, dates, rates, rdates, rrates)