daru-plotly 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ type,fixed acidity,volatile acidity,citric acid,residual sugar,chlorides,free sulfur dioxide,total sulfur dioxide,density,pH,sulphates,alcohol,quality
@@ -0,0 +1,43 @@
1
+ require 'daru'
2
+ require 'daru/plotly/category'
3
+ require 'daru/plotly/dataframe'
4
+ require 'daru/plotly/vector'
5
+ require 'daru/plotly/methods'
6
+
7
+ module Daru
8
+ # Common methods for DataFrame and Vector
9
+ module Plotly
10
+ def plot(source, opts = {})
11
+ data = opts[:data] || generate_data(source, opts)
12
+ layout = default_layout.merge(opts[:layout] || {})
13
+
14
+ plot = ::Plotly::Plot.new(data: data, layout: layout)
15
+ yield plot if block_given?
16
+ plot
17
+ end
18
+
19
+ def default_layout
20
+ {
21
+ width: 1000,
22
+ height: 500
23
+ }
24
+ end
25
+
26
+ def extract_mode_string(modes)
27
+ (Array(modes) || [:markers]).map(&:to_s).join('+')
28
+ end
29
+
30
+ def extract_type(plot_type)
31
+ case plot_type
32
+ when nil
33
+ :scatter
34
+ when *supported_types
35
+ plot_type
36
+ else
37
+ raise ArgumentError, "Type must be included in #{supported_types}."
38
+ end
39
+ end
40
+
41
+ module_function :default_layout, :extract_mode_string, :extract_type
42
+ end
43
+ end
@@ -0,0 +1,11 @@
1
+ require 'rbplotly'
2
+
3
+ module Daru
4
+ module Plotting
5
+ module Category
6
+ module PlotlyLibrary
7
+ include ::Plotly
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,64 @@
1
+ require 'rbplotly'
2
+ require 'daru'
3
+
4
+ module Daru
5
+ module Plotly
6
+ # exports #plot and #generate_data for DataFrame
7
+ module DataFrame
8
+ class << self
9
+ include Daru::Plotly
10
+
11
+ def generate_data(df, opts)
12
+ case extract_type(opts[:type])
13
+ when :pie
14
+ generate_data_for_pie(df, opts)
15
+ when :heatmap
16
+ generate_data_for_heatmap(df, opts)
17
+ else
18
+ generate_data_for_scatter_and_bar(df, opts)
19
+ end.map { |trace| trace.merge(opts[:opts] || {}) }
20
+ end
21
+
22
+ def generate_data_for_pie(df, opts)
23
+ labels = df[opts[:labels] || :labels].to_a
24
+ values = df[opts[:values] || :values].to_a
25
+ [
26
+ {
27
+ labels: labels,
28
+ values: values,
29
+ type: :pie
30
+ }
31
+ ]
32
+ end
33
+
34
+ def generate_data_for_heatmap(df, _opts)
35
+ [
36
+ {
37
+ z: df.data.map(&:to_a).transpose,
38
+ x: df.vectors.to_a,
39
+ y: df.index.to_a,
40
+ type: :heatmap
41
+ }
42
+ ]
43
+ end
44
+
45
+ def generate_data_for_scatter_and_bar(df, opts)
46
+ x = df[opts[:x] || :x].to_a
47
+ mode = extract_mode_string(opts[:mode])
48
+ Array(opts[:y] || :y).map do |vector_name|
49
+ {
50
+ x: x, y: df[vector_name].to_a,
51
+ type: opts[:type],
52
+ mode: mode,
53
+ name: vector_name
54
+ }
55
+ end
56
+ end
57
+
58
+ def supported_types
59
+ %i[scatter bar pie heatmap]
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,23 @@
1
+ require 'daru'
2
+
3
+ module Daru
4
+ module Plotly
5
+ # exports #plot and #generate_data for DataFrame and Vector
6
+ module Methods
7
+ %i[plot generate_data].each do |method_name|
8
+ define_method method_name do |data, opts|
9
+ if data.is_a? Daru::DataFrame
10
+ Daru::Plotly::DataFrame.send(method_name, data, opts)
11
+ elsif data.is_a? Daru::Vector
12
+ Daru::Plotly::Vector.send(method_name, data, opts)
13
+ else
14
+ raise ArgumentError,
15
+ 'first argument should be Daru::DataFrame or Daru::Vector.'
16
+ end
17
+ end
18
+ end
19
+
20
+ module_function :plot, :generate_data
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,47 @@
1
+ require 'rbplotly'
2
+ require 'daru'
3
+
4
+ module Daru
5
+ module Plotly
6
+ # exports #plot and #generate_data for Vector
7
+ module Vector
8
+ class << self
9
+ include Daru::Plotly
10
+
11
+ def generate_data(vector, opts)
12
+ type = extract_type opts[:type]
13
+
14
+ case type
15
+ when :histogram
16
+ generate_data_for_histogram(vector, opts)
17
+ when :scatter, :bar
18
+ generate_data_for_scatter_and_bar(vector, opts)
19
+ when :pie
20
+ generate_data_for_pie(vector, opts)
21
+ end.map { |trace| trace.merge(opts[:opts] || {}) }
22
+ end
23
+
24
+ def generate_data_for_histogram(vector, _opts)
25
+ [{ x: vector.to_a, type: :histogram }]
26
+ end
27
+
28
+ def generate_data_for_scatter_and_bar(vector, opts)
29
+ [
30
+ {
31
+ x: vector.index.to_a, y: vector.to_a,
32
+ type: opts[:type], mode: extract_mode_string(opts[:model])
33
+ }
34
+ ]
35
+ end
36
+
37
+ def generate_data_for_pie(vector, _opts)
38
+ [{ labels: vector.index.to_a, values: vector.to_a, type: :pie }]
39
+ end
40
+
41
+ def supported_types
42
+ %i[scatter bar histogram pie]
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module Daru
2
+ module Plotly
3
+ VERSION = '0.2.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,205 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daru-plotly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke Sangenya
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: daru
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rbplotly
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rbczmq
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: iruby
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: spreadsheet
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: nyaplot
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: gruff
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Draw graph with daru
154
+ email:
155
+ - longinus.eva@gmail.com
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".travis.yml"
163
+ - CODE_OF_CONDUCT.md
164
+ - Dockerfile
165
+ - Gemfile
166
+ - LICENSE
167
+ - README.md
168
+ - Rakefile
169
+ - bin/console
170
+ - bin/setup
171
+ - daru-plotly.gemspec
172
+ - examples/001.csv
173
+ - examples/O_TUKIBETSU_NENREI.csv
174
+ - examples/plots.ipynb
175
+ - examples/wine.csv
176
+ - lib/daru/plotly.rb
177
+ - lib/daru/plotly/category.rb
178
+ - lib/daru/plotly/dataframe.rb
179
+ - lib/daru/plotly/methods.rb
180
+ - lib/daru/plotly/vector.rb
181
+ - lib/daru/plotly/version.rb
182
+ homepage: https://github.com/genya0407/daru-plotly
183
+ licenses: []
184
+ metadata: {}
185
+ post_install_message:
186
+ rdoc_options: []
187
+ require_paths:
188
+ - lib
189
+ required_ruby_version: !ruby/object:Gem::Requirement
190
+ requirements:
191
+ - - ">="
192
+ - !ruby/object:Gem::Version
193
+ version: '0'
194
+ required_rubygems_version: !ruby/object:Gem::Requirement
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ version: '0'
199
+ requirements: []
200
+ rubyforge_project:
201
+ rubygems_version: 2.5.2
202
+ signing_key:
203
+ specification_version: 4
204
+ summary: Draw graph with daru
205
+ test_files: []