import os
import pickle
import numpy as np
from params import get_params
from sklearn.metrics.pairwise import pairwise_distances

def rank(params):

    train_features = pickle.load(open(os.path.join(params['root'],
                                 params['root_save'],params['feats_dir'],
                                 'train' + "_" + str(params['descriptor_size'])
                                 + "_" + params['descriptor_type'] +
                                 "_" + params['keypoint_type'] + '.p'),'rb'))

    for split in ['val','test']:
        features = pickle.load(open(os.path.join(params['root'],
                                   params['root_save'],params['feats_dir'],
                                   split + "_" +
                                   str(params['descriptor_size']) + "_"
                                   + params['descriptor_type'] + "_"
                                   + params['keypoint_type'] + '.p'),'rb'))


        # For each image id in the validation set
        for id in features.keys():

            # Get its feature
            bow_feats = features[id]

            # The ranking is composed with the ids of all training images
            ranking = train_features.keys()

            X = np.array(train_features.values())

            # The .squeeze() method reduces the dimensions of an array to the
            # minimum. E.g. if we have a numpy array of shape (400,1,100)
            # it will transform it to (400,100)
            distances = pairwise_distances(bow_feats,X.squeeze())


            # Sort the ranking according to the distances.
            # We convert 'ranking' to numpy.array to sort it, and then back to list
            # (although we could leave it as numpy array).
            ranking = list(np.array(ranking)[np.argsort(distances.squeeze())])

            # Save to text file
            outfile = open(os.path.join(params['root'],params['root_save'],
                           params['rankings_dir'],params['descriptor_type'],
                           split,id.split('.')[0] + '.txt'),'w')

            for item in ranking:
                outfile.write(item.split('.')[0] + '\n')
            outfile.close()

if __name__ == "__main__":

    params = get_params()
    rank(params)
import os
import pickle
import numpy as np
from params import get_params
from sklearn.metrics.pairwise import pairwise_distances

def rank(params):

    train_features = pickle.load(open(os.path.join(params['root'],
                                 params['root_save'],params['feats_dir'],
                                 'train' + "_" + str(params['descriptor_size'])
                                 + "_" + params['descriptor_type'] +
                                 "_" + params['keypoint_type'] + '.p'),'rb'))

    for split in ['val','test']:
        features = pickle.load(open(os.path.join(params['root'],
                                   params['root_save'],params['feats_dir'],
                                   split + "_" +
                                   str(params['descriptor_size']) + "_"
                                   + params['descriptor_type'] + "_"
                                   + params['keypoint_type'] + '.p'),'rb'))


        # For each image id in the validation set
        for id in features.keys():

            # Get its feature
            bow_feats = features[id]

            # The ranking is composed with the ids of all training images
            ranking = train_features.keys()

            X = np.array(train_features.values())

            # The .squeeze() method reduces the dimensions of an array to the
            # minimum. E.g. if we have a numpy array of shape (400,1,100)
            # it will transform it to (400,100)
            distances = pairwise_distances(bow_feats,X.squeeze())


            # Sort the ranking according to the distances.
            # We convert 'ranking' to numpy.array to sort it, and then back to list
            # (although we could leave it as numpy array).
            ranking = list(np.array(ranking)[np.argsort(distances.squeeze())])

            # Save to text file
            outfile = open(os.path.join(params['root'],params['root_save'],
                           params['rankings_dir'],params['descriptor_type'],
                           split,id.split('.')[0] + '.txt'),'w')

            for item in ranking:
                outfile.write(item.split('.')[0] + '\n')
            outfile.close()

if __name__ == "__main__":

    params = get_params()
    rank(params)
# For each image id in the validation set # Get its feature # The ranking is composed with the ids of all training images # The .squeeze() method reduces the dimensions of an array to the # minimum. E.g. if we have a numpy array of shape (400,1,100) # it will transform it to (400,100) # Sort the ranking according to the distances. # We convert 'ranking' to numpy.array to sort it, and then back to list # (although we could leave it as numpy array). # Save to text file