Exemplo n.º 1
0

dict_v = {'rmPDO':str(remove_PDO), 'Period':period.split('_')[0], 'Shift':period,'Seed':'s{}'.format(rg.kwrgs_TV['seed'])}
dict_rb = dict_v.copy()
freqs = [15, 30, 60]
for f in freqs[:]:
    rg.get_ts_prec(precur_aggr=f)
    rg.df_data = rg.df_data.rename({'2ts':f'{west_east[0].capitalize()}-T',
                                    '0..0..z500_sp':f'{west_east[0].capitalize()}-RW',
                                    '0..0..SST_sp':'SST',
                                    '0..2..sm':'SM'}, axis=1)
    keys = [f'{west_east[0].capitalize()}-T', f'{west_east[0].capitalize()}-RW',
            'SST']
    if remove_PDO:
        rg.df_data['SST'], fig = wPCMCI.df_data_remove_z(rg.df_data.copy(), z=['PDO'],
                                                         keys=['SST'],
                                                         standardize=False,
                                                         plot=True)
        fig_path = os.path.join(rg.path_outsub1, f'regressing_out_PDO_tf{f}')
        fig.savefig(fig_path+rg.figext, bbox_inches='tight')

    # interannualSST = rg.df_data[['SST']].rolling(int((365*2)/f), min_periods=1,center=True).mean()
    # interannualSST = interannualSST.rename({'SST':r'$SST_{lwp}$'}, axis=1)
    # diff = rg.df_data[['SST']].sub(interannualSST.values, level=1)
    # diff = diff.rename({'SST':'SST-SST_lwp'},axis=1)
    # interannualSST.merge(diff.merge(rg.df_data[['SST', 'PDO']], left_index=True, right_index=True),
    #                      left_index=True, right_index=True).loc[0].plot()
    # rg.df_data = rg.df_data.merge(diff, left_index=True, right_index=True)
    # keys = [f'{west_east[0].capitalize()}-RW','SST']
    # rg.df_data[keys] = wPCMCI.df_data_remove_z(rg.df_data, z=['PDO'], keys=keys)

    if f == 15:
Exemplo n.º 2
0
        dmc[month] = rg.list_for_MI[0].prec_labels.copy()

    alphas = np.append([1E-3, 1E-2, 1E-1], np.logspace(.1, 2, num=25))
    kwrgs_model = {
        'scoring': 'neg_mean_squared_error',
        'alphas': alphas,  # large a, strong regul.
        'normalize': False
    }
    print('\n', month, '\n')
    keys = [k for k in rg.df_data.columns[:-2] if k not in [rg.TV.name, 'PDO']]
    if target == 'easterntemp':
        keys = [k for k in keys if int(k.split('..')[1]) in [1, 2]]
    if remove_PDO:
        y_keys = [k for k in keys if 'sst' in k]
        rg.df_data[y_keys], fig = wPCMCI.df_data_remove_z(rg.df_data,
                                                          z=['PDO'],
                                                          keys=y_keys,
                                                          standardize=False)
        fig_path = os.path.join(rg.path_outsub1,
                                f'regressing_out_PDO_tf{month}')
        fig.savefig(fig_path + rg.figext, bbox_inches='tight')

    if any(rg._df_count == rg.n_spl):  # at least one timeseries always present
        oneyr = functions_pp.get_oneyr(
            rg.df_data['RV_mask'].loc[0][rg.df_data['RV_mask'].loc[0]])
        oneyrsize = oneyr.size
        if monthkeys.index(month) >= 1:
            nextyr = functions_pp.get_oneyr(
                rg.df_data['RV_mask'].loc[0][rg.df_data['RV_mask'].loc[0]])
            if nextyr.size != oneyrsize:
                raise ValueError
y_keys = [k for k in rg.df_data.columns[:-2] if k not in df_PDOs.columns]
y_keys = [k for k in y_keys
          if k not in [rg.TV.name]]  # also remove LFV from target?

keys = [k for k in y_keys
        if k not in [rg.TV.name]]  # not use target as precursor
keys = [k for k in y_keys if int(k.split('..')[1]) in region_labels]

# remove PDO low-pass
df_data_r2PDO = rg.df_data[['1..1..sst', '1..2..sst', 'PDO0.5rm']].copy()
df_data_r2PDO = rg.get_subdates_df(df_data_r2PDO, years=(1980, 2020))
# df_data_r2PDO = df_data_r2PDO.rename(rename_labels_d, axis=1) ; y_keys = list(rename_labels_d.values())
df_data_r2PDO[y_keys], fig = wPCMCI.df_data_remove_z(
    df_data_r2PDO,
    z_keys=[f'PDO{lowpass}{lwp_method}'],
    lag_z=1,
    keys=y_keys,
    standardize=False)
fig_path = os.path.join(rg.path_outsub1,
                        f'{sst._name}_r{lowpass}PDO_{period}_{append_str}')
fig.savefig(fig_path + rg.figext, bbox_inches='tight')
df_data_r2PDO.loc[0][keys].corrwith(rg.df_data.loc[0][keys]).plot(kind='bar')

# =============================================================================
# Predictions
# =============================================================================
# out_regr2PDO = prediction_wrapper(df_data_r2PDO.copy(), keys=keys,
#                                  match_lag=match_lag, n_boot=n_boot)
dates = core_pp.get_subdates(rg.dates_TV, start_end_year=(1980, 2020))
target_ts_temp = rg.TV.RV_ts.loc[dates]
clim_mean_temp = float(target_ts_temp.mean())
Exemplo n.º 4
0
    keys = [
        f'$RW^{west_east[0].capitalize()}$',
        f'$SST^{west_east[0].capitalize()}$'
    ]
    rg.df_data = rg.df_data.rename(
        {
            '0..0..z500_sp': keys[0],
            '0..0..N-Pac. SST_sp': keys[1]
        }, axis=1)

    if remove_PDO:
        rg.df_data = rg.get_subdates_df(years=(1980, 2020))
        rg.df_data[keys[1:]], fig = wPCMCI.df_data_remove_z(
            rg.df_data.copy(),
            z_keys=['PDO0.5rm'] + keys[:1],
            keys=keys[1:],
            lag_z=[1],
            standardize=False,
            plot=True)
        fig_path = os.path.join(rg.path_outsub1, f'regressing_out_PDO_tf{f}')
        fig.savefig(fig_path + rg.figext, bbox_inches='tight')
        # ensure no NaNs in data
        rg.df_data = rg.get_subdates_df(start_end_date=('04-01', '08-31'))
        # df_new, _ = wPCMCI.df_data_remove_z(df_data,
        #                                                 z_keys=['PDO0.5rm'],
        #                                                 keys=keys[1:],
        #                                                 lag_z=[1],
        #                                                 standardize=False,
        #                                                 plot=True)
        # df_new = df_new.merge(rg.df_data[['TrainIsTrue', 'RV_mask']], left_index=True, right_index=True)