CORR_P_FAPAR_15_DOWNSAMPLED_10 = "data/correlation_p_fapar/correlation_p_fapar_ge3_15_downsampled_10.tif"
CORR_P_WTD_15_DOWNSAMPLED_10 = "data/correlation_p_wtd/correlation_p_wtd_15_downsampled_10.tif"
CORR_P_PET_WTD_15_DOWNSAMPLED_10 = "data/correlation_p_pet_wtd/correlation_p_pet_wtd_15_downsampled_10.tif"
CORR_WTD_T_15_DOWNSAMPLED_10 = "data/correlation_wtd_t/correlation_wtd_t_15_downsampled_10.tif"
CORR_P_T_15_DOWNSAMPLED_10 = "/Volumes/Elements SE/Thesis/Data/correlation_p_t/correlation_p_t_15_downsampled_10.tif"
CORR_T_FAPAR_15_DOWNSAMPLED_10 = "data/correlation_t_fapar/correlation_t_fapar_ge3_15_downsampled_10.tif"
CORR_P_PET_FAPAR_15_DOWNSAMPLED_10 = "data/correlation_p_pet_fapar/correlation_p_pet_fapar_ge3_15_downsampled_10.tif"

### Correlate WTD and fPAR
M_wtd = mp.Raster(WTD, tiles=(10, 10), window_size=15)
M_th = mp.Raster(TH, tiles=(10, 10), window_size=15)
M_fapar = mp.Raster(FAPAR, tiles=(10, 10), window_size=15)

with mp.Raster(CORR_WTD_FAPAR_15, mode='w', ref_map=WTD, tiles=(10, 10), window_size=15) as M_corr_wtd_fapar_15:
    for i in M_wtd:
        progress_bar((i + 1) / M_wtd.c_tiles)
        wtd = M_wtd[i]
        th = M_th[i]
        fapar = M_fapar[i]
        fapar[th < 3] = np.nan
        M_corr_wtd_fapar_15[i] = mp.correlate_maps(wtd, fapar, window_size=15, fraction_accepted=0.25)

M_corr_wtd_fapar_15 = mp.Raster(CORR_WTD_FAPAR_15, tiles=(10, 10))
M_corr_wtd_fapar_15.focal_mean(output_file=CORR_WTD_FAPAR_15_DOWNSAMPLED_10, window_size=10, reduce=True,
                               fraction_accepted=0)

mp.Raster.close()

### Correlate P and fPAR
M_p = mp.Raster(P, tiles=(10, 10), window_size=15)
M_th = mp.Raster(TH, tiles=(10, 10), window_size=15)
profile['nodata'] = -1
M_classes = mp.Raster(CLASSES, mode='w', profile=profile, overwrite=True)

profile['dtype'] = np.float64
M_classes_float = mp.Raster(CLASSES_FLOAT,
                            mode='w',
                            profile=profile,
                            overwrite=True)

mp.Raster.set_tiles((4, 4))

classes = np.arange(9, dtype=np.int8).reshape((3, 3))
classes_float = np.arange(9, dtype=np.float64).reshape((3, 3))

for i in M_corr_wtd:
    progress_bar((i + 1) / M_corr_wtd.c_tiles)

    corr_wtd = M_corr_wtd[i] + 1
    corr_p_pet = M_corr_p_pet[i] + 1

    mask = np.logical_or(np.isnan(corr_wtd), np.isnan(corr_p_pet))

    corr_wtd[mask] = 0
    corr_p_pet[mask] = 0

    corr_wtd = corr_wtd.astype(int)
    corr_p_pet = corr_p_pet.astype(int)

    output = classes[corr_p_pet, corr_wtd]
    output[mask] = -1
Ejemplo n.º 3
0
sign_threshold = np.full(n_max + 1, np.nan)

for n in range(0, n_max + 1):
    # The 0.178 value was obtained from 06_global_threshold_table.py
    df = n - 2 - (0.178 * n)
    x = t.ppf(0.95, df=df)
    r = x / np.sqrt(df + x**2)
    sign_threshold[n] = x / np.sqrt(df + x**2)

mp.Raster.set_window_size(window_size)
M_corr.window_size = 1
mp.Raster.set_tiles((10, 10))

for i in M_th:
    progress_bar((i + 1) / M_th.c_tiles)
    th = M_th[i]
    wtd = M_wtd[i]
    fapar = M_fapar[i]
    fapar[th < 3] = np.nan
    corr = M_corr[i]

    count_values = rolling_sum(np.logical_and(~np.isnan(fapar),
                                              ~np.isnan(wtd)),
                               window_size=15)

    threshold = sign_threshold[count_values]

    significance = np.full_like(corr, 0, dtype=np.float64)
    significance[np.isnan(corr)] = np.nan
    significance[corr < -threshold] = -1
            formula = f'{item} ~ C({parameter})'
        else:
            formula = f'{item} ~ {parameter}'

        perm_data = df.loc[:, [parameter, item]]
        try:
            model = smf.glm(formula=formula, data=perm_data, family=sm.families.NegativeBinomial()).fit(full_output=True)
        except (ValueError, statsmodels.tools.sm_exceptions.PerfectSeparationError):
            continue

        true_r2 = calc_r2(perm_data.iloc[:, 1], model.predict(perm_data))

        df_r2 = pd.DataFrame(np.full((permutations, 1), np.nan))

        for i in range(permutations):
            progress_bar((i + 1) / permutations)
            while True:
                # Sometimes a model comes up that does not solve. Simply try again.
                try:
                    perm_data = shuffle_data(perm_data)
                    random_model = smf.glm(formula=formula, data=perm_data, family=sm.families.NegativeBinomial()).fit(full_output=True)
                except (ValueError, statsmodels.tools.sm_exceptions.PerfectSeparationError):
                    pass
                else:
                    break

            df_r2.loc[i, :] = calc_r2(perm_data.iloc[:, 1], random_model.predict(perm_data))
        print(f"   {((df_r2 < true_r2).sum()/permutations)[0]}")

        df_r2.to_csv(f"data/item_variance/{parameter}_{item}.csv")