src.fairreckitlib.model.pipeline.model_event

This module contains all event ids, event args and a print switch for the model pipeline.

Constants:

ON_BEGIN_LOAD_TEST_SET: id of the event that is used when a test set is being loaded.
ON_BEGIN_LOAD_TRAIN_SET: id of the event that is used when a train set is being loaded.
ON_BEGIN_MODEL_PIPELINE: id of the event that is used when the model pipeline starts.
ON_BEGIN_RECONSTRUCT_RATINGS: id of the event that is used when reconstructing ratings starts.
ON_BEGIN_TEST_MODEL: id of the event that is used when testing a model started.
ON_BEGIN_TRAIN_MODEL: id of the event that is used when training a model started.
ON_BEGIN_MODEL: id of the event that is used when a model computation started.
ON_END_LOAD_TEST_SET: id of the event that is used when a test set has been loaded.
ON_END_LOAD_TRAIN_SET: id of the event that is used when a train set has been loaded.
ON_END_MODEL_PIPELINE: id of the event that is used when the model pipeline ends.
ON_END_RECONSTRUCT_RATINGS: id of the event that is used when reconstructing ratings finishes.
ON_END_TEST_MODEL: id of the event that is used when testing a model finishes.
ON_END_TRAIN_MODEL: id of the event that is used when training a model finishes.
ON_END_MODEL: id of the event that is used when a model computation finishes.

Classes:

ModelPipelineEventArgs: event args related to the model pipeline.
ModelEventArgs: event args related to a model.

Functions:

get_model_events: list of model pipeline event IDs.
get_model_event_print_switch: switch to print model pipeline event arguments by ID.

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 all event ids, event args and a print switch for the model pipeline.
  2
  3Constants:
  4
  5    ON_BEGIN_LOAD_TEST_SET: id of the event that is used when a test set is being loaded.
  6    ON_BEGIN_LOAD_TRAIN_SET: id of the event that is used when a train set is being loaded.
  7    ON_BEGIN_MODEL_PIPELINE: id of the event that is used when the model pipeline starts.
  8    ON_BEGIN_RECONSTRUCT_RATINGS: id of the event that is used when reconstructing ratings starts.
  9    ON_BEGIN_TEST_MODEL: id of the event that is used when testing a model started.
 10    ON_BEGIN_TRAIN_MODEL: id of the event that is used when training a model started.
 11    ON_BEGIN_MODEL: id of the event that is used when a model computation started.
 12    ON_END_LOAD_TEST_SET: id of the event that is used when a test set has been loaded.
 13    ON_END_LOAD_TRAIN_SET: id of the event that is used when a train set has been loaded.
 14    ON_END_MODEL_PIPELINE: id of the event that is used when the model pipeline ends.
 15    ON_END_RECONSTRUCT_RATINGS: id of the event that is used when reconstructing ratings finishes.
 16    ON_END_TEST_MODEL: id of the event that is used when testing a model finishes.
 17    ON_END_TRAIN_MODEL: id of the event that is used when training a model finishes.
 18    ON_END_MODEL: id of the event that is used when a model computation finishes.
 19
 20Classes:
 21
 22    ModelPipelineEventArgs: event args related to the model pipeline.
 23    ModelEventArgs: event args related to a model.
 24
 25Functions:
 26
 27    get_model_events: list of model pipeline event IDs.
 28    get_model_event_print_switch: switch to print model pipeline event arguments by ID.
 29
 30This program has been developed by students from the bachelor Computer Science at
 31Utrecht University within the Software Project course.
 32© Copyright Utrecht University (Department of Information and Computing Sciences)
 33"""
 34
 35from dataclasses import dataclass
 36from typing import Any, Callable, Dict, List
 37
 38from ...core.events.event_dispatcher import EventArgs
 39from ...core.io.event_io import print_load_df_event_args
 40from .model_config import ModelConfig
 41
 42ON_BEGIN_LOAD_TEST_SET = 'ModelPipeline.on_begin_load_test_set'
 43ON_BEGIN_LOAD_TRAIN_SET = 'ModelPipeline.on_begin_load_train_set'
 44ON_BEGIN_MODEL_PIPELINE = 'ModelPipeline.on_begin'
 45ON_BEGIN_RECONSTRUCT_RATINGS = 'ModelPipeline.on_begin_reconstruct_ratings'
 46ON_BEGIN_TEST_MODEL = 'ModelPipeline.on_begin_model_test'
 47ON_BEGIN_TRAIN_MODEL = 'ModelPipeline.on_begin_model_train'
 48ON_BEGIN_MODEL = 'ModelPipeline.on_begin_model'
 49ON_END_LOAD_TEST_SET = 'ModelPipeline.on_end_load_test_set'
 50ON_END_LOAD_TRAIN_SET = 'ModelPipeline.on_end_load_train_set'
 51ON_END_MODEL_PIPELINE = 'ModelPipeline.on_end'
 52ON_END_RECONSTRUCT_RATINGS = 'ModelPipeline.on_end_reconstruct_ratings'
 53ON_END_TEST_MODEL = 'ModelPipeline.on_end_test_model'
 54ON_END_TRAIN_MODEL = 'ModelPipeline.on_end_train_model'
 55ON_END_MODEL = 'ModelPipeline.on_end_model'
 56
 57
 58@dataclass
 59class ModelPipelineEventArgs(EventArgs):
 60    """Model Pipeline Event Arguments.
 61
 62    event_id: the unique ID that classifies the model pipeline event.
 63    api_name: the name of the api that is used in the model pipeline.
 64    models_config: list of model configurations that is used in the model pipeline.
 65    """
 66
 67    api_name: str
 68    models_config: List[ModelConfig]
 69
 70
 71@dataclass
 72class ModelEventArgs(EventArgs):
 73    """Model Event Arguments.
 74
 75    event_id: the unique ID that classifies the model event.
 76    model_name: the name of the model.
 77    model_params: the parameters of the model.
 78    """
 79
 80    model_name: str
 81    model_params: Dict[str, Any]
 82
 83
 84def get_model_events() -> List[str]:
 85    """Get a list of model pipeline event IDs.
 86
 87    Returns:
 88        a list of unique model pipeline event IDs.
 89    """
 90    return [
 91        # FileEventArgs
 92        ON_BEGIN_RECONSTRUCT_RATINGS,
 93        ON_END_RECONSTRUCT_RATINGS,
 94        # DataframeEventArgs
 95        ON_BEGIN_LOAD_TRAIN_SET,
 96        ON_END_LOAD_TRAIN_SET,
 97        # DataframeEventArgs
 98        ON_BEGIN_LOAD_TEST_SET,
 99        ON_END_LOAD_TEST_SET,
100        # ModelPipelineEventArgs
101        ON_BEGIN_MODEL_PIPELINE,
102        ON_END_MODEL_PIPELINE,
103        # ModelEventArgs
104        ON_BEGIN_TEST_MODEL,
105        ON_END_TEST_MODEL,
106        # ModelEventArgs
107        ON_BEGIN_TRAIN_MODEL,
108        ON_END_TRAIN_MODEL,
109        # ModelEventArgs
110        ON_BEGIN_MODEL,
111        ON_END_MODEL
112    ]
113
114
115def get_model_event_print_switch(elapsed_time: float=None) -> Dict[str,Callable[[EventArgs],None]]:
116    """Get a switch that prints model pipeline event IDs.
117
118    Returns:
119        the print model pipeline event switch.
120    """
121    return {
122        ON_BEGIN_LOAD_TEST_SET: print_load_df_event_args,
123        ON_BEGIN_LOAD_TRAIN_SET: print_load_df_event_args,
124        ON_BEGIN_MODEL_PIPELINE:
125            lambda args: print('\nStarting Model Pipeline:', args.api_name,
126                                'to process', len(args.models_config), 'model(s)'),
127        ON_BEGIN_MODEL:
128            lambda args: print('Starting model:', args.model_name),
129        ON_BEGIN_RECONSTRUCT_RATINGS:
130            lambda args: print('Reconstructing ratings for:', args.file_path),
131        ON_BEGIN_TEST_MODEL:
132            lambda args: print('Testing model:', args.model_name),
133        ON_BEGIN_TRAIN_MODEL:
134            lambda args: print('Training model:', args.model_name),
135        ON_END_LOAD_TEST_SET:
136            lambda args: print_load_df_event_args(args, elapsed_time),
137        ON_END_LOAD_TRAIN_SET:
138            lambda args: print_load_df_event_args(args, elapsed_time),
139        ON_END_MODEL_PIPELINE:
140            lambda args: print('Finished Model Pipeline:', args.api_name,
141                                f'in {elapsed_time:1.4f}s'),
142        ON_END_MODEL:
143            lambda args: print('Finished model:', args.model_name,
144                                f'in {elapsed_time:1.4f}s'),
145        ON_END_RECONSTRUCT_RATINGS:
146            lambda args: print(f'Reconstructed ratings in {elapsed_time:1.4f}s for:',
147                               args.file_path),
148        ON_END_TEST_MODEL:
149            lambda args: print(f'Tested model in {elapsed_time:1.4f}s'),
150        ON_END_TRAIN_MODEL:
151            lambda args: print(f'Trained model in {elapsed_time:1.4f}s'),
152    }
@dataclass
class ModelPipelineEventArgs(src.fairreckitlib.core.events.event_args.EventArgs):
59@dataclass
60class ModelPipelineEventArgs(EventArgs):
61    """Model Pipeline Event Arguments.
62
63    event_id: the unique ID that classifies the model pipeline event.
64    api_name: the name of the api that is used in the model pipeline.
65    models_config: list of model configurations that is used in the model pipeline.
66    """
67
68    api_name: str
69    models_config: List[ModelConfig]

Model Pipeline Event Arguments.

event_id: the unique ID that classifies the model pipeline event. api_name: the name of the api that is used in the model pipeline. models_config: list of model configurations that is used in the model pipeline.

ModelPipelineEventArgs( event_id: str, api_name: str, models_config: List[src.fairreckitlib.model.pipeline.model_config.ModelConfig])
@dataclass
class ModelEventArgs(src.fairreckitlib.core.events.event_args.EventArgs):
72@dataclass
73class ModelEventArgs(EventArgs):
74    """Model Event Arguments.
75
76    event_id: the unique ID that classifies the model event.
77    model_name: the name of the model.
78    model_params: the parameters of the model.
79    """
80
81    model_name: str
82    model_params: Dict[str, Any]

Model Event Arguments.

event_id: the unique ID that classifies the model event. model_name: the name of the model. model_params: the parameters of the model.

ModelEventArgs(event_id: str, model_name: str, model_params: Dict[str, Any])
def get_model_events() -> List[str]:
 85def get_model_events() -> List[str]:
 86    """Get a list of model pipeline event IDs.
 87
 88    Returns:
 89        a list of unique model pipeline event IDs.
 90    """
 91    return [
 92        # FileEventArgs
 93        ON_BEGIN_RECONSTRUCT_RATINGS,
 94        ON_END_RECONSTRUCT_RATINGS,
 95        # DataframeEventArgs
 96        ON_BEGIN_LOAD_TRAIN_SET,
 97        ON_END_LOAD_TRAIN_SET,
 98        # DataframeEventArgs
 99        ON_BEGIN_LOAD_TEST_SET,
100        ON_END_LOAD_TEST_SET,
101        # ModelPipelineEventArgs
102        ON_BEGIN_MODEL_PIPELINE,
103        ON_END_MODEL_PIPELINE,
104        # ModelEventArgs
105        ON_BEGIN_TEST_MODEL,
106        ON_END_TEST_MODEL,
107        # ModelEventArgs
108        ON_BEGIN_TRAIN_MODEL,
109        ON_END_TRAIN_MODEL,
110        # ModelEventArgs
111        ON_BEGIN_MODEL,
112        ON_END_MODEL
113    ]

Get a list of model pipeline event IDs.

Returns: a list of unique model pipeline event IDs.

def get_model_event_print_switch( elapsed_time: float = None) -> Dict[str, Callable[[src.fairreckitlib.core.events.event_args.EventArgs], NoneType]]:
116def get_model_event_print_switch(elapsed_time: float=None) -> Dict[str,Callable[[EventArgs],None]]:
117    """Get a switch that prints model pipeline event IDs.
118
119    Returns:
120        the print model pipeline event switch.
121    """
122    return {
123        ON_BEGIN_LOAD_TEST_SET: print_load_df_event_args,
124        ON_BEGIN_LOAD_TRAIN_SET: print_load_df_event_args,
125        ON_BEGIN_MODEL_PIPELINE:
126            lambda args: print('\nStarting Model Pipeline:', args.api_name,
127                                'to process', len(args.models_config), 'model(s)'),
128        ON_BEGIN_MODEL:
129            lambda args: print('Starting model:', args.model_name),
130        ON_BEGIN_RECONSTRUCT_RATINGS:
131            lambda args: print('Reconstructing ratings for:', args.file_path),
132        ON_BEGIN_TEST_MODEL:
133            lambda args: print('Testing model:', args.model_name),
134        ON_BEGIN_TRAIN_MODEL:
135            lambda args: print('Training model:', args.model_name),
136        ON_END_LOAD_TEST_SET:
137            lambda args: print_load_df_event_args(args, elapsed_time),
138        ON_END_LOAD_TRAIN_SET:
139            lambda args: print_load_df_event_args(args, elapsed_time),
140        ON_END_MODEL_PIPELINE:
141            lambda args: print('Finished Model Pipeline:', args.api_name,
142                                f'in {elapsed_time:1.4f}s'),
143        ON_END_MODEL:
144            lambda args: print('Finished model:', args.model_name,
145                                f'in {elapsed_time:1.4f}s'),
146        ON_END_RECONSTRUCT_RATINGS:
147            lambda args: print(f'Reconstructed ratings in {elapsed_time:1.4f}s for:',
148                               args.file_path),
149        ON_END_TEST_MODEL:
150            lambda args: print(f'Tested model in {elapsed_time:1.4f}s'),
151        ON_END_TRAIN_MODEL:
152            lambda args: print(f'Trained model in {elapsed_time:1.4f}s'),
153    }

Get a switch that prints model pipeline event IDs.

Returns: the print model pipeline event switch.