Skip to content

Commit

Permalink
fix typecheck errors in sparse matrix & iknn
Browse files Browse the repository at this point in the history
  • Loading branch information
mdekstrand committed May 9, 2024
1 parent 309123b commit f8661c3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lenskit/algorithms/item_knn.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class ItemItem(Predictor):
AGG_WA = intern("weighted-average")
RATING_AGGS = [AGG_WA] # the aggregates that use rating values

nnbrs: int | None
nnbrs: int
min_nbrs: int
min_sim: float
save_nbrs: int | None
Expand All @@ -387,7 +387,7 @@ class ItemItem(Predictor):

def __init__(
self,
nnbrs: int | None,
nnbrs: int,
min_nbrs: int = 1,
min_sim: float = 1.0e-6,
save_nbrs: int | None = None,
Expand Down Expand Up @@ -560,9 +560,9 @@ def predict_for_user(self, user, items, ratings=None):
_logger.debug("user %s missing, returning empty predictions", user)
return pd.Series(np.nan, index=items)
upos = self.user_index_.get_loc(user)
row = self.rating_matrix_[upos]
row = self.rating_matrix_[upos] # type: ignore
ratings = pd.Series(
row.values(),
row.values().numpy(),
index=pd.Index(self.item_index_[row.indices()[0]]),
)

Expand Down
5 changes: 2 additions & 3 deletions lenskit/data/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,20 @@
from __future__ import annotations

import logging
from collections import namedtuple
from typing import Generic, Literal, NamedTuple, TypeVar, overload

import numpy as np
import pandas as pd
import scipy.sparse as sps
import torch as t
from csr import CSR
from typing_extensions import Generic, Literal, NamedTuple, TypeVar, overload

_log = logging.getLogger(__name__)

M = TypeVar("M", CSR, sps.csr_matrix, sps.coo_matrix, t.Tensor)


class RatingMatrix(NamedTuple):
class RatingMatrix(NamedTuple, Generic[M]):
"""
A rating matrix with associated indices.
Expand Down

0 comments on commit f8661c3

Please sign in to comment.