scicom 0.4.0-java → 0.4.1-java

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3ffc1bffa5d865e5df2c6843e4b016b34f283209
4
- data.tar.gz: 362146cc4e463467e0d90c1758255c0bc97b9f9a
3
+ metadata.gz: 33c8efc122762f2a26802e9bf733cbb323817cb8
4
+ data.tar.gz: 1b4cf240792f14589d49525be0d5aea96ff9469e
5
5
  SHA512:
6
- metadata.gz: f8d3a2b9b0d447b888b3cef294b21de6f085a5f199f38c7a295234b4eb02c8c32abcbb1ba6d42cbdac65f6b79e630ed7051836b64008f75779296378c3d63874
7
- data.tar.gz: 1b7717cec680c29aa20579f7d4d62a37e3965272bcaadbf99d8c6563e833b873a6141b739283186c286e45127fcceb3c4a6ac98308f1063d7bccf111f7cdacde
6
+ metadata.gz: ea9e5b93ecd6f1e34c0328fa851ba08eb4b0475876161a69ff562b744c9aba990b5f5dd590f6797dd59ea33024203ec28e8d1286a5d1d69e1d5ed2ebed209490
7
+ data.tar.gz: abd5d25b94e18557133d15bec9f840896b6c11410dd1fc82a6700dc557918bcfd61f28aab8efa035e993f03c86df6838324fdf57603325ea0c6afe2147056b81
data/README.md CHANGED
@@ -1,68 +1,3 @@
1
- ## SciCom with Standard R Interface
2
-
3
- SciCom allows R programmers to use R commands inside a Ruby script in
4
- a way similar to RinRuby by calling method eval and passing to it an R
5
- script:
6
-
7
- # Basic integration with R can always be done by calling eval and passing it a valid
8
- # R expression.
9
- > R.eval("r.i = 10L")
10
- > R.eval("print(r.i)")
11
-
12
- [1] 10
13
-
14
- > R.eval("vec = c(10, 20, 30, 40, 50)")
15
- > R.eval("print(vec)")
16
-
17
- [1] 10 20 30 40 50
18
-
19
- > R.eval("print(vec[1])")
20
-
21
- [1] 10
22
-
23
- Programmers can also use here docs to integrate an R script inside a
24
- Ruby script. The next example show a model for predicting baseball
25
- wins based on runs allowed and runs scored. The data comes from
26
- Baseball-Reference.com.
27
-
28
- R.eval <<EOF
29
-
30
- # This dataset comes from Baseball-Reference.com.
31
- baseball = read.csv("baseball.csv")
32
- # str has a bug in Renjin
33
- # str(data)
34
-
35
- # Lets look at the data available for Momeyball.
36
- moneyball = subset(baseball, Year < 2002)
37
-
38
- # Let's see if we can predict the number of wins, by lookin at
39
- # runs allowed (RA) and runs scored (RS). RD is the runs difference.
40
- # We are making a linear model from predicting wins (W) based on RD
41
- moneyball$RD = moneyball$RS - moneyball$RA
42
- WinsReg = lm(W ~ RD, data=moneyball)
43
- print(summary(WinsReg))
44
-
45
- EOF
46
-
47
- The output of the program above is:
48
-
49
- Call:
50
- lm(data = moneyball, formula = W ~ RD)
51
-
52
- Residuals:
53
- Min 1Q Median 3Q Max
54
- -14,266 -2,651 0,123 2,936 11,657
55
-
56
- Coefficients:
57
- Estimate Std. Error t value Pr(>|t|)
58
- (Intercept) 80,881 0,131 616,675 <0 ***
59
- RD 0,106 0,001 81,554 <0 ***
60
- ---
61
- Signif. codes: 0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
62
-
63
- Residual standard error: 3,939 on 900 degrees of freedom
64
- Multiple R-squared: 0,8808, Adjusted R-squared: 0,8807
65
- F-statistic: 6.650,9926 on 1 and 900 DF, p-value: < 0
66
1
 
67
2
  ## The SciCom “language”
68
3
 
@@ -256,3 +191,68 @@ Ruby hashes in the normal Ruby way.
256
191
 
257
192
  [0.0+1.0i]
258
193
 
194
+ ## SciCom with Standard R Interface
195
+
196
+ SciCom allows R programmers to use R commands inside a Ruby script in
197
+ a way similar to RinRuby by calling method eval and passing to it an R
198
+ script:
199
+
200
+ # Basic integration with R can always be done by calling eval and passing it a valid
201
+ # R expression.
202
+ > R.eval("r.i = 10L")
203
+ > R.eval("print(r.i)")
204
+
205
+ [1] 10
206
+
207
+ > R.eval("vec = c(10, 20, 30, 40, 50)")
208
+ > R.eval("print(vec)")
209
+
210
+ [1] 10 20 30 40 50
211
+
212
+ > R.eval("print(vec[1])")
213
+
214
+ [1] 10
215
+
216
+ Programmers can also use here docs to integrate an R script inside a
217
+ Ruby script. The next example show a model for predicting baseball
218
+ wins based on runs allowed and runs scored. The data comes from
219
+ Baseball-Reference.com.
220
+
221
+ R.eval <<EOF
222
+
223
+ # This dataset comes from Baseball-Reference.com.
224
+ baseball = read.csv("baseball.csv")
225
+ # str has a bug in Renjin
226
+ # str(data)
227
+
228
+ # Lets look at the data available for Momeyball.
229
+ moneyball = subset(baseball, Year < 2002)
230
+
231
+ # Let's see if we can predict the number of wins, by lookin at
232
+ # runs allowed (RA) and runs scored (RS). RD is the runs difference.
233
+ # We are making a linear model from predicting wins (W) based on RD
234
+ moneyball$RD = moneyball$RS - moneyball$RA
235
+ WinsReg = lm(W ~ RD, data=moneyball)
236
+ print(summary(WinsReg))
237
+
238
+ EOF
239
+
240
+ The output of the program above is:
241
+
242
+ Call:
243
+ lm(data = moneyball, formula = W ~ RD)
244
+
245
+ Residuals:
246
+ Min 1Q Median 3Q Max
247
+ -14,266 -2,651 0,123 2,936 11,657
248
+
249
+ Coefficients:
250
+ Estimate Std. Error t value Pr(>|t|)
251
+ (Intercept) 80,881 0,131 616,675 <0 ***
252
+ RD 0,106 0,001 81,554 <0 ***
253
+ ---
254
+ Signif. codes: 0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
255
+
256
+ Residual standard error: 3,939 on 900 degrees of freedom
257
+ Multiple R-squared: 0,8808, Adjusted R-squared: 0,8807
258
+ F-statistic: 6.650,9926 on 1 and 900 DF, p-value: < 0
@@ -38,6 +38,7 @@ class Renjin
38
38
  include_package "java.lang"
39
39
 
40
40
  attr_reader :sexp
41
+ attr_reader :refresh
41
42
  attr_reader :rvar
42
43
  attr_reader :attr
43
44
  attr_accessor :scope
@@ -75,8 +76,15 @@ class Renjin
75
76
  @rvar = "sc_#{SecureRandom.hex(8)}"
76
77
 
77
78
  # if this object already has a sexp value then assign to @rvar the existing sexp,
78
- # otherwise, assign itself to @rvar.
79
- (@sexp == nil)? R.assign(@rvar, self) : R.assign(@rvar, @sexp)
79
+ # otherwise, assign itself to @rvar. If a sexp already exists then set the
80
+ # refresh flag to true, so that we know that the sexp was changed.
81
+ # (@sexp == nil)? R.assign(@rvar, self) : R.assign(@rvar, @sexp)
82
+ if (@sexp.nil?)
83
+ R.assign(@rvar, self)
84
+ else
85
+ @refresh = true
86
+ R.assign(@rvar, @sexp)
87
+ end
80
88
 
81
89
  # Whenever a variable is injected in Renjin, it is also added to the Renjin stack.
82
90
  # After eval, every injected variable is removed from Renjin making sure that we
@@ -160,6 +168,7 @@ class Renjin
160
168
  @sexp = sexp
161
169
  @rvar = nil
162
170
  @attr = Attributes.new(self)
171
+ @refresh = false
163
172
 
164
173
  end
165
174
 
@@ -260,7 +260,9 @@ class Renjin
260
260
  end
261
261
 
262
262
  #----------------------------------------------------------------------------------------
263
- #
263
+ # Every time eval is called, a new Renjin::RubySexp is build. If we don't want to wrap
264
+ # the returned value of an evaluation in a RubySexp, then method direct_eval should be
265
+ # called.
264
266
  #----------------------------------------------------------------------------------------
265
267
 
266
268
  def eval(expression)
@@ -334,7 +336,9 @@ class Renjin
334
336
  params << "(#{arg.begin}:#{final_value})"
335
337
  elsif (arg.is_a? Hash)
336
338
  arg.each_pair do |key, value|
337
- params << "#{key.to_s} = #{parse(value)}"
339
+ # k = key.to_s.gsub(/__/,".")
340
+ params << "#{key.to_s.gsub(/__/,'.')} = #{parse(value)}"
341
+ # params << "#{k} = #{parse(value)}"
338
342
  end
339
343
  elsif ((arg.is_a? Renjin::RubySexp) || (arg.is_a? Array) || (arg.is_a? MDArray))
340
344
  params << arg.r
@@ -575,15 +579,15 @@ class Renjin
575
579
  # index.stride)
576
580
 
577
581
  case mdarray.type
578
- when "int"
582
+ when "int", :int
579
583
  vector = Java::RbScicom::MDIntVector.factory(mdarray.nc_array, attributes)
580
- when "double"
584
+ when "double", :double
581
585
  vector = Java::RbScicom::MDDoubleVector.factory(mdarray.nc_array, attributes)
582
- when "byte"
586
+ when "byte", :byte
583
587
  vector = Java::RbScicom::MDLogicalVector.factory(mdarray.nc_array, attributes)
584
- when "string"
588
+ when "string", :string
585
589
  vector = Java::RbScicom::MDStringVector.factory(mdarray.nc_array, attributes)
586
- when "boolean"
590
+ when "boolean", :boolean
587
591
  raise "Boolean vectors cannot be converted to R vectors. If you are trying to \
588
592
  convert to an R Logical object, use a :byte MDArray"
589
593
  else
@@ -132,47 +132,19 @@ class Renjin
132
132
  def eq(other_val)
133
133
  (other_val == nil)? false : R.eval("identical(#{r},#{other_val.r})")
134
134
  end
135
- =begin
136
- #----------------------------------------------------------------------------------------
137
- #
138
- #----------------------------------------------------------------------------------------
139
-
140
- def as__integer
141
- R.as__integer(self)
142
- end
143
-
144
- #----------------------------------------------------------------------------------------
145
- #
146
- #----------------------------------------------------------------------------------------
147
-
148
- def as__double
149
- R.as__double(self)
150
- end
151
135
 
152
136
  #----------------------------------------------------------------------------------------
153
- #
154
- #----------------------------------------------------------------------------------------
155
-
156
- def as__complex
157
- R.as__complex(self)
158
- end
159
-
160
- #----------------------------------------------------------------------------------------
161
- #
162
- #----------------------------------------------------------------------------------------
163
-
164
- def as__character
165
- R.as__character(self)
166
- end
167
- =end
168
- #----------------------------------------------------------------------------------------
169
- #
137
+ # Returns a sexp as an mdarray.
170
138
  #----------------------------------------------------------------------------------------
171
139
 
172
140
  def as__mdarray
173
141
 
174
- if (@mdarray)
175
- elsif (@sexp.java_kind_of? Java::RbScicom::MDDoubleVector)
142
+ if (@mdarray && !@refresh)
143
+ return @mdarray
144
+ end
145
+
146
+ @refresh = false
147
+ if (@sexp.java_kind_of? Java::RbScicom::MDDoubleVector)
176
148
  @mdarray = MDArray.build_from_nc_array(:double, @sexp.array)
177
149
  elsif (@sexp.java_kind_of? Java::OrgRenjinSexp::DoubleArrayVector)
178
150
  @mdarray = MDArray.from_jstorage("double", [@sexp.length()], @sexp.toDoubleArrayUnsafe())
@@ -56,7 +56,7 @@ MDArray to R"
56
56
 
57
57
  # change an element of the MDArray
58
58
  arr1[0, 0] = 10.34567
59
- p "changing element is arr1 will cause the same change in r_matrix"
59
+ p "changing element in arr1 will cause the same change in r_matrix"
60
60
  arr1.print
61
61
 
62
62
  p "element [0, 0] of r_matrix has also changed"
@@ -36,8 +36,10 @@ require_relative 'test_attributes'
36
36
  require_relative 'test_factor'
37
37
  require_relative 'test_array'
38
38
  require_relative 'test_matrix'
39
+ require_relative 'test_mdarray'
39
40
  require_relative 'test_dataframe'
40
41
  require_relative 'test_linear_model'
42
+ require_relative 'test_callback'
41
43
 
42
44
  require_relative 'test_assign_mdarray'
43
45
  require_relative 'test_assign_mdarray_2d'
@@ -27,7 +27,7 @@ require '../config' if @platform == nil
27
27
  require 'scicom'
28
28
 
29
29
  class SciComTest < Test::Unit::TestCase
30
-
30
+
31
31
  context "R environment" do
32
32
 
33
33
  #--------------------------------------------------------------------------------------
@@ -41,7 +41,7 @@ class SciComTest < Test::Unit::TestCase
41
41
  #--------------------------------------------------------------------------------------
42
42
 
43
43
  setup do
44
-
44
+
45
45
  end
46
46
 
47
47
  #--------------------------------------------------------------------------------------
@@ -54,7 +54,7 @@ class SciComTest < Test::Unit::TestCase
54
54
  # fourth column, but it is useful to label the rows and columns to make the rows
55
55
  # (subjects) and columns (variables) distinction more obvious.
56
56
  xij = R.matrix(R.seq(1..40), ncol: 4)
57
-
57
+
58
58
  # method fassign is used whenever in R there would be a function assignment such as,
59
59
  # for example: rownames(x) <- c("v1", "v2", "v3")
60
60
  # R.fassign(xij, :rownames, R.paste("S", R.seq(1, xij.attr.dim[1]), sep: ""))
@@ -64,20 +64,20 @@ class SciComTest < Test::Unit::TestCase
64
64
  xij.fassign(:rownames, R.paste("S", R.seq(1, xij.attr.dim[1]), sep: ""))
65
65
  xij.fassign(:colnames, R.paste("V", R.seq(1, xij.attr.dim[2]), sep: ""))
66
66
  xij.pp
67
-
67
+
68
68
  # if an index can be passed to the R function, then fassign needs to be a bit more
69
69
  # complex. For instance, callling dimnames(x)[[1]] <- "name" is done by the following
70
70
  # call: x.fassign({f: :dimnames, index:[[1]]}, "name"
71
71
  # Changing rownames by using function :dimnames and index [[1]]
72
72
  xij.fassign({f: :dimnames, index: [[1]]},
73
- R.paste("DS", R.seq(1, xij.attr.dim[1]), sep: ""))
73
+ R.paste("DS", R.seq(1, xij.attr.dim[1]), sep: ""))
74
74
 
75
75
  # Changing colnames by using function :dimnames and index [[2]]
76
76
  xij.fassign({f: :dimnames, index: [[2]]},
77
- R.paste("DV", R.seq(1, xij.attr.dim[2]), sep: ""))
78
-
77
+ R.paste("DV", R.seq(1, xij.attr.dim[2]), sep: ""))
78
+
79
79
  xij.pp
80
-
80
+
81
81
  # Just as the transpose of a vector makes a column vector into a row vector, so
82
82
  # does the transpose of a matrix swap the rows for the columns. Note that now the
83
83
  # subjects are columns and the variables are the rows.
@@ -23,7 +23,7 @@ require 'rubygems'
23
23
  require "test/unit"
24
24
  require 'shoulda'
25
25
 
26
- require 'env'
26
+ require '../config' if @platform == nil
27
27
  require 'scicom'
28
28
 
29
29
 
@@ -39,46 +39,34 @@ class SciComTest < Test::Unit::TestCase
39
39
 
40
40
  end
41
41
 
42
-
43
42
  #--------------------------------------------------------------------------------------
44
43
  #
45
44
  #--------------------------------------------------------------------------------------
46
45
 
47
46
  should "convert a vector to an MDArray" do
48
47
 
48
+ r_vector = R.c(1, 2, 3, 4)
49
+
49
50
  # Method 'get' converts a Vector to an MDArray
50
- vec = i3.get
51
+ vec = r_vector.get
51
52
  assert_equal(true, vec.is_a?(MDArray))
52
- assert_equal("int", vec.type)
53
+ assert_equal("double", vec.type)
53
54
 
54
55
  # For consistancy with R notation one can also call as__mdarray to convert a
55
56
  # vector to an MDArray
56
- vec2 = i3.as__mdarray
57
+ vec2 = r_vector.as__mdarray
57
58
  assert_equal(true, vec2.is_a?(MDArray))
58
- assert_equal("int", vec2.type)
59
+ assert_equal("double", vec2.type)
59
60
 
60
61
  # Now 'vec' is an MDArray and its elements can be accessed through indexing, but
61
62
  # this time the first index is 0, and the element is an actual number
62
- assert_equal(10, vec[0])
63
-
64
- # Convert vector to an MDArray
65
- array = vec2.get
66
-
67
- # Use array as any other MDArray...
68
- array.each do |elmt|
69
- p elmt
70
- end
71
-
72
- # ... although there is no need to convert a vector to an MDArray to call each:
73
- # the each method is also defined for vectors
74
- vec1.each do |elmt|
75
- p elmt
76
- end
63
+ assert_equal(1, vec[0])
64
+ assert_equal(2, vec[1])
65
+ assert_equal(3, vec[2])
66
+ assert_equal(4, vec[3])
77
67
 
78
68
  end
79
69
 
80
- =begin
81
-
82
70
  #--------------------------------------------------------------------------------------
83
71
  #
84
72
  #--------------------------------------------------------------------------------------
@@ -88,43 +76,53 @@ class SciComTest < Test::Unit::TestCase
88
76
  # typed_arange does the same as arange but for arrays of other type
89
77
  arr = MDArray.typed_arange(:double, 60)
90
78
  # MDArray is stored in row-major order
91
- arr.reshape!([5, 3, 4])
79
+ arr.reshape!([5, 12])
92
80
  # arr.print
93
81
 
94
- R.eval <<EOF
95
- print(#{arr.r});
96
- vec = #{arr.r};
97
- print(vec);
98
- print(vec[1, 1, 1]);
99
-
100
- EOF
82
+ R.eval <<-EOF
83
+ vec = #{arr.r};
84
+ print(vec);
85
+ print(vec[1, 1]);
86
+ EOF
101
87
 
102
- end
103
- =end
88
+ end
104
89
 
105
90
  #--------------------------------------------------------------------------------------
106
91
  #
107
92
  #--------------------------------------------------------------------------------------
108
93
 
109
94
  should "receive 1 Dimensional MDArrays in R" do
110
-
95
+
96
+ # vec is an MDArray
111
97
  vec = MDArray.double([6], [1, 3, 5, 7, 11, 13])
112
- # vec is just a normal MDArray that can be changed at anytime
113
- vec[0] = 2
114
98
 
99
+ # prime is a R vector
115
100
  R.prime = vec
101
+ R.prime.pp
116
102
 
117
- # now vec is immutable, since it is now in Renjin and Renjin requires an immutable
118
- # vector
119
- assert_raise ( RuntimeError ) { vec[0] = 1 }
103
+ # change the value of vec, the same change should happens in prime... they have the
104
+ # same backing store
105
+ vec[0] = 17
106
+ R.prime.pp
120
107
 
121
- vec2 = R.eval("print(prime)")
122
- vec2.print
108
+ # now r_vec is an R vector
109
+ r_vec = R.c(1, 3, 5, 7, 11, 13)
110
+ r_vec.pp
111
+
112
+ # and prime is an MDArray
113
+ prime = r_vec.get
114
+ prime.print
123
115
 
124
- assert_raise ( RuntimeError ) { vec2[1] = 7 }
116
+ # change the value of prime
117
+ prime[0] = 17
118
+ prime.print
119
+
120
+ r_vec.pp
125
121
 
126
122
  end
127
123
 
124
+ =begin
125
+
128
126
  #--------------------------------------------------------------------------------------
129
127
  #
130
128
  #--------------------------------------------------------------------------------------
@@ -252,7 +250,7 @@ end
252
250
  R.eval("print(#{MDArray.typed_arange(:double, 5).r})")
253
251
 
254
252
  end
255
-
253
+ =end
256
254
  end
257
-
255
+
258
256
  end
data/version.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  $gem_name = "scicom"
2
- $version="0.4.0"
2
+ $version="0.4.1"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scicom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: java
6
6
  authors:
7
7
  - Rodrigo Botafogo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-03 00:00:00.000000000 Z
11
+ date: 2016-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement