morphological_metrics 1.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.
- checksums.yaml +7 -0
- data/.autotest +30 -0
- data/.gemtest +0 -0
- data/.minitest.rb +3 -0
- data/.travis.yml +10 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +48 -0
- data/History.txt +6 -0
- data/Manifest.txt +27 -0
- data/README.rdoc +66 -0
- data/ROADMAP.txt +63 -0
- data/Rakefile +32 -0
- data/bin/mm +3 -0
- data/lib/mm/deltas.rb +35 -0
- data/lib/mm/metric.rb +191 -0
- data/lib/mm/pairs.rb +11 -0
- data/lib/mm/ratio.rb +148 -0
- data/lib/mm/scaling.rb +27 -0
- data/lib/mm/search.rb +97 -0
- data/lib/mm.rb +11 -0
- data/lib/shortcuts.yml +49 -0
- data/test/helpers.rb +22 -0
- data/test/mm/test_deltas.rb +43 -0
- data/test/mm/test_metric.rb +168 -0
- data/test/mm/test_mm.rb +6 -0
- data/test/mm/test_pairs.rb +27 -0
- data/test/mm/test_ratio.rb +132 -0
- data/test/mm/test_scaling.rb +32 -0
- data/test/mm/test_search.rb +89 -0
- metadata +142 -0
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'mm/ratio'
|
2
|
+
|
3
|
+
class TestMM < Minitest::Test; end
|
4
|
+
|
5
|
+
class TestMM::TestRatio < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@ratio = MM::Ratio.new(3,2)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_numerator
|
11
|
+
assert_equal 3, @ratio.numerator
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_denominator
|
15
|
+
assert_equal 2, @ratio.denominator
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_multiplication
|
19
|
+
mul = @ratio * MM::Ratio.new(5,4)
|
20
|
+
assert_equal MM::Ratio.new(15,8), mul
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_multiplication_reduces
|
24
|
+
mul = @ratio * MM::Ratio.new(10,9)
|
25
|
+
assert_equal MM::Ratio.new(5,3), mul
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_division
|
29
|
+
div = @ratio / MM::Ratio.new(5,4)
|
30
|
+
assert_equal MM::Ratio.new(6,5), div
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_from_s_single
|
34
|
+
assert_equal @ratio, MM::Ratio.from_s("3/2")
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_from_s_array
|
38
|
+
assert_equal [@ratio, @ratio], MM::Ratio.from_s(%w(3/2 3/2))
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_from_s_list
|
42
|
+
assert_equal [@ratio, @ratio], MM::Ratio.from_s("3/2 3/2")
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_from_s_multi_digit
|
46
|
+
assert_equal MM::Ratio.new(10,9), MM::Ratio.from_s("10/9")
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_to_f
|
50
|
+
assert_equal 1.5, @ratio.to_f
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_to_s
|
54
|
+
assert_equal "3/2", @ratio.to_s
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_eql
|
58
|
+
assert @ratio.eql?(MM::Ratio.new(3,2))
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_uniq
|
62
|
+
assert_equal [MM::Ratio.new(3,2)], [MM::Ratio.new(3,2), @ratio].uniq
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_plus
|
66
|
+
assert_equal MM::Ratio.new(3,1), @ratio + @ratio
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_minus
|
70
|
+
assert_equal MM::Ratio.new(5,6), @ratio - @ratio.reciprocal
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_reciprocal
|
74
|
+
assert_equal MM::Ratio.new(2,3), @ratio.reciprocal
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_cents
|
78
|
+
assert_in_delta 701.955, @ratio.cents
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_reads_ratios_from_yaml
|
82
|
+
ratios = MM::Ratio.from_yaml(<<-YAML)
|
83
|
+
- 1/1
|
84
|
+
- 10/9
|
85
|
+
- 5/4
|
86
|
+
YAML
|
87
|
+
exp = [MM::Ratio.new(1,1), MM::Ratio.new(10,9), MM::Ratio.new(5,4)]
|
88
|
+
assert_equal exp, ratios
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_to_vector
|
92
|
+
point = [MM::Ratio.new(1,1), MM::Ratio.new(3,2), MM::Ratio.new(5,4)]
|
93
|
+
exp = [MM::Ratio.new(2,3), MM::Ratio.new(6,5)]
|
94
|
+
assert_equal exp, MM::Ratio.to_vector(point)
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_from_vector
|
98
|
+
vector = [MM::Ratio.new(2,3), MM::Ratio.new(6,5)]
|
99
|
+
exp = [MM::Ratio.new(1,1), MM::Ratio.new(3,2), MM::Ratio.new(5,4)]
|
100
|
+
assert_equal exp, MM::Ratio.from_vector(vector)
|
101
|
+
end
|
102
|
+
|
103
|
+
def test_change_interval
|
104
|
+
point = [MM::Ratio.new(1,1), MM::Ratio.new(3,2), MM::Ratio.new(5,4)]
|
105
|
+
exp = [MM::Ratio.new(1,1), MM::Ratio.new(4,3), MM::Ratio.new(10,9)]
|
106
|
+
assert_equal exp, MM::Ratio.change_interval(point, 0, MM::Ratio.new(3,4))
|
107
|
+
end
|
108
|
+
|
109
|
+
def test_change_interval_reciprocal
|
110
|
+
point = [MM::Ratio.new(1,1), MM::Ratio.new(3,2), MM::Ratio.new(5,4)]
|
111
|
+
exp = [MM::Ratio.new(1,1), MM::Ratio.new(2,3), MM::Ratio.new(5,9)]
|
112
|
+
assert_equal exp, MM::Ratio.change_interval(point, 0, :reciprocal)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_factors
|
116
|
+
assert_equal [[2, -1], [3, 1]], MM::Ratio.new(3,2).factors
|
117
|
+
assert_equal [[2, -1], [3, -1], [5, 1]], MM::Ratio.new(5,6).factors
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_each
|
121
|
+
enumerator = MM::Ratio.new(3,2).each
|
122
|
+
assert_kind_of Enumerator, enumerator
|
123
|
+
assert_equal 3, enumerator.next
|
124
|
+
assert_equal 2, enumerator.next
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_prime_limit
|
128
|
+
assert_equal 13, MM::Ratio.new(26,5).prime_limit
|
129
|
+
assert_equal nil, MM::Ratio.new(1,1).prime_limit
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'mm/scaling'
|
2
|
+
require_relative '../helpers.rb'
|
3
|
+
|
4
|
+
class TestMM < Minitest::Test; end
|
5
|
+
|
6
|
+
class TestMM::TestScaling < Minitest::Test
|
7
|
+
include TestHelpers
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@n = [4, 3, 6, 2]
|
11
|
+
@m = [3, 4, 1, 2]
|
12
|
+
end
|
13
|
+
def test_absolute_scaling
|
14
|
+
expected = [[0.667, 0.5, 1.0, 0.333],
|
15
|
+
[0.5, 0.667, 0.167, 0.333]]
|
16
|
+
result = MM::Scaling.absolute([@n, @m])
|
17
|
+
assert_nested_in_delta_2_deep expected, result
|
18
|
+
end
|
19
|
+
def test_relative_scaling
|
20
|
+
expected = [[0.667, 0.5, 1.0, 0.333],
|
21
|
+
[0.75, 1.0, 0.25, 0.5]]
|
22
|
+
result = MM::Scaling.relative([@n, @m])
|
23
|
+
assert_nested_in_delta_2_deep expected, result
|
24
|
+
end
|
25
|
+
def test_get_global_scaling
|
26
|
+
expected = [[0.5, 0.375, 0.75, 0.25],
|
27
|
+
[0.375, 0.5, 0.125, 0.25]]
|
28
|
+
result = MM::Scaling.get_global(8.0).call([@n, @m])
|
29
|
+
assert_nested_in_delta_2_deep expected, result
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'mm/search'
|
2
|
+
require_relative '../helpers.rb'
|
3
|
+
|
4
|
+
class TestMM < Minitest::Test; end
|
5
|
+
|
6
|
+
class TestMM::TestSearch < Minitest::Test
|
7
|
+
include TestHelpers
|
8
|
+
|
9
|
+
def setup
|
10
|
+
@starting_point = 0.4
|
11
|
+
@search = MM::Search.new(@starting_point)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_search_exists
|
15
|
+
refute @search.nil?
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_cost_function_accessor
|
19
|
+
@search.cost_function = ->(){0.5}
|
20
|
+
assert_equal 0.5, @search.cost_function
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_calculate_cost_gets_cost_for_candidates
|
24
|
+
@search.cost_function = ->(x){x - 0.1}
|
25
|
+
candidates = [0.3, 0.5]
|
26
|
+
assert_nested_in_delta [0.2, 0.4], @search.calculate_cost(candidates)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_current_cost_initially_gets_cost_of_starting_point
|
30
|
+
@search.cost_function = ->(x){x - 0.1}
|
31
|
+
assert_in_delta 0.3, @search.current_cost
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_current_cost_gets_cost_of_current_point
|
35
|
+
@search.cost_function = ->(x){x - 0.1}
|
36
|
+
@search.instance_variable_set(:@current_point, 0.3)
|
37
|
+
assert_in_delta 0.2, @search.current_cost
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_adjacent_points_function_accessor
|
41
|
+
@search.adjacent_points_function = ->(current) {[-0.1, 0.1].map {|x| current + x}}
|
42
|
+
assert_nested_in_delta [0.3, 0.5], @search.get_adjacent_points
|
43
|
+
end
|
44
|
+
|
45
|
+
# Testing the final #find method!
|
46
|
+
def test_find
|
47
|
+
@search.adjacent_points_function = ->(current) {[-0.1, 0.1].map {|x| current + x}}
|
48
|
+
@search.cost_function = ->(x){x - 0.1}
|
49
|
+
assert_in_delta 0.1, @search.find
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_find_returns_nil_when_nothing_found
|
53
|
+
@starting_point = [0.1, 0.2, 0.3]
|
54
|
+
@search = MM::Search.new(@starting_point)
|
55
|
+
@search.delta = 0.001
|
56
|
+
@search.starting_point = [0.1, 0.2, 0.3]
|
57
|
+
@search.adjacent_points_function = ->(current) {current.repeated_combination(3)}
|
58
|
+
@search.cost_function = ->(x) { x.inject(0, :+) / x.size }
|
59
|
+
assert_equal nil, @search.find
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_backtrack_removes_point_and_adds_it_to_banned
|
63
|
+
@search.path = [0.1, 0.2]
|
64
|
+
@search.backtrack
|
65
|
+
assert_equal [0.1], @search.path
|
66
|
+
assert_equal [0.2], @search.banned
|
67
|
+
end
|
68
|
+
|
69
|
+
# Stack overflows at (approximately)
|
70
|
+
# 9000 - for integer
|
71
|
+
# 9000 - for Rational
|
72
|
+
# 9000 - for MM::Ratio
|
73
|
+
def test_find_stack_overflow
|
74
|
+
skip # because it takes *forever*
|
75
|
+
@starting_point = MM::Ratio.new(1,1)
|
76
|
+
@search = MM::Search.new(@starting_point)
|
77
|
+
@search.delta = 0.001
|
78
|
+
@search.adjacent_points_function = ->(current) {
|
79
|
+
[MM::Ratio.new(1,1), MM::Ratio.new(-1,1)].map {|m| m + current}
|
80
|
+
}
|
81
|
+
goal = MM::Ratio.new(9000,1)
|
82
|
+
@search.cost_function = ->(x) {
|
83
|
+
(x - goal).abs
|
84
|
+
}
|
85
|
+
assert_equal goal, @search.find
|
86
|
+
puts @search.iterations
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: morphological_metrics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew C. Smith
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: RubyInline
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.9'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.4'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.4'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rdoc
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: hoe
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.12'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.12'
|
69
|
+
description: Implements some Morphological Metrics, described by Larry Polansky.
|
70
|
+
email:
|
71
|
+
- andrewchristophersmith@gmail.com
|
72
|
+
executables:
|
73
|
+
- mm
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files:
|
76
|
+
- History.txt
|
77
|
+
- Manifest.txt
|
78
|
+
- README.rdoc
|
79
|
+
- ROADMAP.txt
|
80
|
+
files:
|
81
|
+
- ".autotest"
|
82
|
+
- ".gemtest"
|
83
|
+
- ".minitest.rb"
|
84
|
+
- ".travis.yml"
|
85
|
+
- Gemfile
|
86
|
+
- Gemfile.lock
|
87
|
+
- History.txt
|
88
|
+
- Manifest.txt
|
89
|
+
- README.rdoc
|
90
|
+
- ROADMAP.txt
|
91
|
+
- Rakefile
|
92
|
+
- bin/mm
|
93
|
+
- lib/mm.rb
|
94
|
+
- lib/mm/deltas.rb
|
95
|
+
- lib/mm/metric.rb
|
96
|
+
- lib/mm/pairs.rb
|
97
|
+
- lib/mm/ratio.rb
|
98
|
+
- lib/mm/scaling.rb
|
99
|
+
- lib/mm/search.rb
|
100
|
+
- lib/shortcuts.yml
|
101
|
+
- test/helpers.rb
|
102
|
+
- test/mm/test_deltas.rb
|
103
|
+
- test/mm/test_metric.rb
|
104
|
+
- test/mm/test_mm.rb
|
105
|
+
- test/mm/test_pairs.rb
|
106
|
+
- test/mm/test_ratio.rb
|
107
|
+
- test/mm/test_scaling.rb
|
108
|
+
- test/mm/test_search.rb
|
109
|
+
homepage: https://www.github.com/andrewcsmith/mm
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options:
|
115
|
+
- "--main"
|
116
|
+
- README.rdoc
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '1.4'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.2.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Implements some Morphological Metrics, described by Larry Polansky.
|
135
|
+
test_files:
|
136
|
+
- test/mm/test_deltas.rb
|
137
|
+
- test/mm/test_metric.rb
|
138
|
+
- test/mm/test_mm.rb
|
139
|
+
- test/mm/test_pairs.rb
|
140
|
+
- test/mm/test_ratio.rb
|
141
|
+
- test/mm/test_scaling.rb
|
142
|
+
- test/mm/test_search.rb
|