Esempio n. 1
0
def morris():
    from SALib.analyze import morris
    from SALib.sample.morris import sample

    problem = {
        'num_vars': 5,
        'names': ['HEME', 'K1', 'MPY', 'MPYgu', 'QCC'],
        'groups': None,
        'bounds': [[.4, .5], [10., 30.], [30., 40.], [15., 25.], [10., 15.]]
    }

    parameter_values = sample(problem,
                              N=10,
                              num_levels=4,
                              grid_jump=2,
                              optimal_trajectories=None)

    parameters = assign_parameters()
    schedule = get_default_schedule()

    y_index = np.argmax(o["Curine"])

    ys = []

    for inputs in parameter_values:
        for i in range(len(problem["names"])):
            setattr(parameters, problem["names"][i], inputs[i])

        _, __, ___, output = pbpk(parameters, schedule)

        ys.append(output["Curine"][y_index])

    sis = morris.analyze(problem,
                         parameter_values,
                         np.array(ys),
                         conf_level=0.95,
                         print_to_console=True,
                         num_levels=4,
                         grid_jump=2,
                         num_resamples=100)

    import matplotlib.pyplot as plt
    from SALib.plotting.morris import horizontal_bar_plot, covariance_plot, sample_histograms

    fig, (ax1, ax2) = plt.subplots(1, 2)
    horizontal_bar_plot(ax1, sis, {}, sortby='mu_star', unit=r"mg/g")
    covariance_plot(ax2, sis, {}, unit=r"mg/g")

    fig2 = plt.figure()
    sample_histograms(fig2, parameter_values, problem, {'color': 'y'})
    plt.show()
Esempio n. 2
0
def test_morris_sample_histograms():

    input_1 = [[0, 1 / 3.], [0, 1.], [2 / 3., 1.]]
    input_2 = [[0, 1 / 3.], [2 / 3., 1 / 3.], [2 / 3., 1.]]
    input_3 = [[2 / 3., 0], [2 / 3., 2 / 3.], [0, 2 / 3.]]
    input_4 = [[1 / 3., 1.], [1., 1.], [1, 1 / 3.]]
    input_5 = [[1 / 3., 1.], [1 / 3., 1 / 3.], [1, 1 / 3.]]
    input_6 = [[1 / 3., 2 / 3.], [1 / 3., 0], [1., 0]]
    input_sample = np.concatenate([input_1, input_2, input_3,
                                   input_4, input_5, input_6])
    problem = {'num_vars': 2, 'groups': None, 'names':['x1','x2']}

    fig2 = plt.figure()
    sample_histograms(fig2, input_sample, problem, {'color':'y'})
Esempio n. 3
0
# Files with a 4th column for "group name" will be detected automatically, e.g.
# param_file = '../../SALib/test_functions/params/Ishigami_groups.txt'

# Generate samples
param_values = sample(problem, N=1000, num_levels=4,
                      optimal_trajectories=None)

# To use optimized trajectories (brute force method),
# give an integer value for optimal_trajectories

# Run the "model" -- this will happen offline for external models
Y = Sobol_G.evaluate(param_values)

# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = morris.analyze(problem, param_values, Y, conf_level=0.95,
                    print_to_console=True,
                    num_levels=4, num_resamples=100)
# Returns a dictionary with keys 'mu', 'mu_star', 'sigma', and 'mu_star_conf'
# e.g. Si['mu_star'] contains the mu* value for each parameter, in the
# same order as the parameter file

fig, (ax1, ax2) = plt.subplots(1, 2)
horizontal_bar_plot(ax1, Si, {}, sortby='mu_star', unit=r"tCO$_2$/year")
covariance_plot(ax2, Si, {}, unit=r"tCO$_2$/year")

fig2 = plt.figure()
sample_histograms(fig2, param_values, problem, {'color': 'y'})
plt.show()
Esempio n. 4
0
def runMorris(control_rows, output_dropdown, trajectories_text, levels_text,
              grid_jump_text, run_morris_button, results_output):
    inputs = [(control_row[0].value, control_row[1].value,
               control_row[2].value) for control_row in control_rows
              if control_row[0].value != no_selection]

    target_parameters = dict([(input[0], (input[1], input[2]))
                              for input in inputs])

    target_output_name = output_dropdown.value

    n_trajectories = trajectories_text.value
    num_levels = levels_text.value
    grid_jump = grid_jump_text.value

    run_morris_button.disabled = True

    try:
        problem, parameter_values, sis = hdm.run_salib_morris(
            target_parameters,
            target_output_name,
            n_trajectories,
            num_levels,
            grid_jump,
            1  # os.cpu_count()
        )
    except:
        with results_output:
            print("Invalid input", file=sys.stderr)
        return
    finally:
        run_morris_button.disabled = False

    from base64 import b64encode
    header = ','.join([name for name in target_parameters.keys()])
    lines = [','.join([str(d) for d in inputs]) for inputs in parameter_values]
    csv = header + os.linesep + os.linesep.join(lines)
    payload = b64encode(csv.encode())
    payload = payload.decode()
    fileName = "parameter_values.csv"
    title = "Download design"
    html = f'<a download="{fileName}" href="data:text/csv;charset=utf-8;base64,{payload}" target="_blank">{title}</a>'
    link = widgets.HTML(html)

    results_output.clear_output()

    with results_output:
        print("{0:<30} {1:>10} {2:>10} {3:>15} {4:>10}".format(
            "Parameter", "Mu_Star", "Mu", "Mu_Star_Conf", "Sigma"))
        num_vars = problem['num_vars']
        for j in list(range(num_vars)):
            print("{0:30} {1:10.3f} {2:10.3f} {3:15.3f} {4:10.3f}".format(
                sis['names'][j], sis['mu_star'][j], sis['mu'][j],
                sis['mu_star_conf'][j], sis['sigma'][j]))

        display(link)

        fig1, (ax1, ax2) = plt.subplots(1, 2)
        horizontal_bar_plot(ax1, sis, {}, sortby='mu_star')
        covariance_plot(ax2, sis, {})

        fig2 = plt.figure()
        sample_histograms(fig2, parameter_values, problem, {'color': 'y'})

        fig1.tight_layout()

        show_inline_matplotlib_plots()
Esempio n. 5
0
# }

# Files with a 4th column for "group name" will be detected automatically, e.g.:
# param_file = '../../SALib/test_functions/params/Ishigami_groups.txt'

# Generate samples
param_values = sample(problem, N=1000, num_levels=4, grid_jump=2, \
                      optimal_trajectories=None)

# To use optimized trajectories (brute force method), give an integer value for optimal_trajectories

# Run the "model" -- this will happen offline for external models
Y = Sobol_G.evaluate(param_values)

# Perform the sensitivity analysis using the model output
# Specify which column of the output file to analyze (zero-indexed)
Si = morris.analyze(problem, param_values, Y, conf_level=0.95, 
                    print_to_console=True,
                    num_levels=4, grid_jump=2, num_resamples=100)
# Returns a dictionary with keys 'mu', 'mu_star', 'sigma', and 'mu_star_conf'
# e.g. Si['mu_star'] contains the mu* value for each parameter, in the
# same order as the parameter file

fig, (ax1, ax2) = plt.subplots(1, 2)
horizontal_bar_plot(ax1, Si,{}, sortby='mu_star', unit=r"tCO$_2$/year")
covariance_plot(ax2, Si, {}, unit=r"tCO$_2$/year")

fig2 = plt.figure()
sample_histograms(fig2, param_values, problem, {'color':'y'})
plt.show()