statsample 0.15.1 → 0.16.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.
metadata.gz.sig CHANGED
Binary file
@@ -1,45 +0,0 @@
1
- require 'GDChart'
2
- module Statsample
3
- module Util # :nodoc:
4
- class << self
5
- def chart_gdchart(file,width,height,chart_type, labels, options,num_datasets,data)
6
- require 'GDChart'
7
- gdc=GDChart.new
8
- gdc.title="Generic title"
9
- gdc.bg_color=0xFFFFFF
10
- gdc.image_type=GDChart::JPEG
11
- options.each{|k,v|
12
- gdc.send(k+"=",v)
13
- }
14
-
15
- File.open(file,"w") {|f|
16
- gdc.out_graph(width,height,f,chart_type, data.length/num_datasets,labels,num_datasets,data)
17
- }
18
- end
19
- end
20
- end
21
- class Vector
22
- # Creates a barchart using ruby-gdchart
23
- def gdchart_frequencies(file, width=300, height=150, chart_type=GDChart::BAR, options={}) # :nodoc:
24
- labels,data=[],[]
25
- self.frequencies.sort.each{|k,v|
26
- labels.push(k.to_s)
27
- data.push(v)
28
- }
29
- options['ext_color']=[0xFF3399,0xFF9933,0xFFEE33,0x33FF33, 0x9966FF]
30
- Statsample::Util.chart_gdchart(file,width,height,chart_type, labels,options,1,data)
31
- end
32
- def gdchart_histogram(bins,file, width=300, height=150, chart_type=GDChart::BAR, options={}) # :nodoc:
33
- check_type :scale
34
- labels=[]
35
- h=histogram(bins)
36
- data=[]
37
- (0...bins).each{|bin|
38
- data.push(h[bin])
39
- range=h.get_range(bin)
40
- labels.push(((range[0]+range[1]) / 2.to_f).to_s)
41
- }
42
- Statsample::Util.chart_gdchart(file, width, height, chart_type, labels,options, 1,data)
43
- end
44
- end
45
- end
@@ -1,108 +0,0 @@
1
- module Statsample
2
- module Graph
3
- class SvgBoxplot < SVG::Graph::Bar # :nodoc:
4
- def initialize(config={})
5
- config[:fields]=["dummy"]
6
- super(config)
7
- end
8
- def get_x_labels
9
- @data.collect{|d|
10
- d[:title]
11
- }
12
- end
13
-
14
- def min_value
15
- min = 0
16
- if min_scale_value.nil?
17
- min = @data.collect{|x| x[:data].min}.min
18
- if min > 0
19
- if min > 10
20
- min=min-2
21
- else
22
- min=0
23
- end
24
- end
25
- else
26
- min = min_scale_value
27
- end
28
- return min
29
- end
30
- def draw_data
31
- minvalue = min_value
32
- fieldwidth = field_width
33
- unit_size = (@graph_height.to_f - font_size*2*top_font) /
34
- (get_y_labels.max - get_y_labels.min)
35
- bargap = bar_gap ? (fieldwidth < 10 ? fieldwidth / 2 : 10) : 0
36
-
37
- bar_width = fieldwidth - bargap
38
- bar_width /= @data.length if stack == :side
39
- x_mod = (@graph_width-bargap) / 2 - (stack==:side ? bar_width/2 : 0)
40
-
41
- bottom = @graph_height
42
-
43
- field_count = 0
44
- for dataset in @data
45
-
46
- # cases (assume 0 = +ve):
47
- # value min length
48
- # +ve +ve value - min
49
- # +ve -ve value - 0
50
- # -ve -ve value.abs - 0
51
-
52
- min=dataset[:data].min
53
- max=dataset[:data].max
54
- median=dataset[:vector].median
55
- q1=dataset[:vector].percentil(25)
56
- q3=dataset[:vector].percentil(75)
57
- iqr=q3-q1
58
- left = (fieldwidth * field_count)
59
- #length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size
60
- # top is 0 if value is negative
61
- top_wisk=(q3+iqr*1.5 < max) ? q3+iqr*1.5 : max
62
- down_wisk= (q1-iqr*1.5 > min) ? q1-iqr*1.5 : min
63
-
64
- top=@graph_height-((top_wisk-minvalue)*unit_size)
65
- down=@graph_height-((down_wisk-minvalue)*unit_size)
66
-
67
- median_bar=@graph_height-((median-minvalue)*unit_size)
68
- middle= left+(bar_width / 2)
69
- left_whis=left+(bar_width * 0.4)
70
- rigth_whis=left+(bar_width*0.6)
71
- left_rect= left+(bar_width * 0.25)
72
- rigth_rect = left+ (bar_width * 0.75)
73
- top_rect=@graph_height-((q3-minvalue)*unit_size)
74
- height_rect=iqr*unit_size
75
- path="M #{left_whis} #{top} H #{rigth_whis} M #{middle} #{top} V #{down} M #{left_whis} #{down} H #{rigth_whis} M #{left_rect} #{median_bar} H #{rigth_rect}"
76
-
77
-
78
- # Marcamos Outliers
79
- if top_wisk!=max or down_wisk!=min
80
- dataset[:vector].valid_data.each{|d|
81
- if(d<down_wisk) or (d>top_wisk)
82
- y_out=(@graph_height - (d -minvalue)*unit_size).to_s
83
- @graph.add_element( "circle", {
84
- "cx" => (middle).to_s,
85
- "cy" => y_out,
86
- "r" => "3",
87
- "class" => "dataPoint#{field_count+1}"
88
- })
89
- @graph.add_element( "text", {
90
- "x" => (middle+20).to_s,
91
- "y" => y_out,
92
- "class" => "dataPointLabel",
93
- "style" => "#{style} stroke: #000;"
94
- }).text = d.to_s end
95
- }
96
- end
97
-
98
- @graph.add_element( "rect", { "x" => left_rect.to_s, "y" => top_rect.to_s, "width" => (bar_width / 2).to_s, "height" => (height_rect).to_s, "class" => "fill#{field_count+1}"})
99
-
100
- @graph.add_element("path",{"d"=>path, "style"=>"stroke:black;stroke-width:2"})
101
-
102
- field_count += 1
103
- end
104
-
105
- end
106
- end
107
- end
108
- end
@@ -1,184 +0,0 @@
1
- require 'SVG/Graph/Bar'
2
- require 'SVG/Graph/BarHorizontal'
3
- require 'SVG/Graph/Pie'
4
- require 'SVG/Graph/Line'
5
- require 'SVG/Graph/Plot'
6
-
7
- module Statsample
8
- class Vector
9
- # Creates a barchart using ruby-gdchart
10
- def svggraph_frequencies(file, width=600, height=300, chart_type=SVG::Graph::BarNoOp, options={})
11
- labels, data1=[],[]
12
- self.frequencies.sort.each{|k,v|
13
- labels.push(k.to_s)
14
- data1.push(v)
15
- }
16
- options[:height]=height
17
- options[:width]=width
18
- options[:fields]=labels
19
- graph = chart_type.new(options)
20
- graph.add_data(
21
- :data => data1,
22
- :title => "Frequencies"
23
- )
24
- if file.respond_to? :write
25
- file.write(graph.burn)
26
- else
27
- File.open(file.to_s,"wb") {|f|
28
- f.puts(graph.burn)
29
- }
30
- end
31
-
32
- end
33
-
34
- def svggraph_histogram(bins, options={})
35
- check_type :scale
36
- options={:graph_title=>"Histogram", :show_graph_title=>true,:show_normal=>true, :mean=>self.mean, :sigma=>sdp }.merge! options
37
- graph = Statsample::Graph::SvgHistogram.new(options)
38
- graph.histogram=histogram(bins)
39
- graph
40
- end
41
- # Returns a Run-Sequence Plot
42
- # Reference: http://www.itl.nist.gov/div898/handbook/eda/section3/runseqpl.htm
43
- def svggraph_runsequence_plot(options={})
44
- check_type :scale
45
- options={:graph_title=>"Run-Sequence Plot", :show_graph_title=>true, :scale_x_integers => true, :add_popups=>true }.merge! options
46
- vx=(1..@data.size).to_a.to_vector(:scale)
47
- vy=@data.to_vector(:scale)
48
- ds={'index'=>vx,'value'=>vy}.to_dataset
49
- graph = Statsample::Graph::SvgScatterplot.new(ds,options)
50
- graph.set_x('index')
51
- graph.parse
52
- graph
53
- end
54
- def svggraph_boxplot(options={})
55
- check_type :scale
56
- options={:graph_title=>"Boxplot", :fields=>['vector'], :show_graph_title=>true}.merge! options
57
- vx=@valid_data.to_a.to_vector(:scale)
58
- graph = Statsample::Graph::SvgBoxplot.new(options)
59
- graph.add_data(:title=>"vector", :data=>@data.to_a)
60
- graph
61
- end
62
-
63
- def svggraph_lag_plot(options={})
64
- check_type :scale
65
- options={:graph_title=>"Lag Plot", :show_graph_title=>true}.merge! options
66
- vx=@valid_data[0...(@valid_data.size-1)].to_vector(:scale)
67
- vy=@valid_data[1...@valid_data.size].to_vector(:scale)
68
- ds={'x_minus_1'=>vx,'x'=>vy}.to_dataset
69
- graph = Statsample::Graph::SvgScatterplot.new(ds,options)
70
- graph.set_x('x_minus_1')
71
- graph.parse
72
- graph
73
- end
74
- # Returns a Normal Probability Plot
75
- # Reference: http://www.itl.nist.gov/div898/handbook/eda/section3/normprpl.htm
76
- def svggraph_normalprobability_plot(options={})
77
- extend Statsample::Util
78
- check_type :scale
79
- options={:graph_title=>"Normal Probability Plot", :show_graph_title=>true}.merge! options
80
- n=@valid_data.size
81
- vx=(1..@valid_data.size).to_a.collect{|i|
82
- Distribution::Normal.p_value(normal_order_statistic_medians(i,n))
83
- }.to_vector(:scale)
84
- vy=@valid_data.sort.to_vector(:scale)
85
- ds={'normal_order_statistics_medians'=>vx, 'ordered_response'=>vy}.to_dataset
86
- graph = Statsample::Graph::SvgScatterplot.new(ds,options)
87
- graph.set_x('normal_order_statistics_medians')
88
- graph.parse
89
- graph
90
- end
91
- end
92
- end
93
-
94
- # replaces all key and fill classes with similar ones, without opacity
95
- # this allows rendering of svg and png on rox and gqview without problems
96
- module SVG #:nodoc:
97
- module Graph
98
- class BarNoOp < Bar # :nodoc:
99
- def get_css; SVG::Graph.get_css_standard; end
100
- end
101
- class BarHorizontalNoOp < BarHorizontal # :nodoc:
102
- def get_css; SVG::Graph.get_css_standard; end
103
- end
104
-
105
- class LineNoOp < Line # :nodoc:
106
- def get_css; SVG::Graph.get_css_standard; end
107
-
108
- end
109
- class PlotNoOp < Plot # :nodoc:
110
- def get_css; SVG::Graph.get_css_standard; end
111
- end
112
- class PieNoOp < Pie # :nodoc:
113
- def get_css; SVG::Graph.get_css_standard; end
114
-
115
- end
116
- class << self
117
- def get_css_standard
118
- return <<-EOL
119
- /* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
120
- .key1,.fill1{
121
- fill: #ff0000;
122
- stroke: none;
123
- stroke-width: 0.5px;
124
- }
125
- .key2,.fill2{
126
- fill: #0000ff;
127
- stroke: none;
128
- stroke-width: 1px;
129
- }
130
- .key3,.fill3{
131
- fill: #00ff00;
132
- stroke: none;
133
- stroke-width: 1px;
134
- }
135
- .key4,.fill4{
136
- fill: #ffcc00;
137
- stroke: none;
138
- stroke-width: 1px;
139
- }
140
- .key5,.fill5{
141
- fill: #00ccff;
142
- stroke: none;
143
- stroke-width: 1px;
144
- }
145
- .key6,.fill6{
146
- fill: #ff00ff;
147
- stroke: none;
148
- stroke-width: 1px;
149
- }
150
- .key7,.fill7{
151
- fill: #00ffff;
152
- stroke: none;
153
- stroke-width: 1px;
154
- }
155
- .key8,.fill8{
156
- fill: #ffff00;
157
- stroke: none;
158
- stroke-width: 1px;
159
- }
160
- .key9,.fill9{
161
- fill: #cc6666;
162
- stroke: none;
163
- stroke-width: 1px;
164
- }
165
- .key10,.fill10{
166
- fill: #663399;
167
- stroke: none;
168
- stroke-width: 1px;
169
- }
170
- .key11,.fill11{
171
- fill: #339900;
172
- stroke: none;
173
- stroke-width: 1px;
174
- }
175
- .key12,.fill12{
176
- fill: #9966FF;
177
- stroke: none;
178
- stroke-width: 1px;
179
- }
180
- EOL
181
- end
182
- end
183
- end
184
- end
@@ -1,206 +0,0 @@
1
- module Statsample
2
- module Graph
3
- class SvgHistogram < SVG::Graph::BarBase # :nodoc:
4
- attr_accessor :inner_margin, :mean, :sigma, :show_normal
5
- def initialize(config)
6
- config[:fields]=[:dummy]
7
- super(config)
8
-
9
- @histogram=nil
10
- end
11
- include REXML
12
-
13
- # In addition to the defaults set in Graph::initialize, sets
14
- # [inner_margin] 14
15
- # [key] false
16
- def set_defaults
17
- super
18
- self.top_align = self.top_font = 0
19
- init_with({
20
- :inner_margin=>16,
21
- :key=>false,
22
- :show_normal=>false
23
- })
24
-
25
- end
26
-
27
- def histogram=(h)
28
- @histogram=h
29
- @data=[{:data=>(0...@histogram.bins).to_a.collect {|i|
30
- @histogram[i]
31
- }}]
32
- end
33
- def get_x_labels
34
- [""]
35
- end
36
-
37
- def get_y_labels
38
- maxvalue = max_value
39
- minvalue = min_value
40
- range = maxvalue - minvalue
41
-
42
- top_pad = range == 0 ? 10 : range / 20.0
43
- scale_range = (maxvalue + top_pad) - minvalue
44
-
45
- scale_division = scale_divisions || (scale_range / 10.0)
46
-
47
- if scale_integers
48
- scale_division = scale_division < 1 ? 1 : scale_division.round
49
- end
50
-
51
- rv = []
52
- maxvalue = maxvalue%scale_division == 0 ?
53
- maxvalue : maxvalue + scale_division
54
- minvalue.step( maxvalue, scale_division ) {|v| rv << v}
55
- rv
56
- end
57
-
58
- def unit_width
59
- (@graph_width-(@inner_margin*2)) / (@histogram.max-@histogram.min).to_f
60
- end
61
- def draw_x_label(v)
62
- left = (v - @histogram.min)*unit_width
63
- x=@inner_margin+left
64
- text = @graph.add_element( "text" )
65
- text.attributes["class"] = "xAxisLabels"
66
- text.text = sprintf("%0.2f",v)
67
- y = @graph_height + x_label_font_size + 3
68
- text.attributes["x"] = x.to_s
69
- text.attributes["y"] = y.to_s
70
- end
71
-
72
- def draw_x_labels
73
- if show_x_labels
74
- (0...@histogram.bins).each do |i|
75
- value = @histogram[i]
76
- range = @histogram.get_range(i)
77
- draw_x_label(range[0])
78
- if(i==(@histogram.bins)-1)
79
- draw_x_label(range[1])
80
- end
81
- end
82
- end
83
- end
84
- def draw_data
85
- minvalue = min_value
86
- fieldwidth = field_width
87
- unit_size = (@graph_height.to_f - font_size*2*top_font) /
88
- (get_y_labels.max - get_y_labels.min)
89
- bottom = @graph_height
90
- field_count = 0
91
- hist_min=@histogram.min
92
- hist_max=@histogram.max
93
- range_hist=hist_max-hist_min
94
- total=0
95
-
96
- (0...@histogram.bins).each do |i|
97
- dataset_count = 0
98
- value = @histogram[i]
99
- total=total+value
100
- range = @histogram.get_range(i)
101
- left = (range[0] - hist_min)*unit_width
102
- bar_width = (range[1] - hist_min)*unit_width - left
103
- length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size
104
- # top is 0 if value is negative
105
- top = bottom - (((value < 0 ? 0 : value) - minvalue) * unit_size)
106
-
107
- @graph.add_element( "rect", {
108
- "x" => (@inner_margin+left).to_s,
109
- "y" => top.to_s,
110
- "width" => bar_width.to_s,
111
- "height" => length.to_s,
112
- "class" => "fill#{dataset_count+1}"
113
- })
114
-
115
- make_datapoint_text(left + @inner_margin+ (bar_width/2), top - 6, value.to_s)
116
- field_count += 1
117
- end
118
- if(show_normal)
119
- divs=30
120
- path=""
121
- 0.upto(divs) do |i|
122
- x_abs=hist_min+(range_hist/divs)*i
123
- y=GSL::Ran::gaussian_pdf((x_abs-mean) / sigma)*total
124
- xg=@inner_margin+((x_abs-hist_min)*unit_width)
125
- yg=bottom-(y-minvalue)*unit_size
126
- if i==0
127
- path="M#{xg} #{yg} "
128
- else
129
- path+="L#{xg} #{yg} "
130
- end
131
- end
132
- @graph.add_element("path",
133
- { "d"=>path, "style"=>"stroke:black;fill:none" })
134
- end
135
- end
136
-
137
-
138
- def get_css
139
- return <<EOL
140
- /* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
141
-
142
- .key1,.fill1{
143
- fill: #ff0000;
144
- stroke: black;
145
- stroke-width: 1px;
146
- }
147
- .key2,.fill2{
148
- fill: #0000ff;
149
- stroke: black;
150
- stroke-width: 1px;
151
- }
152
- .key3,.fill3{
153
- fill: #00ff00;
154
- stroke: none;
155
- stroke-width: 1px;
156
- }
157
- .key4,.fill4{
158
- fill: #ffcc00;
159
- stroke: none;
160
- stroke-width: 1px;
161
- }
162
- .key5,.fill5{
163
- fill: #00ccff;
164
- stroke: none;
165
- stroke-width: 1px;
166
- }
167
- .key6,.fill6{
168
- fill: #ff00ff;
169
- stroke: none;
170
- stroke-width: 1px;
171
- }
172
- .key7,.fill7{
173
- fill: #00ffff;
174
- stroke: none;
175
- stroke-width: 1px;
176
- }
177
- .key8,.fill8{
178
- fill: #ffff00;
179
- stroke: none;
180
- stroke-width: 1px;
181
- }
182
- .key9,.fill9{
183
- fill: #cc6666;
184
- stroke: none;
185
- stroke-width: 1px;
186
- }
187
- .key10,.fill10{
188
- fill: #663399;
189
- stroke: none;
190
- stroke-width: 1px;
191
- }
192
- .key11,.fill11{
193
- fill: #339900;
194
- stroke: none;
195
- stroke-width: 1px;
196
- }
197
- .key12,.fill12{
198
- fill: #9966FF;
199
- stroke: none;
200
- stroke-width: 1px;
201
- }
202
- EOL
203
- end
204
- end
205
- end
206
- end