<jupyter_start><jupyter_text>opt_pars = {'first' : np.array([]),             'second': np.array([]),             'full'  : np.array([])}<jupyter_code># Calculate a simulated runoff and place it in the one pandas DataFrame
qsim = pd.concat([pd.DataFrame({"Qsim_{}".format(period) : gr4j.simulation(meteo, opt_pars[period])}, index = meteo.index) for period in opt_pars.keys()], axis=1)
# Create a helper pandas DataFrame for holding all the data together
qdf = pd.concat([qsim, runoff], axis=1)
# Calculate a calibration/validation matrix

## Create a holder
validation_array = np.zeros((3, 3))

## Calibration periods in rows
for i, period_name in enumerate(periods.keys()):
    
    ## Validation periods in columns
    for j, period_instance in enumerate(periods.values()):
        
        Qo = runoff[period_instance[0]: period_instance[1]]
        
        Qs = qdf["Qsim_"+period_name][period_instance[0]: period_instance[1]]
        
        Q_df = pd.concat([Qs, Qo], axis=1).dropna()
        
        validation_array[i, j] = NS(Q_df["Qobs"].values, Q_df["Qsim_"+period_name].values)
        

# Convert to a pandas DataFrame for a nice look
validation_table = pd.DataFrame(validation_array, columns=periods.keys(), index=periods.keys())

# Plot the results
validation_table<jupyter_output><empty_output><jupyter_text>Read in HYDROGRAPH model resultsqsim_hydrograph = pd.read_csv("../data/runoff_kwbs_hydrograph.csv", header=0, index_col=0, parse_dates=True, names=["Hydrograph"])qsim_hydrograph.head() qsim_hydrograph.plot()<jupyter_code># Read in HYDROGRAPH model results
qsim_hype = pd.read_table("../data/runoff_kwbs_hype.txt", skiprows=2, header=None, index_col=0, parse_dates=True, names=["HYPE", "Qobs"])["HYPE"]
qsim_hype = pd.DataFrame(qsim_hype)
qsim_hype.head()
qsim_hype.plot()
# Convert the baseline solution back to m3/s and concatenate with HYdrograph model results
qdf = pd.concat([qdf / coef, qsim_hype], axis=1)

qdf = qdf.dropna()
qdf[["Qobs", "Qsim_first", "HYPE"]]["1982"].plot(figsize=(16,8))
# Year-wise NS BASELINE
baseline_NS = [NS(qdf["Qobs"][str(year)], qdf["Qsim_full"][str(year)]) for year in range(1980, 1986)]

#hydrograph_NS = [NS(qdf["Qobs"][str(year)], qdf["Hydrograph"][str(year)]) for year in range(1980, 1986)]

hype_NS = [NS(qdf["Qobs"][str(year)], qdf["HYPE"][str(year)]) for year in range(1980, 1986)]
print("Baseline, full period NS:", NS(qdf["Qobs"]["1980":"1985"], qdf["Qsim_full"]["1980":"1985"]))
#print("Hydrograph, full period NS:", NS(qdf["Qobs"]["1980":"1985"], qdf["Hydrograph"]["1980":"1985"]))
print("HYPE, full period NS:", NS(qdf["Qobs"]["1980":"1985"], qdf["HYPE"]["1980":"1985"]))
plt.figure(figsize=(12, 8))
#plt.bar([i for i in range(1980, 1986)], hydrograph_NS, alpha=0.85, label="HYDROGRAPH")
plt.bar([i for i in range(1980, 1986)], hype_NS, alpha=0.7, label="HYPE")
plt.bar([i for i in range(1980, 1986)], baseline_NS, label="Baseline")
plt.legend()
plt.ylabel("NS")<jupyter_output><empty_output><jupyter_text>$$ NSE = 1-\frac{\sum_{i=1}^{N}(obs_{i}-sim_{i})^2}{\sum_{i=1}^{N}(obs_{i}-\bar{obs})^2} $$_______________$$ NSE_{gain} = 1-\frac{\sum_{i=1}^{N}(obs_{i}-sim_{i})^2}{\sum_{i=1}^{N}(obs_{i}-baseline_{i})^2} $$<jupyter_code>def NS_gain(obs, sim, baseline):
    
    numerator = np.nansum( (obs - sim)**2 )
    
    denominator = np.nansum( (obs - baseline)**2 )
    
    return 1 - numerator / denominator
#hydrograph_gain_NS = [NS_gain(qdf["Qobs"][str(year)], qdf["Hydrograph"][str(year)], qdf["Qsim_full"][str(year)]) for year in range(1980, 1986)]
hype_gain_NS = [NS_gain(qdf["Qobs"][str(year)], qdf["HYPE"][str(year)], qdf["Qsim_full"][str(year)]) for year in range(1980, 1986)]
plt.figure(figsize=(12, 8))
#plt.bar([i for i in range(1980, 1986)], hydrograph_gain_NS, label="Hydrograph gain")
plt.bar([i for i in range(1980, 1986)], hype_gain_NS, label="HYPE gain")
plt.ylabel("NS gain")
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19509076962413704, 'fraction_numerical': 0.044745589363334184, 'mean_word_length': 5.998211091234347, 'pattern_counts': {'":': 6, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '270610', 'license': 'MIT', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1466, 'n_words': 290, 'path': 'notebooks/kwbs.ipynb', 'repo_name': 'hydrogo/2018_VinoRead_wrkshp', 'size': '3911'}