charty 0.2.4 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +64 -15
  3. data/charty.gemspec +10 -3
  4. data/lib/charty.rb +5 -2
  5. data/lib/charty/backends/bokeh.rb +2 -2
  6. data/lib/charty/backends/google_charts.rb +1 -1
  7. data/lib/charty/backends/gruff.rb +1 -1
  8. data/lib/charty/backends/plotly.rb +434 -32
  9. data/lib/charty/backends/plotly_helpers/html_renderer.rb +203 -0
  10. data/lib/charty/backends/plotly_helpers/notebook_renderer.rb +87 -0
  11. data/lib/charty/backends/plotly_helpers/plotly_renderer.rb +121 -0
  12. data/lib/charty/backends/pyplot.rb +187 -48
  13. data/lib/charty/backends/rubyplot.rb +1 -1
  14. data/lib/charty/cache_dir.rb +27 -0
  15. data/lib/charty/dash_pattern_generator.rb +57 -0
  16. data/lib/charty/index.rb +1 -1
  17. data/lib/charty/iruby_helper.rb +18 -0
  18. data/lib/charty/plot_methods.rb +115 -3
  19. data/lib/charty/plotter.rb +2 -2
  20. data/lib/charty/plotters.rb +4 -0
  21. data/lib/charty/plotters/abstract_plotter.rb +106 -11
  22. data/lib/charty/plotters/bar_plotter.rb +1 -16
  23. data/lib/charty/plotters/box_plotter.rb +1 -16
  24. data/lib/charty/plotters/distribution_plotter.rb +150 -0
  25. data/lib/charty/plotters/histogram_plotter.rb +242 -0
  26. data/lib/charty/plotters/line_plotter.rb +300 -0
  27. data/lib/charty/plotters/relational_plotter.rb +213 -96
  28. data/lib/charty/plotters/scatter_plotter.rb +8 -43
  29. data/lib/charty/statistics.rb +11 -2
  30. data/lib/charty/table.rb +124 -14
  31. data/lib/charty/table_adapters/base_adapter.rb +97 -0
  32. data/lib/charty/table_adapters/daru_adapter.rb +2 -0
  33. data/lib/charty/table_adapters/datasets_adapter.rb +7 -0
  34. data/lib/charty/table_adapters/hash_adapter.rb +19 -3
  35. data/lib/charty/table_adapters/pandas_adapter.rb +82 -0
  36. data/lib/charty/util.rb +28 -0
  37. data/lib/charty/vector_adapters.rb +5 -1
  38. data/lib/charty/vector_adapters/array_adapter.rb +2 -10
  39. data/lib/charty/vector_adapters/daru_adapter.rb +3 -11
  40. data/lib/charty/vector_adapters/narray_adapter.rb +1 -6
  41. data/lib/charty/vector_adapters/numpy_adapter.rb +1 -1
  42. data/lib/charty/vector_adapters/pandas_adapter.rb +0 -1
  43. data/lib/charty/version.rb +1 -1
  44. metadata +104 -11
  45. data/lib/charty/missing_value_support.rb +0 -14
@@ -21,6 +21,10 @@ module Charty
21
21
 
22
22
  attr_reader :data
23
23
 
24
+ def length
25
+ data.shape[0]
26
+ end
27
+
24
28
  def columns
25
29
  PandasIndex.new(data.columns)
26
30
  end
@@ -29,8 +33,10 @@ module Charty
29
33
  case new_columns
30
34
  when PandasIndex
31
35
  data.columns = new_columns.values
36
+ data.columns.name = new_columns.name
32
37
  when Index
33
38
  data.columns = new_columns.to_a
39
+ data.columns.name = new_columns.name
34
40
  else
35
41
  data.columns = new_columns
36
42
  end
@@ -44,8 +50,10 @@ module Charty
44
50
  case new_index
45
51
  when PandasIndex
46
52
  data.index = new_index.values
53
+ data.index.name = new_index.name
47
54
  when Index
48
55
  data.index = new_index.to_a
56
+ data.index.name = new_index.name
49
57
  else
50
58
  data.index = new_index
51
59
  end
@@ -72,10 +80,84 @@ module Charty
72
80
  end
73
81
  end
74
82
 
83
+ def drop_na
84
+ Charty::Table.new(@data.dropna)
85
+ end
86
+
87
+ def sort_values(by, na_position: :last)
88
+ Charty::Table.new(@data.sort_values(by, na_position: na_position, kind: :mergesort))
89
+ end
90
+
75
91
  private def check_type(type, data, name)
76
92
  return data if data.is_a?(type)
77
93
  raise TypeError, "#{name} must be a #{type}"
78
94
  end
95
+
96
+ def group_by(_table, grouper, sort, drop_na)
97
+ GroupBy.new(@data.groupby(by: grouper, sort: sort, dropna: drop_na))
98
+ end
99
+
100
+ def reset_index
101
+ Charty::Table.new(data.reset_index)
102
+ end
103
+
104
+ class GroupBy < Charty::Table::GroupByBase
105
+ def initialize(groupby)
106
+ @groupby = groupby
107
+ end
108
+
109
+ def indices
110
+ @groupby.indices.map { |k, v|
111
+ [k, v.to_a]
112
+ }.to_h
113
+ end
114
+
115
+ def group_keys
116
+ each_group_key.to_a
117
+ end
118
+
119
+ def each_group_key
120
+ return enum_for(__method__) unless block_given?
121
+
122
+ if PyCall.respond_to?(:iterable)
123
+ PyCall.iterable(@groupby).each do |key, index|
124
+ if key.class == PyCall.builtins.tuple
125
+ key = key.to_a
126
+ end
127
+ yield key
128
+ end
129
+ else # TODO: Remove this clause after the new PyCall will be released
130
+ iter = @groupby.__iter__()
131
+ while true
132
+ begin
133
+ key, sub_data = iter.__next__
134
+ if key.class == PyCall.builtins.tuple
135
+ key = key.to_a
136
+ end
137
+ yield key
138
+ rescue PyCall::PyError => error
139
+ if error.type == PyCall.builtins.StopIteration
140
+ break
141
+ else
142
+ raise error
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
148
+
149
+ def apply(*args, &block)
150
+ res = @groupby.apply(->(data) {
151
+ res = block.call(Charty::Table.new(data), *args)
152
+ Pandas::Series.new(data: res)
153
+ })
154
+ Charty::Table.new(res)
155
+ end
156
+
157
+ def [](key)
158
+ Charty::Table.new(@groupby.get_group(key))
159
+ end
160
+ end
79
161
  end
80
162
  end
81
163
  end
@@ -0,0 +1,28 @@
1
+ module Charty
2
+ module Util
3
+ if [].respond_to?(:filter_map)
4
+ module_function def filter_map(enum, &block)
5
+ enum.filter_map(&block)
6
+ end
7
+ else
8
+ module_function def filter_map(enum, &block)
9
+ enum.inject([]) do |acc, x|
10
+ y = block.call(x)
11
+ if y
12
+ acc.push(y)
13
+ else
14
+ acc
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ module_function def missing?(val)
21
+ val.nil? || nan?(val)
22
+ end
23
+
24
+ module_function def nan?(val)
25
+ val.respond_to?(:nan?) && val.nan?
26
+ end
27
+ end
28
+ end
@@ -22,7 +22,6 @@ module Charty
22
22
  class BaseAdapter
23
23
  extend Forwardable
24
24
  include Enumerable
25
- include MissingValueSupport
26
25
 
27
26
  def self.adapter_name
28
27
  name[/:?(\w+)Adapter\z/, 1]
@@ -61,6 +60,11 @@ module Charty
61
60
  def_delegators :data, :[], :[]=
62
61
  def_delegators :data, :each, :to_a, :empty?
63
62
 
63
+ # Take values at the given positional indices (without indexing)
64
+ def values_at(*indices)
65
+ indices.map {|i| data[i] }
66
+ end
67
+
64
68
  def where_in_array(mask)
65
69
  mask = check_mask_vector(mask)
66
70
  masked_data = []
@@ -30,7 +30,6 @@ module Charty
30
30
  include NameSupport
31
31
  include IndexSupport
32
32
 
33
- # TODO: Reconsider the return value type of values_at
34
33
  def_delegators :data, :values_at
35
34
 
36
35
  def where(mask)
@@ -80,14 +79,7 @@ module Charty
80
79
 
81
80
  def drop_na
82
81
  if numeric?
83
- Charty::Vector.new(data.reject { |x|
84
- case x
85
- when Float
86
- x.nan?
87
- else
88
- x.nil?
89
- end
90
- })
82
+ Charty::Vector.new(data.reject { |x| Util.missing?(x) })
91
83
  else
92
84
  Charty::Vector.new(data.compact)
93
85
  end
@@ -100,7 +92,7 @@ module Charty
100
92
  end
101
93
 
102
94
  def notnull
103
- Charty::Vector.new(data.map {|x| ! missing_value?(x) },
95
+ Charty::Vector.new(data.map {|x| ! Util.missing?(x) },
104
96
  index: index,
105
97
  name: name)
106
98
  end
@@ -55,7 +55,7 @@ module Charty
55
55
  def_delegators :data, :[]=, :to_a
56
56
 
57
57
  def values_at(*indices)
58
- indices.map {|i| data[i] }
58
+ indices.map {|i| data.at(i) }
59
59
  end
60
60
 
61
61
  def where(mask)
@@ -130,15 +130,7 @@ module Charty
130
130
  end
131
131
 
132
132
  def drop_na
133
- values = data.reject do |x|
134
- case
135
- when x.nil?,
136
- x.respond_to?(:nan?) && x.nan?
137
- true
138
- else
139
- false
140
- end
141
- end
133
+ values = data.reject {|x| Util.missing?(x) }
142
134
  Charty::Vector.new(Daru::Vector.new(values))
143
135
  end
144
136
 
@@ -149,7 +141,7 @@ module Charty
149
141
  end
150
142
 
151
143
  def notnull
152
- notnull_data = data.map {|x| ! missing_value?(x) }
144
+ notnull_data = data.map {|x| ! Util.missing?(x) }
153
145
  Charty::Vector.new(notnull_data, index: data.index.to_a, name: name)
154
146
  end
155
147
 
@@ -29,11 +29,6 @@ module Charty
29
29
  include NameSupport
30
30
  include IndexSupport
31
31
 
32
- # TODO: Reconsider the return value type of values_at
33
- def values_at(*indices)
34
- data[indices].to_a
35
- end
36
-
37
32
  def where(mask)
38
33
  mask = check_mask_vector(mask)
39
34
  case mask.data
@@ -157,7 +152,7 @@ module Charty
157
152
  i, n = 0, length
158
153
  notnull_data = Numo::Bit.zeros(n)
159
154
  while i < n
160
- notnull_data[i] = ! missing_value?(data[i])
155
+ notnull_data[i] = ! Util.missing?(data[i])
161
156
  i += 1
162
157
  end
163
158
  when ->(x) { x.respond_to?(:isnan) }
@@ -147,7 +147,7 @@ module Charty
147
147
  i, n = 0, length
148
148
  notnull_data = Numpy::NDArray.new(n, dtype: :bool)
149
149
  while i < n
150
- notnull_data[i] = ! missing_value?(data[i])
150
+ notnull_data[i] = ! Util.missing?(data[i])
151
151
  i += 1
152
152
  end
153
153
  else
@@ -86,7 +86,6 @@ module Charty
86
86
  data.size == 0
87
87
  end
88
88
 
89
- # TODO: Reconsider the return value type of values_at
90
89
  def values_at(*indices)
91
90
  data.take(indices).to_a
92
91
  end
@@ -1,5 +1,5 @@
1
1
  module Charty
2
- VERSION = "0.2.4"
2
+ VERSION = "0.2.9"
3
3
 
4
4
  module Version
5
5
  numbers, TAG = VERSION.split("-")
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: charty
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - youchan
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-05-19 00:00:00.000000000 Z
13
+ date: 2021-06-24 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: red-colors
@@ -18,28 +18,84 @@ dependencies:
18
18
  requirements:
19
19
  - - ">="
20
20
  - !ruby/object:Gem::Version
21
- version: '0'
21
+ version: 0.3.0
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - ">="
27
27
  - !ruby/object:Gem::Version
28
- version: '0'
28
+ version: 0.3.0
29
+ - !ruby/object:Gem::Dependency
30
+ name: red-datasets
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.1.2
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.1.2
29
43
  - !ruby/object:Gem::Dependency
30
44
  name: red-palette
31
45
  requirement: !ruby/object:Gem::Requirement
32
46
  requirements:
33
47
  - - ">="
34
48
  - !ruby/object:Gem::Version
35
- version: 0.2.0
49
+ version: 0.5.0
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 0.5.0
57
+ - !ruby/object:Gem::Dependency
58
+ name: matplotlib
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.2.0
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 1.2.0
71
+ - !ruby/object:Gem::Dependency
72
+ name: pandas
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 0.3.5
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 0.3.5
85
+ - !ruby/object:Gem::Dependency
86
+ name: playwright-ruby-client
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
36
92
  type: :runtime
37
93
  prerelease: false
38
94
  version_requirements: !ruby/object:Gem::Requirement
39
95
  requirements:
40
96
  - - ">="
41
97
  - !ruby/object:Gem::Version
42
- version: 0.2.0
98
+ version: '0'
43
99
  - !ruby/object:Gem::Dependency
44
100
  name: bundler
45
101
  requirement: !ruby/object:Gem::Requirement
@@ -83,21 +139,21 @@ dependencies:
83
139
  - !ruby/object:Gem::Version
84
140
  version: '0'
85
141
  - !ruby/object:Gem::Dependency
86
- name: red-datasets
142
+ name: daru
87
143
  requirement: !ruby/object:Gem::Requirement
88
144
  requirements:
89
145
  - - ">="
90
146
  - !ruby/object:Gem::Version
91
- version: 0.0.9
147
+ version: '0'
92
148
  type: :development
93
149
  prerelease: false
94
150
  version_requirements: !ruby/object:Gem::Requirement
95
151
  requirements:
96
152
  - - ">="
97
153
  - !ruby/object:Gem::Version
98
- version: 0.0.9
154
+ version: '0'
99
155
  - !ruby/object:Gem::Dependency
100
- name: daru
156
+ name: matrix
101
157
  requirement: !ruby/object:Gem::Requirement
102
158
  requirements:
103
159
  - - ">="
@@ -138,6 +194,34 @@ dependencies:
138
194
  - - ">="
139
195
  - !ruby/object:Gem::Version
140
196
  version: '0'
197
+ - !ruby/object:Gem::Dependency
198
+ name: iruby
199
+ requirement: !ruby/object:Gem::Requirement
200
+ requirements:
201
+ - - ">="
202
+ - !ruby/object:Gem::Version
203
+ version: 0.7.0
204
+ type: :development
205
+ prerelease: false
206
+ version_requirements: !ruby/object:Gem::Requirement
207
+ requirements:
208
+ - - ">="
209
+ - !ruby/object:Gem::Version
210
+ version: 0.7.0
211
+ - !ruby/object:Gem::Dependency
212
+ name: csv
213
+ requirement: !ruby/object:Gem::Requirement
214
+ requirements:
215
+ - - ">="
216
+ - !ruby/object:Gem::Version
217
+ version: '0'
218
+ type: :development
219
+ prerelease: false
220
+ version_requirements: !ruby/object:Gem::Requirement
221
+ requirements:
222
+ - - ">="
223
+ - !ruby/object:Gem::Version
224
+ version: '0'
141
225
  description: Visualizing your data in a simple way.
142
226
  email:
143
227
  - youchan01@gmail.com
@@ -217,13 +301,18 @@ files:
217
301
  - lib/charty/backends/google_charts.rb
218
302
  - lib/charty/backends/gruff.rb
219
303
  - lib/charty/backends/plotly.rb
304
+ - lib/charty/backends/plotly_helpers/html_renderer.rb
305
+ - lib/charty/backends/plotly_helpers/notebook_renderer.rb
306
+ - lib/charty/backends/plotly_helpers/plotly_renderer.rb
220
307
  - lib/charty/backends/pyplot.rb
221
308
  - lib/charty/backends/rubyplot.rb
222
309
  - lib/charty/backends/unicode_plot.rb
310
+ - lib/charty/cache_dir.rb
311
+ - lib/charty/dash_pattern_generator.rb
223
312
  - lib/charty/index.rb
313
+ - lib/charty/iruby_helper.rb
224
314
  - lib/charty/layout.rb
225
315
  - lib/charty/linspace.rb
226
- - lib/charty/missing_value_support.rb
227
316
  - lib/charty/plot_methods.rb
228
317
  - lib/charty/plotter.rb
229
318
  - lib/charty/plotters.rb
@@ -232,7 +321,10 @@ files:
232
321
  - lib/charty/plotters/box_plotter.rb
233
322
  - lib/charty/plotters/categorical_plotter.rb
234
323
  - lib/charty/plotters/count_plotter.rb
324
+ - lib/charty/plotters/distribution_plotter.rb
235
325
  - lib/charty/plotters/estimation_support.rb
326
+ - lib/charty/plotters/histogram_plotter.rb
327
+ - lib/charty/plotters/line_plotter.rb
236
328
  - lib/charty/plotters/random_support.rb
237
329
  - lib/charty/plotters/relational_plotter.rb
238
330
  - lib/charty/plotters/scatter_plotter.rb
@@ -248,6 +340,7 @@ files:
248
340
  - lib/charty/table_adapters/narray_adapter.rb
249
341
  - lib/charty/table_adapters/nmatrix_adapter.rb
250
342
  - lib/charty/table_adapters/pandas_adapter.rb
343
+ - lib/charty/util.rb
251
344
  - lib/charty/vector.rb
252
345
  - lib/charty/vector_adapters.rb
253
346
  - lib/charty/vector_adapters/array_adapter.rb