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
|
helpers do
def embedded_svg image, options={}
doc = Nokogiri::HTML::DocumentFragment.parse image
svg = doc.at_css 'svg'
title = doc.at_css 'title'
if options[:class].present?
svg['class'] = options[:class]
end
if options[:title].present?
title.children.remove
text_node = Nokogiri::XML::Text.new(options[:title], doc)
title.add_child(text_node)
end
doc.to_html.html_safe
end
def prediction_to_csv(m,c,p)
model = m
model_name = "#{model.endpoint.gsub('_', ' ')} (#{model.species})"
model_unit = model.regression? ? "(#{model.unit})" : ""
converted_model_unit = model.regression? ? "#{model.unit =~ /\b(mmol\/L)\b/ ? "(mg/L)" : "(mg/kg_bw/day)"}" : ""
csv = ""
compound = c
prediction = p
output = {}
line = ""
output["model_name"] = model_name
output["model_unit"] = model_unit
output["converted_model_unit"] = converted_model_unit
if prediction[:value]
inApp = (prediction[:warnings].join(" ") =~ /Cannot/ ? "no" : (prediction[:warnings].join(" ") =~ /may|Insufficient|Weighted/ ? "maybe" : "yes"))
if prediction[:info] =~ /\b(identical)\b/i
prediction[:info] = "This compound was part of the training dataset. All information "\
"from this compound was removed from the training data before the "\
"prediction to obtain unbiased results."
end
note = "\"#{prediction[:warnings].uniq.join(" ")}\""
output["prediction_value"] = model.regression? ? "#{prediction[:value].delog10.signif(3)}" : "#{prediction[:value]}"
output["converted_value"] = model.regression? ? "#{compound.mmol_to_mg(prediction[:value].delog10).signif(3)}" : nil
if prediction[:measurements].is_a?(Array)
output["measurements"] = model.regression? ? prediction[:measurements].collect{|value| "#{value.delog10.signif(3)}"} : prediction[:measurements].collect{|value| "#{value}"}
output["converted_measurements"] = model.regression? ? prediction[:measurements].collect{|value| "#{compound.mmol_to_mg(value.delog10).signif(3)}"} : false
else
output["measurements"] = model.regression? ? "#{prediction[:measurements].delog10.signif(3)}" : "#{prediction[:measurements]}"
output["converted_measurements"] = model.regression? ? "#{compound.mmol_to_mg(prediction[:measurements].delog10).signif(3)}" : false
end #db_hit
if model.regression?
if !prediction[:prediction_interval].blank?
interval = prediction[:prediction_interval]
output['interval'] = []
output['converted_interval'] = []
output['interval'] << interval[1].delog10.signif(3)
output['interval'] << interval[0].delog10.signif(3)
output['converted_interval'] << compound.mmol_to_mg(interval[1].delog10).signif(3)
output['converted_interval'] << compound.mmol_to_mg(interval[0].delog10).signif(3)
end #prediction interval
line += "#{output['model_name']},#{compound.smiles},"\
"\"#{prediction[:info] ? prediction[:info] : "no"}\",\"#{output['measurements'].join("; ") if prediction[:info]}\","\
"#{!output['prediction_value'].blank? ? output['prediction_value'] : ""},"\
"#{!output['converted_value'].blank? ? output['converted_value'] : ""},"\
"#{!prediction[:prediction_interval].blank? ? output['interval'].first : ""},"\
"#{!prediction[:prediction_interval].blank? ? output['interval'].last : ""},"\
"#{!prediction[:prediction_interval].blank? ? output['converted_interval'].first : ""},"\
"#{!prediction[:prediction_interval].blank? ? output['converted_interval'].last : ""},"\
"#{inApp},#{note.nil? ? "" : note.chomp}\n"
else # Classification
if !prediction[:probabilities].blank?
output['probabilities'] = []
prediction[:probabilities].each{|k,v| output['probabilities'] << v.signif(3)}
end
line += "#{output['model_name']},#{compound.smiles},"\
"\"#{prediction[:info] ? prediction[:info] : "no"}\",\"#{output['measurements'].join("; ") if prediction[:info]}\","\
"#{output['prediction_value']},"\
"#{!prediction[:probabilities].blank? ? output['probabilities'].first : ""},"\
"#{!prediction[:probabilities].blank? ? output['probabilities'].last : ""},"\
"#{inApp},#{note.nil? ? "" : note}\n"
end
output['warnings'] = prediction[:warnings] if prediction[:warnings]
else #no prediction value
inApp = "no"
if prediction[:info] =~ /\b(identical)\b/i
prediction[:info] = "This compound was part of the training dataset. All information "\
"from this compound was removed from the training data before the "\
"prediction to obtain unbiased results."
end
note = "\"#{prediction[:warnings].join(" ")}\""
output['warnings'] = prediction[:warnings]
output['info'] = prediction[:info] if prediction[:info]
if model.regression?
line += "#{output['model_name']},#{compound.smiles},#{prediction[:info] ? prediction[:info] : "no"},"\
"#{prediction[:measurements].collect{|m| m.delog10.signif(3)}.join("; ") if prediction[:info]},,,,,,,"+ [inApp,note].join(",")+"\n"
else
line += "#{output['model_name']},#{compound.smiles},"\
"\"#{prediction[:info] ? prediction[:info] : "no"}\",\"#{output['measurements'].join("; ") if prediction[:info] && !output['measurements'].blank?}\","\
"#{output['prediction_value']},"\
"#{!prediction[:probabilities].blank? ? output['probabilities'].first : ""},"\
"#{!prediction[:probabilities].blank? ? output['probabilities'].last : ""},"\
"#{inApp},#{note.nil? ? "" : note}\n"
end
end
csv += line
# output
csv
end
def dataset_storage
all = Batch.where(:source => /^tmp/)
out = Hash.new
all.reverse.each{|d| out[d.id] = [d.name, d.created_at]}
out
end
end
|