Beispiel #1
0
    def update_config(self):
        out('update_config')

        self.config = PRACMLNConfig()
        self.config['mln'] = self.mln_container.selected_file.get().strip().lstrip('*')
        self.config["db"] = self.db_container.selected_file.get().strip().lstrip('*')
        self.config["output_filename"] = self.output_filename.get()
        self.config["params"] = self.params.get().strip()
        self.config["method"] = LearningMethods.id(self.selected_method.get().strip())
        self.config["pattern"] = self.pattern.get()
        self.config["use_prior"] = int(self.use_prior.get())
        self.config["prior_mean"] = self.priorMean.get()
        self.config["prior_stdev"] = self.priorStdDev.get()
        self.config["incremental"] = int(self.incremental.get())
        self.config["shuffle"] = int(self.shuffle.get())
        self.config["use_initial_weights"] = int(self.use_initial_weights.get())
        self.config["qpreds"] = self.queryPreds.get().strip()
        self.config["epreds"] = self.evidencePreds.get().strip()
        self.config["discr_preds"] = self.discrPredicates.get()
        self.config['logic'] = self.selected_logic.get()
        self.config['grammar'] = self.selected_grammar.get()
        self.config['multicore'] = self.multicore.get()
        self.config['profile'] = self.profile.get()
        self.config['verbose'] = self.verbose.get()
        self.config['ignore_unknown_preds'] = self.ignore_unknown_preds.get()
        self.config['ignore_zero_weight_formulas'] = self.ignore_zero_weight_formulas.get()
        self.config['save'] = self.save.get()
        self.config["output_filename"] = self.output_filename.get().strip()
        self.project.learnconf = PRACMLNConfig()
        self.project.learnconf.update(self.config.config.copy())
Beispiel #2
0
 def print_gndatoms(self, stream=sys.stdout):
     '''
     Prints the alphabetically sorted list of ground atoms in this MRF to the given `stream`.
     '''
     out('=== GROUND ATOMS ===', tb=2)
     l = self._gndatoms.keys()
     for ga in sorted(l):
         stream.write(str(ga) + '\n')
Beispiel #3
0
 def print_world_vars(self, world, stream=sys.stdout, tb=2):
     '''
     Prints the given world `world` as a readable string of the MRF variables to the given stream.
     '''
     out('=== WORLD VARIABLES ===', tb=tb)
     for var in self.variables:
         stream.write(repr(var) + '\n')
         for i, v in enumerate(var.evidence_value(world)):
             vstr = '%.3f' % v if v is not None else '?    '
             stream.write('  %s  %s\n' % (vstr, var.gndatoms[i])) 
Beispiel #4
0
 def loaddb(self, mln, config, db=None):
     if db is None:
         if config == 'query': config = self.queryconf
         elif config == 'learn': config = self.learnconf
         else: raise Exception('Need a database name or config.')
     from pracmln.mln.database import parse_db
     path = self.path if hasattr(self, 'path') else None
     out(db)
     out(self.dbs[db])
     return parse_db(mln, self.dbs[ifNone(db, config['db'])], ignore_unknown_preds=config['ignore_unknown_preds'], projectpath=path)
Beispiel #5
0
    def eval(self, mln, dbs):
        '''
        Returns a confusion matrix for the given (learned) MLN evaluated on
        the databases given in dbs.
        '''
        querypred = self.params.querypred
#         query_dom = self.params.query_dom
        
#         sig = ['?arg%d' % i for i, _ in enumerate(mln.predicates[query_pred])]
#         querytempl = '%s(%s)' % (query_pred, ','.join(sig))
        
#         dbs = map(lambda db: db.copy(mln), dbs)
        
        for db_ in dbs:
            # save and remove the query predicates from the evidence
            db = db_.copy()
            gndtruth = mln.ground(db)
            gndtruth.apply_cw()
            for atom, _ in db.gndatoms(querypred):
                out('removing evidence', repr(atom))
                del db.evidence[atom]
            db.write()
            stop()
            try:
                resultdb = MLNQuery(config=self.params.queryconf, mln=mln, method=InferenceMethods.WCSPInference, db=db, 
                                  cw_preds=[p.name for p in mln.predicates if p.name != self.params.querypred], multicore=False).run().resultdb
                result = mln.ground(db)
                result.set_evidence(resultdb)
                for variable in result.variables:
                    if variable.predicate.name != querypred: continue
                    pvalue = variable.evidence_value()
                    tvalue = variable.evidence_value()
                    prediction = [a for a, v in variable.atomvalues(pvalue) if v == 1]
                    truth = [a for a, v in variable.atomvalues(tvalue) if v == 1]
                    prediction = str(prediction[0]) if prediction else None
                    truth = str(truth[0]) if truth else None
                    self.confmat.addClassificationResult(prediction, truth)
#                 sig2 = list(sig)
#                 entityIdx = mln.predicates[query_pred].argdoms.index(query_dom)
#                 for entity in db.domains[]:
#                     sig2[entityIdx] = entity
#                     query = '%s(%s)' % (queryPred, ','.join(sig2))
#                     for truth in trueDB.query(query):
#                         truth = truth.values().pop()
#                     for pred in resultDB.query(query):
#                         pred = pred.values().pop()
#                     self.confMatrix.addClassificationResult(pred, truth)
#                 for e, v in trueDB.evidence.iteritems():
#                     if v is not None:
#                         db.addGroundAtom('%s%s' % ('' if v is True else '!', e))
            except:
                logger.critical(''.join(traceback.format_exception(*sys.exc_info())))
Beispiel #6
0
 def loaddb(self, mln, config, db=None):
     if db is None:
         if config == 'query': config = self.queryconf
         elif config == 'learn': config = self.learnconf
         else: raise Exception('Need a database name or config.')
     from pracmln.mln.database import parse_db
     path = self.path if hasattr(self, 'path') else None
     out(db)
     out(self.dbs[db])
     return parse_db(mln,
                     self.dbs[ifNone(db, config['db'])],
                     ignore_unknown_preds=config['ignore_unknown_preds'],
                     projectpath=path)
Beispiel #7
0
    def delete_file(self):
        fname = self.selected_file.get().strip()

        # remove element from project mlns and buffer
        if fname in self.file_buffer:
            del self.file_buffer[fname]

        if self.delete_hook is not None:
            self.delete_hook(fname)

        f = self.update_file_choices()
        out(f)
        # select first element from remaining list
        if f: self.list_files['menu'].invoke(0)
        else:
            self.selected_file.set('')
            self.editor.delete("1.0", END)
        self.dirty = True
Beispiel #8
0
    def delete_file(self):
        fname = self.selected_file.get().strip()

        # remove element from project mlns and buffer
        if fname in self.file_buffer:
            del self.file_buffer[fname]

        if self.delete_hook is not None:
            self.delete_hook(fname)

        f = self.update_file_choices()
        out(f)
        # select first element from remaining list
        if f: self.list_files['menu'].invoke(0)
        else:
            self.selected_file.set('')
            self.editor.delete("1.0", END)
        self.dirty = True
Beispiel #9
0
 def print_domains(self):
     out('=== MRF DOMAINS ==', tb=2)
     for dom, values in self.domains.iteritems():
         print dom, '=', ','.join(values) 
Beispiel #10
0
    def learn(self, savegeometry=True, options={}, *args):
        mln_content = self.mln_container.editor.get("1.0", END).encode('utf8').strip()
        db_content = self.db_container.editor.get("1.0", END).encode('utf8').strip()

        # create conf from current gui settings
        self.update_config()

        # write gui settings
        self.write_gconfig(savegeometry=savegeometry)

        # hide gui
        self.master.withdraw()

        try:
            print headline('PRAC LEARNING TOOL')
            print

            if options.get('mlnarg') is not None:
                mlnobj = MLN(mlnfile=os.path.abspath(options.get('mlnarg')),
                             logic=self.config.get('logic', 'FirstOrderLogic'),
                             grammar=self.config.get('grammar', 'PRACGrammar'))
            else:
                mlnobj = parse_mln(mln_content, searchpaths=[self.project_dir],
                                   projectpath=os.path.join(self.project_dir, self.project.name),
                                   logic=self.config.get('logic', 'FirstOrderLogic'),
                                   grammar=self.config.get('grammar', 'PRACGrammar'))

            if options.get('dbarg') is not None:
                dbobj = Database.load(mlnobj, dbfiles=[options.get('dbarg')], ignore_unknown_preds=self.config.get('ignore_unknown_preds', True))
            else:
                if self.config.get('pattern'):
                    local, dblist = self.get_training_db_paths(self.config.get('pattern').strip())
                    dbobj = []
                    # build database list from project dbs
                    if local:
                        for dbname in dblist:
                            dbobj.extend(parse_db(mlnobj, self.project.dbs[dbname].strip(),
                                         ignore_unknown_preds=self.config.get('ignore_unknown_preds', True),
                                         projectpath=os.path.join(self.dir, self.project.name)))
                        out(dbobj)
                    # build database list from filesystem dbs
                    else:
                        for dbpath in dblist:
                            dbobj.extend(Database.load(mlnobj, dbpath, ignore_unknown_preds= self.config.get('ignore_unknown_preds', True)))
                # build single db from currently selected db
                else:
                    dbobj = parse_db(mlnobj, db_content, projectpath=os.path.join(self.dir, self.project.name), dirs=[self.dir])

            learning = MLNLearn(config=self.config, mln=mlnobj, db=dbobj)
            result = learning.run()

            # write to file if run from commandline, otherwise save result
            # to project results
            if options.get('outputfile') is not None:
                output = StringIO.StringIO()
                result.write(output)
                with open(os.path.abspath(options.get('outputfile')), 'w') as f:
                    f.write(output.getvalue())
                logger.info('saved result to {}'.format(os.path.abspath(options.get('outputfile'))))
            elif self.save.get():
                output = StringIO.StringIO()
                result.write(output)
                self.project.add_mln(self.output_filename.get(), output.getvalue())
                self.mln_container.update_file_choices()
                self.project.save(dirpath=self.project_dir)
                logger.info('saved result to file mln/{} in project {}'.format(self.output_filename.get(), self.project.name))
            else:
                logger.debug("No output file given - results have not been saved.")
        except:
            traceback.print_exc()

        # restore gui
        sys.stdout.flush()
        self.master.deiconify()
Beispiel #11
0
    def infer(self, savegeometry=True, options={}, *args):
        mln_content = self.mln_container.editor.get(
            "1.0", END).encode('utf8').strip()
        db_content = self.db_container.editor.get("1.0",
                                                  END).encode('utf8').strip()

        # create conf from current gui settings
        self.update_config()

        # write gui settings
        self.write_gconfig(savegeometry=savegeometry)

        # hide gui
        self.master.withdraw()

        try:
            print headline('PRACMLN QUERY TOOL')
            print

            if options.get('mlnarg') is not None:
                mlnobj = MLN(mlnfile=os.path.abspath(options.get('mlnarg')),
                             logic=self.config.get('logic', 'FirstOrderLogic'),
                             grammar=self.config.get('grammar', 'PRACGrammar'))
            else:
                mlnobj = parse_mln(
                    mln_content,
                    searchpaths=[self.dir],
                    projectpath=os.path.join(self.dir, self.project.name),
                    logic=self.config.get('logic', 'FirstOrderLogic'),
                    grammar=self.config.get('grammar', 'PRACGrammar'))

            if options.get('emlnarg') is not None:
                emln_content = mlnpath(options.get('emlnarg')).content
            else:
                emln_content = self.emln_container.editor.get(
                    "1.0", END).encode('utf8').strip()

            if options.get('dbarg') is not None:
                dbobj = Database.load(mlnobj,
                                      dbfiles=[options.get('dbarg')],
                                      ignore_unknown_preds=self.config.get(
                                          'ignore_unknown_preds', True))
            else:
                out(self.config.get('ignore_unknown_preds', True))
                dbobj = parse_db(mlnobj,
                                 db_content,
                                 ignore_unknown_preds=self.config.get(
                                     'ignore_unknown_preds', True))

            if options.get('queryarg') is not None:
                self.config["queries"] = options.get('queryarg')

            infer = MLNQuery(config=self.config,
                             mln=mlnobj,
                             db=dbobj,
                             emln=emln_content)
            result = infer.run()

            # write to file if run from commandline, otherwise save result to project results
            if options.get('outputfile') is not None:
                output = StringIO.StringIO()
                result.write(output)
                with open(os.path.abspath(options.get('outputfile')),
                          'w') as f:
                    f.write(output.getvalue())
                logger.info('saved result to {}'.format(
                    os.path.abspath(options.get('outputfile'))))
            elif self.save.get():
                output = StringIO.StringIO()
                result.write(output)
                fname = self.output_filename.get()
                self.project.add_result(fname, output.getvalue())
                self.project.save(dirpath=self.dir)
                logger.info(
                    'saved result to file results/{} in project {}'.format(
                        fname, self.project.name))
            else:
                logger.debug(
                    'No output file given - results have not been saved.')

        except:
            traceback.print_exc()

        # restore main window
        sys.stdout.flush()
        self.master.deiconify()
Beispiel #12
0
    def eval(self, mln, dbs):
        '''
        Returns a confusion matrix for the given (learned) MLN evaluated on
        the databases given in dbs.
        '''
        querypred = self.params.querypred
        #         query_dom = self.params.query_dom

        #         sig = ['?arg%d' % i for i, _ in enumerate(mln.predicates[query_pred])]
        #         querytempl = '%s(%s)' % (query_pred, ','.join(sig))

        #         dbs = map(lambda db: db.copy(mln), dbs)

        for db_ in dbs:
            # save and remove the query predicates from the evidence
            db = db_.copy()
            gndtruth = mln.ground(db)
            gndtruth.apply_cw()
            for atom, _ in db.gndatoms(querypred):
                out('removing evidence', repr(atom))
                del db.evidence[atom]
            db.write()
            stop()
            try:
                resultdb = MLNQuery(config=self.params.queryconf,
                                    mln=mln,
                                    method=InferenceMethods.WCSPInference,
                                    db=db,
                                    cw_preds=[
                                        p.name for p in mln.predicates
                                        if p.name != self.params.querypred
                                    ],
                                    multicore=False).run().resultdb
                result = mln.ground(db)
                result.set_evidence(resultdb)
                for variable in result.variables:
                    if variable.predicate.name != querypred: continue
                    pvalue = variable.evidence_value()
                    tvalue = variable.evidence_value()
                    prediction = [
                        a for a, v in variable.atomvalues(pvalue) if v == 1
                    ]
                    truth = [
                        a for a, v in variable.atomvalues(tvalue) if v == 1
                    ]
                    prediction = str(prediction[0]) if prediction else None
                    truth = str(truth[0]) if truth else None
                    self.confmat.addClassificationResult(prediction, truth)
#                 sig2 = list(sig)
#                 entityIdx = mln.predicates[query_pred].argdoms.index(query_dom)
#                 for entity in db.domains[]:
#                     sig2[entityIdx] = entity
#                     query = '%s(%s)' % (queryPred, ','.join(sig2))
#                     for truth in trueDB.query(query):
#                         truth = truth.values().pop()
#                     for pred in resultDB.query(query):
#                         pred = pred.values().pop()
#                     self.confMatrix.addClassificationResult(pred, truth)
#                 for e, v in trueDB.evidence.iteritems():
#                     if v is not None:
#                         db.addGroundAtom('%s%s' % ('' if v is True else '!', e))
            except:
                logger.critical(''.join(
                    traceback.format_exception(*sys.exc_info())))
Beispiel #13
0
    def infer(self, savegeometry=True, options={}, *args):
        mln_content = self.mln_container.editor.get("1.0", END).encode('utf8').strip()
        db_content = self.db_container.editor.get("1.0", END).encode('utf8').strip()

        # create conf from current gui settings
        self.update_config()

        # write gui settings
        self.write_gconfig(savegeometry=savegeometry)

        # hide gui
        self.master.withdraw()

        try:
            print headline('PRACMLN QUERY TOOL')
            print

            if options.get('mlnarg') is not None:
                mlnobj = MLN(mlnfile=os.path.abspath(options.get('mlnarg')),
                             logic=self.config.get('logic', 'FirstOrderLogic'),
                             grammar=self.config.get('grammar', 'PRACGrammar'))
            else:
                mlnobj = parse_mln(mln_content, searchpaths=[self.dir],
                                   projectpath=os.path.join(self.dir, self.project.name),
                                   logic=self.config.get('logic', 'FirstOrderLogic'),
                                   grammar=self.config.get('grammar', 'PRACGrammar'))

            if options.get('emlnarg') is not None:
                emln_content = mlnpath(options.get('emlnarg')).content
            else:
                emln_content = self.emln_container.editor.get("1.0", END).encode('utf8').strip()

            if options.get('dbarg') is not None:
                dbobj = Database.load(mlnobj, dbfiles=[options.get('dbarg')], ignore_unknown_preds=self.config.get('ignore_unknown_preds', True))
            else:
                out(self.config.get('ignore_unknown_preds', True))
                dbobj = parse_db(mlnobj, db_content, ignore_unknown_preds=self.config.get('ignore_unknown_preds', True))

            if options.get('queryarg') is not None:
                self.config["queries"] = options.get('queryarg')

            infer = MLNQuery(config=self.config, mln=mlnobj, db=dbobj, emln=emln_content)
            result = infer.run()


            # write to file if run from commandline, otherwise save result to project results
            if options.get('outputfile') is not None:
                output = StringIO.StringIO()
                result.write(output)
                with open(os.path.abspath(options.get('outputfile')), 'w') as f:
                    f.write(output.getvalue())
                logger.info('saved result to {}'.format(os.path.abspath(options.get('outputfile'))))
            elif self.save.get():
                output = StringIO.StringIO()
                result.write(output)
                fname = self.output_filename.get()
                self.project.add_result(fname, output.getvalue())
                self.project.save(dirpath=self.dir)
                logger.info('saved result to file results/{} in project {}'.format(fname, self.project.name))
            else:
                logger.debug('No output file given - results have not been saved.')
        except:
            traceback.print_exc()

        # restore main window
        sys.stdout.flush()
        self.master.deiconify()
Beispiel #14
0
 def _print_unsatisfied_constraints(self):
     out("   %d unsatisfied:  %s" % (len(self.unsatisfied), map(str, [self.clauses[i] for i in self.unsatisfied])), tb=2)