osl_dynamics.utils.misc#

Miscellaneous utility classes and functions.

Module Contents#

Classes#

MockFlags

Flags for memmap header construction.

MockArray

Create an empty array on disk without creating it in memory.

NumpyLoader

Functions#

nextpow2(x)

Next power of 2.

leading_zeros(number, largest_number)

Pad a number with leading zeros.

override_dict_defaults(default_dict[, override_dict])

Helper function to update default dictionary values with user values.

listify(obj)

Create a list from any input.

replace_argument(func, name, item, args, kwargs[, append])

Replace arguments in function calls.

get_argument(func, name, args, kwargs)

Get an argument passed to a function call whether it is a normal

check_arguments(args, kwargs, index, name, value, ...)

Checks the arguments passed to a function.

array_to_memmap(filename, array)

Save an array and reopen it as a np.memmap.

save(filename, array)

Save a file.

load(filename, **kwargs)

Load a file.

set_random_seed(seed)

Set all random seeds.

set_logging_level(logger, level)

Attributes#

_logger

osl_dynamics.utils.misc._logger[source]#
osl_dynamics.utils.misc.nextpow2(x)[source]#

Next power of 2.

Parameters:

x (int) – Any integer.

Returns:

res – The smallest power of two that is greater than or equal to the absolute value of x.

Return type:

int

osl_dynamics.utils.misc.leading_zeros(number, largest_number)[source]#

Pad a number with leading zeros.

This is useful for creating a consistent naming scheme for files.

Parameters:
  • number (int) – Number to be padded.

  • largest_number (int) – Largest number in the set.

Returns:

padded_number – Number padded with leading zeros.

Return type:

str

osl_dynamics.utils.misc.override_dict_defaults(default_dict, override_dict=None)[source]#

Helper function to update default dictionary values with user values.

Parameters:
  • default_dict (dict) – Dictionary of default values.

  • override_dict (dict, optional) – Dictionary of user values.

Returns:

new_dict – default_dict with values replaced by user values.

Return type:

dict

osl_dynamics.utils.misc.listify(obj)[source]#

Create a list from any input.

If None is passed, return an empty list. If a list is passed, return the list. If a tuple is passed, return it as a list. If any other object is passed, return it as a single item list.

Parameters:

obj (Any) – Object to be transformed to a list.

Return type:

Object as a list.

osl_dynamics.utils.misc.replace_argument(func, name, item, args, kwargs, append=False)[source]#

Replace arguments in function calls.

Parameters:
  • func (callable) – The function being called.

  • name (str) – Name of the variable to be modified.

  • item – The value to be added.

  • args (dict) – Original arguments.

  • kwargs (dict) – Original keyword arguments.

  • append (bool, optional) – Whether the value should be appended or replace the existing argument.

Returns:

  • args (list) – Arguments.

  • kwargs (dict) – Keyword arguments.

osl_dynamics.utils.misc.get_argument(func, name, args, kwargs)[source]#

Get an argument passed to a function call whether it is a normal argument or keyword argument.

Parameters:
  • func (callable) – The function being called.

  • name (str) – Name of the variable to be modified.

  • args (dict) – Arguments.

  • kwargs (dict) – Keyword arguments.

Returns:

args – Argument.

Return type:

argument

osl_dynamics.utils.misc.check_arguments(args, kwargs, index, name, value, comparison_op)[source]#

Checks the arguments passed to a function.

Parameters:
  • args (list) – Arguments.

  • kwargs (dict) – Keyword arguments.

  • index (int) – Index of argument.

  • name (str) – Name of keyword argument.

  • value – Value to compare to given argument.

  • comparison_op (func) – Comparison operation for checking the original.

Returns:

valid – If the given value is valid as determined by the comparison operation.

Return type:

bool

osl_dynamics.utils.misc.array_to_memmap(filename, array)[source]#

Save an array and reopen it as a np.memmap.

Parameters:
  • filename (str) – The name of the file to save to.

  • array (np.ndarray) – The array to save.

Returns:

memmap – Memory map.

Return type:

np.memmap

class osl_dynamics.utils.misc.MockFlags(shape, c_contiguous=True)[source]#

Flags for memmap header construction.

Parameters:
  • shape (list of int) – The shape of the array being mapped.

  • c_contiguous (bool, optional) – Is the array C contiguous or F contiguous?

class osl_dynamics.utils.misc.MockArray(shape, dtype=np.float64, c_contiguous=True)[source]#

Create an empty array on disk without creating it in memory.

Parameters:
  • shape (list of int) – Dimensions or the array being created.

  • dtype (type) – The data type of the array.

  • c_contiguous (bool, optional) – Is the array C contiguous or F contiguous?

save(filename)[source]#
memmap()[source]#
classmethod to_disk(filename, shape, dtype=np.float64, c_contiguous=True)[source]#
classmethod get_memmap(filename, shape, dtype=np.float64, c_contiguous=True)[source]#
class osl_dynamics.utils.misc.NumpyLoader(stream)[source]#

Bases: yaml.UnsafeLoader

find_python_name(name, mark, unsafe=False)[source]#
osl_dynamics.utils.misc.save(filename, array)[source]#

Save a file.

Parameters:
  • filename (str) – Path to file to save to. Must be ‘.npy’ or ‘.pkl’.

  • array (np.ndarray or list) – Array to save.

osl_dynamics.utils.misc.load(filename, **kwargs)[source]#

Load a file.

Parameters:

filename (str) – Path to file to load. Must be ‘.npy’ or ‘.pkl’.

Returns:

array – Array loaded from the file.

Return type:

np.ndarray or list

osl_dynamics.utils.misc.set_random_seed(seed)[source]#

Set all random seeds.

This includes Python’s random module, NumPy and TensorFlow.

Parameters:

seed (int) – Random seed.

osl_dynamics.utils.misc.set_logging_level(logger, level)[source]#