Пример #1
0
def test_morris_horizontal_bar_plot():
    # Create a fixture to represent Si, the dictionary of output metrics

    Si = {'mu_star':[1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
          'names':['x1', 'x2', 'x3', 'x4', 'x5', 'x6'],
          'mu_star_conf':[0.5, 1, 1.5, 2, 2.5, 3.0],
          'sigma':[0.5, 1, 1.5, 2, 2.5, 3.0]}

    fig, ax = plt.subplots(1, 1)
    horizontal_bar_plot(ax, Si,{}, sortby='mu_star', unit=r"tCO$_2$/year")
Пример #2
0
 def plot_morris(self):
     """Save plot as png(300 dpi) and eps (vector)."""
     if not self.psa_si:
         self.calculate_sensitivity()
     for i, v in enumerate(self.objnames):
         fig, (ax1, ax2) = plt.subplots(1, 2)
         horizontal_bar_plot(ax1, self.psa_si.get(i), {}, sortby='mu_star')
         covariance_plot(ax2, self.psa_si.get(i), {})
         save_png_eps(plt, self.cfg.psa_outpath, 'mu_star_%s' % v)
         # close current plot in case of 'figure.max_open_warning'
         plt.cla()
         plt.clf()
         plt.close()
Пример #3
0
 def plot_morris(self):
     """Save plot as png(300 dpi) and eps (vector)."""
     if not self.psa_si:
         self.calculate_sensitivity()
     for i, v in enumerate(self.objnames):
         fig, (ax1, ax2) = plt.subplots(1, 2)
         horizontal_bar_plot(ax1, self.psa_si.get(i), {}, sortby='mu_star')
         covariance_plot(ax2, self.psa_si.get(i), {})
         save_png_eps(plt, self.cfg.psa_outpath, 'mu_star_%s' % v)
         # close current plot in case of 'figure.max_open_warning'
         plt.cla()
         plt.clf()
         plt.close()
Пример #4
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()
Пример #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,
                      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()
Пример #6
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()
D=problem["num_vars"] #number of variables
for x in [0,1]: #x=0 kgCO2, x=1 %comf_t
  for HVACsys in [1,2,3,4,5,6,7,8,9,10,12,13,16,17]:
    
    # Prepare data for the analysis for each system
    filename = 'HVACsys'+str(HVACsys)
    rows=data[data[:,13]==HVACsys,0:13].shape[0] #number of inputs/outputs
    n=divmod(rows,14)[0]
    model_input=data[data[:,13]==HVACsys,0:13][0:n*(D+1),:]
    model_output=data[data[:,13]==HVACsys,14+x:15+x][0:n*(D+1),:]
    print(filename)
    # Perform the sensitivity analysis
    Si = morris.analyze(problem, model_input, model_output, conf_level=0.95,
                        print_to_console=True)
    #create plots
    fig1, (ax1, ax2) = plt.subplots(1, 2)
    ax1.set_title(filename)
    ax2.set_title(filename)
    if x == 0:
      horizontal_bar_plot(ax1, Si, {}, sortby='mu_star', unit=r"kgCO2")
      covariance_plot(ax2, Si, {}, unit=r"kgCO2")
      filename_update=filename+"_kgCO2"
    else:
      horizontal_bar_plot(ax1, Si, {}, sortby='mu_star', unit=r"%ComfTime")
      covariance_plot(ax2, Si, {}, unit=r"%ComfTime")
      filename_update=filename+"_ComfTime"
    #save and download plots  
    plt.savefig(filename_update)
    files.download(filename_update+".png")
    plt.close()
                                      delimiter=';')
saved_param_values[:, 6] = np.loadtxt("r2.txt",
                                      dtype=np.float16,
                                      delimiter=';')

r_Nog = np.loadtxt(os.path.join("sweep_array_all_morris", "r_Nog.txt"),
                   dtype=np.float32)
r_BMP = np.loadtxt(os.path.join("sweep_array_all_morris", "r_BMP.txt"),
                   dtype=np.float32)
f_BMP = np.loadtxt(os.path.join("sweep_array_all_morris", "f_BMP.txt"),
                   dtype=np.float32)
f_Nog = np.loadtxt(os.path.join("sweep_array_all_morris", "f_Nog.txt"),
                   dtype=np.float32)

Y = (np.max(f_BMP, axis=1) - np.min(f_BMP, axis=1)) / (
    np.argmax(f_BMP, axis=1) - np.argmin(f_BMP, axis=1))

Si = morris.analyze(problem,
                    saved_param_values,
                    Y,
                    conf_level=0.95,
                    print_to_console=True,
                    num_levels=4,
                    grid_jump=2,
                    num_resamples=100)

fig, ax1 = plt.subplots(1, 1)
horizontal_bar_plot(ax1, Si, {}, sortby='mu_star')

plt.show()
Пример #9
0
     [2. / 3, 1], [2. / 3, 0], [2. / 3, 2. / 3], [0, 2. / 3], [1. / 3, 1],
     [1, 1], [1, 1. / 3], [1. / 3, 1], [1. / 3, 1. / 3], [1, 1. / 3],
     [1. / 3, 2. / 3], [1. / 3, 0], [1, 0]],
    dtype=np.float)

outputs = np.array([
    0.97, 0.71, 2.39, 0.97, 2.30, 2.39, 1.87, 2.40, 0.87, 2.15, 1.71, 1.54,
    2.15, 2.17, 1.54, 2.20, 1.87, 1.0
],
                   dtype=np.float)

salib_pb = {
    'num_vars': 2,
    'names': ['Test 1', 'Test 2'],
    'groups': None,
    'bounds': [[0.0, 1.0], [0.0, 1.0]]
}

name = "output"
Si = ma.analyze(salib_pb, inputs, outputs, print_to_console=False)
result = {k: v.tolist() for k, v in Si.items() if isinstance(v, np.ndarray)}
print(json.dumps(result))
exit()

fig, (ax1, ax2) = plt.subplots(1, 2)
fig.suptitle('#{name} ' + 'sensitivity')
mp.horizontal_bar_plot(ax1, Si, {})
mp.covariance_plot(ax2, Si, {})

plt.show()
output = np.array([results_dict[i] for i in range(len(results_dict))])
output

# ### Factor Prioritisation
#
# We'll run a sensitivity analysis to see which is the most influential parameter.
#
# The results parameters are called **mu**, **sigma** and **mu_star**.
#
# * **Mu** is the mean effect caused by the input parameter being moved over its range.
# * **Sigma** is the standard deviation of the mean effect.
# * **Mu_star** is the mean absolute effect.

# In[ ]:

Si = ma.analyze(morris_problem, sample, output, print_to_console=False)
print("{:20s} {:>7s} {:>7s} {:>7s}".format("Name", "mu", "mu_star", "sigma"))
for name, s1, st, mean in zip(morris_problem['names'], Si['mu'], Si['mu_star'],
                              Si['sigma']):
    print("{:20s} {:=7.2f} {:=7.2f} {:=7.2f}".format(name, s1, st, mean))

# We can plot the results

# In[ ]:

fig, (ax1, ax2) = plt.subplots(1, 2)
mp.horizontal_bar_plot(ax1, Si, param_dict={})
mp.covariance_plot(ax2, Si, {})

# In[ ]:
Пример #11
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()
# * **Mu_star** is the mean absolute effect.

# In[21]:

Si = ma.analyze(morris_problem, sample, output, print_to_console=False)
print("{:20s} {:>7s} {:>7s} {:>7s}".format("Name", "mu", "mu_star", "sigma"))
for name, s1, st, mean in zip(morris_problem['names'], Si['mu'], Si['mu_star'], Si['sigma']):
    print("{:20s} {:=7.2f} {:=7.2f} {:=7.2f}".format(name, s1, st, mean))


# We can plot the results

# In[22]:

fig, (ax1, ax2) = plt.subplots(1,2)
mp.horizontal_bar_plot(ax1, Si, param_dict={})
mp.covariance_plot(ax2, Si, {})


# ## Sanity check
# Because this is such a trivially fast problem, we can do all 8000 simulations on laptop in a fraction of a second, so let's re-do it here in the notebook to compare with our results from above:

# In[23]:

def monte_carlo_large(data):
    y = max_vehicle_power(data[0], data[1], data[2], data[3], data[6], data[4], data[5])
    return y
# Run the sample through the monte carlo procedure of the power model
output = monte_carlo_large(sample.T)
Si = ma.analyze(morris_problem, sample, output, print_to_console=False)
print("{:20s} {:>7s} {:>7s} {:>7s}".format("Name", "mu", "mu_star", "sigma"))