Class: SpatialStats::Weights::WeightsMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/spatial_stats/weights/weights_matrix.rb

Overview

WeightsMatrix class is used to store spatial weights and related information in various formats.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(weights) ⇒ WeightsMatrix

A new instance of WeightsMatrix

Parameters:

  • weights (Hash)

    hash of format {key: [{id: neighbor_key, weight: 1}]} that describe the relations between neighbors



17
18
19
20
21
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 17

def initialize(weights)
  @weights = weights
  @keys = weights.keys
  @n = keys.size
end

Instance Attribute Details

#keysObject

Returns the value of attribute keys.



22
23
24
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 22

def keys
  @keys
end

#nObject

Returns the value of attribute n.



22
23
24
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 22

def n
  @n
end

#weightsObject

Returns the value of attribute weights.



22
23
24
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 22

def weights
  @weights
end

Instance Method Details

#==(other) ⇒ TrueClass, FalseClass

Equality operator

Parameters:

Returns:

  • (TrueClass, FalseClass)

    equality result



30
31
32
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 30

def ==(other)
  weights == other.weights
end

#denseNumo::DFloat

Compute the n x n Numo::Narray of the weights hash.

Examples:

hash = {1 => [{id: 2, weight: 1}], 2 => [{id: 1, weight: 1},
    {id: 3, weight: 1}], 3 => [{id: 2, weight: 1}]}
wm = WeightsMatrix.new(hash.keys, hash)
wm.full
# => Numo::DFloat[[0, 1, 0], [1, 0, 1], [0, 1, 0]]

Returns:

  • (Numo::DFloat)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 45

def dense
  @dense ||= begin
    mat = Numo::DFloat.zeros(n, n)
    keys.each_with_index do |key, i|
      neighbors = weights[key]
      neighbors.each do |neighbor|
        j = keys.index(neighbor[:id])
        weight = neighbor[:weight]

        # assign the weight to row and column
        mat[i, j] = weight
      end
    end

    mat
  end
end

#sparseCSRMatrix

Compute the CSR representation of the weights.

Returns:



67
68
69
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 67

def sparse
  @sparse ||= CSRMatrix.new(weights, n)
end

#standardizeWeightsMatrix

Row standardized version of the weights matrix. Will return a new version of the weights matrix with standardized weights.

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 90

def standardize
  new_weights = weights

  new_weights.transform_values do |neighbors|
    sum = neighbors.reduce(0.0) { |acc, neighbor| acc + neighbor[:weight] }

    neighbors.map do |neighbor|
      hash = neighbor
      hash[:weight] /= sum
    end
  end

  self.class.new(new_weights)
end

#wcArray

Compute the cardinalities of each neighbor into an array

Returns:

  • (Array)


75
76
77
78
79
80
81
82
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 75

def wc
  @wc ||= begin
    row_index = sparse.row_index
    (0..n - 1).map do |idx|
      row_index[idx + 1] - row_index[idx]
    end
  end
end

#windowWeightsMatrix

Windowed version of the weights matrix. If a row already has an entry for itself, it will be skipped.

Returns:



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 110

def window
  new_weights = weights

  new_weights.each do |key, neighbors|
    unless neighbors.find { |neighbor| neighbor[:id] == key }
      new_neighbors = (neighbors << { id: key, weight: 1 })
      new_weights[key] = new_neighbors.sort_by { |neighbor| neighbor[:id] }
    end
  end

  self.class.new(new_weights)
end