コード例 #1
0
    def test_equal(self):
        v0 = Variable(Interval(0, 10))
        v1 = Variable(Interval(7, 15))

        nw = ConstraintNetwork()
        nw.set_equal(v0, v1)
        nw.minimize_network()
        assert v0 == v1
コード例 #2
0
    def test_hashable(self):
        var = Variable(Interval(10, 150))
        assert isinstance(var, Hashable)

        var = [Variable(Interval(0, x)) for x in range(100)]
        varset = set(var)

        assert len(varset) == 100
        assert all(v in varset for v in var)
コード例 #3
0
    def test_ordering(self):
        inter = Interval(0, 15)
        var1 = Variable(inter)

        var2 = Variable(inter.move(5))
        assert var1 < var2
        assert var1 != var2

        var2 = Variable(inter.move(-5))
        assert var1 > var2
        assert var1 != var2

        var2 = Variable(Interval(0, 5))
        assert var1 > var2
        assert var1 != var2

        var2 = Variable(Interval(0, 20))
        assert var1 < var2
        assert var1 != var2

        var2 = Variable(Interval(0, 15))
        assert var1 == var2
        assert not var1 < var2
        assert not var1 > var2

        var2 = Variable(None)
        assert var1 != var2
        assert var1 > var2
        assert not var1 < var2

        var2 = Variable()
        assert var1 != var2
        assert var1 > var2
コード例 #4
0
    def test_add_constraint(self):
        # Known example assertion (Detcher STP example in TCN paper)
        v0, v1, v2, v3, v4 = [Variable() for _ in range(5)]
        v0.value = Interval(0, 0)
        nw = ConstraintNetwork()
        nw.add_constraint(v0, v1, Interval(10, 20))
        nw.add_constraint(v1, v2, Interval(30, 40))
        nw.add_constraint(v3, v2, Interval(10, 20))
        nw.add_constraint(v3, v4, Interval(40, 50))
        nw.add_constraint(v0, v4, Interval(60, 70))
        nw.minimize_network()
        assert v0.value == Interval(0, 0)
        assert v1.value == Interval(10, 20)
        assert v2.value == Interval(40, 50)
        assert v3.value == Interval(20, 30)
        assert v4.value == Interval(60, 70)

        # Testing if a stricker constraint is applied
        v0, v1, v2, v3, v4 = [Variable() for _ in range(5)]
        v0.value = Interval(0, 0)
        nw = ConstraintNetwork()
        nw.add_constraint(v0, v1, Interval(10, 20))
        nw.add_constraint(v1, v2, Interval(30, 40))
        nw.add_constraint(v3, v2, Interval(10, 20))
        nw.add_constraint(v3, v4, Interval(40, 50))
        nw.add_constraint(v0, v4, Interval(60, 70))

        nw.add_constraint(v0, v1, Interval(10, 15))
        nw.add_constraint(v1, v2, Interval(30, 35))
        nw.add_constraint(v3, v2, Interval(10, 15))
        nw.add_constraint(v3, v4, Interval(40, 45))
        nw.add_constraint(v0, v4, Interval(60, 65))
        nw.minimize_network()
        assert v0.value == Interval(0, 0)
        assert v1.value == Interval(10, 10)
        assert v2.value == Interval(40, 40)
        assert v3.value == Interval(25, 25)
        assert v4.value == Interval(65, 65)
コード例 #5
0
    def test_set_before(self):
        v0, v1 = [Variable(Interval(-1, x)) for x in range(2)]
        nw = ConstraintNetwork()
        nw.set_before(v0, v1)
        nw.minimize_network()
        assert v0 < v1
        assert v0.value.start == v1.value.start
        assert v0.value.start == -1

        nw.set_before(v1, v0)
        nw.minimize_network()
        assert v0 == v1
        assert v0.value == v1.value
        assert v0.value.start == -1
コード例 #6
0
    def test_deep_copy(self):
        var1 = Variable(Interval(10, 150))
        var2 = copy.deepcopy(var1)
        var3 = copy.deepcopy(var1)

        assert var1 is not var2 and var1 is not var3
        assert var1 == var2 and var1 == var3
        assert var2 is not var3
        assert var2 == var3

        assert var1.value is not var2.value and var1.value is not var3.value
        assert var1.value == var2.value and var1.value == var3.value
        assert var2.value is not var3.value
        assert var2.value == var3.value

        assert hash(var1.value) == hash(var2.value) == hash(var3.value)
        assert hash(var1.value) is hash(var1.value) is hash(var3.value)

        assert hash(var1) != hash(var2) and hash(var1) != hash(var3)
        assert hash(var2) != hash(var3)
コード例 #7
0
    def test_between(self):
        v0 = Variable(Interval(0, 10))
        v1 = Variable(Interval(7, 15))
        v2 = Variable(Interval(4, 10))

        nw = ConstraintNetwork()
        nw.set_between(v0, v1, v2)
        nw.minimize_network()
        assert v0 <= v1 <= v2

        v0 = Variable(Interval(0, 10))
        v1 = Variable(Interval(7, 15))
        v2 = Variable(Interval(4, 10))

        nw = ConstraintNetwork()
        nw.set_between(v2, v1, v0)
        nw.minimize_network()
        assert v2 <= v1 <= v0
コード例 #8
0
 def test_properties(self):
     inter = Interval(0, 15)
     var = Variable(inter)
     assert var.start == 0
     assert var.end == 15
コード例 #9
0
def set_knowledge_base(knowledge):
    """
    Sets the knowledge base to be used in the interpretation, and updates
    the necessary global variables.
    """
    global KNOWLEDGE, _OBSERVABLES, _ABDUCIBLES, _LMAP, _EXCLUSION
    KNOWLEDGE = knowledge
    #First, we check the consistency of every single abstraction pattern
    for p in KNOWLEDGE:
        #In the Environment and Abstracted sets no repeated types can happen.
        for qset in (p.abstracted, p.environment):
            for q in qset:
                #The only coincidence must be q
                assert len(set(q.mro()) & qset) == 1
        #There should be no subclass relations between hyp. and abstractions
        for q in p.abstracted:
            assert not p.Hypothesis in q.mro()
        assert not set(p.Hypothesis.mro()) & p.abstracted
        #The abstraction transitions must be properly set.
        for q in p.abstractions:
            assert q in p.abstracted
            for tr in p.abstractions[q]:
                assert tr.observable is q
                assert tr.abstracted is ABSTRACTED
                assert tr in p.transitions
    #Organization of all the observables in abstraction levels.
    _OBSERVABLES = set.union(*(({p.Hypothesis} | p.abstracted | p.environment)
                               for p in KNOWLEDGE))
    _ABDUCIBLES = tuple(set.union(*(p.abstracted for p in KNOWLEDGE)))
    #To perform the level assignment, we use a constraint network.
    _CNET = ConstraintNetwork()
    #Mapping from observable types to abstraction levels.
    _LMAP = {}
    for q in _OBSERVABLES:
        _LMAP[q] = Variable(value=Interval(0, numpy.inf))
    #We set the restrictions, and minimize the network
    #All subclasses must have the same level than the superclasses
    for q in _OBSERVABLES:
        for sup in (set(q.mro()) & _OBSERVABLES) - {q}:
            _CNET.set_equal(_LMAP[q], _LMAP[sup])
    #Abstractions force a level increasing
    for p in KNOWLEDGE:
        for qabs in p.abstracted:
            _CNET.add_constraint(_LMAP[qabs], _LMAP[p.Hypothesis],
                                 Interval(1, numpy.inf))
    #Network minimization
    _CNET.minimize_network()
    #Now we assign to each observable the minimum of the solutions interval.
    for q in _OBSERVABLES:
        _LMAP[q] = int(_LMAP[q].start)
    #Manual definition of the exclusion relation between observables.
    _EXCLUSION = {
        Deflection: (Deflection, ),
        QRS: (QRS, ),
        TWave: (TWave, ),
        PWave: (PWave, ),
        CardiacCycle: (CardiacCycle, ),
        Cardiac_Rhythm: (Cardiac_Rhythm, )
    }
    #Automatic expansion of the exclusion relation.
    for q, qexc in _EXCLUSION.iteritems():
        _EXCLUSION[q] = tuple(
            (q2 for q2 in _OBSERVABLES if issubclass(q2, qexc)))
    for q in sorted(_OBSERVABLES, key=_LMAP.get, reverse=True):
        _EXCLUSION[q] = tuple(
            set.union(*(set(_EXCLUSION[q2]) for q2 in _EXCLUSION
                        if issubclass(q, q2))))