def _expandQueries(self, queries): ''' expands the list of queries where necessary, e.g. queries that are just predicate names are expanded to the corresponding list of atoms ''' equeries = [] for query in queries: #print "got query '%s' of type '%s'" % (str(query), str(type(query))) if type(query) == str: prevLen = len(equeries) if "(" in query: # a fully or partially grounded formula f = FOL.parseFormula(query) for gndFormula in f.iterGroundings(self.mln): equeries.append(gndFormula[0]) else: # just a predicate name try: for gndPred in self.mln._getPredGroundings(query): equeries.append(FOL.parseFormula(gndPred).ground(self.mln, {})) except: raise #Exception("Could not expand query '%s'" % query) if len(equeries) - prevLen == 0: raise Exception("String query '%s' could not be expanded." % query) elif isinstance(query, FOL.Formula): equeries.append(query) else: raise Exception("Received query of unsupported type '%s'" % str(type(query))) return equeries
def _infer(self, verbose=True, details=False, fittingMethod=InferenceMethods.Exact, fittingThreshold=1e-3, fittingSteps=100, fittingParams=None, maxThreshold=None, greedy=False, **args): # add formulas to the model whose weights we can then fit if verbose: print "extending model with %d formulas whose weights will be fit..." % len(self.mrf.softEvidence) for req in self.mrf.softEvidence: formula = FOL.parseFormula(req["expr"]) idxFormula = self.mrf._addFormula(formula, 0.0) gndFormula = formula.ground(self.mrf, {}) self.mrf._addGroundFormula(gndFormula, idxFormula) req["gndExpr"] = req["expr"] req["gndFormula"] = gndFormula req["idxFormula"] = idxFormula # do fitting if fittingParams is None: fittingParams = {} fittingParams.update(args) results, self.data = self.mrf._fitProbabilityConstraints(self.mrf.softEvidence, fittingMethod=fittingMethod, fittingThreshold=fittingThreshold, fittingSteps=fittingSteps, given=self.given, queries=self.queries, verbose=details, fittingParams=fittingParams, maxThreshold=maxThreshold, greedy=greedy) return results