rserve-client 0.1.3 → 0.1.4
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.
- data.tar.gz.sig +0 -0
- data/History.txt +4 -0
- data/Rakefile +1 -0
- data/lib/rserve.rb +3 -1
- data/lib/rserve/protocol/rexpfactory.rb +7 -4
- data/lib/rserve/rexp.rb +2 -1
- data/lib/rserve/rexp/double.rb +2 -1
- data/lib/rserve/rexp/logical.rb +6 -3
- data/lib/rserve/rexp/string.rb +1 -1
- data/spec/rserve_logical_spec.rb +61 -0
- data/spec/rserve_rexpfactory_spec.rb +33 -1
- data/spec/rserve_rfactor_spec.rb +45 -0
- metadata +14 -4
- metadata.gz.sig +0 -0
data.tar.gz.sig
CHANGED
Binary file
|
data/History.txt
CHANGED
data/Rakefile
CHANGED
@@ -6,6 +6,7 @@ require 'rserve'
|
|
6
6
|
Hoe.plugin :git
|
7
7
|
Hoe.spec 'rserve-client' do
|
8
8
|
self.testlib=:rspec
|
9
|
+
self.test_globs="spec/*_spec.rb"
|
9
10
|
self.version=Rserve::VERSION
|
10
11
|
self.rubyforge_name = 'ruby-statsample' # if different than 'rserve'
|
11
12
|
self.developer('Claudio Bustos', 'clbustos_AT_gmail.com')
|
data/lib/rserve.rb
CHANGED
@@ -177,8 +177,9 @@ module Rserve
|
|
177
177
|
d=Array.new(as)
|
178
178
|
(eox-i).times {|i| d[i]=buf[o+i]}
|
179
179
|
o=eox
|
180
|
+
|
180
181
|
d.length.each {|j|
|
181
|
-
if d[j]!=0
|
182
|
+
if d[j]!=0 and d[j]!=1
|
182
183
|
d[j]==REXP::Logical::NA
|
183
184
|
end
|
184
185
|
}
|
@@ -190,9 +191,11 @@ module Rserve
|
|
190
191
|
o+=4
|
191
192
|
d=Array.new(as)
|
192
193
|
as.times {|i| d[i]=buf[o+i]}
|
193
|
-
d.
|
194
|
-
if
|
195
|
-
|
194
|
+
d.collect! {|v|
|
195
|
+
if v!=0 and v!=1
|
196
|
+
REXP::Logical::NA
|
197
|
+
else
|
198
|
+
v
|
196
199
|
end
|
197
200
|
}
|
198
201
|
o=eox
|
data/lib/rserve/rexp.rb
CHANGED
@@ -227,9 +227,10 @@ end
|
|
227
227
|
|
228
228
|
require 'rserve/rexp/vector'
|
229
229
|
require 'rserve/rexp/null'
|
230
|
-
|
231
230
|
require 'rserve/rexp/genericvector'
|
232
231
|
require 'rserve/rexp/integer'
|
232
|
+
require 'rserve/rexp/factor'
|
233
|
+
|
233
234
|
require 'rserve/rexp/double'
|
234
235
|
require 'rserve/rexp/list'
|
235
236
|
require 'rserve/rexp/language'
|
data/lib/rserve/rexp/double.rb
CHANGED
@@ -2,7 +2,8 @@ module Rserve
|
|
2
2
|
class REXP
|
3
3
|
class Double < REXP::Vector
|
4
4
|
attr_reader :payload
|
5
|
-
|
5
|
+
# In Java, you only need to add L at last :(
|
6
|
+
NA = 0x100000000007a2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
6
7
|
def initialize(data, attrs=nil)
|
7
8
|
@payload=case data
|
8
9
|
when Numeric
|
data/lib/rserve/rexp/logical.rb
CHANGED
@@ -13,12 +13,13 @@ module Rserve
|
|
13
13
|
@payload.map {|v| v==NA}
|
14
14
|
end
|
15
15
|
end
|
16
|
+
# l should be a value or array of 0 and 1.
|
16
17
|
def initialize(l,attr=nil)
|
17
18
|
super(attr)
|
18
19
|
if l.is_a? Array
|
19
|
-
@payload=
|
20
|
+
@payload= l
|
20
21
|
else
|
21
|
-
@payload = [ l==1 ? TRUE : FALSE ]
|
22
|
+
@payload = [ l==NA ? NA : (l==1 ? TRUE : FALSE) ]
|
22
23
|
end
|
23
24
|
end
|
24
25
|
def length
|
@@ -27,6 +28,9 @@ module Rserve
|
|
27
28
|
def logical?
|
28
29
|
true
|
29
30
|
end
|
31
|
+
def as_bytes
|
32
|
+
@payload
|
33
|
+
end
|
30
34
|
def as_integers
|
31
35
|
@payload.map {|v| v==NA ? REXP::Integer::NA : ( v == FALSE ? 0 : 1 )}
|
32
36
|
end
|
@@ -41,7 +45,6 @@ module Rserve
|
|
41
45
|
end
|
42
46
|
def false?
|
43
47
|
@payload.map {|v| v==FALSE}
|
44
|
-
|
45
48
|
end
|
46
49
|
end
|
47
50
|
end
|
data/lib/rserve/rexp/string.rb
CHANGED
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.dirname(__FILE__)+"/spec_helper.rb"
|
2
|
+
|
3
|
+
describe Rserve::REXP::Integer do
|
4
|
+
describe "initialization" do
|
5
|
+
it "should accept array as payload" do
|
6
|
+
payload=[0,1,0]
|
7
|
+
a=Rserve::REXP::Logical.new(payload)
|
8
|
+
a.payload.should==payload
|
9
|
+
end
|
10
|
+
it "should accept a single value as payload" do
|
11
|
+
payload=1
|
12
|
+
a=Rserve::REXP::Logical.new(payload)
|
13
|
+
a.payload.should==[ Rserve::REXP::Logical::TRUE ]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
describe "common methods" do
|
17
|
+
before do
|
18
|
+
@n=rand(100)
|
19
|
+
@payload=@n.times.map {rand(2)}
|
20
|
+
@a=Rserve::REXP::Logical.new(@payload)
|
21
|
+
end
|
22
|
+
subject {@a}
|
23
|
+
it "should return correct length of payload" do
|
24
|
+
@a.length.should==@n
|
25
|
+
end
|
26
|
+
it {should be_logical}
|
27
|
+
it "method as_bytes should return payload" do
|
28
|
+
@a.as_bytes.should==@payload
|
29
|
+
end
|
30
|
+
it "method as_integer should return integers" do
|
31
|
+
@a.as_integers.should==@payload.map {|v| v==Rserve::REXP::Logical::TRUE ? 1 : 0}
|
32
|
+
end
|
33
|
+
it "method as_doubles should return floats" do
|
34
|
+
@a.as_doubles.should==@payload.map {|v| v==Rserve::REXP::Logical::TRUE ? 1.0 : 0.0}
|
35
|
+
end
|
36
|
+
it "method as_string should return string" do
|
37
|
+
@a.as_strings.should==@payload.map {|v| v==Rserve::REXP::Logical::TRUE ? "TRUE" : "FALSE"}
|
38
|
+
end
|
39
|
+
it "method true? should return an array with true for TRUE values" do
|
40
|
+
@a.true?.should==@payload.map {|v| v==Rserve::REXP::Logical::TRUE ? true :false}
|
41
|
+
end
|
42
|
+
it "method false? should return an array with true for FALSE values" do
|
43
|
+
@a.false?.should==@payload.map {|v| v==Rserve::REXP::Logical::FALSE ? true :false}
|
44
|
+
end
|
45
|
+
|
46
|
+
it "method is_NA should return coherent answer" do
|
47
|
+
payload=[1,Rserve::REXP::Logical::NA, 0]
|
48
|
+
a=Rserve::REXP::Logical.new(payload)
|
49
|
+
a.na?.should==[false,true,false]
|
50
|
+
a.na?(a.as_bytes[0]).should be_false
|
51
|
+
a.na?(a.as_bytes[1]).should be_true
|
52
|
+
end
|
53
|
+
it "method to_debug_string and to_s returns a coherent response" do
|
54
|
+
@a.to_debug_string.size.should>0
|
55
|
+
@a.to_s.size.should>0
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
end
|
@@ -16,6 +16,11 @@ describe Rserve::Protocol::REXPFactory do
|
|
16
16
|
la.should be_instance_of(Rserve::REXP::Logical)
|
17
17
|
la.true?.should==[true,false,true]
|
18
18
|
end
|
19
|
+
it "should process logical vectors with NA" do
|
20
|
+
la=@r.eval("c(TRUE,NA)")
|
21
|
+
la.should be_instance_of(Rserve::REXP::Logical)
|
22
|
+
la.na?.should==[false,true]
|
23
|
+
end
|
19
24
|
it "should process single double" do
|
20
25
|
la=@r.eval("1.5")
|
21
26
|
la.should be_instance_of(Rserve::REXP::Double)
|
@@ -30,11 +35,38 @@ describe Rserve::Protocol::REXPFactory do
|
|
30
35
|
v.should be_close(a[i],1e-10)
|
31
36
|
}
|
32
37
|
end
|
33
|
-
it "should process
|
38
|
+
it "should process double vector with NA" do
|
39
|
+
la=@r.eval("c(1,NA)")
|
40
|
+
la.should be_instance_of(Rserve::REXP::Double)
|
41
|
+
la.na?.should==[false,true]
|
42
|
+
|
43
|
+
end
|
44
|
+
it "should process string vector" do
|
34
45
|
la=@r.eval("c('abc','def','ghi')")
|
35
46
|
la.should be_instance_of(Rserve::REXP::String)
|
36
47
|
la.as_strings.should==['abc','def','ghi']
|
37
48
|
end
|
49
|
+
it "should process string vector with NA" do
|
50
|
+
la=@r.eval("c('abc','def',NA)")
|
51
|
+
la.should be_instance_of(Rserve::REXP::String)
|
52
|
+
la.na?.should==[false,false,true]
|
53
|
+
|
54
|
+
end
|
55
|
+
it "should process factor" do
|
56
|
+
la=@r.eval <<-EOF
|
57
|
+
state <- c("tas", "sa", "qld", "nsw", "nsw", "nt", "wa", "wa",
|
58
|
+
"qld", "vic", "nsw", "vic", "qld", "qld", "sa", "tas",
|
59
|
+
"sa", "nt", "wa", "vic", "qld", "nsw", "nsw", "wa",
|
60
|
+
"sa", "act", "nsw", "vic", "vic", "act");
|
61
|
+
statef <- factor(state)
|
62
|
+
EOF
|
63
|
+
la.should be_factor
|
64
|
+
la.as_factor.levels.sort.should==%w{act nsw nt qld sa tas vic wa}.sort
|
65
|
+
|
66
|
+
la.as_factor.contains?("tas").should be_true
|
67
|
+
la.as_factor.contains?("nn").should be_false
|
68
|
+
|
69
|
+
end
|
38
70
|
it "should process list" do
|
39
71
|
require 'pp'
|
40
72
|
la=@r.eval("list(name='Fred',age=30,10,20,kids=c(1,2,3))")
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.dirname(__FILE__)+"/spec_helper.rb"
|
2
|
+
|
3
|
+
describe Rserve::RFactor do
|
4
|
+
before do
|
5
|
+
@levels=['a','b','c','d']
|
6
|
+
@ids=[1,1,2,2,3,3,4]
|
7
|
+
@base_index=1
|
8
|
+
@factor=Rserve::RFactor.new(@ids,@levels,false,@base_index)
|
9
|
+
end
|
10
|
+
it "method size should return number of ids" do
|
11
|
+
@factor.size.should==@ids.size
|
12
|
+
end
|
13
|
+
it "method [] should return correct value (1 based)" do
|
14
|
+
@ids.each_index {|i|
|
15
|
+
@factor[i].should == @levels[@ids[i]-@base_index]
|
16
|
+
@factor[1].should=='a'
|
17
|
+
@factor[3].should=='b'
|
18
|
+
@factor[5].should=='c'
|
19
|
+
|
20
|
+
}
|
21
|
+
end
|
22
|
+
it "methods contains? should work with integers and strings" do
|
23
|
+
@factor.contains?(1).should be_true
|
24
|
+
@factor.contains?(5).should be_false
|
25
|
+
@factor.contains?('a').should be_true
|
26
|
+
@factor.contains?('z').should be_false
|
27
|
+
end
|
28
|
+
it "methods count should work with integers and strings" do
|
29
|
+
@factor.count(2).should==2
|
30
|
+
@factor.count(5).should==0
|
31
|
+
@factor.count('a').should==2
|
32
|
+
@factor.count('z').should==0
|
33
|
+
end
|
34
|
+
it "method counts_hash should return correct value" do
|
35
|
+
@factor.counts_hash.should=={'a'=>2,'b'=>2,'c'=>2,'d'=>1}
|
36
|
+
end
|
37
|
+
it "method as_integers should return correct values" do
|
38
|
+
@factor.as_integers.should==@ids
|
39
|
+
end
|
40
|
+
|
41
|
+
it "method as_strings should return correct values" do
|
42
|
+
@factor.as_strings==%w{a a b b c c d}
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 4
|
9
|
+
version: 0.1.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Claudio Bustos
|
@@ -149,5 +149,15 @@ rubygems_version: 1.3.6
|
|
149
149
|
signing_key:
|
150
150
|
specification_version: 3
|
151
151
|
summary: Ruby client for Rserve, a Binary R server (http://www.rforge.net/Rserve/)
|
152
|
-
test_files:
|
153
|
-
|
152
|
+
test_files:
|
153
|
+
- spec/rserve_rfactor_spec.rb
|
154
|
+
- spec/rserve_connection_spec.rb
|
155
|
+
- spec/rserve_double_spec.rb
|
156
|
+
- spec/rserve_packet_spec.rb
|
157
|
+
- spec/rserve_rexpfactory_spec.rb
|
158
|
+
- spec/rserve_rexp_spec.rb
|
159
|
+
- spec/rserve_integer_spec.rb
|
160
|
+
- spec/rserve_spec.rb
|
161
|
+
- spec/rserve_protocol_spec.rb
|
162
|
+
- spec/rserve_logical_spec.rb
|
163
|
+
- spec/rserve_talk_spec.rb
|
metadata.gz.sig
CHANGED
Binary file
|