Skip to content

state_management

logger = logging.getLogger(__name__) module-attribute

AlgorithmStateEntry dataclass

Entry for the algorithm status registry.

This dataclass stores the status of an algorithm for use by AlgorithmStateManager. It contains the algorithm name, unique identifier, current state, associated data segment, and an optional pointer to the algorithm object.

Attributes:

Name Type Description
name str

Name of the algorithm.

algorithm_uuid UUID

Unique identifier for the algorithm.

algorithm_ptr Algorithm

Pointer to the algorithm object.

state AlgorithmStateEnum

State of the algorithm.

data_segment int

Data segment the algorithm is associated with.

params dict[str, Any]

Parameters for the algorithm.

Source code in src/recnexteval/evaluators/core/state_management.py
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@dataclass
class AlgorithmStateEntry:
    """Entry for the algorithm status registry.

    This dataclass stores the status of an algorithm for use by
    `AlgorithmStateManager`. It contains the algorithm name, unique
    identifier, current state, associated data segment, and an optional
    pointer to the algorithm object.

    Attributes:
        name: Name of the algorithm.
        algorithm_uuid: Unique identifier for the algorithm.
        algorithm_ptr: Pointer to the algorithm object.
        state: State of the algorithm.
        data_segment: Data segment the algorithm is associated with.
        params: Parameters for the algorithm.
    """

    name: str
    algorithm_uuid: UUID
    algorithm_ptr: Algorithm
    state: AlgorithmStateEnum = AlgorithmStateEnum.NEW
    data_segment: int = 0
    params: dict[str, Any] = field(default_factory=dict)

name instance-attribute

algorithm_uuid instance-attribute

algorithm_ptr instance-attribute

state = AlgorithmStateEnum.NEW class-attribute instance-attribute

data_segment = 0 class-attribute instance-attribute

params = field(default_factory=dict) class-attribute instance-attribute

AlgorithmStateManager

Source code in src/recnexteval/evaluators/core/state_management.py
 41
 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
class AlgorithmStateManager:
    def __init__(self) -> None:
        self._algorithms: dict[UUID, AlgorithmStateEntry] = {}

    def __iter__(self) -> Iterator[UUID]:
        """Return an iterator over registered algorithm UUIDs.

        Allows iteration over the UUIDs of registered entries.

        Returns:
            An iterator over the UUIDs of registered entries.
        """
        return iter(self._algorithms)

    def __len__(self) -> int:
        """Return the number of registered algorithms.

        Returns:
            The number of registered algorithms.
        """
        return len(self._algorithms)

    def values(self) -> Iterator[AlgorithmStateEntry]:
        """Return an iterator over registered AlgorithmStateEntry objects.

        Allows iteration over the registered entries.

        Returns:
            An iterator over the registered entries.
        """
        return iter(self._algorithms.values())

    def __getitem__(self, key: UUID) -> AlgorithmStateEntry:
        if key not in self._algorithms:
            raise ValueError(f"Algorithm with ID:{key} not registered")
        return self._algorithms[key]

    def __setitem__(self, key: UUID, entry: AlgorithmStateEntry) -> None:
        """Register a new algorithm status entry under `key`.

        Allows the use of square bracket notation to register new entries.

        Args:
            key: The UUID to register the entry under.
            entry: The status entry to register.

        Raises:
            KeyError: If `key` is already registered.
        """
        if key in self:
            raise KeyError(f"Algorithm with ID:{key} already registered")
        self._algorithms[key] = entry

    def __contains__(self, key: UUID) -> bool:
        """Return whether the given key is known to the registry.

        Args:
            key: The key to check.

        Returns:
            True if the key is registered, False otherwise.
        """
        try:
            self[key]
            return True
        except AttributeError:
            return False

    def get(self, algo_id: UUID) -> AlgorithmStateEntry:
        """Get the :class:`AlgorithmStateEntry` for `algo_id`."""
        return self[algo_id]

    def get_state(self, algo_id: UUID) -> AlgorithmStateEnum:
        """Get the current state of the algorithm with `algo_id`."""
        return self[algo_id].state

    def register(
        self,
        algorithm_ptr: type[Algorithm] | Algorithm,
        name: None | str = None,
        params: dict[str, Any] = {},
        algo_uuid: None | UUID = None,
    ) -> UUID:
        """Register new algorithm"""
        if isinstance(algorithm_ptr, type):
            algorithm_ptr = algorithm_ptr(**params)

        if hasattr(algorithm_ptr, "identifier"):
            name = name or algorithm_ptr.identifier  # type: ignore[attr-defined]

        if not name:
            logger.warning("Algorithm name was not provided and could not be inferred from Algorithm pointer")
            name = "UnknownAlgorithm"

        if algo_uuid is None:
            algo_uuid = generate_algorithm_uuid(name=name)

        entry = AlgorithmStateEntry(algorithm_uuid=algo_uuid, name=name, algorithm_ptr=algorithm_ptr, params=params)
        self._algorithms[algo_uuid] = entry
        logger.info(f"Registered algorithm '{name}' with ID {algo_uuid}")
        return algo_uuid

    def can_request_training_data(self, algo_id: UUID) -> tuple[bool, str]:
        """Check if algorithm can request training data"""
        if algo_id not in self._algorithms:
            return False, f"Algorithm {algo_id} not registered"

        state = self._algorithms[algo_id].state

        if state == AlgorithmStateEnum.COMPLETED:
            return False, "Algorithm has completed evaluation"
        if state == AlgorithmStateEnum.NEW:
            return False, "The algorithm must be set to READY state first"
        if state == AlgorithmStateEnum.PREDICTED:
            return False, "Algorithm has already requested data for this window"
        if state == AlgorithmStateEnum.READY:
            return True, ""
        if state == AlgorithmStateEnum.RUNNING:
            return True, ""

        return False, f"Unknown state {state}"

    def can_request_unlabeled_data(self, algo_id: UUID) -> tuple[bool, str]:
        """Check if algorithm can request unlabeled data"""
        if algo_id not in self._algorithms:
            return False, f"Algorithm {algo_id} not registered"

        state = self._algorithms[algo_id].state

        if state == AlgorithmStateEnum.RUNNING:
            return True, ""
        if state == AlgorithmStateEnum.COMPLETED:
            return False, "Algorithm has completed evaluation"
        if state == AlgorithmStateEnum.NEW:
            return False, "The algorithm must be set to RUNNING state to request unlabeled data"
        if state == AlgorithmStateEnum.PREDICTED:
            return False, "Algorithm has already requested data for this window"
        if state == AlgorithmStateEnum.READY:
            return (
                False,
                "The algorithm must be set to RUNNING state to request unlabeled data. Request training data first",
            )

        return False, f"Unknown state {state}"

    def can_submit_prediction(self, algo_id: UUID) -> tuple[bool, str]:
        """Check if algorithm can submit prediction"""
        if algo_id not in self._algorithms:
            return False, f"Algorithm {algo_id} not registered"

        state = self._algorithms[algo_id].state

        if state == AlgorithmStateEnum.RUNNING:
            return True, ""
        if state == AlgorithmStateEnum.READY:
            return False, "There is new data to be requested"
        if state == AlgorithmStateEnum.NEW:
            return False, "Algorithm must request data first"
        if state == AlgorithmStateEnum.PREDICTED:
            return False, "Algorithm already submitted prediction for this window"
        if state == AlgorithmStateEnum.COMPLETED:
            return False, "Algorithm has completed evaluation"

        return False, f"Unknown state {state}"

    def transition(self, algo_id: UUID, new_state: AlgorithmStateEnum, data_segment: None | int = None) -> None:
        """Transition algorithm to new state with validation"""
        if algo_id not in self._algorithms:
            raise ValueError(f"Algorithm {algo_id} not registered")

        entry = self._algorithms[algo_id]
        old_state = entry.state

        # Define valid transitions
        valid_transitions = {
            # old_state: [list of valid new_states]
            AlgorithmStateEnum.NEW: [AlgorithmStateEnum.READY, AlgorithmStateEnum.COMPLETED],
            AlgorithmStateEnum.READY: [AlgorithmStateEnum.RUNNING],
            AlgorithmStateEnum.RUNNING: [AlgorithmStateEnum.RUNNING, AlgorithmStateEnum.PREDICTED],
            AlgorithmStateEnum.PREDICTED: [AlgorithmStateEnum.READY, AlgorithmStateEnum.COMPLETED],
            AlgorithmStateEnum.COMPLETED: [],
        }

        if new_state not in valid_transitions.get(old_state, []):
            raise ValueError(f"Invalid transition: {old_state} -> {new_state}")

        entry.state = new_state
        if data_segment is not None:
            entry.data_segment = data_segment

        logger.debug(f"Algorithm '{entry.name}' transitioned {old_state.value} -> {new_state.value}")

    def is_all_predicted(self) -> bool:
        """Return whether every registered algorithm is in PREDICTED state.

        Returns:
            True if all registered entries have state
            `AlgorithmStateEnum.PREDICTED`, False otherwise.
        """
        if not self._algorithms:
            return False
        return all(entry.state == AlgorithmStateEnum.PREDICTED for entry in self._algorithms.values())

    def get_all_states(self) -> dict[str, AlgorithmStateEnum]:
        """Get state of all algorithms"""
        return {entry.name: entry.state for entry in self._algorithms.values()}

    def is_all_same_data_segment(self) -> bool:
        """Return whether all registered entries share the same data segment.

        Returns:
            True if there is exactly one distinct data segment across all
            registered entries, False otherwise.
        """
        data_segments: set[None | int] = set()
        for key in self:
            data_segments.add(self[key].data_segment)
        return len(data_segments) == 1

    def all_algo_states(self) -> dict[str, AlgorithmStateEnum]:
        """Return a mapping of identifier strings to algorithm states.

        The identifier used is "{name}_{uuid}" for each registered entry.

        Returns:
            Mapping from identifier string to the entry's
            :class:`AlgorithmStateEnum`.
        """
        states: dict[str, AlgorithmStateEnum] = {}
        for key in self:
            states[f"{self[key].name}_{key}"] = self[key].state
        return states

    def set_all_ready(self, data_segment: int) -> None:
        """Set all registered algorithms to the READY state.

        Args:
            data_segment: Data segment to assign to every algorithm.
        """
        for key in self:
            self.transition(key, AlgorithmStateEnum.READY, data_segment)

    def get_algorithm_identifier(self, algo_id: UUID) -> str:
        """Return a stable identifier string for the algorithm.

        Args:
            algo_id: UUID of the algorithm.

        Returns:
            Identifier in the format "{name}_{uuid}".

        Raises:
            AttributeError: If `algo_id` is not registered.
        """
        if algo_id not in self._algorithms:
            raise AttributeError(f"Algorithm with ID:{algo_id} not registered")
        return f"{self[algo_id].name}_{algo_id}"

values()

Return an iterator over registered AlgorithmStateEntry objects.

Allows iteration over the registered entries.

Returns:

Type Description
Iterator[AlgorithmStateEntry]

An iterator over the registered entries.

Source code in src/recnexteval/evaluators/core/state_management.py
63
64
65
66
67
68
69
70
71
def values(self) -> Iterator[AlgorithmStateEntry]:
    """Return an iterator over registered AlgorithmStateEntry objects.

    Allows iteration over the registered entries.

    Returns:
        An iterator over the registered entries.
    """
    return iter(self._algorithms.values())

get(algo_id)

Get the :class:AlgorithmStateEntry for algo_id.

Source code in src/recnexteval/evaluators/core/state_management.py
109
110
111
def get(self, algo_id: UUID) -> AlgorithmStateEntry:
    """Get the :class:`AlgorithmStateEntry` for `algo_id`."""
    return self[algo_id]

get_state(algo_id)

Get the current state of the algorithm with algo_id.

Source code in src/recnexteval/evaluators/core/state_management.py
113
114
115
def get_state(self, algo_id: UUID) -> AlgorithmStateEnum:
    """Get the current state of the algorithm with `algo_id`."""
    return self[algo_id].state

register(algorithm_ptr, name=None, params={}, algo_uuid=None)

Register new algorithm

Source code in src/recnexteval/evaluators/core/state_management.py
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
def register(
    self,
    algorithm_ptr: type[Algorithm] | Algorithm,
    name: None | str = None,
    params: dict[str, Any] = {},
    algo_uuid: None | UUID = None,
) -> UUID:
    """Register new algorithm"""
    if isinstance(algorithm_ptr, type):
        algorithm_ptr = algorithm_ptr(**params)

    if hasattr(algorithm_ptr, "identifier"):
        name = name or algorithm_ptr.identifier  # type: ignore[attr-defined]

    if not name:
        logger.warning("Algorithm name was not provided and could not be inferred from Algorithm pointer")
        name = "UnknownAlgorithm"

    if algo_uuid is None:
        algo_uuid = generate_algorithm_uuid(name=name)

    entry = AlgorithmStateEntry(algorithm_uuid=algo_uuid, name=name, algorithm_ptr=algorithm_ptr, params=params)
    self._algorithms[algo_uuid] = entry
    logger.info(f"Registered algorithm '{name}' with ID {algo_uuid}")
    return algo_uuid

can_request_training_data(algo_id)

Check if algorithm can request training data

Source code in src/recnexteval/evaluators/core/state_management.py
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def can_request_training_data(self, algo_id: UUID) -> tuple[bool, str]:
    """Check if algorithm can request training data"""
    if algo_id not in self._algorithms:
        return False, f"Algorithm {algo_id} not registered"

    state = self._algorithms[algo_id].state

    if state == AlgorithmStateEnum.COMPLETED:
        return False, "Algorithm has completed evaluation"
    if state == AlgorithmStateEnum.NEW:
        return False, "The algorithm must be set to READY state first"
    if state == AlgorithmStateEnum.PREDICTED:
        return False, "Algorithm has already requested data for this window"
    if state == AlgorithmStateEnum.READY:
        return True, ""
    if state == AlgorithmStateEnum.RUNNING:
        return True, ""

    return False, f"Unknown state {state}"

can_request_unlabeled_data(algo_id)

Check if algorithm can request unlabeled data

Source code in src/recnexteval/evaluators/core/state_management.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def can_request_unlabeled_data(self, algo_id: UUID) -> tuple[bool, str]:
    """Check if algorithm can request unlabeled data"""
    if algo_id not in self._algorithms:
        return False, f"Algorithm {algo_id} not registered"

    state = self._algorithms[algo_id].state

    if state == AlgorithmStateEnum.RUNNING:
        return True, ""
    if state == AlgorithmStateEnum.COMPLETED:
        return False, "Algorithm has completed evaluation"
    if state == AlgorithmStateEnum.NEW:
        return False, "The algorithm must be set to RUNNING state to request unlabeled data"
    if state == AlgorithmStateEnum.PREDICTED:
        return False, "Algorithm has already requested data for this window"
    if state == AlgorithmStateEnum.READY:
        return (
            False,
            "The algorithm must be set to RUNNING state to request unlabeled data. Request training data first",
        )

    return False, f"Unknown state {state}"

can_submit_prediction(algo_id)

Check if algorithm can submit prediction

Source code in src/recnexteval/evaluators/core/state_management.py
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def can_submit_prediction(self, algo_id: UUID) -> tuple[bool, str]:
    """Check if algorithm can submit prediction"""
    if algo_id not in self._algorithms:
        return False, f"Algorithm {algo_id} not registered"

    state = self._algorithms[algo_id].state

    if state == AlgorithmStateEnum.RUNNING:
        return True, ""
    if state == AlgorithmStateEnum.READY:
        return False, "There is new data to be requested"
    if state == AlgorithmStateEnum.NEW:
        return False, "Algorithm must request data first"
    if state == AlgorithmStateEnum.PREDICTED:
        return False, "Algorithm already submitted prediction for this window"
    if state == AlgorithmStateEnum.COMPLETED:
        return False, "Algorithm has completed evaluation"

    return False, f"Unknown state {state}"

transition(algo_id, new_state, data_segment=None)

Transition algorithm to new state with validation

Source code in src/recnexteval/evaluators/core/state_management.py
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
def transition(self, algo_id: UUID, new_state: AlgorithmStateEnum, data_segment: None | int = None) -> None:
    """Transition algorithm to new state with validation"""
    if algo_id not in self._algorithms:
        raise ValueError(f"Algorithm {algo_id} not registered")

    entry = self._algorithms[algo_id]
    old_state = entry.state

    # Define valid transitions
    valid_transitions = {
        # old_state: [list of valid new_states]
        AlgorithmStateEnum.NEW: [AlgorithmStateEnum.READY, AlgorithmStateEnum.COMPLETED],
        AlgorithmStateEnum.READY: [AlgorithmStateEnum.RUNNING],
        AlgorithmStateEnum.RUNNING: [AlgorithmStateEnum.RUNNING, AlgorithmStateEnum.PREDICTED],
        AlgorithmStateEnum.PREDICTED: [AlgorithmStateEnum.READY, AlgorithmStateEnum.COMPLETED],
        AlgorithmStateEnum.COMPLETED: [],
    }

    if new_state not in valid_transitions.get(old_state, []):
        raise ValueError(f"Invalid transition: {old_state} -> {new_state}")

    entry.state = new_state
    if data_segment is not None:
        entry.data_segment = data_segment

    logger.debug(f"Algorithm '{entry.name}' transitioned {old_state.value} -> {new_state.value}")

is_all_predicted()

Return whether every registered algorithm is in PREDICTED state.

Returns:

Type Description
bool

True if all registered entries have state

bool

AlgorithmStateEnum.PREDICTED, False otherwise.

Source code in src/recnexteval/evaluators/core/state_management.py
233
234
235
236
237
238
239
240
241
242
def is_all_predicted(self) -> bool:
    """Return whether every registered algorithm is in PREDICTED state.

    Returns:
        True if all registered entries have state
        `AlgorithmStateEnum.PREDICTED`, False otherwise.
    """
    if not self._algorithms:
        return False
    return all(entry.state == AlgorithmStateEnum.PREDICTED for entry in self._algorithms.values())

get_all_states()

Get state of all algorithms

Source code in src/recnexteval/evaluators/core/state_management.py
244
245
246
def get_all_states(self) -> dict[str, AlgorithmStateEnum]:
    """Get state of all algorithms"""
    return {entry.name: entry.state for entry in self._algorithms.values()}

is_all_same_data_segment()

Return whether all registered entries share the same data segment.

Returns:

Type Description
bool

True if there is exactly one distinct data segment across all

bool

registered entries, False otherwise.

Source code in src/recnexteval/evaluators/core/state_management.py
248
249
250
251
252
253
254
255
256
257
258
def is_all_same_data_segment(self) -> bool:
    """Return whether all registered entries share the same data segment.

    Returns:
        True if there is exactly one distinct data segment across all
        registered entries, False otherwise.
    """
    data_segments: set[None | int] = set()
    for key in self:
        data_segments.add(self[key].data_segment)
    return len(data_segments) == 1

all_algo_states()

Return a mapping of identifier strings to algorithm states.

The identifier used is "{name}_{uuid}" for each registered entry.

Returns:

Type Description
dict[str, AlgorithmStateEnum]

Mapping from identifier string to the entry's

dict[str, AlgorithmStateEnum]

class:AlgorithmStateEnum.

Source code in src/recnexteval/evaluators/core/state_management.py
260
261
262
263
264
265
266
267
268
269
270
271
272
def all_algo_states(self) -> dict[str, AlgorithmStateEnum]:
    """Return a mapping of identifier strings to algorithm states.

    The identifier used is "{name}_{uuid}" for each registered entry.

    Returns:
        Mapping from identifier string to the entry's
        :class:`AlgorithmStateEnum`.
    """
    states: dict[str, AlgorithmStateEnum] = {}
    for key in self:
        states[f"{self[key].name}_{key}"] = self[key].state
    return states

set_all_ready(data_segment)

Set all registered algorithms to the READY state.

Parameters:

Name Type Description Default
data_segment int

Data segment to assign to every algorithm.

required
Source code in src/recnexteval/evaluators/core/state_management.py
274
275
276
277
278
279
280
281
def set_all_ready(self, data_segment: int) -> None:
    """Set all registered algorithms to the READY state.

    Args:
        data_segment: Data segment to assign to every algorithm.
    """
    for key in self:
        self.transition(key, AlgorithmStateEnum.READY, data_segment)

get_algorithm_identifier(algo_id)

Return a stable identifier string for the algorithm.

Parameters:

Name Type Description Default
algo_id UUID

UUID of the algorithm.

required

Returns:

Type Description
str

Identifier in the format "{name}_{uuid}".

Raises:

Type Description
AttributeError

If algo_id is not registered.

Source code in src/recnexteval/evaluators/core/state_management.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
def get_algorithm_identifier(self, algo_id: UUID) -> str:
    """Return a stable identifier string for the algorithm.

    Args:
        algo_id: UUID of the algorithm.

    Returns:
        Identifier in the format "{name}_{uuid}".

    Raises:
        AttributeError: If `algo_id` is not registered.
    """
    if algo_id not in self._algorithms:
        raise AttributeError(f"Algorithm with ID:{algo_id} not registered")
    return f"{self[algo_id].name}_{algo_id}"