gnuplotrb 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/gnuplotrb.rb CHANGED
@@ -1,35 +1,35 @@
1
- require 'tempfile'
2
- require 'hamster'
3
- require 'open3'
4
- require 'base64'
5
-
6
- ##
7
- # Require gem if it's available in current gemspace.
8
- #
9
- # @param name [String] gem name
10
- # @return [Boolean] true if gem was loaded, false otherwise
11
- def require_if_available(name)
12
- require name
13
- rescue LoadError
14
- false
15
- end
16
-
17
- require_if_available('daru')
18
-
19
- require 'gnuplotrb/external_classes/string'
20
- require 'gnuplotrb/external_classes/array'
21
- require 'gnuplotrb/external_classes/daru'
22
-
23
- require 'gnuplotrb/version'
24
- require 'gnuplotrb/staff/settings'
25
- require 'gnuplotrb/mixins/option_handling'
26
- require 'gnuplotrb/mixins/error_handling'
27
- require 'gnuplotrb/mixins/plottable'
28
- require 'gnuplotrb/staff/terminal'
29
- require 'gnuplotrb/staff/datablock'
30
- require 'gnuplotrb/staff/dataset'
31
- require 'gnuplotrb/fit'
32
- require 'gnuplotrb/plot'
33
- require 'gnuplotrb/splot'
34
- require 'gnuplotrb/multiplot'
35
- require 'gnuplotrb/animation'
1
+ require 'tempfile'
2
+ require 'hamster'
3
+ require 'open3'
4
+ require 'base64'
5
+
6
+ ##
7
+ # Require gem if it's available in current gemspace.
8
+ #
9
+ # @param name [String] gem name
10
+ # @return [Boolean] true if gem was loaded, false otherwise
11
+ def require_if_available(name)
12
+ require name
13
+ rescue LoadError
14
+ false
15
+ end
16
+
17
+ require_if_available('daru')
18
+
19
+ require 'gnuplotrb/external_classes/string'
20
+ require 'gnuplotrb/external_classes/array'
21
+ require 'gnuplotrb/external_classes/daru'
22
+
23
+ require 'gnuplotrb/version'
24
+ require 'gnuplotrb/staff/settings'
25
+ require 'gnuplotrb/mixins/option_handling'
26
+ require 'gnuplotrb/mixins/error_handling'
27
+ require 'gnuplotrb/mixins/plottable'
28
+ require 'gnuplotrb/staff/terminal'
29
+ require 'gnuplotrb/staff/datablock'
30
+ require 'gnuplotrb/staff/dataset'
31
+ require 'gnuplotrb/fit'
32
+ require 'gnuplotrb/plot'
33
+ require 'gnuplotrb/splot'
34
+ require 'gnuplotrb/multiplot'
35
+ require 'gnuplotrb/animation'
@@ -1,129 +1,129 @@
1
- module GnuplotRB
2
- ##
3
- # Animation allows to create gif animation with given plots
4
- # as frames. Possible frames: Plot, Splot, Multiplot.
5
- # More about its usage in
6
- # {animation notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/animated_plots.ipynb].
7
- #
8
- # == Options
9
- # Animations has several specific options:
10
- # * animate - allows to get animated gif's. Possible values are true (just turn on animation),
11
- # ot hash with suboptions (:loop - count of loops, default 0 - infinity$;
12
- # :delay - delay between frames; :optimize - boolean, reduces file size).
13
- # * size - size of gif file in pixels (size: [500, 500]) or (size: 500)
14
- # * background - background color
15
- # * transparent
16
- # * enhanced
17
- # * font
18
- # * fontscale
19
- # * crop
20
- #
21
- # Animation ignores :term option and does not have methods like #to_png or #to_svg.
22
- # One can also set animation any options related to Plot and they will be considered
23
- # by all nested plots (if they does not override it with their own values).
24
- #
25
- # Animation inherits all plot array handling methods from Multiplot
26
- # and adds aliases for them (#plots -> #frames; #update_frame! -> #update_plot!; etc).
27
- class Animation < Multiplot
28
- ##
29
- # *Plot* here is also named as *frame*
30
- alias_method :frames, :plots
31
- alias_method :update_frame, :update_plot
32
- alias_method :replace_frame, :replace_plot
33
- alias_method :add_frame, :add_plot
34
- alias_method :add_frames, :add_plots
35
- alias_method :remove_frame, :remove_plot
36
- alias_method :update_frame!, :update_plot!
37
- alias_method :replace_frame!, :replace_plot!
38
- alias_method :add_frame!, :add_plot!
39
- alias_method :add_frames!, :add_plots!
40
- alias_method :remove_frame!, :remove_plot!
41
-
42
- ##
43
- # This method creates a gif animation where frames are plots
44
- # already contained by Animation object.
45
- #
46
- # Options passed in #plot have priority over those which were set before.
47
- #
48
- # Inner options of Plots have the highest priority (except
49
- # :term and :output which are ignored).
50
- #
51
- # @param path [String] path to new gif file that will be created as a result
52
- # @param options [Hash] see note about available options in top class documentation
53
- # @return [nil] if path to output file given
54
- # @return [String] gif file contents if no path to output file given
55
- def plot(path = nil, **options)
56
- options[:output] ||= path
57
- plot_options = mix_options(options) do |plot_opts, anim_opts|
58
- plot_opts.merge(term: ['gif', anim_opts])
59
- end.to_h
60
- need_output = plot_options[:output].nil?
61
- plot_options[:output] = Dir::Tmpname.make_tmpname('anim', 0) if need_output
62
- terminal = Terminal.new
63
- multiplot(terminal, plot_options)
64
- # guaranteed wait for plotting to finish
65
- terminal.close
66
- if need_output
67
- result = File.binread(plot_options[:output])
68
- File.delete(plot_options[:output])
69
- else
70
- result = nil
71
- end
72
- result
73
- end
74
-
75
- ##
76
- # #to_|term_name| methods are not supported by animation
77
- def to_specific_term(*_)
78
- fail 'Specific terminals are not supported by Animation'
79
- end
80
-
81
- ##
82
- # This method is used to embed gif animations
83
- # into iRuby notebooks.
84
- def to_iruby
85
- gif_base64 = Base64.encode64(plot)
86
- ['text/html', "<img src=\"data:image/gif;base64, #{gif_base64}\">"]
87
- end
88
-
89
- private
90
-
91
- ##
92
- # Dafault options to be used for that plot
93
- def default_options
94
- {
95
- animate: {
96
- delay: 10,
97
- loop: 0,
98
- optimize: true
99
- }
100
- }
101
- end
102
-
103
- ##
104
- # This plot have some specific options which
105
- # should be handled different way than others.
106
- # Here are keys of this options.
107
- def specific_keys
108
- %w(
109
- animate
110
- size
111
- background
112
- transparent
113
- enhanced
114
- rounded
115
- butt
116
- linewidth
117
- dashlength
118
- tiny
119
- small
120
- medium
121
- large
122
- giant
123
- font
124
- fontscale
125
- crop
126
- )
127
- end
128
- end
129
- end
1
+ module GnuplotRB
2
+ ##
3
+ # Animation allows to create gif animation with given plots
4
+ # as frames. Possible frames: Plot, Splot, Multiplot.
5
+ # More about its usage in
6
+ # {animation notebook}[http://nbviewer.ipython.org/github/dilcom/gnuplotrb/blob/master/notebooks/animated_plots.ipynb].
7
+ #
8
+ # == Options
9
+ # Animations has several specific options:
10
+ # * animate - allows to get animated gif's. Possible values are true (just turn on animation),
11
+ # ot hash with suboptions (:loop - count of loops, default 0 - infinity$;
12
+ # :delay - delay between frames; :optimize - boolean, reduces file size).
13
+ # * size - size of gif file in pixels (size: [500, 500]) or (size: 500)
14
+ # * background - background color
15
+ # * transparent
16
+ # * enhanced
17
+ # * font
18
+ # * fontscale
19
+ # * crop
20
+ #
21
+ # Animation ignores :term option and does not have methods like #to_png or #to_svg.
22
+ # One can also set animation any options related to Plot and they will be considered
23
+ # by all nested plots (if they does not override it with their own values).
24
+ #
25
+ # Animation inherits all plot array handling methods from Multiplot
26
+ # and adds aliases for them (#plots -> #frames; #update_frame! -> #update_plot!; etc).
27
+ class Animation < Multiplot
28
+ ##
29
+ # *Plot* here is also named as *frame*
30
+ alias_method :frames, :plots
31
+ alias_method :update_frame, :update_plot
32
+ alias_method :replace_frame, :replace_plot
33
+ alias_method :add_frame, :add_plot
34
+ alias_method :add_frames, :add_plots
35
+ alias_method :remove_frame, :remove_plot
36
+ alias_method :update_frame!, :update_plot!
37
+ alias_method :replace_frame!, :replace_plot!
38
+ alias_method :add_frame!, :add_plot!
39
+ alias_method :add_frames!, :add_plots!
40
+ alias_method :remove_frame!, :remove_plot!
41
+
42
+ ##
43
+ # This method creates a gif animation where frames are plots
44
+ # already contained by Animation object.
45
+ #
46
+ # Options passed in #plot have priority over those which were set before.
47
+ #
48
+ # Inner options of Plots have the highest priority (except
49
+ # :term and :output which are ignored).
50
+ #
51
+ # @param path [String] path to new gif file that will be created as a result
52
+ # @param options [Hash] see note about available options in top class documentation
53
+ # @return [nil] if path to output file given
54
+ # @return [String] gif file contents if no path to output file given
55
+ def plot(path = nil, **options)
56
+ options[:output] ||= path
57
+ plot_options = mix_options(options) do |plot_opts, anim_opts|
58
+ plot_opts.merge(term: ['gif', anim_opts])
59
+ end.to_h
60
+ need_output = plot_options[:output].nil?
61
+ plot_options[:output] = Dir::Tmpname.make_tmpname('anim', 0) if need_output
62
+ terminal = Terminal.new
63
+ multiplot(terminal, plot_options)
64
+ # guaranteed wait for plotting to finish
65
+ terminal.close
66
+ if need_output
67
+ result = File.binread(plot_options[:output])
68
+ File.delete(plot_options[:output])
69
+ else
70
+ result = nil
71
+ end
72
+ result
73
+ end
74
+
75
+ ##
76
+ # #to_|term_name| methods are not supported by animation
77
+ def to_specific_term(*_)
78
+ fail 'Specific terminals are not supported by Animation'
79
+ end
80
+
81
+ ##
82
+ # This method is used to embed gif animations
83
+ # into iRuby notebooks.
84
+ def to_iruby
85
+ gif_base64 = Base64.encode64(plot)
86
+ ['text/html', "<img src=\"data:image/gif;base64, #{gif_base64}\">"]
87
+ end
88
+
89
+ private
90
+
91
+ ##
92
+ # Dafault options to be used for that plot
93
+ def default_options
94
+ {
95
+ animate: {
96
+ delay: 10,
97
+ loop: 0,
98
+ optimize: true
99
+ }
100
+ }
101
+ end
102
+
103
+ ##
104
+ # This plot have some specific options which
105
+ # should be handled different way than others.
106
+ # Here are keys of this options.
107
+ def specific_keys
108
+ %w(
109
+ animate
110
+ size
111
+ background
112
+ transparent
113
+ enhanced
114
+ rounded
115
+ butt
116
+ linewidth
117
+ dashlength
118
+ tiny
119
+ small
120
+ medium
121
+ large
122
+ giant
123
+ font
124
+ fontscale
125
+ crop
126
+ )
127
+ end
128
+ end
129
+ end
@@ -1,17 +1,17 @@
1
- ##
2
- # Methods to take data for GnuplotRB plots.
3
- class Array
4
- # taken for example from current gnuplot bindings
5
- # @return [String] array converted to Gnuplot format
6
- def to_gnuplot_points
7
- return '' if self.empty?
8
- case self[0]
9
- when Array
10
- self[0].zip(*self[1..-1]).map { |a| a.join(' ') }.join("\n")
11
- when Numeric
12
- join("\n")
13
- else
14
- self[0].zip(*self[1..-1]).to_gnuplot_points
15
- end
16
- end
17
- end
1
+ ##
2
+ # Methods to take data for GnuplotRB plots.
3
+ class Array
4
+ # taken for example from current gnuplot bindings
5
+ # @return [String] array converted to Gnuplot format
6
+ def to_gnuplot_points
7
+ return '' if self.empty?
8
+ case self[0]
9
+ when Array
10
+ self[0].zip(*self[1..-1]).map { |a| a.join(' ') }.join("\n")
11
+ when Numeric
12
+ join("\n")
13
+ else
14
+ self[0].zip(*self[1..-1]).to_gnuplot_points
15
+ end
16
+ end
17
+ end
@@ -1,43 +1,43 @@
1
- if defined? Daru
2
- ##
3
- # See {daru}[https://github.com/v0dro/daru] and
4
- # {plotting from daru}[https://github.com/dilcom/gnuplotrb/blob/master/notebooks/plotting_from_daru.ipynb]
5
- module Daru
6
- ##
7
- # Methods to take data for GnuplotRB plots.
8
- class DataFrame
9
- ##
10
- # Convert DataFrame to Gnuplot format.
11
- #
12
- # @return [String] data converted to Gnuplot format
13
- def to_gnuplot_points
14
- result = ''
15
- each_row_with_index do |row, index|
16
- quoted = index.is_a?(String) || index.is_a?(Symbol)
17
- result += quoted ? "\"#{index}\" " : "#{index} "
18
- result += row.to_a.join(' ')
19
- result += "\n"
20
- end
21
- result
22
- end
23
- end
24
-
25
- ##
26
- # Methods to take data for GnuplotRB plots.
27
- class Vector
28
- ##
29
- # Convert Vector to Gnuplot format.
30
- #
31
- # @return [String] data converted to Gnuplot format
32
- def to_gnuplot_points
33
- result = ''
34
- each_with_index do |value, index|
35
- quoted = index.is_a?(String) || index.is_a?(Symbol)
36
- result += quoted ? "\"#{index}\" " : "#{index} "
37
- result += "#{value}\n"
38
- end
39
- result
40
- end
41
- end
42
- end
43
- end
1
+ if defined? Daru
2
+ ##
3
+ # See {daru}[https://github.com/v0dro/daru] and
4
+ # {plotting from daru}[https://github.com/dilcom/gnuplotrb/blob/master/notebooks/plotting_from_daru.ipynb]
5
+ module Daru
6
+ ##
7
+ # Methods to take data for GnuplotRB plots.
8
+ class DataFrame
9
+ ##
10
+ # Convert DataFrame to Gnuplot format.
11
+ #
12
+ # @return [String] data converted to Gnuplot format
13
+ def to_gnuplot_points
14
+ result = ''
15
+ each_row_with_index do |row, index|
16
+ quoted = (index.is_a?(String) || index.is_a?(Symbol)) && index.length > 0
17
+ result += quoted ? "\"#{index}\" " : "#{index} "
18
+ result += row.to_a.join(' ')
19
+ result += "\n"
20
+ end
21
+ result
22
+ end
23
+ end
24
+
25
+ ##
26
+ # Methods to take data for GnuplotRB plots.
27
+ class Vector
28
+ ##
29
+ # Convert Vector to Gnuplot format.
30
+ #
31
+ # @return [String] data converted to Gnuplot format
32
+ def to_gnuplot_points
33
+ result = ''
34
+ each_with_index do |value, index|
35
+ quoted = (index.is_a?(String) || index.is_a?(Symbol)) && index.length > 0
36
+ result += quoted ? "\"#{index}\" " : "#{index} "
37
+ result += "#{value}\n"
38
+ end
39
+ result
40
+ end
41
+ end
42
+ end
43
+ end