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.
- checksums.yaml +4 -4
- data/README.md +83 -11
- data/Rakefile +7 -0
- data/ext/spatial_stats/csr_matrix.c +365 -0
- data/ext/spatial_stats/csr_matrix.h +35 -0
- data/ext/spatial_stats/extconf.rb +6 -0
- data/ext/spatial_stats/spatial_stats.c +32 -0
- data/lib/spatial_stats/global/bivariate_moran.rb +43 -21
- data/lib/spatial_stats/global/moran.rb +43 -35
- data/lib/spatial_stats/global/stat.rb +31 -27
- data/lib/spatial_stats/local/bivariate_moran.rb +67 -2
- data/lib/spatial_stats/local/geary.rb +36 -5
- data/lib/spatial_stats/local/getis_ord.rb +45 -17
- data/lib/spatial_stats/local/moran.rb +40 -9
- data/lib/spatial_stats/local/multivariate_geary.rb +29 -10
- data/lib/spatial_stats/local/stat.rb +72 -36
- data/lib/spatial_stats/narray_ext.rb +5 -5
- data/lib/spatial_stats/spatial_stats.so +0 -0
- data/lib/spatial_stats/utils/lag.rb +10 -10
- data/lib/spatial_stats/version.rb +1 -1
- data/lib/spatial_stats/weights/contiguous.rb +20 -10
- data/lib/spatial_stats/weights/distant.rb +38 -20
- data/lib/spatial_stats/weights/weights_matrix.rb +60 -26
- data/lib/spatial_stats.rb +1 -0
- metadata +25 -3
@@ -11,13 +11,12 @@ module SpatialStats
|
|
11
11
|
##
|
12
12
|
# A new instance of WeightsMatrix
|
13
13
|
#
|
14
|
-
# @param [
|
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(
|
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 => [{
|
30
|
-
# {
|
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
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
49
|
+
mat
|
55
50
|
end
|
56
51
|
end
|
57
52
|
|
58
53
|
##
|
59
|
-
#
|
54
|
+
# Compute the CSR representation of the weights.
|
60
55
|
#
|
61
|
-
# @return [
|
62
|
-
def
|
63
|
-
@
|
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
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.
|
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-
|
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: []
|