dicee.evaluation.utils

Utility functions for evaluation module.

This module contains shared helper functions used across different evaluation components.

Attributes

DEFAULT_HITS_RANGE

ALL_HITS_RANGE

Functions

make_iterable_verbose(iterable_object, verbose[, ...]) → Iterable

Wrap an iterable with tqdm progress bar if verbose is True.

compute_metrics_from_ranks(ranks, num_triples, hits_dict) → Dict[str, float]

Compute standard link prediction metrics from ranks.

compute_metrics_from_ranks_simple(ranks, num_triples, ...) → Dict[str, float]

Compute link prediction metrics without scaling factor.

update_hits(hits, rank[, hits_range]) → None

Update hits dictionary based on rank.

create_hits_dict([hits_range]) → Dict[int, List[float]]

Create an initialized hits dictionary.

efficient_zero_grad(model) → None

Efficiently zero gradients using parameter.grad = None.

Module Contents

dicee.evaluation.utils.DEFAULT_HITS_RANGE: List[int] = [1, 3, 10]
dicee.evaluation.utils.ALL_HITS_RANGE: List[int]
dicee.evaluation.utils.make_iterable_verbose(iterable_object: Iterable, verbose: bool, desc: str = 'Default', position: int | None = None, leave: bool = True) → Iterable[source]

Wrap an iterable with tqdm progress bar if verbose is True.

Parameters:
  • iterable_object – The iterable to potentially wrap.

  • verbose – Whether to show progress bar.

  • desc – Description for the progress bar.

  • position – Position of the progress bar.

  • leave – Whether to leave the progress bar after completion.

Returns:

The original iterable or a tqdm-wrapped version.

dicee.evaluation.utils.compute_metrics_from_ranks(ranks: List[int], num_triples: int, hits_dict: Dict[int, List[float]], scale_factor: int = 1) → Dict[str, float][source]

Compute standard link prediction metrics from ranks.

Parameters:
  • ranks – List of ranks for each prediction.

  • num_triples – Total number of triples evaluated.

  • hits_dict – Dictionary mapping hit levels to lists of hits.

  • scale_factor – Factor to scale the denominator (e.g., 2 for head+tail).

Returns:

Dictionary containing H@1, H@3, H@10, and MRR metrics.

dicee.evaluation.utils.compute_metrics_from_ranks_simple(ranks: List[int], num_triples: int, hits_dict: Dict[int, List[float]]) → Dict[str, float][source]

Compute link prediction metrics without scaling factor.

Parameters:
  • ranks – List of ranks for each prediction.

  • num_triples – Total number of triples evaluated.

  • hits_dict – Dictionary mapping hit levels to lists of hits.

Returns:

Dictionary containing H@1, H@3, H@10, and MRR metrics.

dicee.evaluation.utils.update_hits(hits: Dict[int, List[float]], rank: int, hits_range: List[int] | None = None) → None[source]

Update hits dictionary based on rank.

Parameters:
  • hits – Dictionary to update in-place.

  • rank – The rank to check against hit levels.

  • hits_range – List of hit levels to check (default: ALL_HITS_RANGE).

dicee.evaluation.utils.create_hits_dict(hits_range: List[int] | None = None) → Dict[int, List[float]][source]

Create an initialized hits dictionary.

Parameters:

hits_range – List of hit levels to initialize (default: ALL_HITS_RANGE).

Returns:

Dictionary with empty lists for each hit level.

dicee.evaluation.utils.efficient_zero_grad(model) → None[source]

Efficiently zero gradients using parameter.grad = None.

This is more efficient than optimizer.zero_grad() as it avoids memory operations.

See: https://pytorch.org/tutorials/recipes/recipes/tuning_guide.html

Parameters:

model – PyTorch model to zero gradients for.