GraDA 1.0.0

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.
Files changed (2) hide show
  1. data/lib/grada.rb +162 -0
  2. metadata +77 -0
data/lib/grada.rb ADDED
@@ -0,0 +1,162 @@
1
+ require 'gnuplot'
2
+
3
+ class Grada
4
+ class NotValidArrayError < RuntimeError; end
5
+ class NotValidDataError < RuntimeError; end
6
+ class NoPlotDataError < RuntimeError; end
7
+
8
+ attr_reader :x
9
+ attr_reader :y
10
+
11
+ DEFAULT_OPTIONS = {width: 1600,
12
+ height: 400,
13
+ title: "Graph",
14
+ x_label: "X",
15
+ y_label: "Y",
16
+ with: 'lines',
17
+ graph_type: :default}
18
+
19
+
20
+ def self.hi
21
+ puts "Hello GraDA"
22
+ end
23
+
24
+ def initialize(x, y = nil)
25
+ raise NoPlotDataError if ! y.nil? && x.size != y.size
26
+
27
+ @x = validate(x)
28
+ @y = y.nil? ? y : validate(y)
29
+ end
30
+
31
+ def display(opts = {})
32
+ @opts = DEFAULT_OPTIONS.merge(opts)
33
+
34
+ if @opts[:graph_type] == :histogram
35
+ population_data?(@x)
36
+
37
+ plot_histogram do |plot|
38
+ plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
39
+ end
40
+ elsif @opts[:graph_type] == :heatmap
41
+ Matrix.columns(@x) rescue raise NoPlotDataError
42
+ @opts[:with] = 'image'
43
+
44
+ plot_heat_map
45
+ else
46
+ raise NoPlotDataError if @y.nil?
47
+
48
+ plot_and do |plot|
49
+ plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
50
+ end
51
+ end
52
+ end
53
+
54
+ def save(opts = {})
55
+ @opts = DEFAULT_OPTIONS.merge(opts)
56
+
57
+ return nil if @opts[:filename].nil?
58
+
59
+ if @opts[:graph_type] == :histogram
60
+ population_data?(@x)
61
+
62
+ plot_histogram do |plot|
63
+ plot.output @opts[:filename]
64
+ plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
65
+ plot.terminal 'png'
66
+ end
67
+ elsif @opts[:graph_type] == :heatmap
68
+ Matrix.columns(@x) rescue raise NoPlotDataError
69
+ @opts[:with] = 'image'
70
+
71
+ plot_heat_map do |plot|
72
+ plot.output @opts[:filename]
73
+ plot.terminal 'png'
74
+ end
75
+ else
76
+ raise NoPlotDataError if @y.nil?
77
+
78
+ plot_and do |plot|
79
+ plot.output @opts[:filename]
80
+ plot.set "terminal x11 size #{@opts[:width]*10},#{@opts[:height]}"
81
+ plot.terminal 'png'
82
+ end
83
+ end
84
+ end
85
+
86
+ private
87
+
88
+ def validate(l)
89
+ raise NotValidArrayError if ! l.is_a?(Array)
90
+
91
+ l.each do |elem|
92
+ raise NotValidDataError if ! ( elem.is_a?(Float) || elem.is_a?(Integer) || elem.is_a?(Array))
93
+ end
94
+ end
95
+
96
+ def population_data?(l)
97
+ raise NotValidArrayError if ! l.is_a?(Array)
98
+
99
+ l.each do |elem|
100
+ raise NotValidDataError if ! ( elem.is_a?(Float) || elem.is_a?(Integer))
101
+ end
102
+ end
103
+
104
+ def plot_and(&block)
105
+ Gnuplot.open do |gp|
106
+ Gnuplot::Plot.new(gp) do |plot|
107
+ block[plot] if block
108
+
109
+ plot.title @opts[:title]
110
+
111
+ plot.xlabel @opts[:x_label]
112
+ plot.ylabel @opts[:y_label]
113
+
114
+ plot.data << Gnuplot::DataSet.new([@x,@y]) do |ds|
115
+ ds.with = @opts[:with]
116
+ end
117
+ end
118
+ end
119
+ end
120
+
121
+ def plot_histogram(&block)
122
+ Gnuplot.open do |gp|
123
+ Gnuplot::Plot.new(gp) do |plot|
124
+ block[plot] if block
125
+
126
+ plot.title @opts[:title]
127
+
128
+ plot.set "style data histogram"
129
+ plot.xlabel @opts[:x_label]
130
+ plot.ylabel "Frecuency"
131
+
132
+ x = @x.sort.group_by { |xi| xi }.map{|k,v| v.count }
133
+
134
+ plot.data << Gnuplot::DataSet.new(x) do |ds|
135
+ ds.with = @opts[:with]
136
+ end
137
+ end
138
+ end
139
+ end
140
+
141
+ def plot_heat_map(&block)
142
+ Gnuplot.open do |gp|
143
+ Gnuplot::Plot.new(gp) do |plot|
144
+ block[plot] if block
145
+
146
+ plot.set "pm3d map"
147
+ plot.set "palette color"
148
+ plot.set "xrange [0:#{@x.size-1}]"
149
+ plot.set "yrange [0:#{@x.size-1}]"
150
+ plot.set "cbrange [#{@opts[:min]}:#{@opts[:max]}]"
151
+ plot.set "cblabel \"#{@opts[:x_label]}\""
152
+ plot.set "palette model RGB"
153
+ plot.set "palette define"
154
+
155
+ plot.title @opts[:title]
156
+ plot.data = [Gnuplot::DataSet.new(Matrix.columns(@x)) do |ds|
157
+ ds.with = @opts[:with]
158
+ end]
159
+ end
160
+ end
161
+ end
162
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: GraDA
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Enrique Figuerola
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.11.0
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.11.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Graphic Data Analysis gem
47
+ email: hard_rock15@msn.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - lib/grada.rb
53
+ homepage: http://rubygems.org/gems/grada
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.23
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: GraDa
77
+ test_files: []