Source code for hydra.utils.hydra

"""Utilities for the Hydra parser."""

from typing import Any

from datasets.features import ClassLabel, Features, List, Sequence, Value

from .constants import (
    SEGS,
    LEMMA_CASES,
    LEMMA_RULES,
    TAGS,
    HEADS,
    ADJACENCY_MATRIX,
    DEPRELS,
    GRAPH_ADJACENCY_MATRIX,
    GRAPH_DEPRELS,
    SPINES,
    ANCHORS,
    ANCHOR_HS,
    NE_IOBS,
)

def _maybe_int(s: str) -> str | int:
    if s and (s.isdigit() or s[0] == '-' and s[1:].isdigit()):
        return int(s)
    return s

def _keys_hook(d: dict[str, Any]) -> dict[str | int, Any]:
    return { _maybe_int(k) : v for k, v in d.items() }

[docs] def load_config(path: str) -> dict[str, Any]: """Load a trained Hydra model’s configuration. Returns: the config. """ with open(f'{path}/config.json') as f: config = json.load(f, object_hook=_keys_hook) return config
[docs] def category_names( segmentation: bool = False, lemmatisation: bool = False, tagging: bool = False, dependency: bool = False, graph: bool = False, hybrid: bool = False, nonterminal_features: list[str] | None = None, named_entities: bool = False ) -> list[str]: """Return the list of categories predicted for each token in a given parser configuration. Model output names will match those. Args: segmentation: enable segmentation module. lemmatisation: enable lemmatisation module. tagging: enable tagging module. dependency: enable dependency parsing module. graph: enable graph parsing module (extended/additional edges). hybrid: enable hybrid syntactic parsing module. nonterminal_features: nonterminal features to predict (experimental). named_entities: enable NER module. Returns: list of category names. """ categories: list[str] = [] if segmentation: categories.append(SEGS) if lemmatisation: categories.append(LEMMA_CASES) categories.append(LEMMA_RULES) if tagging: categories.append(TAGS) if dependency: categories.append(ADJACENCY_MATRIX) categories.append(DEPRELS) if graph: categories.append(GRAPH_ADJACENCY_MATRIX) categories.append(GRAPH_DEPRELS) if hybrid: categories.append(SPINES) categories.append(ANCHORS) categories.append(ANCHOR_HS) if nonterminal_features: categories += nonterminal_features if named_entities: categories.append(NE_IOBS) return categories
def _get_feature_labels(feature: ClassLabel | Sequence | List | Value) -> list[str] | None: if isinstance(feature, ClassLabel): return feature.names # type: ignore[no-any-return] elif isinstance(feature, (Sequence, List)): return _get_feature_labels(feature.feature) else: return None
[docs] def get_labels(features: Features, categories: list[str]) -> dict[str, list[str]]: """Collect possible label values for non-numeric categories from a dataset features signature. Returns: label values. """ labels = {} for cat in categories: _labels = _get_feature_labels(features[cat]) if _labels is not None: labels[cat] = _labels return labels
[docs] def get_param(config: dict[str, Any], key: str, alt_keys: list[str] | None = None, default: Any = None, allow_none: bool = False) -> Any: """Retrieve parameter value from a trained model configuration. Args: config: a model configuration. key: parameter to look up. alt_keys: alternative/fallback keys for backward compatibility (warning isssued if used). default: value to return if key is missing. allow_none: admit `None` as parameter value. Returns: the parameter value. :raises KeyError: if `key` and `alt_keys` are missing, `default=None` and `allow_none=False`. """ if key in config: return config[key] if alt_keys: for k in alt_keys: if k in config: logger.warning(f'Warning: `{k}` parameter in parser config is obsolete and has been renamed to `{key}`. Setting `{key}` to `{k}` value for now.') return config[k] if default is not None or allow_none: return default raise KeyError(f'`{key}` parameter missing from config.')