Utils
API¶
rdworks.utils
¶
Functions:¶
QT(rmsd_matrix, threshold)
¶
Perform QT clustering.
Parameters:
-
rmsd_matrix(ndarray) –pairwise rmsd matrix.
-
threshold(float) –quality threshold (A)
Returns:
-
tuple(tuple) –(cluster assignment, centroid indices)
centroid_medoid(cluster_assignment, rmsd_matrix)
¶
Returns the medoid of each cluster.
The medoid is the member whose sum of distances to the other members of its cluster is minimal.
Parameters:
-
cluster_assignment(list) –cluster index of each item.
-
rmsd_matrix(ndarray) –symmetric pairwise distance matrix.
Returns:
-
list(list) –item index of the medoid of each cluster, in the order of
sorted(set(cluster_assignment)).
compress_string(data)
¶
Compress a string to a base64-encoded string.
Parameters:
-
data(str) –original string.
Raises:
-
TypeError–if
datais not str.
Returns:
-
str(str) –base64-encoded compressed string.
compute(fn, largs, **kwargs)
¶
Returns [fn(*args) for args in largs], optionally in parallel processes.
Parameters:
-
fn(Callable) –function (or class) to call; must be picklable for
max_workers > 1. -
largs(list) –a list of argument tuples, one tuple per call.
Other Parameters:
-
max_workers(int) –number of worker processes. Defaults to 1 (serial).
-
chunksize(int) –number of calls sent to a worker at once. Defaults to 10.
-
progress(bool) –whether to show a progress bar. Defaults to False.
-
desc(str) –progress bar description. Defaults to "Progress".
Returns:
-
list(list) –results in the order of
largs.
convert_tril_to_symm(lower_triangle_values)
¶
Converts lower triangle values to a symmetric full matrix.
Parameters:
-
lower_triangle_values(list) –list of lower triangle matrix values.
Returns:
-
ndarray–np.ndarray: numpy array of a symmetric full matrix.
convert_triu_to_symm(upper_triangle_values)
¶
Converts upper triangle values to a symmetric full matrix.
Parameters:
-
upper_triangle_values(list) –list of upper triangle matrix values.
Returns:
-
ndarray–np.ndarray: numpy array of a symmetric full matrix.
decompress_string(encoded_str)
¶
Decompress a base64-encoded string from compress_string() to the original string.
Whitespace (e.g. line breaks added when stored in a file) is ignored and missing base64 padding is added automatically.
Parameters:
-
encoded_str(str) –base64-encoded compressed string.
Raises:
-
TypeError–if
encoded_stris not str. -
ValueError–if
encoded_strcannot be decoded or decompressed.
Returns:
-
str(str) –original string.
deserialize(encoded_str)
¶
Decode, decompress, and deserialize a base64 string back to Python object.
Parameters:
-
encoded_str(str | bytes) –base64-encoded compressed JSON string from
serialize().
Raises:
-
TypeError–if
encoded_stris not str or bytes. -
ValueError–if
encoded_strcannot be decoded, decompressed, or parsed.
Returns:
-
Any(Any) –deserialized Python object.
dict_to_simplenamespace(data)
¶
fix_decimals_in_dict(in_dict, decimals=2)
¶
Fixes the decimal places of all float values in a dictionary.
Parameters:
-
in_dict(dict) –The dictionary to fix.
-
decimals(int, default:2) –The number of decimal places to fix the float values to.
Returns:
-
dict(dict) –a dictionary with the float values fixed to the specified number of decimal places.
fix_decimals_in_list(in_list, decimals=2)
¶
Fixes the decimal places of all float values in a list.
Parameters:
-
in_list(list) –The list to fix.
-
decimals(int, default:2) –The number of decimal places to fix the float values to.
Returns:
-
list(list) –a list with the float values fixed to the specified number of decimal places.
recursive_round(data, decimals=2)
¶
Recursively round float values to a given decimal places.
data: The input data, which can be a list, dictionary, or any other data type. It can contain nested lists and dictionaries. decimals: number of decimal places.
serialize(data)
¶
Serialize, compress, and encode data to a base64 string.
Notes
The JSON specification only supports string keys in objects. For example, after JSON-serialization/deserialization, keys of integer type are changed to string. {1: 'a', 2: 'b', 3: 'c'} --> {'1': 'a', '2': 'b', '3': 'c'} Unfortunately, this is a fundamental limitation of JSON itself. Integer keys are not valid JSON. Likewise, tuples are restored as lists: (1, 2) --> [1, 2]. numpy scalars and arrays (e.g. np.int64, np.float32, np.bool_, np.ndarray) are converted to native Python types (int, float, bool, list).
Parameters:
-
data(Any) –JSON-serializable Python object (numpy types allowed).
Raises:
-
TypeError–if
datacontains an object that cannot be serialized.
Returns:
-
str(str) –base64-encoded compressed JSON string.