sigfil 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d83faf4c634d50fc09ddf8cba42a48776e46f634
4
+ data.tar.gz: 8e2c9aef795ae2e98a35b4697649ddc539a58864
5
+ SHA512:
6
+ metadata.gz: 821eb24d59886610831fe650943798f8b80fb0a1310f66ce2d07a0df8941d8a2541bc8aba286810fdc7d5ad1fe92a94a78e49e6e9d787f19503c7eabc1e9aff8
7
+ data.tar.gz: 8616a5c60bf92a8c6f7d09085d9fe98bb6ccc566192640609fff56b999e87354709fde990f2035bac0d9813815384521d860c6e4742472a98e6a48931bc256f7
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.13.2
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sigfil.gemspec
4
+ gemspec
5
+
6
+ gem "nmatrix", ">=0.2"
7
+ gem "ffi", ">=1"
8
+ gem "flann", ">=1"
9
+ gem "daru", ">=0.1"
10
+
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # Sigfil
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'sigfil'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install sigfil
19
+
20
+ ## Usage
21
+
22
+ ## Development
23
+
24
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
25
+ `rake test` to run the tests. You can also run `bin/console` for an interactive
26
+ prompt that will allow you to experiment.
27
+
28
+ To install this gem onto your local machine, run `bundle exec rake install`. To
29
+ release a new version, update the version number in `version.rb`, and then run
30
+ `bundle exec rake release`, which will create a git tag for the version, push
31
+ git commits and tags, and push the `.gem` file to
32
+ [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at
37
+ https://github.com/tanahiro/sigfil.
38
+
39
+ ## License
40
+ MIT License
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sigfil"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,89 @@
1
+ require "nmatrix"
2
+ require "flann"
3
+ require "algorithms"
4
+
5
+ module SigFil
6
+ class StatisticalOutlierRemoval
7
+ include Math
8
+
9
+ attr_accessor :mean_k, :std_mul, :dataset, :searcher
10
+
11
+ SEARCHER = [:kdtree, :flann]
12
+
13
+ ##
14
+ # +mean_k+: Number of points to use mean distance estimation
15
+ # +std_mul+: Standard deviation multipliera threshold
16
+ def initialize dataset, mean_k = 2, std_mul = 0.0, searcher = :kdtree
17
+ @dataset = dataset
18
+ @mean_k = mean_k
19
+ @std_mul = std_mul
20
+ unless SEARCHER.include?(searcher)
21
+ raise ArgumentError, "Unknown searcher type: #{searcher}"
22
+ else
23
+ @searcher = searcher
24
+ end
25
+ end
26
+
27
+ def apply_filter scale_factors = nil
28
+ if @searcher == :flann
29
+ Flann.set_distance_type!(:l2)
30
+ end
31
+
32
+ if scale_factors
33
+ unless scale_factors.size == @dataset.cols
34
+ raise ArgumentError, "scale_factors.size != dataset.cols"
35
+ else
36
+ dataset = @dataset.clone
37
+ scale_factors.each_with_index do |s, i|
38
+ dataset[0..-1, i] *= s
39
+ end
40
+ end
41
+ else
42
+ dataset = @dataset
43
+ end
44
+
45
+ case @searcher
46
+ when :flann
47
+ searcher = Flann::Index.new(@dataset) do |params|
48
+ params[:algorithm] = :kdtree
49
+ params[:trees] = 4
50
+ params[:centers_init] = :gonzales
51
+ end
52
+ searcher.build!
53
+ when :kdtree
54
+ dataset_h = dataset.to_a.each_with_index.map {|pt, i| [i, pt]}.to_h
55
+ searcher = Containers::KDTree.new(dataset_h)
56
+ end
57
+
58
+ distances = Array.new(dataset.rows, 0.0)
59
+ dataset.each_row(:clone).with_index do |row, iii|
60
+ case @searcher
61
+ when :flann
62
+ _, dis= searcher.nearest_neighbors(row, @mean_k + 1)
63
+ distances[iii] = NMatrix[*dis[1..-1]].mean[0]
64
+ when :kdtree
65
+ dis = searcher.find_nearest(row.to_a, @mean_k + 1).map do
66
+ |r| sqrt(r[0])
67
+ end
68
+ end
69
+
70
+ distances[iii] = NMatrix[*dis[1..-1]].mean[0]
71
+ end
72
+
73
+ nm_d = NMatrix[*distances]
74
+ d_mean = nm_d.mean[0]
75
+ d_std = nm_d.std[0]
76
+ d_th = d_mean + @std_mul*d_std
77
+
78
+ filtered = []
79
+ distances.each_with_index do |d, i|
80
+ if d <= d_th
81
+ filtered << @dataset.row(i).to_a
82
+ end
83
+ end
84
+
85
+ return NMatrix[*filtered]
86
+ end
87
+ end
88
+ end
89
+
@@ -0,0 +1,3 @@
1
+ module SigFil
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,72 @@
1
+ require "nmatrix"
2
+ require "daru"
3
+
4
+ module SigFil
5
+ class VoxelGrid
6
+ attr_accessor :dataset
7
+ attr_reader :leaf_size
8
+
9
+ def initialize dataset, leaf_size
10
+ @dataset = dataset
11
+ @leaf_size = leaf_size
12
+ get_inverse_leaf_size
13
+
14
+ end
15
+
16
+ def apply_filter
17
+ min, max = get_min_max
18
+
19
+ bb_min, bb_max = get_bb(min, max)
20
+
21
+ #div_b = bb_max.zip(bb_min).map {|x| x[0] - x[1] + 1}
22
+
23
+ index = []
24
+ @dataset.each_row do |pt|
25
+ id = (pt*@inverse_leaf_size - bb_min).floor
26
+ index << id.to_a
27
+ end
28
+
29
+ grid_idx = {}
30
+ df = Daru::DataFrame.rows(index)
31
+ #df.sort!(df.vectors)
32
+ df.each_row_with_index do |row, id|
33
+ grid_idx[row.to_a] ||= []
34
+ grid_idx[row.to_a] << id
35
+ end
36
+
37
+ voxel_ary = []
38
+ grid_idx.each do |g_id, d_id|
39
+ ary = d_id.map {|i| @dataset.row(i).to_a}
40
+ nm = NMatrix[*ary]
41
+ voxel_ary << nm.mean(0).to_a
42
+ end
43
+
44
+ return NMatrix[*voxel_ary]
45
+ end
46
+
47
+ def get_min_max
48
+ min = @dataset.min(0)
49
+ max = @dataset.max(0)
50
+
51
+ return min, max
52
+ end
53
+
54
+ ##
55
+ # Get bounding box
56
+ def get_bb min, max
57
+ bb_min = (min*@inverse_leaf_size).floor
58
+ bb_max = (max*@inverse_leaf_size).floor
59
+
60
+ return bb_min, bb_max
61
+ end
62
+
63
+ def leaf_size= leaf_size
64
+ @leaf_size = leaf_size
65
+ end
66
+
67
+ def get_inverse_leaf_size
68
+ @inverse_leaf_size = NMatrix[@leaf_size.map {|x| 1.0/x}]
69
+ end
70
+ end
71
+ end
72
+
data/lib/sigfil.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "#{__dir__}/sigfil/version"
2
+
3
+ module SigFil
4
+ # Your code goes here...
5
+ end
data/sigfil.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sigfil/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sigfil"
8
+ spec.version = SigFil::VERSION
9
+ spec.authors = ["Hiroyuki Tanaka"]
10
+ spec.email = ["hryktnk@ge.com"]
11
+
12
+ spec.summary = %q{Filters for signal processing}
13
+ spec.description = %q{Filters for signal processing.}
14
+
15
+ spec.homepage = "https://github.com/tanahiro/sigfil"
16
+ spec.license = "MIT"
17
+
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against " \
22
+ "public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.13"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "minitest", "~> 5.0"
35
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sigfil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroyuki Tanaka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Filters for signal processing.
56
+ email:
57
+ - hryktnk@ge.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - bin/console
68
+ - bin/setup
69
+ - lib/sigfil.rb
70
+ - lib/sigfil/statistical_outlier_removal.rb
71
+ - lib/sigfil/version.rb
72
+ - lib/sigfil/voxel_grid.rb
73
+ - sigfil.gemspec
74
+ homepage: https://github.com/tanahiro/sigfil
75
+ licenses:
76
+ - MIT
77
+ metadata:
78
+ allowed_push_host: https://rubygems.org
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.6.7
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Filters for signal processing
99
+ test_files: []