pestifer.util.util module

Various utility functions for pestifer

pestifer.util.util.cell_from_xsc(xsc: Path | str)[source]

Reads a NAMD extended system configuration file and returns the box vectors and origin.

Parameters:

xsc (str) – The path to the NAMD extended system configuration file.

Returns:

A tuple containing the box vectors and origin, or (None, None) if the file is not found or invalid.

Return type:

tuple

pestifer.util.util.cell_to_xsc(box: ndarray, orig: ndarray, xsc: Path | str)[source]

Writes the box vectors and origin to a NAMD extended system configuration file.

Parameters:
  • box (np.ndarray) – A 3x3 array representing the box vectors.

  • orig (np.ndarray) – A 3-element array representing the origin of the box.

  • xsc (str) – The path to the NAMD extended system configuration file to be written.

pestifer.util.util.countTime(fn)[source]

Decorator to measure the time taken by a function to execute. It logs the time taken and the average time per call, as well as the number of calls made to the function.

Parameters:

fn (callable) – The function to be decorated. It can be any callable object, such as a function or a method.

Returns:

A wrapper function that measures the execution time of the original function.

Return type:

callable

pestifer.util.util.flatten(current: dict, key: str, result: dict) dict[source]

Recursively flattens a nested dictionary into a single-level dictionary with keys as dot-separated paths.

Parameters:
  • current (dict) – The current dictionary to flatten.

  • key (str) – The base key to prepend to the flattened keys.

  • result (dict) – The dictionary to store the flattened key-value pairs.

pestifer.util.util.hms(seconds: float) str[source]
pestifer.util.util.hmsf(seconds: float, precision: int = 3) str[source]

Return HH:MM:SS.sss with seconds as float (no 24h wrap).

pestifer.util.util.is_periodic(xsc: Path | str)[source]

Checks to see if the contents of the NAMD output xsc indicate that the current system is periodic

Parameters:

xsc (str) – The path to the NAMD extended system configuration file.

Returns:

True if the xsc file contains periodic box vectors, False otherwise.

Return type:

bool

pestifer.util.util.protect_str_arg(arg: str | None)[source]

Returns a string with spaces replaced by underscores.

Parameters:

arg (str or None) – The string to be processed. If None, returns an empty string.

Returns:

The processed string with spaces replaced by underscores.

Return type:

str

pestifer.util.util.reduce_intlist(L: list[int]) str[source]

Generate a “reduced-byte” representation of a list of integers by collapsing runs of adjacent integers into ‘i to j’ format. Example:

[1,2,3,4,5,7,8,9,10,12] -> ‘1 to 5 7 to 10 12’

Parameters:

L (list) – The list of integers to be processed.

Returns:

The reduced representation as a string.

Return type:

str

pestifer.util.util.remove_argument(parser: ArgumentParser, name_or_flag: str)[source]

Remove an argument by its dest name or option string.

pestifer.util.util.replace(data: list | dict, match: str, repl: str) None[source]

Recursive value search-and-replace; data is either list or dictionary; nesting is ok

Parameters:
  • data (dict or list) – The data structure in which to search and replace values.

  • match (str) – The string to match in the data structure. If a value matches this string, it will be replaced.

  • repl (str) – The string to replace the matched value with. If a value contains the match string, it will be replaced with this string.

pestifer.util.util.running_under_pytest() bool[source]
pestifer.util.util.tarball_walk(tar)[source]

Simulates os.walk() for tarball contents. (chatgpt)

Parameters:

tar (tarfile.TarFile) – A tarfile object representing the tarball to walk through.

Yields:

tuple – A tuple containing the directory path, a list of subdirectories, and a list of files in the current directory.

pestifer.util.util.write_residue_map(the_map: dict, filename: str) None[source]

Writes a flattened map of residue objects to a file. The map is flattened such that each key is a dot-separated string representing the path to the value, and the values are the attributes of the residue objects specified by valkeys.

Parameters:
  • the_map (dict) – A dictionary where keys are strings of the form <chainID>:<resid> as labelled in a CIF file, and values are strings of the form <chainID>:<resid> as labelled by the “auth” attributes.

  • filename (str) – The name of the file to which the flattened map will be written.

  • valkeys (list of str, optional) – A list of attribute names to be extracted from the residue objects. Default is [chainID, resid].