Class: Diffux::DiffClusterFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/diffux_core/diff_cluster_finder.rb

Overview

This class finds clusters in a diff. A cluster is defined as rows that are different, and are closer than DIFF_ROW_THRESHOLD pixels to its neighboring diff row.

Constant Summary collapse

MAXIMUM_ADJACENCY_GAP =
5

Instance Method Summary collapse

Constructor Details

#initialize(number_of_rows) ⇒ DiffClusterFinder

Returns a new instance of DiffClusterFinder.

Parameters:

  • number_of_rows (Numeric)


10
11
12
13
# File 'lib/diffux_core/diff_cluster_finder.rb', line 10

def initialize(number_of_rows)
  @number_of_rows = number_of_rows
  @rows_with_diff = SortedSet.new
end

Instance Method Details

#clustersArray<Hash>

Calculate clusters from diff-rows that are close to each other.

Returns:

  • (Array<Hash>)

    a list of clusters modeled as hashes: ‘{ start: x, finish: y }`



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/diffux_core/diff_cluster_finder.rb', line 31

def clusters
  results = []
  @rows_with_diff.each do |row|
    current = results.last
    if !current || current[:finish] + MAXIMUM_ADJACENCY_GAP < row
      results << {
        start:  row,
        finish: row,
      }
    else
      current[:finish] = row
    end
  end
  results
end

#percent_of_rows_differentFloat

Returns the percent of rows that are different.

Returns:

  • (Float)

    the percent of rows that are different



23
24
25
# File 'lib/diffux_core/diff_cluster_finder.rb', line 23

def percent_of_rows_different
  @rows_with_diff.length.to_f / @number_of_rows * 100
end

#row_is_different(row) ⇒ Object

Tell the DiffClusterFinder about a row that is different.

Parameters:

  • row (Numeric)


18
19
20
# File 'lib/diffux_core/diff_cluster_finder.rb', line 18

def row_is_different(row)
  @rows_with_diff.add row
end