Esempio n. 1
0
def create_best_segment_image(list_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir):
    for name_file in list_file:
        print name_file
        buff_segment = []
        ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
        val_segment = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment for topic in range(n_topic)]
        val_segment = sorted(val_segment,key=itemgetter(1), reverse=True)
        bag_of_best_segment = val_segment[0:n_segment]
        fig = plt.figure()
        index_plt = 1
        for index, prob in bag_of_best_segment:
            print 'jogie'
            img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
            n_col = len(img[0])
            list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            no_segment = int(corpus_meta[index][2])
            buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
            segments_res = csv2Array(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            img[segments_res != no_segment] = (0.0,0.0,0.9)
            ax1 = fig.add_subplot(6,5, index_plt, axisbg='grey')
            ax1.set_xticks(()), ax1.set_yticks(())
            ax1.imshow(img)
            index_plt += 1
        plt.savefig('coba/'+name_file)
        plt.clf()
        plt.close()
Esempio n. 2
0
def create_best_segment_image(list_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir):
    for name_file in list_file:
        print name_file
        buff_segment = []
        ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
        val_segment = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment for topic in range(n_topic)]
        val_segment = sorted(val_segment,key=itemgetter(1), reverse=True)
        bag_of_best_segment = val_segment[0:n_segment]
        fig = plt.figure()
        index_plt = 1
        for index, prob in bag_of_best_segment:
            print 'jogie'
            img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
            n_col = len(img[0])
            list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            no_segment = int(corpus_meta[index][2])
            buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
            segments_res = csv2Array(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            img[segments_res != no_segment] = (0.0,0.0,0.9)
            ax1 = fig.add_subplot(6,5, index_plt, axisbg='grey')
            ax1.set_xticks(()), ax1.set_yticks(())
            ax1.imshow(img)
            index_plt += 1
        plt.savefig('coba/'+name_file)
        plt.clf()
        plt.close()
Esempio n. 3
0
def generate_map_segment(name_file, param):
    global cache_list_segment
    map_segment_init = {}
    map_segment_for_GA = {}
    segment_in_image = [
        int(item_corpus[0]) for item_corpus in param['corpus_meta']
        if name_file == item_corpus[1].split('-')[0]
    ]
    cache = ('key', 'list_of_segment')  #remember 1 before
    for i in range(len(segment_in_image)):
        index = segment_in_image[i]
        n_col = len(
            img_as_float(io.imread(param['img_dir'] + name_file + '.ppm'))[0])
        if param['corpus_meta'][index][1] == cache[0]:
            list_of_segment = cache[1]
        else:
            list_of_segment = csvToListOfSegment(
                param['segment_dir'] + '/' + name_file + '/' +
                param['corpus_meta'][index][1])
            cache = (param['corpus_meta'][index][1], list_of_segment)
        no_segment = int(param['corpus_meta'][index][2])
        pixel_in_segment = [
            row * n_col + col for row, col in list_of_segment[no_segment]
        ]
        max_prob_topic = max(
            [prob_topic for prob_topic in param['prob_topic_doc'][index]])
        if len(pixel_in_segment) >= 1000:
            map_segment_init[index] = (max_prob_topic, pixel_in_segment)
    map_segment_for_GA[name_file] = map_segment_init
    return map_segment_for_GA
Esempio n. 4
0
def create_segmentfile_from_binary_string(name_file, segment_dir, corpus_meta, output_dir, img_dir, ind_segment):
    buff_segment = []
    for index in ind_segment:
        img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
        n_col = len(img[0])
        segments_res = csv2Array(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        no_segment = int(corpus_meta[index][2])
        img[segments_res != no_segment] = (0.0,0.0,0.9)
        buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
        ensure_path(output_dir+'/image/'+name_file+'/')
        io.imsave(output_dir+'/image/'+name_file+'/'+str(index)+'.png', img)
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')
Esempio n. 5
0
def create_segmentfile_from_binary_string(name_file, segment_dir, corpus_meta, output_dir, img_dir, ind_segment):
    buff_segment = []
    for index in ind_segment:
        img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
        n_col = len(img[0])
        segments_res = csv2Array(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        no_segment = int(corpus_meta[index][2])
        img[segments_res != no_segment] = (0.0,0.0,0.9)
        buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
        ensure_path(output_dir+'/image/'+name_file+'/')
        io.imsave(output_dir+'/image/'+name_file+'/'+str(index)+'.png', img)
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')
Esempio n. 6
0
def create_best_n_segment_image(name_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir, img_dir):
    buff_segment = []
    
    ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
    val_segment = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment for topic in range(n_topic)]
    val_segment = sorted(val_segment,key=itemgetter(1), reverse=True)

    bag_of_best_segment = val_segment[0:n_segment]
    for index, prob in bag_of_best_segment:
        img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
        n_col = len(img[0])
        list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        no_segment = int(corpus_meta[index][2])
        buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
    
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')
Esempio n. 7
0
def create_best_n_segment_image(name_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir, img_dir):
    buff_segment = []
    
    ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
    val_segment = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment for topic in range(n_topic)]
    val_segment = sorted(val_segment,key=itemgetter(1), reverse=True)

    bag_of_best_segment = val_segment[0:n_segment]
    for index, prob in bag_of_best_segment:
        img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
        n_col = len(img[0])
        list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        no_segment = int(corpus_meta[index][2])
        buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
    
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')
def generate_map_segment(name_file, param):
    global cache_list_segment
    map_segment_init = {}
    map_segment_for_GA = {}
    segment_in_image = [int(item_corpus[0]) for item_corpus in param['corpus_meta'] if name_file == item_corpus[1].split('-')[0]]
    cache = ('key','list_of_segment') #remember 1 before
    for i in range(len(segment_in_image)):
        index = segment_in_image[i]        
        n_col = len(img_as_float(io.imread(param['img_dir']+name_file+'.ppm'))[0])
        if param['corpus_meta'][index][1] == cache[0]:
            list_of_segment = cache[1]
        else:            
            list_of_segment = csvToListOfSegment(param['segment_dir']+'/'+name_file+'/'+ param['corpus_meta'][index][1])
            cache = (param['corpus_meta'][index][1], list_of_segment)
        no_segment = int(param['corpus_meta'][index][2])
        pixel_in_segment = [row*n_col+col for row, col in list_of_segment[no_segment]]
        max_prob_topic = max([prob_topic for prob_topic in param['prob_topic_doc'][index]])
        if len(pixel_in_segment) >= 1000:
            map_segment_init[index] = (max_prob_topic, pixel_in_segment)
    map_segment_for_GA[name_file] = map_segment_init
    return map_segment_for_GA
Esempio n. 9
0
def sort_segment_cross_topic(name_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir, img_dir):
    buff_segment = []    
    ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
    val_per_topic = []
    for topic in range(n_topic):
        val = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment]
        sort_val = sorted(val, key=itemgetter(1), reverse=True)
        val_per_topic.append(sort_val)    
    sort_val_per_topic =  sorted(val_per_topic, key=lambda x: x[0][1], reverse=True)

    candidate_segment = []
    for i in range(4):
        candidate_segment += sort_val_per_topic[i]

    candidate_segment = sorted(candidate_segment,key=itemgetter(1), reverse=True)
    bag_of_best_segment = candidate_segment[0:n_segment]

    for index, prob in bag_of_best_segment:
        img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
        n_col = len(img[0])
        list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        no_segment = int(corpus_meta[index][2])
        buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])    
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')
Esempio n. 10
0
def sort_segment_based_topic(name_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir, img_dir):
    buff_segment = []    
    ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
    val_per_topic = []
    for topic in range(n_topic):
        val = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment]
        sort_val = sorted(val, key=itemgetter(1), reverse=True)
        val_per_topic.append(sort_val)    
    sort_val_per_topic =  sorted(val_per_topic, key=lambda x: x[0][1], reverse=True)

    first_val = [fi[0][1] for fi in sort_val_per_topic[0:4]]
    quota_per_topic = [int(math.floor(fi/sum(first_val) * n_segment)) for fi in first_val]
    print quota_per_topic

    bag_of_best_segment = [sort_val_per_topic[ind][0:quota] for quota, ind in zip(quota_per_topic, range(len(quota_per_topic)))]

    for row in bag_of_best_segment:
        for index, prob in row:
            img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
            n_col = len(img[0])
            list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            no_segment = int(corpus_meta[index][2])
            buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')
Esempio n. 11
0
def sort_segment_cross_topic(name_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir, img_dir):
    buff_segment = []    
    ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
    val_per_topic = []
    for topic in range(n_topic):
        val = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment]
        sort_val = sorted(val, key=itemgetter(1), reverse=True)
        val_per_topic.append(sort_val)    
    sort_val_per_topic =  sorted(val_per_topic, key=lambda x: x[0][1], reverse=True)

    candidate_segment = []
    for i in range(4):
        candidate_segment += sort_val_per_topic[i]

    candidate_segment = sorted(candidate_segment,key=itemgetter(1), reverse=True)
    bag_of_best_segment = candidate_segment[0:n_segment]

    for index, prob in bag_of_best_segment:
        img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
        n_col = len(img[0])
        list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
        no_segment = int(corpus_meta[index][2])
        buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])    
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')
Esempio n. 12
0
def sort_segment_based_topic(name_file, segment_dir, corpus_meta, prob_topic_doc, n_topic, n_segment, output_dir, img_dir):
    buff_segment = []    
    ind_segment = [int(item_corpus[0]) for item_corpus in corpus_meta if name_file == item_corpus[1].split('-')[0]]
    val_per_topic = []
    for topic in range(n_topic):
        val = [(ind, prob_topic_doc[ind][topic]) for ind in ind_segment]
        sort_val = sorted(val, key=itemgetter(1), reverse=True)
        val_per_topic.append(sort_val)    
    sort_val_per_topic =  sorted(val_per_topic, key=lambda x: x[0][1], reverse=True)

    first_val = [fi[0][1] for fi in sort_val_per_topic[0:4]]
    quota_per_topic = [int(math.floor(fi/sum(first_val) * n_segment)) for fi in first_val]
    print quota_per_topic

    bag_of_best_segment = [sort_val_per_topic[ind][0:quota] for quota, ind in zip(quota_per_topic, range(len(quota_per_topic)))]

    for row in bag_of_best_segment:
        for index, prob in row:
            img = img_as_float(io.imread(img_dir+name_file+'.ppm'))
            n_col = len(img[0])
            list_of_segment = csvToListOfSegment(segment_dir+'/'+name_file+'/'+ corpus_meta[index][1])
            no_segment = int(corpus_meta[index][2])
            buff_segment.append([row*n_col+col for row, col in list_of_segment[no_segment]])
    writeCsv(buff_segment, output_dir+'/'+name_file+'.sup')