def test_arguments(self, items=None, attr=None, users=None): if items is None: items = np.random.randint(1, 1000) if users is None: users = np.random.randint(1, 100) if attr is None: attr = np.random.randint(1, 100) # init with given arguments c = ContentFiltering(num_users=users, num_items=items, num_attributes=attr) test_utils.assert_correct_num_users(users, c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(items, c, c.item_attributes.shape[1]) test_utils.assert_correct_size_generic(attr, c.num_attributes, c.item_attributes.shape[0]) test_utils.assert_correct_size_generic(attr, c.num_attributes, c.user_profiles.shape[1]) test_utils.assert_not_none(c.predicted_scores) # did not set seed, show random behavior c1 = ContentFiltering(num_users=users, num_items=items, num_attributes=attr) with pytest.raises(AssertionError): test_utils.assert_equal_arrays(c.item_attributes, c1.item_attributes)
def test_partial_arguments(self, items=1, users=5): if items is None: items = 1 if users is None: users = np.random.randint(1, 100) # init with partially given arguments s = BassModel(num_users=users) test_utils.assert_correct_num_users(users, s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(users, s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(s.num_items, s, s.item_attributes.shape[1]) test_utils.assert_not_none(s.predicted_scores) s = BassModel(num_items=items) test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(items, s, s.item_attributes.shape[1]) test_utils.assert_not_none(s.predicted_scores) # did not set seed, show random behavior s1 = BassModel(num_users=users) with pytest.raises(AssertionError): test_utils.assert_equal_arrays(s.user_profiles, s1.user_profiles) s1 = BassModel(num_items=items) with pytest.raises(AssertionError): test_utils.assert_equal_arrays(s.user_profiles, s1.user_profiles)
def test_representations(self, item_repr=None, user_repr=None): if item_repr is None: items = np.random.randint(20, 1000) users = (user_repr.shape[0] if user_repr is not None else np.random.randint(20, 100)) item_repr = np.random.random(size=(users, items)) if user_repr is None or user_repr.shape[0] != user_repr.shape[1]: users = item_repr.shape[0] user_repr = np.random.randint(2, size=(users, users)) # test item representation s = SocialFiltering(item_representation=item_repr) test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(item_repr.shape[1], s, s.item_attributes.shape[1]) test_utils.assert_equal_arrays(item_repr, s.item_attributes) test_utils.assert_not_none(s.predicted_scores) # did not set seed, show random behavior s1 = SocialFiltering(item_representation=item_repr) with pytest.raises(AssertionError): test_utils.assert_equal_arrays(s.user_profiles, s1.user_profiles) # test user representation s = SocialFiltering(user_representation=user_repr) test_utils.assert_correct_num_users(user_repr.shape[0], s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(user_repr.shape[0], s, s.user_profiles.shape[1]) test_utils.assert_correct_num_users(user_repr.shape[1], s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(user_repr.shape[1], s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(s.num_items, s, s.item_attributes.shape[1]) test_utils.assert_equal_arrays(user_repr, s.user_profiles) test_utils.assert_not_none(s.predicted_scores) # test item and user representations s = SocialFiltering(user_representation=user_repr, item_representation=item_repr) test_utils.assert_correct_num_users(user_repr.shape[0], s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(user_repr.shape[0], s, s.user_profiles.shape[1]) test_utils.assert_correct_num_users(user_repr.shape[1], s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(user_repr.shape[1], s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(item_repr.shape[1], s, s.item_attributes.shape[1]) test_utils.assert_equal_arrays(user_repr, s.user_profiles) test_utils.assert_equal_arrays(item_repr, s.item_attributes) test_utils.assert_not_none(s.predicted_scores)
def test_default(self): s = BassModel() test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(s.num_items, s, s.item_attributes.shape[1]) test_utils.assert_not_none(s.predicted_scores) # did not set seed, show random behavior s1 = BassModel() with pytest.raises(AssertionError): test_utils.assert_equal_arrays(s.user_profiles, s1.user_profiles)
def test_seeding(self, seed=None, items=None, users=None): if seed is None: seed = np.random.randint(100000) s1 = BassModel(seed=seed) s2 = BassModel(seed=seed) test_utils.assert_equal_arrays(s1.item_attributes, s2.item_attributes) test_utils.assert_equal_arrays(s1.user_profiles, s2.user_profiles) s1.run(timesteps=5) s2.run(timesteps=5) # check that measurements are the same meas1 = s1.get_measurements() meas2 = s2.get_measurements() systate1 = s1.get_system_state() systate2 = s2.get_system_state() test_utils.assert_equal_measurements(meas1, meas2) test_utils.assert_equal_system_state(systate1, systate2) if items is None: items = np.random.randint(1, 1000) if users is None: users = np.random.randint(1, 100) s1 = BassModel(seed=seed, num_users=users, num_items=items) s2 = BassModel(seed=seed, num_users=users, num_items=items) test_utils.assert_equal_arrays(s1.item_attributes, s2.item_attributes) test_utils.assert_equal_arrays(s1.user_profiles, s2.user_profiles) s1.run(timesteps=5) s2.run(timesteps=5) # check that measurements are the same meas1 = s1.get_measurements() meas2 = s2.get_measurements() test_utils.assert_equal_measurements(meas1, meas2) systate1 = s1.get_system_state() systate2 = s2.get_system_state() test_utils.assert_equal_system_state(systate1, systate2)
def test_representations(self, item_repr=None, user_repr=None): if item_repr is None: items = np.random.randint(5, 1000) item_repr = np.random.random(size=(1, items)) if user_repr is None or user_repr.shape[1] != item_repr.shape[0]: users = np.random.randint(5, 100) user_repr = np.random.randint(10, size=(users, 1)) c = PopularityRecommender(item_representation=item_repr) test_utils.assert_correct_num_users(c.num_users, c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(item_repr.shape[1], c, c.item_attributes.shape[1]) test_utils.assert_equal_arrays(item_repr, c.item_attributes) test_utils.assert_not_none(c.predicted_scores) c = PopularityRecommender(user_representation=user_repr) test_utils.assert_correct_num_users(user_repr.shape[0], c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(c.num_items, c, c.item_attributes.shape[1]) test_utils.assert_equal_arrays(user_repr, c.user_profiles) test_utils.assert_not_none(c.predicted_scores) c = PopularityRecommender(user_representation=user_repr, item_representation=item_repr) test_utils.assert_correct_num_users(user_repr.shape[0], c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(item_repr.shape[1], c, c.item_attributes.shape[1]) test_utils.assert_equal_arrays(user_repr, c.user_profiles) test_utils.assert_equal_arrays(item_repr, c.item_attributes) test_utils.assert_not_none(c.predicted_scores)
def test_seeding(self, users=15, attr=15, seed=None): actual_user_repr = np.random.randint(15, size=(users, attr)) if seed is None: seed = np.random.randint(1000) users1 = Users(size=(users, attr), seed=seed) users2 = Users(size=(users, attr), seed=seed) test_utils.assert_equal_arrays(users1.actual_user_profiles, users2.actual_user_profiles) # no seeding users3 = Users(size=(users, attr)) users4 = Users(size=(users, attr)) # very low chances of this passing with pytest.raises(AssertionError): test_utils.assert_equal_arrays(users3.actual_user_profiles, users4.actual_user_profiles)
def test_arguments(self, items=10, users=5): if items is None: items = np.random.randint(10, 1000) if users is None: users = np.random.randint(10, 100) s = SocialFiltering(num_users=users, num_items=items) test_utils.assert_correct_num_users(users, s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(users, s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(items, s, s.item_attributes.shape[1]) test_utils.assert_not_none(s.predicted_scores) # did not set seed, show random behavior s1 = SocialFiltering(num_users=users, num_items=items) with pytest.raises(AssertionError): test_utils.assert_equal_arrays(s.user_profiles, s1.user_profiles)
def test_default(self): c = ContentFiltering() test_utils.assert_correct_num_users(c.num_users, c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(c.num_items, c, c.item_attributes.shape[1]) test_utils.assert_correct_size_generic(c.num_attributes, c.num_attributes, c.user_profiles.shape[1]) test_utils.assert_correct_size_generic(c.num_attributes, c.num_attributes, c.item_attributes.shape[0]) test_utils.assert_not_none(c.predicted_scores) # did not set seed, show random behavior c1 = ContentFiltering() with pytest.raises(AssertionError): test_utils.assert_equal_arrays(c.item_attributes, c1.item_attributes)
def test_additional_params(self, num_items_per_iter=None): if num_items_per_iter is None: num_items_per_iter = np.random.randint(5, 100) s = SocialFiltering(verbose=False, num_items_per_iter=num_items_per_iter) assert (num_items_per_iter == s.num_items_per_iter) # also check other params test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[0]) test_utils.assert_correct_num_users(s.num_users, s, s.user_profiles.shape[1]) test_utils.assert_correct_num_items(s.num_items, s, s.item_attributes.shape[1]) test_utils.assert_not_none(s.predicted_scores) # did not set seed, show random behavior s1 = SocialFiltering(verbose=False, num_items_per_iter=num_items_per_iter) with pytest.raises(AssertionError): test_utils.assert_equal_arrays(s.user_profiles, s1.user_profiles)
def test_content(self, items=10, attr=5, users=6, expand_items_by=2): """WARNING Before running this, make sure ContentFiltering is working properly""" # user_repr = actual_user_repr item_repr = np.random.randint(2, size=(attr, items)) actual_user_repr = np.random.randint(15, size=(users, attr)) model = ContentFiltering(user_representation=actual_user_repr, item_representation=item_repr) s = Users(actual_user_repr) s.compute_user_scores(model.train) test_utils.assert_equal_arrays( s.actual_user_scores, model.train(s.actual_user_profiles, normalize=True)) test_utils.assert_equal_arrays(s.actual_user_scores, model.predicted_scores) # user_repr != actual_user_repr user_repr = np.random.randint(15, size=(users, attr)) model = ContentFiltering(user_representation=user_repr, item_representation=item_repr) assert (model.user_profiles.shape == actual_user_repr.shape) s = Users(actual_user_repr) s.compute_user_scores(model.train) print( np.array_equal( s.actual_user_scores, model.train(s.actual_user_profiles, model.item_attributes, normalize=True))) test_utils.assert_equal_arrays( s.actual_user_scores, model.train(s.actual_user_profiles, normalize=True))
def test_representations(self, item_repr=None, user_repr=None, bad_user_repr=None): if item_repr is None: items = np.random.randint(5, 1000) attr = np.random.randint(5, 100) item_repr = np.random.random(size=(attr, items)) if user_repr is None or user_repr.shape[1] != item_repr.shape[0]: users = np.random.randint(5, 100) user_repr = np.random.randint(10, size=(users, item_repr.shape[0])) c = ContentFiltering(item_representation=item_repr) test_utils.assert_correct_num_users(c.num_users, c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(item_repr.shape[1], c, c.item_attributes.shape[1]) test_utils.assert_correct_size_generic(item_repr.shape[0], c.num_attributes, c.item_attributes.shape[0]) test_utils.assert_correct_size_generic(item_repr.shape[0], c.num_attributes, c.user_profiles.shape[1]) test_utils.assert_equal_arrays(item_repr, c.item_attributes) test_utils.assert_not_none(c.predicted_scores) c = ContentFiltering(user_representation=user_repr) test_utils.assert_correct_num_users(user_repr.shape[0], c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(c.num_items, c, c.item_attributes.shape[1]) test_utils.assert_correct_size_generic(user_repr.shape[1], c.num_attributes, c.item_attributes.shape[0]) test_utils.assert_correct_size_generic(user_repr.shape[1], c.num_attributes, c.user_profiles.shape[1]) test_utils.assert_equal_arrays(user_repr, c.user_profiles) test_utils.assert_not_none(c.predicted_scores) # did not set seed, show random behavior c1 = ContentFiltering(user_representation=user_repr) #with pytest.raises(AssertionError): # test_utils.assert_equal_arrays(c.user_profiles, c1.user_profiles) with pytest.raises(AssertionError): # FIXME this fails from time to time because # it might happen that these two are in fact equal test_utils.assert_equal_arrays(c.item_attributes, c1.item_attributes) #with pytest.raises(AssertionError): # test_utils.assert_equal_arrays(c.num_attributes, c1.num_attributes) c = ContentFiltering(user_representation=user_repr, item_representation=item_repr) test_utils.assert_correct_num_users(user_repr.shape[0], c, c.user_profiles.shape[0]) test_utils.assert_correct_num_items(item_repr.shape[1], c, c.item_attributes.shape[1]) test_utils.assert_correct_size_generic(user_repr.shape[1], c.num_attributes, c.item_attributes.shape[0]) test_utils.assert_correct_size_generic(user_repr.shape[1], c.num_attributes, c.user_profiles.shape[1]) test_utils.assert_correct_size_generic(item_repr.shape[0], c.num_attributes, c.item_attributes.shape[0]) test_utils.assert_correct_size_generic(item_repr.shape[0], c.num_attributes, c.user_profiles.shape[1]) test_utils.assert_equal_arrays(user_repr, c.user_profiles) test_utils.assert_equal_arrays(item_repr, c.item_attributes) test_utils.assert_not_none(c.predicted_scores)