rserve-client 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 0.1.8 / 2010-06-01
2
+
3
+ * Bug fix: attributes for generic vectors (data.frames) stored correctly. A Double vector without attribute dim raises an error on #to_ruby
4
+ * Almost complete REXP#to_ruby according to http://wiki.github.com/clbustos/Rserve-Ruby-client/to_ruby-worflow
5
+ * Updated examples
6
+
7
+
1
8
  === 0.1.7 / 2010-05-28
2
9
 
3
10
  * Reimplement Rlist as an Array with module WithNames included
data/Manifest.txt CHANGED
@@ -1,5 +1,3 @@
1
- .autotest
2
- .gitignore
3
1
  Examples.txt
4
2
  History.txt
5
3
  Manifest.txt
@@ -39,11 +37,13 @@ lib/rserve/withattributes.rb
39
37
  lib/rserve/withnames.rb
40
38
  spec/rserve_connection_spec.rb
41
39
  spec/rserve_double_spec.rb
40
+ spec/rserve_genericvector_spec.rb
42
41
  spec/rserve_integer_spec.rb
43
42
  spec/rserve_logical_spec.rb
44
43
  spec/rserve_packet_spec.rb
45
44
  spec/rserve_protocol_spec.rb
46
45
  spec/rserve_rexp_spec.rb
46
+ spec/rserve_rexp_to_ruby_spec.rb
47
47
  spec/rserve_rexp_wrapper_spec.rb
48
48
  spec/rserve_rexpfactory_spec.rb
49
49
  spec/rserve_rfactor_spec.rb
data/README.txt CHANGED
@@ -86,7 +86,8 @@ Spec
86
86
  # The API could manage complex recursive list
87
87
 
88
88
  x=con.eval('list(l1=list(c(2,3)),l2=c(1,2,3))').to_ruby
89
- => {"l1"=>[[(2/1), (3/1)]], "l2"=>[(1/1), (2/1), (3/1)]}
89
+ => #<Array:19590368 [#<Array:19590116 [[(2/1), (3/1)]] names:nil>, [(1/1), (2/1), (3/1)]] names:["l1", "l2"]>
90
+
90
91
 
91
92
  # You could assign a REXP to R variables
92
93
 
data/examples/lowless.rb CHANGED
@@ -6,5 +6,7 @@ c = Rserve::Connection.new();
6
6
  c.assign("x", data_x);
7
7
  c.assign("y", data_y);
8
8
  l = c.eval("lowess(x,y)").as_list
9
- lx = l.at("x").as_floats
10
- ly = l.at("y").as_floats
9
+ lx = l["x"].as_floats
10
+ ly = l["y"].as_floats
11
+ puts "lx:#{lx}"
12
+ puts "ly:#{ly}"
data/lib/rserve.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'socket'
2
2
 
3
3
  module Rserve
4
- VERSION = '0.1.7'
4
+ VERSION = '0.1.8'
5
5
  end
6
6
 
7
7
 
@@ -74,7 +74,7 @@ module Rserve
74
74
  @cont
75
75
  end
76
76
  def get_attr
77
- attr.nil? ? nil : attr.cont
77
+ @attr.nil? ? nil : @attr.cont
78
78
  end
79
79
  def sexp_mismatch(type)
80
80
  STDERR.puts("Warning: #{type} SEXP size mismatch")
@@ -118,10 +118,12 @@ module Rserve
118
118
  end
119
119
  end
120
120
  def parse_REXP(buf,o)
121
- puts "buffer:#{buf.to_s} | o= #{o}" if $DEBUG
122
121
 
123
122
  xl=get_len(buf,o)
124
123
  has_at = (buf[o]&128)!=0
124
+
125
+ puts "content:#{buf.slice(o,xl+4)} '#{buf.slice(o+4,xl+4).pack("C*")}'" if $DEBUG
126
+
125
127
  is_long = (buf[o]&64 )!=0
126
128
  xt = buf[o]&63
127
129
  o+=4 if is_long
@@ -131,9 +133,14 @@ module Rserve
131
133
 
132
134
  @attr=REXPFactory.new()
133
135
  @cont=nil
134
- o = attr.parse_REXP(buf,o) if has_at
136
+ if has_at
137
+ puts "Processing attribs:" if $DEBUG
138
+ o = attr.parse_REXP(buf,o)
139
+ puts "FINAL ATTRIB:" if $DEBUG
140
+ pp get_attr.as_list if $DEBUG
141
+ end
142
+ puts "REXP: #{xt_name(@type)}(#{@type})[#{o},#{xl}], attr?:#{has_at}, attr=[#{get_attr}]" if $DEBUG
135
143
 
136
- puts "REXP: #{xt_name(@type)}(#{@type})[#{xl}], attr?:#{has_at}, attr=[#{get_attr}]" if $DEBUG
137
144
 
138
145
 
139
146
  if xt==XT_NULL
@@ -233,7 +240,7 @@ module Rserve
233
240
  $STDERR.puts "int SEXP size mismatch"
234
241
  o=eox
235
242
  end
236
-
243
+
237
244
  # hack for factors
238
245
  if (!get_attr.nil?)
239
246
  ca = get_attr().as_list["class"]
@@ -244,10 +251,9 @@ module Rserve
244
251
  xt = XT_FACTOR;
245
252
  end
246
253
  end
247
-
248
-
249
-
254
+
250
255
  if @cont.nil?
256
+
251
257
  @cont=REXP::Integer.new(d,get_attr)
252
258
  end
253
259
  return o
@@ -263,6 +269,7 @@ module Rserve
263
269
  end
264
270
 
265
271
  if xt==XT_LIST_NOTAG or xt==XT_LIST_TAG or xt==XT_LANG_NOTAG or xt==XT_LANG_TAG
272
+
266
273
  lc=REXPFactory.new
267
274
  nf=REXPFactory.new
268
275
  l=Rlist.new
@@ -271,19 +278,21 @@ module Rserve
271
278
  o=lc.parse_REXP(buf,o)
272
279
  if(xt==XT_LIST_TAG or xt==XT_LANG_TAG)
273
280
  o=nf.parse_REXP(buf,o)
274
-
281
+
275
282
  name=nf.cont.as_string if(nf.cont.symbol? or nf.cont.string?)
276
283
  end
284
+ puts "Agregando '#{name}'='#{lc.cont.inspect}'" if $DEBUG
277
285
  if name.nil?
278
286
  l.push(lc.cont)
279
287
  else
280
288
  l.put(name,lc.cont)
281
289
  end
282
290
  end
291
+ p l.inspect if $DEBUG
283
292
 
284
293
  @cont=(xt==XT_LANG_NOTAG or xt==XT_LANG_TAG) ?
285
- REXP::Language.new(l,get_attr) : REXP::List.new(l, get_attr)
286
-
294
+ REXP::Language.new(l, get_attr) : REXP::List.new(l, get_attr)
295
+ pp @cont if $DEBUG
287
296
  if(o!=eox)
288
297
  $STDERR.puts "Mismatch"
289
298
  o=eox
@@ -343,14 +352,13 @@ module Rserve
343
352
  end
344
353
  # fixup for lists since they're stored as attributes of vectors
345
354
  if !get_attr.nil? and !get_attr.as_list['names'].nil?
355
+ puts "PROCESSING NAMES" if $DEBUG
346
356
  nam=get_attr.as_list['names']
347
357
  names=nil
348
358
  if nam.string?
349
359
  names=nam.as_strings
350
360
  elsif nam.vector?
351
- l=nam.to_a
352
- names=Array.new(aa.length)
353
- aa.length.times {|i| names[i]=aa[i].as_string}
361
+ names=nam.as_list.map {|v| v.as_string}
354
362
  end
355
363
  l=Rlist.new(v,names)
356
364
  @cont=(xt==XT_VECTOR_EXP) ? REXP::ExpressionVector.new(l,get_attr) : REXP::GenericVector.new(l,get_attr)
data/lib/rserve/rexp.rb CHANGED
@@ -11,6 +11,9 @@ module Rserve
11
11
  def initialize(attr=nil)
12
12
  # Sorry for this, but I think is necessary to maintain sanity of attributes
13
13
  raise ArgumentError, "Attribute should be a REXP::List, #{attr.class} provided" unless attr.nil? or attr.is_a? REXP::List
14
+
15
+
16
+
14
17
  @attr=attr
15
18
  end
16
19
  # specifies how many items of a vector or list will be displayed in {@link #toDebugString}
@@ -188,20 +191,20 @@ module Rserve
188
191
  #
189
192
  # @return 2D array of doubles in the form double[rows][cols] or <code>null</code> if the contents is no 2-dimensional matrix of doubles
190
193
  def as_double_matrix()
191
- ct = as_doubles()
192
- dim = get_attribute "dim"
193
- raise MismatchException, "matrix (dim attribute missing)" if dim.nil?
194
- ds = dim.as_integers
195
- raise MismatchException, "matrix (wrong dimensionality)" if (ds.length!=2)
196
- m,n = ds[0], ds[1]
197
- # R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4)
198
- # we need to copy everything, since we create 2d array from 1d array
199
- r=m.times.map {|i| n.times.map {|j| ct[j*n+i]}}
194
+ ct = as_doubles()
195
+ dim = get_attribute "dim"
196
+ raise MismatchException, "matrix (dim attribute missing)" if dim.nil?
197
+ ds = dim.as_integers
198
+ raise MismatchException, "matrix (wrong dimensionality)" if (ds.length!=2)
199
+ m,n = ds[0], ds[1]
200
+ # R stores matrices as matrix(c(1,2,3,4),2,2) = col1:(1,2), col2:(3,4)
201
+ # we need to copy everything, since we create 2d array from 1d array
202
+ r=m.times.map {|i| n.times.map {|j| ct[j*n+i]}}
200
203
  end
201
204
  # Returns a standard library's matrix
202
205
  def as_matrix
203
- require 'matrix'
204
- Matrix.rows(as_double_matrix)
206
+ require 'matrix'
207
+ Matrix.rows(as_double_matrix)
205
208
  end
206
209
 
207
210
  # :section: tools
@@ -49,6 +49,13 @@ module Rserve
49
49
  t=super
50
50
  t << "{" << @payload.map(&:to_s).join(",") << "}"
51
51
  end
52
+ def to_ruby
53
+ if !attr.nil? and !attr.as_list['dim'].nil? and attr.as_list['dim'].to_ruby.size==2
54
+ as_matrix
55
+ else
56
+ super
57
+ end
58
+ end
52
59
  end
53
60
  end
54
61
  end
@@ -5,7 +5,7 @@ module Rserve
5
5
  def initialize(list, attr=nil)
6
6
  super(attr)
7
7
  @payload=list.nil? ? Rlist.new() : list
8
- if (payload.named?)
8
+ if (attr.nil? and payload.named? )
9
9
  @attr = REXP::List.new(
10
10
  Rlist.new([REXP::String.new(payload.keys())],
11
11
  ["names"]));
@@ -59,6 +59,17 @@ module Rserve
59
59
  def false?
60
60
  @payload.map {|v| v==FALSE}
61
61
  end
62
+
63
+ def to_ruby
64
+ if @payload.nil? or @payload.size==0
65
+ nil
66
+ elsif @payload.size==1
67
+ @payload[0]==1 ? true : false
68
+ else
69
+ @payload.map {|v| na?(v) ? nil : (v==1 ? true : false)}
70
+ end
71
+ end
72
+
62
73
  end
63
74
  end
64
75
  end
@@ -22,8 +22,12 @@ module Rserve
22
22
  @payload
23
23
  end
24
24
 
25
- def na?
26
- @payload.map {|v| v=='NA'}
25
+ def na?(value=nil)
26
+ if value.nil?
27
+ @payload.map {|v| v==NA}
28
+ else
29
+ value==NA
30
+ end
27
31
  end
28
32
  def to_debug_string
29
33
  t=super
data/lib/rserve/rlist.rb CHANGED
@@ -37,7 +37,9 @@ module Rserve
37
37
  #p "Comparing #{self.inspect} with #{o.inspect} gives #{o.is_a? Rlist and self.data==o.data and self.names==o.names}"
38
38
  o.is_a? Rlist and self.data==o.data and self.names==o.names
39
39
  end
40
-
40
+ def to_s
41
+ super+"[#{size}]"
42
+ end
41
43
 
42
44
  # Returns an Array with module WithNames included
43
45
  # * Unnamed list: returns an Array
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__)+"/spec_helper.rb"
2
+
3
+ describe Rserve::REXP::GenericVector do
4
+ describe "initialization" do
5
+ it "should accept Rlist as payload and create an attrib called 'names'" do
6
+ payload=Rserve::Rlist.new([1,2,3],%w{a b c})
7
+ a=Rserve::REXP::GenericVector.new(payload)
8
+ a.payload.should==payload
9
+ a.attr.as_list['names'].to_ruby.should==%w{a b c}
10
+ end
11
+ it "should accept Rlist and attribs as payload" do
12
+ payload=Rserve::Rlist.new([1,2,3],%w{a b c})
13
+ attribs=Rserve::REXP::List.new(Rserve::Rlist.new([Rserve::REXP::String.new(%w{a b c}), Rserve::REXP::String.new('data.frame')],%w{names class}))
14
+ a=Rserve::REXP::GenericVector.new(payload,attribs)
15
+ a.payload.should==payload
16
+ a.attr.should==attribs
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,44 @@
1
+ require File.dirname(__FILE__)+"/spec_helper.rb"
2
+
3
+ describe "Rserve::REXP#to_ruby" do
4
+ describe "method to_ruby" do
5
+ before do
6
+ @r=Rserve::Connection.new
7
+ end
8
+ it "should return a Fixnum with vector with one integer element" do
9
+ @r.eval("1").to_ruby.should==1
10
+ end
11
+ it "should return an array of Fixnum and nils with vector with two or more elements" do
12
+ @r.eval("c(1,2,3,NA)").to_ruby.should==[1,2,3,nil]
13
+ end
14
+ it "should return a rational with vector with one element" do
15
+ @r.eval("c(0.5)").to_ruby.should==1.quo(2)
16
+ end
17
+ it "should return an array of rationals with vector with more than one elements" do
18
+ @r.eval("c(0.5,0.5,NA)").to_ruby.should==[1.quo(2), 1.quo(2),nil]
19
+ end
20
+ it "should return a Ruby Matrix with R matrix" do
21
+ @r.eval("matrix(c(1,2,3,4),2,2)").to_ruby.should==Matrix[[1,3],[2,4]]
22
+ end
23
+ it "should return a nested array of Ruby Matrixes with vector with more than tree dimensions"
24
+ it "should return a boolean with a logical with one element" do
25
+ @r.eval("TRUE").to_ruby.should be_true
26
+ end
27
+
28
+ it "should return an array of booleans with a logical with two or more elements" do
29
+ @r.eval("c(TRUE,FALSE,NA)").to_ruby.should==[true,false,nil]
30
+ end
31
+
32
+ it "should return a string with a vector with one string" do
33
+ @r.eval("'a'").to_ruby.should=='a'
34
+ end
35
+ it "should return an array of strings with a vector with two or more strings" do
36
+ @r.eval("c('a','b',NA)").to_ruby.should==['a','b',nil]
37
+ end
38
+ it "should return an array extended with Rserve::WithNames for a list" do
39
+ expected=[1,2,3].extend Rserve::WithNames
40
+ expected.names=%w{a b c}
41
+ @r.eval('list(a=1,b=2,c=3)').to_ruby.should==expected
42
+ end
43
+ end
44
+ end
@@ -3,9 +3,11 @@
3
3
  require File.dirname(__FILE__)+"/spec_helper.rb"
4
4
 
5
5
  describe Rserve::Protocol::REXPFactory do
6
+
6
7
  before do
7
8
  @r=Rserve::Connection.new
8
9
  end
10
+
9
11
  it "should process null" do
10
12
  la=@r.eval("NULL")
11
13
  la.should be_instance_of(Rserve::REXP::Null)
@@ -103,6 +105,15 @@ describe Rserve::Protocol::REXPFactory do
103
105
  la=@r.eval("list(2,NA)")
104
106
  la.should be_true
105
107
  end
108
+
109
+ it "should process data.frame" do
110
+ la=@r.eval("data.frame(a=1:10,b=1:10)")
111
+ la.should be_true
112
+ la.attr.as_list['names'].to_ruby.should==%w{a b}
113
+ la.attr.as_list['class'].to_ruby.should=="data.frame"
114
+ #la.attr.as_list['row.names'].to_ruby.should==[1,2,3,4,5,6,7,8,9,10]
115
+ end
116
+
106
117
  it "should retrieve correct lenght for string" do
107
118
  Rserve::Protocol::REXPFactory.new(Rserve::REXP::String.new("a")).get_binary_length.should==8
108
119
  Rserve::Protocol::REXPFactory.new(Rserve::REXP::String.new(["a","b"])).get_binary_length.should==8
@@ -133,7 +144,6 @@ describe Rserve::Protocol::REXPFactory do
133
144
  end
134
145
 
135
146
  it "should retrieve correct binary representation for List" do
136
-
137
147
  rexp=Rserve::REXP::List.new(Rserve::Rlist.new([Rserve::REXP::String.new("a")], ["names"]));
138
148
  buf=Array.new(Rserve::Protocol::REXPFactory.new(rexp).get_binary_length)
139
149
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 7
9
- version: 0.1.7
8
+ - 8
9
+ version: 0.1.8
10
10
  platform: ruby
11
11
  authors:
12
12
  - Claudio Bustos
@@ -35,7 +35,7 @@ cert_chain:
35
35
  rpP0jjs0
36
36
  -----END CERTIFICATE-----
37
37
 
38
- date: 2010-05-28 00:00:00 -04:00
38
+ date: 2010-06-01 00:00:00 -04:00
39
39
  default_executable:
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
@@ -82,8 +82,6 @@ extra_rdoc_files:
82
82
  - Manifest.txt
83
83
  - README.txt
84
84
  files:
85
- - .autotest
86
- - .gitignore
87
85
  - Examples.txt
88
86
  - History.txt
89
87
  - Manifest.txt
@@ -123,11 +121,13 @@ files:
123
121
  - lib/rserve/withnames.rb
124
122
  - spec/rserve_connection_spec.rb
125
123
  - spec/rserve_double_spec.rb
124
+ - spec/rserve_genericvector_spec.rb
126
125
  - spec/rserve_integer_spec.rb
127
126
  - spec/rserve_logical_spec.rb
128
127
  - spec/rserve_packet_spec.rb
129
128
  - spec/rserve_protocol_spec.rb
130
129
  - spec/rserve_rexp_spec.rb
130
+ - spec/rserve_rexp_to_ruby_spec.rb
131
131
  - spec/rserve_rexp_wrapper_spec.rb
132
132
  - spec/rserve_rexpfactory_spec.rb
133
133
  - spec/rserve_rfactor_spec.rb
@@ -173,6 +173,7 @@ test_files:
173
173
  - spec/rserve_rexp_wrapper_spec.rb
174
174
  - spec/rserve_rfactor_spec.rb
175
175
  - spec/rserve_connection_spec.rb
176
+ - spec/rserve_rexp_to_ruby_spec.rb
176
177
  - spec/rserve_double_spec.rb
177
178
  - spec/rserve_withnames_spec.rb
178
179
  - spec/rserve_packet_spec.rb
@@ -181,5 +182,6 @@ test_files:
181
182
  - spec/rserve_integer_spec.rb
182
183
  - spec/rserve_spec.rb
183
184
  - spec/rserve_protocol_spec.rb
185
+ - spec/rserve_genericvector_spec.rb
184
186
  - spec/rserve_logical_spec.rb
185
187
  - spec/rserve_talk_spec.rb
metadata.gz.sig CHANGED
@@ -1 +1,2 @@
1
- ze�+V ��֐�ʸS�p�Dܤ�!vov~��?u�zt���!�_L4P\�-�3�P����*S�x���ή�W���m;��cP��ѺI�>��KG8a��q��>�M�E�.��[QiQ5�� �� Ve}>���6C�p~�Hb-��*�H'�����jJ9\,g��ȒQ�� ��SF��'�����kH�b��u-����&e
1
+ ySF�QW�?��~�o��{:����S�J>vh�I���>�<ogl5\�����N"���i�D��6�����0�|e��JR_�c��lZ�皔�nR����z��0>0�??��Q�M��$K��E��۰����ul��*%d��v<՜��4al
2
+ N`�t��&��
data/.autotest DELETED
@@ -1,23 +0,0 @@
1
- # -*- ruby -*-
2
-
3
- require 'autotest/restart'
4
-
5
- # Autotest.add_hook :initialize do |at|
6
- # at.extra_files << "../some/external/dependency.rb"
7
- #
8
- # at.libs << ":../some/external"
9
- #
10
- # at.add_exception 'vendor'
11
- #
12
- # at.add_mapping(/dependency.rb/) do |f, _|
13
- # at.files_matching(/test_.*rb$/)
14
- # end
15
- #
16
- # %w(TestA TestB).each do |klass|
17
- # at.extra_class_map[klass] = "test/test_misc.rb"
18
- # end
19
- # end
20
-
21
- # Autotest.add_hook :run_command do |at|
22
- # system "rake build"
23
- # end
data/.gitignore DELETED
@@ -1,5 +0,0 @@
1
- coverage.info
2
- *~
3
- doc
4
- pkg
5
- coverage