tukey 0.9.0RC

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a8d2baa0a928708c6c82a545a3f80fc50981fd23
4
+ data.tar.gz: b32cc0bce8639763a04b532bfde06455c69302f1
5
+ SHA512:
6
+ metadata.gz: a787e0c9d891cbbcb66a566118194f90c20278865ba45c50506979baeb6e6da68473541d0c35c132d487b88bbe29c91fba793e8b41701e8a3084d1f12d46c5c9
7
+ data.tar.gz: 08803af0c3dc11cd02ebac3c32456af1b52dd691e6156e8398b16b65fb96a1b6160e0ff95046bde73bbb7b32cc2e1c327d6d45e53520fdecc0532aefc2f87a4c
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.6
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tukey.gemspec
4
+ gemspec
5
+
6
+ gem 'pry', '~> 0.10.4'
7
+ gem 'pry-coolline', '~> 0.2.5'
data/John_Tukey.jpg ADDED
Binary file
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Achilleas Buisman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ ![John Tukey - Statistician and inventor of the 'bit'](John_Tukey.jpg)
2
+
3
+ *John Tukey - Statistician and inventor of the 'bit'*
4
+
5
+ # Tukey
6
+
7
+ Tukey is provides DataSets which can be put in a tree. This way you can store partial results of calculations or other data and, for example, create charts, tables or other presentations.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'tukey'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install tukey
24
+
25
+ # Usage
26
+
27
+ First a quick demonstration:
28
+
29
+ ![screencast](Tukey.gif)
30
+
31
+
32
+ Then a quick example:
33
+
34
+ ```ruby
35
+ report = DataSet.new(label: 'Year star report')
36
+
37
+ report << DataSet.new(label: "January", data: 0)
38
+ report << DataSet.new(label: "February", data: 30)
39
+ report << DataSet.new(label: "March", data: 40)
40
+ report << DataSet.new(label: "April", data: 50)
41
+
42
+ # Get a total:
43
+ report.sum # => 120
44
+ report.average # => 30.0
45
+
46
+ # Labels of all leafs
47
+ # (useful when creating tables and charts, more on that later):
48
+ report.leaf_labels.map(&:name) # => ["January", "February", "March", "April"]
49
+
50
+ # Value of single children:
51
+ report.children.first.value # => 0
52
+
53
+ # Useful development output:
54
+ puts report.pretty_inspect
55
+ #=> * Year star report
56
+ # |- January: 0
57
+ # |- February: 30
58
+ # |- March: 40
59
+ # |- April: 50
60
+
61
+ # Filtering:
62
+ # (can be far more advances, more on that later)
63
+ report.filter { |p, set| set.value >= 40 }
64
+ #=> * Year star report
65
+ # |- March: 40
66
+ # |- April: 50
67
+
68
+ # Combining:
69
+ report2 = DataSet.new(label: "Yearly star report")
70
+ report2 << DataSet.new(label: 'January', data: 10)
71
+ report2 << DataSet.new(label: 'May', data: 60)
72
+ report2 << DataSet.new(label: 'June', data: 70)
73
+
74
+ report.combine(report2, :+)
75
+ # * Year star report
76
+ # |- January: 10 # <= Note the added 10
77
+ # |- February: 30
78
+ # |- March: 40
79
+ # |- April: 50
80
+ # |- May: 60
81
+ # |- June: 70
82
+ ```
83
+
84
+ ## Development
85
+
86
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
87
+
88
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
89
+
90
+ ## Contributing
91
+
92
+ Bug reports and pull requests are welcome on GitHub at https://github.com/abuisman/tukey.
93
+
94
+
95
+ ## License
96
+
97
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
98
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/Tukey.gif ADDED
Binary file
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "tukey"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/tukey.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.join(File.dirname(__FILE__), "tukey", "version")
2
+ require File.join(File.dirname(__FILE__), "tukey", "data_set")
@@ -0,0 +1,294 @@
1
+ require 'securerandom'
2
+ require File.join(File.dirname(__FILE__), "data_set", "label")
3
+
4
+ class DataSet
5
+ include Enumerable
6
+
7
+ attr_accessor :label
8
+
9
+ attr_accessor :data
10
+ attr_accessor :parent
11
+ attr_reader :id
12
+
13
+ def initialize(data: nil, label: nil, parent: nil, id: nil)
14
+ self.data = data # We have to use `self` here because we have a custom setter for `data`
15
+ @parent = parent
16
+ @id = id || SecureRandom.uuid
17
+
18
+ return unless label
19
+
20
+ if label.is_a?(DataSet::Label)
21
+ @label = label
22
+ elsif label.is_a?(String)
23
+ @label = DataSet::Label.new(label)
24
+ elsif label.is_a?(Hash)
25
+ @label = DataSet::Label.new(label.delete(:name), **label)
26
+ else
27
+ fail ArgumentError, 'Given unsupported label type to DataSet initialize'
28
+ end
29
+ end
30
+
31
+ def <<(item)
32
+ self.data ||= []
33
+ fail(CannotAddToNonEnumerableData, parent: self, item: item) unless data_array?
34
+ item.parent = self
35
+ data.push(item)
36
+ end
37
+ alias_method :add_item, :<<
38
+
39
+ def data=(items)
40
+ items.each { |item| item.parent = self if item.is_a? DataSet } if items.is_a? Enumerable
41
+ @data = items
42
+ end
43
+
44
+ def children
45
+ return data if data_array?
46
+ []
47
+ end
48
+
49
+ def siblings
50
+ return [] if parent.nil?
51
+ parent.children.reject { |c| c == self }
52
+ end
53
+
54
+ def oneling?
55
+ siblings.none?
56
+ end
57
+
58
+ def compact_onelings
59
+ filter(orphan_strategy: :adopt, keep_leafs: true) { |p, d| false if d.oneling? && !d.root? }
60
+ end
61
+
62
+ def root?
63
+ parent.nil?
64
+ end
65
+
66
+ def empty?
67
+ if data_array?
68
+ data.all?(&:empty?)
69
+ else
70
+ data.respond_to?(:empty?) ? !!data.empty? : !data
71
+ end
72
+ end
73
+
74
+ def branch?
75
+ children.any?
76
+ end
77
+
78
+ def child_branches
79
+ children.select(&:branch?)
80
+ end
81
+
82
+ def twig?
83
+ !leaf? && children.all?(&:leaf?)
84
+ end
85
+
86
+ def leaf?
87
+ children.none?
88
+ end
89
+
90
+ def leaf_labels
91
+ return [] if leaf?
92
+ return [] if children.none?
93
+ return children.map(&:label) if twig?
94
+ children.map(&:leaf_labels).flatten.uniq
95
+ end
96
+
97
+ def filter(leaf_label_id = nil, keep_leafs: false, orphan_strategy: :destroy, &block)
98
+ fail ArgumentError, 'No block and no leaf_label_id passed' if !block_given? && leaf_label_id.nil?
99
+ self.data.each_with_object(DataSet.new(id: id, label: label.deep_dup)) do |set, parent_set|
100
+ if block_given?
101
+ condition_met = yield(parent_set, set)
102
+ else
103
+ condition_met = set.leaf? ? (set.label.id == leaf_label_id) : nil
104
+ end
105
+
106
+ if condition_met == true
107
+ parent_set.add_item(set.deep_dup)
108
+ elsif condition_met.nil? && set.data_array?
109
+ deep_filter_result = set.deep_dup.filter(leaf_label_id, keep_leafs: keep_leafs, orphan_strategy: orphan_strategy, &block)
110
+ parent_set.add_item(deep_filter_result) if deep_filter_result.data
111
+ elsif condition_met == false && set.data_array?
112
+ if orphan_strategy == :adopt
113
+ deep_filter_result = set.deep_dup.filter(leaf_label_id, keep_leafs: keep_leafs, orphan_strategy: orphan_strategy, &block)
114
+ deep_filter_result.children.each { |c| parent_set.add_item(c) } if deep_filter_result.data
115
+ end
116
+ elsif condition_met.nil? && set.leaf?
117
+ parent_set.add_item(set) if keep_leafs
118
+ end
119
+
120
+ parent_set
121
+ end
122
+ end
123
+
124
+ def find(subtree_id)
125
+ if id == subtree_id
126
+ self
127
+ elsif data_array?
128
+ data.each do |child|
129
+ match = child.find(subtree_id)
130
+ return match if match
131
+ end
132
+
133
+ nil
134
+ end
135
+ end
136
+
137
+ def <=>(other)
138
+ return 0 if data == other.data && label == other.label
139
+ return 1 if data && other.data.nil?
140
+ return -1 if data.nil? && other.data
141
+ return label.id <=> other.label.id if label.id <=> other.label.id
142
+ return data <=> other.data if data.is_a?(Numeric) && other.data.is_a?(Numeric)
143
+ data.size <=> other.data.size
144
+ end
145
+
146
+ # == is used for comparison of two instances directly
147
+ def ==(other)
148
+ other_data = other.data.nil? ? nil : (other.data.is_a?(Enumerable) ? other.data.sort : other.data )
149
+ own_data = data.nil? ? nil : (data.is_a?(Enumerable) ? data.sort : data )
150
+ other.label == label && other_data == own_data
151
+ end
152
+
153
+ # eql? and hash are both used for comparisons when you
154
+ # call `.uniq` on an array of data sets.
155
+ def eql?(other)
156
+ self == other
157
+ end
158
+
159
+ def hash
160
+ "#{data.hash}#{label.hash}".to_i
161
+ end
162
+
163
+ def deep_dup
164
+ new_set = DataSet.new(id: id)
165
+ new_set.label = DataSet::Label.new(dup_value(label.name), id: dup_value(label.id), meta: label.meta.marshal_dump) if label
166
+
167
+ if data_array?
168
+ new_set.data = children.map(&:deep_dup)
169
+ else
170
+ new_set.data = data
171
+ end
172
+
173
+ new_set
174
+ end
175
+
176
+ def value
177
+ fail NotImplementedError, 'DataSet is not a leaf and thus has no value' unless leaf?
178
+ data
179
+ end
180
+
181
+ def reduce
182
+ yield reducable_values
183
+ end
184
+
185
+ def sum
186
+ values = [reducable_values].flatten.compact
187
+ return nil if values.empty?
188
+ values.inject(&:+)
189
+ end
190
+
191
+ def average
192
+ values = [reducable_values].flatten.compact
193
+ return nil if values.empty?
194
+ (values.inject(&:+).to_f / values.size).to_f
195
+ end
196
+
197
+ def reducable_values(set = nil)
198
+ set ||= self
199
+ return set.children.map { |c| reducable_values(c) } if set.data_array?
200
+ set.data
201
+ end
202
+
203
+ def combine(other_data_set, operator)
204
+ combined_data_set = dup
205
+ if data_array? && other_data_set.data_array?
206
+ combined_data_set.data = combine_data_array(other_data_set.children, operator)
207
+ elsif !data_array? && !other_data_set.data_array?
208
+ combined_data_set.data = combine_data_value(other_data_set.value, operator)
209
+ else
210
+ fail ArgumentError, "Can't combine array DataSet with value DataSet"
211
+ end
212
+ combined_data_set
213
+ end
214
+
215
+ def data_array?
216
+ data.is_a? Array
217
+ end
218
+
219
+ def pretty_inspect(level = 0, final_s: '')
220
+ prefix = ''
221
+
222
+ if root?
223
+ prefix << '* '
224
+ else
225
+ prefix << (' ' * (level) * 3) + '|- '
226
+ end
227
+
228
+ if label
229
+ node_s = "#{prefix}#{label.name}"
230
+ else
231
+ node_s = "#{prefix} (no label)"
232
+ end
233
+ node_s += ": #{value}" if leaf?
234
+ final_s += "#{node_s} \n"
235
+
236
+ return final_s if children.none?
237
+
238
+ children.each { |c| final_s << c.pretty_inspect(level + 1) }
239
+ final_s
240
+ end
241
+
242
+ def each(&block)
243
+ yield self
244
+ children.each { |member| member.each(&block) } if data_array?
245
+ self
246
+ end
247
+
248
+ private
249
+
250
+ def dup_value(value)
251
+ value.is_a?(Numeric) ? value : value.dup
252
+ end
253
+
254
+ def combine_data_array(other_children, operator)
255
+ other_children = other_children.dup
256
+ result = children.map do |child|
257
+ other_child = other_children.find { |ods| ods.label == child.label }
258
+ if other_child
259
+ other_children.delete(other_child)
260
+ child.combine(other_child, operator)
261
+ else
262
+ child
263
+ end
264
+ end
265
+ result += other_children # The remaining other children (without matching child in this data set)
266
+ result
267
+ end
268
+
269
+ def combine_data_value(other_value, operator)
270
+ own_value = value
271
+
272
+ # Always return nil if both values are nil (prevents summed data sets of being wrongly considered unempty and thus not hidden)
273
+ return nil if own_value.nil? && other_value.nil?
274
+
275
+ case operator.to_sym
276
+ when :+, :-
277
+ # When adding or subtracting treat nil (unknown) values as zero, instead of returning nil as summation result
278
+ own_value ||= 0.0
279
+ other_value ||= 0.0
280
+ when :/
281
+ # Prevent division by zero resulting in NaN/Infinity values
282
+ other_value = nil if other_value&.zero?
283
+ end
284
+
285
+ return nil if own_value.nil? || other_value.nil?
286
+ own_value.send(operator, other_value)
287
+ end
288
+ end
289
+
290
+ class CannotAddToNonEnumerableData < StandardError
291
+ def initialize(data = nil)
292
+ @data = data
293
+ end
294
+ end
@@ -0,0 +1,39 @@
1
+ require 'ostruct'
2
+
3
+ class DataSet
4
+ class Label
5
+ attr_accessor :id
6
+ attr_accessor :name
7
+ attr_accessor :meta
8
+
9
+ def initialize(name, id: nil, meta: {})
10
+ @name = name
11
+ @id = id || name
12
+ fail ArgumentError, 'DataSet::Label meta must be a Hash' unless meta.is_a?(Hash)
13
+ @meta = OpenStruct.new(meta)
14
+ end
15
+
16
+ # == is used for comparison of two instances directly
17
+ def ==(other)
18
+ other.id == id
19
+ end
20
+
21
+ # eql? and hash are both used for comparisons when you
22
+ # call `.uniq` on an array of labels.
23
+ def eql?(other)
24
+ self == other
25
+ end
26
+
27
+ def hash
28
+ id.hash
29
+ end
30
+
31
+ def deep_dup
32
+ label = dup
33
+ id = id.dup if id
34
+ name = name.dup if name
35
+ meta = meta.dup if meta
36
+ label
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Tukey
2
+ VERSION = "0.9.0RC"
3
+ end
data/tukey.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tukey/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tukey"
8
+ spec.version = Tukey::VERSION
9
+ spec.authors = ["Achilleas Buisman"]
10
+ spec.email = ["tukey@abuisman.nl"]
11
+
12
+ spec.summary = "DataSets for putting data in a tree of sets"
13
+ spec.description = "Tukey is provides DataSets which can be put in a tree. This way you can store partial results of calculations or other data and, for example, create charts, tables or other presentations."
14
+ spec.homepage = "https://github.com/abuisman/tukey"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tukey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0RC
5
+ platform: ruby
6
+ authors:
7
+ - Achilleas Buisman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Tukey is provides DataSets which can be put in a tree. This way you can
56
+ store partial results of calculations or other data and, for example, create charts,
57
+ tables or other presentations.
58
+ email:
59
+ - tukey@abuisman.nl
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - ".travis.yml"
67
+ - Gemfile
68
+ - John_Tukey.jpg
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - Tukey.gif
73
+ - bin/console
74
+ - bin/setup
75
+ - lib/tukey.rb
76
+ - lib/tukey/data_set.rb
77
+ - lib/tukey/data_set/label.rb
78
+ - lib/tukey/version.rb
79
+ - tukey.gemspec
80
+ homepage: https://github.com/abuisman/tukey
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">"
96
+ - !ruby/object:Gem::Version
97
+ version: 1.3.1
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.1
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: DataSets for putting data in a tree of sets
104
+ test_files: []