示例#1
0
文件: infer.py 项目: Bovril/pracmln
 def write(self, stream=sys.stdout, color=None, sort='prob', group=True, reverse=True):
     barwidth = 30
     if tty(stream) and color is None:
         color = 'yellow'
     if sort not in ('alpha', 'prob'):
         raise Exception('Unknown sorting: %s' % sort)
     results = dict(self.results)
     if group:
         for var in sorted(self.mrf.variables, key=str):
             res = dict([(atom, prob) for atom, prob in results.iteritems() if atom in map(str, var.gndatoms)])
             if not res: continue
             if isinstance(var, MutexVariable) or isinstance(var, SoftMutexVariable):
                 stream.write('%s:\n' % var)
             if sort == 'prob':
                 res = sorted(res, key=self.results.__getitem__, reverse=reverse)
             elif sort == 'alpha':
                 res = sorted(res, key=str)
             for atom in res:
                 stream.write('%s %s\n' % (barstr(barwidth, self.results[atom], color=color), atom))
         return
     # first sort wrt to probability
     results = sorted(results, key=self.results.__getitem__, reverse=reverse)
     # then wrt gnd atoms
     results = sorted(results, key=str)
     for q in results:
         stream.write('%s %s\n' % (barstr(barwidth, self.results[q], color=color), q))
     self._watch.printSteps()
示例#2
0
 def write(self, stream=sys.stdout, color=None, bars=True):
     '''
     Writes this database into the stream in the MLN Database format.
     The stream must provide a `write()` method as file objects do.
     '''
     if color is None:
         if stream != sys.stdout:
             color = False
         else:
             color = True
     for atom in sorted(self._evidence):
         truth = self._evidence[atom]
         pred, params = self.mln.logic.parse_atom(atom)
         pred = str(pred)
         params = map(str, params)
         if bars:
             bar = barstr(30, truth, color='magenta' if color else None)
         else:
             bar = ''
         if color:
             strout = '%s  %s\n' % (
                 bar if bars else colorize('%.6f' % truth,
                                           (None, 'magenta', False), True),
                 FirstOrderLogic.Lit(False, pred, params,
                                     self.mln).cstr(color))
         else:
             strout = '%s  %s\n' % (bar if bars else '%.6f' % truth,
                                    FirstOrderLogic.Lit(
                                        False, pred, params,
                                        self.mln).cstr(color))
         stream.write(strout)
示例#3
0
 def write(self,
           stream=sys.stdout,
           color=None,
           sort='prob',
           group=True,
           reverse=True):
     barwidth = 30
     if tty(stream) and color is None:
         color = 'yellow'
     if sort not in ('alpha', 'prob'):
         raise Exception('Unknown sorting: %s' % sort)
     results = dict(self.results)
     if group:
         for var in sorted(self.mrf.variables, key=str):
             res = dict([(atom, prob) for atom, prob in results.iteritems()
                         if atom in map(str, var.gndatoms)])
             if not res: continue
             if isinstance(var, MutexVariable) or isinstance(
                     var, SoftMutexVariable):
                 stream.write('%s:\n' % var)
             if sort == 'prob':
                 res = sorted(res,
                              key=self.results.__getitem__,
                              reverse=reverse)
             elif sort == 'alpha':
                 res = sorted(res, key=str)
             for atom in res:
                 stream.write('%s %s\n' % (barstr(
                     barwidth, self.results[atom], color=color), atom))
         return
     # first sort wrt to probability
     results = sorted(results,
                      key=self.results.__getitem__,
                      reverse=reverse)
     # then wrt gnd atoms
     results = sorted(results, key=str)
     for q in results:
         stream.write('%s %s\n' %
                      (barstr(barwidth, self.results[q], color=color), q))
     self._watch.printSteps()
示例#4
0
文件: infer.py 项目: Bovril/pracmln
 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))
示例#5
0
 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))
示例#6
0
 def write(self, stream=sys.stdout, color=None, bars=True):
     '''
     Writes this database into the stream in the MLN Database format.
     The stream must provide a `write()` method as file objects do.
     '''
     if color is None:
         if stream != sys.stdout: 
             color = False
         else: color = True
     for atom in sorted(self._evidence):
         truth = self._evidence[atom]
         pred, params = self.mln.logic.parse_atom(atom)
         pred = str(pred)
         params = map(str, params)
         if bars:
             bar = barstr(30, truth, color='magenta' if color else None)
         else:
             bar = ''
         if color:
             strout = '%s  %s\n' % (bar if bars else  colorize('%.6f' % truth, (None, 'magenta', False), True), 
                                    FirstOrderLogic.Lit(False, pred, params, self.mln).cstr(color))
         else:
             strout = '%s  %s\n' % (bar if bars else  '%.6f' % truth, FirstOrderLogic.Lit(False, pred, params, self.mln).cstr(color))
         stream.write(strout)
示例#7
0
 def write_pls(self):
     for var in self.mrf.variables:
         print repr(var)
         for i, value in var.itervalues():
             print '    ', barstr(width=50, color='magenta', percent=self._pls[var.idx][i]) + ('*' if var.evidence_value_index() == i else ' '), i, value