spatial_stats 0.2.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,13 +11,12 @@ module SpatialStats
11
11
  ##
12
12
  # A new instance of WeightsMatrix
13
13
  #
14
- # @param [Array] keys ordered list of keys used in weights
15
- # @param [Hash] weights hash of format +{key: [{j_id: neighbor_key, weight: 1}]}+ that describe the relations between neighbors
14
+ # @param [Hash] weights hash of format +{key: [{id: neighbor_key, weight: 1}]}+ that describe the relations between neighbors
16
15
  #
17
16
  # @return [WeightsMatrix]
18
- def initialize(keys, weights)
19
- @keys = keys
17
+ def initialize(weights)
20
18
  @weights = weights
19
+ @keys = weights.keys
21
20
  @n = keys.size
22
21
  end
23
22
  attr_accessor :keys, :weights, :n
@@ -26,41 +25,76 @@ module SpatialStats
26
25
  # Compute the n x n Numo::Narray of the weights hash.
27
26
  #
28
27
  # @example
29
- # hash = {1 => [{j_id: 2, weight: 1}], 2 => [{j_id: 1, weight: 1},
30
- # {j_id: 3, weight: 1}], 3 => [{j_id: 2, weight: 1}]}
28
+ # hash = {1 => [{id: 2, weight: 1}], 2 => [{id: 1, weight: 1},
29
+ # {id: 3, weight: 1}], 3 => [{id: 2, weight: 1}]}
31
30
  # wm = WeightsMatrix.new(hash.keys, hash)
32
31
  # wm.full
33
32
  # # => Numo::DFloat[[0, 1, 0], [1, 0, 1], [0, 1, 0]]
34
33
  #
35
34
  # @return [Numo::DFloat]
36
- def full
37
- # returns a square matrix Wij using @keys as the order of items
38
- @full ||= begin
39
- rows = []
40
- @keys.each do |i|
41
- # iterate through each key to get the data for the row
42
- row = @keys.map do |j|
43
- neighbors = @weights[i]
44
- match = neighbors.find { |neighbor| neighbor[:j_id] == j }
45
- if match
46
- match[:weight]
47
- else
48
- 0
49
- end
35
+ def dense
36
+ @dense ||= begin
37
+ mat = Numo::DFloat.zeros(n, n)
38
+ keys.each_with_index do |key, i|
39
+ neighbors = weights[key]
40
+ neighbors.each do |neighbor|
41
+ j = keys.index(neighbor[:id])
42
+ weight = neighbor[:weight]
43
+
44
+ # assign the weight to row and column
45
+ mat[i, j] = weight
50
46
  end
51
- rows << row
52
47
  end
53
48
 
54
- Numo::DFloat.cast(rows)
49
+ mat
55
50
  end
56
51
  end
57
52
 
58
53
  ##
59
- # Row standardized version of +#full+
54
+ # Compute the CSR representation of the weights.
60
55
  #
61
- # @return [Numo::DFloat]
62
- def standardized
63
- @standardized ||= full.row_standardized
56
+ # @return [CSRMatrix]
57
+ def sparse
58
+ @sparse ||= CSRMatrix.new(dense.to_a.flatten, n, n)
59
+ end
60
+
61
+ ##
62
+ # Row standardized version of the weights matrix.
63
+ # Will return a new version of the weights matrix with standardized
64
+ # weights.
65
+ #
66
+ # @return [WeightsMatrix]
67
+ def standardize
68
+ new_weights = weights
69
+
70
+ new_weights.transform_values do |neighbors|
71
+ sum = neighbors.reduce(0.0) { |acc, neighbor| acc + neighbor[:weight] }
72
+
73
+ neighbors.map do |neighbor|
74
+ hash = neighbor
75
+ hash[:weight] /= sum
76
+ end
77
+ end
78
+
79
+ self.class.new(new_weights)
80
+ end
81
+
82
+ ##
83
+ # Windowed version of the weights matrix.
84
+ # If a row already has an entry for itself, it will be skipped.
85
+ #
86
+ # @return [WeightsMatrix]
87
+ def window
88
+ new_weights = weights
89
+
90
+ new_weights.each do |key, neighbors|
91
+ unless neighbors.find { |neighbor| neighbor[:id] == key }
92
+ new_neighbors = (neighbors << { id: key, weight: 1 })
93
+ new_weights[key] = new_neighbors.sort_by { |neighbor| neighbor[:id] }
94
+ end
95
+ end
96
+
97
+ self.class.new(new_weights)
64
98
  end
65
99
  end
66
100
  end
data/lib/spatial_stats.rb CHANGED
@@ -6,6 +6,7 @@ require 'spatial_stats/global'
6
6
  require 'spatial_stats/local'
7
7
  require 'spatial_stats/narray_ext'
8
8
  require 'spatial_stats/queries'
9
+ require 'spatial_stats/spatial_stats' # c extensions
9
10
  require 'spatial_stats/utils'
10
11
  require 'spatial_stats/weights'
11
12
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spatial_stats
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Doggett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-03 00:00:00.000000000 Z
11
+ date: 2020-04-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake-compiler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.1.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.1.0
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: ruby-prof
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -115,12 +129,17 @@ description: An ActiveRecord/PostGIS extension that provides statistical methods
115
129
  email:
116
130
  - keith.doggett887@gmail.com
117
131
  executables: []
118
- extensions: []
132
+ extensions:
133
+ - ext/spatial_stats/extconf.rb
119
134
  extra_rdoc_files: []
120
135
  files:
121
136
  - MIT-LICENSE
122
137
  - README.md
123
138
  - Rakefile
139
+ - ext/spatial_stats/csr_matrix.c
140
+ - ext/spatial_stats/csr_matrix.h
141
+ - ext/spatial_stats/extconf.rb
142
+ - ext/spatial_stats/spatial_stats.c
124
143
  - lib/spatial_stats.rb
125
144
  - lib/spatial_stats/enumerable_ext.rb
126
145
  - lib/spatial_stats/global.rb
@@ -139,6 +158,7 @@ files:
139
158
  - lib/spatial_stats/queries/variables.rb
140
159
  - lib/spatial_stats/queries/weights.rb
141
160
  - lib/spatial_stats/railtie.rb
161
+ - lib/spatial_stats/spatial_stats.so
142
162
  - lib/spatial_stats/utils.rb
143
163
  - lib/spatial_stats/utils/lag.rb
144
164
  - lib/spatial_stats/version.rb
@@ -151,6 +171,8 @@ homepage: https://www.github.com/keithdoggett/spatial_stats
151
171
  licenses:
152
172
  - MIT
153
173
  metadata:
174
+ homepage_uri: https://www.github.com/keithdoggett/spatial_stats
175
+ source_code_uri: https://www.github.com/keithdoggett/spatial_stats
154
176
  documentation_uri: https://keithdoggett.github.io/spatial_stats/
155
177
  post_install_message:
156
178
  rdoc_options: []