Class: MoreMath::Histogram

Inherits:
Object show all
Defined in:
lib/more_math/histogram.rb

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.

Examples:

Creating a histogram

sequence = [1, 2, 3, 4, 5, 1]
hist = Histogram.new(sequence, bins: 3)

Displaying a histogram

hist.display($stdout, 80)

Defined Under Namespace

Classes: Bin

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sequence, arg = 10) ⇒ Histogram

Create a Histogram for the elements of sequence with bins bins.

Parameters:

  • sequence (Enumerable)

    The sequence to build the histogram from

  • arg (Integer, Hash) (defaults to: 10)

    Number of bins or hash with options like ‘:bins` and `:with_counts`

Options Hash (arg):

  • :bins (Integer) — default: 10

    Number of bins to use

  • :with_counts (Boolean) — default: false

    Whether to display counts in output



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

#binsInteger (readonly)

Number of bins for this Histogram.

Returns:

  • (Integer)


51
52
53
# File 'lib/more_math/histogram.rb', line 51

def bins
  @bins
end

Instance Method Details

#countsArray<Integer>

Get an array of counts from each bin.

Returns:

  • (Array<Integer>)


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.

Parameters:

  • output (IO) (defaults to: $stdout)

    The output stream to write to (default: $stdout)

  • width (Integer, String) (defaults to: 65)

    Width of the display; can be a percentage string like “90%”

Returns:

  • (self)

Raises:

  • (ArgumentError)

    If width is less than 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.

Yields:

  • (Bin)

    each bin

Returns:



64
65
66
# File 'lib/more_math/histogram.rb', line 64

def each_bin(&block)
  @result.each(&block)
end

#max_countInteger

Get the maximum count in any bin.

Returns:

  • (Integer)


105
106
107
# File 'lib/more_math/histogram.rb', line 105

def max_count
  counts.max
end

#terminal_widthInteger

Get terminal width using Tins::Terminal.

Returns:

  • (Integer)


98
99
100
# File 'lib/more_math/histogram.rb', line 98

def terminal_width
  Tins::Terminal.columns
end

#to_aArray<Bin>

Return the computed histogram as an array of Bin objects.

Returns:



56
57
58
# File 'lib/more_math/histogram.rb', line 56

def to_a
  @result
end