daru 0.1.5 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +5 -5
  2. data/.github/ISSUE_TEMPLATE.md +18 -0
  3. data/.gitignore +1 -0
  4. data/.rubocop.yml +21 -7
  5. data/.travis.yml +10 -5
  6. data/CONTRIBUTING.md +15 -10
  7. data/History.md +124 -2
  8. data/README.md +37 -9
  9. data/ReleasePolicy.md +20 -0
  10. data/benchmarks/db_loading.rb +34 -0
  11. data/benchmarks/statistics.rb +6 -6
  12. data/benchmarks/where_clause.rb +1 -1
  13. data/benchmarks/where_vs_filter.rb +1 -1
  14. data/daru.gemspec +17 -41
  15. data/lib/daru.rb +10 -13
  16. data/lib/daru/accessors/gsl_wrapper.rb +1 -1
  17. data/lib/daru/accessors/nmatrix_wrapper.rb +2 -0
  18. data/lib/daru/category.rb +29 -15
  19. data/lib/daru/configuration.rb +34 -0
  20. data/lib/daru/core/group_by.rb +158 -77
  21. data/lib/daru/core/merge.rb +12 -3
  22. data/lib/daru/core/query.rb +20 -4
  23. data/lib/daru/dataframe.rb +692 -118
  24. data/lib/daru/date_time/index.rb +14 -11
  25. data/lib/daru/date_time/offsets.rb +9 -1
  26. data/lib/daru/extensions/which_dsl.rb +55 -0
  27. data/lib/daru/formatters/table.rb +3 -5
  28. data/lib/daru/index/categorical_index.rb +4 -4
  29. data/lib/daru/index/index.rb +131 -42
  30. data/lib/daru/index/multi_index.rb +118 -10
  31. data/lib/daru/io/csv/converters.rb +21 -0
  32. data/lib/daru/io/io.rb +105 -33
  33. data/lib/daru/io/sql_data_source.rb +10 -0
  34. data/lib/daru/iruby/templates/dataframe.html.erb +4 -51
  35. data/lib/daru/iruby/templates/dataframe_mi.html.erb +3 -56
  36. data/lib/daru/iruby/templates/dataframe_mi_tbody.html.erb +35 -0
  37. data/lib/daru/iruby/templates/dataframe_mi_thead.html.erb +21 -0
  38. data/lib/daru/iruby/templates/dataframe_tbody.html.erb +28 -0
  39. data/lib/daru/iruby/templates/dataframe_thead.html.erb +21 -0
  40. data/lib/daru/iruby/templates/vector.html.erb +3 -25
  41. data/lib/daru/iruby/templates/vector_mi.html.erb +3 -34
  42. data/lib/daru/iruby/templates/vector_mi_tbody.html.erb +26 -0
  43. data/lib/daru/iruby/templates/vector_mi_thead.html.erb +8 -0
  44. data/lib/daru/iruby/templates/vector_tbody.html.erb +17 -0
  45. data/lib/daru/iruby/templates/vector_thead.html.erb +8 -0
  46. data/lib/daru/maths/arithmetic/vector.rb +38 -2
  47. data/lib/daru/maths/statistics/dataframe.rb +28 -30
  48. data/lib/daru/maths/statistics/vector.rb +295 -41
  49. data/lib/daru/plotting/gruff/dataframe.rb +13 -15
  50. data/lib/daru/plotting/nyaplot/category.rb +1 -1
  51. data/lib/daru/plotting/nyaplot/dataframe.rb +15 -4
  52. data/lib/daru/plotting/nyaplot/vector.rb +1 -2
  53. data/lib/daru/vector.rb +308 -96
  54. data/lib/daru/version.rb +1 -1
  55. data/profile/vector_new.rb +9 -0
  56. data/spec/accessors/gsl_wrapper_spec.rb +38 -35
  57. data/spec/accessors/nmatrix_wrapper_spec.rb +25 -22
  58. data/spec/category_spec.rb +24 -20
  59. data/spec/core/group_by_spec.rb +238 -4
  60. data/spec/core/merge_spec.rb +1 -1
  61. data/spec/core/query_spec.rb +65 -50
  62. data/spec/daru_spec.rb +22 -0
  63. data/spec/dataframe_spec.rb +473 -16
  64. data/spec/date_time/date_time_index_helper_spec.rb +72 -0
  65. data/spec/date_time/index_spec.rb +34 -16
  66. data/spec/date_time/offsets_spec.rb +14 -0
  67. data/spec/extensions/rserve_spec.rb +1 -1
  68. data/spec/extensions/which_dsl_spec.rb +38 -0
  69. data/spec/fixtures/boolean_converter_test.csv +5 -0
  70. data/spec/fixtures/duplicates.csv +32 -0
  71. data/spec/fixtures/eciresults.html +394 -0
  72. data/spec/fixtures/empty_rows_test.csv +17 -0
  73. data/spec/fixtures/macau.html +3691 -0
  74. data/spec/fixtures/macd_data.csv +150 -0
  75. data/spec/fixtures/matrix_test.csv +55 -55
  76. data/spec/fixtures/moneycontrol.html +6812 -0
  77. data/spec/fixtures/string_converter_test.csv +5 -0
  78. data/spec/fixtures/test_xls.xls +0 -0
  79. data/spec/fixtures/test_xls_2.xls +0 -0
  80. data/spec/fixtures/url_test.txt~ +0 -0
  81. data/spec/fixtures/valid_markup.html +62 -0
  82. data/spec/fixtures/wiki_climate.html +1243 -0
  83. data/spec/fixtures/wiki_table_info.html +631 -0
  84. data/spec/formatters/table_formatter_spec.rb +29 -0
  85. data/spec/index/categorical_index_spec.rb +33 -33
  86. data/spec/index/index_spec.rb +160 -41
  87. data/spec/index/multi_index_spec.rb +143 -33
  88. data/spec/io/io_spec.rb +246 -2
  89. data/spec/io/sql_data_source_spec.rb +31 -41
  90. data/spec/iruby/dataframe_spec.rb +17 -19
  91. data/spec/iruby/vector_spec.rb +26 -28
  92. data/spec/maths/arithmetic/dataframe_spec.rb +1 -1
  93. data/spec/maths/arithmetic/vector_spec.rb +18 -0
  94. data/spec/maths/statistics/vector_spec.rb +153 -15
  95. data/spec/plotting/gruff/category_spec.rb +3 -3
  96. data/spec/plotting/gruff/dataframe_spec.rb +14 -4
  97. data/spec/plotting/gruff/vector_spec.rb +9 -9
  98. data/spec/plotting/nyaplot/category_spec.rb +5 -9
  99. data/spec/plotting/nyaplot/dataframe_spec.rb +95 -47
  100. data/spec/plotting/nyaplot/vector_spec.rb +5 -11
  101. data/spec/shared/vector_display_spec.rb +12 -14
  102. data/spec/spec_helper.rb +30 -7
  103. data/spec/support/matchers.rb +5 -0
  104. data/spec/vector_spec.rb +306 -72
  105. metadata +96 -55
  106. data/spec/fixtures/stock_data.csv +0 -500
@@ -73,6 +73,8 @@ module Daru
73
73
  query = String.try_convert(query) or
74
74
  raise ArgumentError, "Query must be a string, #{query.class} received"
75
75
 
76
+ db = attempt_sqlite3_connection(db) if db.is_a?(String) && Pathname(db).exist?
77
+
76
78
  case db
77
79
  when DBI::DatabaseHandle
78
80
  DbiAdapter.new(db, query)
@@ -82,6 +84,14 @@ module Daru
82
84
  raise ArgumentError, "Unknown database adapter type #{db.class}"
83
85
  end
84
86
  end
87
+
88
+ def attempt_sqlite3_connection(db)
89
+ DBI.connect("DBI:SQLite3:#{db}")
90
+ rescue SQLite3::NotADatabaseException
91
+ raise ArgumentError, "Expected #{db} to point to a SQLite3 database"
92
+ rescue NameError
93
+ raise NameError, "In order to establish a connection to #{db}, please require 'dbi'"
94
+ end
85
95
  end
86
96
  end
87
97
  end
@@ -1,52 +1,5 @@
1
- <table>
2
- <tr>
3
- <th colspan='<%= @vectors.size+1 %>'>Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>)</th>
4
- </tr>
5
-
6
- <% if @vectors.is_a? MultiIndex %>
7
- <% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
8
- <tr>
9
- <% if idx.zero? %>
10
- <th rowspan="<%= @vectors.width %>"></th>
11
- <% end %>
12
- <% tuple.each do |idx, span| %>
13
- <th colspan="<%= span %>"><%= idx %></th>
14
- <% end %>
15
- </tr>
16
- <% end %>
17
- <% else %>
18
- <tr>
19
- <th></th>
20
- <% @vectors.each do |vector| %>
21
- <th><%=vector%></th>
22
- <% end %>
23
- </tr>
24
- <% end %>
25
-
26
- <% @index.first(threshold).each_with_index do |index, pos| %>
27
- <tr>
28
- <td><%= index %></td>
29
- <% row.at(pos).each do |element| %>
30
- <td><%= element.to_s %></td>
31
- <% end %>
32
- </tr>
33
- <% end %>
34
-
35
- <% if nrows > threshold %>
36
- <tr>
37
- <% (@vectors.size + 1).times do %>
38
- <td>...</td>
39
- <% end %>
40
- </tr>
41
-
42
- <% last_index = @index.to_a.last
43
- last_row = row_at @index.size-1 %>
44
-
45
- <tr>
46
- <td><%= last_index %></td>
47
- <% last_row.each do |element| %>
48
- <td><%= element.to_s %></td>
49
- <% end %>
50
- </tr>
51
- <% end %>
1
+ <b> Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>) </b>
2
+ <table border="1" class="dataframe">
3
+ <%= table_thead %>
4
+ <%= table_tbody %>
52
5
  </table>
@@ -1,58 +1,5 @@
1
+ <b> Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>) </b>
1
2
  <table>
2
- <tr>
3
- <th colspan='<%= @vectors.size+index.width %>'>Daru::DataFrame<%= name ? ": #{name} " : ''%>(<%=nrows%>x<%=ncols%>)</th>
4
- </tr>
5
- <% if @vectors.is_a? MultiIndex %>
6
- <% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
7
- <tr>
8
- <% if idx.zero? %>
9
- <th colspan="<%= index.width %>" rowspan="<%= @vectors.width %>"></th>
10
- <% end %>
11
- <% tuple.each do |idx, span| %>
12
- <th colspan="<%= span %>"><%= idx %></th>
13
- <% end %>
14
- </tr>
15
- <% end %>
16
- <% else %>
17
- <tr>
18
- <th colspan="<%= index.width %>"></th>
19
- <% @vectors.each do |vector| %>
20
- <th><%=vector%></th>
21
- <% end %>
22
- </tr>
23
- <% end %>
24
-
25
- <% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(@index.to_a).each do |tuple, index| %>
26
- <tr>
27
- <% tuple.each do |idx, span| %>
28
- <th rowspan="<%= span %>"><%= idx %></th>
29
- <% end %>
30
- <% row[index].each do |element| %>
31
- <td><%= element.to_s %></td>
32
- <% end %>
33
- </tr>
34
- <% end %>
35
-
36
- <% if nrows > threshold %>
37
- <tr>
38
- <% index.width.times do %>
39
- <th>...</th>
40
- <% end %>
41
- <% @vectors.size.times do %>
42
- <td>...</td>
43
- <% end %>
44
- </tr>
45
-
46
- <% last_index = @index.to_a.last
47
- last_row = row[last_index] %>
48
-
49
- <tr>
50
- <% last_index.each do |idx| %>
51
- <th><%= idx %></td>
52
- <% end %>
53
- <% last_row.each do |element| %>
54
- <td><%= element.to_s %></td>
55
- <% end %>
56
- </tr>
57
- <% end %>
3
+ <%= table_thead %>
4
+ <%= table_tbody %>
58
5
  </table>
@@ -0,0 +1,35 @@
1
+ <tbody>
2
+ <% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(@index.to_a).each do |tuple, index| %>
3
+ <tr>
4
+ <% tuple.each do |idx, span| %>
5
+ <th rowspan="<%= span %>"><%= idx %></th>
6
+ <% end %>
7
+ <% row[index].each do |element| %>
8
+ <td><%= element.to_s %></td>
9
+ <% end %>
10
+ </tr>
11
+ <% end %>
12
+
13
+ <% if nrows > threshold %>
14
+ <tr>
15
+ <% index.width.times do %>
16
+ <th>...</th>
17
+ <% end %>
18
+ <% @vectors.size.times do %>
19
+ <td>...</td>
20
+ <% end %>
21
+ </tr>
22
+
23
+ <% last_index = @index.to_a.last
24
+ last_row = row[last_index] %>
25
+
26
+ <tr>
27
+ <% last_index.each do |idx| %>
28
+ <th><%= idx %></td>
29
+ <% end %>
30
+ <% last_row.each do |element| %>
31
+ <td><%= element.to_s %></td>
32
+ <% end %>
33
+ </tr>
34
+ <% end %>
35
+ </tbody>
@@ -0,0 +1,21 @@
1
+ <thead>
2
+ <% if @vectors.is_a? MultiIndex %>
3
+ <% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
4
+ <tr>
5
+ <% if idx.zero? %>
6
+ <th colspan="<%= index.width %>" rowspan="<%= @vectors.width %>"></th>
7
+ <% end %>
8
+ <% tuple.each do |idx, span| %>
9
+ <th colspan="<%= span %>"><%= idx %></th>
10
+ <% end %>
11
+ </tr>
12
+ <% end %>
13
+ <% else %>
14
+ <tr>
15
+ <th colspan="<%= index.width %>"></th>
16
+ <% @vectors.each do |vector| %>
17
+ <th><%=vector%></th>
18
+ <% end %>
19
+ </tr>
20
+ <% end %>
21
+ </thead>
@@ -0,0 +1,28 @@
1
+ <tbody>
2
+ <% @index.first(threshold).each_with_index do |index, pos| %>
3
+ <tr>
4
+ <td><%= index %></td>
5
+ <% row.at(pos).each do |element| %>
6
+ <td><%= element.to_s %></td>
7
+ <% end %>
8
+ </tr>
9
+ <% end %>
10
+
11
+ <% if nrows > threshold %>
12
+ <tr>
13
+ <% (@vectors.size + 1).times do %>
14
+ <td>...</td>
15
+ <% end %>
16
+ </tr>
17
+
18
+ <% last_index = @index.to_a.last
19
+ last_row = row_at @index.size-1 %>
20
+
21
+ <tr>
22
+ <td><%= last_index %></td>
23
+ <% last_row.each do |element| %>
24
+ <td><%= element.to_s %></td>
25
+ <% end %>
26
+ </tr>
27
+ <% end %>
28
+ </tbody>
@@ -0,0 +1,21 @@
1
+ <thead>
2
+ <% if @vectors.is_a? MultiIndex %>
3
+ <% Daru::IRuby::Helpers.tuples_with_colspans(@vectors).each_with_index do |tuple, idx| %>
4
+ <tr>
5
+ <% if idx.zero? %>
6
+ <th rowspan="<%= @vectors.width %>"></th>
7
+ <% end %>
8
+ <% tuple.each do |idx, span| %>
9
+ <th colspan="<%= span %>"><%= idx %></th>
10
+ <% end %>
11
+ </tr>
12
+ <% end %>
13
+ <% else %>
14
+ <tr>
15
+ <th></th>
16
+ <% @vectors.each do |vector| %>
17
+ <th><%=vector%></th>
18
+ <% end %>
19
+ </tr>
20
+ <% end %>
21
+ </thead>
@@ -1,27 +1,5 @@
1
+ <b> Daru::Vector(<%= size %>)<%= ':category' if category? %> </b>
1
2
  <table>
2
- <tr>
3
- <th colspan="2">Daru::Vector(<%= size %>)<%= ':category' if category? %></th>
4
- </tr>
5
- <% if name %>
6
- <tr>
7
- <th> </th>
8
- <th><%= name %></th>
9
- </tr>
10
- <% end %>
11
-
12
- <% @index.each_with_index.first(threshold).each do |index, pos| %>
13
- <tr>
14
- <td><%= index %></td>
15
- <td><%= self.at(pos) %></td>
16
- </tr>
17
- <% end %>
18
-
19
- <% if size > threshold %>
20
- <% last_index = @index.size-1 %>
21
- <tr><td>...</td><td>...</td></tr>
22
- <tr>
23
- <td><%= last_index %></td>
24
- <td><%= self.at last_index %></td>
25
- </tr>
26
- <% end %>
3
+ <%= table_thead %>
4
+ <%= table_tbody %>
27
5
  </table>
@@ -1,36 +1,5 @@
1
+ <b> Daru::Vector(<%= size %>)<%= ':category' if category? %> </b>
1
2
  <table>
2
- <tr>
3
- <th colspan="<%= index.width+1 %>">Daru::Vector(<%= size %>)<%= ':category' if category? %></th>
4
- </tr>
5
- <% if name %>
6
- <tr>
7
- <th colspan="<%= index.width %>"> </th>
8
- <th><%= name %></th>
9
- </tr>
10
- <% end %>
11
-
12
- <% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(to_a).each do |tuple, value| %>
13
- <tr>
14
- <% tuple.each do |idx, span| %>
15
- <th rowspan="<%= span %>"><%= idx %></th>
16
- <% end %>
17
- <td><%= value %></td>
18
- </tr>
19
- <% end %>
20
-
21
- <% if size > threshold %>
22
- <% last_index = @index.to_a.last %>
23
- <tr>
24
- <% last_index.size.times do %>
25
- <th>...</th>
26
- <% end %>
27
- <td>...</td>
28
- </tr>
29
- <tr>
30
- <% last_index.each do |idx| %>
31
- <th><%= idx %></td>
32
- <% end %>
33
- <td><%= self[last_index] %></td>
34
- </tr>
35
- <% end %>
3
+ <%= table_thead %>
4
+ <%= table_tbody %>
36
5
  </table>
@@ -0,0 +1,26 @@
1
+ <tbody>
2
+ <% Daru::IRuby::Helpers.tuples_with_rowspans(@index).first(threshold).zip(to_a).each do |tuple, value| %>
3
+ <tr>
4
+ <% tuple.each do |idx, span| %>
5
+ <th rowspan="<%= span %>"><%= idx %></th>
6
+ <% end %>
7
+ <td><%= value %></td>
8
+ </tr>
9
+ <% end %>
10
+
11
+ <% if size > threshold %>
12
+ <% last_index = @index.to_a.last %>
13
+ <tr>
14
+ <% last_index.size.times do %>
15
+ <th>...</th>
16
+ <% end %>
17
+ <td>...</td>
18
+ </tr>
19
+ <tr>
20
+ <% last_index.each do |idx| %>
21
+ <th><%= idx %></td>
22
+ <% end %>
23
+ <td><%= self[last_index] %></td>
24
+ </tr>
25
+ <% end %>
26
+ </tbody>
@@ -0,0 +1,8 @@
1
+ <thead>
2
+ <% if name %>
3
+ <tr>
4
+ <th colspan="<%= index.width %>"> </th>
5
+ <th><%= name %></th>
6
+ </tr>
7
+ <% end %>
8
+ </thead>
@@ -0,0 +1,17 @@
1
+ <tbody>
2
+ <% @index.each_with_index.first(threshold).each do |index, pos| %>
3
+ <tr>
4
+ <td><%= index %></td>
5
+ <td><%= self.at(pos) %></td>
6
+ </tr>
7
+ <% end %>
8
+
9
+ <% if size > threshold %>
10
+ <% last_index = @index.size-1 %>
11
+ <tr><td>...</td><td>...</td></tr>
12
+ <tr>
13
+ <td><%= last_index %></td>
14
+ <td><%= self.at last_index %></td>
15
+ </tr>
16
+ <% end %>
17
+ </tbody>
@@ -0,0 +1,8 @@
1
+ <thead>
2
+ <% if name %>
3
+ <tr>
4
+ <th> </th>
5
+ <th><%= name %></th>
6
+ </tr>
7
+ <% end %>
8
+ </thead>
@@ -42,6 +42,34 @@ module Daru
42
42
  recode { |e| e.round(precision) unless e.nil? }
43
43
  end
44
44
 
45
+ # Add specified vector.
46
+ #
47
+ # @param other [Daru::Vector] The vector thats added to this.
48
+ # @param opts [Boolean] :skipnil if true treats nils as 0.
49
+ #
50
+ # @example
51
+ #
52
+ # v0 = Daru::Vector.new [1, 2, nil, nil]
53
+ # v1 = Daru::Vector.new [2, 1, 3, nil]
54
+ #
55
+ # irb> v0.add v1
56
+ # => #<Daru::Vector(4)>
57
+ # 0 3
58
+ # 1 3
59
+ # 2 nil
60
+ # 3 nil
61
+ #
62
+ # irb> v0.add v1, skipnil: true
63
+ # => #<Daru::Vector(4)>
64
+ # 0 3
65
+ # 1 3
66
+ # 2 3
67
+ # 3 0
68
+ #
69
+ def add other, opts={}
70
+ v2v_binary :+, other, skipnil: opts.fetch(:skipnil, false)
71
+ end
72
+
45
73
  private
46
74
 
47
75
  def math_unary_op operation
@@ -62,19 +90,27 @@ module Daru
62
90
  name: @name, index: @index
63
91
  end
64
92
 
65
- def v2v_binary operation, other
93
+ def v2v_binary operation, other, opts={}
66
94
  # FIXME: why the sorting?.. - zverok, 2016-05-18
67
95
  index = (@index.to_a | other.index.to_a).sort
68
96
 
69
97
  elements = index.map do |idx|
70
98
  this = self.index.include?(idx) ? self[idx] : nil
71
99
  that = other.index.include?(idx) ? other[idx] : nil
72
-
100
+ this, that = zero_nil_args(this, that, opts.fetch(:skipnil, false))
73
101
  this && that ? this.send(operation, that) : nil
74
102
  end
75
103
 
76
104
  Daru::Vector.new(elements, name: @name, index: index)
77
105
  end
106
+
107
+ def zero_nil_args(this, that, skipnil)
108
+ if skipnil
109
+ this = 0 if this.nil?
110
+ that = 0 if that.nil?
111
+ end
112
+ [this, that]
113
+ end
78
114
  end
79
115
  end
80
116
  end