コード例 #1
0
ファイル: utils.py プロジェクト: spark-brc/cper_viz
def get_stats(df):
    df_stat = df.dropna()
    sim = df_stat.iloc[:, 0].to_numpy()
    obd = df_stat.iloc[:, 1].to_numpy()
    df_nse = evaluator(nse, sim, obd)[0]
    df_rmse = evaluator(rmse, sim, obd)[0]
    df_pibas = evaluator(pbias, sim, obd)[0]
    r_squared = (((sum((obd - obd.mean()) * (sim - sim.mean())))**2) / ((sum(
        (obd - obd.mean())**2) * (sum((sim - sim.mean())**2)))))
    return df_nse, df_rmse, df_pibas, r_squared
コード例 #2
0
def Update_performance_table(link, Event):
    #Read the observed data   
    Qo = pd.read_msgpack('BaseData/USGS/'+str(link)+'.msg')

    Qsim = {}
    Performance = {'QpDiff': {}, 'TpDiff': {}, 'Kling-Gupta':{}}
    for lambdas in ['005','01','015','02','025']:
        #try:
        #Read the data
        path = 'Results/'+str(link)+'_'+lambdas+'_'+str(Event)+'.csv'
        Qs = pd.read_csv(path, index_col = 0, skiprows=0, parse_dates=True)
        Qsim.update({lambdas : Qs['Qsim'].resample('30min').mean()})
        Qo = Qo[Qs.resample('30min').mean().index]
        #Obtains best lambda in terms of the peak mag and timming
        Performance['QpDiff'].update({lambdas: np.abs(Qs['Qsim'].max() - Qo.max())})
        Td = Qs['Qsim'].idxmax() - Qo.idxmax()
        Performance['TpDiff'].update({lambdas: np.abs(Td.total_seconds()) / 3600.})
        pos = np.where((np.isfinite(Qsim[lambdas].values)) & (np.isfinite(Qo.values)))[0]
        Performance['Kling-Gupta'].update({lambdas: heval.evaluator(heval.kge, Qsim[lambdas].values[pos], Qo.values[pos])[0][0]})
        #except:
        #    pass
    Performance = pd.DataFrame.from_dict(Performance)
    D = Performance.round(2).to_dict('records', )
    for c,k in enumerate(['005','01','015','02','025']):
        D[c].update({'Lambda1':k})
    return D
コード例 #3
0
 def nse(x, x_0):
     nse = he.evaluator(he.nse,
                        simulation_s=np.array(list(x.values())),
                        evaluation=np.array(list(x_0.values())))[0]
     print("nse ", nse)
     # make record
     with open(os.path.join(temp_path, "NSEs_" + dbid + ".txt"),
               "a") as nse_file:
         nse_file.write(str(nse) + "\n")
     return nse
コード例 #4
0
            #Read the data
            path = 'Results/' + args.link + '_' + lambdas + '_' + str(
                Event) + '.csv'
            Qs = pd.read_csv(path, index_col=0, skiprows=0, parse_dates=True)
            Qsim.update({lambdas: Qs['Qsim'].resample('30min').mean()})
            Qo = Qobs[Qs.resample('30min').mean().index]
            #Obtains best lambda in terms of the peak mag and timming
            Performance['qpeak'].update(
                {lambdas: np.abs(Qs['Qsim'].max() - Qo.max())})
            Td = Qs['Qsim'].idxmax() - Qo.idxmax()
            Performance['time'].update(
                {lambdas: np.abs(Td.total_seconds()) / 3600.})
            pos = np.where((np.isfinite(Qsim[lambdas].values))
                           & (np.isfinite(Qo.values)))[0]
            Performance['kg'].update({
                lambdas:
                heval.evaluator(heval.kge, Qsim[lambdas].values[pos],
                                Qo.values[pos])[0][0]
            })
        except:
            Performance['qpeak'].update({lambdas: np.nan})
            Performance['time'].update({lambdas: np.nan})
            Performance['kg'].update({lambdas: np.nan})

    BigPerf.update({str(Event): Performance})

#Saves the json of that link
j = json.dumps(BigPerf)
f = open("Summary/" + args.link + '_summary.json', "w")
f.write(j)
f.close()
コード例 #5
0
def Plot_event(link, Event, obj_func):
    #Read the observed data   
    Qo = pd.read_msgpack('BaseData/USGS/'+str(link)+'.msg')

    Qsim = {}
    Performance = {'qpeak': {}, 'time': {}, 'kg':{}}
    for lambdas in ['005','01','015','02','025']:
        #try:
        #Read the data
        path = 'Results/'+str(link)+'_'+lambdas+'_'+str(Event)+'.csv'
        Qs = pd.read_csv(path, index_col = 0, skiprows=0, parse_dates=True)
        Qsim.update({lambdas : Qs['Qsim'].resample('30min').mean()})
        Qo = Qo[Qs.resample('30min').mean().index]
        #Obtains best lambda in terms of the peak mag and timming
        Performance['qpeak'].update({lambdas: np.abs(Qs['Qsim'].max() - Qo.max())})
        Td = Qs['Qsim'].idxmax() - Qo.idxmax()
        Performance['time'].update({lambdas: np.abs(Td.total_seconds()) / 3600.})
        pos = np.where((np.isfinite(Qsim[lambdas].values)) & (np.isfinite(Qo.values)))[0]
        Performance['kg'].update({lambdas: heval.evaluator(heval.kge, Qsim[lambdas].values[pos], Qo.values[pos])[0][0]})
        #except:
        #    pass
    Performance = pd.DataFrame.from_dict(Performance)

    #Select wich to plot in function of the objetive 
    if obj_func == 'Lower Qpeak difference':
        Best = Performance['qpeak'].idxmin()
    elif obj_func == 'Lower timming at the Qpeak':
        Best = Performance['time'].idxmin()
    elif obj_func == 'Best KG':
        Best = Performance['kg'].idxmax()
    #Plot the events
    data = []
    for k in ['005','01','015','02','025']:
        if k != Best:
            trace = go.Scatter(
                x = Qsim[k].index,
                y = Qsim[k].values,
                name = k,
                line = dict(
                    width = 4,
                    color = 'rgb(192,192,192)'
                )
            )
            data.append(trace)

    #Plot the best event
    trace = go.Scatter(
       x = Qsim[Best].index,
       y = Qsim[Best].values,
       name = Best,
       line = dict(
           width = 5,
           color = 'rgb(0,102,204)'
       )
    )
    data.append(trace)

    #Plot the observed data
    trace = go.Scatter(
        x = Qo.index,
        y = Qo.values,
        name = 'Observed',
        mode = 'markers',
        marker = dict(
            color = 'rgb(0,0,0)',
        )
    )
    data.append(trace)

    #The set up of the figure
    layout = dict(
        xaxis = dict(
            title = 'Time [30min]',
            titlefont = dict(
                size = 18
            ),
            tickfont = dict(
                size = 16
            )
        ),
        yaxis = dict(
            title = 'Streamflow [m3s-1]',
            titlefont = dict(
                size = 18
            ),
            tickfont = dict(
                size = 16
            )
        ),
        margin=go.layout.Margin(
            l=50,
            r=50,
            b=100,
            t=20,
            pad=4
        ),
    )

    return {
        'data': data,
        'layout': layout
    }
コード例 #6
0
# notations indicating an outlier
ax.annotate('Outlier',
            xy=(190, 105),
            xytext=(183, 102),
            arrowprops=dict(arrowstyle='->', ec='grey', lw=2),
            bbox=dict(boxstyle="round", fc="0.8"))
ax.annotate('Outlier',
            xy=(165, 85),
            xytext=(158, 82),
            arrowprops=dict(arrowstyle='->', ec='grey', lw=2),
            bbox=dict(boxstyle="round", fc="0.8"))

# NSE, R2 calculation
r2 = (np.corrcoef(df_flow['obs'], df_flow['sim']))[0, 1]**2
r2_val = round(r2, 3)
nse = he.evaluator(he.nse, np.array(df_flow['obs']), np.array(df_flow['sim']))
nse_val = round(nse[0], 3)

# labels and title
lims = (0, max(df_flow.max()))
# ax.set(xlim=lims, ylim=lims)

plt.xlabel('Simulation', fontsize=14)
plt.ylabel('Observation', fontsize=14)
plt.title('Relation between obs. and sim.', fontsize=15)
# write r2, nse in the graph
plt.text(1, lims[1] * 0.9, r'$R^2: $' + str(r2_val), fontdict={'size': 12})
plt.text(1, lims[1] * 0.85, r'$NSE: $' + str(nse_val), fontdict={'size': 12})

plt.show()
コード例 #7
0
        for d in range(len(sim_list_2)):
            c.write(str(sim_list_2[d]) + "\n")
        c.close()

        # CALCULATE EFFICIENCY STATISTICS
        # import and check
        Hobs_18_volume = numpy.loadtxt(fname=obsdata_filename_1)
        sim_output_H = numpy.loadtxt(fname=simdata_filename_1)
        Pobs_18_volume = numpy.loadtxt(fname=obsdata_filename_2)
        sim_output_P = numpy.loadtxt(fname=simdata_filename_2)

        #if not numpy.array_equal(Hobs_18_volume, sim_output_H):
        #raise Exception('The observed and simulated periods (time series length) do not match. ' + str(len(Hobs_18_volume)) + "," + str(len(sim_output_H)))

        # calculate - hen
        H_nse = evaluator(nse, sim_output_H, Hobs_18_volume)
        H_pbias = evaluator(pbias, sim_output_H, Hobs_18_volume)
        H_rmse = evaluator(rmse, sim_output_H, Hobs_18_volume)
        sd_1 = stdev(Hobs_18_volume)
        H_rsr = H_rmse / sd_1
        H_linr = stats.linregress(Hobs_18_volume, sim_output_H)
        H_r2 = H_linr[2]**2

        # calculate - plume
        P_nse = evaluator(nse, sim_output_P, Pobs_18_volume)
        P_pbias = evaluator(pbias, sim_output_P, Pobs_18_volume)
        P_rmse = evaluator(rmse, sim_output_P, Pobs_18_volume)
        sd_2 = stdev(Pobs_18_volume)
        P_rsr = P_rmse / sd_2
        P_linr = stats.linregress(Pobs_18_volume, sim_output_P)
        P_r2 = P_linr[2]**2
コード例 #8
0
        b = open("C:/AGNPS_Watershed_Studies/Bunny_Pothole_Model_Volume/9_BAN_calibration/Python_Output/sim_output_volume.txt", "w+").close()
        b = open("C:/AGNPS_Watershed_Studies/Bunny_Pothole_Model_Volume/9_BAN_calibration/Python_Output/sim_output_volume.txt", "w+")
        for z in range(len(sim_list)):
            b.write(str(sim_list[z])+"\n")
        b.close()

        # CALCULATE EFFICIENCY STATISTICS
        # import and check
        Bobs_16_18_volume = numpy.loadtxt(fname = obsdata_filename) # object is named: B for bunny, obs for 'observed' and then years of data, and metric
        sim_output = numpy.loadtxt(fname = simdata_filename)

        #if not numpy.array_equal(Bobs_16_18_volume, sim_output):
            #raise Exception('The observed and simulated periods (time series length) do not match. ' + str(len(Bobs_16_18_volume)) + "," + str(len(sim_output)))

        # calculate
        b_nse = evaluator(nse, sim_output, Bobs_16_18_volume) # b for bunny, and nse for Nash Sutcliffe efficiency
        b_pbias = evaluator(pbias, sim_output, Bobs_16_18_volume) # percent bias
        b_rmse = evaluator(rmse, sim_output, Bobs_16_18_volume)  # rmse
        sd = stdev(Bobs_16_18_volume) # standard deviation
        b_rsr = b_rmse / sd  # RSR
        b_linr = stats.linregress(Bobs_16_18_volume, sim_output)
        b_r2 = b_linr[2]**2 # R^2

        # WRITE STATISTICS TO SUMMARY FILE
        trialdata = str(i) + "," + str(A_min) + ".," + str(B_min) + ".," + str(C_min) + ".," + str(D_min) + ".," + str(inf_min) + "," + str(b_nse) + "," + str(b_pbias) + "," + str(b_rsr) + "," + str(b_r2) + "\n"
        s = open(statssum_filename, "a")
        s.write(trialdata)
        s.close()

        # increment
        i = i + 1 # next model run
コード例 #9
0
def wt_plot(plot_df):

    colnams = plot_df.columns.tolist()
    # plot
    fig, ax = plt.subplots(figsize=(12, 4))
    ax.grid(True)
    ax.plot(plot_df.index,
            plot_df.iloc[:, 0],
            label='Simulated',
            color='green',
            marker='^',
            alpha=0.7)
    ax.scatter(
        plot_df.index,
        plot_df.iloc[:, 1],
        label='Observed',
        # color='red',
        facecolors="None",
        edgecolors='red',
        lw=1.5,
        alpha=0.4,
        # zorder=2,
    )
    ax.plot(
        plot_df.index,
        plot_df.iloc[:, 1],
        color='red',
        alpha=0.4,
        zorder=2,
    )
    ax2 = ax.twinx()
    ax2.bar(plot_df.index,
            plot_df.prep,
            label='Precipitation',
            width=20,
            color="blue",
            align='center',
            alpha=0.5,
            zorder=0)
    ax2.set_ylabel("Precipitation $(mm)$", color="blue", fontsize=14)
    ax.set_ylabel("Depth to Water $(m)$", fontsize=14)
    ax2.invert_yaxis()
    ax2.set_ylim(plot_df.prep.max() * 3, 0)
    ax.margins(y=0.2)
    ax.tick_params(axis='both', labelsize=12)
    ax2.tick_params(axis='y', labelsize=12)

    # add stats
    plot_df = plot_df.drop('prep', axis=1)
    org_stat = plot_df.dropna()

    sim_org = org_stat.iloc[:, 0].to_numpy()
    obd_org = org_stat.iloc[:, 1].to_numpy()
    df_nse = evaluator(nse, sim_org, obd_org)
    df_rmse = evaluator(rmse, sim_org, obd_org)
    df_pibas = evaluator(pbias, sim_org, obd_org)
    r_squared = (((sum(
        (obd_org - obd_org.mean()) * (sim_org - sim_org.mean())))**2) / ((sum(
            (obd_org - obd_org.mean())**2) * (sum(
                (sim_org - sim_org.mean())**2)))))
    ax.text(0.95,
            0.05,
            'NSE: {:.2f} | RMSE: {:.2f} | PBIAS: {:.2f} | R-Squared: {:.2f}'.
            format(df_nse[0], df_rmse[0], df_pibas[0], r_squared),
            horizontalalignment='right',
            fontsize=10,
            bbox=dict(facecolor='green', alpha=0.5),
            transform=ax.transAxes)
    ax.set_title(colnams[0], loc='center', fontsize=12)
    fig.tight_layout()
    lines, labels = fig.axes[0].get_legend_handles_labels()
    ax.legend(
        lines,
        labels,
        loc='lower left',
        ncol=5,
        # bbox_to_anchor=(0, 0.202),
        fontsize=12)
    # plt.legend()
    plt.show()
コード例 #10
0
j2K_daily_ice_contribution = (time_loop['iceRunoff'] /
                              time_loop['catchmentSimRunoff']) * 100
j2K_monthly_ice_contribution = j2K_daily_ice_contribution.resample('M').mean()
j2k_annual_ice_contribution = j2K_daily_ice_contribution.resample('Y').mean()

j2K_daily_glacier_contribution = (time_loop['glacierRunoff'] /
                                  time_loop['catchmentSimRunoff']) * 100
j2K_monthly_glacier_contribution = j2K_daily_glacier_contribution.resample(
    'M').mean()
j2k_annual_glacier_contribution = j2K_daily_glacier_contribution.resample(
    'Y').mean()

#############  COMPUTE J2K PERFORMANCE  ##############

### Kling-Gupta Efficiency (objective function 1)
kge_j2k = evaluator(kge, time_loop['catchmentSimRunoff'].values,
                    arvan_obs['runoff'].values)[0][0]
kge_j2k_no_glacier = evaluator(kge, time_loop['noGlacierRunoff'].values,
                               arvan_obs['runoff'].values)[0][0]

# Nash-Sutcliffe Efficiency (nse)
nse_j2k = evaluator(nse, time_loop['catchmentSimRunoff'].values,
                    arvan_obs['runoff'].values)[0]
nse_j2k_no_glacier = evaluator(nse, time_loop['noGlacierRunoff'].values,
                               arvan_obs['runoff'].values)[0]

print("\nJ2K KGE with glacier: " + str(kge_j2k))
print("\nJ2K NSE with glacier: " + str(nse_j2k))

print("\nJ2K KGE without glacier: " + str(kge_j2k_no_glacier))
print("\nJ2K NSE without glacier: " + str(nse_j2k_no_glacier))