Пример #1
0
def array_to_string(array_to_write,dim):

    if dim == 0:
        string = str(array_to_write)+'\n'
    elif dim == 1:
        string = ' '.join(astr(array_to_write,prec=8))+'\n'
    else:
        raise ValueError('3_calculate_augmented_model_outputs.py: Only scalar and 1-dimensional model outputs are supported yet!')
    
    return string
Пример #2
0
                    'Only scalar and 1D model outputs are supported!')

for ikey in range(nkeys):
    for ipara in range(dims_all):
        if ee_counter[ipara, ikey] > 0:
            ee[ipara, ikey] /= ee_counter[ipara, ikey]

# -------------------------
# write final file
# -------------------------
#     format:
#     # model output #1: 'out1'
#     # model output #2: 'out2'
#     # ii     para_name    elemeffect(ii),ii=1:3,jj=1:1      counter(ii),ii=1:3,jj=1:1
#       1      'x_1'        0.53458196335158181               5
#       2      'x_2'        7.0822368906630215                5
#       3      'x_3'        3.5460086652980554                5
f = open(outfile, 'w')
for ikey in range(nkeys):
    f.write('# model output #' + str(ikey + 1) + ': ' + keys[ikey] + '\n')
f.write('# ii     para_name    elemeffect(ii),ii=1:' + str(dims_all) +
        ',jj=1:' + str(nkeys) + '      counter(ii),ii=1:' + str(dims_all) +
        ',jj=1:' + str(nkeys) + ' \n')
for ipara in range(dims_all):
    f.write(
        str(ipara) + '   ' + para_name[ipara] + '   ' +
        ' '.join(astr(ee[ipara, :], prec=8)) + '   ' +
        ' '.join(astr(ee_counter[ipara, :])) + '\n')
f.close()
print("wrote:   '" + outfile + "'")
Пример #3
0
    figsize = mpl.rcParams['figure.figsize']

    ifig = 0

# -------------------------------------------------------------------------
# Fig 1 - sorted Elementary Effects
#
if (multi_obj_approach == 'rectangle'):
    keepit = []
cutoff_obj = np.ones(nobj) * -9999.
for iobj in range(nobj):

    sort_idx = np.argsort(ee_masked[:, iobj])
    print('')
    print('OBJECTIVE #', iobj + 1)
    print('sorted ee:              ', astr(ee_masked[sort_idx, iobj], prec=4))
    print('correspond to para:     ', idx_para[sort_idx] + 1)

    if (not (noplot)):
        ifig += 1
        iplot = 0
        # print('      Plot - Fig ', ifig)
        fig = plt.figure(ifig)

    if (not (noplot)):
        iplot += 1
        ylab = r'$\mathrm{EE}$'
        sub = fig.add_axes(
            position(nrow, ncol, iplot, hspace=hspace, vspace=vspace))

    if cutoff == '-1':
Пример #4
0
            cbar.set_ticks(cticks)
            cbar.set_ticklabels(cticknames)
            cbar.ax.tick_params(direction='out', length=3, width=1, colors=fgcolor, labelsize=cbartsize, pad=-1.0,
                                bottom='off', top='on', labelbottom='off', labeltop='on')
        else:
            cticks = cvals[:-1] # remove last tick
            cticks = cvals      # dont remove last tick
            cticknames = [ str2tex('$C_{'+str(i)+'}$') if i>0 else str2tex('no data') for i in cticks ]
            cticknames = str2tex(cticknames, usetex=usetex)
            cbar.set_ticks(cticks[:-1]+np.diff(cticks)/2.) # draw ticks in the middle
            cbar.set_ticklabels(cticknames)
            cbar.ax.tick_params(direction='out', length=3, width=1, colors=fgcolor, labelsize=cbartsize, pad=-1.0,
                                bottom=False, top=True, labelbottom=False, labeltop=True)
            for iitick,itick in enumerate(cticks[:-1]):
                tick_location = cticks 
                csub.text(1.0/(len(cvals)-1)*(iitick+0.5), 0.5, str2tex(astr(percent_in_cat[iitick],prec=1)+'%',usetex=usetex),
                              fontsize='xx-small', va='center', ha='center',rotation=0, transform=csub.transAxes)


        # categorizer name
        if categorizer_name == 'categorize_meteo_decision_tree_EEE':
            clab = "EEE-based\n category"
        elif categorizer_name == 'categorize_meteo_decision_tree_expert':
            clab = "Expert-based\n category"
        else:
            clab = 'Not known\n category label'   
        clab = str2tex(clab, usetex=usetex)
        cbar.ax.yaxis.set_label_text(clab, fontsize=cbartsize, va='center', ha='right', rotation=0)
        cbar.ax.yaxis.set_label_coords(-0.03, 0.5, transform=csub.transAxes)

        dy = -0.5
        input_file=input_file,
        time_step=time_step,
        date_time_step=date_time_step,
        period_time_step=period_time_step,
        all_time_steps=all_time_steps,
        grid_cells=grid_cells,
        recipe=recipe)

    if all_time_steps:
        count = {
            cc: np.shape(np.where(categories == cc)[0])[0]
            for cc in np.unique(categories)
        }
        percentage = {
            cc: np.shape(np.where(categories == cc)[0])[0] /
            np.float(len(categories)) * 100.0
            for cc in np.unique(categories)
        }
        print("count:      ", count)
        print("percentage: ", percentage)

        print("")
        for cc in np.unique(categories):
            print(
                str(count[cc]) + " & (" + astr(percentage[cc], prec=0) +
                "\%) & ")

    else:
        print("")
        print('category: ', categories, '   (numbers start with 1)')
Пример #6
0
# if informative(0)    -> maskpara=False
# if noninformative(1) -> maskpara=True
mask_para = np.where((nc[:, 3].flatten()) == 1., True, False)

dims_all = np.shape(mask_para)[0]
idx_para = np.arange(dims_all)[
    mask_para]  # indexes of parameters which will be changed [0,npara-1]
dims = np.sum(mask_para)

# pick only non-masked bounds
lower_bound_mask = lower_bound[np.where(mask_para)]
upper_bound_mask = upper_bound[np.where(mask_para)]
para_dist_mask = para_dist[np.where(mask_para)]
para_name_mask = para_name[np.where(mask_para)]

fileID = astr(np.arange(1, nfiles + 1), zero=True)

# print('dims = '+astr(dims))

for kk in range(nfiles):
    # print('Sampling #'+astr(kk+1)+' of '+astr(nfiles))

    lower_bound_01 = np.zeros(dims)
    upper_bound_01 = np.ones(dims)

    [OptMatrix, OptOutVec] = morris_sampling(dims,
                                             lower_bound_01,
                                             upper_bound_01,
                                             N=ntraj,
                                             p=6,
                                             r=ntraj,
Пример #7
0
            keys = sobol_indexes_raven['paras']['si'].keys()

            print("")
            for ikey in keys:

                print("")
                print("---------------------")
                print(ikey)
                print("---------------------")

                # scalar model output (such as NSE)
                if (len(np.shape(
                        sobol_indexes_raven['paras']['si'][ikey])) == 1):
                    # parameter sensitivities
                    print("   si  numeric  (x1,x2,x3,...,w1,w2,w3,...) = ", [
                        astr(pp, prec=5) if ~np.isnan(pp) else "nan"
                        for pp in sobol_indexes_raven['paras']['si'][ikey]
                    ])
                    print("   sti numeric  (x1,x2,x3,...,w1,w2,w3,...) = ", [
                        astr(pp, prec=5) if ~np.isnan(pp) else "nan"
                        for pp in sobol_indexes_raven['paras']['sti'][ikey]
                    ])

                    # process option sensitivities
                    print("   si  numeric  (a1,a2,a3,...,w1,w2,w3,...) = ", [
                        astr(pp, prec=5) if ~np.isnan(pp) else "nan" for pp in
                        sobol_indexes_raven['process_options']['si'][ikey]
                    ])
                    print("   sti numeric  (a1,a2,a3,...,w1,w2,w3,...) = ", [
                        astr(pp, prec=5) if ~np.isnan(pp) else "nan" for pp in
                        sobol_indexes_raven['process_options']['sti'][ikey]
Пример #8
0
                      jset)

                sobol_indexes_disjoint_tmp[str(
                    jset
                )] = sa_model_multiple_processes_DVM.sa_model_multiple_processes_DVM(
                    options_paras_disjoint,
                    para_ranges,
                    model_function_disjoint,
                    constants=None,
                    nsets=iset,
                    nsets_parasets=jset)
                # process sensitivities
                print(
                    "      si  numeric  (A,B,C) = ",
                    astr(sobol_indexes_disjoint_tmp[str(jset)]['processes']
                         ['si']['out'],
                         prec=5))
                print(
                    "      si  analytic (A,B,C) = ",
                    astr(theo_si_sti_disjoint['wa_A-wb_B-wc_C_process'][0],
                         prec=5))
                print(
                    "      sti numeric  (A,B,C) = ",
                    astr(sobol_indexes_disjoint_tmp[str(jset)]['processes']
                         ['sti']['out'],
                         prec=5))
                print(
                    "      sti analytic (A,B,C) = ",
                    astr(theo_si_sti_disjoint['wa_A-wb_B-wc_C_process'][1],
                         prec=5))
                print(
Пример #9
0
    lsub.set_xticks([])
    lsub.set_yticks([])
    lsub.set_axis_off()

if (outtype == 'pdf'):
    pdf_pages.savefig(fig)
    plt.close(fig)
elif (outtype == 'png'):
    pngfile = pngbase + "{0:04d}".format(ifig) + ".png"
    fig.savefig(pngfile,
                transparent=transparent,
                bbox_inches=bbox_inches,
                pad_inches=pad_inches)
    plt.close(fig)

# -------------------------------------------------------------------------
# Finished
#

if (outtype == 'pdf'):
    pdf_pages.close()
elif (outtype == 'png'):
    pass
else:
    plt.show()

t2 = time.time()
strin = '[m]: ' + astr(
    (t2 - t1) / 60., 1) if (t2 - t1) > 60. else '[s]: ' + astr(t2 - t1, 0)
print('Time ', strin)
Пример #10
0
                         horizontalalignment='center',
                         verticalalignment='bottom',
                         transform=sub.transAxes)

    if (outtype == 'pdf'):
        pdf_pages.savefig(fig)
        plt.close(fig)
    elif (outtype == 'png'):
        pngfile = pngbase + "{0:04d}".format(ifig) + ".png"
        fig.savefig(pngfile,
                    transparent=transparent,
                    bbox_inches=bbox_inches,
                    pad_inches=pad_inches)
        plt.close(fig)

    # --------------------------------------
    # Finish
    # --------------------------------------
    if (outtype == 'pdf'):
        pdf_pages.close()
    elif (outtype == 'png'):
        pass
    else:
        plt.show()

    t2 = time.time()
    str = '  Time plot [m]: ' + astr(
        (t2 - t1) /
        60., 1) if (t2 - t1) > 60. else '  Time plot [s]: ' + astr(t2 - t1, 0)
    print(str)
Пример #11
0
    plt.setp(sub, xlabel=xlab)  # axis labels
    plt.setp(sub, ylabel=ylab)
    sub.grid(False)

    sub.text(0.0,
             1.14,
             str2tex("Original: "),
             transform=sub.transAxes,
             color=mcol2,
             rotation=0,
             fontsize='small',
             horizontalalignment='left',
             verticalalignment='center')
    sub.text(0.17,
             1.14,
             str2tex("NSE = " + astr(nse_original[ibasin], prec=2),
                     usetex=usetex),
             transform=sub.transAxes,
             color=mcol2,
             rotation=0,
             fontsize='small',
             horizontalalignment='left',
             verticalalignment='center')
    sub.text(0.34,
             1.14,
             str2tex("KGE = " + astr(kge_original[ibasin], prec=2),
                     usetex=usetex),
             transform=sub.transAxes,
             color=mcol2,
             rotation=0,
             fontsize='small',
Пример #12
0
        for iset in nsets:

            
            print('')
            print('SA of disjoint-parameter setup: nsets = ',iset)

            basin_prop={'id':'xxx'}
            sobol_indexes_disjoint[str(iset)] = sa_model_multiple_processes.sa_model_multiple_processes(options_paras_disjoint,
                                                                                                          para_ranges,
                                                                                                          model_function_disjoint,
                                                                                                          basin_prop,
                                                                                                          constants=None,
                                                                                                          nsets=iset)

            # parameter sensitivities
            print("   si  numeric  (x1,x2,x3,x4,x5,x6,x7,w1,..,w4) = ",astr(sobol_indexes_disjoint[str(iset)]['paras']['si']['out'],prec=5))
            print("   si  analytic (x1,x2,x3,x4,x5,x6,x7,w1,..,w4) = ",astr(theo_si_sti_disjoint['wa_A-wb_B-wc_C_para'][0],prec=5))
            print("   sti numeric  (x1,x2,x3,x4,x5,x6,x7,w1,..,w4) = ",astr(sobol_indexes_disjoint[str(iset)]['paras']['sti']['out'],prec=5))
            print("   sti analytic (x1,x2,x3,x4,x5,x6,x7,w1,..,w4) = ",astr(theo_si_sti_disjoint['wa_A-wb_B-wc_C_para'][1],prec=5))
            print("   mae error    (x1,x2,x3,x4,x5,x6,x7,w1,..,w4) = ",np.mean(np.array(
                list(np.abs(sobol_indexes_disjoint[str(iset)]['paras']['si']['out']-theo_si_sti_disjoint['wa_A-wb_B-wc_C_para'][0]))+
                list(np.abs(sobol_indexes_disjoint[str(iset)]['paras']['sti']['out']-theo_si_sti_disjoint['wa_A-wb_B-wc_C_para'][1])))))

            # process option sensitivities
            print("   si  numeric  (a1,a2,b1,b2,b3,c1,c2,w1,..,w4) = ",astr(sobol_indexes_disjoint[str(iset)]['process_options']['si']['out'],prec=5))
            print("   si  analytic (a1,a2,b1,b2,b3,c1,c2,w1,..,w4) = ",astr(theo_si_sti_disjoint['wa_A-wb_B-wc_C_option'][0],prec=5))
            print("   sti numeric  (a1,a2,b1,b2,b3,c1,c2,w1,..,w4) = ",astr(sobol_indexes_disjoint[str(iset)]['process_options']['sti']['out'],prec=5))
            print("   sti analytic (a1,a2,b1,b2,b3,c1,c2,w1,..,w4) = ",astr(theo_si_sti_disjoint['wa_A-wb_B-wc_C_option'][1],prec=5))
            print("   mae error    (a1,a2,b1,b2,b3,c1,c2,w1,..,w4) = ",np.mean(np.array(
                list(np.abs(sobol_indexes_disjoint[str(iset)]['process_options']['si']['out']-theo_si_sti_disjoint['wa_A-wb_B-wc_C_option'][0]))+
                list(np.abs(sobol_indexes_disjoint[str(iset)]['process_options']['sti']['out']-theo_si_sti_disjoint['wa_A-wb_B-wc_C_option'][1])))))
Пример #13
0
#   I           unsigned int    integer         4
#   l           long            integer         4
#   L           unsigned long   integer         4
#   f           float           float           4
#   d           double          float           8
#   s           char[]          string
from_day = 0
to_day = 0
#
for iyear in range(startyear, endyear + 1):
    leap = (((iyear % 4) == 0) & ((iyear % 100) != 0)) | ((iyear % 400) == 0)
    days_year = 365 + leap
    bindata = open(indir + str(iyear) + '.bin', "rb").read()
    values = np.array(
        struct.unpack(
            astr(ncols * nrows * days_year) + 'f',
            bindata[0:4 * ncols * nrows * days_year]))
    # create daily fields and roll axis, because for netcdf writer first dimension has to be time
    var = np.rollaxis(
        np.reshape(values, (nrows, ncols, days_year), order='Fortran'), 2, 0)
    from_day = to_day
    to_day = to_day + days_year
    # write data to nc file
    writenetcdf(fhandle, vhandle, var=var, time=np.arange(from_day, to_day))
    # write time steps to nc
    times = np.arange(from_day, to_day)
    writenetcdf(fhandle,
                thand,
                time=np.arange(times[0], times[-1] + 1),
                var=times)
# close netcdf
Пример #14
0
            elif ( len(np.shape(model_output[ikey])) == 2):
                # 1D model output
                ee[ipara_changed,ikey] += np.mean(np.abs(model_output[ikey][iset,:]-model_output[ikey][iset+1,:]) / np.abs(parasets[iset,ipara_changed] - parasets[iset+1,ipara_changed]))
            else:
                raise ValueError('Only scalar and 1D model outputs are supported!')

for ikey in range(nkeys):
    for ipara in range(dims_all):
        if ee_counter[ipara,ikey] > 0:
            ee[ipara,ikey] /= ee_counter[ipara,ikey]

# -------------------------
# write final file
# -------------------------
#     format:
#     # model output #1: 'out1'
#     # model output #2: 'out2'
#     # ii     para_name    elemeffect(ii),ii=1:3,jj=1:1      counter(ii),ii=1:3,jj=1:1
#       1      'x_1'        0.53458196335158181               5
#       2      'x_2'        7.0822368906630215                5
#       3      'x_3'        3.5460086652980554                5      
f = open(outfile, 'w')
for ikey in range(nkeys):
    f.write('# model output #'+str(ikey+1)+': '+keys[ikey]+'\n')
f.write('# ii     para_name    elemeffect(ii),ii=1:'+str(dims_all)+',jj=1:'+str(nkeys)+'      counter(ii),ii=1:'+str(dims_all)+',jj=1:'+str(nkeys)+' \n')
for ipara in range(dims_all):
    f.write(str(ipara)+'   '+para_name[ipara]+'   '+' '.join(astr(ee[ipara,:],prec=8))+'   '+' '.join(astr(ee_counter[ipara,:]))+'\n')
f.close()
print("wrote:   '"+outfile+"'")