def write_elapsed_time(self, stream=sys.stdout, color=None): if stream is sys.stdout and color is None: color = True elif color is None: color = False if color: col = 'blue' else: col = None total = float(self._watch['inference'].elapsedtime) stream.write(headline('INFERENCE RUNTIME STATISTICS')) print self._watch.finish() for t in sorted(self._watch.tags.values(), key=lambda t: t.elapsedtime, reverse=True): stream.write('%s %s %s\n' % (barstr(width=30, percent=t.elapsedtime / total, color=col), elapsed_time_str(t.elapsedtime), t.label))
def start(self, savegeometry=True): # create conf from current gui settings self.update_config() self.update_mln(askoverwrite=False) # write gui settings self.write_gconfig(savegeometry=savegeometry) # hide gui self.master.withdraw() # if evidence was modified in gui, update dbs for next inference step self.update_result_from_dbeditor() # runinference try: print headline('PRAC QUERY TOOL') print next_step = self.prac_inference.runstep() curstep = self.prac_inference.lastnode.infchain[-1] evidence = curstep.outdbs module = self.prac.module(self.selected_module.get()) self.infStep = module(self.prac_inference, project=self.project, projectpath=os.path.join( self.module_dir, self.project.name)) except: cls, e, tb = sys.exc_info() sys.stderr.write("Error: %s\n" % str(e)) traceback.print_tb(tb) # restore main window self.master.deiconify()
def write_elapsed_time(self, stream=sys.stdout, color=None): if stream is sys.stdout and color is None: color = True elif color is None: color = False if color: col = 'blue' else: col = None total = float(self._watch['inference'].elapsedtime) stream.write(headline('INFERENCE RUNTIME STATISTICS')) print self._watch.finish() for t in sorted(self._watch.tags.values(), key=lambda t: t.elapsedtime, reverse=True): stream.write( '%s %s %s\n' % (barstr(width=30, percent=t.elapsedtime / total, color=col), elapsed_time_str(t.elapsedtime), t.label))
def run(self): ''' Run the MLN learning with the given parameters. ''' # load the MLN if isinstance(self.mln, MLN): mln = self.mln else: raise Exception('No MLN specified') # load the training databases if type(self.db) is list and all( [isinstance(e, Database) for e in self.db]): dbs = self.db elif isinstance(self.db, Database): dbs = [self.db] elif isinstance(self.db, str): db = self.db if db is None or not db: raise Exception('no trainig data given!') dbpaths = [os.path.join(self.directory, 'db', db)] dbs = [] for p in dbpaths: dbs.extend(Database.load(mln, p, self.ignore_unknown_preds)) else: raise Exception( 'Unexpected type of training databases: %s' % type(self.db)) if self.verbose: print(('loaded %d database(s).' % len(dbs))) watch = StopWatch() if self.verbose: confg = dict(self._config) confg.update(eval("dict(%s)" % self.params)) if type(confg.get('db', None)) is list: confg['db'] = '%d Databases' % len(confg['db']) print((tabulate( sorted(list(confg.items()), key=lambda key_v: str(key_v[0])), headers=('Parameter:', 'Value:')))) params = dict([(k, getattr(self, k)) for k in ( 'multicore', 'verbose', 'profile', 'ignore_zero_weight_formulas')]) # for discriminative learning if issubclass(self.method, DiscriminativeLearner): if self.discr_preds == QUERY_PREDS: # use query preds params['qpreds'] = self.qpreds elif self.discr_preds == EVIDENCE_PREDS: # use evidence preds params['epreds'] = self.epreds # gaussian prior settings if self.use_prior: params['prior_mean'] = self.prior_mean params['prior_stdev'] = self.prior_stdev # expand the parameters params.update(self.params) if self.profile: prof = Profile() print('starting profiler...') prof.enable() else: prof = None # set the debug level olddebug = logger.level logger.level = eval('logs.%s' % params.get('debug', 'WARNING').upper()) mlnlearnt = None try: # run the learner mlnlearnt = mln.learn(dbs, self.method, **params) if self.verbose: print() print(headline('LEARNT MARKOV LOGIC NETWORK')) print() mlnlearnt.write() except SystemExit: print('Cancelled...') finally: if self.profile: prof.disable() print(headline('PROFILER STATISTICS')) ps = pstats.Stats(prof, stream=sys.stdout).sort_stats( 'cumulative') ps.print_stats() # reset the debug level logger.level = olddebug print() watch.finish() watch.printSteps() return mlnlearnt
def learn(self, savegeometry=True, options=None, *_): if options is None: options = {} mln_content = self.mln_container.editor.get("1.0", END).strip() db_content = self.db_container.editor.get("1.0", END).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 = io.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 = io.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()
def main(): headline("Running xfold...") usage = 'PRAC Crossfold validation on pracmodules' parser = argparse.ArgumentParser(description=usage) parser.add_argument("-k", "--folds", dest="folds", type='int', default=10, help="Number of folds for k-fold Cross Validation") parser.add_argument("-p", "--percent", dest="percent", type='int', default=100, help="Use only PERCENT% of the data. (default=100)") parser.add_argument("-v", "--verbose", dest="verbose", action='store_true', default=False, help="Verbose mode.") parser.add_argument("-c", "--multicore", dest="multicore", action='store_true', default=False, help="Verbose mode.") parser.add_argument('-n', '--noisy', dest='noisy', type='str', default=None, help='-nDOMAIN defines DOMAIN as a noisy string.') parser.add_argument( '-f', '--folder', dest='folder', type='str', default=None, help='-f <folder> the folder in which the results shall be saved.') parser.add_argument('-m', '--module', dest='module', type='str', default=None, help='-m <module> the module for the mln to xfold.') parser.add_argument('-d', '--dbs', action='callback', type='string', callback=parse_list, dest='dbs') parser.add_argument('--predicate', dest='predicate', type='str', default=None, help='The predicate.') parser.add_argument('--domain', dest='domain', type='str', default=None, help='The domain.') parser.add_argument('--mln', dest='mln', type='str', default=None, help='The mln needed for training and inference.') parser.add_argument( '--altMLN', dest='altMLN', type='str', default=None, help='Alternative mln for loading the database files. Optional') parser.add_argument('--logic', dest='logic', type='str', default='FuzzyLogic', help='The logic to load the mln with.') parser.add_argument("--onthefly", dest="onthefly", default=False, action='store_true', help="Generate MLN on the fly") args = parser.parse_args() opts_ = vars(args) folds = args.folds percent = args.percent verbose = args.verbose multicore = args.multicore dirname = args.folder moduleName = args.module noisy = ['text'] predName = args.predicate domain = args.domain dbfiles = args.dbs mlnFileName = args.mln altMLNFileName = args.altMLN or args.mln # equal to mlnFileName if no alternative mln given logic = args.logic onthefly = args.onthefly startTime = time.time() # set up the directory timestamp = time.strftime("%Y-%b-%d-%H-%M-%S", time.localtime()) if dirname is None: idx = 1 while True: dirname = '%s-%d' % (moduleName, idx) idx += 1 if not os.path.exists(dirname): break dirname += '-' + timestamp expdir = os.getenv('PRAC_EXPERIMENTS', '.') expdir = os.path.join(expdir, dirname) if os.path.exists(expdir): print('Directory "%s" exists. Overwrite? ([y]/n)' % expdir, ' ') answer = sys.stdin.read(1) if answer not in ('y', '\n'): exit(0) else: shutil.rmtree(expdir) os.mkdir(expdir) # set up the logger logs.getlogger().setLevel(logs.INFO) log = logs.getlogger() fileLogger = FileHandler(os.path.join(expdir, 'xval.log')) fileLogger.setFormatter(logs.formatter) log.addHandler(fileLogger) log.info('Log for %d-fold cross-validation of %s using %s' % (folds, moduleName, dbfiles)) log.info('Date: %s' % timestamp) log.info('Results will be written into %s' % expdir) # load module prac = PRAC() module = prac.module(moduleName) # read MLN and dbs mln_ = readMLNFromFile(mlnFileName, logic=logic) altMLN = readMLNFromFile(altMLNFileName, logic=logic) dbs = [] for dbfile in dbfiles: db = readDBFromFile(altMLN, dbfile) if type(db) is list: dbs.extend(db) else: dbs.append(db) log.info('Read %d databases.' % len(dbs)) cwpreds = [pred for pred in mln_.predicates if pred != predName] # create the partition of data subsetLen = int(math.ceil(len(dbs) * percent / 100.0)) if subsetLen < len(dbs): log.info('Using only %d of %d DBs' % (subsetLen, len(dbs))) dbs = sample(dbs, subsetLen) if len(dbs) < folds: log.error( 'Cannot do %d-fold cross validation with only %d databases.' % (folds, len(dbs))) exit(0) shuffle(dbs) partSize = int(math.ceil(len(dbs) / float(folds))) partition = [] for i in range(folds): partition.append(dbs[i * partSize:(i + 1) * partSize]) foldRunnables = [] for foldIdx in range(folds): params = XValFoldParams() params.mln = mln_.duplicate() params.altMLN = altMLN.duplicate() params.learnDBs = [] for dbs in [d for i, d in enumerate(partition) if i != foldIdx]: params.learnDBs.extend(dbs) params.testDBs = partition[foldIdx] params.foldIdx = foldIdx params.foldCount = folds params.noisyStringDomains = noisy params.directory = expdir params.queryPred = predName params.queryDom = domain params.module = module params.logic = logic params.mlnFileName = mlnFileName params.altMLNFileName = altMLNFileName params.onthefly = onthefly foldRunnables.append(XValFold(params)) log.info('Params for fold %d:\n%s' % (foldIdx, str(params))) if multicore: # set up a pool of (non-daemon!!) worker processes try: workerPool = NDPool() log.info('Starting %d-fold Cross-Validation in %d processes.' % (folds, workerPool._processes)) result = workerPool.map_async(runFold, foldRunnables).get() workerPool.close() workerPool.join() cm = ConfusionMatrixSim() for r in result: cm.combine(r.confMatrix) elapsedTime = time.time() - startTime cm.toFile(os.path.join(expdir, 'conf_matrix.cm')) cm.precisionsToFile(os.path.join(expdir, 'precisions.txt')) cm.precisionsToFile(os.path.join(expdir, 'precisions_sim.txt'), sim=True) pdfname = 'conf_matrix' pdfnameSim = 'conf_matrix_sim' log.info('creating pdf if confusion matrix...') cm.toPDF(pdfname) cm.toPDF(pdfnameSim, sim=True) os.rename('{}.pdf'.format(pdfname), os.path.join(expdir, '{}.pdf'.format(pdfname))) os.rename('{}_sim.pdf'.format(pdfname), os.path.join(expdir, '{}_sim.pdf'.format(pdfname))) except (KeyboardInterrupt, SystemExit, SystemError): log.critical("Caught KeyboardInterrupt, terminating workers") workerPool.terminate() workerPool.join() exit(1) except: log.error('\n' + ''.join(traceback.format_exception(*sys.exc_info()))) exit(1) else: log.info( 'Starting {}-fold Cross-Validation in 1 process.'.format(folds)) cm = ConfusionMatrixSim() for fold in foldRunnables: cm.combine(runFold(fold).confMatrix) elapsedTime = time.time() - startTime cm.toFile(os.path.join(expdir, 'conf_matrix.cm')) cm.precisionsToFile(os.path.join(expdir, 'precisions.txt')) cm.precisionsToFile(os.path.join(expdir, 'precisions_sim.txt'), sim=True) pdfname = 'conf_matrix' pdfnameSim = 'conf_matrix_sim' log.info('creating pdf if confusion matrix...') cm.toPDF(pdfname) cm.toPDF(pdfnameSim, sim=True) os.rename('{}.pdf'.format(pdfname), os.path.join(expdir, '{}.pdf'.format(pdfname))) os.rename('{}_sim.pdf'.format(pdfname), os.path.join(expdir, '{}_sim.pdf'.format(pdfname))) log.info('{}-fold crossvalidation {} took {:.2f} min'.format( folds, '(MP)' if multicore else '(SP)', elapsedTime / 60.0))
def infer(self, savegeometry=True, options={}, *args): mln_content = self.mln_container.editor.get("1.0", END).strip() db_content = self.db_container.editor.get("1.0", END).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).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 = io.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 = io.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()
def run(self): watch = StopWatch() watch.tag('inference', self.verbose) # load the MLN if isinstance(self.mln, MLN): mln = self.mln else: raise Exception('No MLN specified') if self.use_emln and self.emln is not None: mlnstrio = io.StringIO() mln.write(mlnstrio) mlnstr = mlnstrio.getvalue() mlnstrio.close() emln = self.emln mln = parse_mln(mlnstr + emln, grammar=self.grammar, logic=self.logic) # load the database if isinstance(self.db, Database): db = self.db elif isinstance(self.db, list) and len(self.db) == 1: db = self.db[0] elif isinstance(self.db, list) and len(self.db) == 0: db = Database(mln) elif isinstance(self.db, list): raise Exception( 'Got {} dbs. Can only handle one for inference.'.format( len(self.db))) else: raise Exception('DB of invalid format {}'.format(type(self.db))) # expand the # parameters params = dict(self._config) if 'params' in params: params.update(eval("dict(%s)" % params['params'])) del params['params'] params['verbose'] = self.verbose if self.verbose: print((tabulate(sorted(list(params.items()), key=lambda k_v: str(k_v[0])), headers=('Parameter:', 'Value:')))) if type(db) is list and len(db) > 1: raise Exception('Inference can only handle one database at a time') elif type(db) is list: db = db[0] params['cw_preds'] = [x for x in self.cw_preds if bool(x)] # extract and remove all non-algorithm for s in GUI_SETTINGS: if s in params: del params[s] if self.profile: prof = Profile() print('starting profiler...') prof.enable() # set the debug level olddebug = logger.level logger.level = (eval('logs.%s' % params.get('debug', 'WARNING').upper())) result = None try: mln_ = mln.materialize(db) mrf = mln_.ground(db) inference = self.method(mrf, self.queries, **params) if self.verbose: print() print((headline('EVIDENCE VARIABLES'))) print() mrf.print_evidence_vars() result = inference.run() if self.verbose: print() print((headline('INFERENCE RESULTS'))) print() inference.write() if self.verbose: print() inference.write_elapsed_time() except SystemExit: traceback.print_exc() print('Cancelled...') finally: if self.profile: prof.disable() print((headline('PROFILER STATISTICS'))) ps = pstats.Stats(prof, stream=sys.stdout).sort_stats('cumulative') ps.print_stats() # reset the debug level logger.level = olddebug if self.verbose: print() watch.finish() watch.printSteps() return result
def main(): headline("Running main...") usage = 'PRAC Object Recognition tool' parser = argparse.ArgumentParser(description=usage) parser.add_argument( "-i", "--interactive", dest="interactive", default=False, action='store_true', help="Starts PRAC object recognition with an interactive " "GUI tool.") parser.add_argument( "-t", "--train", dest="trainMLN", nargs=1, default=None, help= "Train given MLN with inference results from argument. Example: pracobjrec -t orange.n.01 'It is a yellow " "or orange fruit.'") parser.add_argument( "-r", "--regular", dest="regular", default=False, action='store_true', help="Runs regular inference pipeline. Arguments: mlnName") parser.add_argument("-f", "--onthefly", dest="onthefly", default=False, action='store_true', help="Generate MLN on the fly") parser.add_argument( "-m", "--mln", nargs=2, dest='mln', default=None, help="Runs regular inference pipeline. Arguments: mlnName") args = parser.parse_args() opts_ = vars(args) interactive = args.interactive regular = args.regular sentences = args prac = PRAC() prac.wordnet = WordNet(concepts=None) infer = PRACInference(prac, sentences) # in case we have natural-language parameters, parse them if len(infer.instructions) > 0: parser = prac.module('nl_parsing') prac.run(infer, parser) if interactive: # use the GUI logger.info('Entering interactive mode') gui = PRACQueryGUI(infer) gui.open() elif args.trainMLN: # training with property inference output logger.info( 'Training MLN {} with result from property inference'.format( args.trainMLN)) # property inference from parsed input propExtract = prac.module('prop_extraction') prac.run(infer, propExtract) objRecog = prac.module('obj_recognition') praclearn = PRACLearning(prac) praclearn.otherParams['mln'] = args.mln[0] praclearn.otherParams['logic'] = args.mln[1] praclearn.otherParams['concept'] = args.trainMLN praclearn.otherParams['onthefly'] = args.onthefly praclearn.training_dbs = infer.inference_steps[-1].output_dbs objRecog.train(praclearn) sys.exit(0) else: # regular PRAC pipeline logger.info('Entering regular inference pipeline') # property inference from parsed input propExtract = prac.module('prop_extraction') prac.run(infer, propExtract) objRecog = prac.module('obj_recognition') # object inference based on inferred properties prac.run(infer, objRecog) step = infer.inference_steps[-1] print() print(prac_heading('PRAC INFERENCE RESULTS')) print() print('Object description: {}'.format( colorize(''.join(sentences), (None, 'white', True), True))) print() for db in step.output_dbs: print('Inferred properties:') for ek in sorted(db.evidence): e = db.evidence[ek] if e == 1.0 and any( ek.startswith(p) for p in [ 'color', 'size', 'shape', 'hypernym', 'hasa', 'dimension', 'consistency', 'material' ]): print('{}({}, {}'.format( colorize(ek.split('(')[0], (None, 'white', True), True), colorize( ek.split('(')[1].split(',')[0], (None, 'magenta', True), True), colorize( ek.split('(')[1].split(',')[1], (None, 'green', True), True))) for db in step.output_dbs: print() print('Inferred possible concepts:') for ek in sorted(db.evidence, key=db.evidence.get, reverse=True): e = db.evidence[ek] if e > 0.001 and ek.startswith('object'): print('{} {}({}, {})'.format( colorize('{:.4f}'.format(e), (None, 'cyan', True), True), colorize('object', (None, 'white', True), True), colorize( ek.split(',')[0].split('(')[1], (None, 'magenta', True), True), colorize( ek.split(',')[1].split(')')[0], (None, 'yellow', True), True)))
def run(self): ''' Run the MLN learning with the given parameters. ''' # load the MLN if isinstance(self.mln, MLN): mln = self.mln else: raise Exception('No MLN specified') # load the training databases if type(self.db) is list and all( map(lambda e: isinstance(e, Database), self.db)): dbs = self.db elif isinstance(self.db, Database): dbs = [self.db] elif isinstance(self.db, basestring): db = self.db if db is None or not db: raise Exception('no trainig data given!') dbpaths = [os.path.join(self.directory, 'db', db)] dbs = [] for p in dbpaths: dbs.extend(Database.load(mln, p, self.ignore_unknown_preds)) else: raise Exception( 'Unexpected type of training databases: %s' % type(self.db)) if self.verbose: print 'loaded %d database(s).' % len(dbs) watch = StopWatch() if self.verbose: confg = dict(self._config) confg.update(eval("dict(%s)" % self.params)) if type(confg.get('db', None)) is list: confg['db'] = '%d Databases' % len(confg['db']) print tabulate( sorted(list(confg.viewitems()), key=lambda (key, v): str(key)), headers=('Parameter:', 'Value:')) params = dict([(k, getattr(self, k)) for k in ( 'multicore', 'verbose', 'profile', 'ignore_zero_weight_formulas')]) # for discriminative learning if issubclass(self.method, DiscriminativeLearner): if self.discr_preds == QUERY_PREDS: # use query preds params['qpreds'] = self.qpreds elif self.discr_preds == EVIDENCE_PREDS: # use evidence preds params['epreds'] = self.epreds # gaussian prior settings if self.use_prior: params['prior_mean'] = self.prior_mean params['prior_stdev'] = self.prior_stdev # expand the parameters params.update(self.params) if self.profile: prof = Profile() print 'starting profiler...' prof.enable() else: prof = None # set the debug level olddebug = praclog.level() praclog.level( eval('logging.%s' % params.get('debug', 'WARNING').upper())) mlnlearnt = None try: # run the learner mlnlearnt = mln.learn(dbs, self.method, **params) if self.verbose: print print headline('LEARNT MARKOV LOGIC NETWORK') print mlnlearnt.write() except SystemExit: print 'Cancelled...' finally: if self.profile: prof.disable() print headline('PROFILER STATISTICS') ps = pstats.Stats(prof, stream=sys.stdout).sort_stats( 'cumulative') ps.print_stats() # reset the debug level praclog.level(olddebug) print watch.finish() watch.printSteps() return mlnlearnt
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()
def run(self): watch = StopWatch() watch.tag('inference', self.verbose) # load the MLN if isinstance(self.mln, MLN): mln = self.mln else: raise Exception('No MLN specified') if self.use_emln and self.emln is not None: mlnstr = StringIO.StringIO() mln.write(mlnstr) mlnstr.close() mlnstr = str(mlnstr) emln = self.emln mln = parse_mln(mlnstr + emln, grammar=self.grammar, logic=self.logic) # load the database if isinstance(self.db, Database): db = self.db elif isinstance(self.db, list) and len(self.db) == 1: db = self.db[0] elif isinstance(self.db, list): raise Exception( 'Got {} dbs. Can only handle one for inference.'.format( len(self.db))) else: raise Exception('DB of invalid format {}'.format(type(self.db))) # expand the # parameters params = dict(self._config) if 'params' in params: params.update(eval("dict(%s)" % params['params'])) del params['params'] if self.verbose: print tabulate(sorted(list(params.viewitems()), key=lambda (k, v): str(k)), headers=('Parameter:', 'Value:')) # create the MLN and evidence database and the parse the queries # mln = parse_mln(modelstr, searchPath=self.dir.get(), logic=self.config['logic'], grammar=self.config['grammar']) # db = parse_db(mln, db_content, ignore_unknown_preds=params.get('ignore_unknown_preds', False)) if type(db) is list and len(db) > 1: raise Exception('Inference can only handle one database at a time') elif type(db) is list: db = db[0] # parse non-atomic params # if type(self.queries) is not list: # queries = parse_queries(mln, str(self.queries)) params['cw_preds'] = filter(lambda x: bool(x), self.cw_preds) # extract and remove all non-algorithm for s in GUI_SETTINGS: if s in params: del params[s] if self.profile: prof = Profile() print 'starting profiler...' prof.enable() # set the debug level olddebug = praclog.level() praclog.level( eval('logging.%s' % params.get('debug', 'WARNING').upper())) result = None try: mln_ = mln.materialize(db) mrf = mln_.ground(db) inference = self.method(mrf, self.queries, **params) if self.verbose: print print headline('EVIDENCE VARIABLES') print mrf.print_evidence_vars() result = inference.run() if self.verbose: print print headline('INFERENCE RESULTS') print inference.write() if self.verbose: print inference.write_elapsed_time() except SystemExit: print 'Cancelled...' finally: if self.profile: prof.disable() print headline('PROFILER STATISTICS') ps = pstats.Stats(prof, stream=sys.stdout).sort_stats('cumulative') ps.print_stats() # reset the debug level praclog.level(olddebug) if self.verbose: print watch.finish() watch.printSteps() return result
def main(): logger.level = logs.DEBUG usage = 'PRAC Query Tool' parser = argparse.ArgumentParser(description=usage) parser.add_argument("instruction", help="The instruction.") parser.add_argument( "-i", "--interactive", dest="interactive", default=False, action='store_true', help="Starts PRAC inference with an interactive GUI tool.") parser.add_argument("-v", "--verbose", dest="verbose", default=1, type=int, action="store", help="Set verbosity level {0..3}. Default is 1.") args = parser.parse_args() opts_ = vars(args) sentences = args.instruction prac = PRAC() prac.verbose = args.verbose conf = PRACMLNConfig(DEFAULT_CONFIG) if args.interactive: # use the GUI from tkinter import Tk root = Tk() # in case we have natural-language parameters, parse them infer = PRACInference(prac, sentences) if len(sentences) > 0: # module = prac.module('nl_parsing') # prac.run(infer, module) n = infer.runstep() # print parsing result for odb in n.outdbs: odb.write() # print input sentence print(n.nlinstr()) #Started control structure handling ''' cs_recognition = prac.module('cs_recognition') prac.run(inference, cs_recognition) dbs = inference.inference_steps[-1].output_dbs dbs_ = [] for db in dbs: dbs_.extend(parser.extract_multiple_action_cores(db)) inference.inference_steps[-1].output_dbs = dbs_ ''' app = PRACQueryGUI(root, infer.prac, n, conf, directory=args[0] if args else None) root.mainloop() exit(0) # regular PRAC pipeline infer = PRACInference(prac, sentences) infer.run() print(headline('inference results')) print('instructions:') for i in infer.root: print(i) frames = [] for step in infer.steps(): print(step.frame) print(prac_heading('cram plans', color='blue')) for step in infer.steps(): if hasattr(step, 'plan'): print(step.plan) # infer.write() exit(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()
def run(self): watch = StopWatch() watch.tag('inference', self.verbose) # load the MLN if isinstance(self.mln, MLN): mln = self.mln else: raise Exception('No MLN specified') if self.use_emln and self.emln is not None: mlnstr = StringIO.StringIO() mln.write(mlnstr) mlnstr.close() mlnstr = str(mlnstr) emln = self.emln mln = parse_mln(mlnstr + emln, grammar=self.grammar, logic=self.logic) # load the database if isinstance(self.db, Database): db = self.db elif isinstance(self.db, list) and len(self.db) == 1: db = self.db[0] elif isinstance(self.db, list): raise Exception( 'Got {} dbs. Can only handle one for inference.'.format( len(self.db))) else: raise Exception('DB of invalid format {}'.format(type(self.db))) # expand the # parameters params = dict(self._config) if 'params' in params: params.update(eval("dict(%s)" % params['params'])) del params['params'] if self.verbose: print tabulate(sorted(list(params.viewitems()), key=lambda (k, v): str(k)), headers=('Parameter:', 'Value:')) if type(db) is list and len(db) > 1: raise Exception('Inference can only handle one database at a time') elif type(db) is list: db = db[0] params['cw_preds'] = filter(lambda x: bool(x), self.cw_preds) # extract and remove all non-algorithm for s in GUI_SETTINGS: if s in params: del params[s] if self.profile: prof = Profile() print 'starting profiler...' prof.enable() # set the debug level olddebug = praclog.level() praclog.level(eval('logging.%s' % params.get('debug', 'WARNING').upper())) result = None try: mln_ = mln.materialize(db) mrf = mln_.ground(db) inference = self.method(mrf, self.queries, **params) if self.verbose: print print headline('EVIDENCE VARIABLES') print mrf.print_evidence_vars() result = inference.run() if self.verbose: print print headline('INFERENCE RESULTS') print inference.write() if self.verbose: print inference.write_elapsed_time() except SystemExit: print 'Cancelled...' finally: if self.profile: prof.disable() print headline('PROFILER STATISTICS') ps = pstats.Stats(prof, stream=sys.stdout).sort_stats('cumulative') ps.print_stats() # reset the debug level praclog.level(olddebug) if self.verbose: print watch.finish() watch.printSteps() return result