napistu_torch.utils.base_utils
General utilities for the Python standard library.
Classes
- CorruptionError
Raised when MPS memory corruption is detected.
Public Functions
- ensure_path(path: Union[str, Path], expand_user: bool = True) -> Path
Convert a string or Path to a Path object, optionally expanding user home directory.
- normalize_and_validate_indices(indices: Union[int, List[int], tuple, range], max_value: int, param_name: str = “indices”) -> List[int]
Normalize indices to a list and validate they are integers in valid range.
- shortest_common_prefix(names: List[str], min_length: int = 3) -> str
Find shortest common prefix respecting word boundaries.
Functions
|
Convert a string or Path to a Path object, optionally expanding user home directory. |
|
Normalize indices to a list and validate they are integers in valid range. |
|
Find shortest common prefix respecting word boundaries. |
Exceptions
Raised when MPS memory corruption is detected. |
- exception napistu_torch.utils.base_utils.CorruptionError
Bases:
ValueErrorRaised when MPS memory corruption is detected.
- napistu_torch.utils.base_utils.ensure_path(path: str | Path, expand_user: bool = True) Path
Convert a string or Path to a Path object, optionally expanding user home directory.
- Parameters:
path (Union[str, Path]) – Path to convert. Can be a string (e.g., “~/data/store”) or Path object.
expand_user (bool, default=True) – If True, expand tildes (~) to the user’s home directory.
- Returns:
Path object, with user expanded if expand_user=True.
- Return type:
Path
- Raises:
TypeError – If path is not a str or Path object.
Examples
>>> ensure_path("~/data/store") PosixPath('/home/user/data/store') >>> ensure_path(Path("./relative/path")) PosixPath('./relative/path') >>> ensure_path("~/data", expand_user=False) PosixPath('~/data')
- napistu_torch.utils.base_utils.normalize_and_validate_indices(indices: int | List[int] | tuple | range, max_value: int, param_name: str = 'indices') List[int]
Normalize indices to a list and validate they are integers in valid range.
- Parameters:
indices (int, List[int], tuple, or range) – Indices to normalize and validate. Can be a single integer, list, tuple, or range.
max_value (int) – Maximum valid index value (exclusive). Valid range is [0, max_value).
param_name (str, optional) – Name of the parameter for error messages (default: “indices”)
- Returns:
Normalized list of validated indices
- Return type:
List[int]
- Raises:
ValueError – If indices are invalid (wrong type, not integers, out of range, or duplicates)
- napistu_torch.utils.base_utils.shortest_common_prefix(names: List[str], min_length: int = 3) str
Find shortest common prefix respecting word boundaries.
- Parameters:
names (List[str]) – Feature names to find common prefix for
min_length (int, default=3) – Minimum acceptable prefix length
- Returns:
Shortest common prefix, or alphabetically first name if prefix too short
- Return type:
str
Examples
>>> shortest_common_prefix(['is_string_x', 'is_string_y']) 'is_string' >>> shortest_common_prefix(['is_omnipath_kinase', 'is_omnipath_phosphatase']) 'is_omnipath' >>> shortest_common_prefix(['is_a', 'is_b']) # Too short 'is_a'