def test_build_context_for_1_hop(self): starting_thing = neighbour.Thing("0", "person", "entity") samplers = [samp.Sampler(2, ordered.ordered_sample, limit=2)] context_builder = builder.ContextBuilder( samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build( mock.Mock(grakn.client.Transaction), starting_thing) expected_context = { 0: [ builder.Node((0, ), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, ), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ], 1: [builder.Node((), neighbour.Thing("0", "person", "entity"))], } self.assertEqual(expected_context, thing_context)
def mock_traversal_output(): c = builder.ThingContext(neighbour.Thing("0", "person", "entity"), [ builder.Neighbour( "employee", neighbour.TARGET_PLAYS, builder.ThingContext( neighbour.Thing("1", "employment", "relation"), [ builder.Neighbour( "employer", neighbour.NEIGHBOUR_PLAYS, builder.ThingContext( neighbour.Thing("2", "company", "entity"), [])), ])), builder.Neighbour( "@has-name-owner", neighbour.TARGET_PLAYS, builder.ThingContext(neighbour.Thing( "3", "@has-name", "relation"), [ builder.Neighbour( "@has-name-value", neighbour.NEIGHBOUR_PLAYS, builder.ThingContext( neighbour.Thing("4", "name", "attribute", data_type='string', value="Employee Name"), [])), ])) ]) return c
def test_build_context_for_0_hop(self): starting_thing = neighbour.Thing("0", "person", "entity") samplers = [] context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build(mock.Mock(grakn.client.Transaction), starting_thing) expected_context = { 0: [builder.Node((), neighbour.Thing("0", "person", "entity"))], } self.assertEqual(expected_context, thing_context)
def test_build_context_batch(self): batch = [neighbour.Thing("0", "person", "entity"), neighbour.Thing("0", "person", "entity")] samplers = [] context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build_batch(mock.Mock(grakn.client.Session), batch) expected_context_batch = [{ 0: [builder.Node((), neighbour.Thing("0", "person", "entity"))], }, { 0: [builder.Node((), neighbour.Thing("0", "person", "entity"))], } ] self.assertEqual(expected_context_batch, thing_context)
def test_input_output(self): neighbour_sample_sizes = (2, 3) samplers = [lambda x: x for sample_size in neighbour_sample_sizes] starting_thing = neighbour.Thing("0", "person", "entity") context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build(self._tx, starting_thing) self.assertEqual(thing_context, mocks.mock_traversal_output())
def _build_data(role_label, role_direction, neighbour_id, neighbour_type, neighbour_metatype, data_type=None, value=None): return neighbour.Connection( role_label, role_direction, neighbour.Thing(neighbour_id, neighbour_type, neighbour_metatype, data_type=data_type, value=value))
def setUp(self): self._tx = self.session.transaction(grakn.TxType.WRITE) neighbour_sample_sizes = (2, 3) samplers = [lambda x: x for sample_size in neighbour_sample_sizes] starting_thing = neighbour.Thing("0", "person", "entity") things = [starting_thing] context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) self._neighbourhood_depths = [context_builder.build(self._tx, thing) for thing in things] self._neighbour_roles = builder.convert_thing_contexts_to_neighbours(self._neighbourhood_depths) self._flattened = flatten_tree(self._neighbour_roles)
def test_input_output_integration(self): """ Runs using real samplers :return: """ sampling_method = ordered.ordered_sample samplers = [samp.Sampler(2, sampling_method, limit=2), samp.Sampler(3, sampling_method, limit=1)] starting_thing = neighbour.Thing("0", "person", "entity") context_builder = builder.ContextBuilder(samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build(self._tx, starting_thing) self.assertEqual(thing_context, mocks.mock_traversal_output())
def test_date(self): neighbour_roles = [ builder.Neighbour( None, None, builder.ThingContext( neighbour.Thing("1", "start-date", "attribute", data_type='date', value=datetime.datetime(day=1, month=1, year=2018)), mock.gen([]))), ] self._array_builder = array.ArrayConverter((2, 3)) self._context_arrays = self._array_builder.convert_to_array( neighbour_roles)
def _build_data(role_label, role_direction, neighbour_id, neighbour_type, neighbour_metatype, data_type=None, value=None): return { 'role_label': role_label, 'role_direction': role_direction, 'neighbour_thing': neighbour.Thing(neighbour_id, neighbour_type, neighbour_metatype, data_type=data_type, value=value) }
def mock_context(): return { 2: [builder.Node((), neighbour.Thing("0", "person", "entity"))], 1: [ builder.Node((0, ), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, ), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ], 0: [ builder.Node((0, 0), neighbour.Thing("0", "person", "entity"), "has", neighbour.TARGET_PLAYS), # Note that (0, 1) is reversed compared to the natural expectation builder.Node((0, 1), neighbour.Thing("3", "company", "entity"), "employer", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 1), neighbour.Thing("0", "person", "entity"), "employee", neighbour.NEIGHBOUR_PLAYS), ] }
def test_outcome_is_as_expected(self): context_batch = [{ 1: [builder.Node((), neighbour.Thing("0", "person", "entity"))], 0: [ builder.Node((0, ), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, ), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ] }] max_hops_shape = (1, 2, 1) filled_arrays = array.convert_context_batch_to_arrays( context_batch, max_hops_shape, role_type=(np.dtype('U50'), ''), role_direction=(np.int, -1), neighbour_type=(np.dtype('U50'), ''), neighbour_data_type=(np.dtype('U10'), ''), neighbour_value_long=(np.int, 0), neighbour_value_double=(np.float, 0.0), neighbour_value_boolean=(np.int, -1), neighbour_value_date=('datetime64[s]', ''), neighbour_value_string=(np.dtype('U50'), '')) expected_filled_arrays = [{ 'role_type': np.array([[['has'], ['employee']]], dtype=np.dtype('U50')), 'role_direction': np.array([[[neighbour.NEIGHBOUR_PLAYS], [neighbour.TARGET_PLAYS]]], dtype=np.int), 'neighbour_type': np.array([[['name'], ['employment']]], dtype=np.dtype('U50')), 'neighbour_data_type': np.array([[['string'], ['']]], dtype=np.dtype('U10')), 'neighbour_value_long': np.array([[[0], [0]]], dtype=np.int), 'neighbour_value_double': np.array([[[0.0], [0.0]]], dtype=np.int), 'neighbour_value_boolean': np.array([[[-1], [-1]]], dtype=np.int), 'neighbour_value_date': np.array([[[''], ['']]], dtype='datetime64[s]'), 'neighbour_value_string': np.array([[['Sundar Pichai'], ['']]], dtype=np.dtype('U50')) }, { 'neighbour_type': np.array([['person']], dtype=np.dtype('U50')), 'neighbour_data_type': np.array([['']], dtype=np.dtype('U10')), 'neighbour_value_long': np.array([[0]], dtype=np.int), 'neighbour_value_double': np.array([[0.0]], dtype=np.int), 'neighbour_value_boolean': np.array([[-1]], dtype=np.int), 'neighbour_value_date': np.array([['']], dtype='datetime64[s]'), 'neighbour_value_string': np.array([['']], dtype=np.dtype('U50')) }] np.testing.assert_equal(filled_arrays, expected_filled_arrays)
def test_build_context_for_3_hop(self): starting_thing = neighbour.Thing("0", "person", "entity") samplers = [ samp.Sampler(2, ordered.ordered_sample, limit=2), samp.Sampler(2, ordered.ordered_sample, limit=2), samp.Sampler(3, ordered.ordered_sample, limit=3) ] context_builder = builder.ContextBuilder( samplers, neighbour_finder=mocks.MockNeighbourFinder()) thing_context = context_builder.build( mock.Mock(grakn.client.Transaction), starting_thing) expected_context = { 3: [builder.Node((), neighbour.Thing("0", "person", "entity"))], 2: [ builder.Node((0, ), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, ), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ], 1: [ builder.Node((0, 0), neighbour.Thing("0", "person", "entity"), "has", neighbour.TARGET_PLAYS), # Note that (0, 1) is reversed compared to the natural expectation builder.Node((0, 1), neighbour.Thing("3", "company", "entity"), "employer", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 1), neighbour.Thing("0", "person", "entity"), "employee", neighbour.NEIGHBOUR_PLAYS), ], 0: [ builder.Node((0, 0, 0), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 0, 0), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), builder.Node((0, 0, 1), neighbour.Thing("4", "name", "attribute", data_type='string', value='Google'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 0, 1), neighbour.Thing("4", "name", "attribute", data_type='string', value='Google'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((2, 0, 1), neighbour.Thing("4", "name", "attribute", data_type='string', value='Google'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((0, 1, 1), neighbour.Thing("1", "name", "attribute", data_type='string', value='Sundar Pichai'), "has", neighbour.NEIGHBOUR_PLAYS), builder.Node((1, 1, 1), neighbour.Thing("2", "employment", "relation"), "employee", neighbour.TARGET_PLAYS), ] } self.assertEqual(expected_context, thing_context)