def main(CONFIG, return_m=False, force_restore=False):
    X = np.load('../data/data_x.npy', allow_pickle=True) #Aggr, Points
    Y = np.load('../data/data_y.npy', allow_pickle=True)

    xs = np.load('../data/data_xs.npy', allow_pickle=True) #100 , 50
    ys = np.load('../data/data_ys.npy', allow_pickle=True)

    print(X[0].shape)
    print(X[1].shape)


    i = 0
    #z_r = kmeans2(X[i].reshape([X[i].shape[0]*X[i].shape[1], 1]), 10, minit='points')[0] 
    z_r = X[i].reshape([X[i].shape[0]*X[i].shape[1], 1])

    dataset = get_dataset(X, Y, z_r)
    context = get_context(CONFIG, X, Y)

    #elbo_model =  gprn.models.GPAggr
    elbo_model =  gprn.models.GPRN_Aggr

    if CONFIG['composite_w']:
        context.parameters = gprn.composite_corrections.MagnitudeCorrection
        #context.parameters = gprn.composite_corrections.CurvatureCorrection


    m = gprn.GPRN(
        model = elbo_model,
        context = context,
        data = dataset
    )

    m.optimise(context.train_flag, context.restore_flag)

    prediction(CONFIG, m, X, xs)
Beispiel #2
0
def main(CONFIG, return_m=False, force_restore=False):
    X = np.load('../data/data_with_features/processed_x_train.npy', allow_pickle=True) #[SAT, LAQN]
    Y = np.load('../data/data_with_features/processed_y_train.npy', allow_pickle=True)

    xs = np.load('../data/data_with_features/processed_x_test.npy', allow_pickle=True) #100 , 50
    xs_grid = np.load('../data/data_with_features/processed_x_test_grid.npy', allow_pickle=True) #100 , 50
    xs_small_grid = np.load('../data/data_with_features/processed_x_test_small_grid.npy', allow_pickle=True)

    xs_small_fine_grid = np.load('../data/data_with_features/processed_x_test_small_fine_grid.npy', allow_pickle=True)

    X[0] = np.array(X[0])
    X[1] = np.array(X[1])
    Y[0] = np.array(Y[0])
    Y[1] = np.array(Y[1])

    print(X[0].shape)
    print(X[1].shape)
    print(Y[0].shape)
    print(Y[1].shape)


    #ys = np.load('data/data_ys.npy')

    num_z = 500
    #num_z = 100

    i = 0
    z_r = kmeans2(X[i].reshape([X[i].shape[0]*X[i].shape[1], X[i].shape[2]]), num_z, minit='points')[0] 
    #z_r = X[i].reshape([X[i].shape[0]*X[i].shape[1], X[i].shape[2])

    dataset = get_dataset(CONFIG, X, Y, z_r)
    context = get_context(CONFIG, X, Y)

    elbo_model =  gprn.models.GPAggr

    #context.parameters = gprn.composite_corrections.MagnitudeCorrection

    m = gprn.GPRN(
        model = elbo_model,
        context = context,
        data = dataset
    )

    m.optimise(context.train_flag, context.restore_flag)

    prediction(CONFIG, m, X, xs, xs_grid, xs_small_grid, xs_small_fine_grid, plot_grid=True)
Beispiel #3
0
def run(CONFIG, test=None, r=1, return_m=False, force_restore=False):
    test_id = test['id']
    print('test_id:', test_id)
    tf.reset_default_graph()
    print(CONFIG)
    CONFIG['num_layers'] = 2
    num_z = CONFIG['z']

    X = np.load('../data/data_x_{test_id}_{r}.npy'.format(test_id=test_id,
                                                          r=r),
                allow_pickle=True)
    Y = np.load('../data/data_y_{test_id}_{r}.npy'.format(test_id=test_id,
                                                          r=r),
                allow_pickle=True)

    print(np.min(X[0]), np.min(X[1]))
    print(np.max(X[0]), np.max(X[1]))

    #add empty Y columns
    #Y[0] is task 1
    Y[0] = np.concatenate([np.empty(Y[0].shape), Y[0]], axis=1)
    Y[0][:, 0] = np.nan

    Y[1] = np.concatenate([Y[1], np.empty(Y[1].shape)], axis=1)
    Y[1][:, 1] = np.nan

    XS = np.load('../data/data_xs_{test_id}.npy'.format(test_id=test_id),
                 allow_pickle=True)
    XS_VIS = np.load(
        '../data/data_x_vis_{test_id}.npy'.format(test_id=test_id),
        allow_pickle=True)

    rs = lambda x: x.reshape(x.shape[0] * x.shape[1], x.shape[2])

    XS = rs(XS)

    i = 0
    x = X[i].reshape([X[i].shape[0] * X[i].shape[1], X[i].shape[2]])
    if num_z is None or num_z > x.shape[0]:
        z_x = x
    else:
        z_x = kmeans2(x, num_z, minit='points')[0]

    i = 0
    x = X[i].reshape([X[i].shape[0] * X[i].shape[1], X[i].shape[2]])
    if num_z is None or num_z > x.shape[0]:
        z_r = x
    else:
        z_r = kmeans2(x, num_z, minit='points')[0]

    #z_r = X[i].reshape([X[i].shape[0]*X[i].shape[1], 1])

    Z = np.array([z_r, z_x])

    dataset = get_dataset(CONFIG, X, Y, z_r)
    context = get_context(CONFIG, X, Y, test=test_id, r=1)

    #elbo_model =  gprn.models.GPRN_Aggr
    elbo_model = gprn.models.GPRN_Aggr

    #context.parameters = gprn.composite_corrections.MagnitudeCorrection

    m = gprn.GPRN(model=elbo_model, context=context, data=dataset)

    m.optimise(context.train_flag, context.restore_flag)

    df_all, df_train, df_test = setup_data.get_site_1(test, root='../')

    y_pred, y_var = m.predict(rs(X[1]), r=1)

    #get task 0
    target_task = 0
    y_pred = np.expand_dims(y_pred[:, target_task], -1)
    y_var = np.expand_dims(y_var[:, target_task], -1)

    ys_pred, ys_var = m.predict(XS, r=1)
    ys_pred = np.expand_dims(ys_pred[:, target_task], -1)
    ys_var = np.expand_dims(ys_var[:, target_task], -1)

    y_vis_pred, y_vis_var = m.predict(rs(XS_VIS), r=1)
    y_vis_pred = np.expand_dims(y_vis_pred[:, target_task], -1)
    y_vis_var = np.expand_dims(y_vis_var[:, target_task], -1)

    if False:
        y_pred = setup_data.denormalise_wrt(y_pred,
                                            np.array(df_train['pm10']),
                                            sphere_flag=True)
        ys_pred = setup_data.denormalise_wrt(ys_pred,
                                             np.array(df_train['pm10']),
                                             sphere_flag=True)
        y_vis_pred = setup_data.denormalise_wrt(y_vis_pred,
                                                np.array(df_train['pm10']),
                                                sphere_flag=True)

    pred_y = np.concatenate([y_pred, y_var], axis=1)
    pred_ys = np.concatenate([ys_pred, ys_var], axis=1)
    pred_y_vis = np.concatenate([y_vis_pred, y_vis_var], axis=1)

    std = np.nanstd(np.array(df_train['pm10']), axis=0)
    y_var = y_var * std**2
    ys_var = ys_var * std**2
    y_vis_var = y_vis_var * std**2

    np.save(
        '../results/{prefix}_y_{test_id}_{r}'.format(
            prefix=CONFIG['file_prefix'], r=r, test_id=test_id), pred_y)
    np.save(
        '../results/{prefix}_ys_{test_id}_{r}'.format(
            prefix=CONFIG['file_prefix'], r=r, test_id=test_id), pred_ys)
    np.save(
        '../results/{prefix}_y_vis_{test_id}_{r}'.format(
            prefix=CONFIG['file_prefix'], r=r, test_id=test_id), pred_y_vis)