booldog.utils.misc
Attributes
Classes
enum.Enum subclass that adds a values classmethod for retrieving |
Functions
Coerce v to a numpy.ndarray. |
|
|
Parameter argument to numpy array |
|
Checks if path is writeable. If not, attempts to provide reason, and raises |
Get version of booldog package |
Module Contents
- booldog.utils.misc.logger
- class booldog.utils.misc.ExtendedEnum(*args, **kwds)
Bases:
enum.Enumenum.Enum subclass that adds a values classmethod for retrieving all member values as a plain list.
Used as the base class for booldog.boolean.modifications.ModificationTypes.
- classmethod values()
Return the values of all members of this enum.
- Returns:
The
.valueof each member of the enum, in definition order.- Return type:
list
- booldog.utils.misc.ensure_ndarray(v)
Coerce v to a numpy.ndarray.
- Parameters:
v (numpy.ndarray, int, float, or iterable) – The value to coerce. If v is already a numpy.ndarray it is returned unchanged. If v is an int or float, a length-1 array containing v is returned. Otherwise, v is assumed to be an iterable (e.g. list, tuple, generator) and is unpacked into a new array via
np.array([*v]).- Returns:
v coerced to an array, as described above.
- Return type:
numpy.ndarray
- booldog.utils.misc.parameter_to_array(parameter, graph_keys)
Parameter argument to numpy array
- Parameters:
parameter (array, int, float or dict) –
- if array:
make sure length is n
- if int or float:
return an array of length n with value
- if dict:
returns an array of length n with values set according to keys (nodes indexed by graph_keys), and the rest set to ‘default’ key
graph_keys (dict) – Mapping from node identifier to its (0-based) index in the returned array, e.g. booldog.network.BoolDogModel.index.
len(graph_keys)determines n, the length of the returned array; for a dict parameter, its keys are looked up in graph_keys to find which index to assign each value to.
- Returns:
parameter_array – array of length n
- Return type:
numpy array
Notes
If parameter is already a numpy.ndarray of length
len(graph_keys), it is returned unchanged, bypassing all the cases below. Otherwise, an all-ones array of lengthlen(graph_keys)is built and then:if parameter is an
int/float, every entry is set to that value;if parameter is a
dict, every entry is first set toparameter['default']if a'default'key is present, then entries are overwritten per-key from parameter (using graph_keys to map each key to its index); keys of parameter absent from graph_keys raise KeyError, and keys of graph_keys absent from parameter keep the all-ones/default value;for any other input – including a numpy.ndarray whose length does not match
len(graph_keys)– a warning is logged and an all-ones array is returned, silently ignoring the supplied value.
- booldog.utils.misc.file_writable(path)
Checks if path is writeable. If not, attempts to provide reason, and raises an Exception.
- Parameters:
path (str or Path) – Path to file to check for writability. If the file does not exist, the function will check if it can be created.
- Return type:
None
- Raises:
IOError – If path cannot be opened for writing. If the underlying error is a permission error (
errno.EACCES) or path is a directory (errno.EISDIR), a descriptive message is logged first via logging.Logger.error; the original exception is then re-raised unchanged in all cases.
Notes
A warning is logged (but no exception raised) if path already exists, since it will be overwritten.
If path already exists, the check opens it in
'r+b'mode (read and write, no truncation) so its existing content is left untouched — only if path does not yet exist is it created viaopen(path, 'wb')as an empty file.
- booldog.utils.misc.get_pkg_version()
Get version of booldog package
- Returns:
version – Version of booldog package
- Return type:
str
Notes
Reads the version from the installed
BoolDogdistribution’s metadata via importlib.metadata.version. This requires the package to be installed (e.g. via pip/poetry) with its distribution metadata available; it will raise importlib.metadata.PackageNotFoundError ifbooldoghas no installed distribution metadata to read.