Ejemplo n.º 1
0
 def test_users_region_cache(self):
     """
     [recommendation.filter.Region] Test user in region cache system
     """
     for u in USERS:
         user = User.objects.get(external_id=u["external_id"])
         for reg in user.regions.all():
             assert reg.region_id in Region.get_user_regions(user.pk), \
                 "Region %s for user %s is not in cache (%s)" % (reg, user, Region.get_user_regions(user.pk))
Ejemplo n.º 2
0
 def test_users_region_cache(self):
     """
     [recommendation.filter.Region] Test user in region cache system
     """
     for u in USERS:
         user = User.objects.get(external_id=u["external_id"])
         for reg in user.regions.all():
             assert reg.region_id in Region.get_user_regions(user.pk), \
                 "Region %s for user %s is not in cache (%s)" % (reg, user, Region.get_user_regions(user.pk))
Ejemplo n.º 3
0
def load_rest():
    User.load_to_cache()

    # Load main models
    Popularity.load_to_cache()
    TensorCoFi.load_to_cache()
    if "recommendation.language" in settings.INSTALLED_APPS:
        from recommendation.language.models import Region
        Region.load_to_cache()
    if "recommendation.diversity" in settings.INSTALLED_APPS:
        from recommendation.diversity.models import ItemGenre, Genre
        Genre.load_to_cache()
        ItemGenre.load_to_cache()
Ejemplo n.º 4
0
 def __call__(self, user, early_recommendation, size=None, **kwargs):
     """
     Call the filter
     """
     user_regions = tuple([Region.get_item_list_by_region(region) for region in Region.get_user_regions(user.pk)])
     if len(user_regions) > 0:
         for item_id, score in enumerate(np.sum(user_regions, axis=0), start=1):
             if score == 0:
                 try:
                     early_recommendation[item_id-1] = -1000
                 except IndexError:
                     pass
     return early_recommendation
Ejemplo n.º 5
0
 def setup_class(cls, *args, **kwargs):
     """
     Put elements in db
     """
     for region in REGIONS:
         Region.objects.create(**region)
     for app in ITEMS:
         item = Item.objects.create(id=app["id"], name=app["name"], external_id=app["external_id"])
         for region in app["regions"]:
             ItemRegion.objects.create(region=Region.objects.get(slug=region), item=item)
     for u in USERS:
         user = User.objects.create(id=u["id"], external_id=u["external_id"])
         for i in u["items"]:
             Inventory.objects.create(user=user, item=Item.get_item_by_external_id(i))
         UserRegion.objects.create(region=Region.objects.get(slug=u["region"]), user=user)
     Item.load_to_cache()
     User.load_to_cache()
     Region.load_to_cache()
Ejemplo n.º 6
0
 def setup_class(cls, *args, **kwargs):
     """
     Put elements in db
     """
     path = resource_filename(recommendation.__name__, "/")
     fill.FillTool({"items": True, "--mozilla": True, "prod": True}).load()
     fill.FillTool({"users": True, "--mozilla": True, "<path>": path+"data/user"}).load()
     modelcrafter.main("train", "popularity")
     modelcrafter.main("train", "tensorcofi")
     # Load user and items
     Item.load_to_cache()
     User.load_to_cache()
     # Load main models
     Popularity.load_to_cache()
     TensorCoFi.load_to_cache()
     Locale.load_to_cache()
     Region.load_to_cache()
     Genre.load_to_cache()
     cls.client = Client()
Ejemplo n.º 7
0
 def setup_class(cls, *args, **kwargs):
     """
     Put elements in db
     """
     for region in REGIONS:
         Region.objects.create(**region)
     for app in ITEMS:
         item = Item.objects.create(id=app["id"],
                                    name=app["name"],
                                    external_id=app["external_id"])
         for region in app["regions"]:
             ItemRegion.objects.create(
                 region=Region.objects.get(slug=region), item=item)
     for u in USERS:
         user = User.objects.create(id=u["id"],
                                    external_id=u["external_id"])
         for i in u["items"]:
             Inventory.objects.create(user=user,
                                      item=Item.get_item_by_external_id(i))
         UserRegion.objects.create(
             region=Region.objects.get(slug=u["region"]), user=user)
     Item.load_to_cache()
     User.load_to_cache()
     Region.load_to_cache()
Ejemplo n.º 8
0
    def get_regions(region_names):
        """
        Get regions from database and create the ones that don't exist.
        :param region_names:
        :return: A dict with regions mapped to their name
        """
        regions = {region.name: region for region in Region.objects.filter(name__in=region_names)}
        if len(regions) != len(region_names):
            new_regions = {}
            for name in region_names:
                slug = region_names[name]["slug"]
                if name not in regions:
                    new_regions[name] = Region(name=name, slug=slug)

            Region.objects.bulk_create(new_regions.values())
            for region in Region.objects.filter(name__in=new_regions):
                regions[region.name] = region
        return regions
Ejemplo n.º 9
0
application = get_wsgi_application()

from django.conf import settings
from recommendation.models import Item, User, TensorCoFi, Popularity
# Load user and items
Item.load_to_cache()
User.load_to_cache()

# Load main models
Popularity.load_to_cache()

TensorCoFi.load_to_cache()


if "recommendation.language" in settings.INSTALLED_APPS:
    from recommendation.language.models import Locale, Region
    Locale.load_to_cache()
    Region.load_to_cache()


#if "recommendation.simple_logging" in recommendation.settings.INSTALLED_APPS:
#    print("Loading logs to cache...")
#    from recommendation.simple_logging.models import LogEntry
#    LogEntry.load_to_cache()
#    print("done!")

if "recommendation.diversity" in settings.INSTALLED_APPS:
    from recommendation.diversity.models import ItemGenre, Genre
    Genre.load_to_cache()
    ItemGenre.load_to_cache()
Ejemplo n.º 10
0
from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

from django.conf import settings
from recommendation.models import Item, User, TensorCoFi, Popularity
# Load user and items
Item.load_to_cache()
User.load_to_cache()

# Load main models
Popularity.load_to_cache()

TensorCoFi.load_to_cache()

if "recommendation.language" in settings.INSTALLED_APPS:
    from recommendation.language.models import Locale, Region
    Locale.load_to_cache()
    Region.load_to_cache()

#if "recommendation.simple_logging" in recommendation.settings.INSTALLED_APPS:
#    print("Loading logs to cache...")
#    from recommendation.simple_logging.models import LogEntry
#    LogEntry.load_to_cache()
#    print("done!")

if "recommendation.diversity" in settings.INSTALLED_APPS:
    from recommendation.diversity.models import ItemGenre, Genre
    Genre.load_to_cache()
    ItemGenre.load_to_cache()