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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 | class SelectionTimestampMixin:
@overload
def timestamps_gt(self: T, timestamp: float) -> T: ...
@overload
def timestamps_gt(self: T, timestamp: float, inplace: Literal[True]) -> None: ...
def timestamps_gt(self: T, timestamp: float, inplace: bool = False) -> None | T:
"""Select interactions after a given timestamp.
:param timestamp: The timestamp with which
the interactions timestamp is compared.
:type timestamp: float
:param inplace: Apply the selection in place if True, defaults to False
:type inplace: bool, optional
:return: None if `inplace`, otherwise returns a new InteractionMatrix object
:rtype: Union[InteractionMatrix, None]
"""
return self._timestamps_cmp(operator.gt, timestamp, inplace)
@overload
def timestamps_gte(self: T, timestamp: float) -> T: ...
@overload
def timestamps_gte(self: T, timestamp: float, inplace: Literal[True]) -> None: ...
def timestamps_gte(self: T, timestamp: float, inplace: bool = False) -> None | T:
"""Select interactions after and including a given timestamp.
:param timestamp: The timestamp with which
the interactions timestamp is compared.
:type timestamp: float
:param inplace: Apply the selection in place if True, defaults to False
:type inplace: bool, optional
:return: None if `inplace`, otherwise returns a new InteractionMatrix object
:rtype: Union[InteractionMatrix, None]
"""
return self._timestamps_cmp(operator.ge, timestamp, inplace)
@overload
def timestamps_lt(self: T, timestamp: float) -> T: ...
@overload
def timestamps_lt(self: T, timestamp: float, inplace: Literal[True]) -> None: ...
def timestamps_lt(self: T, timestamp: float, inplace: bool = False) -> None | T:
"""Select interactions up to a given timestamp.
:param timestamp: The timestamp with which
the interactions timestamp is compared.
:type timestamp: float
:param inplace: Apply the selection in place if True, defaults to False
:type inplace: bool, optional
:return: None if `inplace`, otherwise returns a new InteractionMatrix object
:rtype: Union[InteractionMatrix, None]
"""
return self._timestamps_cmp(operator.lt, timestamp, inplace)
@overload
def timestamps_lte(self: T, timestamp: float) -> T: ...
@overload
def timestamps_lte(self: T, timestamp: float, inplace: Literal[True]) -> None: ...
def timestamps_lte(self: T, timestamp: float, inplace: bool = False) -> None | T:
"""Select interactions up to and including a given timestamp.
:param timestamp: The timestamp with which
the interactions timestamp is compared.
:type timestamp: float
:param inplace: Apply the selection in place if True, defaults to False
:type inplace: bool, optional
:return: None if `inplace`, otherwise returns a new InteractionMatrix object
:rtype: Union[InteractionMatrix, None]
"""
return self._timestamps_cmp(operator.le, timestamp, inplace)
def _timestamps_cmp(self: T, op, timestamp: float, inplace: bool = False) -> None | T:
# import here to avoid circular imports at module import time
from .exception import TimestampAttributeMissingError
if not self.has_timestamps:
raise TimestampAttributeMissingError()
logger.debug(f"Performing {op.__name__}(t, {timestamp})")
mask = op(self._df[self.TIMESTAMP_IX], timestamp)
return self._apply_mask(mask, inplace=inplace)
def get_users_n_last_interaction(
self: T, n_seq_data: int = 1, t_upper: None | int = None, user_in: None | set[int] = None, inplace: bool = False
) -> T:
return self._get_last_n_interactions(
by=ItemUserBasedEnum.USER, n_seq_data=n_seq_data, t_upper=t_upper, id_in=user_in, inplace=inplace
)
def get_items_n_last_interaction(
self: T, n_seq_data: int = 1, t_upper: None | int = None, item_in: None | set[int] = None, inplace: bool = False
) -> T:
return self._get_last_n_interactions(by=ItemUserBasedEnum.ITEM, n_seq_data=n_seq_data, t_upper=t_upper, id_in=item_in, inplace=inplace)
def get_users_n_first_interaction(
self: T, n_seq_data: int = 1, t_lower: None | int = None, inplace: bool = False
) -> T:
return self._get_first_n_interactions(ItemUserBasedEnum.USER, n_seq_data, t_lower, inplace)
def get_items_n_first_interaction(
self: T, n_seq_data: int = 1, t_lower: None | int = None, inplace: bool = False
) -> T:
return self._get_first_n_interactions(ItemUserBasedEnum.ITEM, n_seq_data, t_lower, inplace)
|