def test_tree_to_word():
    """ Test the transformation from a tree (root node) to a word """
    root = Node("a", Node("b", Node("1"), Node("2")), Node("3"))
    assert_equals(str(MPT(root)), "a b 1 2 3")

    root = Node("a", Node("bef", Node("a", Node("6"), Node("8")), Node("2")),
                Node("13"))
    assert_equals(str(MPT(root)), "a bef a 6 8 2 13")
예제 #2
0
def test_equals():
    """ Test the equality testing of MPTs """
    mpt1 = MPT("a b c 1 2 a 4 e 4 5 d 6 7")
    mpt2 = MPT("pq b c 1 2 pq 4 e 4 5 z 6 7")
    mpt3 = MPT("a b c 1 2 a 4 e 4 5 6")
    mpt4 = MPT("pq b c 1 2 pq 4 e 4 5 d 6 8")

    assert_equals(mpt1, mpt2)
    assert_false(mpt1 == mpt3)
    assert_false(mpt1 != mpt2)
    assert_false(mpt4 == mpt1)
def test_mpt_from_word():
    """ Test if building from a word functions properly """
    mpt_obj = context.MPTS["test1"]

    mpt_obj2 = MPT("a bc c 0 1 a 2 e 2 3 d 4 5")

    assert_equals(mpt_obj, mpt_obj2)
예제 #4
0
    def build_mpt_from_subtrees(self, subtrees):
        """ Build MPTs from the given subtrees

        Parameters
        ----------
        subtrees : [str]
            List of subtrees

        Returns
        -------

        """

        mpts = []
        leaf_step = 0

        for subtree in subtrees:
            # transform each subtree to bmpt
            bmpt = trans.easy_to_bmpt(subtree, leaf_step=leaf_step)
            mpts.append(MPT(bmpt))
            leaf_step += len(subtree)

        joint = joint_tree.join(mpts)
        joint.subtrees = subtrees  # TODO to easy?

        return joint
def test_to_tikz():
    """ Test the conversion to tikz """
    tikz_str = r"\Tree [.a 13 [.ce [.e 5 6 ] 0 ] ]"

    mpt = MPT("a 13 ce e 5 6 0")

    assert_equals(visualize_mpt.to_tikz(mpt), tikz_str)
def test_word_to_tree():
    """ Test the transformation from a word to a tree (root node) """
    root = Node("a", Node("bef", Node("a", Node("6"), Node("8")), Node("2")),
                Node("13"))
    root2 = transformations.word_to_nodes(MPTWord("a bef a 6 8 2 13"))

    assert_equals(MPT(root), MPT(root2))

    mpt = context.MPTS["2htms"]

    word = MPTWord(
        "y0 y5 y8 Do 0 G1 0 1 Dn 3 G1 2 3 y6 Do 4 G2 4 5 y7 Dn 7 G2 6 7 Do 8 G3 8 9 y1 y4 Dn 11 G3 10 11 Do 12 G4 12 13 y2 Dn 15 G4 14 15 y3 Do 16 G5 16 17 Dn 19 G5 18 19"
    )
    mpt2 = MPT(transformations.word_to_nodes(word))
    print(type(mpt))
    print(type(mpt2))
    assert_equals(mpt, mpt2)
예제 #7
0
def test_get_levels():
    str_ = "b c 2 1 a 2 d 1 0"

    mpt = MPT(str_)

    levels = mpt.get_levels(mpt.root)
    levels = {
        key: [node.content for node in value]
        for key, value in levels.items()
    }
    print(levels)
    assert_equals(
        {
            0: ["b"],
            1: ["c", "a"],
            2: ["2", "1", "2", "d"],
            3: ["1", "0"]
        }, levels)
def test_apply_rgs():
    mpt = "a a 0 1 b 2 3"
    param = "a"
    rgs = [1, 0]

    res = sub.apply_rgs(param, rgs, mpt.split(" "))
    assert_equals(res, ["a1", "a", "0", "1", "b", "2", "3"])

    p2 = "b"
    rgs2 = [12]
    subst = sub.Substitution({param: rgs, p2: rgs2})
    mpt = MPT(mpt)
    assert_equals(subst.apply(mpt), "a1 a 0 1 b12 2 3")
def test():
    mpt1 = MPT("a bef a 6 8 2 13")
    res = transformations.get_formulae(mpt1)

    print(res)

    assert_true({
        '2': ['a * (1-bef)'],
        '6': ['a * bef * a'],
        '8': ['a * bef * (1-a)'],
        '13': ['(1-a)']
    } == res)

    leaf = lambda x: all([ch in string.ascii_uppercase for ch in x])
    mpt1 = MPT('p A B', leaf_test=leaf)

    assert_equals(transformations.get_formulae(mpt1), {
        'A': ['p'],
        'B': ['(1-p)']
    })

    mpt2 = MPT('r N g N O', leaf_test=leaf)
    assert_equals(transformations.get_formulae(mpt2), {
        'N': ['r', '(1-r) * g'],
        'O': ['(1-r) * (1-g)']
    })

    mpt3 = context.MPTS["test1"]

    assert_equals(
        transformations.get_formulae(mpt3), {
            '0': ['a * bc * c'],
            '1': ['a * bc * (1-c)'],
            '2': ['a * (1-bc) * a', 'a * (1-bc) * (1-a) * e'],
            '3': ['a * (1-bc) * (1-a) * (1-e)'],
            '4': ['(1-a) * d'],
            '5': ['(1-a) * (1-d)']
        })
예제 #10
0
    def eval_random_model(self, subtrees):
        # deletion
        del_tree = self.random_deletion_model()

        # substitutions
        param_rgs = self.random_substitution_configs(del_tree)

        subst = substitution.Substitution(param_rgs)
        model = MPT(subst.apply(del_tree))
        model.subtrees = subtrees

        if props.check(model, 'identifiable'):

            evaluation = fitting.fit_mpt(model,
                                         self.func,
                                         self.data_path,
                                         sep=self.sep)
            print(str(model))
            print(evaluation)
            print()
            sys.stdout.flush()
            return str(model), evaluation

        return None, None
예제 #11
0
    def build_mpt_from_subtrees(self, subtrees):
        """ Build MPTs from the given subtrees

        Parameters
        ----------
        subtrees : [str]
            List of subtrees

        Returns
        -------
        joint tree

        """

        mpts = []
        for subtree in subtrees:

            bmpt = subtree[0]
            mpts.append(MPT(bmpt, leaf_test=self.leaf_test))

        joint = joint_tree.join(mpts)
        joint.subtrees = subtrees  # TODO to easy?

        return joint
예제 #12
0
import os

from mptpy.tools.parsing import Parser
from mptpy.mpt import MPT

PARSER = Parser()
MPTS = {}

for entry in os.scandir('./tests/test_models/test_build'):
    MPTS[entry.name.split(".")[0]] = PARSER.parse(entry.path)

MPTS["testdeletion"] = MPT("b c 2 1 a 2 d 1 0")
for mpt in MPTS:
    print(mpt, MPTS[mpt])
예제 #13
0
def test_tree_length():
    """ Test the tree length function """
    mpt = MPT("pq b c 1 2 pq 4 e 4 5 z 6 7")
    assert_equals(len(mpt.root), 13)
    assert_equals(len(mpt.root.neg), 3)
    assert_equals(len(mpt.root.pos), 9)