Class: DTW::Bands::Slanted

Inherits:
Data
  • Object
show all
Defined in:
lib/dtwrb/bands.rb,
sig/dtwrb.rbs

Overview

The band follows the diagonal of the rectangle, so the radius keeps constraining the path when the two sequences differ in length.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ratio:) ⇒ Slanted

Returns a new instance of Slanted.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
# File 'lib/dtwrb/bands.rb', line 27

def initialize(ratio:)
  width = Float(ratio)
  raise ArgumentError, "band ratio must be non-negative, got #{width}" if width.negative?

  super(ratio: width)
end

Instance Attribute Details

#ratioObject (readonly)

Returns the value of attribute ratio

Returns:

  • (Object)

    the current value of ratio



26
27
28
# File 'lib/dtwrb/bands.rb', line 26

def ratio
  @ratio
end

Class Method Details

.newSlanted

Parameters:

  • ratio: (::Numeric)

Returns:



128
# File 'sig/dtwrb.rbs', line 128

def self.new: (ratio: ::Numeric) -> Slanted

Instance Method Details

#bounds(row, rows, columns) ⇒ [::Integer, ::Integer]

Parameters:

  • (::Integer)
  • (::Integer)
  • (::Integer)

Returns:

  • ([::Integer, ::Integer])


38
39
40
41
42
43
44
# File 'lib/dtwrb/bands.rb', line 38

def bounds(row, rows, columns)
  return [1, columns] if rows < 2 || columns < 2

  width = radius(rows, columns)
  centre = 1.0 + ((row - 1) * (columns - 1).fdiv(rows - 1))
  [(centre - width).ceil, (centre + width).floor]
end

#radius(rows, columns) ⇒ ::Integer

Parameters:

  • (::Integer)
  • (::Integer)

Returns:

  • (::Integer)


34
35
36
# File 'lib/dtwrb/bands.rb', line 34

def radius(rows, columns)
  [(ratio * [rows, columns].max).ceil, reachable(rows, columns)].max
end

#reachable(rows, columns) ⇒ ::Integer

Half a step of the diagonal, below which consecutive rows no longer overlap.

Parameters:

  • (::Integer)
  • (::Integer)

Returns:

  • (::Integer)


47
48
49
50
51
# File 'lib/dtwrb/bands.rb', line 47

def reachable(rows, columns)
  return 1 if rows < 2 || columns < 2

  [((columns - 1).fdiv(rows - 1) / 2).ceil, 1].max
end

#to_h::Hash[::Symbol, untyped]

Returns:

  • (::Hash[::Symbol, untyped])


133
# File 'sig/dtwrb.rbs', line 133

def to_h: () -> ::Hash[::Symbol, untyped]

#withSlanted

Parameters:

  • (Object)

Returns:



132
# File 'sig/dtwrb.rbs', line 132

def with: (**untyped) -> Slanted