Skip to content

Commit

Permalink
rubocop safe auto correct
Browse files Browse the repository at this point in the history
  • Loading branch information
relf committed Jun 13, 2020
1 parent 6bf802c commit 8a49899
Show file tree
Hide file tree
Showing 90 changed files with 677 additions and 777 deletions.
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ group :development, :test do
# Compute test coverage
gem "coveralls", require: false
# rswag
gem 'rspec-rails'
gem 'rswag-specs'
gem "rspec-rails"
gem "rswag-specs"
end

group :development do
Expand All @@ -79,7 +79,7 @@ group :staging, :production do
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data" #, platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem "tzinfo-data" # , platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# required in dev but seems to be required also in production
gem "highline"

Expand Down Expand Up @@ -128,4 +128,4 @@ gem "thrift", "0.11.0"
gem "image_processing", "~> 1.2"

# Document API
gem 'rswag-api'
gem "rswag-api"
2 changes: 1 addition & 1 deletion app/controllers/analyses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def destroy
redirect_to mdas_url, alert: "Can not delete nested analysis (you should delete parent first)."
else
unless @mda.operations.blank? # remove operations in reverse order first
@mda.operations.reverse.each { |ope| ope.destroy! }
@mda.operations.reverse_each { |ope| ope.destroy! }
end
@mda.destroy!
redirect_to mdas_url, notice: "Analysis #{@mda.name} was successfully deleted."
Expand Down
12 changes: 6 additions & 6 deletions app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ class Api::ApiController < ActionController::Base
# Authorization
include Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
after_action :verify_authorized, except: [:index], unless: :api_docs_controller?
after_action :verify_policy_scoped, only: [:index], unless: :api_docs_controller?
after_action :verify_authorized, except: [:index], unless: :api_docs_controller?
after_action :verify_policy_scoped, only: [:index], unless: :api_docs_controller?

respond_to :json

# API is protected through Api Key authentication not CSRF
protect_from_forgery with: :null_session

before_action :authenticate, unless: :api_docs_controller?
before_action :check_wop_version
before_action :check_wop_version

attr_reader :current_user

Expand All @@ -27,7 +27,7 @@ def whatsopt_url

private
def authenticate
authenticate_or_request_with_http_token('WhatsOpt') do |token, options|
authenticate_or_request_with_http_token("WhatsOpt") do |token, options|
@current_user = User.where(api_key: token).first
end
end
Expand All @@ -41,11 +41,11 @@ def user_not_authorized
end

def api_docs_controller?
controller_name == 'api_docs'
controller_name == "api_docs"
end

def wop_agent_version
$1 if request.headers['User-Agent'] =~ /^wop\/(.*)/
$1 if request.headers["User-Agent"] =~ /^wop\/(.*)/
end

def check_wop_version
Expand Down
1 change: 0 additions & 1 deletion app/controllers/api/v1/analyses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class Api::V1::AnalysesController < Api::ApiController

before_action :set_mda, only: [:show, :update]

# GET /api/v1/mdas[?with_sub_analyses=true]
Expand Down
10 changes: 4 additions & 6 deletions app/controllers/api/v1/meta_models_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class Api::V1::MetaModelsController < Api::ApiController

before_action :set_meta_model, only: [:show, :update, :destroy]

def index
Expand Down Expand Up @@ -53,7 +52,7 @@ def create
rescue MetaModel::BadKindError => err
json_response({ message: "Bad metamodel kind: #{err.message}" }, :bad_request)
end

# PATCH /api/v1/meta_models/1
def update
format = meta_model_params[:format] || MetaModel::MATRIX_FORMAT # format default to Matrix
Expand All @@ -75,11 +74,10 @@ def set_meta_model
end

def meta_model_params
params.require(:meta_model).permit(:kind, :format,
options: [ :name, :value ],
params.require(:meta_model).permit(:kind, :format,
options: [ :name, :value ],
variables: [ inputs: [], outputs: [] ],
x: [],
x: [],
)
end

end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/operations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create
end
authorize mda
@operation = Operation.build_operation(mda, ope_params)
@operation.save!
@operation.save!
render json: @operation, status: :created
end
end
Expand Down
10 changes: 4 additions & 6 deletions app/controllers/api/v1/optimizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def create
# PATCH /api/v1/optimizations/1
def update
@optim.check_optimization_inputs(optim_params)
inputs = {x: optim_params['x'], y: optim_params['y']}
@optim.update!(inputs: inputs, outputs: {status: Optimization::RUNNING, x_suggested: nil})
inputs = { x: optim_params["x"], y: optim_params["y"] }
@optim.update!(inputs: inputs, outputs: { status: Optimization::RUNNING, x_suggested: nil })
OptimizationJob.perform_later(@optim)
head :no_content
end
Expand All @@ -34,8 +34,7 @@ def destroy
head :no_content
end

private

private
def set_optimization
@optim = Optimization.find(params[:id])
@proxy = @optim.proxy
Expand All @@ -45,5 +44,4 @@ def set_optimization
def optim_params
params.require(:optimization).permit!
end

end
end
3 changes: 1 addition & 2 deletions app/controllers/api/v1/prediction_qualities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ def show
quality = mm.qualification
render json: quality, status: :ok
end

end
end
36 changes: 17 additions & 19 deletions app/controllers/api/v1/sensitivity_analyses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require "whats_opt/salib_sensitivity_analyser"
#require "whats_opt/openturns_sensitivity_analyser"
# require "whats_opt/openturns_sensitivity_analyser"

class Api::V1::SensitivityAnalysesController < Api::ApiController
# GET /api/v1/{operation_id}/sensitivity_analysis
Expand All @@ -17,22 +17,20 @@ def show
end

private

def _get_sensitivity_analysis_infos(ope)
case ope.category
when Operation::CAT_SENSITIVITY
if ope.driver =~ /salib_sensitivity_(sobol|morris)/
analyser = WhatsOpt::SalibSensitivityAnalyser.new(ope, kind: $1.to_sym)
status, sa, err = analyser.run
return { statusOk: status, sensitivity: sa, error: err }
elsif ope.driver =~ /openturns_sensitivity_pce/
analyser = WhatsOpt::OpenturnsSensitivityAnalyser.new(ope)
status, sa, err = analyser.run
return { statusOk: status, sensitivity: sa, error: err }
def _get_sensitivity_analysis_infos(ope)
case ope.category
when Operation::CAT_SENSITIVITY
if ope.driver =~ /salib_sensitivity_(sobol|morris)/
analyser = WhatsOpt::SalibSensitivityAnalyser.new(ope, kind: $1.to_sym)
status, sa, err = analyser.run
return { statusOk: status, sensitivity: sa, error: err }
elsif /openturns_sensitivity_pce/.match?(ope.driver)
analyser = WhatsOpt::OpenturnsSensitivityAnalyser.new(ope)
status, sa, err = analyser.run
return { statusOk: status, sensitivity: sa, error: err }
end
end
end
return { statusOk: false, sensitivity: sa,
error: "Bad operation category: Should be #{Operation::CAT_SENSITIVITY} (got #{ope.category})" }
end

end
{ statusOk: false, sensitivity: sa,
error: "Bad operation category: Should be #{Operation::CAT_SENSITIVITY} (got #{ope.category})" }
end
end
1 change: 0 additions & 1 deletion app/controllers/api/v1/versionings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ def set_versions
@version[:whatsopt] = whatsopt_version
@version[:wop] = wop_recommended_version
end

end
4 changes: 1 addition & 3 deletions app/controllers/api_docs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class ApiDocsController < ApplicationController

def show
end

end
end
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def user_not_authorized
end

def api_docs_controller?
controller_name == 'api_docs'
controller_name == "api_docs"
end

def no_authorization_verify?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/response.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module Response
def json_response(object, status=:ok, options={})
def json_response(object, status = :ok, options = {})
if options[:serializer]
render json: object, status: status, serializer: options[:serializer]
elsif options[:each_serializer]
Expand Down
1 change: 0 additions & 1 deletion app/helpers/layout_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

module LayoutHelper

include WhatsOpt::Version

def deployment_info
Expand Down
6 changes: 3 additions & 3 deletions app/lib/whats_opt/openmdao_driver_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(algoname, options)
@options[k] = v.to_s
if ["true", "false"].include?(@options[k])
@options[k] = @options[k].capitalize # Python boolean
elsif @options[k] =~ /\b[a-zA-Z]/ # wrap string
elsif /\b[a-zA-Z]/.match?(@options[k]) # wrap string
@options[k] = "'#{@options[k]}'"
end
end
Expand Down Expand Up @@ -126,9 +126,9 @@ def initialize(algoname = :scipy_optimizer_slsqp, options_hash = {})
end

def create_driver
if @algoname =~ /doe/
if /doe/.match?(@algoname)
OpenmdaoDoeDriver.new(@algoname, @dict[@algoname])
elsif @algoname =~ /optimizer/
elsif /optimizer/.match?(@algoname)
OpenmdaoOptimizerDriver.new(@algoname, @dict[@algoname])
else
OpenmdaoRunOnceDriver.new(@algoname, @dict[@algoname])
Expand Down
7 changes: 3 additions & 4 deletions app/lib/whats_opt/openturns_sensitivity_analyser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module WhatsOpt
class OpenturnsSensitivityAnalyser
class OpenturnsSensitivityAnalyser
def initialize(ope_pce)
if ope_pce.base_operation && ope_pce.base_operation.driver == "openturns_metamodel_pce"
@mm = ope_pce.base_operation.meta_model
Expand All @@ -14,7 +14,7 @@ def run
ok, out, err = false, "{}", ""
ok, err = check_metamodel
if ok
sa = {saMethod: 'sobol', saResult: get_sobol_pce_sensitivity_analysis}
sa = { saMethod: "sobol", saResult: get_sobol_pce_sensitivity_analysis }
end
return ok, sa, err
end
Expand All @@ -28,8 +28,7 @@ def check_metamodel
end

def get_sobol_pce_sensitivity_analysis
@mm.surrogates.inject({}){|acc, surr| acc.update(surr.get_sobol_pce_sensitivity_analysis)}
@mm.surrogates.inject({}) { |acc, surr| acc.update(surr.get_sobol_pce_sensitivity_analysis) }
end

end
end
26 changes: 12 additions & 14 deletions app/lib/whats_opt/optimizer_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@

module WhatsOpt
class OptimizerProxy < ServiceProxy

CSTRS_TYPES = {
'<' => WhatsOpt::Services::ConstraintType::LESS,
'=' => WhatsOpt::Services::ConstraintType::EQUAL,
'>' => WhatsOpt::Services::ConstraintType::GREATER,
}
"<" => WhatsOpt::Services::ConstraintType::LESS,
"=" => WhatsOpt::Services::ConstraintType::EQUAL,
">" => WhatsOpt::Services::ConstraintType::GREATER,
}

def _initialize
@protocol = Thrift::MultiplexedProtocol.new(
Expand All @@ -19,23 +18,23 @@ def _initialize
@client = Services::OptimizerStore::Client.new(@protocol)
end

def create_optimizer(optimizer_kind, xlimits, cstr_specs=[], options={})
def create_optimizer(optimizer_kind, xlimits, cstr_specs = [], options = {})
opts = {}
options.each do |ks, v|
k = ks.to_s
opts[k] = Services::OptionValue.new(matrix: v) if v.to_s =~ /^\[\[.*\]\]$/
opts[k] = Services::OptionValue.new(vector: v) if (!opts[k] && v.to_s =~ /^\[.*\]$/)
opts[k] = Services::OptionValue.new(integer: v.to_i) if (!opts[k] && v.to_s == v.to_i.to_s)
opts[k] = Services::OptionValue.new(number: v.to_f) if (!opts[k] && v.to_s =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/)
opts[k] = Services::OptionValue.new(matrix: v) if /^\[\[.*\]\]$/.match?(v.to_s)
opts[k] = Services::OptionValue.new(vector: v) if !opts[k] && v.to_s =~ /^\[.*\]$/
opts[k] = Services::OptionValue.new(integer: v.to_i) if !opts[k] && v.to_s == v.to_i.to_s
opts[k] = Services::OptionValue.new(number: v.to_f) if !opts[k] && v.to_s =~ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
opts[k] = Services::OptionValue.new(str: v.to_s) unless opts[k]
end
end
cspecs = cstr_specs.map do |cspec|
Services::ConstraintSpec.new(type: CSTRS_TYPES[cspec[:type]] || '?', bound: cspec[:bound])
Services::ConstraintSpec.new(type: CSTRS_TYPES[cspec[:type]] || "?", bound: cspec[:bound])
end
_send { @client.create_optimizer(@id, optimizer_kind, xlimits, cspecs.compact, opts) }
end

def ask()
def ask
res = nil
_send { res = @client.ask(@id) }
res
Expand All @@ -50,6 +49,5 @@ def tell(x, y)
def destroy_optimizer
_send { @client.destroy_optimizer(@id) }
end

end
end
8 changes: 3 additions & 5 deletions app/lib/whats_opt/python_utils.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require 'open3'
require 'json'
require "open3"
require "json"

module WhatsOpt
module PythonUtils

PYTHON = APP_CONFIG["python_cmd"] || "python"

class ArrayParseError < StandardError
Expand All @@ -13,7 +12,7 @@ def str_to_ary(str)
str = sanitize_pystring(str)
out, err, status = Open3.capture3("#{PYTHON} << EOF\nimport numpy as np\nprint(np.array(#{str}).reshape((-1,)).tolist())\nEOF")
raise ArrayParseError.new(err) unless status.exitstatus == 0
return JSON.parse(out.chomp)
JSON.parse(out.chomp)
end

def sanitize_pystring(str)
Expand All @@ -24,6 +23,5 @@ def sanitize_pystring(str)
str = str.gsub(/!/, "__EXCLAMATION__")
str
end

end
end
2 changes: 1 addition & 1 deletion app/lib/whats_opt/salib_sensitivity_analyser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SalibSensitivityAnalyser < CodeGenerator
def initialize(ope, kind: :morris)
super(ope.analysis)
@kind = kind
@sobol = (kind == :sobol)
@sobol = (kind == :sobol)
@input_varcases = ope.input_cases
@output_varcases = ope.output_cases
Rails.logger.info @input_varcases.map(&:var_label)
Expand Down
Loading

0 comments on commit 8a49899

Please sign in to comment.