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
-
#==(other) ⇒ TrueClass, FalseClass
Equality operator.
-
#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
#==(other) ⇒ TrueClass, FalseClass
Equality operator
30 31 32 |
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 30 def ==(other) weights == other.weights end |
#dense ⇒ Numo::DFloat
Compute the n x n Numo::Narray of the weights hash.
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 |
#sparse ⇒ CSRMatrix
Compute the CSR representation of the weights.
67 68 69 |
# File 'lib/spatial_stats/weights/weights_matrix.rb', line 67 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.
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 |
#wc ⇒ Array
Compute the cardinalities of each neighbor into an 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 |
#window ⇒ WeightsMatrix
Windowed version of the weights matrix. If a row already has an entry for itself, it will be skipped.
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 |