Exemplo n.º 1
0
def depthxc_estimates(d1, d2, BASIS=BASIS, THETA=THETA, COV=COV):
    n_params = THETA.shape[0]

    bounds_x1 = (-8, +8)
    bounds_x2 = (-2, +20)
    shape = (100, 400)
    test_X = grid(bounds_x1, bounds_x2, *shape)

    xc_filt = ((test_X[:, 1] >= d1) * (test_X[:, 1] < d2))

    D = np.sort(np.unique(test_X[:, 0]))
    n_d = len(D)
    DEPTH_IDX = 20

    predmat = np.zeros((DEPTH_IDX, n_d))
    maskmat = np.zeros((DEPTH_IDX, n_d))

    for tidx, d in enumerate(D):
        idxs = xc_filt * (test_X[:, 0] == d)
        x_pts = test_X[idxs, 1]  # 1 x 6
        y_pts = BASIS[idxs, :]  # 6 x 394
        # trap integration for each column
        int_basis = np.zeros(n_params)
        var = np.zeros(DEPTH_IDX)
        for pidx in range(n_params):
            int_basis[pidx] = np.trapz(y_pts[:, pidx], x_pts)
        for depth_idx in range(DEPTH_IDX):
            var[depth_idx] = np.linalg.multi_dot(
                (int_basis, COV[:, :, depth_idx], int_basis))
        inprod = np.dot(int_basis, THETA)
        predmat[:, tidx] = inprod / (max(x_pts) - min(x_pts))
        sd = np.sqrt(var)
        maskmat[:, tidx] = (inprod > 2 * sd) | (inprod < -2 * sd)
    predmat_small = predmat
    maskmat_small = maskmat
    predmat = np.zeros((DEPTH_IDX, n_d * 4))
    maskmat = np.zeros((DEPTH_IDX, n_d * 4))
    xnew = np.linspace(-8, +8, n_d * 4)
    for depth_idx in range(DEPTH_IDX):
        predmat[depth_idx, :] = np.interp(xnew, D, predmat_small[depth_idx, :])
        maskmat[depth_idx, :] = np.repeat(maskmat_small[depth_idx, :], 4)
    mat = predmat.copy()
    mat[~maskmat.astype(bool)] = np.nan
    return mat
def depthtime_estimates(center, ws=0.5, BASIS=BASIS, THETA=THETA, COV=COV):
    ws = 0.5
    n_params = THETA.shape[0]

    bounds_x1 = (-8, +8)
    bounds_x2 = (-2, +20)
    shape = (100, 400)
    test_X = grid(bounds_x1, bounds_x2, *shape)

    xc_filt = ((test_X[:, 0] > center - ws) * (test_X[:, 0] < center + ws))

    TAU = np.sort(np.unique(test_X[:, 1]))
    n_tau = len(TAU)
    DEPTH_IDX = 20

    predmat = np.zeros((DEPTH_IDX, n_tau))
    maskmat = np.zeros((DEPTH_IDX, n_tau))

    for tidx, tau in enumerate(TAU):
        idxs = xc_filt * (test_X[:, 1] == tau)
        x_pts = test_X[idxs, 0]  # 1 x 6
        y_pts = BASIS[idxs, :]  # 6 x 394
        # trap integration for each column
        int_basis = np.zeros(n_params)
        var = np.zeros(DEPTH_IDX)
        for pidx in range(n_params):
            int_basis[pidx] = np.trapz(y_pts[:, pidx], x_pts)
        for depth_idx in range(DEPTH_IDX):
            var[depth_idx] = np.linalg.multi_dot(
                (int_basis, COV[:, :, depth_idx], int_basis))
        inprod = np.dot(int_basis, THETA)
        predmat[:, tidx] = inprod / (max(x_pts) - min(x_pts))
        sd = np.sqrt(var)
        maskmat[:, tidx] = (inprod > 2 * sd) | (inprod < -2 * sd)
    mat = predmat.copy()
    mat[~maskmat.astype(bool)] = np.nan
    return mat
Exemplo n.º 3
0
# lamb_choice = 'AdaptSignalInflate'
#lamb_choice = 'AdaptInflate' # This seems the selected one based on the description in the paper
lamb_choice = 'Custom'

# Loop through all cases
for sub in sub_list:
    print(sub)
    # Load output from B10 (Argo profile data)
    df = pkl.load(
        open(f'{data_dir}/HurricaneVariableCovDF_Subset{sub}.pkl', 'rb'))

    X = np.array(df[['standard_signed_angle', 'hurricane_dtd']]).copy()

    bounds_x1 = (-8, +8)
    bounds_x2 = (-2, +20)
    train_knots = grid(bounds_x1, bounds_x2, 33, 45)
    n_param = train_knots.shape[0] + 3
    shape = (100, 400)
    test_X = grid(bounds_x1, bounds_x2, *shape)
    n_test = test_X.shape[0]

    y = variable_diff(df, stage, 0)
    ymat = np.zeros((1, DEPTH_IDX, len(y)))

    for depth_idx in range(DEPTH_IDX):
        ymat[0, depth_idx, :] = variable_diff(df, stage, depth_idx)

    # Load results from B32 (lambda values and LOOCV scores)
    #LAMB_SMALL, LOOCV_SMALL = pkl.load(open(f'{data_dir}/B32_LOOCV_Data_{sub}.pkl', 'rb'))
    # Load results from B35 (lambda values and LOOCV scores)
    LAMB_LARGE, LOOCV_LARGE = pkl.load(
        BASIS = pkl.load(
            open(f'{tps_est_dir}/TPS_Basis_Block_0.5_{filt}.pkl', 'rb'))
        THETA = pkl.load(
            open(f'{tps_est_dir}/TPS_Theta_Block_0.5_{filt}.pkl', 'rb'))
        COV = pkl.load(
            open(f'{tps_est_dir}/TPS_CovTheta_Block_0.5_{filt}.pkl', 'rb'))

        center = 0  #int(sys.argv[1]) da cambiare a mano in base al cross track angle che si vuole valutare
        ws = 0.5
        n_params = THETA.shape[0]

        bounds_x1 = (-8, +8)
        bounds_x2 = (-2, +20)
        shape = (100, 400)
        test_X = grid(bounds_x1, bounds_x2, *shape)

        xc_filt = ((test_X[:, 0] > center - ws) * (test_X[:, 0] < center + ws))

        TAU = np.sort(np.unique(test_X[:, 1]))
        n_tau = len(TAU)
        grid_start = int(grid_lower)
        grid_end = int(grid_upper)
        grid_stride = int(grid_stride)
        DEPTH_IDX = int((grid_end - grid_start) / grid_stride + 1)

        predmat = np.zeros((DEPTH_IDX, n_tau))
        maskmat = np.zeros((DEPTH_IDX, n_tau))

        for tidx, tau in enumerate(TAU):
            idxs = xc_filt * (test_X[:, 1] == tau)
Exemplo n.º 5
0
 def __init__(self, lamb, knots=grid((-8, 8), (-2, 20), 17, 23)):
     self.lamb = lamb
     self.knots = knots
     self.train_X = NULL
     self.train_y = NULL
    sub_list = (
        'all_combined',
        'all_hurricanes',
        'all_tstd',
    )

for sub in sub_list:
    print(sub)
    df = pkl.load(
        open(f'{data_dir}/HurricaneVariableCovDF_Subset{sub}.pkl', 'rb'))

    X = np.array(df[['standard_signed_angle', 'hurricane_dtd']]).copy()

    bounds_x1 = (-8, +8)
    bounds_x2 = (-2, +20)
    train_knots = grid(bounds_x1, bounds_x2, 17, 23)
    n_param = train_knots.shape[0] + 3
    shape = (100, 400)
    test_X = grid(bounds_x1, bounds_x2, *shape)
    n_test = test_X.shape[0]

    PREDS_NOREW = np.zeros((n_test, DEPTH_IDX))
    PREDS_DIAG = np.zeros((n_test, DEPTH_IDX))
    PREDS_BLOCK = np.zeros((n_test, DEPTH_IDX))
    STDEV_DIAG = np.zeros((n_test, DEPTH_IDX))
    STDEV_BLOCK = np.zeros((n_test, DEPTH_IDX))
    MASK_DIAG = np.zeros((n_test, DEPTH_IDX))
    MASK_BLOCK = np.zeros((n_test, DEPTH_IDX))
    THETA_BLOCK = np.zeros((n_param, DEPTH_IDX))
    BASIS_BLOCK = np.zeros((n_test, n_param))  # Invariant to depth
    COV_THETA_BLOCK = np.zeros((n_param, n_param, DEPTH_IDX))