grada 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/grada.rb +241 -0
- metadata +93 -0
data/lib/grada.rb
ADDED
@@ -0,0 +1,241 @@
|
|
1
|
+
require 'gnuplot'
|
2
|
+
|
3
|
+
class Grada
|
4
|
+
# Not valid the format of the object to construct the graph
|
5
|
+
#
|
6
|
+
class NotValidArrayError < RuntimeError; end
|
7
|
+
|
8
|
+
# Not valid the content of the array you're passing to build the graph
|
9
|
+
#
|
10
|
+
class NotValidDataError < RuntimeError; end
|
11
|
+
|
12
|
+
# Can't build the plot
|
13
|
+
#
|
14
|
+
class NoPlotDataError < RuntimeError; end
|
15
|
+
|
16
|
+
attr_reader :x
|
17
|
+
attr_reader :y
|
18
|
+
|
19
|
+
DEFAULT_OPTIONS = {width: 1600,
|
20
|
+
height: 400,
|
21
|
+
title: "Graph",
|
22
|
+
x_label: "X",
|
23
|
+
y_label: "Y",
|
24
|
+
with: 'lines',
|
25
|
+
graph_type: :default}
|
26
|
+
|
27
|
+
# Hello GraDA
|
28
|
+
#
|
29
|
+
|
30
|
+
def self.hi
|
31
|
+
puts "Hello GraDA"
|
32
|
+
end
|
33
|
+
|
34
|
+
# Initialize object with the data you want to plot.
|
35
|
+
# It can vary depending on the type of graph.
|
36
|
+
# The second argument is optional.
|
37
|
+
#
|
38
|
+
# Example:
|
39
|
+
# >> radiation_levels_median_per_day = [0.001,0.01,1,10,1000]
|
40
|
+
# >> radiation_days = [0,1,2,3,4]
|
41
|
+
# >> grada = Grada.new(radiation_days, radiation_levels_median_per_day)
|
42
|
+
# => #<Grada:0x007f962a8dc9b8 @x=[0, 1, 2, 3, 4], @y=[0.001, 0.01, 1, 10, 1000]>
|
43
|
+
# Arguments:
|
44
|
+
# x: (Array)
|
45
|
+
# y: (Array) *optional*
|
46
|
+
|
47
|
+
def initialize(x, y = nil)
|
48
|
+
|
49
|
+
@x = validate(x)
|
50
|
+
@y = y.nil? ? y : validate(y)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Displays a graph in a window.
|
54
|
+
# You can specify all the options that you need:
|
55
|
+
# *width* (Integer)
|
56
|
+
# *height* (Integer)
|
57
|
+
# *title* (Integer)
|
58
|
+
# *x_label* (String)
|
59
|
+
# *y_label* (String)
|
60
|
+
# *graph_type* (:histogram, :heatmap) default: :default
|
61
|
+
# *with* ('points', 'linespoints') default: 'lines'
|
62
|
+
#
|
63
|
+
#
|
64
|
+
# Example:
|
65
|
+
# >> grada.display
|
66
|
+
# => ""
|
67
|
+
# >> grada.display({ title: 'Atomic Device X', x_label: 'Day', y_label: 'smSv', with: 'points' })
|
68
|
+
# => ""
|
69
|
+
# Arguments:
|
70
|
+
# opts: (Hash) *optional*
|
71
|
+
|
72
|
+
def display(opts = {})
|
73
|
+
@opts = DEFAULT_OPTIONS.merge(opts)
|
74
|
+
|
75
|
+
if @opts[:graph_type] == :histogram
|
76
|
+
population_data?(@x)
|
77
|
+
|
78
|
+
plot_histogram do |plot|
|
79
|
+
plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
|
80
|
+
end
|
81
|
+
elsif @opts[:graph_type] == :heatmap
|
82
|
+
Matrix.columns(@x) rescue raise NoPlotDataError
|
83
|
+
@opts[:with] = 'image'
|
84
|
+
|
85
|
+
plot_heat_map
|
86
|
+
else
|
87
|
+
raise NoPlotDataError if @y.nil?
|
88
|
+
|
89
|
+
plot_and do |plot|
|
90
|
+
plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Save the graph in a png file.
|
96
|
+
# You can specify all the options that you need as _display_ but also need to specify the file
|
97
|
+
#
|
98
|
+
# Example:
|
99
|
+
# >> grada.save({ filename: 'secret/radiation_levels/ffa/zonex/devicex/radiation_level_malaga.png' ,title: 'Atomic Device X', x_label: 'Day', y_label: 'smSv', with: 'points' })
|
100
|
+
# => ""
|
101
|
+
# Arguments:
|
102
|
+
# opts: (Hash) *optional*
|
103
|
+
|
104
|
+
def save(opts = {})
|
105
|
+
@opts = DEFAULT_OPTIONS.merge(opts)
|
106
|
+
|
107
|
+
return nil if @opts[:filename].nil?
|
108
|
+
|
109
|
+
if @opts[:graph_type] == :histogram
|
110
|
+
population_data?(@x)
|
111
|
+
|
112
|
+
plot_histogram do |plot|
|
113
|
+
plot.output @opts[:filename]
|
114
|
+
plot.set "terminal x11 size #{@opts[:width]},#{@opts[:height]}"
|
115
|
+
plot.terminal 'png'
|
116
|
+
end
|
117
|
+
elsif @opts[:graph_type] == :heatmap
|
118
|
+
Matrix.columns(@x) rescue raise NoPlotDataError
|
119
|
+
@opts[:with] = 'image'
|
120
|
+
|
121
|
+
plot_heat_map do |plot|
|
122
|
+
plot.output @opts[:filename]
|
123
|
+
plot.terminal 'png'
|
124
|
+
end
|
125
|
+
else
|
126
|
+
raise NoPlotDataError if @y.nil?
|
127
|
+
|
128
|
+
plot_and do |plot|
|
129
|
+
plot.output @opts[:filename]
|
130
|
+
plot.set "terminal x11 size #{@opts[:width]*10},#{@opts[:height]}"
|
131
|
+
plot.terminal 'png'
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
private
|
137
|
+
|
138
|
+
def validate(l)
|
139
|
+
raise NotValidArrayError if ! l.is_a?(Array)
|
140
|
+
|
141
|
+
l.each do |elem|
|
142
|
+
raise NotValidDataError if ! ( elem.is_a?(Float) || elem.is_a?(Integer) || elem.is_a?(Array) || elem.is_a?(Hash))
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def population_data?(l)
|
147
|
+
raise NotValidArrayError if ! l.is_a?(Array)
|
148
|
+
|
149
|
+
l.each do |elem|
|
150
|
+
raise NotValidDataError if ! ( elem.is_a?(Float) || elem.is_a?(Integer))
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def multiple_data?(l)
|
155
|
+
if l.is_a?(Array)
|
156
|
+
l.each do |elem|
|
157
|
+
return false if ! elem.is_a?(Hash)
|
158
|
+
end
|
159
|
+
|
160
|
+
return true
|
161
|
+
end
|
162
|
+
|
163
|
+
false
|
164
|
+
end
|
165
|
+
|
166
|
+
def plot_and(&block)
|
167
|
+
::Gnuplot.open do |gp|
|
168
|
+
::Gnuplot::Plot.new(gp) do |plot|
|
169
|
+
block[plot] if block
|
170
|
+
|
171
|
+
plot.title @opts[:title]
|
172
|
+
|
173
|
+
plot.xlabel @opts[:x_label]
|
174
|
+
plot.ylabel @opts[:y_label]
|
175
|
+
|
176
|
+
if multiple_data?(@y)
|
177
|
+
@y.each do |dic|
|
178
|
+
dic.each do |k, v|
|
179
|
+
if k.to_sym != :with
|
180
|
+
raise NoPlotDataError if ! v.nil? && @x.size != v.size
|
181
|
+
|
182
|
+
plot.data << ::Gnuplot::DataSet.new([@x,v]) do |ds|
|
183
|
+
ds.with = dic[:with] || @opts[:with]
|
184
|
+
ds.title = "#{k}"
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
else
|
190
|
+
raise NoPlotDataError if ! @y.nil? && @x.size != @y.size
|
191
|
+
|
192
|
+
plot.data << ::Gnuplot::DataSet.new([@x,@y]) do |ds|
|
193
|
+
ds.with = @opts[:with]
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def plot_histogram(&block)
|
201
|
+
::Gnuplot.open do |gp|
|
202
|
+
::Gnuplot::Plot.new(gp) do |plot|
|
203
|
+
block[plot] if block
|
204
|
+
|
205
|
+
plot.title @opts[:title]
|
206
|
+
|
207
|
+
plot.set "style data histogram"
|
208
|
+
plot.xlabel @opts[:x_label]
|
209
|
+
plot.ylabel "Frecuency"
|
210
|
+
|
211
|
+
x = @x.sort.group_by { |xi| xi }.map{|k,v| v.count }
|
212
|
+
|
213
|
+
plot.data << ::Gnuplot::DataSet.new(x) do |ds|
|
214
|
+
ds.with = @opts[:with]
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def plot_heat_map(&block)
|
221
|
+
::Gnuplot.open do |gp|
|
222
|
+
::Gnuplot::Plot.new(gp) do |plot|
|
223
|
+
block[plot] if block
|
224
|
+
|
225
|
+
plot.set "pm3d map"
|
226
|
+
plot.set "palette color"
|
227
|
+
plot.set "xrange [0:#{@x.size-1}]"
|
228
|
+
plot.set "yrange [0:#{@x.size-1}]"
|
229
|
+
plot.set "cbrange [#{@opts[:min]}:#{@opts[:max]}]"
|
230
|
+
plot.set "cblabel \"#{@opts[:x_label]}\""
|
231
|
+
plot.set "palette model RGB"
|
232
|
+
plot.set "palette define"
|
233
|
+
|
234
|
+
plot.title @opts[:title]
|
235
|
+
plot.data = [::Gnuplot::DataSet.new(Matrix.columns(@x)) do |ds|
|
236
|
+
ds.with = @opts[:with]
|
237
|
+
end]
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grada
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.1
|
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
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: gnuplot
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.6.2
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.6.2
|
62
|
+
description: Graphic Data Analysis gem
|
63
|
+
email: hard_rock15@msn.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- lib/grada.rb
|
69
|
+
homepage: https://github.com/emfigo/grada
|
70
|
+
licenses: []
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 1.8.23
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: GraDA
|
93
|
+
test_files: []
|