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

#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)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 35

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:



57
58
59
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 57

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:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 80

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)


65
66
67
68
69
70
71
72
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 65

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:



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 100

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