daru 0.1.4.1 → 0.1.5

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 (56) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +3 -0
  3. data/.travis.yml +3 -0
  4. data/CONTRIBUTING.md +27 -3
  5. data/Guardfile +7 -0
  6. data/History.md +39 -1
  7. data/README.md +1 -1
  8. data/daru.gemspec +9 -2
  9. data/lib/daru.rb +4 -1
  10. data/lib/daru/accessors/gsl_wrapper.rb +93 -91
  11. data/lib/daru/accessors/nmatrix_wrapper.rb +109 -107
  12. data/lib/daru/category.rb +22 -15
  13. data/lib/daru/core/group_by.rb +13 -2
  14. data/lib/daru/core/merge.rb +37 -31
  15. data/lib/daru/core/query.rb +10 -2
  16. data/lib/daru/dataframe.rb +95 -34
  17. data/lib/daru/date_time/index.rb +15 -16
  18. data/lib/daru/date_time/offsets.rb +14 -11
  19. data/lib/daru/formatters/table.rb +2 -2
  20. data/lib/daru/index/categorical_index.rb +201 -0
  21. data/lib/daru/index/index.rb +289 -0
  22. data/lib/daru/index/multi_index.rb +266 -0
  23. data/lib/daru/maths/statistics/vector.rb +13 -9
  24. data/lib/daru/monkeys.rb +0 -7
  25. data/lib/daru/plotting/gruff/category.rb +1 -0
  26. data/lib/daru/plotting/gruff/dataframe.rb +3 -3
  27. data/lib/daru/plotting/nyaplot/dataframe.rb +1 -1
  28. data/lib/daru/vector.rb +36 -21
  29. data/lib/daru/version.rb +1 -1
  30. data/spec/accessors/array_wrapper_spec.rb +3 -0
  31. data/spec/accessors/{wrappers_spec.rb → gsl_wrapper_spec.rb} +0 -35
  32. data/spec/accessors/nmatrix_wrapper_spec.rb +32 -0
  33. data/spec/{categorical_spec.rb → category_spec.rb} +3 -0
  34. data/spec/core/group_by_spec.rb +17 -1
  35. data/spec/core/merge_spec.rb +38 -1
  36. data/spec/core/query_spec.rb +5 -0
  37. data/spec/dataframe_spec.rb +230 -57
  38. data/spec/date_time/offsets_spec.rb +84 -3
  39. data/spec/formatters/table_formatter_spec.rb +9 -0
  40. data/spec/index/categorical_index_spec.rb +2 -0
  41. data/spec/index/index_spec.rb +17 -2
  42. data/spec/{math → maths}/arithmetic/dataframe_spec.rb +0 -0
  43. data/spec/{math → maths}/arithmetic/vector_spec.rb +0 -0
  44. data/spec/{math → maths}/statistics/dataframe_spec.rb +1 -1
  45. data/spec/{math → maths}/statistics/vector_spec.rb +7 -12
  46. data/spec/plotting/gruff/category_spec.rb +44 -0
  47. data/spec/plotting/gruff/dataframe_spec.rb +84 -0
  48. data/spec/plotting/gruff/vector_spec.rb +70 -0
  49. data/spec/plotting/nyaplot/category_spec.rb +51 -0
  50. data/spec/plotting/{dataframe_spec.rb → nyaplot/dataframe_spec.rb} +0 -83
  51. data/spec/plotting/nyaplot/vector_spec.rb +66 -0
  52. data/spec/spec_helper.rb +3 -2
  53. data/spec/vector_spec.rb +68 -1
  54. metadata +53 -24
  55. data/lib/daru/index.rb +0 -761
  56. data/spec/plotting/vector_spec.rb +0 -230
@@ -1,230 +0,0 @@
1
- require 'spec_helper.rb'
2
-
3
- describe Daru::Vector, 'plotting' do
4
- let(:vector) { Daru::Vector.new([11, 22, 33], index: [:a, :b, :c]) }
5
- let(:plot) { instance_double('Nyaplot::Plot') }
6
- let(:diagram) { instance_double('Nyaplot::Diagram') }
7
-
8
- before do
9
- Daru.plotting_library = :nyaplot
10
- allow(Nyaplot::Plot).to receive(:new).and_return(plot)
11
- end
12
-
13
- it 'plots the vector' do
14
- expect(plot).to receive(:add).with(:box, [11, 22, 33]).ordered
15
- expect(plot).to receive(:show).ordered
16
-
17
- vector.plot(type: :box)
18
- end
19
-
20
- context 'scatter' do
21
- it 'is default type' do
22
- expect(plot).to receive(:add).with(:scatter, instance_of(Array), instance_of(Array)).ordered
23
- expect(plot).to receive(:show).ordered
24
-
25
- vector.plot
26
- end
27
-
28
- it 'sets x_axis to 0...size' do
29
- expect(plot).to receive(:add).with(:scatter, [0, 1, 2], [11, 22, 33]).ordered
30
- expect(plot).to receive(:show).ordered
31
-
32
- vector.plot(type: :scatter)
33
- end
34
- end
35
-
36
- [:box, :histogram].each do |type|
37
- context type.to_s do
38
- it 'does not set x axis' do
39
- expect(plot).to receive(:add).with(type, [11, 22, 33]).ordered
40
- expect(plot).to receive(:show).ordered
41
-
42
- vector.plot(type: type)
43
- end
44
- end
45
- end
46
-
47
- [:bar, :line].each do |type| # FIXME: what other types 2D plot could have?..
48
- context type.to_s do
49
- it 'sets x axis to index' do
50
- expect(plot).to receive(:add).with(type, [:a, :b, :c], [11, 22, 33]).ordered
51
- expect(plot).to receive(:show).ordered
52
-
53
- vector.plot(type: type)
54
- end
55
- end
56
- end
57
-
58
- context 'with block provided' do
59
- it 'yields plot and diagram' do
60
- expect(plot).to receive(:add).with(:box, [11, 22, 33]).ordered.and_return(diagram)
61
- expect(plot).to receive(:show).ordered
62
-
63
- expect { |b| vector.plot(type: :box, &b) }.to yield_with_args(plot, diagram)
64
- end
65
- end
66
- end
67
-
68
- describe Daru::Vector, 'plotting category' do
69
- let(:plot) { instance_double('Nyaplot::Plot') }
70
- let(:diagram) { instance_double('Nyaplot::Diagram') }
71
- let(:dv) do
72
- Daru::Vector.new ['III']*10 + ['II']*5 + ['I']*5,
73
- type: :category,
74
- categories: ['I', 'II', 'III']
75
- end
76
- before do
77
- Daru.plotting_library = :nyaplot
78
- allow(Nyaplot::Plot).to receive(:new).and_return(plot)
79
- end
80
- context 'bar' do
81
- it 'plots bar graph taking a block' do
82
- expect(plot).to receive(:add).with(:bar, ['I', 'II', 'III'], [5, 5, 10])
83
- expect(plot).to receive :x_label
84
- expect(plot).to receive :y_label
85
- expect(plot).to receive(:show)
86
- dv.plot(type: :bar) do |p|
87
- p.x_label 'Categories'
88
- p.y_label 'Frequency'
89
- end
90
- end
91
-
92
- it 'plots bar graph without taking a block' do
93
- expect(plot).to receive(:add).with(:bar, ["I", "II", "III"], [5, 5, 10])
94
- expect(plot).to receive(:show)
95
- dv.plot(type: :bar)
96
- end
97
-
98
- it 'plots bar graph with percentage' do
99
- expect(plot).to receive(:add).with(:bar, ["I", "II", "III"], [25, 25, 50])
100
- expect(plot).to receive(:yrange).with [0, 100]
101
- expect(plot).to receive(:show)
102
- dv.plot(type: :bar, method: :percentage)
103
- end
104
-
105
- it 'plots bar graph with fraction' do
106
- expect(plot).to receive(:add).with(:bar, ["I", "II", "III"], [0.25, 0.25, 0.50])
107
- expect(plot).to receive(:yrange).with [0, 1]
108
- expect(plot).to receive(:show)
109
- dv.plot(type: :bar, method: :fraction)
110
- end
111
- end
112
-
113
- context 'other type' do
114
- it { expect { dv.plot(type: :scatter) }.to raise_error ArgumentError }
115
- end
116
- end
117
-
118
- describe Daru::Vector, 'plotting vector with gruff' do
119
- let(:dv) { Daru::Vector.new [1, 2, 3] }
120
- before { Daru.plotting_library = :gruff }
121
-
122
- context 'line' do
123
- let(:plot) { instance_double 'Gruff::Line' }
124
- before { allow(Gruff::Line).to receive(:new).and_return(plot) }
125
-
126
- it 'plots line graph without block' do
127
- expect(plot).to receive(:labels=)
128
- expect(plot).to receive(:data)
129
- dv.plot type: :line
130
- end
131
-
132
- it 'plots line graph with block' do
133
- expect(plot).to receive :labels=
134
- expect(plot).to receive :data
135
- expect(plot).to receive :title=
136
- dv.plot(type: :line) { |p| p.title = 'hello' }
137
- end
138
- end
139
-
140
- context 'bar' do
141
- let(:plot) { instance_double 'Gruff::Bar' }
142
- before { allow(Gruff::Bar).to receive(:new).and_return(plot) }
143
-
144
- it 'plots bar graph' do
145
- expect(plot).to receive :labels=
146
- expect(plot).to receive :data
147
- dv.plot type: :bar
148
- end
149
- end
150
-
151
- context 'pie' do
152
- let(:plot) { instance_double 'Gruff::Pie' }
153
- before { allow(Gruff::Pie).to receive(:new).and_return(plot) }
154
-
155
- it 'plots pie graph' do
156
- expect(plot).to receive(:data).exactly(3).times
157
- dv.plot type: :pie
158
- end
159
- end
160
-
161
- context 'scatter' do
162
- let(:plot) { instance_double 'Gruff::Scatter' }
163
- before { allow(Gruff::Scatter).to receive(:new).and_return(plot) }
164
-
165
- it 'plots scatter graph' do
166
- expect(plot).to receive :data
167
- dv.plot type: :scatter
168
- end
169
- end
170
-
171
- context 'sidebar' do
172
- let(:plot) { instance_double 'Gruff::SideBar' }
173
- before { allow(Gruff::SideBar).to receive(:new).and_return(plot) }
174
-
175
- it 'plots sidebar' do
176
- expect(plot).to receive :labels=
177
- expect(plot).to receive(:data).exactly(3).times
178
- dv.plot type: :sidebar
179
- end
180
- end
181
-
182
- context 'invalid type' do
183
- it { expect { dv.plot type: :lol }.to raise_error ArgumentError }
184
- end
185
- end
186
-
187
- describe Daru::Vector, 'plotting category vector with gruff' do
188
- before { Daru.plotting_library = :gruff }
189
- let(:dv) { Daru::Vector.new [1, 2, 3], type: :category }
190
-
191
- context 'bar' do
192
- let(:plot) { instance_double 'Gruff::Bar' }
193
- before { allow(Gruff::Bar).to receive(:new).and_return(plot) }
194
- it 'plots bar graph' do
195
- expect(plot).to receive :labels=
196
- expect(plot).to receive :data
197
- dv.plot type: :bar
198
- end
199
-
200
- it 'plots bar graph with block' do
201
- expect(plot).to receive :labels=
202
- expect(plot).to receive :data
203
- expect(plot).to receive :title=
204
- dv.plot(type: :bar) { |p| p.title = 'hello' }
205
- end
206
- end
207
-
208
- context 'pie' do
209
- let(:plot) { instance_double 'Gruff::Pie' }
210
- before { allow(Gruff::Pie).to receive(:new).and_return(plot) }
211
- it 'plots pie graph' do
212
- expect(plot).to receive(:data).exactly(3).times
213
- dv.plot type: :pie
214
- end
215
- end
216
-
217
- context 'sidebar' do
218
- let(:plot) { instance_double 'Gruff::SideBar' }
219
- before { allow(Gruff::SideBar).to receive(:new).and_return(plot) }
220
- it 'plots sidebar graph' do
221
- expect(plot).to receive :labels=
222
- expect(plot).to receive(:data).exactly(3).times
223
- dv.plot type: :sidebar
224
- end
225
- end
226
-
227
- context 'invalid type' do
228
- it { expect { dv.plot type: :lol }.to raise_error ArgumentError }
229
- end
230
- end