def compare_model():
   index = Index()
   models = [Vectoriel(index, SimpleWeighter(index), normalized=True),
               Vectoriel(index, BasicWeighter(index), normalized=True),
               Vectoriel(index, ThirdWeighter(index), normalized=False),
               Vectoriel(index, FourthWeighter(index), normalized=False),
               Okapi(index),
               LanguageModel(index, .7),
               HITS(index, seeds=3, k=5),
               PageRank(index, seeds=3, k=5)
               ]
     names = ['Vectoriel1', 'Vectoriel2','Vectoriel3','Vectoriel4','Okapi', 'Language', 'HITS', 'PageRank']
     models_scores = compare_models(names, models)
     fig = plt.figure(figsize=(10,8))
     colors=iter(cm.hot(np.linspace(0,.7,len(names))))
     x = [s['AveragePrecision']['mean'] for s in scores.values()]
     plt.xticks(x, scores.keys())
     plt.bar(x, models_scores.values(), alpha=.4, color=c)
     # for n, s in models_scores.items():
     #     color = next(colors)
     #     y = s['PrecisionRecall']['mean']
     #     x = list(range(len(y)))
     #     plt.plot(x, y, color=color, label=n)
     # plt.legend()
     plt.savefig('plot/models_comparison_precision_eval.png')
def optimize_pagerank_parameters(measures=['AveragePrecision']):

    index = Index()

    models, names, scores = [], [], {}
    seeds_values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 30, 50]
    k_values = [0, 1, 3, 5, 10, 20]

    colors=iter(cm.hot(np.linspace(0,.7,len(k_values))))
    fig = plt.figure(figsize=(10,8))
    for k in k_values:
        models_b, names_b = [], []
        for seeds in seeds_values:
            print('k : ', k, 'seeds : ', seeds)
            name = 'PageRank_seeds=%d_k=%d'%(seeds, k)
            models_b.append(PageRank(index, seeds=seeds, k=k))
            names_b.append(name)
            names.append(name)

        scores_b = compare_models(names_b, models_b, measures)
        scores.update(scores_b)
        ap_b = [scores_b[model_name]['AveragePrecision']['eval_std'] for model_name in names_b]


        color = next(colors)
        label = 'k = '+str(k)
        plt.plot(seeds_values, ap_b, c=color, label=label)
    plt.title('PageRank model : average precision std depending on k and seeds values')
    plt.xlabel('seeds')
    plt.ylabel('Average Precision')
    plt.legend()
    plt.savefig('plot/PageRank_Model_ap_std.png')
Esempio n. 3
0
def save_heatmap(heatmap, path='./image.png', colour=False):
    '''
    Saves a heatmap to the specified path, in colour if colour is True, or in greyscale if colour is False.
    @param heatmap: A pixel matrix heatmap.
    @type heatmap: Rectangular numpy matrix of floats
    @param path: The file path to save the file into.
    @type path: String
    @param colour: Whether or not the heatmap should be saved in colour.
    @type colour: Boolean
    @rtype: Void
    '''
    from scipy import misc
    import numpy as np
    from time import time
    import Image
    heatmap = heatmap[::-1]
    if colour:
        #from matplotlib import colors
        from matplotlib.pyplot import cm

        print "Saving colour heatmap to " + path + "..."

        print "\tMapping to colour scale..."
        s = time()
        print "\t\tRescaling..."
        heatmap = heatmap / 5.
        print "\t\tDone."

        # excerpt from the registered 'hot' colormap:
        # '_segmentdata': {'blue': ((0.0, 0.0, 0.0), (0.746032, 0.0, 0.0), (1.0, 1.0, 1.0)),
        # 'green': ((0.0, 0.0, 0.0), (0.365079, 0.0, 0.0), (0.746032, 1.0, 1.0), (1.0, 1.0, 1.0)),
        # 'red': ((0.0, 0.0416, 0.0416), (0.365079, 1.0, 1.0), (1.0, 1.0, 1.0))}
        #cdict = [(0.0,      (1.0, 0.0, 0.0, 0.0)),
        #         (0.365079, (1.0, 0.0, 0.0, 0.5)),
        #         (0.746032, (1.0, 1.0, 0.0, 0.5)),
        #         (1.0,      (1.0, 1.0, 1.0, 0.5))]
        #cmap_hot = colors.LinearSegmentedColormap.from_list(name='yes', colors=cdict)
        #heatmap_color = Image.fromarray(np.uint8(cmap_hot(heatmap, bytes=True)))

        heatmap_color = Image.fromarray(
            np.uint8(cm.hot(heatmap, alpha=0.5, bytes=True)))
        e = time()

        print "\tTime to convert to colour: " + str(e - s)
        misc.imsave(path, heatmap_color)
        print "Done."
    else:
        print "Saving greyscale heatmap to " + path + "..."
        misc.imsave(path, heatmap)
        print "Done."
Esempio n. 4
0
def save_heatmap(heatmap, path='./image.png', colour=False):
    '''
    Saves a heatmap to the specified path, in colour if colour is True, or in greyscale if colour is False.
    @param heatmap: A pixel matrix heatmap.
    @type heatmap: Rectangular numpy matrix of floats
    @param path: The file path to save the file into.
    @type path: String
    @param colour: Whether or not the heatmap should be saved in colour.
    @type colour: Boolean
    @rtype: Void
    '''
    from scipy import misc  
    import numpy as np  
    from time import time
    import Image
    heatmap = heatmap[::-1]
    if colour:
        #from matplotlib import colors
        from matplotlib.pyplot import cm
        
        print "Saving colour heatmap to " + path + "..."
        
        print "\tMapping to colour scale..."
        s = time()
        print "\t\tRescaling..."
        heatmap = heatmap / 5.
        print"\t\tDone."
        
        # excerpt from the registered 'hot' colormap:
        # '_segmentdata': {'blue': ((0.0, 0.0, 0.0), (0.746032, 0.0, 0.0), (1.0, 1.0, 1.0)),
                        # 'green': ((0.0, 0.0, 0.0), (0.365079, 0.0, 0.0), (0.746032, 1.0, 1.0), (1.0, 1.0, 1.0)),
                        # 'red': ((0.0, 0.0416, 0.0416), (0.365079, 1.0, 1.0), (1.0, 1.0, 1.0))}
        #cdict = [(0.0,      (1.0, 0.0, 0.0, 0.0)),
        #         (0.365079, (1.0, 0.0, 0.0, 0.5)),
        #         (0.746032, (1.0, 1.0, 0.0, 0.5)),
        #         (1.0,      (1.0, 1.0, 1.0, 0.5))]
        #cmap_hot = colors.LinearSegmentedColormap.from_list(name='yes', colors=cdict)
        #heatmap_color = Image.fromarray(np.uint8(cmap_hot(heatmap, bytes=True)))
        
        heatmap_color = Image.fromarray(np.uint8(cm.hot(heatmap, alpha=0.5, bytes=True)))
        e = time()
        
        print "\tTime to convert to colour: " + str(e - s)
        misc.imsave(path, heatmap_color)
        print "Done."
    else:
        print "Saving greyscale heatmap to " + path + "..."
        misc.imsave(path, heatmap)
        print "Done."
def optimize_okapi_parameters(measures=['AveragePrecision']):

    index = Index()
    k1_values = np.arange(1, 2.1, 0.1)
    b_values = np.arange(0.65, 0.90, 0.05)

    # k1_values = [1.1, 1.5]
    # b_values = [.7, .8]

    fig = plt.figure(figsize=(10,8))
    scores, names = {}, []
    colors=iter(cm.hot(np.linspace(0,.8,len(b_values))))
    for b in b_values:
        models_b, names_b = [], []
        for k1 in k1_values:
            name = 'Okapi_k1=%.2f_b=%.2f'%(k1, b)
            models_b.append(Okapi(index, k1, b))
            names_b.append(name)
            names.append(name)

        scores_b = compare_models(names_b, models_b, measures)
        scores.update(scores_b)
        ap_b = [scores_b[model_name]['AveragePrecision']['eval_std'] for model_name in names_b]


        color = next(colors)
        label = 'b = '+str(b)
        plt.plot(k1_values, ap_b, c=color, label=label)

    plt.title('Okapi model : average precision std depending on b values')
    plt.xlabel('k1')
    plt.ylabel('Average Precision std')
    plt.legend()
    plt.savefig('plot/Okapi_Model_ap_std.png')


    ap = [scores[model_name]['AveragePrecision']['mean'] for model_name in names]
    best_model = names[np.argmax(ap)]
    print('best_model', best_model)
Esempio n. 6
0
import numpy as np
import matplotlib
matplotlib.use("Qt5Agg")

from matplotlib.pyplot import cm
from spimagine import volshow
import OpenGL.GL as GL

if __name__ == '__main__':

    data = np.einsum("ij,k", np.ones((100, ) * 2), np.linspace(0, 1, 100))
    w = volshow(data)
    w.glWidget._set_colormap_array(cm.hot(np.linspace(0, 1, 2**12))[:, :3])

    print("maximal texture size: ", GL.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE))
Esempio n. 7
0
import numpy as np
import matplotlib
matplotlib.use("Qt5Agg")

from matplotlib.pyplot import cm
from spimagine import volshow
import OpenGL.GL as GL


if __name__ == '__main__':

    data = np.einsum("ij,k",np.ones((100,)*2), np.linspace(0,1,100))
    w = volshow(data)
    w.glWidget._set_colormap_array(cm.hot(np.linspace(0,1,2**12))[:,:3])

    print("maximal texture size: ", GL.glGetIntegerv(GL.GL_MAX_TEXTURE_SIZE))