spatial_stats 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpatialStats
4
+ module Weights
5
+ module Distant
6
+ def self.distance_band(scope, field, bandwidth)
7
+ p_key = scope.primary_key
8
+ keys = scope.pluck(p_key).sort
9
+
10
+ neighbors = SpatialStats::Queries::Weights
11
+ .distance_band_neighbors(scope, field, bandwidth)
12
+
13
+ neighbors = neighbors.group_by(&:i_id)
14
+ weights = neighbors.transform_values do |value|
15
+ value.map do |neighbor|
16
+ hash = neighbor.as_json(only: [:j_id]).symbolize_keys
17
+ hash[:weight] = 1
18
+ hash
19
+ end
20
+ end
21
+ SpatialStats::Weights::WeightsMatrix.new(keys, weights)
22
+ end
23
+
24
+ def self.knn(scope, field, n)
25
+ p_key = scope.primary_key
26
+ keys = scope.pluck(p_key).sort
27
+
28
+ neighbors = SpatialStats::Queries::Weights
29
+ .knn(scope, field, n)
30
+
31
+ neighbors = neighbors.group_by(&:i_id)
32
+ weights = neighbors.transform_values do |value|
33
+ value.map do |neighbor|
34
+ hash = neighbor.as_json(only: [:j_id]).symbolize_keys
35
+ hash[:weight] = 1
36
+ hash
37
+ end
38
+ end
39
+ SpatialStats::Weights::WeightsMatrix.new(keys, weights)
40
+ end
41
+
42
+ def self.idw_band(scope, field, bandwidth, alpha = 1)
43
+ p_key = scope.primary_key
44
+ keys = scope.pluck(p_key).sort
45
+
46
+ neighbors = SpatialStats::Queries::Weights
47
+ .idw_band(scope, field, bandwidth, alpha)
48
+ neighbors = neighbors.group_by { |pair| pair[:i_id] }
49
+
50
+ # only keep j_id and weight
51
+ weights = neighbors.transform_values do |value|
52
+ value.map do |neighbor|
53
+ { weight: neighbor[:weight], j_id: neighbor[:j_id] }
54
+ end
55
+ end
56
+ SpatialStats::Weights::WeightsMatrix.new(keys, weights)
57
+ end
58
+
59
+ def self.idw_knn(scope, field, n, alpha = 1)
60
+ p_key = scope.primary_key
61
+ keys = scope.pluck(p_key).sort
62
+
63
+ neighbors = SpatialStats::Queries::Weights
64
+ .idw_knn(scope, field, n, alpha)
65
+ neighbors = neighbors.group_by { |pair| pair[:i_id] }
66
+
67
+ # only keep j_id and weight
68
+ weights = neighbors.transform_values do |value|
69
+ value.map do |neighbor|
70
+ { weight: neighbor[:weight], j_id: neighbor[:j_id] }
71
+ end
72
+ end
73
+ SpatialStats::Weights::WeightsMatrix.new(keys, weights)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'numo/narray'
4
+
5
+ module SpatialStats
6
+ module Weights
7
+ class WeightsMatrix
8
+ def initialize(keys, weights)
9
+ @keys = keys
10
+ @weights = weights
11
+ @n = keys.size
12
+ end
13
+ attr_accessor :keys, :weights, :n
14
+
15
+ def full
16
+ # returns a square matrix Wij using @keys as the order of items
17
+ @full ||= begin
18
+ rows = []
19
+ @keys.each do |i|
20
+ # iterate through each key to get the data for the row
21
+ row = @keys.map do |j|
22
+ neighbors = @weights[i]
23
+ match = neighbors.find { |neighbor| neighbor[:j_id] == j }
24
+ if match
25
+ match[:weight]
26
+ else
27
+ 0
28
+ end
29
+ end
30
+ rows << row
31
+ end
32
+
33
+ Numo::DFloat.cast(rows)
34
+ end
35
+ end
36
+
37
+ def standardized
38
+ @standardized ||= full.row_standardized
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spatial_stats/weights/contiguous'
4
+ require 'spatial_stats/weights/distant'
5
+ require 'spatial_stats/weights/weights_matrix'
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'spatial_stats/railtie'
4
+ require 'spatial_stats/enumerable_ext'
5
+ require 'spatial_stats/global'
6
+ require 'spatial_stats/local'
7
+ require 'spatial_stats/narray_ext'
8
+ require 'spatial_stats/queries'
9
+ require 'spatial_stats/utils'
10
+ require 'spatial_stats/weights'
11
+
12
+ module SpatialStats
13
+ def self.included(klass)
14
+ puts 'here', klass
15
+ # klass.extend(SpatialStats::Queries::Weights)
16
+ end
17
+ # Your code goes here...
18
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :spatial_stats do
4
+ # # Task goes here
5
+ # end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spatial_stats
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keith Doggett
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: numo-narray
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 6.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 6.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord-postgis-adapter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 6.0.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 6.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: database_cleaner
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.8.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: pg
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: ruby-prof
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: tzinfo
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.2.6
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 1.2.6
111
+ description: An ActiveRecord/PostGIS extension that providesstatistical methods to
112
+ spatial postgresql databases.It integrates with ActiveRecord to perform spatial
113
+ weighting in PostGIS and performs statistical computations inside your rails app.
114
+ Supports contiguious and distance-based calculations.
115
+ email:
116
+ - keith.doggett887@gmail.com
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - MIT-LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - lib/spatial_stats.rb
125
+ - lib/spatial_stats/enumerable_ext.rb
126
+ - lib/spatial_stats/global.rb
127
+ - lib/spatial_stats/global/bivariate_moran.rb
128
+ - lib/spatial_stats/global/moran.rb
129
+ - lib/spatial_stats/global/stat.rb
130
+ - lib/spatial_stats/local.rb
131
+ - lib/spatial_stats/local/bivariate_moran.rb
132
+ - lib/spatial_stats/local/g.rb
133
+ - lib/spatial_stats/local/geary.rb
134
+ - lib/spatial_stats/local/moran.rb
135
+ - lib/spatial_stats/local/multivariate_geary.rb
136
+ - lib/spatial_stats/local/stat.rb
137
+ - lib/spatial_stats/narray_ext.rb
138
+ - lib/spatial_stats/queries.rb
139
+ - lib/spatial_stats/queries/variables.rb
140
+ - lib/spatial_stats/queries/weights.rb
141
+ - lib/spatial_stats/railtie.rb
142
+ - lib/spatial_stats/utils.rb
143
+ - lib/spatial_stats/utils/lag.rb
144
+ - lib/spatial_stats/version.rb
145
+ - lib/spatial_stats/weights.rb
146
+ - lib/spatial_stats/weights/contiguous.rb
147
+ - lib/spatial_stats/weights/distant.rb
148
+ - lib/spatial_stats/weights/weights_matrix.rb
149
+ - lib/tasks/spatial_stats_tasks.rake
150
+ homepage: https://www.github.com/Kdoggett887/spatial_stats
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: 2.3.0
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubygems_version: 3.0.3
170
+ signing_key:
171
+ specification_version: 4
172
+ summary: An ActiveRecord/PostGIS extension that provides statistical methods to spatial
173
+ postgresql databases.
174
+ test_files: []