feldtruby 0.3.12 → 0.3.13
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/feldtruby/statistics.rb +61 -37
- data/lib/feldtruby/version.rb +1 -1
- data/test/test_statistics.rb +6 -5
- metadata +1 -2
- data/Rplots.pdf +0 -0
data/lib/feldtruby/statistics.rb
CHANGED
@@ -13,7 +13,11 @@ class RCommunicator
|
|
13
13
|
# Will stay open until closed or the ruby interpreter exits.
|
14
14
|
# echo = false and interactive = false
|
15
15
|
@r = RinRuby.new(false, false)
|
16
|
+
|
17
|
+
@installed_libraries = []
|
18
|
+
|
16
19
|
setup_r()
|
20
|
+
|
17
21
|
end
|
18
22
|
|
19
23
|
# Include necessary libraries.
|
@@ -23,7 +27,13 @@ class RCommunicator
|
|
23
27
|
|
24
28
|
# Include a library after ensuring it has been installed
|
25
29
|
def include_library(lib)
|
30
|
+
|
31
|
+
return if @installed_libraries.include?(lib)
|
32
|
+
|
33
|
+
@installed_libraries << lib
|
34
|
+
|
26
35
|
@r.eval "if(!library(#{lib}, logical.return=TRUE)) {install.packages(\"#{lib}\"); library(#{lib});}"
|
36
|
+
|
27
37
|
end
|
28
38
|
|
29
39
|
# Load R scripts in the feldtruby/R directory.
|
@@ -197,12 +207,6 @@ end
|
|
197
207
|
# Plotting data sets in R with ggplot2 and save them to files.
|
198
208
|
module FeldtRuby::Statistics::Plotting
|
199
209
|
|
200
|
-
GfxFormatToGfxParams = {
|
201
|
-
"pdf" => {:width => 7, :height => 5, :paper => 'special'},
|
202
|
-
"png" => {:units => "cm", :width => 12, :height => 8},
|
203
|
-
"tiff" => {:units => "cm", :width => 12, :height => 8},
|
204
|
-
}
|
205
|
-
|
206
210
|
def gfx_device(format, width = nil, height = nil)
|
207
211
|
|
208
212
|
format = format.to_s # If given as a symbol instead of a string
|
@@ -228,19 +232,6 @@ module FeldtRuby::Statistics::Plotting
|
|
228
232
|
|
229
233
|
end
|
230
234
|
|
231
|
-
def set_file_ending(filepath, newEnding)
|
232
|
-
|
233
|
-
dirname = File.dirname(filepath)
|
234
|
-
|
235
|
-
current_ending = filepath.split(".").last
|
236
|
-
|
237
|
-
# Works even if there is no current file ending:
|
238
|
-
basename = File.basename(filepath, "." + current_ending)
|
239
|
-
|
240
|
-
File.join dirname, basename
|
241
|
-
|
242
|
-
end
|
243
|
-
|
244
235
|
def plot_2dims(csvFilePath, graphFilePath, xName, yName, title = "scatterplot", format = "pdf", width = nil, height = nil)
|
245
236
|
|
246
237
|
include_library("ggplot2")
|
@@ -286,38 +277,71 @@ module FeldtRuby::Statistics::Plotting
|
|
286
277
|
}
|
287
278
|
end
|
288
279
|
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
280
|
+
GfxFormatToGfxParams = {
|
281
|
+
"pdf" => {:width => 7, :height => 5, :paper => 'special'},
|
282
|
+
"png" => {:units => "cm", :width => 12, :height => 8},
|
283
|
+
"tiff" => {:units => "cm", :width => 12, :height => 8},
|
284
|
+
}
|
285
|
+
|
286
|
+
# Wrap a sve_graph call around a block that draws a diagram and this will
|
287
|
+
# save the diagram to a file. The filetype is given by the file ending of
|
288
|
+
# the file name.
|
289
|
+
def save_graph(filename, width = nil, height = nil)
|
290
|
+
|
291
|
+
file_ending = filename.split(".").last
|
292
|
+
|
293
|
+
raise "Don't now about graphics format #{file_ending}" unless GfxFormatToGfxParams.has_key?(file_ending)
|
294
|
+
|
295
|
+
params = GfxFormatToGfxParams[file_ending].clone
|
296
|
+
|
297
|
+
params[:width] = width if width
|
298
|
+
params[:height] = height if height
|
299
|
+
|
300
|
+
RC.eval("#{file_ending}(#{filename.inspect}, #{hash_to_R_params(params)})")
|
301
|
+
|
293
302
|
yield() # Just be sure not to nest these save_graph calls within each other...
|
303
|
+
|
294
304
|
RC.eval("dev.off()")
|
305
|
+
|
295
306
|
end
|
296
307
|
|
297
308
|
# Overlaid density graph of the observations (sampled distributions) in data1
|
298
|
-
# and data2.
|
299
|
-
|
309
|
+
# and data2. The _dataMap_ maps the name of each data series to an array with
|
310
|
+
# its observations.
|
311
|
+
def overlaid_densities(dataMap, title = "Densities of distributions", datasetsName = "distribution", xlabel = "values", ylabel = "density")
|
312
|
+
|
300
313
|
include_library("ggplot2")
|
314
|
+
include_library("reshape2")
|
315
|
+
|
316
|
+
cardinalities = dataMap.values.map {|vs| vs.length}.uniq
|
301
317
|
|
302
|
-
|
318
|
+
unless cardinalities.length == 1
|
319
|
+
|
320
|
+
raise ArgumentError.new("Must have same cardinality")
|
321
|
+
|
322
|
+
end
|
303
323
|
|
304
324
|
script = <<-EOS
|
305
|
-
df <- data.frame(
|
306
|
-
|
307
|
-
|
308
|
-
f <-
|
309
|
-
f <- f +
|
310
|
-
f <- f +
|
311
|
-
|
312
|
-
|
313
|
-
|
325
|
+
df <- data.frame(index = (1:#{cardinalities.first}), #{hash_to_R_params(dataMap)})
|
326
|
+
df.m <- melt(df, id = "index")
|
327
|
+
names(df.m)[2] <- _datasetsName_
|
328
|
+
f <- ggplot(df.m, aes(value, fill=#{datasetsName}))
|
329
|
+
f <- f + geom_density(alpha = 0.2, size = 0.5) + scale_color_brewer()
|
330
|
+
f <- f + ggtitle(_title_) + xlab(_xlabel_) + ylab(_ylabel_)
|
331
|
+
f <- f + theme_bw()
|
332
|
+
f <- f + theme(
|
333
|
+
plot.title = element_text(face="bold", size=12),
|
334
|
+
axis.title.x = element_text(face="bold", size=10),
|
335
|
+
axis.title.y = element_text(face="bold", size=10)
|
336
|
+
)
|
314
337
|
f
|
315
|
-
#dev.off()
|
316
338
|
EOS
|
317
|
-
|
339
|
+
|
340
|
+
subst_eval script, {:title => title, :datasetsName => datasetsName,
|
318
341
|
:xlabel => xlabel, :ylabel => ylabel}
|
319
342
|
|
320
343
|
end
|
344
|
+
|
321
345
|
end
|
322
346
|
|
323
347
|
class FeldtRuby::RCommunicator
|
data/lib/feldtruby/version.rb
CHANGED
data/test/test_statistics.rb
CHANGED
@@ -170,19 +170,20 @@ describe "Plotting" do
|
|
170
170
|
#
|
171
171
|
# end
|
172
172
|
|
173
|
-
it "can do overlaid density plot" do
|
173
|
+
it "can do overlaid density plot of three arrays" do
|
174
174
|
|
175
175
|
d1 = Array.new(100) {rand(10)}
|
176
|
-
d2 = Array.new(100) {2 + rand(
|
176
|
+
d2 = Array.new(100) {2 + rand(6)}
|
177
|
+
g = Array.new(100) {1 + rand(12)}
|
177
178
|
|
178
|
-
out = "
|
179
|
+
out = "tmp2.pdf"
|
179
180
|
|
180
181
|
RC.save_graph(out) do
|
181
|
-
RC.overlaid_densities(d1, d2)
|
182
|
+
RC.overlaid_densities({:ind1 => d1, :ind2 => d2, :goal => g})
|
182
183
|
end
|
183
184
|
|
184
185
|
File.exist?(out).must_equal true
|
185
186
|
File.delete out
|
186
|
-
|
187
|
+
|
187
188
|
end
|
188
189
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: feldtruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.13
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -73,7 +73,6 @@ files:
|
|
73
73
|
- R/diffusion_kde.R
|
74
74
|
- README.md
|
75
75
|
- Rakefile
|
76
|
-
- Rplots.pdf
|
77
76
|
- TODO
|
78
77
|
- feldtruby.gemspec
|
79
78
|
- lib/feldtruby.rb
|
data/Rplots.pdf
DELETED
Binary file
|