Esempio n. 1
0
def load_glabels_from_txt(filepath):
    if logging_enabled == True:
        print("- Entered data::load_glabels_from_txt Global Method")

    filepath = append_ext_to_filepath('.txt', filepath)
    rtn = {}
    int_map = {}
    seen_glabels = set()
    with open(filepath) as f:
        for line in f:
            ls = line.rstrip().split()
            assert (len(ls) == 2)
            gid = int(ls[0])
            try:
                glabel = int(ls[1])
            except ValueError:
                label_string = ls[1]
                glabel = int_map.get(label_string)
                if glabel is None:
                    glabel = len(int_map)  # guarantee 0-based
                    int_map[label_string] = glabel  # increase the size of int_map by 1
            rtn[gid] = glabel
            seen_glabels.add(glabel)
    if 0 not in seen_glabels:  # check 0-based graph labels
        raise RuntimeError('{} has no glabel 0; {}'.format(filepath, seen_glabels))
    return rtn
Esempio n. 2
0
def save_glabels_as_txt(filepath, glabels):
    if logging_enabled == True:
        print("- Entered data::save_glabels_as_txt Global Method")

    filepath = append_ext_to_filepath('.txt', filepath)
    with open(filepath, 'w') as f:
        for id, glabel in OrderedDict(glabels).items():
            f.write('{}\t{}\n'.format(id, glabel))
Esempio n. 3
0
def _save_fig(plt, dir, fn, need_eps=False, print_path=False):
    plt_cnt = 0
    if dir is None or fn is None:
        return plt_cnt
    final_path_without_ext = join(dir, fn)
    exts = ['.png']
    if need_eps:
        exts += '.eps'
    for ext in exts:
        final_path = append_ext_to_filepath(ext, final_path_without_ext)
        create_dir_if_not_exists(dirname(final_path))
        try:
            plt.savefig(final_path, bbox_inches='tight')
        except:
            warn('savefig')
        if print_path:
            print('Saved to {}'.format(final_path))
        plt_cnt += 1
    return plt_cnt
Esempio n. 4
0
def save_glabels_as_txt(filepath, glabels):
    filepath = append_ext_to_filepath('.txt', filepath)
    with open(filepath, 'w') as f:
        for id, glabel in OrderedDict(glabels).items():
            f.write('{}\t{}\n'.format(id, glabel))