biopsy 0.1.0.alpha → 0.1.1.alpha

Sign up to get free protection for your applications and to get access to all the features.
data/test/helper.rb CHANGED
@@ -11,9 +11,10 @@ require 'test/unit'
11
11
  begin; require 'turn/autorun'; rescue LoadError; end
12
12
  require 'shoulda-context'
13
13
  require 'biopsy'
14
+ require 'yaml'
14
15
 
15
16
  Turn.config.format = :pretty
16
- Turn.config.trace = 10
17
+ Turn.config.trace = 5
17
18
 
18
19
  Biopsy::Settings.instance.set_defaults
19
20
 
@@ -24,8 +25,6 @@ class Helper
24
25
 
25
26
  attr_reader :tmp_dir
26
27
  attr_reader :target_dir
27
- attr_reader :domain_dir
28
- attr_reader :domain_path
29
28
  attr_reader :target_path
30
29
  attr_reader :objective_dir
31
30
  attr_reader :objective_path
@@ -48,18 +47,32 @@ class Helper
48
47
  # Return a hash of valid target data
49
48
  def target_data
50
49
  {
51
- :input_files => {
52
- :in => 'input.txt'
50
+ :name => 'target_test',
51
+ :output => {
52
+ :onlyfile => 'output.txt'
53
53
  },
54
- :output_files => {
55
- :params => 'output.txt'
56
- },
57
- :parameter_ranges => {
58
- :a => (-40..40).step(2).to_a,
59
- :b => (0..100).step(2).to_a,
60
- :c => (-50..50).to_a
54
+ :parameters => {
55
+ :a => {
56
+ type: 'integer',
57
+ opt: true,
58
+ min: -40,
59
+ max: 40,
60
+ step: 2
61
+ },
62
+ :b => {
63
+ type: 'integer',
64
+ opt: true,
65
+ min: 0,
66
+ max: 40,
67
+ step: 2
68
+ },
69
+ :c => {
70
+ type: 'integer',
71
+ opt: true,
72
+ min: -20,
73
+ max: 20
74
+ }
61
75
  },
62
- :constructor_path => 'test_constructor.rb'
63
76
  }
64
77
  end
65
78
 
@@ -73,12 +86,15 @@ class Helper
73
86
  # Create a valid target definition in the target dir
74
87
  def create_valid_target
75
88
  data = self.target_data
76
- name = 'test_target'
89
+ name = 'target_test'
77
90
  @target_path = File.join(@target_dir, name + '.yml')
78
91
  self.yaml_dump data, @target_path
79
- File.open(File.join(@target_dir, data[:constructor_path]), 'w') do |f|
92
+ File.open(File.join(@target_dir, name + '.rb'), 'w') do |f|
80
93
  f.puts %Q{
81
- class TestConstructor
94
+ class TargetTest
95
+
96
+ def initialize
97
+ end
82
98
 
83
99
  require 'yaml'
84
100
 
@@ -86,7 +102,7 @@ class TestConstructor
86
102
  File.open('output.txt', 'w') do |f|
87
103
  f.puts(params.to_yaml)
88
104
  end
89
- { :params => File.expand_path('output.txt') }
105
+ nil
90
106
  end
91
107
 
92
108
  end
@@ -95,45 +111,6 @@ end
95
111
  name
96
112
  end
97
113
 
98
- # Return a hash of valid domain data
99
- def domain_data
100
- {
101
- :input_filetypes => [
102
- {
103
- :n => 1,
104
- :allowed_extensions => [
105
- '.txt'
106
- ]
107
- }
108
- ],
109
- :output_filetypes => [
110
- {
111
- :n => 1,
112
- :allowed_extensions => [
113
- '.txt'
114
- ]
115
- }
116
- ],
117
- :objectives => [
118
- 'test1', 'test2'
119
- ]
120
- }
121
- end
122
-
123
- def setup_domain
124
- @domain_dir = File.join(@tmp_dir, 'domains')
125
- Dir.mkdir @domain_dir
126
- Biopsy::Settings.instance.domain_dir = [@domain_dir]
127
- end
128
-
129
- def create_valid_domain
130
- data = domain_data
131
- name = 'test_domain'
132
- @domain_path = File.join(@domain_dir, name + '.yml')
133
- self.yaml_dump data, @domain_path
134
- name
135
- end
136
-
137
114
  def setup_objective
138
115
  @objective_dir = File.join(@tmp_dir, 'objectives')
139
116
  Dir.mkdir @objective_dir
@@ -152,8 +129,8 @@ class TestObjective < Biopsy::ObjectiveFunction
152
129
  @weighting = 1
153
130
  end
154
131
 
155
- def run(input, threads)
156
- file = input[:params]
132
+ def run(raw_output, output_files, threads)
133
+ file = output_files[:onlyfile].first
157
134
  input = YAML::load_file(file)
158
135
  a = input[:a].to_i
159
136
  b = input[:b].to_i
@@ -8,15 +8,10 @@ class TestExperiment < Test::Unit::TestCase
8
8
  @h = Helper.new
9
9
  @h.setup_tmp_dir
10
10
 
11
- # we need a domain
12
- @h.setup_domain
13
- domain_name = @h.create_valid_domain
14
- @domain = Biopsy::Domain.new domain_name
15
-
16
11
  # and a target
17
12
  @h.setup_target
18
13
  target_name = @h.create_valid_target
19
- @target = Biopsy::Target.new @domain
14
+ @target = Biopsy::Target.new
20
15
  @target.load_by_name target_name
21
16
 
22
17
  # and an objective
@@ -30,53 +25,45 @@ class TestExperiment < Test::Unit::TestCase
30
25
 
31
26
  should "fail to init when passed a non existent target" do
32
27
  assert_raise Biopsy::TargetLoadError do
33
- Biopsy::Experiment.new('fake_target', 'test_domain')
34
- end
35
- end
36
-
37
- should "fail to init when passed a non existent domain" do
38
- assert_raise Biopsy::DomainLoadError do
39
- Biopsy::Experiment.new('test_target', 'fake_domain')
28
+ Biopsy::Experiment.new('fake_target')
40
29
  end
41
30
  end
42
31
 
43
32
  should "be able to select a valid point from the parameter space" do
44
- e = Biopsy::Experiment.new('test_target', 'test_domain')
33
+ e = Biopsy::Experiment.new('target_test')
45
34
  start_point = e.random_start_point
46
35
  start_point.each_pair do |param, value|
47
- assert @h.target_data[:parameter_ranges][param].include? value
36
+ assert @target.parameters[param].include?(value), "#{value} not in #{@target.parameters[param]}"
48
37
  end
49
38
  end
50
39
 
51
40
  should "be able to select a starting point" do
52
- e = Biopsy::Experiment.new('test_target', 'test_domain')
41
+ e = Biopsy::Experiment.new('target_test')
53
42
  start_point = e.start
54
43
  start_point.each_pair do |param, value|
55
- assert @h.target_data[:parameter_ranges][param].include? value
44
+ assert @target.parameters[param].include?(value), "#{value} not in #{@target.parameters[param]}"
56
45
  end
57
46
  end
58
47
 
59
48
  should "respect user's choice of starting point" do
60
49
  s = {:a => 2, :b => 4}
61
- e = Biopsy::Experiment.new('test_target', 'test_domain', s)
50
+ e = Biopsy::Experiment.new('target_test', s)
62
51
  assert_equal s, e.start
63
52
  end
64
53
 
65
54
  should "automatically select an optimiser if none is specified" do
66
- e = Biopsy::Experiment.new('test_target', 'test_domain')
55
+ e = Biopsy::Experiment.new('target_test')
67
56
  assert e.algorithm.kind_of? Biopsy::TabuSearch
68
57
  end
69
58
 
70
59
  should "return an optimal set of parameters and score when run" do
71
60
  # Kernel.srand 123
72
- e = Biopsy::Experiment.new('test_target', 'test_domain')
73
- known_best = {
74
- :a => 4,
75
- :b => 4,
76
- :c => 4
77
- }
78
- best_found = e.run[:parameters]
79
- assert_equal known_best, best_found
61
+ Dir.chdir @h.tmp_dir do
62
+ e = Biopsy::Experiment.new('target_test')
63
+ known_best = -4
64
+ best_found = e.run[:score]
65
+ assert known_best < best_found
66
+ end
80
67
  end
81
68
 
82
69
  end # Experiment context
@@ -8,20 +8,15 @@ class TestObjectiveHandler < Test::Unit::TestCase
8
8
  @h = Helper.new
9
9
  @h.setup_tmp_dir
10
10
 
11
- # we need a domain
12
- @h.setup_domain
13
- domain_name = @h.create_valid_domain
14
- @domain = Biopsy::Domain.new domain_name
15
-
16
11
  # and a target
17
12
  @h.setup_target
18
13
  target_name = @h.create_valid_target
19
- @target = Biopsy::Target.new @domain
14
+ @target = Biopsy::Target.new
20
15
  @target.load_by_name target_name
21
16
 
22
17
  # and an objective
23
18
  @h.setup_objective
24
- objective_name = @h.create_valid_objective
19
+ @h.create_valid_objective
25
20
  end
26
21
 
27
22
  teardown do
@@ -29,7 +24,7 @@ class TestObjectiveHandler < Test::Unit::TestCase
29
24
  end
30
25
 
31
26
  should "return loaded objectives on init" do
32
- oh = Biopsy::ObjectiveHandler.new @domain, @target
27
+ oh = Biopsy::ObjectiveHandler.new @target
33
28
  refute oh.objectives.empty?
34
29
  end
35
30
 
@@ -49,13 +44,13 @@ class TestObjectiveHandler < Test::Unit::TestCase
49
44
  f.puts 'another_objective'
50
45
  end
51
46
  end
52
- oh = Biopsy::ObjectiveHandler.new @domain, @target
47
+ oh = Biopsy::ObjectiveHandler.new @target
53
48
  assert_equal 1, oh.objectives.length
54
49
  assert_equal 'AnotherObjective', oh.objectives.keys.first
55
50
  end
56
51
 
57
52
  should "run an objective and return the result" do
58
- oh = Biopsy::ObjectiveHandler.new @domain, @target
53
+ oh = Biopsy::ObjectiveHandler.new @target
59
54
  values = {
60
55
  :a => 4,
61
56
  :b => 4,
@@ -65,12 +60,14 @@ class TestObjectiveHandler < Test::Unit::TestCase
65
60
  File.open(file, 'w') do |f|
66
61
  f.puts values.to_yaml
67
62
  end
68
- result = oh.run_for_output({:params => file}, 0, 1, allresults=true)
69
- assert_equal 0, result[:results]["TestObjective"][:result]
63
+ Dir.chdir(@h.tmp_dir) do
64
+ result = oh.run_for_output(nil, {:onlyfile => file})
65
+ assert_equal 0, result
66
+ end
70
67
  end
71
68
 
72
69
  should "perform euclidean distance dimension reduction" do
73
- oh = Biopsy::ObjectiveHandler.new @domain, @target
70
+ oh = Biopsy::ObjectiveHandler.new @target
74
71
  results = {
75
72
  :a => {
76
73
  :optimum => 100,
@@ -6,8 +6,7 @@ class TestSettings < Test::Unit::TestCase
6
6
 
7
7
  setup do
8
8
  @data = {
9
- :domain => 'test_domain',
10
- :objectives_dir => './objectives',
9
+ :objectives_dir => './objectives'
11
10
  }
12
11
  @config_file = File.expand_path 'testconfig.yml'
13
12
  @settings = Biopsy::Settings.instance
@@ -22,7 +21,6 @@ class TestSettings < Test::Unit::TestCase
22
21
  end
23
22
 
24
23
  should "load the specified config file" do
25
- assert @settings.domain == @data[:domain]
26
24
  assert @settings.objectives_dir == @data[:objectives_dir]
27
25
  end
28
26
 
@@ -50,25 +48,18 @@ class TestSettings < Test::Unit::TestCase
50
48
  end
51
49
 
52
50
  should "make loaded settings available as methods" do
53
- assert @settings.domain == @data[:domain], 'domain key not loaded as method'
54
51
  assert @settings.objectives_dir == @data[:objectives_dir], 'objectives_dir key not loaded as method'
55
52
  end
56
53
 
57
54
  should "produce a YAML string representation" do
58
55
  s = @settings.to_s
59
56
  h = YAML.load(s)
60
- @data.each_pair do |key, value|
57
+ h.each_pair do |key, value|
61
58
  varname = "@#{key.to_s}".to_sym
62
59
  assert_equal value, @settings.instance_variable_get(varname)
63
60
  end
64
61
  end
65
62
 
66
- should "error when a non-existent config is requested" do
67
- assert_raise Biopsy::SettingsError do
68
- @settings.locate_config :fake_key, 'blah'
69
- end
70
- end
71
-
72
63
  end # RunHandler context
73
64
 
74
65
  end # TestRunHandler
data/test/test_target.rb CHANGED
@@ -10,15 +10,10 @@ class TestTarget < Test::Unit::TestCase
10
10
  @h = Helper.new
11
11
  @h.setup_tmp_dir
12
12
 
13
- # we need a domain
14
- @h.setup_domain
15
- domain_name = @h.create_valid_domain
16
- @domain = Biopsy::Domain.new domain_name
17
-
18
13
  # and a target
19
14
  @h.setup_target
20
15
  target_name = @h.create_valid_target
21
- @target = Biopsy::Target.new @domain
16
+ @target = Biopsy::Target.new
22
17
  @target.load_by_name target_name
23
18
  end
24
19
 
@@ -36,7 +31,9 @@ class TestTarget < Test::Unit::TestCase
36
31
  end
37
32
 
38
33
  should "fail to find a non-existent definition" do
39
- assert_equal nil, @target.locate_definition('not_real')
34
+ assert_raise Biopsy::TargetLoadError do
35
+ @target.locate_definition('not_real')
36
+ end
40
37
  end
41
38
 
42
39
  should "reject any invalid config" do
@@ -57,17 +54,29 @@ class TestTarget < Test::Unit::TestCase
57
54
  end
58
55
  end
59
56
 
60
- should "reject a config that doesn't match the domain spec" do
61
- d = @h.target_data
62
- d[:input_files][:fake] = 'another.file'
63
- assert @target.validate_config(d).length > 0
64
- end
65
-
66
57
  should "be able to store a loaded config file" do
67
58
  config = YAML::load_file(@h.target_path).deep_symbolize
68
59
  @target.store_config config
69
60
  @h.target_data.each_pair do |key, value|
70
- assert_equal value, @target.instance_variable_get('@' + key.to_s)
61
+ if key == :parameters
62
+ parsed = {}
63
+ value.each_pair do |param, spec|
64
+ if spec[:min] && spec[:max]
65
+ r = (spec[:min]..spec[:max])
66
+ r = spec[:step] ? r.step(spec[:step]) : r
67
+ parsed[param] = r.to_a
68
+ elsif spec[:values]
69
+ parsed[param] = spec[:values]
70
+ else
71
+ assert false, "parameter #{param} with spec #{spec} has no range or values"
72
+ end
73
+ end
74
+ parsed.each_pair do |param, spec|
75
+ assert_equal spec, @target.parameters[param]
76
+ end
77
+ else
78
+ assert_equal value, @target.instance_variable_get('@' + key.to_s)
79
+ end
71
80
  end
72
81
  end
73
82
 
@@ -75,13 +84,15 @@ class TestTarget < Test::Unit::TestCase
75
84
  config = YAML::load_file(@h.target_path).deep_symbolize
76
85
  @target.store_config config
77
86
 
78
- assert !@target.check_constructor, "missing constructor is invalid"
87
+ assert_raise Biopsy::TargetLoadError do
88
+ @target.check_constructor('target_missing')
89
+ end
79
90
 
80
- File.open(@h.target_data[:constructor_path], 'w') do |f|
91
+ File.open(@target.constructor_path, 'w') do |f|
81
92
  f.puts '[x**2 for x in range(10)]' # python :)
82
93
  end
83
- assert !@target.check_constructor, "invalid ruby is invalid"
84
- File.delete @h.target_data[:constructor_path]
94
+ assert !@target.check_constructor('target_test'), "invalid ruby is invalid"
95
+ File.delete @target.constructor_path
85
96
  end
86
97
 
87
98
  end # Target context
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: biopsy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.alpha
5
- prerelease: 6
4
+ version: 0.1.1.alpha
6
5
  platform: ruby
7
6
  authors:
8
7
  - Richard Smith
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-06 00:00:00.000000000 Z
11
+ date: 2013-09-18 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,103 +27,90 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: threach
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rubystats
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - '>='
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - '>='
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: statsample
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - '>='
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - '>='
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: turn
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - '>='
84
74
  - !ruby/object:Gem::Version
85
75
  version: '0'
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - '>='
92
81
  - !ruby/object:Gem::Version
93
82
  version: '0'
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: simplecov
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - '>='
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - '>='
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: shoulda-context
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - '>='
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - '>='
124
109
  - !ruby/object:Gem::Version
125
110
  version: '0'
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: coveralls
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
115
  - - ~>
132
116
  - !ruby/object:Gem::Version
@@ -134,7 +118,6 @@ dependencies:
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
122
  - - ~>
140
123
  - !ruby/object:Gem::Version
@@ -146,53 +129,50 @@ extensions: []
146
129
  extra_rdoc_files: []
147
130
  files:
148
131
  - Rakefile
149
- - lib/biopsy/base_extensions.rb
150
- - lib/biopsy/domain.rb
151
- - lib/biopsy/experiment.rb
152
- - lib/biopsy/objective_function.rb
132
+ - lib/biopsy/version.rb
153
133
  - lib/biopsy/objective_handler.rb
134
+ - lib/biopsy/settings.rb
135
+ - lib/biopsy/base_extensions.rb
136
+ - lib/biopsy/target.rb
154
137
  - lib/biopsy/objectives/fastest_optimum.rb
138
+ - lib/biopsy/experiment.rb
155
139
  - lib/biopsy/opt_algorithm.rb
156
- - lib/biopsy/optimisers/genetic_algorithm.rb
157
- - lib/biopsy/optimisers/parameter_sweeper.rb
140
+ - lib/biopsy/objective_function.rb
158
141
  - lib/biopsy/optimisers/tabu_search.rb
159
- - lib/biopsy/settings.rb
160
- - lib/biopsy/target.rb
161
- - lib/biopsy/version.rb
142
+ - lib/biopsy/optimisers/parameter_sweeper.rb
143
+ - lib/biopsy/optimisers/genetic_algorithm.rb
162
144
  - lib/biopsy.rb
163
- - test/helper.rb
164
- - test/test_domain.rb
165
- - test/test_experiment.rb
166
- - test/test_file.rb
167
- - test/test_hash.rb
168
- - test/test_objective_handler.rb
169
145
  - test/test_settings.rb
170
146
  - test/test_string.rb
147
+ - test/helper.rb
148
+ - test/test_objective_handler.rb
149
+ - test/test_file.rb
171
150
  - test/test_target.rb
151
+ - test/test_hash.rb
152
+ - test/test_experiment.rb
172
153
  - README.md
173
154
  - LICENSE.txt
174
155
  homepage: https://github.com/Blahah/biopsy
175
156
  licenses: []
157
+ metadata: {}
176
158
  post_install_message:
177
159
  rdoc_options: []
178
160
  require_paths:
179
161
  - lib
180
162
  required_ruby_version: !ruby/object:Gem::Requirement
181
- none: false
182
163
  requirements:
183
- - - ! '>='
164
+ - - '>='
184
165
  - !ruby/object:Gem::Version
185
166
  version: '0'
186
167
  required_rubygems_version: !ruby/object:Gem::Requirement
187
- none: false
188
168
  requirements:
189
- - - ! '>'
169
+ - - '>'
190
170
  - !ruby/object:Gem::Version
191
171
  version: 1.3.1
192
172
  requirements: []
193
173
  rubyforge_project:
194
- rubygems_version: 1.8.24
174
+ rubygems_version: 2.0.3
195
175
  signing_key:
196
- specification_version: 3
176
+ specification_version: 4
197
177
  summary: framework for optimising any computational pipeline or program
198
178
  test_files: []