rust 0.3 → 0.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 985f7e940ab123fa452dae63792bde90613b134176ff4eaf5b59a719dd8a1ed5
4
- data.tar.gz: bef0bb5028c99cb43c8e5453fdbbaf687bad65f8e6b54b06f949e5df6fb61bda
3
+ metadata.gz: aaa404db11033ff42b529516ae4a3f3e252bdddf7677d082847ee06625144f8e
4
+ data.tar.gz: f72ebc2c95385b87a445f3fb8de3e517579b002e0606209903acd2327616befd
5
5
  SHA512:
6
- metadata.gz: 418181b9357665ecc654e9a765e24aa792d6287dd5998b1b1bd8f3278e6951d785fff8afe9594c6c95e1f7a384cb08f844939728b62a96ab416259de1c14512b
7
- data.tar.gz: 810f14821924bd1b0cebf4fbf9f52e510abc0e935be6cc2852294fd374d54411bcd2a1943f5b60d2ce93501abcaff9cac25dd458e1a8b354b665aab199705a4a
6
+ metadata.gz: 00567c9b6216f7e9dc1a4135dbea263eecae2178273851c023980233531729313e15147e1bec8e9521df2d4fee15e2c4c8bf9bd4a0e2d1475f1d383339a17c21
7
+ data.tar.gz: 89800fff95be559e6f6bbbc05b6b5f5b52b7d59c54c61cd76ef679876ce5de0c08b405d88eadf0b254c95306ad98f38120e5960a92ce8358fca19ed6cc5c181d
data/bin/ruby-rust ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/ruby
2
+
3
+ system "irb -r \"rust\""
@@ -1,9 +1,10 @@
1
- require_relative 'rust-core'
1
+ require_relative '../core'
2
+ require 'csv'
2
3
 
3
4
  module Rust
4
5
  class CSV
5
6
  def self.read_all(pattern, **options)
6
- result = {}
7
+ result = DataFrameHash.new
7
8
  Dir.glob(pattern).each do |filename|
8
9
  result[filename] = CSV.read(filename, **options)
9
10
  end
@@ -49,10 +50,9 @@ module Rust
49
50
  raise TypeError, "Expected Rust::DataFrame" unless dataframe.is_a?(Rust::DataFrame)
50
51
 
51
52
  write_headers = options[:headers] != false
52
- options[:headers] = dataframe.column_names if options[:headers] == nil
53
+ options[:headers] = dataframe.column_names unless options[:headers]
53
54
 
54
55
  hash = {}
55
- labels = nil
56
56
  ::CSV.open(filename, 'w', write_headers: write_headers, **options) do |csv|
57
57
  dataframe.each do |row|
58
58
  csv << row
@@ -93,3 +93,13 @@ module Rust
93
93
  end
94
94
  end
95
95
  end
96
+
97
+ module Rust::RBindings
98
+ def read_csv(filename, **options)
99
+ Rust::CSV.read(filename, **options)
100
+ end
101
+
102
+ def write_csv(filename, dataframe, **options)
103
+ Rust::CSV.write(filename, dataframe, **options)
104
+ end
105
+ end
@@ -0,0 +1,157 @@
1
+ require 'code-assertions'
2
+ require 'stringio'
3
+ require 'rinruby'
4
+
5
+ module Rust
6
+ CLIENT_MUTEX = Mutex.new
7
+ R_MUTEX = Mutex.new
8
+
9
+ R_ENGINE = RinRuby.new(echo: false)
10
+
11
+ private_constant :R_ENGINE
12
+ private_constant :R_MUTEX
13
+ private_constant :CLIENT_MUTEX
14
+
15
+ @@debugging = $RUST_DEBUG || false
16
+ @@in_client_mutex = false
17
+
18
+ def self.debug
19
+ @@debugging = true
20
+ end
21
+
22
+ def self.debug?
23
+ return @@debugging
24
+ end
25
+
26
+ def self.exclusive
27
+ result = nil
28
+ CLIENT_MUTEX.synchronize do
29
+ @@in_client_mutex = true
30
+ result = yield
31
+ @@in_client_mutex = false
32
+ end
33
+ return result
34
+ end
35
+
36
+ def self.[]=(variable, value)
37
+ if value.is_a?(RustDatatype)
38
+ value.load_in_r_as(variable.to_s)
39
+ elsif value.is_a?(String) || value.is_a?(Numeric) || value.is_a?(Array) || value.is_a?(::Matrix)
40
+ R_ENGINE.assign(variable, value)
41
+ else
42
+ raise "Trying to assign #{variable} with #{value.class}; expected RustDatatype, String, Numeric, or Array"
43
+ end
44
+
45
+ end
46
+
47
+ def self.[](variable)
48
+ return RustDatatype.pull_variable(variable)
49
+ end
50
+
51
+ def self._eval_big(r_command, return_warnings = false)
52
+ r_command = r_command.join("\n") if r_command.is_a?(Array)
53
+
54
+ self._rexec(r_command, return_warnings) do |cmd|
55
+ result = true
56
+ instructions = cmd.lines
57
+
58
+ while instructions.size > 0
59
+ current_command = ""
60
+
61
+ while (instructions.size > 0) && (current_command.length + instructions.first.length < 10000)
62
+ current_command << instructions.shift
63
+ end
64
+
65
+ result &= R_ENGINE.eval(current_command)
66
+ end
67
+
68
+ result
69
+ end
70
+ end
71
+
72
+ def self._pull(r_command, return_warnings = false)
73
+ self._rexec(r_command, return_warnings) { |cmd| R_ENGINE.pull(cmd) }
74
+ end
75
+
76
+ def self._eval(r_command, return_warnings = false)
77
+ self._rexec(r_command, return_warnings) { |cmd| R_ENGINE.eval(cmd) }
78
+ end
79
+
80
+ def self._rexec(r_command, return_warnings = false)
81
+ puts "Calling _rexec with command: #{r_command}" if @@debugging
82
+ R_MUTEX.synchronize do
83
+ assert("This command must be executed in an exclusive block") { @@in_client_mutex }
84
+
85
+ result = nil
86
+ begin
87
+ $stdout = StringIO.new
88
+ if return_warnings
89
+ R_ENGINE.echo(true, true)
90
+ else
91
+ R_ENGINE.echo(false, false)
92
+ end
93
+ result = yield(r_command)
94
+ ensure
95
+ R_ENGINE.echo(false, false)
96
+ warnings = $stdout.string
97
+ $stdout = STDOUT
98
+ end
99
+
100
+ if return_warnings
101
+ puts " Got #{warnings.size} warnings, with result #{result.inspect[0...100]}" if @@debugging
102
+ return result, warnings.lines.map { |w| w.strip.chomp }
103
+ else
104
+ puts " Result: #{result.inspect[0...100]}" if @@debugging
105
+ return result
106
+ end
107
+ end
108
+ end
109
+
110
+ def self.check_library(name)
111
+ self.exclusive do
112
+ result, _ = self._pull("require(\"#{name}\", character.only = TRUE)", true)
113
+ return result
114
+ end
115
+ end
116
+
117
+ def self.load_library(name)
118
+ self.exclusive do
119
+ self._eval("library(\"#{name}\", character.only = TRUE)")
120
+ end
121
+
122
+ return nil
123
+ end
124
+
125
+ def self.install_library(name)
126
+ self.exclusive do
127
+ self._eval("install.packages(\"#{name}\", dependencies = TRUE)")
128
+ end
129
+
130
+ return nil
131
+ end
132
+
133
+ def self.prerequisite(library)
134
+ self.install_library(library) unless self.check_library(library)
135
+ self.load_library(library)
136
+ end
137
+ end
138
+
139
+ module Rust::RBindings
140
+ def data_frame(*args)
141
+ Rust::DataFrame.new(*args)
142
+ end
143
+ end
144
+
145
+ module Rust::TestCases
146
+ def self.sample_dataframe(columns, size=100)
147
+ result = Rust::DataFrame.new(columns)
148
+ size.times do |i|
149
+ result << columns.map { |c| yield i, c }
150
+ end
151
+ return result
152
+ end
153
+ end
154
+
155
+ def bind_r!
156
+ include Rust::RBindings
157
+ end
@@ -0,0 +1,4 @@
1
+ self_path = File.expand_path(__FILE__)
2
+ Dir.glob(File.dirname(self_path) + "/*.rb").each do |lib|
3
+ require_relative lib unless lib == self_path
4
+ end