Class: SpatialStats::Weights::WeightsMatrix
- Inherits:
-
Object
- Object
- SpatialStats::Weights::WeightsMatrix
- 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
-
#keys ⇒ Object
Returns the value of attribute keys.
-
#n ⇒ Object
Returns the value of attribute n.
-
#weights ⇒ Object
Returns the value of attribute weights.
Instance Method Summary collapse
-
#dense ⇒ Numo::DFloat
Compute the n x n Numo::Narray of the weights hash.
-
#initialize(weights) ⇒ WeightsMatrix
constructor
A new instance of WeightsMatrix.
-
#sparse ⇒ CSRMatrix
Compute the CSR representation of the weights.
-
#standardize ⇒ WeightsMatrix
Row standardized version of the weights matrix.
-
#wc ⇒ Array
Compute the cardinalities of each neighbor into an array.
-
#window ⇒ WeightsMatrix
Windowed version of the weights matrix.
Constructor Details
#initialize(weights) ⇒ WeightsMatrix
A new instance of WeightsMatrix
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
#keys ⇒ Object
Returns the value of attribute keys.
22 23 24 |
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 22 def keys @keys end |
#n ⇒ Object
Returns the value of attribute n.
22 23 24 |
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 22 def n @n end |
#weights ⇒ Object
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
#dense ⇒ Numo::DFloat
Compute the n x n Numo::Narray of the weights hash.
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 |
#sparse ⇒ CSRMatrix
Compute the CSR representation of the weights.
57 58 59 |
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 57 def sparse @sparse ||= CSRMatrix.new(weights, n) end |
#standardize ⇒ WeightsMatrix
Row standardized version of the weights matrix. Will return a new version of the weights matrix with standardized weights.
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 |
#wc ⇒ Array
Compute the cardinalities of each neighbor into an 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 |
#window ⇒ WeightsMatrix
Windowed version of the weights matrix. If a row already has an entry for itself, it will be skipped.
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 |