1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
require_relative "setup.rb"
class NanoparticleTest < MiniTest::Test
include OpenTox::Validation
def setup
@training_dataset = Dataset.where(:name => "Protein Corona Fingerprinting Predicts the Cellular Interaction of Gold and Silver Nanoparticles").first
unless @training_dataset
Import::Enanomapper.import File.join(File.dirname(__FILE__),"data","enm")
@training_dataset = Dataset.where(name: "Protein Corona Fingerprinting Predicts the Cellular Interaction of Gold and Silver Nanoparticles").first
end
@prediction_feature = @training_dataset.features.select{|f| f["name"] == 'log2(Net cell association)'}.first
end
def test_create_model
model = Model::Lazar.create training_dataset: @training_dataset
nanoparticle = @training_dataset.nanoparticles[-34]
prediction = model.predict nanoparticle
p prediction
refute_nil prediction[:value]
assert_includes nanoparticle.dataset_ids, @training_dataset.id
model.delete
end
def test_inspect_cv
skip
cv = CrossValidation.all.sort_by{|cv| cv.created_at}.last
#p cv
#p cv.id
#cv.correlation_plot_id = nil
File.open("tmp.pdf","w+"){|f| f.puts cv.correlation_plot}
#p cv.statistics
#p cv.model.@training_dataset.substances.first.physchem_descriptors.keys.collect{|d| Feature.find(d).name}
CrossValidation.all.sort_by{|cv| cv.created_at}.reverse.each do |cv|
p cv.name
p cv.created_at
begin
p cv.r_squared
rescue
end
end
end
def test_inspect_worst_prediction
skip
cv = CrossValidation.all.sort_by{|cv| cv.created_at}.last
worst_predictions = cv.worst_predictions(n: 3,show_neigbors: false)
assert_equal 3, worst_predictions.size
assert_kind_of Integer, worst_predictions.first[:neighbors]
worst_predictions = cv.worst_predictions
assert_equal 5, worst_predictions.size
assert_kind_of Array, worst_predictions.first[:neighbors]
assert_kind_of Integer, worst_predictions.first[:neighbors].first[:common_descriptors]
puts worst_predictions.to_yaml
worst_predictions = cv.worst_predictions(n: 2, show_common_descriptors: true)
#puts worst_predictions.to_yaml
assert_equal 2, worst_predictions.size
assert_kind_of Array, worst_predictions.first[:neighbors]
refute_nil worst_predictions.first[:neighbors].first[:common_descriptors]
#p cv.model.training_dataset.features
end
def test_validate_model
algorithms = { :prediction => {:method => "Algorithm::Regression.weighted_average" } }
model = Model::Lazar.create training_dataset: @training_dataset
cv = RegressionCrossValidation.create model
p cv.rmse
p cv.r_squared
#File.open("tmp.pdf","w+"){|f| f.puts cv.correlation_plot}
refute_nil cv.r_squared
refute_nil cv.rmse
end
def test_validate_pls_model
algorithms = {
:descriptors => {
:method => "properties",
:types => ["P-CHEM"]
},
:prediction => {:method => "Algorithm::Caret.regression", :parameters => 'pls' },
}
model = Model::Lazar.create prediction_feature: @prediction_feature, training_dataset: @training_dataset, algorithms: algorithms
cv = RegressionCrossValidation.create model
p cv.rmse
p cv.r_squared
refute_nil cv.r_squared
refute_nil cv.rmse
end
def test_validate_random_forest_model
algorithms = {
:descriptors => {
:method => "properties",
:types => ["P-CHEM"]
},
:prediction => {:method => "Algorithm::Caret.regression", :parameters => 'rf' }
}
model = Model::Lazar.create prediction_feature: @prediction_feature, training_dataset: @training_dataset, algorithms: algorithms
cv = RegressionCrossValidation.create model
p cv.rmse
p cv.r_squared
refute_nil cv.r_squared
refute_nil cv.rmse
end
def test_validate_proteomics_pls_model
algorithms = {
:descriptors => {
:method => "properties",
:types => ["Proteomics"]
},
:prediction => {:method => "Algorithm::Caret.regression", :parameters => 'rf' }
}
model = Model::Lazar.create prediction_feature: @prediction_feature, training_dataset: @training_dataset, algorithms: algorithms
cv = RegressionCrossValidation.create model
p cv.rmse
p cv.r_squared
refute_nil cv.r_squared
refute_nil cv.rmse
end
def test_validate_all_default_model
algorithms = {
:descriptors => {
:types => ["Proteomics","P-CHEM"]
},
}
model = Model::Lazar.create prediction_feature: @prediction_feature, training_dataset: @training_dataset, algorithms: algorithms
cv = RegressionCrossValidation.create model
p cv.rmse
p cv.r_squared
refute_nil cv.r_squared
refute_nil cv.rmse
end
def test_export
skip
Dataset.all.each do |d|
puts d.to_csv
end
end
def test_summaries
skip
datasets = Dataset.all
datasets = datasets.select{|d| !d.name.nil?}
datasets.each do |d|
#p d.features.select{|f| f.name.match (/Total/)}
#p d.features.collect{|f| "#{f.name} #{f.unit} #{f.conditions}"}
p d.features.uniq.collect{|f| f.name}
end
assert_equal 9, datasets.size
=begin
features = Feature.all.to_a
#p features.collect do |f|
#f if f.category == "TOX"
#end.to_a.flatten.size
toxcounts = {}
pccounts = {}
Nanoparticle.all.each do |np|
np.measurements.each do |t,v|
toxcounts[t] ||= 0
toxcounts[t] += 1#v.uniq.size
end
np.physchem_descriptors.each do |t,v|
pccounts[t] ||= 0
pccounts[t] += 1#v.uniq.size
end
end
#puts counts.keys.collect{|i| Feature.find(i)}.to_yaml
#pccounts.each{|e,n| p Feature.find(e),n if n > 100}
#p toxcounts.collect{|e,n| Feature.find(e).name if n > 1}.uniq
toxcounts.each{|e,n| p Feature.find(e),n if n > 100}
=end
end
def test_import_ld
skip
dataset_ids = Import::Enanomapper.import_ld
end
end
|