def check_common_relations(folder_input, output=None, file_types='types.pddl', class_file='classes.cfg', rels_file='relations.cfg', keep_names=False):
    if not output:
        output = join(dirname(folder_input), 'common_relations.txt')

    fdh = fh.FolderHandler(folder_input)
    dic = {}
    for file_input in fdh:
        with fh.CompressedFile(file_input) as cf:
            relations = []
            fname, _ = splitext(basename(file_input))
            for start, end, sub, rel, obj in cf:
                relations.append((sub, rel, obj))
            relations = set(relations)
            for rels in relations:
                if dic.has_key(rels):
                    dic[rels].append(fname)
                else:
                    dic[rels] = [fname]

    dtimes = {}
    for rel in dic:
        size = len(dic[rel])
        if dtimes.has_key(size):
            dtimes[size].append(rel)
        else:
            dtimes[size] = [rel] 

    with open(output, 'w') as fout:
        for size in sorted(dtimes, reverse=True):
            fout.write('# {} recipes\n'.format(size))
            for sub, rel, obj in dtimes[size]:
                fout.write('{} {} {}\n'.format(sub, rel, obj))
예제 #2
0
def domains_folder(folder_input, output, domain):
    if not output:
        output = folder_input
        output = join(folder_input, 'pddls_full.tmp')
        output = fh.mkdir_from_file(output)
        fdic = join(output, 'dictionary.dat')
        fpddl = join(output, 'domain.pddl')
        ftmpt = join(output, 'template.pddl')

    dprec = {}
    srep = StateRepresentation(folder_input)
    logger.info('Dictionary contaning {} relations.'.format(len(srep)))
    relfiles = fh.FolderHandler(folder_input)
    for file_input in relfiles:
        fname, _ = fh.filename(file_input)
        file_output = join(output, fname + '.pddl')
        # get all preconditions and effects before applying XNORp
        preconditions_effects_pair(file_input, srep, dprec)
    #print(len(dprec))

    #for eff in sorted(dprec):
    #    print(eff, len(dprec[eff]))
    #    print(dprec[id][0])
    #    print(dprec[id][1])
    #    print()

    #XNORp(dprec, srep, convert_null=True)
    convert_to_negative(dprec, srep)

    generate_pddl_pair(srep, dprec, fpddl, domain)
    srep.save(fdic)
    generate_template_file(srep, ftmpt, domain=domain)
def split_from_folder(folder_input, output=None):
    """ Read a folder containing Decompressed files and 
        create a split dataset for each file.

        Parameters:
        -----------
        folder_input: string
            path to the folder containing Decompressed files
        output: string (optional)
            path to the folder where the files containing splits
            are saved.
    """
    if not output:
        output = join(folder_input, 'splits.tmp')
        output = fh.mkdir_from_file(output)
    
    # create folders
    folders = {}
    for split in SPLITS:
        folder_out = join(output, str(split))
        folder_out = fh.mkdir_from_file(folder_out)
        folders[split] = folder_out

    files = fh.FolderHandler(folder_input)
    for path in files:
        logger.info('Processing file: %s' % path)
        split_file(path, folders)
    logger.info('Finished successfully!')
예제 #4
0
def main(inputfolder, fileoutput):
    if not fileoutput:
        fileoutput = join(inputfolder, '../lis_annotation.txt')
    fileclasses = join(dirname(fileoutput), 'classes.txt')
    
    dclasses = {}
    fhandler = fh.FolderHandler(inputfolder, ext='xml', sort_id=True)
    nb_files = fhandler.nb_files()
    pb = pbar.ProgressBar(nb_files)
    logger.info('Processing %d files!' % nb_files)
    with open(fileoutput, 'w') as fout:
        ##0 \t object \t (52,104,52,43) \t 0 \t data1/boild-egg/0.jpg
        fout.write('Frame:\tLabel:\tPoints:\tBounding Box ID:\tFrame path\n')
        for fname in fhandler:
            id = fhandler.id
            fvoc = fh.VOCXML(fname)
            path = fvoc.image_path()
            objs = fvoc.extract_objects()
            for obj in objs:
                name, dpos = obj
                if not dclasses.has_key(name):
                    dclasses[name] = len(dclasses)
                w = dpos['xmax'] - dpos['xmin']
                h = dpos['ymax'] - dpos['ymin']
                fout.write('%d\t%s\t(%d,%d,%d,%d)\t%d\t%s\n' % (id, name, dpos['xmin'], dpos['ymin'], w, h, dclasses[name], path))
            pb.update()
        fout.write('---\nModified on:\t11.11.2011\t11:11')
    logger.info('Saved output file at: %s' % fileoutput)
    with open(fileclasses, 'w') as fout:
        for cl in dclasses:
            fout.write('%d %s\n' % (dclasses[cl], cl))
예제 #5
0
def domains_folder(folder_input, output, domain):
    if not output:
        output = folder_input
        output = join(folder_input, 'auto_pddls.tmp')
        output = fh.mkdir_from_file(output)
        fdic = join(output, 'dictionary.dat')
        fpddl = join(output, 'auto_domain.pddl')
        ftmpt = join(output, 'template.pddl')

    # pddl.ini
    config = fh.PDDLInit()
    dinit = config.dic_initial_states()

    dprec = {}
    srep = StateRepresentation(folder_input)
    logger.info('Dictionary contaning {} relations.'.format(len(srep)))
    relfiles = fh.FolderHandler(folder_input)
    for file_input in relfiles:
        fname, _ = fh.filename(file_input)
        file_output = join(output, fname + '.pddl')
        # get all preconditions and effects before applying XNORp
        preconditions_effects(file_input, srep, dprec, dinit)

    XNORp(dprec, srep)
    generate_pddl(srep, dprec, fpddl, domain)
    srep.save(fdic)
    generate_template_file(srep, ftmpt, dinit, domain=domain)
def compute_from_folder(folder_input, output, plot=False):
    """ Receives a folder containing files with scores for each
        goal.

    Parameters:
    -----------
    folder_input: string
        path to the folder containing files with relations
    output: string
        path to the file where the goals are saved.
    """
    if not output:
        output = join(folder_input, 'results.txt')

    drecipes = {}
    filescores = fh.FolderHandler(folder_input, ext='csv')
    with open(output, 'w') as fout:
        for file_score in filescores:
            logger.info('Reading file: {}'.format(file_score))
            fname = fh.filename(file_score, extension=False).split('-')[1]
            # fname = boiledegg
            acc, correct, spread, nb_obs = compute_accuracy(file_score,
                                                            name=fname)
            fout.write(
                '{}: \n\tAccuracy: {}\n\tCorrect: {}\n\tSpread: {}\n\tObservations: {}\n\n'
                .format(fname, acc, correct, spread, nb_obs))
            if plot:
                plot_result(file_score, name=fname)

    logger.info('Accuracy scores saved in file: {}'.format(output))
예제 #7
0
def generate_goal_states(folder_input, output):
    """ Generate a file containing the goal states for a set of files
        containing relations between objects. Each recipe has its own
        set of goals. Recipes are grouped according to the name of the
        file, where the defaults uses `<nb_file>-<repipe>.txt` format.

    Parameters:
    -----------
    folder_input: string
        path to the folder containing files with relations
    output: string
        path to the file where the goals are saved.
    """
    if not output:
        output = join(folder_input, 'goal_states.dat')

    drecipes = {}
    relfiles = fh.FolderHandler(folder_input)
    for file_input in relfiles:
        logger.info('Reading file: {}'.format(file_input))
        fname = fh.filename(file_input, extension=False)[2:]
        relations = goal_state_from_file(file_input)
        add_relations(drecipes, fname, relations)

    logger.info('Saving goal states in: {}'.format(output))
    save_goal(drecipes, output)
예제 #8
0
 def _extract_relations(self, folder_input):
     """ Extract relations (sub, rel, obj) from multiple files. """
     relations = []
     logger.info('Generating dictionary...')
     relfiles = fh.FolderHandler(folder_input)
     for file_input in relfiles:
         with fh.DecompressedFile(file_input) as cf:
             relations.extend(cf.list_relations())
     return relations
def decompress_data(folder_input,
                    output=None,
                    class_file='classes.cfg',
                    rels_file='relations.cfg'):
    if not output:
        output = join(folder_input, 'decompressed.tmp')
        output = fh.mkdir_from_file(output)

    # load files
    compfiles = fh.FolderHandler(folder_input)
    for file_input in compfiles:
        foutput = join(output, basename(file_input))
        dr.decompress_relations(file_input,
                                output=foutput,
                                class_file=class_file,
                                rels_file=rels_file)
def merge_annotation(folder_input, output=None, class_file='classes.cfg'):
    if not output:
        output = join(folder_input, 'merged_bboxes.txt')

    # Load classes for objects from dict {0: 'rel0', 1: 'rel1'}
    do = fh.ConfigFile(class_file).load_classes(cnames=True)
    logger.info('Loaded dictionary with {} objects.'.format(len(do)))

    files = fh.FolderHandler(folder_input)
    with open(output, 'w') as fout:
        fout.write('Frame\tLabel\tPoints\tBounding Box ID\tFrame path:\n')
        for path in files:
            logger.info('Processing file: %s' % path)
            filelis = fh.LisFile(path)
            with filelis as flis:
                for arr in flis:
                    check_error(do, flis.obj, flis.nb_line)
                    # 0	table	(0,112,101,142)	29	0.jpg
                    fout.write('%s\t%s\t%s\t%s\t%s\n' %
                               (arr[0], arr[1], arr[2], arr[3],
                                join(flis.path, arr[4])))
    logger.info('Saved bounding boxes in file: %s' % output)
def merge_annotation(folder_input, output=None, class_file='classes.cfg', rels_file='relations.cfg'):
    if not output:
        output = join(folder_input, 'merged_relations.txt')

    # Load classes for objects from dict {0: 'rel0', 1: 'rel1'}
    do = fh.ConfigFile(class_file).load_classes(cnames=True)
    logger.info('Loaded dictionary with {} objects.'.format(len(do)))
    dr = fh.ConfigFile(rels_file).load_classes(cnames=True)
    logger.info('Loaded dictionary with {} relations.'.format(len(dr)))

    files = fh.FolderHandler(folder_input)
    with open(output, 'w') as fout:
        fout.write('Frame\tSubject\tRelation\tObject\tPath\n')
        for path in files:
            logger.info('Processing file: %s' % path)
            filerels = fh.DecompressedFile(path)
            with filerels as frels:
                for fr, o1, r, o2 in frels:
                    check_error(do, o1, frels.nb_lines)
                    check_error(do, o2, frels.nb_lines)
                    check_error(dr, r, frels.nb_lines)
                    fout.write('%d\t%s\t%s\t%s\t%s\n' % (fr, o1, r, o2, filerels.path))
    logger.info('Saved relations in file: %s' % output)
예제 #12
0
            pathimg = str(fr)+'.jpg'
            if not drels.has_key((o1, r, o2)) and not recorded.has_key((o1, r, o2)):
                logger.error('There is not possible relation: [{}, {}, {}]'.format(o1, r, o2))
                recorded[(o1, r, o2)] = ''
                errors += 1

    if errors:
        logger.info('Finished WITH {} errors!'.format(errors))
    else:
        logger.info('Finished without errors!')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('input', metavar='file_or_folder', help='File or folder containing decompressed relations annotation')
    parser.add_argument('-b', '--bounding_boxes', help='File containing LIS annotation for objects', default='Bounding_Boxes_Annotation.txt')
    parser.add_argument('-r', '--relations', help='File containing possible relations', default='possible_relations.cfg')
    parser.add_argument('-o', '--output', help='Path to the file to save the new relations.')
    parser.add_argument('-f', '--folder', help='Perform sanitizing of files inside a folder.', action='store_true')
    args = parser.parse_args()

    if args.folder:
        hdobj = fh.FolderHandler(args.input)
        for objpath in hdobj:
            fname = basename(objpath)
            relpath = join(args.input, fname)
            #sanitize(objpath, relpath, args.output)
            sanitize_relations(relpath, args.relations)
    #else:
    #    sanitize(args.objectfile, args.relationfile, args.output)
예제 #13
0
def main(bagfolder):
    fhandler = fh.FolderHandler(bagfolder, ext='bag')
    for bagname in fhandler:
        main_extract(bagname)