daru 0.1.6 → 0.2.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.
@@ -0,0 +1,38 @@
1
+ require 'daru/extensions/which_dsl'
2
+
3
+ describe "which DSL" do
4
+ before do
5
+ @df = Daru::DataFrame.new({
6
+ number: [1,2,3,4,5,6,Float::NAN],
7
+ sym: [:one, :two, :three, :four, :five, :six, :seven],
8
+ names: ['sameer', 'john', 'james', 'omisha', 'priyanka', 'shravan',nil]
9
+ })
10
+ end
11
+
12
+ it "accepts simple single eq statement" do
13
+ answer = Daru::DataFrame.new({
14
+ number: [4],
15
+ sym: [:four],
16
+ names: ['omisha']
17
+ }, index: Daru::Index.new([3])
18
+ )
19
+ expect( @df.which{ `number` == 4 } ).to eq(answer)
20
+ end
21
+
22
+ it "accepts somewhat complex comparison operator chaining" do
23
+ answer = Daru::DataFrame.new({
24
+ number: [3,4],
25
+ sym: [:three, :four],
26
+ names: ['james', 'omisha']
27
+ }, index: Daru::Index.new([2,3]))
28
+ expect(
29
+ @df.which{ (`names` == 'james') | (`sym` == :four) }
30
+ ).to eq(answer)
31
+ end
32
+
33
+ it "accepts vector methods" do
34
+ # expect( @df.which{ `number` == `number`.only_valid.max } ).to eq(@df.row_at(5)) # row_at(5) return a Vector object!?
35
+ expect( @df.which{ `number` <= `number`.only_valid.max } ).to eq(@df.row_at(0..5))
36
+ end
37
+
38
+ end
@@ -0,0 +1,32 @@
1
+ color,director_name,num_critic_for_reviews
2
+ Color,David Ayer,406
3
+ Color,Ivan Reitman,97
4
+ Color,Eric Brevig,143
5
+ Color,Kelly Asbury,106
6
+ Color,Frank Coraci,178
7
+ Color,Stephen Hopkins,102
8
+ Color,Jonathan Demme,209
9
+ Color,Henry Jaglom,19
10
+ Color,Genndy Tartakovsky,152
11
+ Black and White,,31
12
+ Color,James Algar,129
13
+ Color,Simon Wells,124
14
+ Color,Ron Underwood,35
15
+ Color,Dominic Sena,166
16
+ Color,Eric Brevig,143
17
+ Color,Vincent Ward,121
18
+ Color,Steven Brill,87
19
+ Color,Terry Gilliam,233
20
+ Color,Tim Burton,132
21
+ Color,Lucile Hadzihalilovic,63
22
+ Color,Lee Tamahori,95
23
+ Color,Jonathan Mostow,258
24
+ Color,Roger Donaldson,162
25
+ Color,Eric Brevig,147
26
+ Color,Barry Cook,113
27
+ Color,Roger Christian,174
28
+ Color,Joe Dante,101
29
+ Black and White,Rob Marshall,205
30
+ Color,Richard Donner,123
31
+ Color,Kevin Costner,79
32
+ Color,George Miller,61
@@ -13,8 +13,8 @@ describe Daru::IO do
13
13
  df = Daru::DataFrame.from_csv('spec/fixtures/matrix_test.csv',
14
14
  col_sep: ' ', headers: true)
15
15
 
16
- df.vectors = [:image_resolution, :mls, :true_transform].to_index
17
- expect(df.vectors).to eq([:image_resolution, :mls, :true_transform].to_index)
16
+ df.vectors = [:image_resolution, :true_transform, :mls].to_index
17
+ expect(df.vectors).to eq([:image_resolution, :true_transform, :mls].to_index)
18
18
  expect(df[:image_resolution].first).to eq(6.55779)
19
19
  expect(df[: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")
20
20
  end
@@ -78,6 +78,24 @@ describe Daru::Vector do
78
78
  end
79
79
  end
80
80
 
81
+ context "#add" do
82
+
83
+ it "adds two vectors with nils as 0 if skipnil is true" do
84
+ expect(@with_md1.add(@with_md2, skipnil: true)).to eq(Daru::Vector.new(
85
+ [1, 7, 3, 3, 1, 7],
86
+ name: :missing,
87
+ index: [:a, :b, :c, :corona, :obi, :wan]))
88
+ end
89
+
90
+ it "adds two vectors same as :+ if skipnil is false" do
91
+ expect(@with_md1.add(@with_md2, skipnil: false)).to eq(Daru::Vector.new(
92
+ [nil, 7, nil, nil, nil, 7],
93
+ name: :missing,
94
+ index: [:a, :b, :c, :corona, :obi, :wan]))
95
+ end
96
+
97
+ end
98
+
81
99
  context "#abs" do
82
100
  it "calculates abs value" do
83
101
  @with_md1.abs
@@ -12,7 +12,7 @@ describe Daru::Vector do
12
12
  end
13
13
  end
14
14
 
15
- let(:dv) { dv = Daru::Vector.new (["Tyrion", "Daenerys", "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :j]) }
15
+ let(:dv) { dv = Daru::Vector.new (["Tyrion", "Daenerys", nil, "Jon Starkgaryen"]), index: Daru::Index.new([:t, :d, :n, :j]) }
16
16
 
17
17
  context "#max" do
18
18
  it "returns max value" do
@@ -24,14 +24,23 @@ describe Daru::Vector do
24
24
  it "returns max value, sorted by comparitive block input" do
25
25
  expect(dv.max { |a,b| a.size <=> b.size }).to eq("Jon Starkgaryen")
26
26
  end
27
- it "returns max value, sorted by object block input" do
28
- expect(dv.max { |x| x.size }).to eq("Jon Starkgaryen")
29
- end
30
27
  it "returns N max values, sorted by comparitive block input" do
31
28
  expect(dv.max(2) {|a,b| a.size <=> b.size}).to eq(["Jon Starkgaryen","Daenerys"])
32
29
  end
30
+ end
31
+
32
+ context "#max_by" do
33
+ it "raises error without object block" do
34
+ expect { dv.max_by }.to raise_error(ArgumentError)
35
+ end
36
+ it "raises error without object block when N is given" do
37
+ expect { dv.max_by(2) }.to raise_error(ArgumentError)
38
+ end
39
+ it "returns max value, sorted by object block input" do
40
+ expect(dv.max_by { |x| x.size }).to eq("Jon Starkgaryen")
41
+ end
33
42
  it "returns N max values, sorted by object block input" do
34
- expect(dv.max(2) {|x| x.size }).to eq(["Jon Starkgaryen","Daenerys"])
43
+ expect(dv.max_by(2) {|x| x.size }).to eq(["Jon Starkgaryen","Daenerys"])
35
44
  end
36
45
  end
37
46
 
@@ -45,14 +54,23 @@ describe Daru::Vector do
45
54
  it "returns index_of_max value, sorted by comparitive block input" do
46
55
  expect(dv.index_of_max { |a,b| a.size <=> b.size }).to eq(:j)
47
56
  end
48
- it "returns index_of_max value, sorted by object block input" do
49
- expect(dv.index_of_max { |x| x.size }).to eq(:j)
50
- end
51
57
  it "returns N index_of_max values, sorted by comparitive block input" do
52
58
  expect(dv.index_of_max(2) {|a,b| a.size <=> b.size}).to eq([:j, :d])
53
59
  end
60
+ end
61
+
62
+ context "#index_of_max_by" do
63
+ it "raises error without object block" do
64
+ expect { dv.index_of_max_by }.to raise_error(ArgumentError)
65
+ end
66
+ it "raises error without object block when N is given" do
67
+ expect { dv.index_of_max_by(2) }.to raise_error(ArgumentError)
68
+ end
69
+ it "returns index_of_max value, sorted by object block input" do
70
+ expect(dv.index_of_max_by { |x| x.size }).to eq(:j)
71
+ end
54
72
  it "returns N index_of_max values, sorted by object block input" do
55
- expect(dv.index_of_max(2) {|x| x.size }).to eq([:j, :d])
73
+ expect(dv.index_of_max_by(2) {|x| x.size }).to eq([:j, :d])
56
74
  end
57
75
  end
58
76
 
@@ -66,14 +84,23 @@ describe Daru::Vector do
66
84
  it "returns min value, sorted by comparitive block input" do
67
85
  expect(dv.min { |a,b| a.size <=> b.size }).to eq("Tyrion")
68
86
  end
69
- it "returns min value, sorted by object block input" do
70
- expect(dv.min { |x| x.size }).to eq("Tyrion")
71
- end
72
87
  it "returns N min values, sorted by comparitive block input" do
73
88
  expect(dv.min(2) {|a,b| a.size <=> b.size}).to eq(["Tyrion","Daenerys"])
74
89
  end
90
+ end
91
+
92
+ context "#min_by" do
93
+ it "raises error without object block" do
94
+ expect { dv.min_by }.to raise_error(ArgumentError)
95
+ end
96
+ it "raises error without object block when N is given" do
97
+ expect { dv.min_by(2) }.to raise_error(ArgumentError)
98
+ end
99
+ it "returns min value, sorted by object block input" do
100
+ expect(dv.min_by { |x| x.size }).to eq("Tyrion")
101
+ end
75
102
  it "returns N min values, sorted by object block input" do
76
- expect(dv.min(2) {|x| x.size }).to eq(["Tyrion","Daenerys"])
103
+ expect(dv.min_by(2) {|x| x.size }).to eq(["Tyrion","Daenerys"])
77
104
  end
78
105
  end
79
106
 
@@ -87,38 +114,23 @@ describe Daru::Vector do
87
114
  it "returns index of min value, sorted by comparitive block input" do
88
115
  expect(dv.index_of_min { |a,b| a.size <=> b.size }).to eq(:t)
89
116
  end
90
- it "returns index of min value, sorted by object block input" do
91
- expect(dv.index_of_min { |x| x.size }).to eq(:t)
92
- end
93
117
  it "returns N index of min values, sorted by comparitive block input" do
94
118
  expect(dv.index_of_min(2) {|a,b| a.size <=> b.size}).to eq([:t, :d])
95
119
  end
96
- it "returns N index of min values, sorted by object block input" do
97
- expect(dv.index_of_min(2) {|x| x.size }).to eq([:t, :d])
98
- end
99
120
  end
100
121
 
101
- context "#max_by" do
102
- it "tests alias of max_by to max" do
103
- expect(dv.method(:max_by)).to eq(dv.method(:max))
122
+ context "#index_of_min_by" do
123
+ it "raises error without object block" do
124
+ expect { dv.index_of_min_by }.to raise_error(ArgumentError)
104
125
  end
105
- end
106
-
107
- context "#min_by" do
108
- it "tests alias of min_by to min" do
109
- expect(dv.method(:min_by)).to eq(dv.method(:min))
126
+ it "raises error without object block when N is given" do
127
+ expect { dv.index_of_min_by(2) }.to raise_error(ArgumentError)
110
128
  end
111
- end
112
-
113
- context "#index_of_max_by" do
114
- it "tests alias of index_of_max_by to index_of_max" do
115
- expect(dv.method(:index_of_max_by)).to eq(dv.method(:index_of_max))
129
+ it "returns index of min value, sorted by object block input" do
130
+ expect(dv.index_of_min_by { |x| x.size }).to eq(:t)
116
131
  end
117
- end
118
-
119
- context "#index_of_min_by" do
120
- it "tests alias of index_of_min_by to index_of_min" do
121
- expect(dv.method(:index_of_min_by)).to eq(dv.method(:index_of_min))
132
+ it "returns N index of min values, sorted by object block input" do
133
+ expect(dv.index_of_min_by(2) {|x| x.size }).to eq([:t, :d])
122
134
  end
123
135
  end
124
136
 
@@ -299,7 +311,11 @@ describe Daru::Vector do
299
311
 
300
312
  context "#proportions" do
301
313
  it "calculates proportions" do
302
- @dv.proportions
314
+ actual_proportions = {
315
+ array: {323=>0.1,11=>0.1,555=>0.1,666=>0.2,234=>0.1,21=>0.1,343=>0.1,1=>0.1,2=>0.1},
316
+ gsl: {323.0=>0.1, 11.0=>0.1, 555.0=>0.1, 666.0=>0.2, 234.0=>0.1, 21.0=>0.1, 343.0=>0.1, 1.0=>0.1, 2.0=>0.1}
317
+ }
318
+ expect(@dv.proportions).to eq(actual_proportions[dtype])
303
319
  end
304
320
  end
305
321
 
@@ -21,6 +21,16 @@ describe Daru::DataFrame, 'plotting' do
21
21
  index: [:one, :two, :three, :four]
22
22
  )
23
23
  }
24
+
25
+ let(:df_with_index_as_default) {
26
+ Nyaplot::DataFrame.new [
27
+ {:x=>1, :y1=>5, :y2=>-3, :cat=>:a, :_index=>:one},
28
+ {:x=>2, :y1=>7, :y2=>-7, :cat=>:b, :_index=>:two},
29
+ {:x=>3, :y1=>9, :y2=>-11, :cat=>:c, :_index=>:three},
30
+ {:x=>4, :y1=>11, :y2=>-15, :cat=>:d, :_index=>:four}
31
+ ]
32
+ }
33
+
24
34
  let(:plot) { instance_double('Nyaplot::Plot') }
25
35
  let(:diagram) { instance_double('Nyaplot::Diagram') }
26
36
 
@@ -58,6 +68,19 @@ describe Daru::DataFrame, 'plotting' do
58
68
  end
59
69
  end
60
70
 
71
+ context 'default x axis' do
72
+ it 'sets the x axis as the index value if not defined' do
73
+ expect(plot).to receive(:add_with_df)
74
+ .with(df_with_index_as_default, :line, :_index, :y1)
75
+ .ordered
76
+
77
+ expect(
78
+ data_frame.plot(
79
+ type: :line, y: :y1)
80
+ ).to eq plot
81
+ end
82
+ end
83
+
61
84
  context 'multiple charts' do
62
85
  it 'works with single type provided' do
63
86
  expect(plot).to receive(:add_with_df)
@@ -30,7 +30,7 @@ require 'simplecov'
30
30
  SimpleCov.start do
31
31
  add_filter 'vendor'
32
32
  add_filter 'spec'
33
- minimum_coverage_by_file 95
33
+ # minimum_coverage_by_file 95 -- too strict for now. Reconsider after specs redesign.
34
34
  end
35
35
 
36
36
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
@@ -1769,6 +1769,45 @@ describe Daru::Vector do
1769
1769
  end
1770
1770
  end
1771
1771
 
1772
+ context '#rolling_fillna!' do
1773
+ subject do
1774
+ Daru::Vector.new(
1775
+ [Float::NAN, 2, 1, 4, nil, Float::NAN, 3, nil, Float::NAN]
1776
+ )
1777
+ end
1778
+
1779
+ context 'rolling_fillna! forwards' do
1780
+ before { subject.rolling_fillna!(:forward) }
1781
+ its(:to_a) { is_expected.to eq [0, 2, 1, 4, 4, 4, 3, 3, 3] }
1782
+ end
1783
+
1784
+ context 'rolling_fillna! backwards' do
1785
+ before { subject.rolling_fillna!(direction: :backward) }
1786
+ its(:to_a) { is_expected.to eq [2, 2, 1, 4, 3, 3, 3, 0, 0] }
1787
+ end
1788
+
1789
+ context 'all invalid vector' do
1790
+ subject do
1791
+ Daru::Vector.new(
1792
+ [Float::NAN, Float::NAN, Float::NAN, Float::NAN, Float::NAN]
1793
+ )
1794
+ end
1795
+ before { subject.rolling_fillna!(:forward) }
1796
+ its(:to_a) { is_expected.to eq [0, 0, 0, 0, 0] }
1797
+ end
1798
+
1799
+ context 'with non-default index' do
1800
+ subject do
1801
+ Daru::Vector.new(
1802
+ [Float::NAN, 2, 1, 4, nil, Float::NAN, 3, nil, Float::NAN],
1803
+ index: %w[a b c d e f g h i]
1804
+ )
1805
+ end
1806
+ before { subject.rolling_fillna!(direction: :backward) }
1807
+ it { is_expected.to eq Daru::Vector.new([2, 2, 1, 4, 3, 3, 3, 0, 0], index: %w[a b c d e f g h i]) }
1808
+ end
1809
+ end
1810
+
1772
1811
  context "#type" do
1773
1812
  before(:each) do
1774
1813
  @numeric = Daru::Vector.new([1,2,3,4,5])
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.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sameer Deshmukh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-09 00:00:00.000000000 Z
11
+ date: 2017-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backports
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: packable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.9
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.9
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: spreadsheet
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -387,6 +401,7 @@ executables: []
387
401
  extensions: []
388
402
  extra_rdoc_files: []
389
403
  files:
404
+ - ".github/ISSUE_TEMPLATE.md"
390
405
  - ".gitignore"
391
406
  - ".rspec"
392
407
  - ".rspec_formatter.rb"
@@ -399,6 +414,7 @@ files:
399
414
  - LICENSE
400
415
  - README.md
401
416
  - Rakefile
417
+ - ReleasePolicy.md
402
418
  - benchmarks/TradeoffData.csv
403
419
  - benchmarks/csv_reading.rb
404
420
  - benchmarks/dataframe_creation.rb
@@ -442,6 +458,7 @@ files:
442
458
  - lib/daru/date_time/offsets.rb
443
459
  - lib/daru/exceptions.rb
444
460
  - lib/daru/extensions/rserve.rb
461
+ - lib/daru/extensions/which_dsl.rb
445
462
  - lib/daru/formatters/table.rb
446
463
  - lib/daru/helpers/array.rb
447
464
  - lib/daru/index/categorical_index.rb
@@ -485,6 +502,7 @@ files:
485
502
  - profile/joining.rb
486
503
  - profile/sorting.rb
487
504
  - profile/vector_each_with_index.rb
505
+ - profile/vector_new.rb
488
506
  - spec/accessors/array_wrapper_spec.rb
489
507
  - spec/accessors/gsl_wrapper_spec.rb
490
508
  - spec/accessors/nmatrix_wrapper_spec.rb
@@ -498,9 +516,11 @@ files:
498
516
  - spec/date_time/index_spec.rb
499
517
  - spec/date_time/offsets_spec.rb
500
518
  - spec/extensions/rserve_spec.rb
519
+ - spec/extensions/which_dsl_spec.rb
501
520
  - spec/fixtures/bank2.dat
502
521
  - spec/fixtures/boolean_converter_test.csv
503
522
  - spec/fixtures/countries.json
523
+ - spec/fixtures/duplicates.csv
504
524
  - spec/fixtures/eciresults.html
505
525
  - spec/fixtures/empties.dat
506
526
  - spec/fixtures/empty_rows_test.csv
@@ -584,7 +604,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
584
604
  version: '0'
585
605
  requirements: []
586
606
  rubyforge_project:
587
- rubygems_version: 2.5.2
607
+ rubygems_version: 2.6.10
588
608
  signing_key:
589
609
  specification_version: 4
590
610
  summary: Data Analysis in RUby
@@ -602,9 +622,11 @@ test_files:
602
622
  - spec/date_time/index_spec.rb
603
623
  - spec/date_time/offsets_spec.rb
604
624
  - spec/extensions/rserve_spec.rb
625
+ - spec/extensions/which_dsl_spec.rb
605
626
  - spec/fixtures/bank2.dat
606
627
  - spec/fixtures/boolean_converter_test.csv
607
628
  - spec/fixtures/countries.json
629
+ - spec/fixtures/duplicates.csv
608
630
  - spec/fixtures/eciresults.html
609
631
  - spec/fixtures/empties.dat
610
632
  - spec/fixtures/empty_rows_test.csv