Skip to content

dataset

DATASET_REGISTRY = DatasetRegistry() module-attribute

Registry for datasets.

Contains the recnexteval metrics by default, and allows registration of new metrics via the register function.

Example
from recnexteval.pipelines import METRIC_REGISTRY

# Construct a Recall object with parameter K=20
algo = METRIC_REGISTRY.get('Recall')(K=20)

from recnexteval.algorithms import Recall
METRIC_REGISTRY.register('HelloWorld', Recall)

# Also construct a Recall object with parameter K=20
algo = METRIC_REGISTRY.get('HelloWorld')(K=20)

DatasetRegistry

Bases: Registry[Dataset]

Registry for easy retrieval of dataset types by name.

The registry comes preregistered with all the recnexteval datasets.

Source code in src/recnexteval/registries/dataset.py
 7
 8
 9
10
11
12
13
14
15
class DatasetRegistry(Registry[Dataset]):
    """Registry for easy retrieval of dataset types by name.

    The registry comes preregistered with all the recnexteval datasets.
    """

    def __init__(self) -> None:
        module = importlib.import_module('recnexteval.datasets')
        super().__init__(module)

IS_BASE = True class-attribute instance-attribute

name property

Name of the object's class.

:return: Name of the object's class :rtype: str

registered = {} instance-attribute

src = src instance-attribute

get(key)

Retrieve the value for this key.

This value is a Python type, most often a class.

Parameters:

Name Type Description Default
key str

The key to fetch.

required

Returns:

Type Description
type[T]

The class type associated with the key.

Raises:

Type Description
KeyError

If the key is not found in the registry.

Source code in src/recnexteval/registries/base.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
def get(self, key: str) -> type[T]:
    """Retrieve the value for this key.

    This value is a Python type, most often a class.

    Args:
        key: The key to fetch.

    Returns:
        The class type associated with the key.

    Raises:
        KeyError: If the key is not found in the registry.
    """
    return self[key]

register(key, cls)

Register a new Python type (most often a class).

After registration, the key can be used to fetch the Python type from the registry.

Parameters:

Name Type Description Default
key str

Key to register the type at. Needs to be unique to the registry.

required
cls type[T]

Class to register.

required

Raises:

Type Description
KeyError

If the key is already registered.

Source code in src/recnexteval/registries/base.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def register(self, key: str, cls: type[T]) -> None:
    """Register a new Python type (most often a class).

    After registration, the key can be used to fetch the Python type from the registry.

    Args:
        key: Key to register the type at. Needs to be unique to the registry.
        cls: Class to register.

    Raises:
        KeyError: If the key is already registered.
    """
    if key in self.registered:
        raise KeyError(f"key `{key}` already registered")
    self.registered[key] = cls

get_registered_keys(include_base=False)

Get a list of all registered keys.

Returns:

Type Description
list[str]

A list of all registered keys.

Source code in src/recnexteval/registries/base.py
106
107
108
109
110
111
112
113
114
115
def get_registered_keys(self, include_base: bool = False) -> list[str]:
    """Get a list of all registered keys.

    Returns:
        A list of all registered keys.
    """
    if include_base:
        return list(self.registered.keys())
    else:
        return [key for key, cls in self.registered.items() if not getattr(cls, "IS_BASE", True)]

registered_values()

Get a list of all registered types.

Returns:

Type Description
list[type[T]]

A list of all registered types.

Source code in src/recnexteval/registries/base.py
117
118
119
120
121
122
123
def registered_values(self) -> list[type[T]]:
    """Get a list of all registered types.

    Returns:
        A list of all registered types.
    """
    return [self.registered[key] for key in self.get_registered_keys(include_base=False)]

registered_items()

Get a list of all registered key-type pairs.

Returns:

Type Description
list[tuple[str, type[T]]]

A list of all registered key-type pairs.

Source code in src/recnexteval/registries/base.py
125
126
127
128
129
130
131
def registered_items(self) -> list[tuple[str, type[T]]]:
    """Get a list of all registered key-type pairs.

    Returns:
        A list of all registered key-type pairs.
    """
    return [(key, self.registered[key]) for key in self.get_registered_keys(include_base=False)]