daru 0.0.3.1 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/spec/index_spec.rb CHANGED
@@ -51,4 +51,12 @@ describe Daru::Index do
51
51
  :bob, :jimi, :richie].to_index)
52
52
  end
53
53
  end
54
+
55
+ context "#[]" do
56
+ it "works with ranges" do
57
+ id = Daru::Index.new [:one, :two, :three, :four, :five, :six, :seven]
58
+
59
+ expect(id[:two..:five]).to eq(Daru::Index.new([:two, :three, :four, :five]))
60
+ end
61
+ end
54
62
  end
data/spec/io/io_spec.rb CHANGED
@@ -15,6 +15,7 @@ describe Daru::DataFrame do
15
15
  end
16
16
  end
17
17
 
18
+
18
19
  expect(df.vectors).to eq([:image_resolution, :true_transform, :mls].to_index)
19
20
  expect(df.vector[:image_resolution].first).to eq(6.55779)
20
21
  expect(df.vector[:true_transform].first[15]).to eq(1.0)
@@ -1,12 +1,153 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
3
  describe Daru::Vector do
4
+ [NMatrix, Array].each do |dtype|
5
+ describe dtype do
6
+ before :each do
7
+ @dv = Daru::Vector.new [323, 11, 555, 666, 234, 21, 666, 343, 1, 2], dtype: dtype
8
+ end
4
9
 
5
- context "#mean" do
10
+ context "#mean" do
11
+ it "calculates mean" do
12
+ expect(@dv.mean).to eq(282.2)
13
+ end
14
+ end
6
15
 
7
- end
16
+ context "#sum_of_squares" do
17
+ it "calcs sum of squares" do
18
+
19
+ end
20
+ end
21
+
22
+ context "#standard_deviation_sample" do
23
+ it "calcs standard deviation sample" do
24
+
25
+ end
26
+ end
27
+
28
+ context "#variance_sample" do
29
+ it "calculates sample variance" do
30
+
31
+ end
32
+ end
33
+
34
+ context "#standard_deviation_population" do
35
+ it "calculates standard deviation population" do
36
+
37
+ end
38
+ end
39
+
40
+ context "#variance_population" do
41
+ it "calculates population variance" do
42
+ expect(@dv.variance_population).to eq(67606.95999999999)
43
+ end
44
+ end
45
+
46
+ context "#sum_of_squared_deviation" do
47
+ it "calculates sum of squared deviation" do
48
+ expect(@dv.sum_of_squared_deviation).to eq(676069.6)
49
+ end
50
+ end
51
+
52
+ context "#skew" do
53
+ it "calculates skewness" do
54
+ @dv.skew
55
+ end
56
+ end
57
+
58
+ context "#max" do
59
+ it "returns the max value" do
60
+ @dv.max
61
+ end
62
+ end
63
+
64
+ context "#min" do
65
+ it "returns the min value" do
66
+ @dv.min
67
+ end
68
+ end
69
+
70
+ context "#sum" do
71
+ it "returns the sum" do
72
+ @dv.sum
73
+ end
74
+ end
75
+
76
+ context "#product" do
77
+ it "returns the product" do
78
+ @dv.product
79
+ end
80
+ end
81
+
82
+ context "#median" do
83
+ it "returns the median" do
84
+ @dv.median
85
+ end
86
+ end
87
+
88
+ context "#mode" do
89
+ it "returns the mode" do
90
+ @dv.mode
91
+ end
92
+ end
93
+
94
+ context "#kurtosis" do
95
+ it "calculates kurtosis" do
96
+ @dv.kurtosis
97
+ end
98
+ end
99
+
100
+ context "#percentile" do
101
+ it "calculates percentile" do
102
+ expect(@dv.percentile(50)).to eq(333.0)
103
+ end
104
+ end
105
+
106
+ context "#recode" do
107
+
108
+ end
109
+
110
+ context "#recode!" do
111
+
112
+ end
113
+
114
+ context "#frequencies" do
115
+ it "calculates frequencies" do
116
+ @dv.frequencies
117
+ end
118
+ end
119
+
120
+ context "#average_deviation_population" do
121
+ it "calculates average_deviation_population" do
122
+ @dv.average_deviation_population
123
+ end
124
+ end
125
+
126
+ context "#proportion" do
127
+
128
+ end
129
+
130
+ context "#proportions" do
131
+ it "calculates proportions" do
132
+ @dv.proportions
133
+ end
134
+ end
135
+
136
+ context "#ranked" do
137
+ it "curates by rank" do
138
+ @dv.ranked
139
+ end
140
+ end
141
+
142
+ context "#count" do
8
143
 
9
- context "#median" do
144
+ end
10
145
 
146
+ context "#coefficient_of_variation" do
147
+ it "calculates coefficient_of_variation" do
148
+ @dv.coefficient_of_variation
149
+ end
150
+ end
151
+ end
11
152
  end
12
153
  end
data/spec/vector_spec.rb CHANGED
@@ -1,155 +1,172 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
3
  describe Daru::Vector do
4
- context "#initialize" do
5
- it "initializes from an Array" do
6
- dv = Daru::Vector.new [1,2,3,4,5], name: :ravan, index: [:ek, :don, :teen, :char, :pach]
4
+ [Array, NMatrix].each do |dtype|
5
+ describe dtype do
6
+ context "#initialize" do
7
+ it "initializes from an Array" do
8
+ dv = Daru::Vector.new [1,2,3,4,5], name: :ravan,
9
+ index: [:ek, :don, :teen, :char, :pach], dtype: dtype
7
10
 
8
- expect(dv.name) .to eq(:ravan)
9
- expect(dv.index).to eq(Daru::Index.new [:ek, :don, :teen, :char, :pach])
10
- end
11
-
12
- it "accepts Index object" do
13
- idx = Daru::Index.new [:yoda, :anakin, :obi, :padme, :r2d2]
14
-
15
- dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: idx
16
-
17
- expect(dv.name) .to eq(:yoga)
18
- expect(dv.index).to eq(idx)
19
- end
20
-
21
- it "raises error for improper Index" do
22
- expect {
23
- dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: [:i, :j, :k]
24
- }.to raise_error
25
-
26
- expect {
27
- idx = Daru::Index.new [:i, :j, :k]
28
- dv = Daru::Vector.new [1,2,3,4,5], name: :yoda, index: idx
29
- }.to raise_error
30
- end
31
-
32
- it "initializes without specifying an index" do
33
- dv = Daru::Vector.new [1,2,3,4,5], name: :vishnu
34
-
35
- expect(dv.index).to eq(Daru::Index.new [0,1,2,3,4])
36
- end
37
-
38
- it "inserts nils for extra indices" do
39
- dv = Daru::Vector.new [1,2,3], name: :yoga, index: [0,1,2,3,4]
40
-
41
- expect(dv).to eq([1,2,3,nil,nil].dv(:yoga))
42
- end
43
- end
44
-
45
- context "#[]" do
46
- before :each do
47
- @dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: [:yoda, :anakin, :obi, :padme, :r2d2]
48
- end
49
-
50
- it "returns an element after passing an index" do
51
- expect(@dv[:yoda]).to eq(1)
52
- end
53
-
54
- it "returns an element after passing a numeric index" , :focus => true do
55
- expect(@dv[0]).to eq(1)
56
- end
57
-
58
- it "returns a vector with given indices for multiple indices" do
59
- expect(@dv[:yoda, :anakin]).to eq(Daru::Vector.new([1,2], name: :yoda,
60
- index: [:yoda, :anakin]))
61
- end
62
- end
63
-
64
- context "#[]=" do
65
- before :each do
66
- @dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: [:yoda, :anakin, :obi, :padme, :r2d2]
67
- end
68
-
69
- it "assigns at the specified index" do
70
- @dv[:yoda] = 666
71
-
72
- expect(@dv[:yoda]).to eq(666)
73
- end
74
-
75
- it "assigns at the specified Integer index" do
76
- @dv[0] = 666
77
-
78
- expect(@dv[:yoda]).to eq(666)
79
- end
80
- end
81
-
82
- context "#concat" do
83
- before :each do
84
- @dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: [:warwick, :thompson, :jackson, :fender, :esp]
85
- end
86
-
87
- it "concatenates a new element at the end of vector with index" do
88
- @dv.concat 6, :ibanez
89
-
90
- expect(@dv.index) .to eq(
91
- [:warwick, :thompson, :jackson, :fender, :esp, :ibanez].to_index)
92
- expect(@dv[:ibanez]).to eq(6)
93
- expect(@dv[5]) .to eq(6)
94
- end
95
-
96
- it "concatenates without index if index is default numeric" do
97
- vector = Daru::Vector.new [1,2,3,4,5], name: :nums
98
-
99
- vector.concat 6
100
-
101
- expect(vector.index).to eq([0,1,2,3,4,5].to_index)
102
- expect(vector[5]) .to eq(6)
103
- end
104
-
105
- it "raises error if index not specified and non-numeric index" do
106
- expect {
107
- @dv.concat 6
108
- }.to raise_error
109
- end
110
- end
111
-
112
- context "#delete" do
113
- it "deletes specified value in the vector" do
114
- dv = Daru::Vector.new [1,2,3,4,5], name: :a
115
-
116
- dv.delete 3
117
-
118
- expect(dv).to eq(Daru::Vector.new [1,2,4,5], name: :a)
119
- end
120
- end
121
-
122
- context "#delete_at" do
123
- before :each do
124
- @dv = Daru::Vector.new [1,2,3,4,5], name: :a, index: [:one, :two, :three, :four, :five]
125
- end
126
-
127
- it "deletes element of specified index" do
128
- @dv.delete_at :one
129
-
130
- expect(@dv).to eq(Daru::Vector.new [2,3,4,5], name: :a, index: [:two, :three, :four, :five])
131
- end
132
-
133
- it "deletes element of specified integer index" do
134
- @dv.delete_at 2
135
-
136
- expect(@dv).to eq(Daru::Vector.new [1,2,4,5], name: :a, index: [:one, :two, :four, :five])
137
- end
138
- end
139
-
140
- context "#index_of" do
141
- it "returns index of specified value" do
142
- dv = Daru::Vector.new [1,2,3,4,5], name: :a, index: [:one, :two, :three, :four, :five]
143
-
144
- expect(dv.index_of(1)).to eq(:one)
145
- end
146
- end
147
-
148
- context "#to_hash" do
149
- it "returns the vector as a hash" do
150
- dv = Daru::Vector.new [1,2,3,4,5], name: :a, index: [:one, :two, :three, :four, :five]
151
-
152
- expect(dv.to_hash).to eq({one: 1, two: 2, three: 3, four: 4, five: 5})
11
+ expect(dv.name) .to eq(:ravan)
12
+ expect(dv.index).to eq(Daru::Index.new [:ek, :don, :teen, :char, :pach])
13
+ end
14
+
15
+ it "accepts Index object" do
16
+ idx = Daru::Index.new [:yoda, :anakin, :obi, :padme, :r2d2]
17
+
18
+ dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: idx, dtype: dtype
19
+
20
+ expect(dv.name) .to eq(:yoga)
21
+ expect(dv.index).to eq(idx)
22
+ end
23
+
24
+ it "raises error for improper Index" do
25
+ expect {
26
+ dv = Daru::Vector.new [1,2,3,4,5], name: :yoga, index: [:i, :j, :k]
27
+ }.to raise_error
28
+
29
+ expect {
30
+ idx = Daru::Index.new [:i, :j, :k]
31
+ dv = Daru::Vector.new [1,2,3,4,5], name: :yoda, index: idx, dtype: dtype
32
+ }.to raise_error
33
+ end
34
+
35
+ it "initializes without specifying an index" do
36
+ dv = Daru::Vector.new [1,2,3,4,5], name: :vishnu, dtype: dtype
37
+
38
+ expect(dv.index).to eq(Daru::Index.new [0,1,2,3,4])
39
+ end
40
+
41
+ it "inserts nils for extra indices" do
42
+ dv = Daru::Vector.new [1,2,3], name: :yoga, index: [0,1,2,3,4], dtype: Array
43
+
44
+ expect(dv).to eq([1,2,3,nil,nil].dv(:yoga,nil, Array))
45
+ end
46
+ end
47
+
48
+ context "#[]" do
49
+ before :each do
50
+ @dv = Daru::Vector.new [1,2,3,4,5], name: :yoga,
51
+ index: [:yoda, :anakin, :obi, :padme, :r2d2], dtype: dtype
52
+ end
53
+
54
+ it "returns an element after passing an index" do
55
+ expect(@dv[:yoda]).to eq(1)
56
+ end
57
+
58
+ it "returns an element after passing a numeric index" , :focus => true do
59
+ expect(@dv[0]).to eq(1)
60
+ end
61
+
62
+ it "returns a vector with given indices for multiple indices" do
63
+ expect(@dv[:yoda, :anakin]).to eq(Daru::Vector.new([1,2], name: :yoda,
64
+ index: [:yoda, :anakin], dtype: dtype))
65
+ end
66
+ end
67
+
68
+ context "#[]=" do
69
+ before :each do
70
+ @dv = Daru::Vector.new [1,2,3,4,5], name: :yoga,
71
+ index: [:yoda, :anakin, :obi, :padme, :r2d2], dtype: dtype
72
+ end
73
+
74
+ it "assigns at the specified index" do
75
+ @dv[:yoda] = 666
76
+
77
+ expect(@dv[:yoda]).to eq(666)
78
+ end
79
+
80
+ it "assigns at the specified Integer index" do
81
+ @dv[0] = 666
82
+
83
+ expect(@dv[:yoda]).to eq(666)
84
+ end
85
+ end
86
+
87
+ context "#concat" do
88
+ before :each do
89
+ @dv = Daru::Vector.new [1,2,3,4,5], name: :yoga,
90
+ index: [:warwick, :thompson, :jackson, :fender, :esp], dtype: dtype
91
+ end
92
+
93
+ it "concatenates a new element at the end of vector with index" do
94
+ @dv.concat 6, :ibanez
95
+
96
+ expect(@dv.index) .to eq(
97
+ [:warwick, :thompson, :jackson, :fender, :esp, :ibanez].to_index)
98
+ expect(@dv[:ibanez]).to eq(6)
99
+ expect(@dv[5]) .to eq(6)
100
+ end
101
+
102
+ it "concatenates without index if index is default numeric" do
103
+ vector = Daru::Vector.new [1,2,3,4,5], name: :nums, dtype: dtype
104
+
105
+ vector.concat 6
106
+
107
+ expect(vector.index).to eq([0,1,2,3,4,5].to_index)
108
+ expect(vector[5]) .to eq(6)
109
+ end
110
+
111
+ it "raises error if index not specified and non-numeric index" do
112
+ expect {
113
+ @dv.concat 6
114
+ }.to raise_error
115
+ end
116
+ end
117
+
118
+ context "#delete" do
119
+ it "deletes specified value in the vector" do
120
+ dv = Daru::Vector.new [1,2,3,4,5], name: :a, dtype: dtype
121
+
122
+ dv.delete 3
123
+
124
+ expect(dv).to eq(Daru::Vector.new [1,2,4,5], name: :a)
125
+ end
126
+ end
127
+
128
+ context "#delete_at" do
129
+ before :each do
130
+ @dv = Daru::Vector.new [1,2,3,4,5], name: :a,
131
+ index: [:one, :two, :three, :four, :five], dtype: dtype
132
+ end
133
+
134
+ it "deletes element of specified index" do
135
+ @dv.delete_at :one
136
+
137
+ expect(@dv).to eq(Daru::Vector.new [2,3,4,5], name: :a,
138
+ index: [:two, :three, :four, :five]), dtype: dtype
139
+ end
140
+
141
+ it "deletes element of specified integer index" do
142
+ @dv.delete_at 2
143
+
144
+ expect(@dv).to eq(Daru::Vector.new [1,2,4,5], name: :a,
145
+ index: [:one, :two, :four, :five]), dtype: dtype
146
+ end
147
+ end
148
+
149
+ context "#index_of" do
150
+ it "returns index of specified value" do
151
+ dv = Daru::Vector.new [1,2,3,4,5], name: :a,
152
+ index: [:one, :two, :three, :four, :five], dtype: dtype
153
+
154
+ expect(dv.index_of(1)).to eq(:one)
155
+ end
156
+ end
157
+
158
+ context "#to_hash" do
159
+ it "returns the vector as a hash" do
160
+ dv = Daru::Vector.new [1,2,3,4,5], name: :a,
161
+ index: [:one, :two, :three, :four, :five], dtype: dtype
162
+
163
+ expect(dv.to_hash).to eq({one: 1, two: 2, three: 3, four: 4, five: 5})
164
+ end
165
+ end
166
+
167
+ context "#uniq" do
168
+ # TODO
169
+ end
153
170
  end
154
171
  end
155
172
  end if mri?