def test_single_percentile_data(): n = 1000 x = np.arange(n, dtype=np.float) y = np.ones(n) s = scaling.lin_cdf_match(y, x) nptest.assert_almost_equal(s, np.full_like(s, np.nan)) s = scaling.cdf_match(y, x) nptest.assert_almost_equal(s, np.full_like(s, np.nan))
def compare_ssm_index(index, warp_gpi, sm_dataset, start=None, end=None, weekly=True, plot=False): df = ssm_iwmi.IWMI_read_csv() iwmi_bare, iwmi_crop = ssm_iwmi.IWMI_ts_index(df, index) if sm_dataset == 'cci': sm_ts = ssm_TUW.read_CCI(warp_gpi, start, end) if sm_dataset == 'ascat': sm_ts = ssm_TUW.read_ASCAT_ssm(warp_gpi) if sm_dataset == 'ers': sm_ts = ssm_TUW.read_ERS_ssm(warp_gpi, args=['sm'], start=None, end=None) if weekly == True: iwmi_bare_weekly = iwmi_bare.resample('W', how='mean').dropna() iwmi_crop_weekly = iwmi_crop.resample('W', how='mean').dropna() sm_ts_weekly = sm_ts.resample('W', how='mean').dropna() # correlation of bare and crop if len(iwmi_bare) != 0 and len(iwmi_crop) != 0: iwmi_bare_weekly.columns.values[0] = 1 match_bare_crop = temp_match.matching(iwmi_crop_weekly, iwmi_bare_weekly) corr_crop_bare = metrics.spearmanr(match_bare_crop.iloc[:, 0], match_bare_crop.iloc[:, 1])[0] print corr_crop_bare if len(iwmi_bare) == 0: iwmi_bare_weekly = iwmi_bare_weekly else: match_bare = temp_match.matching(sm_ts_weekly, iwmi_bare_weekly) iwmi_bare_weekly_match = match_bare.iloc[:,1] sm_ts_weekly_bare = match_bare.iloc[:,0] iwmi_bare_resc = scaling.lin_cdf_match(iwmi_bare_weekly.iloc[:,0], sm_ts_weekly) iwmi_bare_weekly = pd.DataFrame(iwmi_bare_resc, index=iwmi_bare_weekly.index) corr_bare = metrics.spearmanr(iwmi_bare_weekly_match, sm_ts_weekly_bare)[0] if len(iwmi_crop) == 0: iwmi_crop_weekly = iwmi_crop_weekly else: match_crop = temp_match.matching(sm_ts_weekly, iwmi_crop_weekly) iwmi_crop_weekly_match = match_crop.iloc[:, 1] sm_ts_weekly_crop = match_crop.iloc[:,0] iwmi_crop_resc = scaling.lin_cdf_match(iwmi_crop_weekly.iloc[:,0], sm_ts_weekly) iwmi_crop_weekly = pd.DataFrame(iwmi_crop_resc, index=iwmi_crop_weekly.index) corr_crop = metrics.spearmanr(iwmi_crop_weekly_match, sm_ts_weekly_crop)[0] else: if len(iwmi_bare) == 0: iwmi_bare_resc = iwmi_bare else: iwmi_bare_resc = scaling.lin_cdf_match(iwmi_bare, sm_ts) if len(iwmi_crop) == 0: iwmi_crop_resc = iwmi_crop else: iwmi_crop_resc = scaling.lin_cdf_match(iwmi_crop, sm_ts) iwmi_bare = pd.DataFrame(iwmi_bare_resc, index=iwmi_bare.index) iwmi_crop = pd.DataFrame(iwmi_crop_resc, index=iwmi_crop.index) if plot == True: if weekly == True: sm_ts_plot = sm_ts_weekly iwmi_bare_plot = iwmi_bare_weekly iwmi_crop_plot = iwmi_crop_weekly else: sm_ts_plot = sm_ts iwmi_bare_plot = iwmi_bare iwmi_crop_plot = iwmi_crop ax = sm_ts_plot.plot(color='b') if len(iwmi_crop_plot) != 0: iwmi_crop_plot.plot(color='r', ax=ax) if len(iwmi_bare_plot) != 0: iwmi_bare_plot.plot(color='g', ax=ax) plt.legend([sm_dataset+' ts', 'iwmi crop, index '+str(index), 'iwmi bare, index '+str(index)]) if sm_dataset in ['ers', 'ascat']: plt.ylabel('degree of saturation [%]') if 'corr_crop_bare' in locals(): plt.title('corr_bare_crop ='+str(round(corr_crop_bare, 3))) plt.ylim([0, 140]) else: plt.ylabel('volumetric soil moisture [m3/m3]') if 'corr_bare' in locals() and 'corr_crop' in locals(): plt.title('corr_bare = '+str(round(corr_bare,3))+ ' corr_crop = '+str(round(corr_crop,3))+ '\n corr_bare_crop = '+str(round(corr_crop_bare,3))) elif 'corr_bare' in locals(): plt.title('corr_bare = '+str(round(corr_bare,3))) elif 'corr_crop' in locals(): plt.title('corr_crop = '+str(round(corr_crop,3))) plt.ylim([0, 100]) plt.grid() #plt.show() plt.savefig(os.path.join(root.x, 'staff', 'ipfeil', 'iwmi_plots', sm_dataset, sm_dataset+'_cdf_'+str(index)+'.png')) plt.clf() return iwmi_bare_plot, iwmi_crop_plot, sm_ts_plot
amsre_dat_obj = datasets.DatasetTs(amsre_datfile) amsre_gpi_data = amsre_dat_obj.read_ts(gpi) tmi_dat_obj = datasets.DatasetTs(tmi_datfile) tmi_gpi_data = tmi_dat_obj.read_ts(gpi) print amsre_gpi_data.dtype, amsre_gpi_data.shape print tmi_gpi_data.dtype, tmi_gpi_data.shape # In CDF evaluation should be only values without Nan wh = amsre_gpi_data['sm'] > -100 amsre_gpi_data['sm'][wh] wh2 = tmi_gpi_data['sm'] > -100 tmi_gpi_data scalled_data = scaling.lin_cdf_match(tmi_gpi_data['sm'][wh2], amsre_gpi_data['sm'][wh]) print tmi_gpi_data['sm'].shape print scalled_data.shape # Estimating the parameters in_data = tmi_gpi_data['sm'][wh2] scale_to = amsre_gpi_data['sm'][wh] print "In data:", in_data print "Scale to data:", scale_to percentiles = [0, 5, 10, 30, 50, 70, 90, 95, 100] in_data_pctl = np.array(np.percentile(in_data, percentiles)) scale_to_pctl = np.array(np.percentile(scale_to, percentiles)) uniq_ind = np.unique(in_data_pctl, return_index=True)[1]