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_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_movie_node(self): n0 = ProfessionalNode(42, False) n1 = WorkNode(42) self.assertFalse(n1.is_person) self.assertEqual(n0, n1) self.assertEqual(hash(n0), hash(n1))
def test_get_works(self): g = data4tests.get_small_graph() works = fame.get_works(g) self.assertEqual(300, len(works)) self.assertTrue(WorkNode(119567) in works) for w in works: self.assertFalse(w.is_person)
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 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_weight_by_cast_order(self): g = data4tests.get_small_graph() ia = data4tests.MockIMBD(g) fame.weight_by_cast_order(g, ia) for edge in g.edges: self.assertIn("weight", g.edges[edge]) neighbors = list(g.neighbors((WorkNode(206634)))) self.assertEqual(4, len(neighbors)) neighbors.sort(key=lambda p: g.edges[(WorkNode(206634), p)]["weight"], reverse=True) actual = [g.edges[(m, WorkNode(206634))]["weight"] for m in neighbors] expected = fame.normalized_exponential_decay(4) self.assertAlmostEqual(0, norm(actual - expected)) # order of cast should be same as order of weights from highest to lowest cast = ia.get_movie(206634)["cast"] for i, actor in enumerate(cast): self.assertEqual(actor.getID(), neighbors[i].id)
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_works_by_pagerank(self): g = data4tests.get_small_graph() works_rank, _ = fame.works_by_pagerank(g) actual = [w for w, _ in works_rank[:4]] expected = [ WorkNode(3154822), WorkNode(118901), WorkNode(1233192), WorkNode(98638), ] self.assertEqual(expected, actual) actual = [r for _, r in works_rank[:4]] expected = np.array([ 0.0033347298102977384, 0.0019010827959628127, 0.0019010827959628127, 0.0019010827959628127, ]) 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 test_add_work(self): g = nx.Graph() imdb_tsv.add_work(g, self.basics[0]) expected = { "title": "Three Little Words", "adult": 0, "start": "1950", "end": "\\N", "runtime": "102", "genres": {"Biography", "Musical", "Comedy"}, } actual = g.nodes[WorkNode(43044)] self.assertEqual(expected, actual)
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 fetch_contributors(self, work): return self.fetch_neighbors(WorkNode(work))
def test_rating(self): g = self.make_graph() three_little_words = g.nodes[WorkNode(43044)] the_mirror_has_two_faces = g.nodes[WorkNode(117057)] self.assertEqual(6.9, imdb_tsv.get_rating(three_little_words)) self.assertEqual(6.6, imdb_tsv.get_rating(the_mirror_has_two_faces))
def test_mock_movie(self): one = data4tests.MockImdbObject.mock_movie(WorkNode(1)) self.assertEqual(1, one.getID()) self.assertEqual('1', one['title']) self.assertEqual(38, one['votes']) self.assertAlmostEqual(9.971848109388686, one['rating'])
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 __init__(self, tconst): WorkNode.__init__(self, int(tconst[2:]))
def get_movie(self, id): movie_node = WorkNode(id) m = MockImdbObject.mock_movie(movie_node) MockImdbObject.mock_cast(m, movie_node, self.g) return m