scicom 0.2.0-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 +7 -0
- data/LICENSE.txt +674 -0
- data/README.md +66 -0
- data/README.md~ +290 -0
- data/Rakefile +51 -0
- data/config.rb +163 -0
- data/doc/PypeR.pdf +0 -0
- data/doc/Stat 133 Class Notes (Phil Spector).pdf +29905 -45
- data/doc/The R interface.docx +0 -0
- data/lib/JRubyR/as_mdarray.rb +60 -0
- data/lib/JRubyR/attributes.rb +74 -0
- data/lib/JRubyR/dataframe.rb +35 -0
- data/lib/JRubyR/environment.rb +60 -0
- data/lib/JRubyR/function.rb +61 -0
- data/lib/JRubyR/index.rb +278 -0
- data/lib/JRubyR/list.rb +56 -0
- data/lib/JRubyR/list_orig.rb +111 -0
- data/lib/JRubyR/logical_value.rb +56 -0
- data/lib/JRubyR/rbsexp.rb +386 -0
- data/lib/JRubyR/renjin.rb +431 -0
- data/lib/JRubyR/ruby_classes.rb +58 -0
- data/lib/JRubyR/sequence.rb +56 -0
- data/lib/JRubyR/vector.rb +493 -0
- data/lib/env.rb +12 -0
- data/lib/rinruby.rb +795 -0
- data/lib/scicom.rb +29 -0
- data/target/helper.jar +0 -0
- data/test/baseball.csv +1 -0
- data/test/env.rb +7 -0
- data/test/test_R_interface.rb +165 -0
- data/test/test_array.rb +191 -0
- data/test/test_attributes.rb +261 -0
- data/test/test_basic.rb +156 -0
- data/test/test_column-major.rb +114 -0
- data/test/test_complete.rb +49 -0
- data/test/test_creation.rb +299 -0
- data/test/test_dataframe.rb +248 -0
- data/test/test_distribution.rb +320 -0
- data/test/test_double_assign.rb +240 -0
- data/test/test_double_receive.rb +106 -0
- data/test/test_environment.rb +57 -0
- data/test/test_factor.rb +285 -0
- data/test/test_functions.rb +67 -0
- data/test/test_linear_model.rb +64 -0
- data/test/test_list.rb +220 -0
- data/test/test_matrix.rb +205 -0
- data/test/test_mdarray.rb +258 -0
- data/test/test_operators.rb +227 -0
- data/test/test_sequence.rb +63 -0
- data/test/test_subsetting.rb +67 -0
- data/test/test_tmp.rb +67 -0
- data/test/test_vector.rb +227 -0
- data/vendor/Renjin.pdf +0 -0
- data/vendor/renjin-script-engine-0.7.0-RC7-SNAPSHOT-jar-with-dependencies.jar +0 -0
- data/version.rb +2 -0
- metadata +196 -0
data/lib/scicom.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# @author Rodrigo Botafogo
|
5
|
+
#
|
6
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
7
|
+
# and distribute this software and its documentation, without fee and without a signed
|
8
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
9
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
10
|
+
# distributions.
|
11
|
+
#
|
12
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
13
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
14
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
15
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
16
|
+
#
|
17
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
18
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
19
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
20
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
21
|
+
# OR MODIFICATIONS.
|
22
|
+
##########################################################################################
|
23
|
+
|
24
|
+
require 'mdarray'
|
25
|
+
|
26
|
+
require_relative 'env'
|
27
|
+
require_relative 'JRubyR/renjin'
|
28
|
+
|
29
|
+
|
data/target/helper.jar
ADDED
Binary file
|
data/test/baseball.csv
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Team,League,Year,RS,RA,W,OBP,SLG,BA,Playoffs,RankSeason,RankPlayoffs,G,OOBP,OSLG
|
data/test/env.rb
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation, without fee and without a signed
|
6
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
7
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
8
|
+
# distributions.
|
9
|
+
#
|
10
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
11
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
12
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
13
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
#
|
15
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
16
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
17
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
18
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
19
|
+
# OR MODIFICATIONS.
|
20
|
+
##########################################################################################
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require "test/unit"
|
24
|
+
require 'shoulda'
|
25
|
+
|
26
|
+
require 'env'
|
27
|
+
require 'scicom'
|
28
|
+
|
29
|
+
class SciComTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
context "R environment" do
|
32
|
+
|
33
|
+
#--------------------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#--------------------------------------------------------------------------------------
|
36
|
+
|
37
|
+
setup do
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
#--------------------------------------------------------------------------------------
|
42
|
+
# Creating a variable in R and assign a value to it. In this case assign the NULL
|
43
|
+
# value. There are two ways of assign variables in R, through method assign or with
|
44
|
+
# the '=' method. To retrieve an R variable just acess it in the R namespace.
|
45
|
+
#--------------------------------------------------------------------------------------
|
46
|
+
|
47
|
+
should "use R.eval to evaluate an R expression" do
|
48
|
+
|
49
|
+
# The NULL value
|
50
|
+
|
51
|
+
# variable null is NULL. Variable 'null' exists in the R namespace and can be
|
52
|
+
# access normally in a call to 'eval'
|
53
|
+
R.eval("null = NULL")
|
54
|
+
R.eval("print(null)")
|
55
|
+
|
56
|
+
# Basic integration with R can always be done by calling eval and passing it a valid
|
57
|
+
# R expression.
|
58
|
+
R.eval("r.i = 10L")
|
59
|
+
R.eval("print(r.i)")
|
60
|
+
|
61
|
+
R.eval("vec = c(10, 20, 30, 40, 50)")
|
62
|
+
R.eval("print(vec)")
|
63
|
+
R.eval("print(vec[1])")
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
#--------------------------------------------------------------------------------------
|
68
|
+
#
|
69
|
+
#--------------------------------------------------------------------------------------
|
70
|
+
|
71
|
+
should "use assign and pull to set and get data from R" do
|
72
|
+
|
73
|
+
# Using method assign, to assign NULL to variable 'null' in R namespace.
|
74
|
+
R.assign("null", nil)
|
75
|
+
assert_equal(nil, R.null)
|
76
|
+
|
77
|
+
# Variable 'res' is available only in the Ruby namespace and not in the R namespace.
|
78
|
+
# a NULL object in R is converted to nil in Ruby.
|
79
|
+
res = R.pull("null")
|
80
|
+
assert_equal(nil, res)
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
#--------------------------------------------------------------------------------------
|
85
|
+
#
|
86
|
+
#--------------------------------------------------------------------------------------
|
87
|
+
|
88
|
+
should "assing value to R variables as Ruby attributes" do
|
89
|
+
|
90
|
+
# Assign a value to an R variable, 'n2'.
|
91
|
+
R.n2 = nil
|
92
|
+
assert_equal(nil, R.n2)
|
93
|
+
|
94
|
+
# One can access variables created in R namespace by using R.<var>. Variable in
|
95
|
+
# R that have a '.' such as 'r.i3' need to have the '.' substituted by '__'
|
96
|
+
R.eval("r.i3 = 10.235")
|
97
|
+
R.r__i3.pp
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
#--------------------------------------------------------------------------------------
|
102
|
+
#
|
103
|
+
#--------------------------------------------------------------------------------------
|
104
|
+
|
105
|
+
should "accept here docs " do
|
106
|
+
|
107
|
+
R.eval <<EOF
|
108
|
+
r.i2 = 10L
|
109
|
+
print(r.i2)
|
110
|
+
EOF
|
111
|
+
|
112
|
+
# Variables created in Ruby can be accessed in an eval clause:
|
113
|
+
val = "10L"
|
114
|
+
R.eval <<EOF
|
115
|
+
r.i3 = #{val}
|
116
|
+
print(r.i3)
|
117
|
+
EOF
|
118
|
+
|
119
|
+
R.eval <<EOF
|
120
|
+
|
121
|
+
# This dataset comes from Baseball-Reference.com.
|
122
|
+
baseball = read.csv("baseball.csv")
|
123
|
+
# str is bogus on Renjin
|
124
|
+
# str(data)
|
125
|
+
|
126
|
+
# prints the index of maximum and minimum years for the dataset
|
127
|
+
print(which.max(baseball$Year))
|
128
|
+
print(which.min(baseball$Year))
|
129
|
+
|
130
|
+
# Lets look at the data available for Momeyball.
|
131
|
+
moneyball = subset(baseball, Year < 2002)
|
132
|
+
|
133
|
+
# Let's see if we can predict the number of wins, by lookin at
|
134
|
+
# runs allowed (RA) and runs scored (RS). RD is the runs difference.
|
135
|
+
# We are making a linear model from predicting wins (W) based on RD
|
136
|
+
moneyball$RD = moneyball$RS - moneyball$RA
|
137
|
+
WinsReg = lm(W ~ RD, data=moneyball)
|
138
|
+
print(summary(WinsReg))
|
139
|
+
EOF
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
# Result of calling print(summary(WinsRed)):
|
148
|
+
#
|
149
|
+
# Call:
|
150
|
+
# lm(data = moneyball, formula = W ~ RD)
|
151
|
+
#
|
152
|
+
# Residuals:
|
153
|
+
# Min 1Q Median 3Q Max
|
154
|
+
# -14,266 -2,651 0,123 2,936 11,657
|
155
|
+
#
|
156
|
+
# Coefficients:
|
157
|
+
# Estimate Std. Error t value Pr(>|t|)
|
158
|
+
# (Intercept) 80,881 0,131 616,675 <0 ***
|
159
|
+
# RD 0,106 0,001 81,554 <0 ***
|
160
|
+
# ---
|
161
|
+
# Signif. codes: 0 '***' 0,001 '**' 0,01 '*' 0,05 '.' 0,1 ' ' 1
|
162
|
+
#
|
163
|
+
# Residual standard error: 3,939 on 900 degrees of freedom
|
164
|
+
# Multiple R-squared: 0,8808, Adjusted R-squared: 0,8807
|
165
|
+
# F-statistic: 6.650,9926 on 1 and 900 DF, p-value: < 0
|
data/test/test_array.rb
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation, without fee and without a signed
|
6
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
7
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
8
|
+
# distributions.
|
9
|
+
#
|
10
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
11
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
12
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
13
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
#
|
15
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
16
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
17
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
18
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
19
|
+
# OR MODIFICATIONS.
|
20
|
+
##########################################################################################
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require "test/unit"
|
24
|
+
require 'shoulda'
|
25
|
+
|
26
|
+
require 'env'
|
27
|
+
require 'scicom'
|
28
|
+
|
29
|
+
class SciComTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
context "R environment" do
|
32
|
+
|
33
|
+
#--------------------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#--------------------------------------------------------------------------------------
|
36
|
+
|
37
|
+
setup do
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
#--------------------------------------------------------------------------------------
|
42
|
+
#
|
43
|
+
#--------------------------------------------------------------------------------------
|
44
|
+
|
45
|
+
should "create array with the creator function" do
|
46
|
+
|
47
|
+
# You have two different options for constructing matrices or arrays. Either you use
|
48
|
+
# the creator functions matrix() and array(), or you simply change the dimensions
|
49
|
+
# using the dim() function.
|
50
|
+
|
51
|
+
# Use the creator functions in R
|
52
|
+
# You can create an array easily with the array() function, where you give the data
|
53
|
+
# as the first argument and a vector with the sizes of the dimensions as the second
|
54
|
+
# argument. The number of dimension sizes in that argument gives you the number of
|
55
|
+
# dimensions. For example, you make an array with four columns, three rows, and
|
56
|
+
# two “tables” like this:
|
57
|
+
my_array = R.array((1..24), dim: R.c(3,4,2))
|
58
|
+
# Renjin has a bug and does not print correctly multi-dimensional array, printing
|
59
|
+
# them as one-dimensional vector.
|
60
|
+
# Correct output should be something like:
|
61
|
+
# , , 1
|
62
|
+
# [,1] [,2] [,3] [,4]
|
63
|
+
# [1,] 1 4 7 10
|
64
|
+
# [2,] 2 5 8 11
|
65
|
+
# [3,] 3 6 9 12
|
66
|
+
# , , 2
|
67
|
+
# [,1] [,2] [,3] [,4]
|
68
|
+
# [1,] 13 16 19 22
|
69
|
+
# [2,] 14 17 20 23
|
70
|
+
# [3,] 15 18 21 24
|
71
|
+
my_array.pp
|
72
|
+
my_array.dim.pp
|
73
|
+
|
74
|
+
# This array has three dimensions. Notice that, although the rows are given as the
|
75
|
+
# first dimension, the tables are filled column-wise. So, for arrays, R fills the
|
76
|
+
# columns, then the rows, and then the rest.
|
77
|
+
|
78
|
+
# Change the dimensions of a vector in R
|
79
|
+
# Alternatively, you could just add the dimensions using the dim() function. This is
|
80
|
+
# a little hack that goes a bit faster than using the array() function; it’s especially
|
81
|
+
# useful if you have your data already in a vector. (This little trick also works for
|
82
|
+
# creating matrices, by the way, because a matrix is nothing more than an array with
|
83
|
+
# only two dimensions.)
|
84
|
+
|
85
|
+
# Say you already have a vector with the numbers 1 through 24, like this:
|
86
|
+
my_vector = R.c((1..24))
|
87
|
+
my_vector.pp
|
88
|
+
|
89
|
+
# You can easily convert that vector to an array exactly like my_array simply by
|
90
|
+
# assigning the attribute dimensions, like this:
|
91
|
+
my_vector.attr.dim = R.c(3, 4, 2)
|
92
|
+
my_vector.attr.dim.pp
|
93
|
+
|
94
|
+
# You can check whether two objects are identical by using the identical function. To
|
95
|
+
# check, for example, whether my_vector and my_array are identical, you simply do the
|
96
|
+
# following:
|
97
|
+
my_array.identical(my_vector).pp
|
98
|
+
|
99
|
+
end
|
100
|
+
|
101
|
+
#--------------------------------------------------------------------------------------
|
102
|
+
#
|
103
|
+
#--------------------------------------------------------------------------------------
|
104
|
+
|
105
|
+
should "access array elements with indexing" do
|
106
|
+
|
107
|
+
my_array = R.array((1..24), dim: R.c(3,4,2))
|
108
|
+
|
109
|
+
# getting a subarray of the original array: is a 4 * 2 array with dimension vector
|
110
|
+
# c(4,2) and data vector containing the values
|
111
|
+
sub = my_array[2, nil, nil]
|
112
|
+
sub.pp
|
113
|
+
sub.attr.dim.pp
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
#--------------------------------------------------------------------------------------
|
118
|
+
#
|
119
|
+
#--------------------------------------------------------------------------------------
|
120
|
+
|
121
|
+
should "allow usage of index arrays" do
|
122
|
+
|
123
|
+
# As well as an index vector in any subscript position, a matrix may be used with a
|
124
|
+
# single index matrix in order either to assign a vector of quantities to an irregular
|
125
|
+
# collection of elements in the array, or to extract an irregular collection as a vector.
|
126
|
+
|
127
|
+
# A matrix example makes the process clear. In the case of a doubly indexed array, an
|
128
|
+
# index matrix may be given consisting of two columns and as many rows as desired. The
|
129
|
+
# entries in the index matrix are the row and column indices for the doubly indexed
|
130
|
+
# array. Suppose for example we have a 4 by 5 array X and we wish to do the following:
|
131
|
+
|
132
|
+
# Extract elements X[1,3], X[2,2] and X[3,1] as a vector structure, and
|
133
|
+
# Replace these entries in the array X by zeroes.
|
134
|
+
# In this case we need a 3 by 2 subscript array, as in the following example.
|
135
|
+
x = R.array((1..20), dim: R.c(4,5)) # Generate a 4 by 5 array.
|
136
|
+
x.pp
|
137
|
+
|
138
|
+
# Now create an index array: i is a 3 by 2 index array.
|
139
|
+
i = R.array(R.c((1..3), (3..1)), dim: R.c(3,2))
|
140
|
+
i.pp
|
141
|
+
|
142
|
+
# Extract all elements in the index array
|
143
|
+
x[i].pp
|
144
|
+
|
145
|
+
# Now replace those elements by zeros
|
146
|
+
x[i] = 0
|
147
|
+
x.pp
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
#--------------------------------------------------------------------------------------
|
152
|
+
#
|
153
|
+
#--------------------------------------------------------------------------------------
|
154
|
+
|
155
|
+
should "apply outer function to arrays" do
|
156
|
+
|
157
|
+
# outer() function applies a function to two arrays.
|
158
|
+
# outer(x, y, FUN="*", ...)
|
159
|
+
# x %o% y
|
160
|
+
|
161
|
+
# x,y: arrays
|
162
|
+
# FUN: function to use on the outer products, default is multiply
|
163
|
+
|
164
|
+
x = R.c(1, 2.3, 2, 3, 4, 8, 12, 43)
|
165
|
+
y = R.c(2, 4)
|
166
|
+
|
167
|
+
# Calculate logarithm value of array x elements using array y as bases:
|
168
|
+
R.outer(x, y, "log").pp
|
169
|
+
|
170
|
+
# calling outer on the vector directly
|
171
|
+
x.outer(y, "log").pp
|
172
|
+
|
173
|
+
# Add array x elements with array y elements:
|
174
|
+
x.outer(y, "+").pp
|
175
|
+
|
176
|
+
# Multiply array x elements with array y elements:
|
177
|
+
mult = x._ :o, y #equal to outer(x,y,"*")
|
178
|
+
mult.pp
|
179
|
+
|
180
|
+
# same as above
|
181
|
+
x.outer(y, "*").pp
|
182
|
+
|
183
|
+
# Concatenate characters to the array elements:
|
184
|
+
z = R.c("a","b")
|
185
|
+
x.outer(z, "paste").pp
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
##########################################################################################
|
4
|
+
# Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
|
5
|
+
# and distribute this software and its documentation, without fee and without a signed
|
6
|
+
# licensing agreement, is hereby granted, provided that the above copyright notice, this
|
7
|
+
# paragraph and the following two paragraphs appear in all copies, modifications, and
|
8
|
+
# distributions.
|
9
|
+
#
|
10
|
+
# IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
|
11
|
+
# INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
|
12
|
+
# THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
|
13
|
+
# POSSIBILITY OF SUCH DAMAGE.
|
14
|
+
#
|
15
|
+
# RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
16
|
+
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
|
17
|
+
# SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
|
18
|
+
# RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
|
19
|
+
# OR MODIFICATIONS.
|
20
|
+
##########################################################################################
|
21
|
+
|
22
|
+
require 'rubygems'
|
23
|
+
require "test/unit"
|
24
|
+
require 'shoulda'
|
25
|
+
|
26
|
+
require 'env'
|
27
|
+
require 'scicom'
|
28
|
+
|
29
|
+
class SciComTest < Test::Unit::TestCase
|
30
|
+
|
31
|
+
context "R environment" do
|
32
|
+
|
33
|
+
#--------------------------------------------------------------------------------------
|
34
|
+
#
|
35
|
+
#--------------------------------------------------------------------------------------
|
36
|
+
|
37
|
+
setup do
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
#--------------------------------------------------------------------------------------
|
42
|
+
# Creating a variable in R and assign a value to it. In this case assign the NULL
|
43
|
+
# value. There are two ways of assign variables in R, through method assign or with
|
44
|
+
# the '=' method. To retrieve an R variable just acess it in the R namespace.
|
45
|
+
#--------------------------------------------------------------------------------------
|
46
|
+
|
47
|
+
should "assign attributes to objects" do
|
48
|
+
|
49
|
+
vec = R.c(1, 2, 3, 4)
|
50
|
+
vec.attr.name = "my.attribute"
|
51
|
+
vec.attr.truth = true
|
52
|
+
vec.attr.column_names = R.c("one", "two", "three", "four")
|
53
|
+
vec.attr.values = R.c((1..3))
|
54
|
+
vec.pp
|
55
|
+
|
56
|
+
# retrieve attr
|
57
|
+
assert_equal("my.attribute", vec.attr.name.gz)
|
58
|
+
assert_equal(true, vec.attr.truth.gt)
|
59
|
+
assert_equal("one", vec.attr.column_names.get(0))
|
60
|
+
assert_equal("three", vec.attr.column_names.get(2))
|
61
|
+
assert_equal(1, vec.attr.values.gz)
|
62
|
+
|
63
|
+
vec.attr.column_names[(1..2)] = R.c("1", "2")
|
64
|
+
assert_equal(true, (R.c("1", "2", "three", "four").eq vec.attr.column_names).gt)
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
#--------------------------------------------------------------------------------------
|
69
|
+
#
|
70
|
+
#--------------------------------------------------------------------------------------
|
71
|
+
|
72
|
+
should "assign special attributes to objects" do
|
73
|
+
|
74
|
+
# simple matrix with dimnames
|
75
|
+
mat = R.cbind(a: (1..3), pi: Math::PI)
|
76
|
+
|
77
|
+
# retrieve dimnames. dimnames is a list.
|
78
|
+
lst = mat.attr.dimnames
|
79
|
+
# dimnames is a list. In R, dimnames is a list with two elements. The first is NULL
|
80
|
+
# (always?) and the second is an array with the actual names
|
81
|
+
assert_equal(nil, lst[[1]])
|
82
|
+
assert_equal("a", lst[[2]][1].gz)
|
83
|
+
assert_equal("pi", lst[[2]][2].gz)
|
84
|
+
|
85
|
+
# mat does also have attribute dim
|
86
|
+
assert_equal(3, mat.attr.dim.get(0))
|
87
|
+
assert_equal(2, mat.attr.dim.get(1))
|
88
|
+
|
89
|
+
# We cannot set 'class' attribute in an R vector by calling .class as this is a
|
90
|
+
# Ruby method. We substiture the .class method by .rclass. However, in the R
|
91
|
+
# namespace the attribute set is actually 'class'.
|
92
|
+
mat.attr.rclass = "myClass"
|
93
|
+
assert_equal("myClass", mat.attr.rclass.get(0))
|
94
|
+
# retrieving the 'class' attribute by calling eval.
|
95
|
+
assert_equal("myClass", R.eval("attr(#{mat.r}, \"class\")").gz)
|
96
|
+
|
97
|
+
# Attribute 'comment' should not be seen when printed, but is there normally. In
|
98
|
+
# Renjin this attr does show up when printed
|
99
|
+
mat.attr.comment = "this is my comment on this matrix"
|
100
|
+
|
101
|
+
mat.pp
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
#--------------------------------------------------------------------------------------
|
106
|
+
#
|
107
|
+
#--------------------------------------------------------------------------------------
|
108
|
+
|
109
|
+
should "modify attributes in chains" do
|
110
|
+
|
111
|
+
# define a list with given names as attr
|
112
|
+
z = R.list(a: 1, b: "c", c: (1..3))
|
113
|
+
|
114
|
+
assert_equal("a", z.attr.names.gz)
|
115
|
+
assert_equal("b", z.attr.names.get(1))
|
116
|
+
assert_equal("c", z.attr.names.get(2))
|
117
|
+
|
118
|
+
# modify one of the names' attributes
|
119
|
+
z.attr.names[3] = "c2"
|
120
|
+
assert_equal(true, (R.c("a", "b", "c2").eq z.attr.names).gt)
|
121
|
+
|
122
|
+
# modify all names attribute directly
|
123
|
+
z.attr.names = R.c("d", "e", "f")
|
124
|
+
assert_equal(true, (R.c("d", "e", "f").eq z.attr.names).gt)
|
125
|
+
|
126
|
+
# create a vector with the names attribute. Vector names is now an alias to
|
127
|
+
# z.attr.names.
|
128
|
+
names = z.attr.names
|
129
|
+
assert_equal(true, (R.c("d", "e", "f").eq names).gt)
|
130
|
+
# change the names vector
|
131
|
+
names[1] = "g"
|
132
|
+
assert_equal(false, (R.c("d", "e", "f").eq names).gt)
|
133
|
+
assert_equal(true, (R.c("g", "e", "f").eq names).gt)
|
134
|
+
|
135
|
+
# differently from R, the names' attribute is also changed
|
136
|
+
assert_equal(false, (R.c("d", "e", "f").eq z.attr.names).gt)
|
137
|
+
assert_equal(true, (R.c("g", "e", "f").eq z.attr.names).gt)
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
#--------------------------------------------------------------------------------------
|
142
|
+
#
|
143
|
+
#--------------------------------------------------------------------------------------
|
144
|
+
|
145
|
+
should "unbind a vector from the attributes in chains" do
|
146
|
+
|
147
|
+
# define a list with given names as attr
|
148
|
+
z = R.list(a: 1, b: "c", c: (1..3))
|
149
|
+
|
150
|
+
# change the names using normal call to R.c and channing
|
151
|
+
z.attr.names = R.c("d", "e", "f")
|
152
|
+
|
153
|
+
# create a vector with the names attribute. Vector names is now an alias to
|
154
|
+
# z.attr.names.
|
155
|
+
names = z.attr.names
|
156
|
+
assert_equal(true, (R.c("d", "e", "f").eq names).gt)
|
157
|
+
|
158
|
+
# unbind names from z.attr.names.
|
159
|
+
names.unbind
|
160
|
+
|
161
|
+
# change the names vector
|
162
|
+
names[1] = "g"
|
163
|
+
assert_equal(false, (R.c("d", "e", "f").eq names).gt)
|
164
|
+
assert_equal(true, (R.c("g", "e", "f").eq names).gt)
|
165
|
+
|
166
|
+
# the names' attribute is NOT changed, as unbind on names was called
|
167
|
+
assert_equal(true, (R.c("d", "e", "f").eq z.attr.names).gt)
|
168
|
+
assert_equal(false, (R.c("g", "e", "f").eq z.attr.names).gt)
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
#--------------------------------------------------------------------------------------
|
173
|
+
#
|
174
|
+
#--------------------------------------------------------------------------------------
|
175
|
+
|
176
|
+
should "change attributes through normal R calls" do
|
177
|
+
|
178
|
+
# define a list with given names as attr
|
179
|
+
z = R.list(a: 1, b: "c", c: (1..3))
|
180
|
+
|
181
|
+
# Vector names is a copy of names attribute.
|
182
|
+
names = R.attr(z, "names")
|
183
|
+
assert_equal(true, (R.c("a", "b", "c").eq z.attr.names).gt)
|
184
|
+
|
185
|
+
# changing names...
|
186
|
+
names[1] = "hello there"
|
187
|
+
assert_equal(true, (R.c("hello there", "b", "c").eq names).gt)
|
188
|
+
|
189
|
+
# ... does not change the names attribute and there is no need to call unbind,
|
190
|
+
# as names was never bound to the chain
|
191
|
+
assert_equal(true, (R.c("a", "b", "c").eq z.attr.names).gt)
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
#--------------------------------------------------------------------------------------
|
196
|
+
#
|
197
|
+
#--------------------------------------------------------------------------------------
|
198
|
+
|
199
|
+
should "assign attributes between objects" do
|
200
|
+
|
201
|
+
# define a list with given names as attr
|
202
|
+
z = R.list(a: 1, b: "c", c: (1..3))
|
203
|
+
|
204
|
+
# define a vector with the same number of elements as the list
|
205
|
+
vec = R.c((1..3))
|
206
|
+
vec.attr.names = z.attr.names
|
207
|
+
vec.attr.names[1] = "ggg"
|
208
|
+
# changing vec names does not have any impact on z.names
|
209
|
+
assert_equal(true, (R.c("ggg", "b", "c").eq vec.attr.names).gt)
|
210
|
+
assert_equal(true, (R.c("a", "b", "c").eq z.attr.names).gt)
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
#--------------------------------------------------------------------------------------
|
215
|
+
#
|
216
|
+
#--------------------------------------------------------------------------------------
|
217
|
+
|
218
|
+
should "acess vector data by using indexing with 'names' attribute" do
|
219
|
+
|
220
|
+
# Must be careful... any assignment to a vector (object?) creates a new object
|
221
|
+
# value in Ruby will reference the old object and they will be different.
|
222
|
+
dbl_var = R.eval("var = c(1, 2, 3, 4)")
|
223
|
+
dbl_var.attr.names = R.c("one", "two", "three", "four")
|
224
|
+
dbl_var.attr.name = "my.name"
|
225
|
+
dbl_var.pp
|
226
|
+
|
227
|
+
# access element on a vector by name
|
228
|
+
dbl_var["one"].pp
|
229
|
+
assert_equal(2, dbl_var["two"].gz)
|
230
|
+
assert_equal(4, dbl_var["four"].gz)
|
231
|
+
|
232
|
+
R.eval <<EOF
|
233
|
+
l = c(1, 2, 3, 4)
|
234
|
+
attr(l, "name") = "my.name"
|
235
|
+
print(attributes(l))
|
236
|
+
|
237
|
+
# l2 = l
|
238
|
+
# print(attributes(l2))
|
239
|
+
EOF
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
#--------------------------------------------------------------------------------------
|
244
|
+
#
|
245
|
+
#--------------------------------------------------------------------------------------
|
246
|
+
|
247
|
+
should "acess data by using accessor methods from 'names' attribute" do
|
248
|
+
|
249
|
+
dbl_var = R.eval("var = c(1, 2, 3, 4)")
|
250
|
+
dbl_var.attr.names = R.c("one", "two", "three", "four")
|
251
|
+
|
252
|
+
assert_equal(1, dbl_var.one.gz)
|
253
|
+
assert_equal(2, dbl_var.two.gz)
|
254
|
+
assert_equal(3, dbl_var.three.gz)
|
255
|
+
assert_equal(4, dbl_var.four.gz)
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|