algoplot 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Jon-Michael Deldin
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,37 @@
1
+ # algoplot: A script for comparing the growth of functions
2
+
3
+ ## Examples
4
+
5
+ Merge sort (O(n log n)) vs. bubble sort (O(n^2))
6
+
7
+ algoplot -o merge-vs-bubble.png 'x * log(x)' 'x^2'
8
+
9
+ ![merge sort vs. bubble sort plot](https://github.com/jmdeldin/algoplot/raw/master/doc/merge-vs-bubble.png)
10
+
11
+ ## Usage
12
+
13
+ - Run `algoplot -h` for specific options
14
+ - Use `x` instead of `n` when plotting functions
15
+
16
+ ## Installation
17
+
18
+ You need
19
+
20
+ - [Gnuplot](http://www.gnuplot.info) installed with the `gnuplot` command
21
+ available on your `$PATH`
22
+ - [Ruby](http://ruby-lang.org) (only tested with 1.9.3)
23
+
24
+ Once you have the prerequisites installed, simply run the following command
25
+ from your terminal:
26
+
27
+ gem install algoplot
28
+
29
+ You may need to prefix it with `sudo` depending on your setup.
30
+
31
+ ## Author
32
+
33
+ Jon-Michael Deldin <dev@jmdeldin.com>
34
+
35
+ https://github.com/jmdeldin/algoplot
36
+
37
+ Feedback and patches are welcome!
@@ -0,0 +1,13 @@
1
+ require 'rake/clean'
2
+ require 'rake/testtask'
3
+
4
+ CLEAN.include '*.gem'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.test_files = FileList['spec/*_spec.rb']
8
+ end
9
+
10
+ desc 'build gem'
11
+ task :gemify do
12
+ sh "gem build algoplot.gemspec"
13
+ end
@@ -0,0 +1,16 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'algoplot'
3
+ s.version = '0.0.1'
4
+ s.date = '2012-05-15'
5
+ s.summary = "algoplot is a script to visually compare algorithms."
6
+ s.description =
7
+ "algoplot uses Gnuplot to generate growth plots given N and functions."
8
+ s.authors = ["Jon-Michael Deldin"]
9
+ s.email = 'dev@jmdeldin.com'
10
+ s.homepage = 'http://github.com/jmdeldin/algoplot'
11
+ s.files = `git ls-files`.split("\n")
12
+ s.executables = ["algoplot"]
13
+ s.require_path = 'lib'
14
+
15
+ s.add_dependency('gnuplot', '>= 2.4.1')
16
+ end
@@ -0,0 +1,10 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+ # Run `algoplot -h` for usage.
4
+ #
5
+
6
+ require_relative '../lib/algoplot/options'
7
+
8
+ opts = AlgoPlot::Options.new(ARGV).options
9
+
10
+ AlgoPlot::plot(opts[:n], opts[:functions], opts[:output])
Binary file
@@ -0,0 +1,34 @@
1
+ require 'gnuplot'
2
+
3
+ module AlgoPlot
4
+
5
+ def self.get_extension(filename)
6
+ File.extname(filename.downcase).delete('.')
7
+ end
8
+
9
+ def self.is_extension_allowed?(ext)
10
+ %w(pdf png jpg).include? ext
11
+ end
12
+
13
+ # ds.with = "lines"
14
+ def self.create_dataset(func)
15
+ Gnuplot::DataSet.new(func)
16
+ end
17
+
18
+ def self.plot(n, functions, filename)
19
+ ext = get_extension(filename)
20
+ fail "Extension #{ext} not allowed" unless is_extension_allowed?(ext)
21
+ Gnuplot.open do |gp|
22
+ Gnuplot::Plot.new(gp) do |plot|
23
+ plot.terminal ext
24
+ plot.output filename
25
+ plot.xrange "[0:#{n}]"
26
+ plot.title "Growth of #{functions.join(', ')}"
27
+ plot.ylabel "steps"
28
+ plot.xlabel "N"
29
+ plot.data = functions.map { |f| create_dataset(f) }
30
+ end
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,52 @@
1
+ require 'optparse'
2
+ require_relative '../algoplot'
3
+
4
+ module AlgoPlot
5
+ class Options
6
+ attr_reader :options
7
+
8
+ SCRIPT = 'algoplot'
9
+
10
+ def initialize(argv)
11
+ @options = {:n => 100, :functions => [], :output => nil}
12
+
13
+ argv = ["-h"] if argv.empty?
14
+ parse_options(argv)
15
+ end
16
+
17
+ private
18
+
19
+ def parse_options(argv)
20
+ parser = OptionParser.new do |opts|
21
+ opts.banner = "Usage: #{SCRIPT} -o FILE.pdf fn1 fn2 [...]"
22
+
23
+ opts.on("-n SIZE", "--size", "input size") do |n|
24
+ @options[:n] = n.to_i
25
+ end
26
+
27
+ opts.on("-o FILE", "--output", "filename (PNG, PDF, JPG)") do |o|
28
+ @options[:output] = o
29
+ end
30
+
31
+ opts.on('-h', '--help', 'show this screen') do
32
+ STDERR.puts opts
33
+ abort
34
+ end
35
+ end
36
+
37
+ parser.parse!(argv)
38
+
39
+ if options[:output].nil?
40
+ raise ArgumentError, "Output filename must be given\n#{parser}"
41
+ end
42
+
43
+ if argv.size < 2
44
+ raise ArgumentError, "At least two functions must be given.\n#{parser}"
45
+ else
46
+ @options[:functions] = argv
47
+ end
48
+ end
49
+
50
+
51
+ end
52
+ end
@@ -0,0 +1,38 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/algoplot'
3
+ require_relative '../lib/algoplot/options'
4
+
5
+ describe :AlgoPlot do
6
+ it 'only accepts JPG, PNG, PDF extensions' do
7
+ %w(pdf png jpg).each do |ext|
8
+ AlgoPlot::is_extension_allowed?(ext).must_equal true
9
+ end
10
+
11
+ AlgoPlot::is_extension_allowed?('gif').wont_equal true
12
+ end
13
+
14
+ it 'separates a file extension from the filename' do
15
+ AlgoPlot::get_extension('example.file.png').must_equal 'png'
16
+ end
17
+
18
+ it 'downcases a file extension' do
19
+ AlgoPlot::get_extension('example.file.PNG').must_equal 'png'
20
+ end
21
+
22
+ it 'creates a DataSet object from a string' do
23
+ AlgoPlot::create_dataset('x**2').must_be_instance_of(Gnuplot::DataSet)
24
+ end
25
+
26
+ it 'writes plots to a file' do
27
+ require 'tempfile'
28
+ file = Tempfile.new(['plot', '.png'])
29
+
30
+ begin
31
+ AlgoPlot::plot(100, %w(x x**2), file.path)
32
+ File.size(file.path).must_be :>, 10
33
+ ensure
34
+ file.close
35
+ file.unlink
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,24 @@
1
+ require 'minitest/autorun'
2
+ require_relative '../lib/algoplot/options'
3
+
4
+ describe :Options do
5
+ subject { AlgoPlot::Options }
6
+
7
+ it 'fails when given fewer than two functions' do
8
+ lambda { subject.new(%w(x)) }.must_raise ArgumentError
9
+ end
10
+
11
+ it 'fails when output is omitted' do
12
+ lambda { subject.new(%w(x x**2)) }.must_raise ArgumentError
13
+ end
14
+
15
+ it 'returns a hash of options' do
16
+ opts = subject.new(%w(-o foo.png -n 2000 x x**2))
17
+
18
+ exp = {:n => 2000, :output => 'foo.png', :functions => %w(x x**2)}
19
+
20
+ opts.options.must_equal exp
21
+ end
22
+
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: algoplot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jon-Michael Deldin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: gnuplot
16
+ requirement: &10669400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.4.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *10669400
25
+ description: algoplot uses Gnuplot to generate growth plots given N and functions.
26
+ email: dev@jmdeldin.com
27
+ executables:
28
+ - algoplot
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - LICENSE
33
+ - README.markdown
34
+ - Rakefile
35
+ - algoplot.gemspec
36
+ - bin/algoplot
37
+ - doc/merge-vs-bubble.png
38
+ - lib/algoplot.rb
39
+ - lib/algoplot/options.rb
40
+ - spec/algoplot_spec.rb
41
+ - spec/options_spec.rb
42
+ homepage: http://github.com/jmdeldin/algoplot
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project:
62
+ rubygems_version: 1.8.11
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: algoplot is a script to visually compare algorithms.
66
+ test_files: []