Ejemplo n.º 1
0
    def test_duplicate_predicate_name(self):
        model = Model('test-predicate')
        predicate_name = 'Foo'

        a = Predicate(predicate_name, closed = True, size = 2)
        model.add_predicate(a)

        # Adding the same predicate again should by no issue.
        model.add_predicate(a)

        try:
            b = Predicate(predicate_name, closed = True, size = 2)
            model.add_predicate(b)
            self.fail('Duplicate predicate name did not raise an exception.')
        except PredicateError:
            # Expected
            pass

        try:
            b = Predicate(predicate_name, closed = True, size = 3)
            model.add_predicate(b)
            self.fail('Duplicate predicate name did not raise an exception.')
        except PredicateError:
            # Expected
            pass
Ejemplo n.º 2
0
def add_predicates(model):
    link_predicate = Predicate('Link', closed=True, size=2)
    model.add_predicate(link_predicate)

    hascat_predicate = Predicate('HasCat', closed=False, size=2)
    model.add_predicate(hascat_predicate)

    return link_predicate, hascat_predicate
Ejemplo n.º 3
0
def add_predicates(model):
    predicate = Predicate('Similar', closed = True, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate('Block', closed = True, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate('Friends', closed = False, size = 2)
    model.add_predicate(predicate)
Ejemplo n.º 4
0
def add_predicates(model):
    predicate = Predicate('Trusts', closed = False, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate('Knows', closed = True, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate('Prior', closed = True, size = 1)
    model.add_predicate(predicate)
Ejemplo n.º 5
0
def add_predicates (model):
    predicate = Predicate("Knows", closed = False, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate("Likes", closed = True, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate("Lived", closed = True, size = 2)
    model.add_predicate(predicate)
Ejemplo n.º 6
0
def add_predicates(model):
    predicate = Predicate('Knows', closed = False, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate('Likes', closed = True, size = 2)
    model.add_predicate(predicate)

    predicate = Predicate('Lived', closed = True, size = 2)
    model.add_predicate(predicate)
Ejemplo n.º 7
0
    def _add_predicates(self, model, relations):
        """
        Add predicates based on the given relations.
        """
        model.add_predicate(Predicate('spam_msg', closed=False, size=1))
        model.add_predicate(Predicate('prior_msg', closed=True, size=1))

        for relation in relations:
            model.add_predicate(
                Predicate('spam_{}'.format(relation), closed=False, size=1))
            model.add_predicate(
                Predicate('has_{}'.format(relation), closed=True, size=2))
Ejemplo n.º 8
0
 def test_no_quoting(self):
     predicate = Predicate('1', closed = True, size = 2)
     path = os.path.join(PSLTest.TEST_DATA_DIR, 'misc', 'binary_small_quoted.txt')
     predicate.add_data_file(Partition.OBSERVATIONS, path)
     expected = pandas.DataFrame([
         ['A', 'B', 1.0],
         ['\'C\'', '"D"', 1.0],
         ['1  ', '   2   ', 1.0],
         ['"3  "', '\'   4   \'', 1.0],
         ['\'\'5\'\'', '""6""', 1.0],
     ])
     pandas.testing.assert_frame_equal(predicate._data[Partition.OBSERVATIONS], expected)
Ejemplo n.º 9
0
    def test_name_normalization(self):
        # [(input, expected), ...]
        names = [
            ('a', 'A'),
            ('foo', 'FOO'),
            ('Bar', 'BAR'),
            ('BAZ', 'BAZ'),
            ('123', '123'),
        ]

        for (input_name, expected_name) in names:
            predicate = Predicate(input_name, closed = True, size = 2)
            self.assertEqual(predicate.name(), expected_name)
def add_predicates(model):
    predicates = [
        # (name, size, closed?)
        ('Sub', 2, True),
        ('RSub', 2, True),
        ('Mut', 2, True),
        ('RMut', 2, True),
        ('Inv', 2, True),
        ('Domain', 2, True),
        ('Range2', 2, True),
        ('SameEntity', 2, True),
        ('ValCat', 2, True),
        ('ValRel', 3, True),
        ('CandCat_General', 2, True),
        ('CandRel_General', 3, True),
        ('CandCat_CBL', 2, True),
        ('CandRel_CBL', 3, True),
        ('CandCat_CMC', 2, True),
        ('CandCat_CPL', 2, True),
        ('CandRel_CPL', 3, True),
        ('CandCat_Morph', 2, True),
        ('CandCat_SEAL', 2, True),
        ('CandRel_SEAL', 3, True),
        ('PromCat_General', 2, True),
        ('PromRel_General', 3, True),
        ('Cat', 2, False),
        ('Rel', 3, False),
    ]

    for (name, size, closed) in predicates:
        predicate = Predicate(name, closed = closed, size = size)
        model.add_predicate(predicate)
Ejemplo n.º 11
0
    def test_add_list(self):
        predicate = Predicate('Foo', closed = True, size = 2)

        input_data = [
            ['A', 'B', 0.0],
            ['C', 'D', 0.5],
            [1, 2, 1.0],
        ]
        predicate.add_data(Partition.OBSERVATIONS, input_data)

        expected = pandas.DataFrame([
            ['A', 'B', 0.0],
            ['C', 'D', 0.5],
            [1, 2, 1.0],
        ])

        pandas.testing.assert_frame_equal(predicate._data[Partition.OBSERVATIONS], expected)
Ejemplo n.º 12
0
    def get_predicate(self, name):
        """
        Get a specific predicate or None if one does not exist.
        Name normalization will be handled internally.

        Returns:
            A predicate matching the name, or None.
        """

        return self._predicates[Predicate.normalize_name(name)]
Ejemplo n.º 13
0
    def add_predicate(self, predicate: Predicate):
        """
        Add a predicate to the model.
        Two predicates with the same name should never be added to the same model.

        Args:
            predicate: The predicate to add.

        Returns:
            This model.
        """

        if (predicate is None):
            raise ModelError('Cannot add a None predicate.')

        name = predicate.name()
        if (name in self._predicates and predicate != self._predicates[name]):
            raise PredicateError("Within a model, predciates must have unique names. Got a duplicate: %s." % (name))

        self._predicates[predicate.name()] = predicate
        return self
Ejemplo n.º 14
0
def makeModel(model_name, addPrior=True, sim=False):
    model = Model(model_name)
    Trusts = Predicate("Trusts", size=2, closed=False)
    Knows = Predicate("Knows", size=2, closed=True)
    Prior = Predicate("Prior", size=1, closed=True)
    model.add_predicate(Trusts)
    model.add_predicate(Knows)
    model.add_predicate(Prior)

    if model_name in [
            "triad-personality", "personality-similarity", "triad-pers-sim",
            "personality"
    ]:
        Trusting = Predicate("Trusting", size=1, closed=False)
        TrustWorthy = Predicate("TrustWorthy", size=1, closed=False)
        model.add_predicate(Trusting)
        model.add_predicate(TrustWorthy)

    if model_name in [
            "similarity", "triad-similarity", "personality-similarity",
            "triad-pers-sim"
    ]:
        SameTastes = Predicate("SameTastes", size=2, closed=True)
        model.add_predicate(SameTastes)

    return model
Ejemplo n.º 15
0
    def test_add_record(self):
        predicate = Predicate('Foo', closed = True, size = 2)

        predicate.add_data_row(Partition.OBSERVATIONS, ['A', 'B'])
        predicate.add_data_row(Partition.OBSERVATIONS, ['C', 'D'], 0.5)
        predicate.add_data_row(Partition.OBSERVATIONS, [1, 2])

        expected = pandas.DataFrame([
            ['A', 'B', 1.0],
            ['C', 'D', 0.5],
            [1, 2, 1.0],
        ])

        pandas.testing.assert_frame_equal(predicate._data[Partition.OBSERVATIONS], expected)
Ejemplo n.º 16
0
    def test_init_args(self):
        failing_configs = [
            ({'raw_name': 'Foo', 'closed': False}, 'No size supplied.'),
            ({'raw_name': 'Foo', 'closed': False, 'size': -1}, 'Negative size.'),
            ({'raw_name': 'Foo', 'closed': False, 'size': 0}, 'Zero size.'),
            ({'raw_name': 'Foo', 'closed': False, 'size': 2, 'arg_types': [Predicate.ArgType.UNIQUE_INT_ID]}, 'Type size mismatch.'),
            ({'raw_name': 'Foo', 'closed': False, 'size': 1, 'arg_types': ['UniqueIntID']}, 'Non-enum arg type.'),
        ]

        for (args, reason) in failing_configs:
            try:
                predicate = Predicate(**args)
                self.fail('Failed to raise exception on: ' + reason)
            except PredicateError as ex:
                # Expected
                pass
Ejemplo n.º 17
0
    def test_add_wrong_number_of_cols(self):
        # Too few.
        predicate = Predicate('1', closed = True, size = 3)
        path = os.path.join(PSLTest.TEST_DATA_DIR, 'misc', 'binary_small.txt')
        try:
            predicate.add_data_file(Partition.OBSERVATIONS, path)
            self.fail('Failed to raise exception when too few columns.')
        except PredicateError as ex:
            # Expected
            pass

        # Too many.
        predicate = Predicate('2', closed = True, size = 1)
        path = os.path.join(PSLTest.TEST_DATA_DIR, 'misc', 'binary_small_truth.txt')
        try:
            predicate.add_data_file(Partition.OBSERVATIONS, path)
            self.fail('Failed to raise exception when too many columns.')
        except PredicateError as ex:
            # Expected
            pass
Ejemplo n.º 18
0
def add_predicates(model):
    predicate = Predicate('Bias', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Boss', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Idol', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Knows', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('KnowsWell', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Mentor', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('OlderRelative', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Votes', closed=False, size=2)
    model.add_predicate(predicate)
Ejemplo n.º 19
0
def add_predicates(model):
    predicate = Predicate('Predicts', closed=True, size=4)
    model.add_predicate(predicate)

    predicate = Predicate('Friend', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Likes', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Joins', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Has', closed=True, size=2)
    model.add_predicate(predicate)

    predicate = Predicate('Is', closed=False, size=3)
    model.add_predicate(predicate)
Ejemplo n.º 20
0
def add_predicates(model):
    predicate = Predicate('AuthorName',
                          closed=True,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_INT_ID,
                              Predicate.ArgType.UNIQUE_STRING_ID
                          ])
    model.add_predicate(predicate)

    predicate = Predicate('AuthorBlock',
                          closed=True,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_INT_ID,
                              Predicate.ArgType.UNIQUE_STRING_ID
                          ])
    model.add_predicate(predicate)

    predicate = Predicate('PaperTitle',
                          closed=True,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_INT_ID,
                              Predicate.ArgType.UNIQUE_STRING_ID
                          ])
    model.add_predicate(predicate)

    predicate = Predicate('AuthorOf',
                          closed=True,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_INT_ID,
                              Predicate.ArgType.UNIQUE_INT_ID
                          ])
    model.add_predicate(predicate)

    predicate = Predicate('SimName',
                          closed=True,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_STRING_ID,
                              Predicate.ArgType.UNIQUE_STRING_ID
                          ])
    model.add_predicate(predicate)

    predicate = Predicate('SimTitle',
                          closed=True,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_STRING_ID,
                              Predicate.ArgType.UNIQUE_STRING_ID
                          ])
    model.add_predicate(predicate)

    predicate = Predicate('SameAuthor',
                          closed=False,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_INT_ID,
                              Predicate.ArgType.UNIQUE_INT_ID
                          ])
    model.add_predicate(predicate)

    predicate = Predicate('SamePaper',
                          closed=False,
                          arg_types=[
                              Predicate.ArgType.UNIQUE_INT_ID,
                              Predicate.ArgType.UNIQUE_INT_ID
                          ])
    model.add_predicate(predicate)
Ejemplo n.º 21
0
    def read_predicates_from_file(predicate_path):
        for line in predicate_path.open("r").readlines():
            name, closed, size = line.split("/")

            yield Predicate(name, closed == "closed", int(size))
Ejemplo n.º 22
0
    def test_add_file(self):
        predicate = Predicate('1', closed = True, size = 2)
        path = os.path.join(PSLTest.TEST_DATA_DIR, 'misc', 'binary_small.txt')
        predicate.add_data_file(Partition.OBSERVATIONS, path)
        expected = pandas.DataFrame([
            ['A', 'B', 1.0],
            ['C', 'D', 1.0],
            ['1', '2', 1.0],
        ])
        pandas.testing.assert_frame_equal(predicate._data[Partition.OBSERVATIONS], expected)

        predicate = Predicate('2', closed = True, size = 2)
        path = os.path.join(PSLTest.TEST_DATA_DIR, 'misc', 'binary_small.txt')
        predicate.add_data_file(Partition.OBSERVATIONS, path, has_header = True)
        expected = pandas.DataFrame([
            ['C', 'D', 1.0],
            ['1', '2', 1.0],
        ])
        pandas.testing.assert_frame_equal(predicate._data[Partition.OBSERVATIONS], expected)

        predicate = Predicate('3', closed = True, size = 2)
        path = os.path.join(PSLTest.TEST_DATA_DIR, 'misc', 'binary_small.csv')
        predicate.add_data_file(Partition.OBSERVATIONS, path, delim = ',')
        expected = pandas.DataFrame([
            ['A', 'B', 1.0],
            ['C', 'D', 1.0],
            ['1', '2', 1.0],
        ])
        pandas.testing.assert_frame_equal(predicate._data[Partition.OBSERVATIONS], expected)

        predicate = Predicate('4', closed = True, size = 2)
        path = os.path.join(PSLTest.TEST_DATA_DIR, 'misc', 'binary_small_truth.txt')
        predicate.add_data_file(Partition.OBSERVATIONS, path)
        expected = pandas.DataFrame([
            ['A', 'B', 0.0],
            ['C', 'D', 0.5],
            ['1', '2', 1.0],
        ])
        pandas.testing.assert_frame_equal(predicate._data[Partition.OBSERVATIONS], expected)
Ejemplo n.º 23
0
def run():
    model = Model(MODEL_NAME)

    # Add Predicates

    knows_predicate = Predicate('Knows', closed=False, size=2)
    model.add_predicate(knows_predicate)

    likes_predicate = Predicate('Likes', closed=True, size=2)
    model.add_predicate(likes_predicate)

    lived_predicate = Predicate('Lived', closed=True, size=2)
    model.add_predicate(lived_predicate)

    # Add Data

    path = os.path.join(DATA_DIR, 'knows_obs.txt')
    knows_predicate.add_data_file(Partition.OBSERVATIONS, path)

    path = os.path.join(DATA_DIR, 'lived_obs.txt')
    lived_predicate.add_data_file(Partition.OBSERVATIONS, path)

    path = os.path.join(DATA_DIR, 'likes_obs.txt')
    likes_predicate.add_data_file(Partition.OBSERVATIONS, path)

    path = os.path.join(DATA_DIR, 'knows_targets.txt')
    knows_predicate.add_data_file(Partition.TARGETS, path)

    path = os.path.join(DATA_DIR, 'knows_truth.txt')
    knows_predicate.add_data_file(Partition.TRUTH, path)

    # Add Rules
    model.add_rule(
        Rule('20: Lived(P1, L) & Lived(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2'
             ))
    model.add_rule(
        Rule(
            '5: Lived(P1, L1) & Lived(P2, L2) & (P1 != P2) & (L1 != L2) -> !Knows(P1, P2) ^2'
        ))
    model.add_rule(
        Rule('10: Likes(P1, L) & Likes(P2, L) & (P1 != P2) -> Knows(P1, P2) ^2'
             ))
    model.add_rule(
        Rule(
            '5: Knows(P1, P2) & Knows(P2, P3) & (P1 != P3) -> Knows(P1, P3) ^2'
        ))
    model.add_rule(Rule('Knows(P1, P2) = Knows(P2, P1) .'))
    model.add_rule(Rule('5: !Knows(P1, P2) ^2'))

    # Run Inference
    results = model.infer(psl_config=ADDITIONAL_PSL_OPTIONS)

    return results