Class: FlatKit::StatType::NominalStats

Inherits:
FlatKit::StatType show all
Defined in:
lib/flat_kit/stat_type/nominal_stats.rb

Overview

Internal: Status object to keep track of the count and frequency of values.

Direct Known Subclasses

NumericalStats, OrdinalStats

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FlatKit::StatType

for, nominal_types, numerical_types, ordinal_types, #to_hash, #to_json

Constructor Details

#initialize(collecting_frequencies: false) ⇒ NominalStats

Returns a new instance of NominalStats.



18
19
20
21
22
23
24
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 18

def initialize(collecting_frequencies: false)
  super()
  @mutex = Mutex.new
  @count = 0
  @collecting_frequencies = collecting_frequencies
  @frequencies = Hash.new(0)
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



8
9
10
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 8

def count
  @count
end

Class Method Details

.all_statsObject



14
15
16
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 14

def self.all_stats
  @all_stats ||= %w[count unique_count unique_values mode]
end

.default_statsObject



10
11
12
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 10

def self.default_stats
  @default_stats ||= %w[count]
end

Instance Method Details

#collected_statsObject



26
27
28
29
30
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 26

def collected_stats
  return self.class.default_stats unless @collecting_frequencies

  self.class.all_stats
end

#frequenciesObject



50
51
52
53
54
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 50

def frequencies
  return nil unless @collecting_frequencies

  @frequencies
end

#modeObject



32
33
34
35
36
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 32

def mode
  return nil unless @collecting_frequencies

  @frequencies.max_by { |_item, item_count| item_count }.first
end

#unique_countObject



38
39
40
41
42
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 38

def unique_count
  return nil unless @collecting_frequencies

  @frequencies.size
end

#unique_valuesObject



44
45
46
47
48
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 44

def unique_values
  return nil unless @collecting_frequencies

  @frequencies.keys
end

#update(value) ⇒ Object



56
57
58
59
60
61
# File 'lib/flat_kit/stat_type/nominal_stats.rb', line 56

def update(value)
  @mutex.synchronize do
    @count += 1
    @frequencies[value] += 1 if @collecting_frequencies
  end
end