def test_has_profession(self): g = self.make_graph() self.assertFalse( imdb_tsv.has_profession_in_graph(g, PersonNode(8888888888), "actor") ) self.assertFalse(imdb_tsv.has_profession_in_graph(g, PersonNode(1), "producer")) self.assertTrue(imdb_tsv.has_profession_in_graph(g, PersonNode(3), "producer"))
def test_actors_only_weights(self): g = self.make_graph() weight = "actor_weight" weights.weight_only_actors(g, weight=weight) self.assertEqual(1, g.edges[(PersonNode(1), WorkNode(43044))][weight]) self.assertEqual(1, g.edges[(PersonNode(1), WorkNode(50419))][weight]) self.assertEqual(1, g.edges[(PersonNode(2), WorkNode(117057))][weight]) self.assertEqual( 0, g.edges[(PersonNode(861703), WorkNode(43044))][weight])
def test_rating_weights(self): g = self.make_graph() weight = "rating_weight" weights.weight_by_rating(g, weight=weight) self.assertAlmostEqual( 0.69, g.edges[(PersonNode(1), WorkNode(43044))][weight]) self.assertAlmostEqual( 0.70, g.edges[(PersonNode(1), WorkNode(50419))][weight]) self.assertAlmostEqual( 0.66, g.edges[(PersonNode(2), WorkNode(117057))][weight]) self.assertAlmostEqual( 0.69, g.edges[(PersonNode(861703), WorkNode(43044))][weight])
def test_order(self): g = self.make_graph() e = g.edges[(PersonNode(1), WorkNode(43044))] self.assertEqual(1, imdb_tsv.get_order(e)) e = g.edges[(PersonNode(1), WorkNode(50419))] self.assertEqual(2, imdb_tsv.get_order(e)) e = g.edges[(PersonNode(2), WorkNode(117057))] self.assertEqual(3, imdb_tsv.get_order(e)) e = g.edges[(PersonNode(861703), WorkNode(43044))] self.assertEqual(5, imdb_tsv.get_order(e))
def test_fame_by_number_of_works(self): g = data4tests.get_small_graph() people_degree = fame.fame_by_number_of_works(g) self.assertEqual(660, len(people_degree)) actual = [p for p, _ in people_degree[:4]] expected = [ PersonNode(194), PersonNode(545), PersonNode(147), PersonNode(93) ] self.assertEqual(expected, actual) actual = [d for _, d in people_degree[:4]] expected = [57, 56, 50, 46] self.assertEqual(expected, actual)
def test_get_people(self): g = data4tests.get_small_graph() people = fame.get_people(g) self.assertEqual(660, len(people)) self.assertTrue(PersonNode(1669) in people) for p in people: self.assertTrue(p.is_person)
def test_person_node(self): n0 = ProfessionalNode(42, True) n1 = PersonNode(42) self.assertTrue(n1.is_person) self.assertEqual(n0, n1) self.assertEqual(hash(n0), hash(n1))
def test_neighbor_features(self): g = data4tests.get_small_graph() ia = data4tests.MockIMBD(g) ratings = fame.neighbor_features( g, PersonNode(194), lambda m: ia.get_movie(m.id)["rating"]) self.assertAlmostEqual(4.699903862142268, ratings[WorkNode(160513)]) self.assertAlmostEqual(7.70552832501407, ratings[WorkNode(1658801)]) self.assertEqual(57, len(ratings))
def test_credit_order_weights(self): def activation(x): y = (1 - x) / 3 return math.tanh(y) + 1 g = self.make_graph() weight = "credit_weight" weights.weight_credit_order(g, weight=weight) self.assertEqual(activation(1), g.edges[(PersonNode(1), WorkNode(43044))][weight]) self.assertEqual(activation(2), g.edges[(PersonNode(1), WorkNode(50419))][weight]) self.assertEqual(activation(3), g.edges[(PersonNode(2), WorkNode(117057))][weight]) self.assertEqual( activation(5), g.edges[(PersonNode(861703), WorkNode(43044))][weight])
def test_acted_in(self): g = self.make_graph() thorpe = PersonNode(861703) astaire = PersonNode(1) bacall = PersonNode(2) three_little_words = WorkNode(43044) the_mirror_has_two_faces = WorkNode(117057) self.assertFalse(imdb_tsv.acted_in_in_graph(g, thorpe, three_little_words)) self.assertFalse( imdb_tsv.acted_in_in_graph(g, thorpe, the_mirror_has_two_faces) ) self.assertTrue(imdb_tsv.acted_in_in_graph(g, astaire, three_little_words)) self.assertFalse( imdb_tsv.acted_in_in_graph(g, astaire, the_mirror_has_two_faces) ) self.assertFalse(imdb_tsv.acted_in_in_graph(g, bacall, three_little_words)) self.assertTrue(imdb_tsv.acted_in_in_graph(g, bacall, the_mirror_has_two_faces))
def make_professional_graph(): g = nx.Graph() populate_people(g) populate_movies(g) update_ratings(g) make_edges(g) # we want the component with Fred Astaire who has ID 1 return g.subgraph(nx.node_connected_component(g, PersonNode(1))).copy()
def test_fame_by_pagerank(self): g = data4tests.get_small_graph() people_rank, _ = fame.fame_by_pagerank(g) actual = [p for p, _ in people_rank[:4]] expected = [ PersonNode(545), PersonNode(194), PersonNode(147), PersonNode(93) ] self.assertEqual(expected, actual) actual = [r for _, r in people_rank[:4]] expected = np.array([ 0.02079940230132745, 0.01956018860654516, 0.01724945845804374, 0.01593796482225136, ]) self.assertAlmostEqual(0, norm(actual - expected))
def test_normalize_votes_weights(self): def activate(x): y = (x - mu) / sigma y = (math.tanh(y) + 1) / 2 return y g = self.make_graph() weight = "vote_weight" mu, sigma = 13005.75, 8347.634017342878 weights.weight_by_normalized_votes(g, weight=weight) self.assertAlmostEqual( activate(1497), g.edges[(PersonNode(1), WorkNode(43044))][weight]) self.assertAlmostEqual( activate(24896), g.edges[(PersonNode(1), WorkNode(50419))][weight]) self.assertAlmostEqual( activate(14369), g.edges[(PersonNode(2), WorkNode(117057))][weight]) self.assertAlmostEqual( activate(1497), g.edges[(PersonNode(861703), WorkNode(43044))][weight])
def weight_by_cast_order(g, ia_s3, weight="weight"): d = dict() for node in get_works(g): movie = ia_s3.get_movie(node.id) cast = movie["cast"] x = normalized_exponential_decay(len(cast)) for i, actor in enumerate(cast): edge = (PersonNode(actor.getID()), node) d[edge] = x[i] weight_zero(g, weight=weight) for edge, w in d.items(): g.edges[edge][weight] = w
def test_weight_by_votes(self): g = data4tests.get_small_graph() ia = data4tests.MockIMBD(g) fame.weight_by_feature_order( g, nodes=fame.get_people(g), get_feature=lambda m: ia.get_movie(m.id)["votes"], ) for edge in g.edges: self.assertIn("weight", g.edges[edge]) neighbors = list(g.neighbors((PersonNode(194)))) self.assertEqual(57, len(neighbors)) neighbors.sort(key=lambda m: g.edges[(PersonNode(194), m)]["weight"], reverse=True) actual = [g.edges[(PersonNode(194), m)]["weight"] for m in neighbors] expected = fame.normalized_exponential_decay(57) self.assertAlmostEqual(0, norm(actual - expected)) # order of vote count should be same as order of weights from highest to lowest for i in range(56): self.assertTrue( ia.get_movie(neighbors[i].id)["votes"] >= ia.get_movie( neighbors[i + 1].id)["votes"])
def test_add_person(self): g = nx.Graph() imdb_tsv.add_person(g, self.names[0]) expected = { "name": "Fred Astaire", "birth": "1899", "death": "1987", "professions": {"miscellaneous", "actor", "soundtrack"}, "known": { WorkNode(53137), WorkNode(50419), WorkNode(72308), WorkNode(43044), }, } actual = g.nodes[PersonNode(1)] self.assertEqual(expected, actual)
def test_contributed_as(self): g = self.make_graph() thorpe = PersonNode(861703) three_little_words = WorkNode(43044) the_mirror_has_two_faces = WorkNode(117057) self.assertIn(three_little_words, g.nodes) self.assertIn((thorpe, three_little_words), g.edges) self.assertTrue( imdb_tsv.contributed_as_in_graph(g, thorpe, three_little_words, "director") ) self.assertFalse( imdb_tsv.contributed_as_in_graph(g, thorpe, three_little_words, "cook") ) self.assertFalse( imdb_tsv.contributed_as_in_graph( g, thorpe, the_mirror_has_two_faces, "director" ) )
def test_add_arc(self): g = nx.Graph() grapher.add_arc(g, "The Air I Breathe", "Kevin Bacon", job="actor") grapher.add_arc(g, "The Air I Breathe", "Sarah Michelle Geller", job="actress") grapher.add_arc(g, "A Powerful Noise Live", "Sarah Michelle Geller", job="self") grapher.add_arc(g, "A Powerful Noise Live", "Natalie Portman", job="self") self.assertEqual(5, g.number_of_nodes()) self.assertEqual(4, g.number_of_edges()) self.assertIn(WorkNode("The Air I Breathe"), g.nodes) self.assertIn(WorkNode("A Powerful Noise Live"), g.nodes) self.assertIn(PersonNode("Kevin Bacon"), g.nodes) self.assertIn(PersonNode("Sarah Michelle Geller"), g.nodes) self.assertIn(PersonNode("Natalie Portman"), g.nodes) self.assertEqual( {"actor"}, g.edges[(WorkNode("The Air I Breathe"), PersonNode("Kevin Bacon"))]["job"], ) self.assertEqual( {"actress"}, g.edges[ (WorkNode("The Air I Breathe"), PersonNode("Sarah Michelle Geller")) ]["job"], ) self.assertEqual( {"self"}, g.edges[ ( WorkNode("A Powerful Noise Live"), PersonNode("Sarah Michelle Geller"), ) ]["job"], ) self.assertEqual( {"self"}, g.edges[ (WorkNode("A Powerful Noise Live"), PersonNode("Natalie Portman")) ]["job"], )
def test_is_actress(self): g = self.make_graph() self.assertFalse(imdb_tsv.is_actress_in_graph(g, PersonNode(1))) self.assertTrue(imdb_tsv.is_actress_in_graph(g, PersonNode(2))) self.assertTrue(imdb_tsv.is_actress_in_graph(g, PersonNode(3)))
def fetch_works_for_person(self, person): return self.fetch_neighbors(PersonNode(person))
def test_add_contribution(self): g = self.make_graph() expected = {"ordering": 1, "contributions": {"actor": {"job": "\\N"}}} actual = g.edges[(PersonNode(1), WorkNode(43044))] self.assertEqual(expected, actual)
def test_mock_actor(self): one = data4tests.MockImdbObject.mock_actor(PersonNode(1)) self.assertEqual(1, one.getID()) self.assertEqual("1", one['name']) self.assertAlmostEqual(41.7022004702574, one['pay'])
def test_make_game_by_iteration(self): # select one third of the actors in the graph as candidates for start and end nodes candidates = [ node for node in self.g.nodes if node.is_person and (node.id % 3 == 0) ] # choose actors who have been in the same movie a, b = game_maker.make_game_by_iteration(self.g, candidates, 1, r=self.r) self.assertEqual(PersonNode(51), a) self.assertEqual(PersonNode(532290), b) self.assertEqual(2, nx.shortest_path_length(self.g, a, b)) a, b = game_maker.make_game_by_iteration(self.g, candidates, 1, r=self.r) self.assertEqual(PersonNode(1512), a) self.assertEqual(PersonNode(147), b) self.assertEqual(2, nx.shortest_path_length(self.g, a, b)) # choose actors who are two movies apart a, b = game_maker.make_game_by_iteration(self.g, candidates, 2, r=self.r) self.assertEqual(PersonNode(171513), a) self.assertEqual(PersonNode(1644), b) self.assertEqual(4, nx.shortest_path_length(self.g, a, b)) a, b = game_maker.make_game_by_iteration(self.g, candidates, 2, r=self.r) self.assertEqual(PersonNode(378), a) self.assertEqual(PersonNode(1512), b) self.assertEqual(4, nx.shortest_path_length(self.g, a, b)) a, b = game_maker.make_game_by_iteration(self.g, candidates, 2, r=self.r) self.assertEqual(PersonNode(770961), a) self.assertEqual(PersonNode(138), b) self.assertEqual(4, nx.shortest_path_length(self.g, a, b)) # choose some actors that are three movies apart a, b = game_maker.make_game_by_iteration(self.g, candidates, 3, r=self.r) self.assertEqual(PersonNode(330687), a) self.assertEqual(PersonNode(1605114), b) self.assertEqual(6, nx.shortest_path_length(self.g, a, b)) a, b = game_maker.make_game_by_iteration(self.g, candidates, 3, r=self.r) self.assertEqual(PersonNode(123), a) self.assertEqual(PersonNode(861915), b) self.assertEqual(6, nx.shortest_path_length(self.g, a, b))
def __init__(self, nconst): PersonNode.__init__(self, int(nconst[2:]))
def get_person(self, id): person_node = PersonNode(id) p = MockImdbObject.mock_actor(person_node) MockImdbObject.mock_known_for(p, person_node, self.g) return p