# Path: basic/common.py
# def printStatus(trace, message):
#     printMessage('INFO', trace, message)
# 
# Path: simpleknn/bigfile.py
# class BigFile:
# 
#     def __init__(self, datadir):
#         self.nr_of_images, self.ndims = map(int, open(os.path.join(datadir,'shape.txt')).readline().split())
#         id_file = os.path.join(datadir, "id.txt")
#         self.names = open(id_file).read().strip().split()
#         assert(len(self.names) == self.nr_of_images)
#         self.name2index = dict(zip(self.names, range(self.nr_of_images)))
#         self.binary_file = os.path.join(datadir, "feature.bin")
#         print ("[%s] %dx%d instances loaded from %s" % (self.__class__.__name__, self.nr_of_images, self.ndims, datadir))
# 
# 
#     def read(self, requested, isname=True):
#         requested = set(requested)
#         if isname:
#             index_name_array = [(self.name2index[x], x) for x in requested if x in self.name2index]
#         else:
#             assert(min(requested)>=0)
#             assert(max(requested)<len(self.names))
#             index_name_array = [(x, self.names[x]) for x in requested]
#         if len(index_name_array) == 0:
#             return [], []
#        
#         index_name_array.sort(key=lambda v:v[0])
#         sorted_index = [x[0] for x in index_name_array]
# 
#         nr_of_images = len(index_name_array)
#         vecs = [None] * nr_of_images
#         offset = np.float32(1).nbytes * self.ndims
#         
#         res = array.array('f')
#         fr = open(self.binary_file, 'rb')
#         fr.seek(index_name_array[0][0] * offset)
#         res.fromfile(fr, self.ndims)
#         previous = index_name_array[0][0]
#  
#         for next in sorted_index[1:]:
#             move = (next-1-previous) * offset
#             #print next, move
#             fr.seek(move, 1)
#             res.fromfile(fr, self.ndims)
#             previous = next
# 
#         fr.close()
# 
#         return [x[1] for x in index_name_array], [ res[i*self.ndims:(i+1)*self.ndims].tolist() for i in range(nr_of_images) ]
# 
# 
#     def read_one(self, name):
#         renamed, vectors = self.read([name])
#         return vectors[0]    
# 
#     def shape(self):
#         return [self.nr_of_images, self.ndims]


import os
import numpy as np
from basic.common import printStatus
from simpleknn.bigfile import BigFile
DEFAULT_BLOCK_SIZE = 2000

INFO = os.path.basename(__file__)

class Image2Vec:

    def __init__(self, label_file, label2vec_dir):
        self.labels = map(str.strip, open(label_file).readlines())
        self.nr_of_labels = len(self.labels)
        feat_file = BigFile(label2vec_dir)
        renamed, vectors = feat_file.read(self.labels)
        name2index = dict(zip(renamed, range(len(renamed))))
        self.label_vectors = [None] * self.nr_of_labels
        self.feat_dim = feat_file.ndims

        for i in xrange(self.nr_of_labels):
            idx = name2index.get(self.labels[i], -1)
            self.label_vectors[i] = np.array(vectors[idx]) if idx >= 0 else None

        nr_of_inactive_labels = len([x for x in self.label_vectors if x is None])    
        printStatus(INFO, '#active_labels=%d, embedding_size=%d' % (self.nr_of_labels - nr_of_inactive_labels, self.feat_dim))



    def embedding(self, prob_vec, k=10):
        assert(len(prob_vec) == self.nr_of_labels), 'len(prob_vec)=%d, nr_of_labels=%d' % (len(prob_vec), self.nr_of_labels)
        top_hits = np.argsort(prob_vec)[::-1][:k]
        new_vec = np.array([0.] * self.feat_dim)

        Z = 0.

# Path: basic/common.py
# def printStatus(trace, message):
#     printMessage('INFO', trace, message)
# 
# Path: simpleknn/bigfile.py
# class BigFile:
# 
#     def __init__(self, datadir):
#         self.nr_of_images, self.ndims = map(int, open(os.path.join(datadir,'shape.txt')).readline().split())
#         id_file = os.path.join(datadir, "id.txt")
#         self.names = open(id_file).read().strip().split()
#         assert(len(self.names) == self.nr_of_images)
#         self.name2index = dict(zip(self.names, range(self.nr_of_images)))
#         self.binary_file = os.path.join(datadir, "feature.bin")
#         print ("[%s] %dx%d instances loaded from %s" % (self.__class__.__name__, self.nr_of_images, self.ndims, datadir))
# 
# 
#     def read(self, requested, isname=True):
#         requested = set(requested)
#         if isname:
#             index_name_array = [(self.name2index[x], x) for x in requested if x in self.name2index]
#         else:
#             assert(min(requested)>=0)
#             assert(max(requested)<len(self.names))
#             index_name_array = [(x, self.names[x]) for x in requested]
#         if len(index_name_array) == 0:
#             return [], []
#        
#         index_name_array.sort(key=lambda v:v[0])
#         sorted_index = [x[0] for x in index_name_array]
# 
#         nr_of_images = len(index_name_array)
#         vecs = [None] * nr_of_images
#         offset = np.float32(1).nbytes * self.ndims
#         
#         res = array.array('f')
#         fr = open(self.binary_file, 'rb')
#         fr.seek(index_name_array[0][0] * offset)
#         res.fromfile(fr, self.ndims)
#         previous = index_name_array[0][0]
#  
#         for next in sorted_index[1:]:
#             move = (next-1-previous) * offset
#             #print next, move
#             fr.seek(move, 1)
#             res.fromfile(fr, self.ndims)
#             previous = next
# 
#         fr.close()
# 
#         return [x[1] for x in index_name_array], [ res[i*self.ndims:(i+1)*self.ndims].tolist() for i in range(nr_of_images) ]
# 
# 
#     def read_one(self, name):
#         renamed, vectors = self.read([name])
#         return vectors[0]    
# 
#     def shape(self):
#         return [self.nr_of_images, self.ndims]


# Path: util/im2vec.py
import os
import numpy as np
from basic.common import printStatus
from simpleknn.bigfile import BigFile

DEFAULT_BLOCK_SIZE = 2000

INFO = os.path.basename(__file__)

class Image2Vec:

    def __init__(self, label_file, label2vec_dir):
        self.labels = map(str.strip, open(label_file).readlines())
        self.nr_of_labels = len(self.labels)
        feat_file = BigFile(label2vec_dir)
        renamed, vectors = feat_file.read(self.labels)
        name2index = dict(zip(renamed, range(len(renamed))))
        self.label_vectors = [None] * self.nr_of_labels
        self.feat_dim = feat_file.ndims

        for i in xrange(self.nr_of_labels):
            idx = name2index.get(self.labels[i], -1)
            self.label_vectors[i] = np.array(vectors[idx]) if idx >= 0 else None

        nr_of_inactive_labels = len([x for x in self.label_vectors if x is None])    
        printStatus(INFO, '#active_labels=%d, embedding_size=%d' % (self.nr_of_labels - nr_of_inactive_labels, self.feat_dim))



    def embedding(self, prob_vec, k=10):
        assert(len(prob_vec) == self.nr_of_labels), 'len(prob_vec)=%d, nr_of_labels=%d' % (len(prob_vec), self.nr_of_labels)
        top_hits = np.argsort(prob_vec)[::-1][:k]
        new_vec = np.array([0.] * self.feat_dim)

        Z = 0.

        for idx in top_hits: