src.fairreckitlib.model.model_factory

This module contains functionality to create a model factory.

Constants:

KEY_MODELS: key that is used to identify models.

Functions:

create_algorithm_pipeline_factory: wrap algorithm factory with pipeline creation.
create_model_factory: create factory with prediction/recommendation factories.
create_prediction_model_factory: create factory with predictor API factories.
create_recommendation_model_factory: create factory with recommender API factories.

Deprecated:

from .algorithms.elliot import elliot_factory from .pipeline.recommendation_pipeline_elliot import RecommendationPipelineElliot

def create_recommendation_model_factory() -> GroupFactory: ... # elliot recommenders model_factory.add_factory(create_algorithm_pipeline_factory( elliot_factory.create_recommender_factory(), RecommendationPipelineElliot )) ...

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

  1"""This module contains functionality to create a model factory.
  2
  3Constants:
  4
  5    KEY_MODELS: key that is used to identify models.
  6
  7Functions:
  8
  9    create_algorithm_pipeline_factory: wrap algorithm factory with pipeline creation.
 10    create_model_factory: create factory with prediction/recommendation factories.
 11    create_prediction_model_factory: create factory with predictor API factories.
 12    create_recommendation_model_factory: create factory with recommender API factories.
 13
 14Deprecated:
 15
 16from .algorithms.elliot import elliot_factory
 17from .pipeline.recommendation_pipeline_elliot import RecommendationPipelineElliot
 18
 19def create_recommendation_model_factory() -> GroupFactory:
 20    ...
 21    # elliot recommenders
 22    model_factory.add_factory(create_algorithm_pipeline_factory(
 23        elliot_factory.create_recommender_factory(),
 24        RecommendationPipelineElliot
 25    ))
 26    ...
 27
 28This program has been developed by students from the bachelor Computer Science at
 29Utrecht University within the Software Project course.
 30© Copyright Utrecht University (Department of Information and Computing Sciences)
 31"""
 32
 33from typing import Callable
 34
 35from ..core.config.config_factories import Factory, GroupFactory
 36from ..core.core_constants import TYPE_PREDICTION, TYPE_RECOMMENDATION
 37from ..core.events.event_dispatcher import EventDispatcher
 38from ..data.data_transition import DataTransition
 39from .algorithms.implicit import implicit_factory
 40from .algorithms.lenskit import lenskit_factory
 41from .algorithms.surprise import surprise_factory
 42from .pipeline.model_pipeline import ModelPipeline
 43from .pipeline.model_pipeline_surprise import \
 44    PredictionPipelineSurprise, RecommendationPipelineSurprise
 45from .pipeline.prediction_pipeline import PredictionPipeline
 46from .pipeline.recommendation_pipeline import RecommendationPipeline, RecommendationPipelineCSR
 47
 48KEY_MODELS = 'models'
 49
 50
 51def create_algorithm_pipeline_factory(
 52        algo_factory: Factory,
 53        create_pipeline: Callable[
 54            [Factory, DataTransition, EventDispatcher], ModelPipeline
 55        ]) -> Factory:
 56    """Create an algorithm pipeline factory.
 57
 58    Args:
 59        algo_factory: the factory with available algorithms.
 60        create_pipeline: the pipeline creation function associated with the factory.
 61
 62    Returns
 63        the algorithm pipeline factory.
 64    """
 65    algo_factory.create_pipeline = create_pipeline
 66    return algo_factory
 67
 68
 69def create_model_factory() -> GroupFactory:
 70    """Create a model factory with all predictor and recommender algorithms.
 71
 72    Returns:
 73        the group factory with all predictors and recommenders.
 74    """
 75    model_factory = GroupFactory(KEY_MODELS)
 76    model_factory.add_factory(create_prediction_model_factory())
 77    model_factory.add_factory(create_recommendation_model_factory())
 78    return model_factory
 79
 80
 81def create_prediction_model_factory() -> GroupFactory:
 82    """Create a model factory with all predictor algorithms.
 83
 84    Consists of algorithms from two APIs:
 85        1) LensKit predictor algorithms.
 86        2) Surprise predictor algorithms.
 87
 88    Returns:
 89        the group factory with all predictors.
 90    """
 91    model_factory = GroupFactory(TYPE_PREDICTION)
 92
 93    # lenskit predictors
 94    model_factory.add_factory(create_algorithm_pipeline_factory(
 95        lenskit_factory.create_predictor_factory(),
 96        PredictionPipeline
 97    ))
 98    # surprise predictors
 99    model_factory.add_factory(create_algorithm_pipeline_factory(
100        surprise_factory.create_predictor_factory(),
101        PredictionPipelineSurprise
102    ))
103
104    return model_factory
105
106
107def create_recommendation_model_factory() -> GroupFactory:
108    """Create a model factory with all recommender algorithms.
109
110    Consists of algorithms from four APIs:
111        1) LensKit recommender algorithms.
112        2) Implicit recommender algorithms.
113        3) Surprise recommender algorithms.
114
115    Returns:
116        the group factory with all recommenders.
117    """
118    model_factory = GroupFactory(TYPE_RECOMMENDATION)
119
120    # lenskit recommenders
121    model_factory.add_factory(create_algorithm_pipeline_factory(
122        lenskit_factory.create_recommender_factory(),
123        RecommendationPipeline
124    ))
125    # implicit recommenders
126    model_factory.add_factory(create_algorithm_pipeline_factory(
127        implicit_factory.create_recommender_factory(),
128        RecommendationPipelineCSR
129    ))
130    # surprise recommenders
131    model_factory.add_factory(create_algorithm_pipeline_factory(
132        surprise_factory.create_recommender_factory(),
133        RecommendationPipelineSurprise
134    ))
135
136    return model_factory
def create_algorithm_pipeline_factory( algo_factory: src.fairreckitlib.core.config.config_factories.Factory, create_pipeline: Callable[[src.fairreckitlib.core.config.config_factories.Factory, src.fairreckitlib.data.data_transition.DataTransition, src.fairreckitlib.core.events.event_dispatcher.EventDispatcher], src.fairreckitlib.model.pipeline.model_pipeline.ModelPipeline]) -> src.fairreckitlib.core.config.config_factories.Factory:
52def create_algorithm_pipeline_factory(
53        algo_factory: Factory,
54        create_pipeline: Callable[
55            [Factory, DataTransition, EventDispatcher], ModelPipeline
56        ]) -> Factory:
57    """Create an algorithm pipeline factory.
58
59    Args:
60        algo_factory: the factory with available algorithms.
61        create_pipeline: the pipeline creation function associated with the factory.
62
63    Returns
64        the algorithm pipeline factory.
65    """
66    algo_factory.create_pipeline = create_pipeline
67    return algo_factory

Create an algorithm pipeline factory.

Args: algo_factory: the factory with available algorithms. create_pipeline: the pipeline creation function associated with the factory.

Returns the algorithm pipeline factory.

def create_model_factory() -> src.fairreckitlib.core.config.config_factories.GroupFactory:
70def create_model_factory() -> GroupFactory:
71    """Create a model factory with all predictor and recommender algorithms.
72
73    Returns:
74        the group factory with all predictors and recommenders.
75    """
76    model_factory = GroupFactory(KEY_MODELS)
77    model_factory.add_factory(create_prediction_model_factory())
78    model_factory.add_factory(create_recommendation_model_factory())
79    return model_factory

Create a model factory with all predictor and recommender algorithms.

Returns: the group factory with all predictors and recommenders.

def create_prediction_model_factory() -> src.fairreckitlib.core.config.config_factories.GroupFactory:
 82def create_prediction_model_factory() -> GroupFactory:
 83    """Create a model factory with all predictor algorithms.
 84
 85    Consists of algorithms from two APIs:
 86        1) LensKit predictor algorithms.
 87        2) Surprise predictor algorithms.
 88
 89    Returns:
 90        the group factory with all predictors.
 91    """
 92    model_factory = GroupFactory(TYPE_PREDICTION)
 93
 94    # lenskit predictors
 95    model_factory.add_factory(create_algorithm_pipeline_factory(
 96        lenskit_factory.create_predictor_factory(),
 97        PredictionPipeline
 98    ))
 99    # surprise predictors
100    model_factory.add_factory(create_algorithm_pipeline_factory(
101        surprise_factory.create_predictor_factory(),
102        PredictionPipelineSurprise
103    ))
104
105    return model_factory

Create a model factory with all predictor algorithms.

Consists of algorithms from two APIs: 1) LensKit predictor algorithms. 2) Surprise predictor algorithms.

Returns: the group factory with all predictors.

def create_recommendation_model_factory() -> src.fairreckitlib.core.config.config_factories.GroupFactory:
108def create_recommendation_model_factory() -> GroupFactory:
109    """Create a model factory with all recommender algorithms.
110
111    Consists of algorithms from four APIs:
112        1) LensKit recommender algorithms.
113        2) Implicit recommender algorithms.
114        3) Surprise recommender algorithms.
115
116    Returns:
117        the group factory with all recommenders.
118    """
119    model_factory = GroupFactory(TYPE_RECOMMENDATION)
120
121    # lenskit recommenders
122    model_factory.add_factory(create_algorithm_pipeline_factory(
123        lenskit_factory.create_recommender_factory(),
124        RecommendationPipeline
125    ))
126    # implicit recommenders
127    model_factory.add_factory(create_algorithm_pipeline_factory(
128        implicit_factory.create_recommender_factory(),
129        RecommendationPipelineCSR
130    ))
131    # surprise recommenders
132    model_factory.add_factory(create_algorithm_pipeline_factory(
133        surprise_factory.create_recommender_factory(),
134        RecommendationPipelineSurprise
135    ))
136
137    return model_factory

Create a model factory with all recommender algorithms.

Consists of algorithms from four APIs: 1) LensKit recommender algorithms. 2) Implicit recommender algorithms. 3) Surprise recommender algorithms.

Returns: the group factory with all recommenders.