Common Utilities#
Common utility functions used throughout the project.
- class atomworks.common.KeyToIntMapper[source]#
Bases:
object
Maps keys to unique integers based on the order of the first appearance of the key.
This is useful for mapping id’s such as chain_id, chain_entity, molecule_iid, etc. to integers.
Example
>>> chain_id_to_int = KeyToIntMapper() >>> chain_id_to_int("A") # 0 >>> chain_id_to_int("C") # 1 >>> chain_id_to_int("A") # 0 >>> chain_id_to_int("B") # 2
- atomworks.common.as_list(value: Any) list [source]#
Convert a value to a list.
- Handles various types using duck typing:
Iterable objects (lists, tuples, strings, etc.): converted to list
Single values: wrapped in a list
- Parameters:
value – The value to convert to a list.
- Returns:
A list containing the value(s).
- atomworks.common.default(obj: Any, default: Any) Any [source]#
Return obj if not None, otherwise return default.
- Parameters:
obj – The primary object to return.
default – The fallback value if obj is None.
- Returns:
obj if it is not None, otherwise default.
- atomworks.common.exists(obj: Any) bool [source]#
Check that obj is not None.
- Parameters:
obj – The object to check.
- Returns:
True if obj is not None, False otherwise.
- atomworks.common.immutable_lru_cache(maxsize: int = 128, typed: bool = False, deepcopy: bool = True) Callable [source]#
An immutable version of lru_cache for caching functions that return mutable objects.
- Parameters:
maxsize – Maximum number of items to cache.
typed – Whether to treat different types as separate cache entries.
deepcopy – Whether to use deep copy for immutable caching.
- Returns:
A decorator that provides immutable caching functionality.
- atomworks.common.listmap(func: Callable, *iterables) list [source]#
Like map, but returns a list instead of an iterator.
- Parameters:
func – The function to apply.
*iterables – Variable number of iterables to map over.
- Returns:
A list containing the results of applying func to the iterables.
- atomworks.common.not_isin(element: ndarray, array: ndarray, **isin_kwargs) ndarray [source]#
Like ~np.isin, but more efficient.
- Parameters:
element – The array to test.
array – The array of values to test against.
**isin_kwargs – Additional keyword arguments for np.isin.
- Returns:
Boolean array indicating which elements are not in the array.
- atomworks.common.string_to_md5_hash(s: str, truncate: int = 32) str [source]#
Generate an MD5 hash of a string and return the first truncate characters.
- Parameters:
s – The string to hash.
truncate – Number of characters to return from the hash.
- Returns:
The truncated MD5 hash as a string.
- atomworks.common.sum_string_arrays(*objs: ndarray | str) ndarray [source]#
Sum a list of string arrays or strings into a single string array.
Concatenates the arrays and determines the shortest string length to set as dtype.
- Parameters:
*objs – Variable number of string arrays or strings to sum.
- Returns:
A single concatenated string array.