daru 0.0.5 → 0.1.0

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.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.build.sh +14 -0
  3. data/.travis.yml +26 -4
  4. data/CONTRIBUTING.md +31 -0
  5. data/Gemfile +1 -2
  6. data/{History.txt → History.md} +110 -44
  7. data/README.md +21 -288
  8. data/Rakefile +1 -0
  9. data/daru.gemspec +12 -8
  10. data/lib/daru.rb +36 -1
  11. data/lib/daru/accessors/array_wrapper.rb +8 -3
  12. data/lib/daru/accessors/gsl_wrapper.rb +113 -0
  13. data/lib/daru/accessors/nmatrix_wrapper.rb +6 -17
  14. data/lib/daru/core/group_by.rb +0 -1
  15. data/lib/daru/dataframe.rb +1192 -83
  16. data/lib/daru/extensions/rserve.rb +21 -0
  17. data/lib/daru/index.rb +14 -0
  18. data/lib/daru/io/io.rb +170 -8
  19. data/lib/daru/maths/arithmetic/dataframe.rb +4 -3
  20. data/lib/daru/maths/arithmetic/vector.rb +4 -4
  21. data/lib/daru/maths/statistics/dataframe.rb +48 -27
  22. data/lib/daru/maths/statistics/vector.rb +215 -33
  23. data/lib/daru/monkeys.rb +53 -7
  24. data/lib/daru/multi_index.rb +21 -4
  25. data/lib/daru/plotting/dataframe.rb +83 -25
  26. data/lib/daru/plotting/vector.rb +9 -10
  27. data/lib/daru/vector.rb +596 -61
  28. data/lib/daru/version.rb +3 -0
  29. data/spec/accessors/wrappers_spec.rb +51 -0
  30. data/spec/core/group_by_spec.rb +0 -2
  31. data/spec/daru_spec.rb +58 -0
  32. data/spec/dataframe_spec.rb +768 -73
  33. data/spec/extensions/rserve_spec.rb +52 -0
  34. data/spec/fixtures/bank2.dat +200 -0
  35. data/spec/fixtures/repeated_fields.csv +7 -0
  36. data/spec/fixtures/scientific_notation.csv +4 -0
  37. data/spec/fixtures/test_xls.xls +0 -0
  38. data/spec/io/io_spec.rb +161 -24
  39. data/spec/math/arithmetic/dataframe_spec.rb +26 -7
  40. data/spec/math/arithmetic/vector_spec.rb +8 -0
  41. data/spec/math/statistics/dataframe_spec.rb +16 -1
  42. data/spec/math/statistics/vector_spec.rb +215 -47
  43. data/spec/spec_helper.rb +21 -2
  44. data/spec/vector_spec.rb +368 -12
  45. metadata +99 -16
  46. data/lib/version.rb +0 -3
  47. data/notebooks/grouping_splitting_pivots.ipynb +0 -529
  48. data/notebooks/intro_with_music_data_.ipynb +0 -303
@@ -0,0 +1,52 @@
1
+ begin
2
+ require "rserve"
3
+ require 'daru/extensions/rserve'
4
+
5
+ describe "Daru rserve extension" do
6
+ before do
7
+ @r = Rserve::Connection.new
8
+ end
9
+
10
+ after do
11
+ @r.close
12
+ end
13
+
14
+ describe Daru::Vector do
15
+ context "#to_REXP" do
16
+ it "converts to and from R data" do
17
+ a = Daru::Vector.new(100.times.map { |i| rand > 0.9 ? nil : i + rand })
18
+ rexp = a.to_REXP
19
+ expect(rexp.is_a?(Rserve::REXP::Double)).to eq(true)
20
+ expect(rexp.to_ruby).to eq(a.to_a)
21
+
22
+ @r.assign 'a', rexp
23
+ expect(@r.eval('a').to_ruby).to eq(a.to_a)
24
+ end
25
+ end
26
+ end
27
+
28
+ describe Daru::DataFrame do
29
+ context "#to_REXP" do
30
+ it "converts to and from R data" do
31
+ a = Daru::Vector.new(100.times.map { |i| rand > 0.9 ? nil : i + rand })
32
+ b = Daru::Vector.new(100.times.map { |i| rand > 0.9 ? nil : i + rand })
33
+ c = Daru::Vector.new(100.times.map { |i| rand > 0.9 ? nil : i + rand })
34
+ ds = Daru::DataFrame.new({ :a => a, :b => b, :c => c })
35
+ rexp = ds.to_REXP
36
+ expect(rexp.is_a? Rserve::REXP::GenericVector).to eq(true)
37
+
38
+ ret = rexp.to_ruby
39
+ expect(ret['a']).to eq(a.to_a)
40
+ @r.assign 'df', rexp
41
+ out_df = @r.eval('df').to_ruby
42
+
43
+ expect(out_df.attributes['class']).to eq('data.frame')
44
+ expect(out_df.attributes['names']).to eq(%w(a b c))
45
+ expect(out_df['a']).to eq(a.to_a)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ rescue LoadError => e
51
+ puts "Requires rserve extension"
52
+ end
@@ -0,0 +1,200 @@
1
+ 214.80000 131.00000 131.10000 9.00000 9.70000 141.00000
2
+ 214.60000 129.70000 129.70000 8.10000 9.50000 141.70000
3
+ 214.80000 129.70000 129.70000 8.70000 9.60000 142.20000
4
+ 214.80000 129.70000 129.60000 7.50000 10.40000 142.00000
5
+ 215.00000 129.60000 129.70000 10.40000 7.70000 141.80000
6
+ 215.70000 130.80000 130.50000 9.00000 10.10000 141.40000
7
+ 215.50000 129.50000 129.70000 7.90000 9.60000 141.60000
8
+ 214.50000 129.60000 129.20000 7.20000 10.70000 141.70000
9
+ 214.90000 129.40000 129.70000 8.20000 11.00000 141.90000
10
+ 215.20000 130.40000 130.30000 9.20000 10.00000 140.70000
11
+ 215.30000 130.40000 130.30000 7.90000 11.70000 141.80000
12
+ 215.10000 129.50000 129.60000 7.70000 10.50000 142.20000
13
+ 215.20000 130.80000 129.60000 7.90000 10.80000 141.40000
14
+ 214.70000 129.70000 129.70000 7.70000 10.90000 141.70000
15
+ 215.10000 129.90000 129.70000 7.70000 10.80000 141.80000
16
+ 214.50000 129.80000 129.80000 9.30000 8.50000 141.60000
17
+ 214.60000 129.90000 130.10000 8.20000 9.80000 141.70000
18
+ 215.00000 129.90000 129.70000 9.00000 9.00000 141.90000
19
+ 215.20000 129.60000 129.60000 7.40000 11.50000 141.50000
20
+ 214.70000 130.20000 129.90000 8.60000 10.00000 141.90000
21
+ 215.00000 129.90000 129.30000 8.40000 10.00000 141.40000
22
+ 215.60000 130.50000 130.00000 8.10000 10.30000 141.60000
23
+ 215.30000 130.60000 130.00000 8.40000 10.80000 141.50000
24
+ 215.70000 130.20000 130.00000 8.70000 10.00000 141.60000
25
+ 215.10000 129.70000 129.90000 7.40000 10.80000 141.10000
26
+ 215.30000 130.40000 130.40000 8.00000 11.00000 142.30000
27
+ 215.50000 130.20000 130.10000 8.90000 9.80000 142.40000
28
+ 215.10000 130.30000 130.30000 9.80000 9.50000 141.90000
29
+ 215.10000 130.00000 130.00000 7.40000 10.50000 141.80000
30
+ 214.80000 129.70000 129.30000 8.30000 9.00000 142.00000
31
+ 215.20000 130.10000 129.80000 7.90000 10.70000 141.80000
32
+ 214.80000 129.70000 129.70000 8.60000 9.10000 142.30000
33
+ 215.00000 130.00000 129.60000 7.70000 10.50000 140.70000
34
+ 215.60000 130.40000 130.10000 8.40000 10.30000 141.00000
35
+ 215.90000 130.40000 130.00000 8.90000 10.60000 141.40000
36
+ 214.60000 130.20000 130.20000 9.40000 9.70000 141.80000
37
+ 215.50000 130.30000 130.00000 8.40000 9.70000 141.80000
38
+ 215.30000 129.90000 129.40000 7.90000 10.00000 142.00000
39
+ 215.30000 130.30000 130.10000 8.50000 9.30000 142.10000
40
+ 213.90000 130.30000 129.00000 8.10000 9.70000 141.30000
41
+ 214.40000 129.80000 129.20000 8.90000 9.40000 142.30000
42
+ 214.80000 130.10000 129.60000 8.80000 9.90000 140.90000
43
+ 214.90000 129.60000 129.40000 9.30000 9.00000 141.70000
44
+ 214.90000 130.40000 129.70000 9.00000 9.80000 140.90000
45
+ 214.80000 129.40000 129.10000 8.20000 10.20000 141.00000
46
+ 214.30000 129.50000 129.40000 8.30000 10.20000 141.80000
47
+ 214.80000 129.90000 129.70000 8.30000 10.20000 141.50000
48
+ 214.80000 129.90000 129.70000 7.30000 10.90000 142.00000
49
+ 214.60000 129.70000 129.80000 7.90000 10.30000 141.10000
50
+ 214.50000 129.00000 129.60000 7.80000 9.80000 142.00000
51
+ 214.60000 129.80000 129.40000 7.20000 10.00000 141.30000
52
+ 215.30000 130.60000 130.00000 9.50000 9.70000 141.10000
53
+ 214.50000 130.10000 130.00000 7.80000 10.90000 140.90000
54
+ 215.40000 130.20000 130.20000 7.60000 10.90000 141.60000
55
+ 214.50000 129.40000 129.50000 7.90000 10.00000 141.40000
56
+ 215.20000 129.70000 129.40000 9.20000 9.40000 142.00000
57
+ 215.70000 130.00000 129.40000 9.20000 10.40000 141.20000
58
+ 215.00000 129.60000 129.40000 8.80000 9.00000 141.10000
59
+ 215.10000 130.10000 129.90000 7.90000 11.00000 141.30000
60
+ 215.10000 130.00000 129.80000 8.20000 10.30000 141.40000
61
+ 215.10000 129.60000 129.30000 8.30000 9.90000 141.60000
62
+ 215.30000 129.70000 129.40000 7.50000 10.50000 141.50000
63
+ 215.40000 129.80000 129.40000 8.00000 10.60000 141.50000
64
+ 214.50000 130.00000 129.50000 8.00000 10.80000 141.40000
65
+ 215.00000 130.00000 129.80000 8.60000 10.60000 141.50000
66
+ 215.20000 130.60000 130.00000 8.80000 10.60000 140.80000
67
+ 214.60000 129.50000 129.20000 7.70000 10.30000 141.30000
68
+ 214.80000 129.70000 129.30000 9.10000 9.50000 141.50000
69
+ 215.10000 129.60000 129.80000 8.60000 9.80000 141.80000
70
+ 214.90000 130.20000 130.20000 8.00000 11.20000 139.60000
71
+ 213.80000 129.80000 129.50000 8.40000 11.10000 140.90000
72
+ 215.20000 129.90000 129.50000 8.20000 10.30000 141.40000
73
+ 215.00000 129.60000 130.20000 8.70000 10.00000 141.20000
74
+ 214.40000 129.90000 129.60000 7.50000 10.50000 141.80000
75
+ 215.20000 129.90000 129.70000 7.20000 10.60000 142.10000
76
+ 214.10000 129.60000 129.30000 7.60000 10.70000 141.70000
77
+ 214.90000 129.90000 130.10000 8.80000 10.00000 141.20000
78
+ 214.60000 129.80000 129.40000 7.40000 10.60000 141.00000
79
+ 215.20000 130.50000 129.80000 7.90000 10.90000 140.90000
80
+ 214.60000 129.90000 129.40000 7.90000 10.00000 141.80000
81
+ 215.10000 129.70000 129.70000 8.60000 10.30000 140.60000
82
+ 214.90000 129.80000 129.60000 7.50000 10.30000 141.00000
83
+ 215.20000 129.70000 129.10000 9.00000 9.70000 141.90000
84
+ 215.20000 130.10000 129.90000 7.90000 10.80000 141.30000
85
+ 215.40000 130.70000 130.20000 9.00000 11.10000 141.20000
86
+ 215.10000 129.90000 129.60000 8.90000 10.20000 141.50000
87
+ 215.20000 129.90000 129.70000 8.70000 9.50000 141.60000
88
+ 215.00000 129.60000 129.20000 8.40000 10.20000 142.10000
89
+ 214.90000 130.30000 129.90000 7.40000 11.20000 141.50000
90
+ 215.00000 129.90000 129.70000 8.00000 10.50000 142.00000
91
+ 214.70000 129.70000 129.30000 8.60000 9.60000 141.60000
92
+ 215.40000 130.00000 129.90000 8.50000 9.70000 141.40000
93
+ 214.90000 129.40000 129.50000 8.20000 9.90000 141.50000
94
+ 214.50000 129.50000 129.30000 7.40000 10.70000 141.50000
95
+ 214.70000 129.60000 129.50000 8.30000 10.00000 142.00000
96
+ 215.60000 129.90000 129.90000 9.00000 9.50000 141.70000
97
+ 215.00000 130.40000 130.30000 9.10000 10.20000 141.10000
98
+ 214.40000 129.70000 129.50000 8.00000 10.30000 141.20000
99
+ 215.10000 130.00000 129.80000 9.10000 10.20000 141.50000
100
+ 214.70000 130.00000 129.40000 7.80000 10.00000 141.20000
101
+ 214.40000 130.10000 130.30000 9.70000 11.70000 139.80000
102
+ 214.90000 130.50000 130.20000 11.00000 11.50000 139.50000
103
+ 214.90000 130.30000 130.10000 8.70000 11.70000 140.20000
104
+ 215.00000 130.40000 130.60000 9.90000 10.90000 140.30000
105
+ 214.70000 130.20000 130.30000 11.80000 10.90000 139.70000
106
+ 215.00000 130.20000 130.20000 10.60000 10.70000 139.90000
107
+ 215.30000 130.30000 130.10000 9.30000 12.10000 140.20000
108
+ 214.80000 130.10000 130.40000 9.80000 11.50000 139.90000
109
+ 215.00000 130.20000 129.90000 10.00000 11.90000 139.40000
110
+ 215.20000 130.60000 130.80000 10.40000 11.20000 140.30000
111
+ 215.20000 130.40000 130.30000 8.00000 11.50000 139.20000
112
+ 215.10000 130.50000 130.30000 10.60000 11.50000 140.10000
113
+ 215.40000 130.70000 131.10000 9.70000 11.80000 140.60000
114
+ 214.90000 130.40000 129.90000 11.40000 11.00000 139.90000
115
+ 215.10000 130.30000 130.00000 10.60000 10.80000 139.70000
116
+ 215.50000 130.40000 130.00000 8.20000 11.20000 139.20000
117
+ 214.70000 130.60000 130.10000 11.80000 10.50000 139.80000
118
+ 214.70000 130.40000 130.10000 12.10000 10.40000 139.90000
119
+ 214.80000 130.50000 130.20000 11.00000 11.00000 140.00000
120
+ 214.40000 130.20000 129.90000 10.10000 12.00000 139.20000
121
+ 214.80000 130.30000 130.40000 10.10000 12.10000 139.60000
122
+ 215.10000 130.60000 130.30000 12.30000 10.20000 139.60000
123
+ 215.30000 130.80000 131.10000 11.60000 10.60000 140.20000
124
+ 215.10000 130.70000 130.40000 10.50000 11.20000 139.70000
125
+ 214.70000 130.50000 130.50000 9.90000 10.30000 140.10000
126
+ 214.90000 130.00000 130.30000 10.20000 11.40000 139.60000
127
+ 215.00000 130.40000 130.40000 9.40000 11.60000 140.20000
128
+ 215.50000 130.70000 130.30000 10.20000 11.80000 140.00000
129
+ 215.10000 130.20000 130.20000 10.10000 11.30000 140.30000
130
+ 214.50000 130.20000 130.60000 9.80000 12.10000 139.90000
131
+ 214.30000 130.20000 130.00000 10.70000 10.50000 139.80000
132
+ 214.50000 130.20000 129.80000 12.30000 11.20000 139.20000
133
+ 214.90000 130.50000 130.20000 10.60000 11.50000 139.90000
134
+ 214.60000 130.20000 130.40000 10.50000 11.80000 139.70000
135
+ 214.20000 130.00000 130.20000 11.00000 11.20000 139.50000
136
+ 214.80000 130.10000 130.10000 11.90000 11.10000 139.50000
137
+ 214.60000 129.80000 130.20000 10.70000 11.10000 139.40000
138
+ 214.90000 130.70000 130.30000 9.30000 11.20000 138.30000
139
+ 214.60000 130.40000 130.40000 11.30000 10.80000 139.80000
140
+ 214.50000 130.50000 130.20000 11.80000 10.20000 139.60000
141
+ 214.80000 130.20000 130.30000 10.00000 11.90000 139.30000
142
+ 214.70000 130.00000 129.40000 10.20000 11.00000 139.20000
143
+ 214.60000 130.20000 130.40000 11.20000 10.70000 139.90000
144
+ 215.00000 130.50000 130.40000 10.60000 11.10000 139.90000
145
+ 214.50000 129.80000 129.80000 11.40000 10.00000 139.30000
146
+ 214.90000 130.60000 130.40000 11.90000 10.50000 139.80000
147
+ 215.00000 130.50000 130.40000 11.40000 10.70000 139.90000
148
+ 215.30000 130.60000 130.30000 9.30000 11.30000 138.10000
149
+ 214.70000 130.20000 130.10000 10.70000 11.00000 139.40000
150
+ 214.90000 129.90000 130.00000 9.90000 12.30000 139.40000
151
+ 214.90000 130.30000 129.90000 11.90000 10.60000 139.80000
152
+ 214.60000 129.90000 129.70000 11.90000 10.10000 139.00000
153
+ 214.60000 129.70000 129.30000 10.40000 11.00000 139.30000
154
+ 214.50000 130.10000 130.10000 12.10000 10.30000 139.40000
155
+ 214.50000 130.30000 130.00000 11.00000 11.50000 139.50000
156
+ 215.10000 130.00000 130.30000 11.60000 10.50000 139.70000
157
+ 214.20000 129.70000 129.60000 10.30000 11.40000 139.50000
158
+ 214.40000 130.10000 130.00000 11.30000 10.70000 139.20000
159
+ 214.80000 130.40000 130.60000 12.50000 10.00000 139.30000
160
+ 214.60000 130.60000 130.10000 8.10000 12.10000 137.90000
161
+ 215.60000 130.10000 129.70000 7.40000 12.20000 138.40000
162
+ 214.90000 130.50000 130.10000 9.90000 10.20000 138.10000
163
+ 214.60000 130.10000 130.00000 11.50000 10.60000 139.50000
164
+ 214.70000 130.10000 130.20000 11.60000 10.90000 139.10000
165
+ 214.30000 130.30000 130.00000 11.40000 10.50000 139.80000
166
+ 215.10000 130.30000 130.60000 10.30000 12.00000 139.70000
167
+ 216.30000 130.70000 130.40000 10.00000 10.10000 138.80000
168
+ 215.60000 130.40000 130.10000 9.60000 11.20000 138.60000
169
+ 214.80000 129.90000 129.80000 9.60000 12.00000 139.60000
170
+ 214.90000 130.00000 129.90000 11.40000 10.90000 139.70000
171
+ 213.90000 130.70000 130.50000 8.70000 11.50000 137.80000
172
+ 214.20000 130.60000 130.40000 12.00000 10.20000 139.60000
173
+ 214.80000 130.50000 130.30000 11.80000 10.50000 139.40000
174
+ 214.80000 129.60000 130.00000 10.40000 11.60000 139.20000
175
+ 214.80000 130.10000 130.00000 11.40000 10.50000 139.60000
176
+ 214.90000 130.40000 130.20000 11.90000 10.70000 139.00000
177
+ 214.30000 130.10000 130.10000 11.60000 10.50000 139.70000
178
+ 214.50000 130.40000 130.00000 9.90000 12.00000 139.60000
179
+ 214.80000 130.50000 130.30000 10.20000 12.10000 139.10000
180
+ 214.50000 130.20000 130.40000 8.20000 11.80000 137.80000
181
+ 215.00000 130.40000 130.10000 11.40000 10.70000 139.10000
182
+ 214.80000 130.60000 130.60000 8.00000 11.40000 138.70000
183
+ 215.00000 130.50000 130.10000 11.00000 11.40000 139.30000
184
+ 214.60000 130.50000 130.40000 10.10000 11.40000 139.30000
185
+ 214.70000 130.20000 130.10000 10.70000 11.10000 139.50000
186
+ 214.70000 130.40000 130.00000 11.50000 10.70000 139.40000
187
+ 214.50000 130.40000 130.00000 8.00000 12.20000 138.50000
188
+ 214.80000 130.00000 129.70000 11.40000 10.60000 139.20000
189
+ 214.80000 129.90000 130.20000 9.60000 11.90000 139.40000
190
+ 214.60000 130.30000 130.20000 12.70000 9.10000 139.20000
191
+ 215.10000 130.20000 129.80000 10.20000 12.00000 139.40000
192
+ 215.40000 130.50000 130.60000 8.80000 11.00000 138.60000
193
+ 214.70000 130.30000 130.20000 10.80000 11.10000 139.20000
194
+ 215.00000 130.50000 130.30000 9.60000 11.00000 138.50000
195
+ 214.90000 130.30000 130.50000 11.60000 10.60000 139.80000
196
+ 215.00000 130.40000 130.30000 9.90000 12.10000 139.60000
197
+ 215.10000 130.30000 129.90000 10.30000 11.50000 139.70000
198
+ 214.80000 130.30000 130.40000 10.60000 11.10000 140.00000
199
+ 214.70000 130.70000 130.80000 11.20000 11.20000 139.40000
200
+ 214.30000 129.90000 129.90000 10.20000 11.50000 139.60000
@@ -0,0 +1,7 @@
1
+ "id","name","age","city","a1","name","age"
2
+ 1,"Alex",20,"New York","a,b","a",3
3
+ 2,"Claude",23,"London","b,c","b",4
4
+ 3,"Peter",25,"London","a","c",5
5
+ 4,"Franz",27,"Paris",,"d",6
6
+ 5,"George","5,5","Tome","a,b,c","f",
7
+ 6,"Fernand",20,"London","c,b","f",8
@@ -0,0 +1,4 @@
1
+ x,y
2
+ 1,9.629587310436753e+127
3
+ 2,1.9341543147883677e+129
4
+ 3,3.88485279048245e+130
@@ -1,40 +1,177 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
- describe Daru::DataFrame do
4
- context ".from_csv" do
5
- it "loads from a CSV file" do
6
- df = Daru::DataFrame.from_csv('spec/fixtures/matrix_test.csv',
7
- col_sep: ' ', headers: true)
3
+ describe Daru::IO do
4
+ describe Daru::DataFrame do
5
+ context ".from_csv" do
6
+ it "loads from a CSV file" do
7
+ df = Daru::DataFrame.from_csv('spec/fixtures/matrix_test.csv',
8
+ col_sep: ' ', headers: true)
8
9
 
9
- expect(df.vectors).to eq([:image_resolution, :mls, :true_transform].to_index)
10
- expect(df.vector[:image_resolution].first).to eq(6.55779)
11
- expect(df.vector[:true_transform].first).to eq("-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")
10
+ expect(df.vectors).to eq([:image_resolution, :mls, :true_transform].to_index)
11
+ expect(df.vector[:image_resolution].first).to eq(6.55779)
12
+ expect(df.vector[:true_transform].first).to eq("-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")
13
+ end
14
+
15
+ it "works properly for repeated headers", focus: true do
16
+ df = Daru::DataFrame.from_csv('spec/fixtures/repeated_fields.csv')
17
+ expect(df.vectors.to_a).to eq([:a1, :age_1, :age_2, :city, :id, :name_1, :name_2])
18
+
19
+ age = Daru::Vector.new([3, 4, 5, 6, nil, 8])
20
+ expect(df[:age_2]).to eq(age)
21
+ end
22
+
23
+ it "accepts scientific notation as float" do
24
+ ds = Daru::DataFrame.from_csv('spec/fixtures/scientific_notation.csv')
25
+ expect(ds.vectors.to_a).to eq([:x, :y])
26
+ y = [9.629587310436753e+127, 1.9341543147883677e+129, 3.88485279048245e+130]
27
+ y.zip(ds[:y]).each do |y_expected, y_ds|
28
+ expect(y_ds).to be_within(0.001).of(y_expected)
29
+ end
30
+ end
31
+ end
32
+
33
+ context "#write_csv" do
34
+ it "writes DataFrame to a CSV file" do
35
+ df = Daru::DataFrame.new({
36
+ a: [1,2,3,4,5],
37
+ b: [11,22,33,44,55],
38
+ c: ['a', 'g', 4, 5,'addadf'],
39
+ d: [nil, 23, 4,'a','ff']})
40
+ t = Tempfile.new('data.csv')
41
+ df.write_csv t.path
42
+
43
+ expect(Daru::DataFrame.from_csv(t.path)).to eq(df)
44
+ end
45
+ end
46
+
47
+ context ".from_excel" do
48
+ before do
49
+ id = Daru::Vector.new([1, 2, 3, 4, 5, 6])
50
+ name = Daru::Vector.new(%w(Alex Claude Peter Franz George Fernand))
51
+ age = Daru::Vector.new( [20, 23, 25, nil, 5.5, nil])
52
+ city = Daru::Vector.new(['New York', 'London', 'London', 'Paris', 'Tome', nil])
53
+ a1 = Daru::Vector.new(['a,b', 'b,c', 'a', nil, 'a,b,c', nil])
54
+ @expected = Daru::DataFrame.new({
55
+ :id => id, :name => name, :age => age, :city => city, :a1 => a1
56
+ }, order: [:id, :name, :age, :city, :a1])
57
+ end
58
+
59
+ it "loads DataFrame from an Excel Spreadsheet" do
60
+ df = Daru::DataFrame.from_excel 'spec/fixtures/test_xls.xls'
61
+
62
+ expect(df.nrows).to eq(6)
63
+ expect(df.vectors.to_a).to eq([:id, :name, :age, :city, :a1])
64
+ expect(df[:age][5]).to eq(nil)
65
+ expect(@expected).to eq(df)
66
+ end
67
+ end
68
+
69
+ context "#write_excel" do
70
+ before do
71
+ a = Daru::Vector.new(100.times.map { rand(100) })
72
+ b = Daru::Vector.new((['b'] * 100))
73
+ @expected = Daru::DataFrame.new({ :b => b, :a => a })
74
+
75
+ tempfile = Tempfile.new('test_write.xls')
76
+
77
+ @expected.write_excel tempfile.path
78
+ @df = Daru::DataFrame.from_excel tempfile.path
79
+ end
80
+
81
+ it "correctly writes DataFrame to an Excel Spreadsheet" do
82
+ expect(@expected).to eq(@df)
83
+ end
84
+ end
85
+
86
+ context ".from_sql" do
87
+ it "loads data from an SQL database" do
88
+ # TODO: write these tests
89
+ end
90
+ end
91
+
92
+ context "#write_sql" do
93
+ it "writes the DataFrame to an SQL database" do
94
+ # TODO: write these tests
95
+ end
96
+ end
97
+
98
+ context ".from_plaintext" do
99
+ it "reads data from plain text files" do
100
+ df = Daru::DataFrame.from_plaintext 'spec/fixtures/bank2.dat', [:v1,:v2,:v3,:v4,:v5,:v6]
101
+
102
+ expect(df.vectors.to_a).to eq([:v1,:v2,:v3,:v4,:v5,:v6])
103
+ end
12
104
  end
13
- end
14
105
 
15
- context "JSON" do
16
- it "loads parsed JSON" do
17
- require 'json'
106
+ context "JSON" do
107
+ it "loads parsed JSON" do
108
+ require 'json'
18
109
 
19
- json = File.read 'spec/fixtures/countries.json'
20
- df = Daru::DataFrame.new JSON.parse(json)
110
+ json = File.read 'spec/fixtures/countries.json'
111
+ df = Daru::DataFrame.new JSON.parse(json)
21
112
 
22
- expect(df.vectors).to eq([
23
- :name, :nativeName, :tld, :cca2, :ccn3, :cca3, :currency, :callingCode,
24
- :capital, :altSpellings, :relevance, :region, :subregion, :language,
25
- :languageCodes, :translations, :latlng, :demonym, :borders, :area].to_index)
113
+ expect(df.vectors).to eq([
114
+ :name, :nativeName, :tld, :cca2, :ccn3, :cca3, :currency, :callingCode,
115
+ :capital, :altSpellings, :relevance, :region, :subregion, :language,
116
+ :languageCodes, :translations, :latlng, :demonym, :borders, :area].to_index)
26
117
 
27
- expect(df.row[0][:name]).to eq("Afghanistan")
118
+ expect(df.row[0][:name]).to eq("Afghanistan")
119
+ end
120
+ end
121
+
122
+ context "Marshalling" do
123
+ it "" do
124
+ vector = Daru::Vector.new (0..100).collect { |_n| rand(100) }
125
+ dataframe = Daru::Vector.new({a: vector, b: vector, c: vector})
126
+ expect(Marshal.load(Marshal.dump(dataframe))).to eq(dataframe)
127
+ end
128
+ end
129
+
130
+ context "#save" do
131
+ before do
132
+ @data_frame = Daru::DataFrame.new({b: [11,12,13,14,15], a: [1,2,3,4,5],
133
+ c: [11,22,33,44,55]},
134
+ order: [:a, :b, :c],
135
+ index: [:one, :two, :three, :four, :five])
136
+ end
137
+
138
+ it "saves df to a file" do
139
+ outfile = Tempfile.new('dataframe.df')
140
+ @data_frame.save(outfile.path)
141
+ a = Daru::IO.load(outfile.path)
142
+ expect(a).to eq(@data_frame)
143
+ end
28
144
  end
29
145
  end
30
146
 
31
- context "#inspect" do
32
- it "prints DataFrame pretty" do
147
+ describe Daru::Vector do
148
+ context "Marshalling" do
149
+ it "" do
150
+ vector = Daru::Vector.new (0..100).collect { |_n| rand(100) }
151
+ expect(Marshal.load(Marshal.dump(vector))).to eq(vector)
152
+ end
153
+ end
33
154
 
155
+ context "#save" do
156
+ ALL_DTYPES.each do |dtype|
157
+ it "saves to a file and returns the same Vector of type #{dtype}" do
158
+ vector = Daru::Vector.new(
159
+ [5, 5, 5, 5, 5, 6, 6, 7, 8, 9, 10, 1, 2, 3, 4, 11, -99, -99],
160
+ dtype: dtype)
161
+ outfile = Tempfile.new('vector.vec')
162
+ vector.save(outfile.path)
163
+ expect(Daru::IO.load(outfile.path)).to eq(vector)
164
+ end
165
+ end
34
166
  end
35
167
  end
36
168
 
37
- context "#to_csv" do
38
- # TODO
169
+ describe Daru::Index do
170
+ context "Marshalling" do
171
+ it "" do
172
+ i = Daru::Index.new([:a, :b, :c, :d, :e], [8,6,4,3,5])
173
+ expect(Marshal.load(Marshal.dump(i))).to eq(i)
174
+ end
175
+ end
39
176
  end
40
- end
177
+ end