Skip to content

processor

Processor

Bases: ABC

Base class for processing data.

Abstract class for processing data. Subclasses should implement the process method to handle specific data processing logic.

Source code in src/recnexteval/settings/processor.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Processor(ABC):
    """Base class for processing data.

    Abstract class for processing data. Subclasses should implement the `process` method
    to handle specific data processing logic.
    """

    def __init__(self) -> None:
        pass

    @abstractmethod
    def process(
        self,
        past_interaction: InteractionMatrix,
        future_interaction: InteractionMatrix,
    ) -> tuple[InteractionMatrix, InteractionMatrix]:
        """Injects the user ID to indicate ID for prediction.

        User ID to be predicted by the model will be indicated with item ID of
        "-1" as the corresponding label. The matrix with past interactions will
        contain the user ID to be predicted which will be derived from the set
        of user IDs in the future interaction matrix. Timestamp of the masked
        interactions will be preserved as the item ID are simply masked with
        "-1".

        Args:
            past_interaction: Matrix of past interactions.
            future_interaction: Matrix of future interactions.

        Returns:
            Tuple of past interaction with injected user ID to predict and
                ground truth future interactions of the actual interaction.
        """
        pass

process(past_interaction, future_interaction) abstractmethod

Injects the user ID to indicate ID for prediction.

User ID to be predicted by the model will be indicated with item ID of "-1" as the corresponding label. The matrix with past interactions will contain the user ID to be predicted which will be derived from the set of user IDs in the future interaction matrix. Timestamp of the masked interactions will be preserved as the item ID are simply masked with "-1".

Parameters:

Name Type Description Default
past_interaction InteractionMatrix

Matrix of past interactions.

required
future_interaction InteractionMatrix

Matrix of future interactions.

required

Returns:

Type Description
tuple[InteractionMatrix, InteractionMatrix]

Tuple of past interaction with injected user ID to predict and ground truth future interactions of the actual interaction.

Source code in src/recnexteval/settings/processor.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
@abstractmethod
def process(
    self,
    past_interaction: InteractionMatrix,
    future_interaction: InteractionMatrix,
) -> tuple[InteractionMatrix, InteractionMatrix]:
    """Injects the user ID to indicate ID for prediction.

    User ID to be predicted by the model will be indicated with item ID of
    "-1" as the corresponding label. The matrix with past interactions will
    contain the user ID to be predicted which will be derived from the set
    of user IDs in the future interaction matrix. Timestamp of the masked
    interactions will be preserved as the item ID are simply masked with
    "-1".

    Args:
        past_interaction: Matrix of past interactions.
        future_interaction: Matrix of future interactions.

    Returns:
        Tuple of past interaction with injected user ID to predict and
            ground truth future interactions of the actual interaction.
    """
    pass

PredictionDataProcessor

Bases: Processor

Injects the user ID to indicate ID for prediction.

Operates on the past and future interaction matrices to inject the user ID to be predicted by the model into the past interaction matrix. The resulting past interaction matrix will contain the user ID to be predicted which will be derived from the set of user IDs in the future interaction matrix. Timestamp of the masked interactions will be preserved as the item ID are simply masked with "-1".

The corresponding ground truth future interactions of the actual interaction will be returned as well in a tuple when process is called.

Source code in src/recnexteval/settings/processor.py
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class PredictionDataProcessor(Processor):
    """Injects the user ID to indicate ID for prediction.

    Operates on the past and future interaction matrices to inject the user
    ID to be predicted by the model into the past interaction matrix. The
    resulting past interaction matrix will contain the user ID to be
    predicted which will be derived from the set of user IDs in the future
    interaction matrix. Timestamp of the masked interactions will be preserved as
    the item ID are simply masked with "-1".

    The corresponding ground truth future interactions of the actual interaction
    will be returned as well in a tuple when `process` is called.
    """

    def _inject_user_id(
        self,
        past_interaction: InteractionMatrix,
        future_interaction: InteractionMatrix,
        top_K: int = 1,
    ) -> tuple[InteractionMatrix, InteractionMatrix]:
        """Injects the user ID to indicate ID for prediction.

        User ID to be predicted by the model will be indicated with item ID of
        "-1" as the corresponding label. The matrix with past interactions will
        contain the user ID to be predicted which will be derived from the set
        of user IDs in the future interaction matrix. Timestamp of the masked
        interactions will be preserved as the item ID are simply masked with
        "-1".

        Args:
            past_interaction: Matrix of past interactions.
            future_interaction: Matrix of future interactions.
            top_K: Number of top interactions to consider. Defaults to 1.

        Returns:
            tuple[InteractionMatrix, InteractionMatrix]: Tuple of past interaction with injected user ID to predict and
                ground truth future interactions of the actual interaction.
        """
        users_to_predict = future_interaction.get_users_n_first_interaction(top_K)
        masked_frame = users_to_predict.copy_df()
        masked_frame[InteractionMatrix.ITEM_IX] = InteractionMatrix.MASKED_LABEL
        return past_interaction.concat(masked_frame), users_to_predict

    def _inject_item_id(
        self,
        past_interaction: InteractionMatrix,
        future_interaction: InteractionMatrix,
        top_K: int = 1,
    ) -> tuple[InteractionMatrix, InteractionMatrix]:
        """Injects the item ID to indicate ID for prediction.

        Item ID to be predicted by the model will be indicated with item ID of
        "-1" as the corresponding label. The matrix with past interactions will
        contain the item ID to be predicted which will be derived from the set
        of item IDs in the future interaction matrix. Timestamp of the masked
        interactions will be preserved as the item ID are simply masked with
        "-1".

        Args:
            past_interaction: Matrix of past interactions.
            future_interaction: Matrix of future interactions.
            top_K: Number of top interactions to consider. Defaults to 1.

        Returns:
            Tuple of past interaction with injected item ID to predict and
                ground truth future interactions of the actual interaction.
        """
        items_to_predict = future_interaction.get_items_n_first_interaction(top_K)
        masked_frame = items_to_predict.copy_df()
        masked_frame[InteractionMatrix.USER_IX] = InteractionMatrix.MASKED_LABEL
        return past_interaction.concat(masked_frame), items_to_predict

    def process(
        self,
        past_interaction: InteractionMatrix,
        future_interaction: InteractionMatrix,
        top_K: int = 1,
    ) -> tuple[InteractionMatrix, InteractionMatrix]:
        """Processes past and future interactions to prepare data for prediction.

        Injects user IDs for prediction into the past interaction matrix based on future interactions.

        Args:
            past_interaction: Matrix of past interactions.
            future_interaction: Matrix of future interactions.
            top_K: Number of top interactions to consider. Defaults to 1.

        Returns:
            Tuple of processed past interaction and ground truth.
        """
        return self._inject_user_id(
            past_interaction=past_interaction,
            future_interaction=future_interaction,
            top_K=top_K,
        )

process(past_interaction, future_interaction, top_K=1)

Processes past and future interactions to prepare data for prediction.

Injects user IDs for prediction into the past interaction matrix based on future interactions.

Parameters:

Name Type Description Default
past_interaction InteractionMatrix

Matrix of past interactions.

required
future_interaction InteractionMatrix

Matrix of future interactions.

required
top_K int

Number of top interactions to consider. Defaults to 1.

1

Returns:

Type Description
tuple[InteractionMatrix, InteractionMatrix]

Tuple of processed past interaction and ground truth.

Source code in src/recnexteval/settings/processor.py
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
def process(
    self,
    past_interaction: InteractionMatrix,
    future_interaction: InteractionMatrix,
    top_K: int = 1,
) -> tuple[InteractionMatrix, InteractionMatrix]:
    """Processes past and future interactions to prepare data for prediction.

    Injects user IDs for prediction into the past interaction matrix based on future interactions.

    Args:
        past_interaction: Matrix of past interactions.
        future_interaction: Matrix of future interactions.
        top_K: Number of top interactions to consider. Defaults to 1.

    Returns:
        Tuple of processed past interaction and ground truth.
    """
    return self._inject_user_id(
        past_interaction=past_interaction,
        future_interaction=future_interaction,
        top_K=top_K,
    )