osl_dynamics.utils.misc#

Miscellaneous utility classes and functions.

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.

top_eig(M, k)

Compute the top-k eigenvalues and eigenvectors of a symmetric matrix.

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 argument.

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[, op_determinism])

Set all random seeds.

system_call(cmd[, verbose])

Run a shell command.

setup_fsl(directory)

Setup FSL.

Module Contents#

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.top_eig(M, k)[source]#

Compute the top-k eigenvalues and eigenvectors of a symmetric matrix.

This function uses either scipy.sparse.linalg.eigsh (for large matrices and a smaller number of eigenvectors) or numpy.linalg.eigh (for smaller matrices or full eigendecomposition).

Parameters:
  • M (np.ndarray) – Symmetric matrix of shape (n, n).

  • k (int) – Number of top eigenvalues/eigenvectors to compute.

Returns:

  • vals (np.ndarray) – Top-k eigenvalues. Shape is (k,).

  • vecs (np.ndarray) – Corresponding eigenvectors as columns. Shape is (n, k).

Return type:

Tuple[numpy.ndarray, numpy.ndarray]

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 (Any) – 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.

Return type:

Tuple[list, dict]

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

Get argument.

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 (Any) – 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?

c_contiguous = True[source]#
f_contiguous[source]#
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?

shape[source]#
dtype[source]#
flags[source]#
filename = None[source]#
save(filename)[source]#
Parameters:

filename (str)

Return type:

None

memmap()[source]#
Return type:

numpy.memmap

classmethod to_disk(filename, shape, dtype=np.float64, c_contiguous=True)[source]#
Parameters:
  • filename (str)

  • shape (List[int])

  • dtype (type)

  • c_contiguous (bool)

Return type:

None

classmethod get_memmap(filename, shape, dtype=np.float64, c_contiguous=True)[source]#
Parameters:
  • filename (str)

  • shape (List[int])

  • dtype (type)

  • c_contiguous (bool)

Return type:

numpy.memmap

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.

Return type:

None

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, op_determinism=False)[source]#

Set all random seeds.

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

Parameters:
  • seed (int) – Random seed.

  • op_determinism (bool, optional) – Whether to enable operation determinism in TensorFlow. If True, TensorFlow operations will be deterministic, generally at the cost of lower performance. Note that the model may run slower if enabled.

Return type:

None

osl_dynamics.utils.misc.system_call(cmd, verbose=True)[source]#

Run a shell command.

Parameters:
  • cmd (str) – Command to execute.

  • verbose (bool, optional) – Print the command before executing.

Return type:

None

osl_dynamics.utils.misc.setup_fsl(directory)[source]#

Setup FSL.

Parameters:

directory (str) – Path to FSL installation.