In [1]:
from IPython.display import HTML
from ipywidgets import widgets, HBox, VBox
from IPython import display

import numpy as np
import pandas as pd

HTML('''<script>
code_show=true; 
function code_toggle() {
 if (code_show){
 $('div.input').hide();
 } else {
 $('div.input').show();
 }
 code_show = !code_show
} 
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')


Out[1]:

In [2]:
metrics = ('adi', 'bearing', 'area', 'orientation', 'forward_spread', 'lateral_spread', 'structure' )
simulators = ('phoenix', 'australis', 'spark', 'prometheus')

scales = []

for attrib in sorted(metrics):
    scales.append(widgets.FloatSlider(
        value=0.5,
        min=0.0,
        max=1.0,
        step=0.1,
        margin=10,
        padding=10,
        height=100,
        orientation='vertical',
        description=attrib))
    
button = widgets.Button(description='calculate', margin=70, width=100, color='blue')

Relative comparison of simulators


In [3]:
data = pd.DataFrame(np.random.uniform(-1, 1, (len(simulators), len(metrics))), columns=sorted(metrics), index=simulators)

styles = [
    dict(selector="th", props=[("font-size", "100%"),
                               ("padding", "20px"),
                               ("width", "200px"),
                               ("border", '0px'),
                               ("text-align", "center")]),
    dict(selector="tr", props=[("border", '0px')]),
    dict(selector="td", props=[("font-size", "100%"),
                               ("padding", "20px"),
                               ("text-align", "center")]),
]

(data.style
     .set_table_styles(styles)
     .background_gradient(cmap='seismic', low=5.0, high=5.0)
     .set_precision(3))


Out[3]:
adi area bearing forward_spread lateral_spread orientation structure
phoenix -0.778 0.762 0.848 -0.786 0.737 -0.655 0.458
australis 0.709 -0.114 0.642 -0.0359 0.293 0.159 -0.319
spark -0.883 0.804 0.089 0.909 0.746 0.335 0.418
prometheus 0.0515 -0.526 -0.343 -0.0831 -0.694 0.027 0.223

In [6]:
def comparison(args):

    weights = [x.value for x in scales]

    reduction = pd.DataFrame((data * weights).mean(1), columns=['aggregate score'])
    
    display.clear_output(True)
    
    display.display(reduction.style
            .set_table_styles(styles)
            .background_gradient(cmap='seismic', low=5.0, high=5.0)
            .highlight_null('red')
            .set_precision(2))
    return None

for slider in scales:
    slider.observe(comparison, 'value')

Weighted comparison based on user-defined relevance of metrics:


In [7]:
d = HBox(scales)
d


aggregate score
phoenix -0.022
australis 0.15
spark 0.24
prometheus -0.093

In [ ]: