Class: MoreMath::Histogram
Overview
Represents a histogram for visualizing data distributions
The Histogram class provides functionality to create and display histograms from sequences of numerical data. It divides the data into bins and counts how many elements fall into each bin, then displays this information in a readable format with optional UTF-8 bar characters.
Defined Under Namespace
Classes: Bin
Instance Attribute Summary collapse
-
#bins ⇒ Integer
readonly
Number of bins for this Histogram.
Instance Method Summary collapse
-
#counts ⇒ Array<Integer>
Get an array of counts from each bin.
-
#display(output = $stdout, width = 65) ⇒ self
Display this histogram to
outputusingwidthcolumns. -
#each_bin {|Bin| ... } ⇒ Array<Bin>
Iterate over each bin in the histogram.
-
#initialize(sequence, arg = 10) ⇒ Histogram
constructor
Create a Histogram for the elements of
sequencewithbinsbins. -
#max_count ⇒ Integer
Get the maximum count in any bin.
-
#terminal_width ⇒ Integer
Get terminal width using Tins::Terminal.
-
#to_a ⇒ Array<Bin>
Return the computed histogram as an array of Bin objects.
Constructor Details
#initialize(sequence, arg = 10) ⇒ Histogram
Create a Histogram for the elements of sequence with bins bins.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/more_math/histogram.rb', line 35 def initialize(sequence, arg = 10) @with_counts = false if arg.is_a?(Hash) bins = arg.fetch(:bins, 10) wc = arg[:with_counts] and @with_counts = wc else bins = arg end @sequence = sequence @bins = bins @result = compute end |
Instance Attribute Details
#bins ⇒ Integer (readonly)
Number of bins for this Histogram.
51 52 53 |
# File 'lib/more_math/histogram.rb', line 51 def bins @bins end |
Instance Method Details
#counts ⇒ Array<Integer>
Get an array of counts from each bin.
71 72 73 |
# File 'lib/more_math/histogram.rb', line 71 def counts each_bin.map(&:count) end |
#display(output = $stdout, width = 65) ⇒ self
Display this histogram to output using width columns. Raises ArgumentError if width < 15.
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/more_math/histogram.rb', line 82 def display(output = $stdout, width = 65) if width.is_a?(String) && width =~ /(.+)%\z/ percentage = Float($1).clamp(0, 100) width = (terminal_width * (percentage / 100.0)).floor end width > 15 or raise ArgumentError, "width needs to be >= 15" for r in rows output << output_row(r, width) end output << "max_count=#{max_count}\n" self end |
#each_bin {|Bin| ... } ⇒ Array<Bin>
Iterate over each bin in the histogram.
64 65 66 |
# File 'lib/more_math/histogram.rb', line 64 def each_bin(&block) @result.each(&block) end |
#max_count ⇒ Integer
Get the maximum count in any bin.
105 106 107 |
# File 'lib/more_math/histogram.rb', line 105 def max_count counts.max end |
#terminal_width ⇒ Integer
Get terminal width using Tins::Terminal.
98 99 100 |
# File 'lib/more_math/histogram.rb', line 98 def terminal_width Tins::Terminal.columns end |
#to_a ⇒ Array<Bin>
Return the computed histogram as an array of Bin objects.
56 57 58 |
# File 'lib/more_math/histogram.rb', line 56 def to_a @result end |