Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
keithdoggett committed Feb 28, 2020
0 parents commit 5747be1
Show file tree
Hide file tree
Showing 66 changed files with 1,150 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.bundle/
log/*.log
pkg/
test/dummy/db/*.sqlite3
test/dummy/db/*.sqlite3-journal
test/dummy/db/*.sqlite3-*
test/dummy/log/*.log
test/dummy/storage/
test/dummy/tmp/
15 changes: 15 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

# Declare your gem's dependencies in spatial_stats.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use a debugger
# gem 'byebug', group: [:development, :test]
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2020 Keith Doggett

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# SpatialStats
Short description and motivation.

## Usage
How to use my plugin.

## Installation
Add this line to your application's Gemfile:

```ruby
gem 'spatial_stats'
```

And then execute:
```bash
$ bundle
```

Or install it yourself as:
```bash
$ gem install spatial_stats
```

## Contributing
Contribution directions go here.

## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
27 changes: 27 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

require 'rdoc/task'

RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'SpatialStats'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb')
end

require 'bundler/gem_tasks'

require 'rake/testtask'

Rake::TestTask.new(:test) do |t|
t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = false
end

task default: :test
5 changes: 5 additions & 0 deletions bin/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
$: << File.expand_path("../test", __dir__)

require "bundler/setup"
require "rails/plugin/test"
5 changes: 5 additions & 0 deletions lib/spatial_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "spatial_stats/railtie"

module SpatialStats
# Your code goes here...
end
135 changes: 135 additions & 0 deletions lib/spatial_stats/queries/weights.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# frozen_string_literal: true

module SpatialStats
def self.included(klass)
klass.extend(ClassMethods)
end

module ClassMethods
def inverse_distance_weights_knn(scope, column, n, alpha)
column = ActiveRecord::Base.connection.quote_column_name(column)
neighbors = find_by_sql([<<-SQL, scope: scope, n: n])
WITH scope as (:scope)
SELECT neighbors.*
FROM scope AS a
CROSS JOIN LATERAL (
SELECT a.#{primary_key} as i_id, b.#{primary_key} as j_id,
ST_Distance(a.#{column}, b.#{column}) as distance
FROM scope as b
WHERE a.#{primary_key} <> b.#{primary_key}
ORDER BY a.#{column} <-> b.#{column}
LIMIT :n
) AS neighbors
SQL

# if the lowest distance is <1, then we need to scale
# every distance by the factor that makes the lowest 1
min_dist = neighbors.map(&:distance).min
scale = if min_dist < 1
1 / min_dist
else
1
end

neighbors = neighbors.group_by(&:i_id)
neighbors.transform_values do |val|
# val is array of neighbors
val.map! do |record|
data = record.as_json.symbolize_keys
data[:weight] = 1.0 / ((scale * data[:distance])**alpha)
data
end
end
end

def inverse_distance_weights_band(scope, column, bandwidth, alpha = 1)
column = ActiveRecord::Base.connection.quote_column_name(column)
neighbors = find_by_sql([<<-SQL, scope: scope, bandwidth: bandwidth])
WITH neighbors AS (
WITH scope AS (:scope)
SELECT a.#{primary_key} as i_id, b.#{primary_key} as j_id,
ST_DWithin(a.#{column}, b.#{column}, :bandwidth) as is_neighbor,
ST_Distance(a.#{column}, b.#{column}) as distance
FROM scope as a, scope as b
ORDER BY i_id
)
SELECT * FROM neighbors WHERE is_neighbor = 't' AND i_id <> j_id
SQL

# if the lowest distance is <1, then we need to scale
# every distance by the factor that makes the lowest 1
min_dist = neighbors.map(&:distance).min
scale = if min_dist < 1
1 / min_dist
else
1
end

neighbors = neighbors.group_by(&:i_id)
neighbors.transform_values do |val|
# val is array of neighbors
val.map! do |record|
data = record.as_json.symbolize_keys
data[:weight] = 1.0 / ((scale * data[:distance])**alpha)
data
end
end
end

def knn(scope, column, n)
column = ActiveRecord::Base.connection.quote_column_name(column)
neighbors = find_by_sql([<<-SQL, scope: scope, n: n])
WITH scope as (:scope)
SELECT neighbors.*
FROM scope AS a
CROSS JOIN LATERAL (
SELECT a.#{primary_key} as i_id, b.#{primary_key} as j_id
FROM scope as b
WHERE a.#{primary_key} <> b.#{primary_key}
ORDER BY a.#{column} <-> b.#{column}
LIMIT :n
) AS neighbors
SQL
neighbors.group_by(&:i_id).transform_values { |v| v.map(&:j_id) }
end

def distance_band_neighbors(scope, column, distance)
column = ActiveRecord::Base.connection.quote_column_name(column)
neighbors = find_by_sql([<<-SQL, scope: scope, distance: distance])
WITH neighbors AS (
WITH scope AS (:scope)
SELECT a.#{primary_key} as i_id, b.#{primary_key} as j_id,
ST_DWithin(a.#{column}, b.#{column}, :distance) as is_neighbor
FROM scope as a, scope as b
ORDER BY i_id
)
SELECT * FROM neighbors WHERE is_neighbor = 't' AND i_id <> j_id
SQL
neighbors.group_by(&:i_id).transform_values { |v| v.map(&:j_id) }
end

# DE-9IM queen contiguiety = F***T****
def queen_contiguity_neighbors(scope, column)
_contiguity_neighbors(scope, column, 'F***T****')
end

def rook_contiguity_neighbors(scope, column)
_contiguity_neighbors(scope, column, 'F***1****')
end

def _contiguity_neighbors(scope, column, pattern)
column = ActiveRecord::Base.connection.quote_column_name(column)
neighbors = find_by_sql([<<-SQL, scope: scope])
WITH neighbors AS (
WITH scope AS (:scope)
SELECT a.#{primary_key} as i_id, b.#{primary_key} as j_id,
ST_RELATE(a.#{column}, b.#{column}, \'#{pattern}\') as is_neighbor
FROM scope as a, scope as b
ORDER BY i_id
)
SELECT * FROM neighbors WHERE is_neighbor = 't'
SQL
neighbors.group_by(&:i_id).transform_values { |v| v.map(&:j_id) }
end
end
end
4 changes: 4 additions & 0 deletions lib/spatial_stats/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module SpatialStats
class Railtie < ::Rails::Railtie
end
end
3 changes: 3 additions & 0 deletions lib/spatial_stats/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module SpatialStats
VERSION = '0.1.0'
end
4 changes: 4 additions & 0 deletions lib/tasks/spatial_stats_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :spatial_stats do
# # Task goes here
# end
31 changes: 31 additions & 0 deletions spatial_stats.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
$:.push File.expand_path("lib", __dir__)

# Maintain your gem's version:
require "spatial_stats/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |spec|
spec.name = "spatial_stats"
spec.version = SpatialStats::VERSION
spec.authors = ["Keith Doggett"]
spec.email = ["[email protected]"]
spec.homepage = "TODO"
spec.summary = "TODO: Summary of SpatialStats."
spec.description = "TODO: Description of SpatialStats."
spec.license = "MIT"

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end

spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]

spec.add_dependency "rails", "~> 6.0.2", ">= 6.0.2.1"

spec.add_development_dependency "sqlite3"
end
1 change: 1 addition & 0 deletions test/dummy/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.6.3
6 changes: 6 additions & 0 deletions test/dummy/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative 'config/application'

Rails.application.load_tasks
2 changes: 2 additions & 0 deletions test/dummy/app/assets/config/manifest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//= link_tree ../images
//= link_directory ../stylesheets .css
Empty file.
15 changes: 15 additions & 0 deletions test/dummy/app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/
4 changes: 4 additions & 0 deletions test/dummy/app/channels/application_cable/channel.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/channels/application_cable/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module ApplicationCable
class Connection < ActionCable::Connection::Base
end
end
2 changes: 2 additions & 0 deletions test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApplicationController < ActionController::Base
end
Empty file.
2 changes: 2 additions & 0 deletions test/dummy/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ApplicationHelper
end
15 changes: 15 additions & 0 deletions test/dummy/app/javascript/packs/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require activestorage
//= require_tree .
7 changes: 7 additions & 0 deletions test/dummy/app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end
4 changes: 4 additions & 0 deletions test/dummy/app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: '[email protected]'
layout 'mailer'
end
3 changes: 3 additions & 0 deletions test/dummy/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Empty file.
14 changes: 14 additions & 0 deletions test/dummy/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Dummy</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<%= stylesheet_link_tag 'application', media: 'all' %>
</head>

<body>
<%= yield %>
</body>
</html>
Loading

0 comments on commit 5747be1

Please sign in to comment.