Object-Oriented Probabilistic Relational Model

Creative Commons License

aGrUM

interactive online version

In [1]:
import os

os.chdir("res")

import pyagrum as gum
import pyagrum.lib.notebook as gnb

O3PRM is inspired by relational language, directly based on the BN and improved by object oriented paradigms, where the focus is set on classes of objects and by defining relations among these objects (see http://o3prm.gitlab.io/ for more details)

6e34a0bdefe44baf95517155cbab76b9

O3PRM syntax

In [2]:
with open("./Asia.o3prm", "r") as prm:
  for line in prm.readlines():
    print(line, end="")
class Asia {

    boolean visitToAsia {
        [ 0.99,  // False
          0.01 ] // True
    };

    boolean tuberculosis dependson visitToAsia {
        // False | True
        [    0.99, 0.95,  // False
             0.01, 0.05 ] // True
    };

    boolean smoking {
        [ 0.50,  // False
          0.50 ] // True
    };

    boolean lungCancer dependson smoking {
        // False | True => smoking
        [    0.99, 0.90,  // False
             0.01, 0.10 ] // True
    };

    boolean bronchitis dependson smoking {
        // False | True => smoking
        [    0.70, 0.40,  // False
             0.30, 0.60 ] // True
    };

    boolean tubOrCancer dependson tuberculosis, lungCancer {
        //      False    ||     True      => tuberculosis
        //  False | True || False | True  => lungCancer
        [     1.00, 0.00,     0.00, 0.00,  // False
              0.00, 1.00,     1.00, 1.00 ] // True
    };

    boolean positiveXRay dependson tubOrCancer {
        // False | True => tubOrCancer
        [ 0.95, 0.02,  // False
          0.05, 0.98 ] // True
    };

    boolean dyspnea dependson tubOrCancer, bronchitis {
        //      False    ||     True      => tubOrCancer
        //  False | True || False | True  => bronchitis
        [     0.90, 0.20,     0.30, 0.10,  // False
              0.10, 0.80,     0.70, 0.90 ] // True
    };
}

Using o3prm syntax for creating BayesNet

In [3]:
bn = gum.loadBN("./Asia.o3prm", verbose=False)
bn
Out[3]:
G visitToAsia visitToAsia tuberculosis tuberculosis visitToAsia->tuberculosis positiveXRay positiveXRay bronchitis bronchitis dyspnea dyspnea bronchitis->dyspnea lungCancer lungCancer tubOrCancer tubOrCancer lungCancer->tubOrCancer tubOrCancer->positiveXRay tubOrCancer->dyspnea smoking smoking smoking->bronchitis smoking->lungCancer tuberculosis->tubOrCancer
In [4]:
bn = gum.loadBN("./aSys.o3prm")
bn
Out[4]:
G A2.Cost A2.Cost A0.Productivity A0.Productivity A2.Agg_Productivity A2.Agg_Productivity A0.Productivity->A2.Agg_Productivity A0.Cost A0.Cost A0.Productivity->A0.Cost A1.Capacity A1.Capacity A1.Cost A1.Cost A1.Capacity->A1.Cost A2.Productivity A2.Productivity A2.Agg_Productivity->A2.Productivity A2.Productivity->A2.Cost A0.Capacity A0.Capacity A0.Capacity->A0.Cost A2.Capacity A2.Capacity A2.Capacity->A2.Cost A1.Productivity A1.Productivity A1.Productivity->A2.Agg_Productivity A1.Productivity->A1.Cost
In [5]:
classpath = "./ComplexPrinters"
filename = "./ComplexPrinters/fr/lip6/printers/system.o3prm"

system = "Work"
bn = gum.loadBN(filename, system=system, classpath=classpath)
In [6]:
# the inference will take place in a rather large junction tree
gnb.showJunctionTreeMap(bn, scaleClique=0.1, scaleSep=0.05, lenEdge=1.2, size="8!")
../_images/notebooks_25-Models_o3prm_10_0.svg
In [8]:
gum.saveBN(bn, "bnprinters.bifxml")
In [9]:
bn2 = gum.loadBN("bnprinters.bifxml")
gnb.showInference(bn2, size="25!")
../_images/notebooks_25-Models_o3prm_12_0.svg
In [ ]:
# if cairosvg is installed, one can export a graph (or inference) as a pdf
# gnb.exportInference(bn,filename="../out/ComplexPrinters.pdf")
In [ ]:
bn = gum.loadBN("./aSys.o3prm")
gnb.sideBySide(gnb.getBN(bn, size="5"), gnb.getInference(bn, size="5"))

Exploring Probabilistic Relational Model

In [ ]:
classpath = "./ComplexPrinters"
filename = "./ComplexPrinters/complexprinters_system.o3prm"
explor = gum.PRMexplorer()
explor.load(filename)
In [ ]:
for cl in explor.classes():
  print("Class : " + cl)
  print("  - Super class : " + ("None" if explor.getSuperClass(cl) == None else explor.getSuperClass(cl)))
  print("  - Implemented interface : ")
  for inter in explor.classImplements(cl):
    print("      " + inter)
  print("  - Direct sub-types : ")
  for ext in explor.getDirectSubClass(cl):
    print("      " + ext)
  print("  - Attributes : ")
  for t, n, depensons in explor.classAttributes(cl):
    s = ""
    for depenson in depensons:
      s = s + depenson + " "
    print("      " + t + " " + n + " (" + s + ")")
  print("  - References : ")
  for t, n, isArray in explor.classReferences(cl):
    print("      " + t + ("[]" if isArray else "") + " " + n)
  print("  - Aggragates : ")
  for t, n, g, l, slots in explor.classAggregates(cl):
    s = ""
    for slot in slots:
      s = s + slot + " "
    print("      " + t + " " + n + " " + g + " " + ("NoLabel" if l == None else l) + " (" + s + ")")
  print("  - SlotChains : ")
  for t, n, isMultiple in explor.classSlotChains(cl):
    print("      " + t + " " + n + " " + ("[]" if isMultiple else ""))
  print("  - Parameters : ")
  for param in explor.classParameters(cl):
    print("      " + param)
  # print("  - Dag : ")
  # (dic, dotString) = explor.classDag(cl)
  # print(dic)
  # print(dotString)

  print()
In [ ]:
print("The following lists the systems of the prm:\n")
systems = explor.getalltheSystems()
sys1 = systems[0]
print("Name of the system: " + sys1[0] + "\n")
print("Nodes : dict(id: [name,type])")
print(sys1[1])
print("\n")
print("Arcs : List[(tail, head),(tail, head)...]")
print(sys1[2])
In [ ]:
gnb.showTensor(explor.cpf("Computer", "equipState"))
In [ ]:
for cl in explor.types():
  print("Type : " + cl)
  print("  - Super type : " + ("None" if explor.getSuperType(cl) == None else explor.getSuperType(cl)))
  print("  - Direct sub-types : ")
  for name in explor.getDirectSubTypes(cl):
    print("      " + name)
  print("  - Labels : ")
  for t in explor.getLabels(cl):
    print("      " + t)
  print("  - Labels mapping : ")
  for key, val in dict().items() if explor.getLabelMap(cl) == None else explor.getLabelMap(cl).items():
    print("      " + key + " -> " + val)
  print()
In [ ]:
for cl in explor.interfaces():
  print("Interface : " + cl)
  print("  - Super interface : " + ("None" if explor.getSuperInterface(cl) == None else explor.getSuperInterface(cl)))
  print("  - Direct sub-interfaces : ")
  for name in explor.getDirectSubInterfaces(cl):
    print("      " + name)
  print("  - Implementations : ")
  for impl in explor.getImplementations(cl):
    print("      " + impl)
  print("  - Attributes : ")
  for t, n in explor.interAttributes(cl, allAttributes=True):
    print("      " + t + " " + n)
  print("  - References : ")
  for t, n, isArray in explor.interReferences(cl):
    print("      " + t + ("[]" if isArray else "") + " " + n)
  print()
In [ ]:
print(explor.isType("fr.lip6.printers.base.Printer"))
print(explor.isClass("fr.lip6.printers.base.Printer"))
print(explor.isInterface("fr.lip6.printers.base.Printer"))