neuroglancer_scripts.utils module

Miscellaneous utility functions.

neuroglancer_scripts.utils.LENGTH_UNITS = {'km': 1e-12, 'm': 1e-09, 'mm': 1e-06, 'nm': 1.0, 'pm': 1000.0, 'um': 0.001}

List of physical units of length.

neuroglancer_scripts.utils.ceil_div(a, b)[source]

Ceil integer division (ceil(a / b) using integer arithmetic).

neuroglancer_scripts.utils.format_length(length_nm, unit)[source]

Format a length according to the provided unit (input in nanometres).

Parameters:
  • length_nm (float) – a length in nanometres

  • unit (str) – must be one of LENGTH_UNITS.keys

Returns:

the formatted length, rounded to the specified unit (no fractional part is printed)

Return type:

str

neuroglancer_scripts.utils.invert_permutation(p)[source]

Invert a permutation.

Parameters:

p – a permutation (sequence of integers between 0 and len(p) - 1)

Returns:

an array s, where s[i] gives the index of i in p

Return type:

numpy.ndarray

neuroglancer_scripts.utils.permute(seq, p)[source]

Permute the elements of a sequence according to a permutation.

Parameters:
  • seq – a sequence to be permuted

  • p – a permutation (sequence of integers between 0 and len(seq) - 1)

Returns:

tuple(seq[i] for i in p)

Return type:

tuple

neuroglancer_scripts.utils.readable_count(count)[source]

Format a number to a human-readable string with an IEC binary prefix.

The resulting string has a minimum of 2 significant digits. It is never longer than 6 characters, unless the input exceeds 2**60. You are expected to concatenate the result with the relevant unit (e.g. B for bytes):

>>> readable_count(512) + "B"
'512 B'
>>> readable_count(1e10) + "B"
'9.3 GiB'
Parameters:

count (int) – number to be converted (must be >= 0)

Returns:

a string representation of the number with an IEC binary prefix

Return type:

str