-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HSIC sensitivity analysis (#187)
* Add HSIC computation * Generate Sensitivity Analyser code (thrift) * Add HSIC sensitivity analysis server * Implement HSIC sensitivity analyser on ruby side * Make HSIC sensitivity available in REST API * Add var names in HSIC computation API * Add HSIC sensitivity plot * Relax test tolerance * Linting
- Loading branch information
Showing
35 changed files
with
2,062 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import createPlotlyComponent from 'react-plotly.js/factory'; | ||
import Plotly from './custom-plotly'; | ||
|
||
const Plot = createPlotlyComponent(Plotly); | ||
|
||
class HsicScatterPlot extends React.PureComponent { | ||
render() { | ||
const { hsicData } = this.props; | ||
const { | ||
parameters_names: parameterNames, obj_name: objName, hsic: { r2, pvperm }, | ||
} = hsicData; | ||
const traceR2 = { | ||
name: 'R2', | ||
x: parameterNames.map((_, i) => i + 0.9), | ||
y: r2, | ||
type: 'scatter', | ||
mode: 'markers+text', | ||
text: parameterNames, | ||
textposition: 'left center', | ||
marker: { size: 10 }, | ||
cliponaxis: false, | ||
}; | ||
|
||
const tracePvperm = { | ||
name: 'p-value by permutation', | ||
x: parameterNames.map((_, i) => i + 1.1), | ||
y: pvperm, | ||
type: 'scatter', | ||
mode: 'markers+text', | ||
text: parameterNames, | ||
textposition: 'right center', | ||
marker: { size: 10 }, | ||
cliponaxis: false, | ||
yaxis: 'y2', | ||
}; | ||
|
||
const data = [traceR2, tracePvperm]; | ||
const layout = { | ||
title: `${objName} optimization HSIC sensitivity`, | ||
xaxis: { | ||
rangemode: 'tozero', | ||
title: { text: 'Design Variables' }, | ||
}, | ||
yaxis: { | ||
rangemode: 'tozero', | ||
title: { text: 'HSIC r2' }, | ||
titlefont: { color: '#1f77b4' }, | ||
tickfont: { color: '#1f77b4' }, | ||
}, | ||
yaxis2: { | ||
rangemode: 'tozero', | ||
title: 'HSIC p-value', | ||
titlefont: { color: '#ff7f0e' }, | ||
tickfont: { color: '#ff7f0e' }, | ||
overlaying: 'y', | ||
side: 'right', | ||
}, | ||
}; | ||
|
||
return (<Plot data={data} layout={layout} />); | ||
} | ||
} | ||
|
||
HsicScatterPlot.propTypes = { | ||
hsicData: PropTypes.shape({ | ||
obj_name: PropTypes.string.isRequired, | ||
hsic: PropTypes.shape({ | ||
indices: PropTypes.array.isRequired, | ||
r2: PropTypes.array, | ||
pvas: PropTypes.array.isRequired, | ||
pvperm: PropTypes.array, | ||
}), | ||
parameters_names: PropTypes.array.isRequired, | ||
}).isRequired, | ||
}; | ||
|
||
export default HsicScatterPlot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require "matrix" | ||
|
||
module WhatsOpt | ||
class HsicSensitivityAnalyser | ||
def initialize(ope_doe) | ||
if ope_doe.doe? | ||
@ope = ope_doe | ||
@proxy = SensitivityAnalyserProxy.new | ||
else | ||
raise "Bad operation: doe operation required for hsic sensitivity analyzer (got #{ope_pce.base_operation.driver})" | ||
end | ||
end | ||
|
||
def get_hsic_sensitivity(thresholding=Services::HsicThresholding::ZERO, quantile=0.2, g_threshold=0.0) | ||
ok, err = true, "" | ||
|
||
# xdoe from cases | ||
xdata = @ope.input_cases.map {|c| c.values} | ||
xdoe = Matrix[*xdata].t | ||
|
||
# objective from cases | ||
# XXX: suppose mono-objective | ||
obj_vars = @ope.cases.with_role_case(WhatsOpt::Variable::MIN_OBJECTIVE_ROLE) | ||
obj_vals = obj_vars.map {|c| c.values} | ||
# cstrs from cases | ||
# XXX: works only for negative constraints | ||
cstrs_vars = @ope.cases.with_role_case(WhatsOpt::Variable::NEG_CONSTRAINT_ROLE) | ||
cstrs_vals = cstrs_vars.map {|c| c.values} | ||
ydoe = (Matrix[*obj_vals].vstack(Matrix[*cstrs_vals])).t | ||
|
||
hsic = @proxy.compute_hsic(xdoe.to_a, ydoe.to_a, thresholding, quantile, g_threshold) | ||
result = { obj_name: obj_vars.first.var_label, hsic: hsic, parameters_names: @ope.input_cases.map {|c| c.var_label} } | ||
return ok, result, err | ||
rescue StandardError => e | ||
p e | ||
return false, {}, e.to_s | ||
end | ||
|
||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: trueTypes | ||
|
||
require "thrift" | ||
require "sensitivity_analyser" | ||
|
||
module WhatsOpt | ||
class SensitivityAnalyserProxy < ServiceProxy | ||
def _initialize | ||
@analyser_protocol = Thrift::MultiplexedProtocol.new( | ||
@protocol, "SensitivityAnalyserService" | ||
) | ||
@client = Services::SensitivityAnalyser::Client.new(@analyser_protocol) | ||
end | ||
|
||
def compute_hsic(xdoe, ydoe, thresholding, quantile, g_threshold) | ||
hsic = nil | ||
_send { hsic = @client.compute_hsic(xdoe, ydoe, thresholding, quantile, g_threshold) } | ||
hsic | ||
end | ||
|
||
def _send | ||
@transport.open() | ||
yield | ||
rescue Thrift::TransportException => e | ||
# puts "#{e}" | ||
Rails.logger.warn e | ||
false | ||
else | ||
true | ||
ensure | ||
@transport.close() | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.