Utilities

class rhodent.typing.DistributedArray

Array that contains data on the root rank of the calculation communicator. Acts as an empty numpy array on other ranks.

Is implemented as numpy.ndarray on the root rank and ArrayIsOnRootRank on other ranks.

class rhodent.utils.Logger(t0=None)[source]
Parameters:

t0 (Optional[float]) – Start time (default is current time).

log(*args, **kwargs)[source]

Log message, prepending a timestamp.

startup_message()[source]

Print a start up message.

class rhodent.utils.ParallelMatrix(shape, dtype, comm=None, array=None)[source]

Distributed array, with data on the root rank.

Parameters:
  • shape (tuple[int, …]) – Shape of array.

  • dtype (DTypeLike[DTypeT]) – Dtype of array.

  • comm (Communicator | None) – MPI communicator.

  • array (NDArray[DTypeT] | None) – Array on root rank of the communicator. Must be None on other ranks.

property array: ndarray[tuple[int, ...], dtype[DTypeT]]

Array with data. May only be called on the root rank.

broadcast()[source]

Broadcasted data.

Return type:

ndarray[tuple[int, ...], dtype[TypeVar(DTypeT, bound= generic, covariant=True)]]

property root: bool

Whether this rank is the root rank.

class rhodent.utils.Result(mutable=False)[source]

Class holding results.

class rhodent.utils.ResultKeys(*scalar_keys)[source]

List of result keys.

add_key(key, shape=(), dtype=<class 'float'>)[source]

Add a new result key.

Parameters:
  • key (str) – Name of result.

  • shape (tuple[int, ...] | int) – Shape of result (at one time or frequency instance). Default is scalar.

  • dtype (Union[dtype[Any], None, type[Any], _SupportsDType[dtype[Any]], str, tuple[Any, int], tuple[Any, SupportsIndex | Sequence[SupportsIndex]], list[Any], _DTypeDict, tuple[Any, Any]]) – Result dtype.

rhodent.utils.find_files(fmt, log=None, *, expected_keys)[source]

Find files in file system matching the format string fmt.

This function walks the file tree and looks for file names matching the format string fmt.

Parameters:
  • fmt (str) – Format string.

  • log (Optional[Logger]) – Optional logger object.

  • expected_keys (list[str]) – List of replacement fields {...} that are expected to be parsed from the file names. Unexpected fields raise ValueError.

Returns:

filename - List of filenames found. key - List of parsed value for each key in expected_keys.

Return type:

Dictionary with keys, sorted by the parsed values matching expected_keys

Example

>>> fmt = 'pulserho_pf3.80/t{time:09.1f}{tag}.npy'
>>> find_files(fmt, expected_keys=['time', 'tag'])
{'filename': ['pulserho_pf3.80/t0000010.0.npy',
              'pulserho_pf3.80/t0000010.0-Iomega.npy',
              'pulserho_pf3.80/t0000060.0.npy',
              'pulserho_pf3.80/t0000060.0-Iomega.npy'],
 'time': [10.0, 10.0, 60.0, 60.0],
 'tag': ['', '-Iomega', '', '-Iomega']}
rhodent.utils.format_string_to_glob(fmt)[source]

Convert a format string to a glob-type expression.

Replaces all the replacement fields {...} in the format string with a glob *.

Example

>>> format_string_to_glob('pulserho_pf{pulsefreq:.2f}/t{time:09.1f}{tag}.npy')
pulserho_pf*/t*.npy
Parameters:

fmt (str) – Format string.

Return type:

Glob-type expression.

rhodent.utils.format_string_to_regex(fmt)[source]

Convert a format string to a regex expression.

Replaces all the replacement fields {...} in the format string with a regular expression and escapes all special characters outside the replacement fields.

Replacement fields for variables time, freq, pulsefreq and pulsefwhm are replaced by regex matching floating point numbers. Replacement fields for variables reim and tag are replaced by regex matching alphabetic characters and dashes. Remaining replacement fields are replaced by regex matching alphabetic characters.

This can be used to parse a formatted string in order to get back the original values.

Example

>>> fmt = 'pulserho_pf{pulsefreq:.2f}/t{time:09.1f}{tag}.npy'
>>> s = fmt.format(pulsefreq=3.8, time=30000, tag='-Iomega')
pulserho_pf3.80/t0030000.0-Iomega.npy
>>> regex = format_string_to_regex(fmt)
re.compile('pulserho_pf(?P<pulsefreq>[-+]?[\d.]+)/t(?P<time>[-+]?[\d.]+)(?P<tag>[-A-za-z]*)\.npy')
>>> regex.fullmatch(s).groupdict()
{'pulsefreq': '3.80', 'time': '0030000.0', 'tag': '-Iomega'}
Parameters:

fmt (str) – Format string.

Return type:

Compiled regex pattern.

Notes

Replacement fields should be named and not contain any attributes or indexing.

rhodent.utils.partial_format(fmt, **kwargs)[source]

Partially format the format string.

Equivalent to calling fmt.format(**kwargs) but replacement fields that are not present in the **kwargs will be left in the format string.

Parameters:
  • fmt – Format string.

  • **kwargs – Passed to the str.format() call.

Return type:

Partially formatted string.

Example

>>> fmt = 'pulserho_pf{pulsefreq:.2f}/t{time:09.1f}{tag}.npy'
>>> partial_format(fmt, pulsefreq=3.8)
pulserho_pf3.80/t{time:09.1f}{tag}.npy
rhodent.utils.env.rhodent_getenv(variable)[source]

Get value of environment variable, or default.

Possible environment variables are:

  • RHODENT_RESPONSE_MAX_MEM - Value in units of MiB. When constructing response, try to limit memory usage below this value.

  • RHODENT_RESPONSE_MAX_MEM_PER_RANK - Value in units of MiB. Ignored if RHODENT_RESPONSE_MAX_MEM is set.

  • RHODENT_REDISTRIBUTE_MAXSIZE - Maximal number of elements in single redistribute call. Larger arrays are split.

Parameters:

variable – Name of variable, without the 'RHODENT_' prefix.

Return type:

Value of variable as string.

class rhodent.utils.memory.HasMemoryEstimate[source]

Classes inheriting from this class are able to provide a memory estimate

class rhodent.utils.memory.MemoryEntry(shape, dtype, on_num_ranks=1, total_size=None)[source]
get_total_size()[source]

Get the total number of elements on all ranks.

Return type:

int

class rhodent.utils.memory.MemoryEstimate(comment='', children=<factory>, arrays=<factory>)[source]
property grand_total: int

Grand total of bytes.

class rhodent.utils.result.Result(mutable=False)[source]

Class holding results.

class rhodent.utils.result.ResultKeys(*scalar_keys)[source]

List of result keys.

add_key(key, shape=(), dtype=<class 'float'>)[source]

Add a new result key.

Parameters:
  • key (str) – Name of result.

  • shape (tuple[int, ...] | int) – Shape of result (at one time or frequency instance). Default is scalar.

  • dtype (Union[dtype[Any], None, type[Any], _SupportsDType[dtype[Any]], str, tuple[Any, int], tuple[Any, SupportsIndex | Sequence[SupportsIndex]], list[Any], _DTypeDict, tuple[Any, Any]]) – Result dtype.