def test_recommender_extra_data(test_ctx): # Test that the recommender uses locale data from the "extra" # section if available. def validate_recommendations(data, expected_locale): # Make sure the structure of the recommendations is correct and that we # recommended the the right addon. data = sorted(data, key=lambda x: x[1], reverse=True) assert isinstance(data, list) assert len(data) == len(FAKE_LOCALE_DATA[expected_locale]) # Make sure that the reported addons are the one from the fake data. for (addon_id, weight), (expected_id, expected_weight) in zip( data, FAKE_LOCALE_DATA[expected_locale]): assert addon_id == expected_id assert weight == expected_weight ctx = install_mock_data(test_ctx) r = LocaleRecommender(ctx) recommendations = r.recommend({}, 10, extra_data={"locale": "en"}) validate_recommendations(recommendations, "en") # Make sure that we favour client data over the extra data. recommendations = r.recommend({"locale": "en"}, 10, extra_data={"locale": "te-ST"}) validate_recommendations(recommendations, "en")
def test_can_recommend_no_model(mock_s3_json_downloader): r = LocaleRecommender() # We should never be able to recommend if something went # wrong with the model. assert not r.can_recommend({}) assert not r.can_recommend({"locale": []}) assert not r.can_recommend({"locale": "it"})
def __init__(self, TOP_ADDONS_BY_LOCALE_FILE_PATH): LocaleRecommender.__init__(self) with open(TOP_ADDONS_BY_LOCALE_FILE_PATH) as data_file: top_addons_by_locale = json.load(data_file) self.top_addons_by_locale = defaultdict(lambda: defaultdict(int), top_addons_by_locale)
def test_can_recommend_no_model(test_ctx): ctx = install_mock_data(test_ctx) r = LocaleRecommender(ctx) # We should never be able to recommend if something went # wrong with the model. assert not r.can_recommend({}) assert not r.can_recommend({"locale": []}) assert not r.can_recommend({"locale": "it"})
def test_can_recommend(mock_s3_json_downloader): r = LocaleRecommender() # Test that we can't recommend if we have not enough client info. assert not r.can_recommend({}) assert not r.can_recommend({"locale": []}) # Check that we can recommend if the user has at least an addon. assert r.can_recommend({"locale": "en"})
def test_can_recommend_no_model(): ctx = create_test_ctx() r = LocaleRecommender(ctx) # We should never be able to recommend if something went # wrong with the model. assert not r.can_recommend({}) assert not r.can_recommend({"locale": []}) assert not r.can_recommend({"locale": "it"})
def test_can_recommend(test_ctx): ctx = install_mock_data(test_ctx) r = LocaleRecommender(ctx) # Test that we can't recommend if we have not enough client info. assert not r.can_recommend({}) assert not r.can_recommend({"locale": []}) # Check that we can recommend if the user has at least an addon. assert r.can_recommend({"locale": "en"})
def test_recommendations(mock_s3_json_downloader): # Test that the locale recommender returns the correct # locale dependent addons. r = LocaleRecommender() recommendations = r.recommend({"locale": "en"}, 10) # Make sure the structure of the recommendations is correct and that we # recommended the the right addon. assert isinstance(recommendations, list) assert len(recommendations) == len(FAKE_LOCALE_DATA["en"]) # Make sure that the reported addons are the one from the fake data. for addon_id in recommendations: assert addon_id in FAKE_LOCALE_DATA["en"]
def test_recommender_str(): """Tests that the string representation of the recommender is correct """ # TODO: this test is brittle and should be removed once it is safe # to do so ctx = create_test_ctx() r = LocaleRecommender(ctx) assert str(r) == "LocaleRecommender"
def test_recommendations(test_ctx): """Test that the locale recommender returns the correct locale dependent addons. The JSON output for this recommender should be a list of 2-tuples of (GUID, weight). """ ctx = install_mock_data(test_ctx) r = LocaleRecommender(ctx) recommendations = r.recommend({"locale": "en"}, 10) # Make sure the structure of the recommendations is correct and that we # recommended the the right addon. assert isinstance(recommendations, list) assert len(recommendations) == len(FAKE_LOCALE_DATA["en"]) # Make sure that the reported addons are the one from the fake data. for (addon_id, weight) in recommendations: assert 1 == weight assert addon_id in FAKE_LOCALE_DATA["en"]
def load_recommenders(): from taar.recommenders import LocaleRecommender from taar.recommenders import SimilarityRecommender from taar.recommenders import CollaborativeRecommender from taar.context import package_context ctx = package_context() lr = LocaleRecommender(ctx) sr = SimilarityRecommender(ctx) cr = CollaborativeRecommender(ctx) return {LOCALE: lr, COLLABORATIVE: cr, SIMILARITY: sr}
def load_recommenders(ctx): from taar.recommenders import LocaleRecommender from taar.recommenders import SimilarityRecommender from taar.recommenders import CollaborativeRecommender ctx = default_context() reload_configuration() lr = LocaleRecommender(ctx) sr = SimilarityRecommender(ctx) cr = CollaborativeRecommender(ctx) return {LOCALE: lr, COLLABORATIVE: cr, SIMILARITY: sr}
def test_recommender_extra_data(): # Test that the recommender uses locale data from the "extra" # section if available. def validate_recommendations(data, expected_locale): # Make sure the structure of the recommendations is correct and that we # recommended the the right addon. assert isinstance(data, list) assert len(data) == len(FAKE_LOCALE_DATA[expected_locale]) # Make sure that the reported addons are the one from the fake data. for (addon_id, weight) in data: assert addon_id in FAKE_LOCALE_DATA[expected_locale] assert 1 == weight ctx = create_test_ctx() r = LocaleRecommender(ctx) recommendations = r.recommend({}, 10, extra_data={"locale": "en"}) validate_recommendations(recommendations, "en") # Make sure that we favour client data over the extra data. recommendations = r.recommend({"locale": "en"}, 10, extra_data={"locale": "te-ST"}) validate_recommendations(recommendations, "en")
def default_context(): ctx = _default_context() from taar.recommenders import CollaborativeRecommender from taar.recommenders import SimilarityRecommender from taar.recommenders import LocaleRecommender # Note that the EnsembleRecommender is *not* in this map as it # needs to ensure that the recommender_map key is installed in the # context ctx['recommender_factory_map'] = { 'collaborative': lambda: CollaborativeRecommender(ctx.child()), 'similarity': lambda: SimilarityRecommender(ctx.child()), 'locale': lambda: LocaleRecommender(ctx.child()) } return ctx
def default_context(): ctx = Context() from taar.recommenders import LegacyRecommender from taar.recommenders import CollaborativeRecommender from taar.recommenders import SimilarityRecommender from taar.recommenders import LocaleRecommender from taar.cache import Clock from taar.cache import JSONCache # Note that the EnsembleRecommender is *not* in this map as it # needs to ensure that the recommender_map key is installed in the # context ctx['recommender_factory_map'] = { 'legacy': lambda: LegacyRecommender(ctx.child()), 'collaborative': lambda: CollaborativeRecommender(ctx.child()), 'similarity': lambda: SimilarityRecommender(ctx.child()), 'locale': lambda: LocaleRecommender(ctx.child()) } ctx['utils'] = utils ctx['clock'] = Clock() ctx['cache'] = JSONCache(ctx) return ctx
def app_context(): """ Prepare context for TAAR web servie """ from taar.settings import AppSettings, DefaultCacheSettings, RedisCacheSettings from taar.recommenders.cache import TAARCache from taar.recommenders.redis_cache import TAARCacheRedis from taar.logs.moz_logging import Logging ctx = Context() logger = Logging(ctx) logger.set_log_level(AppSettings.PYTHON_LOG_LEVEL) ctx[IMozLogging] = logger if AppSettings.DISABLE_REDIS: ctx['cache_settings'] = DefaultCacheSettings ctx[ITAARCache] = TAARCache.get_instance(ctx) else: ctx['cache_settings'] = RedisCacheSettings ctx[ITAARCache] = TAARCacheRedis.get_instance(ctx) from taar.recommenders import CollaborativeRecommender from taar.recommenders import SimilarityRecommender from taar.recommenders import LocaleRecommender # Note that the EnsembleRecommender is *not* in this map as it # needs to ensure that the recommender_map key is installed in the # context ctx["recommender_factory_map"] = { "collaborative": lambda: CollaborativeRecommender(ctx.child()), "similarity": lambda: SimilarityRecommender(ctx.child()), "locale": lambda: LocaleRecommender(ctx.child()), } return ctx
num_mask = get_num_masked(addons) masked, unmasked = random_partition(addons, num_mask) client['installed_addons'] = unmasked client['masked_addons'] = masked return client training_masked = map(mask_addons, training) recommenders = { "collaborative": CollaborativeRecommender(), "similarity": SimilarityRecommender(), "locale": LocaleRecommender("./top_addons_by_locale.json"), "legacy": LegacyRecommender() } def compute_features(client_data): recommendations = [] matrix = [] for _, recommender in recommenders.items(): recommendations.append(recommender.get_weighted_recommendations(client_data)) for addon in whitelist: matrix.append([features[addon] for features in recommendations]) return client_data, np.array(matrix)
def test_recommender_str(mock_s3_json_downloader): # Tests that the string representation of the recommender is correct r = LocaleRecommender() assert str(r) == "LocaleRecommender"