daru 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: 1a2952b88ba0b72a6d7ada74bd03b03b7690f774
4
- data.tar.gz: fdace87c3406adda7f141aaab055fb1e40c66e0c
3
+ metadata.gz: e40b5dd9b236c4f7bbc3e96e0d97f73fe6d940b4
4
+ data.tar.gz: 377d3ddb6826c72179e84eb2f9cf66cc120b1236
5
5
  SHA512:
6
- metadata.gz: be50d9aafa90638208605de2bd68c121df1ce72eebe463ed3ec06f548104b5f86f73ea572b7424e03fca518f21f3425dc05608ace18cd3d430111c61b526ce3a
7
- data.tar.gz: 10c7243159bad7188f99095308035dc5064bea91d4ba966264f6d84a4b8dfac86c096f126df7c149da331770efdc3c0e8b2a54b48d6bd121ff046d0c9b5d740a
6
+ metadata.gz: 8e42d5fb359945d2ccab1f19062c1cc2e5123e4a33af54d0aeeda6db966d4d53444f50a40e8af2fbdffdf42e3416dcddf3421815b5e787567e73032365a59f62
7
+ data.tar.gz: 089564afd6be1eef4f01060b064273ea2ca5efb2dd60d2b63019d2f324bb560e814e1dde855838e54b28cf04b55be8fc02a821bf48adc7e8e65830005618e900
data/Gemfile.lock CHANGED
@@ -32,4 +32,4 @@ DEPENDENCIES
32
32
  bundler
33
33
  daru!
34
34
  nmatrix (~> 0.1.0.rc5)
35
- rspec (~> 3.0)
35
+ rspec
data/README.md CHANGED
@@ -2,3 +2,35 @@ daru
2
2
  ====
3
3
 
4
4
  Data Analysis in RUby
5
+
6
+ ## Introduction
7
+
8
+ daru (Data Analysis in RUby) is a library for storage, analysis and manipulation of data. It aims to be the preferred data analysis library for Ruby.
9
+
10
+ Development of daru was started to address the fragmentation of Dataframe-like classes which were created in many ruby gems as per their own needs.
11
+
12
+ This creates a hurdle in using these gems together to solve a problem. For example, calculating something in [statsample](https://github.com/clbustos/statsample) and plotting the results in [Nyaplot](https://github.com/domitry/nyaplot).
13
+
14
+ daru is heavily inspired by `Statsample::Dataset`, `Nyaplot::DataFrame` and the super-awesome pandas, a very mature solution in Python.
15
+
16
+ ## Data Structures
17
+
18
+ daru employs several data structures for storing and manipulating data:
19
+ * Vector - A basic 1-D vector.
20
+ * DataFrame - A 2-D matrix-like structure which is internally composed of named `Vector` classes.
21
+
22
+ daru data structures can be constructed by using several Ruby classes. These include `Array`, `Hash`, `Matrix`, [NMatrix](https://github.com/SciRuby/nmatrix) and [MDArray](https://github.com/rbotafogo/mdarray). daru brings a uniform API for handling and manipulating data represented in any of the above Ruby classes.
23
+
24
+ ## Testing
25
+
26
+ Install jruby using `rvm install jruby`, then run `jruby -S gem install mdarray`, followed by `bundle install`. You will need to install `mdarray` manually because of strange gemspec file behaviour. If anyone can automate this then I'd greatly appreciate it! Then run `rspec` in JRuby to test for MDArray functionality.
27
+
28
+ Then switch to MRI, do a normal `bundle install` followed by `rspec` for testing everything else with NMatrix functionality.
29
+
30
+ ## Roadmap
31
+
32
+ * Automate testing for both MRI and JRuby.
33
+ * Enable creation of DataFrame by only specifying an NMatrix/MDArray in initialize. Vector naming happens automatically (alphabetic) or is specified in an Array.
34
+ * Add support for missing values in vectors.
35
+ * Add normal and destructive map iterators.
36
+ * Completely test all functionality for NMatrix and MDArray.
data/daru.gemspec CHANGED
@@ -5,8 +5,7 @@ require 'version.rb'
5
5
 
6
6
  DESCRIPTION = <<MSG
7
7
  Daru (Data Analysis in RUby) is a library for storage, analysis and manipulation
8
- of data. It aims to be the preferred data storage library for all Ruby gems, promoting
9
- interoperability and easy of use between various ruby packages.
8
+ of data.
10
9
  MSG
11
10
 
12
11
  Gem::Specification.new do |spec|
@@ -25,6 +24,8 @@ Gem::Specification.new do |spec|
25
24
  spec.require_paths = ["lib"]
26
25
 
27
26
  spec.add_development_dependency 'bundler'
28
- spec.add_development_dependency 'rspec', '~> 3.0'
29
- spec.add_development_dependency 'nmatrix', '~> 0.1.0.rc5'
27
+ spec.add_development_dependency 'rspec'
28
+ if RUBY_ENGINE != 'jruby'
29
+ spec.add_development_dependency 'nmatrix', '~> 0.1.0.rc5'
30
+ end
30
31
  end
data/lib/daru.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'securerandom'
2
2
  require 'matrix'
3
+ require 'csv'
3
4
 
4
5
  require 'daru/vector.rb'
5
6
  require 'daru/dataframe.rb'
@@ -1,7 +1,180 @@
1
1
  module Daru
2
2
  class DataFrame
3
- def initialize source, name=SecureRandom.uuid
4
-
3
+
4
+ attr_reader :vectors
5
+
6
+ attr_reader :fields
7
+
8
+ attr_reader :size
9
+
10
+ attr_reader :name
11
+
12
+ def initialize source, fields=[], name=SecureRandom.uuid
13
+ if source.empty?
14
+ @vectors = fields.inject({}){ |a,x| a[x]=Daru::Vector.new; a}
15
+ else
16
+ @vectors = source
17
+ end
18
+
19
+ @fields = fields.empty? ? source.keys.sort : fields
20
+ @name = name
21
+
22
+ check_length
23
+ set_fields_order if @vectors.keys.sort != @fields.sort
24
+ set_vector_names
25
+ end
26
+
27
+ def self.from_csv file, opts={}
28
+ opts[:col_sep] ||= ','
29
+ opts[:headers] ||= true
30
+ opts[:converters] ||= :numeric
31
+ opts[:header_converters] ||= :symbol
32
+
33
+ csv = CSV.open file, 'r', opts
34
+
35
+ yield csv if block_given?
36
+
37
+ first = true
38
+ df = nil
39
+
40
+ csv.each do |row|
41
+ if first
42
+ df = Daru::DataFrame.new({}, csv.headers)
43
+ first = false
44
+ end
45
+
46
+ df.insert_row row
47
+ end
48
+
49
+ df
50
+ end
51
+
52
+ def column name
53
+ @vectors[name]
54
+ end
55
+
56
+ def delete name
57
+ @vectors.delete name
58
+ @fields.delete name
59
+ end
60
+
61
+ def [](name)
62
+ column name
63
+ end
64
+
65
+ def []=(name, vector)
66
+ insert_vector name, vector
67
+ end
68
+
69
+ def row index
70
+ raise Exception, "Expected index to be within bounds" if index > @size
71
+
72
+ row = []
73
+ self.each_column do |column|
74
+ row << column[index]
75
+ end
76
+
77
+ row
78
+ end
79
+
80
+ def has_vector? vector
81
+ !!@vectors[vector]
82
+ end
83
+
84
+ def each_row
85
+ 0.upto(@size) do |index|
86
+ yield row(index)
87
+ end
88
+ end
89
+
90
+ def each_row_with_index
91
+ 0.upto(@size) do |index|
92
+ yield row(index), index
93
+ end
94
+ end
95
+
96
+ def each_column
97
+ @vectors.values.each do |column|
98
+ yield column
99
+ end
100
+ end
101
+
102
+ def insert_vector name, vector
103
+ raise Exeception, "Expected vector size to be same as DataFrame\
104
+ size." if vector.size != self.size
105
+
106
+ @vectors.merge({name => vector})
107
+ @fields << name
108
+ end
109
+
110
+ def insert_row row
111
+ raise Exception, "Expected new row to same as the number of rows \
112
+ in the DataFrame" if row.size != @fields.size
113
+
114
+ @fields.each_with_index do |field, index|
115
+ @vectors[field] << row[index]
116
+ end
117
+ end
118
+
119
+ def to_html(threshold=15)
120
+ html = '<table>'
121
+
122
+ self.each_row_with_index do |row, index|
123
+ break if index > threshold and index <= @size
124
+ html += '<tr>'
125
+ row.each{ |val| html.concat('<td>' + val.to_s + '</td>') }
126
+ html += '</tr>'
127
+ if i == threshold
128
+ html += '<tr>'
129
+ row.size.times { html.concat('<td>...</td>') }
130
+ html += '</tr>'
131
+ end
132
+ end
133
+
134
+ html += '</table>'
135
+ end
136
+
137
+ def to_s
138
+ to_html
139
+ end
140
+
141
+ def method_missing(name, *args)
142
+ if md = name.match(/(.+)\=/)
143
+ insert_vector name[/(.+)\=/].delete("="), args[0]
144
+ elsif self.has_vector? name
145
+ column name
146
+ else
147
+ super(name, *args)
148
+ end
149
+ end
150
+
151
+ private
152
+ def check_length
153
+ size = nil
154
+
155
+ @vectors.each_value do |vector|
156
+ if size.nil?
157
+ size = vector.size
158
+ elsif size != vector.size
159
+ raise Exception, "Expected all vectors to be of the same size. Vector \
160
+ #{vector.name} is of size #{vector.size} and another one of size #{size}"
161
+ end
162
+ end
163
+
164
+ @size = size
165
+ end
166
+
167
+ def set_fields_order
168
+ @fields = @vectors.keys & @fields
169
+ @fields += @vecorts.keys.sort - @fields
170
+ end
171
+
172
+ # Writes names specified in the hash to the actual name of the vector.
173
+ # Will over-ride any previous name assigned to the vector.
174
+ def set_vector_names
175
+ @fields.each do |name|
176
+ @vectors[name].name = name
177
+ end
5
178
  end
6
179
  end
7
180
  end
data/lib/daru/vector.rb CHANGED
@@ -1,3 +1,43 @@
1
+ class Array
2
+ def daru_vector
3
+ Daru::Vector.new self
4
+ end
5
+
6
+ alias_method :dv, :daru_vector
7
+ end
8
+
9
+ class Range
10
+ def daru_vector
11
+ Daru::Vector.new self
12
+ end
13
+
14
+ alias_method :dv, :daru_vector
15
+ end
16
+
17
+ class Hash
18
+ def daru_vector
19
+ Daru::Vector.new self.values[0], self.keys[0]
20
+ end
21
+
22
+ alias_method :dv, :daru_vector
23
+ end
24
+
25
+ class NMatrix
26
+ def daru_vector
27
+ Daru::Vector.new self
28
+ end
29
+
30
+ alias_method :dv, :daru_vector
31
+ end
32
+
33
+ class MDArray
34
+ def daru_vector
35
+ Daru::Vector.new self
36
+ end
37
+
38
+ alias_method :dv, :daru_vector
39
+ end
40
+
1
41
  module Daru
2
42
  class Vector
3
43
  include Enumerable
@@ -6,27 +46,78 @@ module Daru
6
46
  @vector.each(&block)
7
47
  end
8
48
 
9
- attr_reader :name
10
-
11
- attr_reader :size
49
+ attr_accessor :name
12
50
 
13
- def initialize source, name=SecureRandom.uuid
14
- @name = source.is_a?(Hash) ? source.keys[0] : name
51
+ attr_reader :size
15
52
 
16
- @vector =
17
- if source.is_a? Hash
18
- source.values[0].is_a?(Range) ? source.values[0].to_a : source.values[0]
19
- elsif source.is_a? Range or source.is_a? Matrix
20
- source.to_a.flatten
53
+ attr_reader :vector
54
+
55
+ def initialize source=[], name=nil
56
+ if source.is_a?(Hash)
57
+ initialize source.values[0], source.keys[0]
21
58
  else
22
- source
23
- end
59
+ @name = name || SecureRandom.uuid
60
+
61
+ @vector =
62
+ case source
63
+ when Range, Matrix
64
+ source.to_a.flatten
65
+ else
66
+ source
67
+ end
24
68
 
25
- @size = @vector.size
69
+ @size = @vector.size
70
+ end
26
71
  end
27
72
 
28
73
  def [](index)
29
74
  @vector[index]
30
75
  end
76
+
77
+ def []=(index, value)
78
+ @vector[index] = value
79
+ end
80
+
81
+ def ==(other)
82
+ other.vector == @vector and other.name == @name
83
+ end
84
+
85
+ def <<(element)
86
+ @vector << element
87
+ end
88
+
89
+ def to_json
90
+ self.to_a.to_json
91
+ end
92
+
93
+ def to_a
94
+ @vector.to_a
95
+ end
96
+
97
+ def to_nmatrix
98
+ @vector.to_nm
99
+ end
100
+
101
+ alias_method :to_nm, :to_nmatrix
102
+
103
+ def first lim=1
104
+ lim == 1 ? @vector.first : @vector.first(lim)
105
+ end
106
+
107
+ def to_html threshold=15
108
+ html = '<table><tr><th>' + @name.to_s + '</th></tr>>'
109
+
110
+ @vector.to_a.each_with_index do |el,i|
111
+ next if threshold < i and i < @arr.length-1
112
+ content = i == threshold ? '...' : el.to_s
113
+ html.concat('<tr><td>' + content + '</td></tr>')
114
+ end
115
+
116
+ html += '</table>'
117
+ end
118
+
119
+ def dup
120
+ Daru::Vector.new @vector.dup, @name.dup
121
+ end
31
122
  end
32
123
  end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Daru
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,100 @@
1
+ image_resolution true_transform mls
2
+ 6.55779 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,4262.65,0,0,0,1 0
3
+ 2.14746 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1278.86,0,0,0,1 0
4
+ 8.31104 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,5448.81,0,0,0,1 0
5
+ 3.47872 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,2179.52,0,0,0,1 0
6
+ 4.16725 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,2645.34,0,0,0,1 0
7
+ 5.79983 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,3749.86,0,0,0,1 0
8
+ 1.9058 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1115.36,0,0,0,1 0
9
+ 1.9058 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1115.36,0,0,0,1 0
10
+ 4.11806 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,2612.06,0,0,0,1 0
11
+ 6.26622 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,4065.39,0,0,0,1 0
12
+ 2.57805 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1570.18,0,0,0,1 0
13
+ 4.76151 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,3047.39,0,0,0,1 0
14
+ 7.11002 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,4636.26,0,0,0,1 0
15
+ 5.40811 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,3484.84,0,0,0,1 0
16
+ 8.19567 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,5370.75,0,0,0,1 0
17
+ 3.47872 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,2179.52,0,0,0,1 0
18
+ 2.91762 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1799.91,0,0,0,1 0
19
+ 4.76151 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,3047.39,0,0,0,1 0
20
+ 8.19567 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,5370.75,0,0,0,1 0
21
+ 2.91762 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1799.91,0,0,0,1 0
22
+ 6.26622 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,4065.39,0,0,0,1 0
23
+ 4.16725 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,2645.34,0,0,0,1 0
24
+ 3.78516 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,2386.84,0,0,0,1 0
25
+ 3.78516 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,2386.84,0,0,0,1 0
26
+ 7.47603 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,4883.89,0,0,0,1 0
27
+ 5.79983 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,3749.86,0,0,0,1 0
28
+ 2.57805 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1570.18,0,0,0,1 0
29
+ 8.31104 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,5448.81,0,0,0,1 0
30
+ 1.77097 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1024.14,0,0,0,1 0
31
+ 5.9596 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,3857.95,0,0,0,1 0
32
+ 5.40811 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,3484.84,0,0,0,1 0
33
+ 4.16725 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,2645.34,0,0,0,1 0
34
+ 7.11002 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,4636.26,0,0,0,1 0
35
+ 7.47603 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,4883.89,0,0,0,1 0
36
+ 13.6304 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,9047.63,0,0,0,1 0
37
+ 5.9596 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,3857.95,0,0,0,1 0
38
+ 4.76151 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,3047.39,0,0,0,1 0
39
+ 5.40811 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,3484.84,0,0,0,1 0
40
+ 12.4066 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,8219.63,0,0,0,1 0
41
+ 5.79983 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,3749.86,0,0,0,1 0
42
+ 7.11002 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,4636.26,0,0,0,1 0
43
+ 7.11002 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,4636.26,0,0,0,1 0
44
+ 2.58728 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1576.42,0,0,0,1 0
45
+ 12.4066 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,8219.63,0,0,0,1 0
46
+ 3.78516 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,2386.84,0,0,0,1 0
47
+ 2.58728 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1576.42,0,0,0,1 0
48
+ 5.03378 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,3231.59,0,0,0,1 0
49
+ 5.40811 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,3484.84,0,0,0,1 0
50
+ 6.26622 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,4065.39,0,0,0,1 0
51
+ 2.58728 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1576.42,0,0,0,1 0
52
+ 2.91762 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1799.91,0,0,0,1 0
53
+ 5.9596 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,3857.95,0,0,0,1 0
54
+ 1.77097 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1024.14,0,0,0,1 0
55
+ 7.47603 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,4883.89,0,0,0,1 0
56
+ 5.03378 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,3231.59,0,0,0,1 0
57
+ 1.81745 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1055.59,0,0,0,1 0
58
+ 1.77097 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1024.14,0,0,0,1 0
59
+ 2.14746 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1278.86,0,0,0,1 0
60
+ 8.19567 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,5370.75,0,0,0,1 0
61
+ 2.91762 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1799.91,0,0,0,1 0
62
+ 1.81745 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1055.59,0,0,0,1 0
63
+ 8.31104 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,5448.81,0,0,0,1 0
64
+ 1.81745 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1055.59,0,0,0,1 0
65
+ 1.77097 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1024.14,0,0,0,1 0
66
+ 2.57805 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1570.18,0,0,0,1 0
67
+ 5.03378 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,3231.59,0,0,0,1 0
68
+ 6.55779 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,4262.65,0,0,0,1 0
69
+ 6.55779 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,4262.65,0,0,0,1 0
70
+ 3.47872 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,2179.52,0,0,0,1 0
71
+ 4.11806 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,2612.06,0,0,0,1 0
72
+ 6.55779 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,4262.65,0,0,0,1 0
73
+ 8.31104 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,5448.81,0,0,0,1 0
74
+ 2.14746 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1278.86,0,0,0,1 0
75
+ 2.32632 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1399.86,0,0,0,1 0
76
+ 1.9058 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1115.36,0,0,0,1 0
77
+ 6.26622 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,4065.39,0,0,0,1 0
78
+ 2.32632 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1399.86,0,0,0,1 0
79
+ 7.47603 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,4883.89,0,0,0,1 0
80
+ 4.11806 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,2612.06,0,0,0,1 0
81
+ 13.6304 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,9047.63,0,0,0,1 0
82
+ 2.14746 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1278.86,0,0,0,1 0
83
+ 1.9058 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,1115.36,0,0,0,1 0
84
+ 5.03378 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,3231.59,0,0,0,1 0
85
+ 5.9596 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,3857.95,0,0,0,1 0
86
+ 4.76151 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,3047.39,0,0,0,1 0
87
+ 1.81745 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,1055.59,0,0,0,1 0
88
+ 3.47872 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,2179.52,0,0,0,1 0
89
+ 2.32632 -0.2362347,0.6308649,0.7390552,0,0.6523478,-0.4607318,0.6018043,0,0.7201635,0.6242881,-0.3027024,1399.86,0,0,0,1 0
90
+ 12.4066 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,8219.63,0,0,0,1 0
91
+ 12.4066 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,8219.63,0,0,0,1 0
92
+ 2.32632 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1399.86,0,0,0,1 0
93
+ 8.19567 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,5370.75,0,0,0,1 0
94
+ 4.11806 0.3832846,0.8818775,-0.2745634,0,0.8566163,-0.4505714,-0.2513841,0,-0.3454004,-0.1388438,-0.9281277,2612.06,0,0,0,1 0
95
+ 2.58728 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1576.42,0,0,0,1 0
96
+ 5.79983 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,3749.86,0,0,0,1 0
97
+ 2.57805 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,1570.18,0,0,0,1 0
98
+ 13.6304 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,9047.63,0,0,0,1 0
99
+ 3.78516 -0.1539447,-0.2832125,-0.9466212,0,-0.1585857,0.9527035,-0.2592422,0,0.9752699,0.1102116,-0.1915772,2386.84,0,0,0,1 0
100
+ 4.16725 -0.895577,-0.4178617,0.152753,0,-0.1004795,-0.1445008,-0.9843898,0,0.4334117,-0.8969454,0.08742505,2645.34,0,0,0,1 0
@@ -1,19 +1,20 @@
1
+ require 'spec_helper.rb'
1
2
  # Tests if interpreter is JRuby
2
3
 
3
- # describe Daru::Vector do
4
- # context ".initialize" do
5
- # it "creates a vector object with an MDArray" do
6
- # vector = Daru::Vector.new(MDArray.new([5], [1,2,3,4,5]), :uhura)
4
+ describe Daru::Vector do
5
+ context ".initialize" do
6
+ it "creates a vector object with an MDArray" do
7
+ vector = Daru::Vector.new(MDArray.double([5], [1,2,3,4,5]), :uhura)
7
8
 
8
- # expect(vector[1]) .to eq(2)
9
- # expect(vector.name).to eq(:uhura)
10
- # end
9
+ expect(vector[1]) .to eq(2)
10
+ expect(vector.name).to eq(:uhura)
11
+ end
11
12
 
12
- # it "creates a vector object with a Hash with different values" do
13
- # vector = Daru::Vector.new { sulu: MDArray.new([5], [1,2,3,4,5])}
13
+ it "creates a vector object with a Hash with different values" do
14
+ vector = Daru::Vector.new({ sulu: MDArray.double([5], [1,2,3,4,5])})
14
15
 
15
- # expect(vector[1]) .to eq(2)
16
- # expect(vector.name).to eq(:sulu)
17
- # end
18
- # end
19
- # end
16
+ expect(vector[1]) .to eq(2)
17
+ expect(vector.name).to eq(:sulu)
18
+ end
19
+ end
20
+ end if RUBY_ENGINE == 'jruby'
@@ -1,5 +1,103 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
3
  describe Daru::DataFrame do
4
-
5
- end
4
+ context "DataFrame from normal array vectors" do
5
+
6
+ before :each do
7
+ @df = Daru::DataFrame.new({a: Daru::Vector.new(1..3),
8
+ b: (50..52).daru_vector, b_bad: ['Jesse', 'Walter', 'Hank'].daru_vector})
9
+
10
+ @vector = Daru::Vector.new 1..3, :a
11
+ end
12
+
13
+ it "checks for the size of DataFrame" do
14
+ expect(@df.size).to eq(3)
15
+ end
16
+
17
+ it "raises exception for uneven vectors" do
18
+ expect do
19
+ df = Daru::DataFrame.new({a: (1..5).dv, b: [1,2,3].daru_vector})
20
+ end.to raise_error
21
+ end
22
+
23
+ it "returns vector by specifying as method" do
24
+ expect(@df.a).to eq(@vector)
25
+ end
26
+
27
+ it "returns vector by specifying as index" do
28
+ expect(@df[:a]).to eq(@vector)
29
+ end
30
+
31
+ it "returns vector by specifying as a column argument" do
32
+ expect(@df.column(:a)).to eq(@vector)
33
+ end
34
+
35
+ it "returns a row" do
36
+ r = @df.row 0
37
+
38
+ expect(r).to eq([1,50,'Jesse'])
39
+ end
40
+
41
+ it "iterates over columns" do
42
+ @df.each_column do |col|
43
+ expect(col.is_a?(Daru::Vector)).to be(true)
44
+ expect([:a, :b, :b_bad].include? col.name).to be(true)
45
+ end
46
+ end
47
+
48
+ it "iterates over rows" do
49
+ @df.each_row do |row|
50
+ expect(row.size).to be(@df.fields.size)
51
+ end
52
+ end
53
+
54
+ it "shows column fields" do
55
+ expect(@df.fields).to eq([:a, :b, :b_bad])
56
+ end
57
+
58
+ it "inserts a new vector" do
59
+ @df.insert_vector :c, Daru::Vector.new([3,6,9])
60
+
61
+ expect(@df.fields.include?(:c)).to be(true)
62
+ end
63
+
64
+ it "inserts a new row" do
65
+ @df.insert_row [6,6,"Fred"]
66
+
67
+ expect(@df.a.vector) .to eq([1,2,3,6])
68
+ expect(@df.b.vector) .to eq([50,51,52,6])
69
+ expect(@df.b_bad.vector).to eq(['Jesse', 'Walter', 'Hank', 'Fred'])
70
+ end
71
+
72
+ it "raises an error for inappropriate row insertion" do
73
+ expect { @df.insert_row [1,1] }.to raise_error
74
+ end
75
+
76
+ it "deletes a vector" do
77
+ @df.delete :a
78
+
79
+ expect(@df.fields.include? :a).to be(false)
80
+ end
81
+ end
82
+
83
+ context "DataFrame loads from files" do
84
+
85
+ it "loads a DataFrame from CSV" do
86
+ df = Daru::DataFrame.from_csv('spec/fixtures/matrix_test.csv',
87
+ {col_sep: ' ', headers: true}) do |csv|
88
+ csv.convert do |field, info|
89
+ case info[:header]
90
+ when :true_transform
91
+ field.split(',').map { |s| s.to_f }
92
+ else
93
+ field
94
+ end
95
+ end
96
+ end
97
+
98
+ expect(df.fields).to eq([:image_resolution, :true_transform, :mls])
99
+ expect(df[:image_resolution].first).to eq(6.55779)
100
+ expect(df.column(:true_transform).first[15]).to eq(1.0)
101
+ end
102
+ end
103
+ end if RUBY_ENGINE == 'ruby'
@@ -57,23 +57,48 @@ describe Daru::Vector do
57
57
  end
58
58
  end
59
59
 
60
- context "#each" do
61
- it "executes the given block" do
62
- sum = 0
63
- vector = Daru::Vector.new 1..5, :mickey
60
+ context "tests for methods" do # TODO: Name this better
61
+ before do
62
+ @anakin = Daru::Vector.new NMatrix.new([5], [1,2,3,4,5]), :anakin
63
+ @luke = Daru::Vector.new NMatrix.new([3], [3,4,5,6]) , :luke
64
+ end
64
65
 
65
- vector.each{ |e| sum += e}
66
+ it "checks for an each block" do
67
+ sum = 0
66
68
 
69
+ @anakin.each{ |e| sum += e}
67
70
  expect(sum).to eq(15)
68
71
  end
69
- end
70
72
 
71
- context "#==" do
72
- it "checks for equality of vectors" do
73
- anakin = Daru::Vector.new 1..5, :anakin
74
- luke = Daru::Vector.new 3..6, :luke
73
+ it "checks for inequality of vectors" do
74
+ expect(@anakin == @luke).to be(false)
75
+ end
76
+
77
+ it "calculates maximum value" do
78
+ expect(@anakin.max).to eq(5)
79
+ end
80
+
81
+ it "calculates minimmum value" do
82
+ expect(@anakin.min).to eq(1)
83
+ end
84
+
85
+ it "delegates to the internal array storage" do
86
+ expect(@anakin.size).to eq(@anakin.to_a.size)
87
+ end
88
+
89
+ it "assigns on index" do
90
+ @anakin[0] = 666
91
+ expect(@anakin[0]).to eq(666)
92
+ end
93
+
94
+ it "returns only the vector object" do
95
+ expect(@anakin.vector == NMatrix.new([5], [1,2,3,4,5])).to be(true)
96
+ end
97
+
98
+ it "tests for equality" do
99
+ clone = Daru::Vector.new NMatrix.new([5], [1,2,3,4,5]), :clone
75
100
 
76
- expect(anakin == luke).to be(false)
101
+ expect(@anakin == clone).to be(false)
77
102
  end
78
103
  end
79
- end
104
+ end if RUBY_ENGINE == 'ruby'
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  require 'rspec'
2
- require 'nmatrix'
2
+
3
+ if RUBY_ENGINE == 'jruby'
4
+ require 'mdarray'
5
+ else
6
+ require 'nmatrix'
7
+ end
3
8
 
4
9
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
10
  $LOAD_PATH.unshift(File.dirname(__FILE__))
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daru
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sameer Deshmukh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-06 00:00:00.000000000 Z
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '3.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '3.0'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: nmatrix
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,9 +52,9 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.1.0.rc5
55
- description: "Daru (Data Analysis in RUby) is a library for storage, analysis and
56
- manipulation\nof data. It aims to be the preferred data storage library for all
57
- Ruby gems, promoting \ninteroperability and easy of use between various ruby packages.\n"
55
+ description: |
56
+ Daru (Data Analysis in RUby) is a library for storage, analysis and manipulation
57
+ of data.
58
58
  email:
59
59
  - sameer.deshmukh93@gmail.com
60
60
  executables: []
@@ -71,6 +71,7 @@ files:
71
71
  - lib/daru/dataframe.rb
72
72
  - lib/daru/vector.rb
73
73
  - lib/version.rb
74
+ - spec/fixtures/matrix_test.csv
74
75
  - spec/jruby/dataframe_spec.rb
75
76
  - spec/jruby/vector_spec.rb
76
77
  - spec/mri/dataframe_spec.rb
@@ -101,6 +102,7 @@ signing_key:
101
102
  specification_version: 4
102
103
  summary: Data Analysis in RUby
103
104
  test_files:
105
+ - spec/fixtures/matrix_test.csv
104
106
  - spec/jruby/dataframe_spec.rb
105
107
  - spec/jruby/vector_spec.rb
106
108
  - spec/mri/dataframe_spec.rb