import pickle
import random


def load_data(fpath, data_size):
    """ Loads the preprocessed data pickle.
    This assumes the following:
    - image data is preprocessed with a VGGNet to (196,512) and this array is flattened with numpy C-order (default)
    - text data is preprocessed with Word2Vec to (34,52) and this array is flattened with numpy C-order (default)

    Params
    data_size: percentage of the dataset that is randomly picked

    Returns
    data: dict with key=image_id and value=(id, image encoded,caption encoded)
    """

    data = pickle.load(open(fpath, 'rb'))

    if data_size < 1.0:
        # Randomly pick data_size percentage of the dataset
        keys = list(data.keys())
        pick = random.sample(keys, int(data_size * len(keys)))
        data = {k: data[k] for k in pick}

    return data