Skip to content

Commit

Permalink
mm show ok
Browse files Browse the repository at this point in the history
  • Loading branch information
relf committed Jun 11, 2020
1 parent d9db9ca commit e732026
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/lib/whats_opt/salib_sensitivity_analyser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def initialize(ope, kind: :morris)
@sobol = (kind == :sobol)
@input_varcases = ope.input_cases
@output_varcases = ope.output_cases
Rails.logger.info @input_varcases.map(&:float_varname)
Rails.logger.info @output_varcases.map(&:float_varname)
Rails.logger.info @input_varcases.map(&:var_label)
Rails.logger.info @output_varcases.map(&:var_label)
end

def run
Expand Down
6 changes: 3 additions & 3 deletions app/lib/whats_opt/templates/run_sensitivity_analysis.py.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lower = []
upper = []
inputs = []
<% @input_varcases.each do |vc| %>
names.append("<%= vc.float_varname %>")
names.append("<%= vc.var_label %>")
inputs.append(np.array(<%= vc.values %>))
<% end %>
#bounds = zip(lower, upper)
Expand All @@ -32,8 +32,8 @@ salib_pb = {
result = {}

<% @output_varcases.each do |vc| %>
# <%= vc.float_varname %>
outname = '<%= vc.float_varname %>'
# <%= vc.var_label %>
outname = '<%= vc.var_label %>'
result[outname] = {}
output = np.array(<%= vc.values %>)
<% if @sobol %>
Expand Down
10 changes: 3 additions & 7 deletions app/models/case.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ def nb_of_points
values.size
end

def float_varname
label
def var_label
@label ||= Case.label(variable.name, coord_index)
end

def label
@label ||= Case.labelOf(variable.name, coord_index)
end

def self.labelOf(name, coord)
def self.label(name, coord)
name + (coord < 0 ? "" : "[#{coord}]")
end

Expand Down
19 changes: 16 additions & 3 deletions app/models/meta_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ def is_prototype?

def build_surrogates
opts = default_options.map {|o| {name: o[:name], value: o[:value]}}
# At build time, a meta_model is in a "metamodel prototype" analysis by construction
analysis.response_variables.each do |v|
(0...v.dim).each do |index|
surrogates.build(variable: v, coord_index: index-1, kind: default_surrogate_kind, options_attributes: opts)
surrogates.build(variable: v, coord_index: v.ndim == 0 ? -1 : index,
kind: default_surrogate_kind, options_attributes: opts)
end
end
end
Expand Down Expand Up @@ -109,11 +111,22 @@ def predict(values)
end
res
rescue => e
Rails.logger.warn "METAMODEL in ##{analysis.id} #{analysis.name}: Cannot make prediction for #{values}, error: #{e}"
raise PredictionError.new("Cannot make prediction for #{values}, error: #{e}")
end

def training_input_names
@training_input_names ||= operation.input_cases.map { |c| c.label }
@training_input_names ||= operation.input_cases.map { |c| c.var_label }
end

def xlabels
analysis.input_variables.map do |v|
(0...v.dim).map{ |i| Case.label(v.name, v.ndim == 0 ? -1 : i) }
end.flatten
end

def ylabels
self.surrogates.map(&:var_label)
end

def training_input_uncertainties
Expand All @@ -124,7 +137,7 @@ def training_input_uncertainties
end

def training_input_values
@training_inputs ||= Matrix.columns(operation.input_cases.sort_by { |c| c.label }.map(&:values)).to_a
@training_inputs ||= Matrix.columns(operation.input_cases.sort_by { |c| c.var_label }.map(&:values)).to_a
end

def training_output_values(varname, coord_index)
Expand Down
2 changes: 1 addition & 1 deletion app/models/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def update_operation(ope_attrs)
end

def _ope_cases
@ope_cases ||= base_operation ? base_operation._ope_cases : cases.sort_by{|c| c.label}
@ope_cases ||= base_operation ? base_operation._ope_cases : cases.sort_by{|c| c.var_label}
end

def input_cases
Expand Down
4 changes: 2 additions & 2 deletions app/models/surrogate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def proxy
WhatsOpt::SurrogateProxy.new(id: id.to_s)
end

def float_varname
variable.name + (coord_index < 0 ? "" : "[#{coord_index}]")
def var_label
Case.label(variable.name, coord_index)
end

def trained?
Expand Down
12 changes: 8 additions & 4 deletions app/serializers/meta_model_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class MetaModelSerializer < ActiveModel::Serializer
attributes :id, :name, :owner_email, :created_at, :note
attributes :id, :name, :owner_email, :created_at, :note, :xlabels, :ylabels

def name
object.analysis.name
Expand All @@ -15,7 +15,11 @@ def note
object.analysis.note
end

# def x
# object.surrogates.map{ |surr| surr.}
# end
def xlabels
object.xlabels
end

def ylabels
object.ylabels
end
end
4 changes: 3 additions & 1 deletion test/controllers/api/v1/meta_models_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class Api::V1::MetaModelsControllerTest < ActionDispatch::IntegrationTest
get api_v1_meta_model_url(mm), as: :json, headers: @auth_headers
assert_response :success
mminfos = JSON.parse(response.body)
assert_equal ["created_at", "id", "name", "note", "owner_email"], mminfos.keys.sort
assert_equal ["created_at", "id", "name", "note", "owner_email", "xlabels", "ylabels"], mminfos.keys.sort
assert_equal ["x1", "z[0]", "z[1]"], mminfos["xlabels"]
assert_equal ["obj"], mminfos["ylabels"]
end

test "should create a metamodel" do
Expand Down

0 comments on commit e732026

Please sign in to comment.