<commit_before>class SegmentCalculations
  attr_accessor :times, :valid_data_array, :low_bad, :low_q, :high_q, :high_bad, :mean, :std

  def initialize(segment, begin_times_hash = nil, end_times_hash = nil)
    begin_times_hash ||= segment.begin_split.time_hash(segment.begin_bitkey)
    end_times_hash ||= segment.end_split.time_hash(segment.end_bitkey)
    @times = calculate_times(begin_times_hash, end_times_hash)
    create_valid_data_array
    set_status_limits(segment)
  end

  def status(value)
    return nil unless value
    if (value < low_bad) | (value > high_bad)
      'bad'
    elsif (value < low_q) | (value > high_q)
      'questionable'
    else
      'good'
    end
  end

  def limits
    [low_bad, low_q, high_q, high_bad]
  end

  private

  def calculate_times(begin_hash, end_hash)
    b = begin_hash.dup
    e = end_hash.dup
    b.keep_if { |k, _| e.keys.include?(k) }
    e.keep_if { |k, _| b.keys.include?(k) }
    e.merge(b) { |_, x, y| x - y }
  end

  def create_valid_data_array
    baseline_median = times.median
    return [] unless baseline_median
    data_array = times.values
    data_array.keep_if { |v| (v > (baseline_median / 2)) && (v < (baseline_median * 2)) }
    self.valid_data_array = data_array
  end

  def set_status_limits(segment)
    if segment.end_split.start?
      self.low_bad = 0
      self.low_q = 0
      self.high_q = 0
      self.high_bad = 0
    elsif segment.distance == 0 # Time within a waypoint group/aid station
      self.low_bad = 0
      self.low_q = 0
      self.high_q = 6.hours
      self.high_bad = 1.day
    else # This is a "real" segment between waypoint groups
      set_limits_by_terrain(segment)
      set_limits_by_stats(valid_data_array)
    end
  end

  def set_limits_by_terrain(segment)
    typical_time = segment.typical_time_by_terrain
    self.low_bad = typical_time / 5
    self.low_q = typical_time / 3.5
    self.high_q = typical_time * 3.5
    self.high_bad = typical_time * 5
  end

  def set_limits_by_stats(data_array)
    return unless data_array && data_array.count > 2
    self.mean = data_array.mean
    self.std = data_array.standard_deviation
    self.low_bad = [self.low_bad, mean - (4 * std), 0].max
    self.low_q = [self.low_q, mean - (3 * std), 0].max
    self.high_q = [self.high_q, mean + (4 * std)].min
    self.high_bad = [self.high_bad, mean + (10 * std)].min

  end

end<commit_msg>Changed threshold for using statistical data status calculation from 2 to 20; added constant.<commit_after>class SegmentCalculations
  attr_accessor :times, :valid_data_array, :low_bad, :low_q, :high_q, :high_bad, :mean, :std

  STAT_CALC_THRESHOLD = 20

  def initialize(segment, begin_times_hash = nil, end_times_hash = nil)
    begin_times_hash ||= segment.begin_split.time_hash(segment.begin_bitkey)
    end_times_hash ||= segment.end_split.time_hash(segment.end_bitkey)
    @times = calculate_times(begin_times_hash, end_times_hash)
    create_valid_data_array
    set_status_limits(segment)
  end

  def status(value)
    return nil unless value
    if (value < low_bad) | (value > high_bad)
      'bad'
    elsif (value < low_q) | (value > high_q)
      'questionable'
    else
      'good'
    end
  end

  def limits
    [low_bad, low_q, high_q, high_bad]
  end

  private

  def calculate_times(begin_hash, end_hash)
    b = begin_hash.dup
    e = end_hash.dup
    b.keep_if { |k, _| e.keys.include?(k) }
    e.keep_if { |k, _| b.keys.include?(k) }
    e.merge(b) { |_, x, y| x - y }
  end

  def create_valid_data_array
    baseline_median = times.median
    return [] unless baseline_median
    data_array = times.values
    data_array.keep_if { |v| (v > (baseline_median / 2)) && (v < (baseline_median * 2)) }
    self.valid_data_array = data_array
  end

  def set_status_limits(segment)
    if segment.end_split.start?
      self.low_bad = 0
      self.low_q = 0
      self.high_q = 0
      self.high_bad = 0
    elsif segment.distance == 0 # Time within a waypoint group/aid station
      self.low_bad = 0
      self.low_q = 0
      self.high_q = 6.hours
      self.high_bad = 1.day
    else # This is a "real" segment between waypoint groups
      set_limits_by_terrain(segment)
      set_limits_by_stats(valid_data_array)
    end
  end

  def set_limits_by_terrain(segment)
    typical_time = segment.typical_time_by_terrain
    self.low_bad = typical_time / 5
    self.low_q = typical_time / 3.5
    self.high_q = typical_time * 3.5
    self.high_bad = typical_time * 5
  end

  def set_limits_by_stats(data_array)
    return unless data_array && data_array.count > STAT_CALC_THRESHOLD
    self.mean = data_array.mean
    self.std = data_array.standard_deviation
    self.low_bad = [self.low_bad, mean - (4 * std), 0].max
    self.low_q = [self.low_q, mean - (3 * std), 0].max
    self.high_q = [self.high_q, mean + (4 * std)].min
    self.high_bad = [self.high_bad, mean + (10 * std)].min

  end

end
{'fraction_non_alphanumeric': 0.10276760276760277, 'fraction_numerical': 0.01098901098901099, 'mean_word_length': 3.0586292320396367, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7642063', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1795, 'n_words': 394}