def test_nash_sutcliff(): """ test nash sutcliff """ x = random.randn(10) ok_(scores.nash_sutcliffe(x, x) == 1) ok_(scores.nash_sutcliffe(x, 5 * x) > 0) ok_(scores.nash_sutcliffe(x, -x) < 0)
def plot_total_precip_and_temp_re_1d(ax_pr, ax_temp, crcm5_manager, rot_latlon_projection, areas2d, model_data, mask=None): """ plot relative error of total precipitation in time """ assert isinstance(crcm5_manager, Crcm5ModelDataManager) assert isinstance(ax_pr, Axes) if mask is None: i_model0, j_model0 = model_data.metadata["ix"], model_data.metadata[ "jy"] mask = crcm5_manager.get_mask_for_cells_upstream(i_model0, j_model0) #print model_data.time[0], model_data.time[-1] #####Precipitation ts_prec_mod, dt_mod = crcm5_manager.get_monthly_sums_over_points( mask, "PR", areas2d=areas2d, start_date=model_data.time[0], end_date=model_data.time[-1]) cruManager = CRUDataManager( path="data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.pre.dat.nc", var_name="pre") ts_prec_obs = cruManager.get_monthly_timeseries_using_mask( mask, crcm5_manager.lons2D, crcm5_manager.lats2D, areas2d, start_date=model_data.time[0], end_date=model_data.time[-1]) mod = np.array( ts_prec_mod.data) * dt_mod.seconds * 1000.0 #converting to mm/month sta = np.array(ts_prec_obs.data) #CRU data used was initially in mm/month #print sta #print mod #print mod.shape #print sta.shape assert len(sta) == len(mod) ax_pr.annotate("r = {0:.2f}".format(float(np.corrcoef([mod, sta])[0, 1])), xy=(0.7, 0.8), xycoords="axes fraction") ax_pr.annotate("ns = {0:.2f}".format(scores.nash_sutcliffe(mod, sta)), xy=(0.7, 0.9), xycoords="axes fraction") ax_pr.plot(ts_prec_mod.time, (mod - sta) / sta, color="k", linewidth=2) ax_pr.xaxis.set_major_formatter(DateFormatter("%y/%m")) ax_pr.xaxis.set_major_locator(MonthLocator(bymonth=list(range(1, 13, 2)))) ax_pr.set_title("CRCM5 versus CRU") ax_pr.set_ylabel("Total Precip (Pmod - Pobs)/Pobs") #Temperature ts_temp_mod = crcm5_manager.get_spatial_integral_over_mask_of_dyn_field( mask, areas2d, var_name="TT", level=1000, level_kind=level_kinds.PRESSURE, path_to_folder=crcm5_manager.samples_folder, file_prefix="dp") print("ts_temp_mod time interval: ", ts_temp_mod.time[0], ts_temp_mod.time[-1]) ts_temp_mod = ts_temp_mod.time_slice(model_data.time[0], model_data.time[-1]) cruManager = CRUDataManager( path="data/cru_data/CRUTS3.1/cru_ts_3_10.1901.2009.tmp.dat.nc", var_name="tmp") ts_temp_obs = cruManager.get_monthly_timeseries_using_mask( mask, crcm5_manager.lons2D, crcm5_manager.lats2D, areas2d, start_date=model_data.time[0], end_date=model_data.time[-1]) mod = np.array(ts_temp_mod.get_ts_of_monthly_means().data) / sum( areas2d[mask == 1]) #converting to mm/month sta = np.array(ts_temp_obs.data) / sum( areas2d[mask == 1]) #CRU data used was initially in mm/month assert len(sta) == len(mod) ax_temp.annotate("r = {0:.2f}".format(float(np.corrcoef([mod, sta])[0, 1])), xy=(0.7, 0.8), xycoords="axes fraction") ax_temp.annotate("ns = {0:.2f}".format(scores.nash_sutcliffe(mod, sta)), xy=(0.7, 0.9), xycoords="axes fraction") ax_temp.plot(ts_prec_mod.time, (mod - sta), color="k", linewidth=2) ax_temp.xaxis.set_major_formatter(DateFormatter("%y/%m")) ax_temp.xaxis.set_major_locator( MonthLocator(bymonth=list(range(1, 13, 2)))) ax_temp.set_title("CRCM5 versus CRU") ax_temp.set_ylabel("Temperature, deg., Tmod - Tobs") pass