nanshe.util.xnumpy module

The module xnumpy provides useful functions in combination with numpy.

Overview

The module xnumpy provides some addition useful functions that are useful in conjunction with numpy. The functions provided vary from handling view, adding calculations, combining arrays in interesting ways, handling masks, etc.

API

exception nanshe.util.xnumpy.NotNumPyStructuredArrayType[source]

Bases: exceptions.Exception

Designed for being thrown if a NumPy Structured Array is received.

nanshe.util.xnumpy.add_singleton_axis_beginning(*args, **kwargs)[source]

Adds a singleton axis to the beginning of the array.

Parameters:new_array (numpy.ndarray) – array to add the singleton axis to.
Returns:
a numpy array with the
singleton axis added at the end (should be view)
Return type:(numpy.ndarray)

Examples

>>> add_singleton_axis_beginning(numpy.ones((7,9,6))).shape
(1, 7, 9, 6)
>>> add_singleton_axis_beginning(numpy.eye(3)).shape
(1, 3, 3)
nanshe.util.xnumpy.add_singleton_axis_end(*args, **kwargs)[source]

Adds a singleton axis to the end of the array.

Parameters:new_array (numpy.ndarray) – array to add the singleton axis to.
Returns:
a numpy array with the
singleton axis added at the end (should be view)
Return type:(numpy.ndarray)

Examples

>>> add_singleton_axis_end(numpy.ones((7,9,6))).shape
(7, 9, 6, 1)
>>> add_singleton_axis_end(numpy.eye(3)).shape
(3, 3, 1)
nanshe.util.xnumpy.add_singleton_axis_pos(*args, **kwargs)[source]

Adds a singleton axis to the given position.

Allows negative values for axis. Also, automatically bounds axis in an acceptable regime if it is not already.

Parameters:
  • a_array (numpy.ndarray) – array to add the singleton axis to.
  • axis (int) – position for the axis to be in the final array (defaults to zero).
Returns:

a numpy array with the singleton

axis added (should be a view).

Return type:

(numpy.ndarray)

Examples

>>> add_singleton_axis_pos(numpy.ones((7,9,6))).shape
(1, 7, 9, 6)
>>> add_singleton_axis_pos(numpy.ones((7,9,6)), 0).shape
(1, 7, 9, 6)
>>> add_singleton_axis_pos(numpy.ones((7,9,6)), axis = 0).shape
(1, 7, 9, 6)
>>> add_singleton_axis_pos(numpy.ones((7,9,6)), 1).shape
(7, 1, 9, 6)
>>> add_singleton_axis_pos(numpy.ones((7,9,6)), 2).shape
(7, 9, 1, 6)
>>> add_singleton_axis_pos(numpy.ones((7,9,6)), 3).shape
(7, 9, 6, 1)
>>> add_singleton_axis_pos(numpy.ones((7,9,6)), -1).shape
(7, 9, 6, 1)
>>> add_singleton_axis_pos(numpy.ones((7,9,6)), -2).shape
(7, 9, 1, 6)
nanshe.util.xnumpy.add_singleton_op(*args, **kwargs)[source]

Performs an operation on the given array on the specified axis, which otherwise would have eliminated the axis in question. This function will instead ensure that the given axis remains after the operation as a singleton.

Note

The operation must be able to take only two arguments where the first is the array and the second is the axis to apply the operation along.

Parameters:
  • op (callable) – callable that takes a numpy.ndarray and an int in order.
  • new_array (numpy.ndarray) – array to perform operation on and add singleton axis too.
  • axis (int) – the axis to apply the operation along and turn into a singleton.
Returns:

the array with the operation

performed.

Return type:

(numpy.ndarray)

Examples

>>> add_singleton_op(numpy.max, numpy.ones((7,9,6)), 0).shape
(1, 9, 6)
>>> add_singleton_op(numpy.max, numpy.ones((7,9,6)), -1).shape
(7, 9, 1)
>>> add_singleton_op(numpy.min, numpy.ones((7,9,6)), 1).shape
(7, 1, 6)
>>> add_singleton_op(numpy.mean, numpy.ones((7,9,6)), 1).shape
(7, 1, 6)
nanshe.util.xnumpy.all_permutations_equal(*args, **kwargs)[source]

Takes two arrays and constructs a new array that contains the result of equality comparison on every permutation of elements in each array (like broadcasting).

Suppose that new_result contained the result, then one would find that the result of the following operation on the specific indices

new_op( new_array_1[ i_1_1, i_1_2, … ], new_array_2[ i_2_1, i_2_2, … ] )

would be found in new_result as shown

new_result[ i_1_1, i_1_2, …, i_2_1, i_2_2, … ]

Parameters:new_array (numpy.ndarray) – array to add the singleton axis to.
Returns:
a numpy array with the
singleton axis added at the end.
Return type:(numpy.ndarray)

Examples

>>> all_permutations_equal(numpy.ones((1,3)), numpy.eye(2)).shape
(1, 3, 2, 2)
>>> all_permutations_equal(numpy.ones((2,2)), numpy.eye(2)).shape
(2, 2, 2, 2)
>>> all_permutations_equal(numpy.ones((2,2)), numpy.eye(2))
array([[[[ True, False],
         [False,  True]],
<BLANKLINE>
        [[ True, False],
         [False,  True]]],
<BLANKLINE>
<BLANKLINE>
       [[[ True, False],
         [False,  True]],
<BLANKLINE>
        [[ True, False],
         [False,  True]]]], dtype=bool)
>>> all_permutations_equal(
...     numpy.ones((2,2)), numpy.fliplr(numpy.eye(2))
... )
array([[[[False,  True],
         [ True, False]],
<BLANKLINE>
        [[False,  True],
         [ True, False]]],
<BLANKLINE>
<BLANKLINE>
       [[[False,  True],
         [ True, False]],
<BLANKLINE>
        [[False,  True],
         [ True, False]]]], dtype=bool)
>>> all_permutations_equal(numpy.zeros((2,2)), numpy.eye(2))
array([[[[False,  True],
         [ True, False]],
<BLANKLINE>
        [[False,  True],
         [ True, False]]],
<BLANKLINE>
<BLANKLINE>
       [[[False,  True],
         [ True, False]],
<BLANKLINE>
        [[False,  True],
         [ True, False]]]], dtype=bool)
>>> all_permutations_equal(numpy.zeros((2,2)), numpy.eye(3))
array([[[[False,  True,  True],
         [ True, False,  True],
         [ True,  True, False]],
<BLANKLINE>
        [[False,  True,  True],
         [ True, False,  True],
         [ True,  True, False]]],
<BLANKLINE>
<BLANKLINE>
       [[[False,  True,  True],
         [ True, False,  True],
         [ True,  True, False]],
<BLANKLINE>
        [[False,  True,  True],
         [ True, False,  True],
         [ True,  True, False]]]], dtype=bool)
>>> all_permutations_equal(
...     numpy.arange(4).reshape((2,2)),
...     numpy.arange(2,6).reshape((2,2))
... )
array([[[[False, False],
         [False, False]],
<BLANKLINE>
        [[False, False],
         [False, False]]],
<BLANKLINE>
<BLANKLINE>
       [[[ True, False],
         [False, False]],
<BLANKLINE>
        [[False,  True],
         [False, False]]]], dtype=bool)
nanshe.util.xnumpy.array_to_matrix(*args, **kwargs)[source]

Flattens an array so that the row is the only original shape kept.

Parameters:a (numpy.ndarray) – The array to flatten, partially.
Returns:
The matrix version of the array
after flattening.
Return type:(numpy.ndarray)

Examples

>>> array_to_matrix(numpy.eye(3))
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> array_to_matrix(numpy.arange(24).reshape(4, 3, 2))
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
>>> array_to_matrix(numpy.arange(24).reshape(4, 6))
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23]])
>>> array_to_matrix(numpy.zeros((0, 4, 3, 2)))
array([], shape=(0, 24), dtype=float64)
nanshe.util.xnumpy.binomial_coefficients(*args, **kwargs)[source]

Generates a row in Pascal’s triangle (binomial coefficients).

Parameters:n (int) – which row of Pascal’s triangle to return.
Returns:
a numpy array containing the row of
Pascal’s triangle.
Return type:cs(numpy.ndarray)

Examples

>>> binomial_coefficients(-25)
array([], dtype=int64)
>>> binomial_coefficients(-1)
array([], dtype=int64)
>>> binomial_coefficients(0)
array([1])
>>> binomial_coefficients(1)
array([1, 1])
>>> binomial_coefficients(2)
array([1, 2, 1])
>>> binomial_coefficients(4)
array([1, 4, 6, 4, 1])
>>> binomial_coefficients(4.0)
array([1, 4, 6, 4, 1])
nanshe.util.xnumpy.compute_mapping_matches(*args, **kwargs)[source]

Given a mapping this function computes number of matches and mismatches found.

In the array returned, first value is the number of true positives (or matches) and then mismatches along each dimension of the mapping in order.

If axis 0 is the ground truth, then the second and third values are the number of false negatives (or misses) and false positives (or false alarm)

Parameters:mapping (numpy.ndarray) – a 2D bool array mapping intersections between 2 groups.
Returns:
Counts of the
number of matches and mismatches.
Return type:out(numpy.ndarray)

Examples

>>> compute_mapping_matches(numpy.arange(6).reshape(2,3) < 0)
array([0, 2, 3], dtype=uint64)
>>> compute_mapping_matches(numpy.arange(6).reshape(2,3) <= 0)
array([1, 1, 2], dtype=uint64)
>>> compute_mapping_matches(
...     (numpy.arange(6).reshape(2,3) % 2) == 1
... )
array([1, 1, 2], dtype=uint64)
>>> compute_mapping_matches(numpy.eye(2, dtype=bool))
array([2, 0, 0], dtype=uint64)
>>> compute_mapping_matches(numpy.fliplr(numpy.eye(2, dtype=bool)))
array([2, 0, 0], dtype=uint64)
nanshe.util.xnumpy.compute_mapping_relevance(*args, **kwargs)[source]

Given a mapping this function computes the recall and precision.

If axis 0 is the ground truth, then the returned values are the recall and precision in order. If axis 1 is the ground truth, then they are flipped.

Parameters:mapping (numpy.ndarray) – a 2D bool array mapping intersections between 2 groups.
Returns:
relevance - a combination of
recall and precision
recall - the ratio of
relevant predicted positives out of all relevant positives.
precision - the ratio of
relevant predicted positives out of all predicted positives.
Return type:relevance(tuple of floats)

Examples

>>> compute_mapping_relevance(numpy.arange(6).reshape(2,3) < 0)
array([ 0.,  0.])
>>> compute_mapping_relevance(numpy.arange(6).reshape(2,3) <= 0)
array([ 0.5       ,  0.33333333])
>>> compute_mapping_relevance(
...     (numpy.arange(6).reshape(2,3) % 2) == 1
... )
array([ 0.5       ,  0.33333333])
>>> compute_mapping_relevance(numpy.eye(2, dtype=bool))
array([ 1.,  1.])
>>> compute_mapping_relevance(
...     numpy.fliplr(numpy.eye(2, dtype=bool))
... )
array([ 1.,  1.])
nanshe.util.xnumpy.contains(*args, **kwargs)[source]

Gets a mask array that is true every time something from to_contain appears in new_array.

Parameters:
  • new_array (numpy.ndarray) – array to check for matches.
  • to_contain (array_like) – desired matches to find.
Returns:

a mask for new_array that

selects values from to_contain.

Return type:

(numpy.ndarray)

Examples

>>> contains(numpy.zeros((2,2)), 0)
array([[ True,  True],
       [ True,  True]], dtype=bool)
>>> contains(numpy.zeros((2,2)), 1)
array([[False, False],
       [False, False]], dtype=bool)
>>> contains(numpy.zeros((2,2)), [1])
array([[False, False],
       [False, False]], dtype=bool)
>>> contains(numpy.zeros((2,2)), numpy.array([1]))
array([[False, False],
       [False, False]], dtype=bool)
>>> contains(numpy.arange(4).reshape((2,2)), numpy.array([0]))
array([[ True, False],
       [False, False]], dtype=bool)
>>> contains(numpy.arange(4).reshape((2,2)), numpy.array([1]))
array([[False,  True],
       [False, False]], dtype=bool)
>>> contains(numpy.arange(4).reshape((2,2)), numpy.array([2]))
array([[False, False],
       [ True, False]], dtype=bool)
>>> contains(numpy.arange(4).reshape((2,2)), numpy.array([3]))
array([[False, False],
       [False,  True]], dtype=bool)
nanshe.util.xnumpy.dot_product(*args, **kwargs)[source]

Determines the dot product between the two pairs of vectors from each set.

Parameters:
  • new_vector_set_1 (numpy.ndarray) – first set of vectors.
  • new_vector_set_2 (numpy.ndarray) – second set of vectors.
Returns:

an array with the distances

between each pair of vectors.

Return type:

(numpy.ndarray)

Examples

>>> (dot_product(numpy.eye(2), numpy.eye(2)) == numpy.eye(2)).all()
True
>>> (dot_product(numpy.eye(10), numpy.eye(10)) == numpy.eye(10)).all()
True
>>> dot_product(numpy.array([[ 1,  0]]), numpy.array([[ 1,  0]]))
array([[ 1.]])
>>> dot_product(numpy.array([[ 1,  0]]), numpy.array([[ 0,  1]]))
array([[ 0.]])
>>> dot_product(numpy.array([[ 1,  0]]), numpy.array([[-1,  0]]))
array([[-1.]])
>>> dot_product(numpy.array([[ 1,  0]]), numpy.array([[ 0, -1]]))
array([[ 0.]])
>>> dot_product(numpy.array([[ 1,  0]]), numpy.array([[ 1,  1]]))
array([[ 1.]])
>>> dot_product(
...     numpy.array([[ True,  False]]),
...     numpy.array([[ True,  True]])
... )
array([[ 1.]])
nanshe.util.xnumpy.dot_product_L2_normalized(*args, **kwargs)[source]

Determines the dot product between a pair of vectors from each set and divides them by the L_2 norm of the two.

Parameters:
  • new_vector_set_1 (numpy.ndarray) – first set of vectors.
  • new_vector_set_2 (numpy.ndarray) – second set of vectors.
Returns:

an array with the distances

between each pair of vectors.

Return type:

(numpy.ndarray)

Examples

>>> (dot_product_L2_normalized(numpy.eye(2), numpy.eye(2)) == numpy.eye(2)).all()
True
>>> (dot_product_L2_normalized(numpy.eye(10), numpy.eye(10)) == numpy.eye(10)).all()
True
>>> dot_product_L2_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  0]]),
... )
array([[ 1.]])
>>> dot_product_L2_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 0,  1]]),
... )
array([[ 0.]])
>>> dot_product_L2_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[-1,  0]]),
... )
array([[-1.]])
>>> dot_product_L2_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 0, -1]]),
... )
array([[ 0.]])
>>> dot_product_L2_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  1]]),
... )
array([[ 0.70710678]])
>>> dot_product_L2_normalized(
...     numpy.array([[ True,  False]]),
...     numpy.array([[ True,   True]]),
... )
array([[ 0.70710678]])
>>> dot_product_L2_normalized(
...     numpy.arange(6).reshape((2,3)),
...     numpy.arange(5, 17).reshape((4,3)),
... )
array([[ 0.85280287,  0.82857143,  0.8157437 ,  0.80782729],
       [ 0.9978158 ,  0.99385869,  0.99111258,  0.98921809]])
nanshe.util.xnumpy.dot_product_normalized(*args, **kwargs)[source]

Determines the dot product between a pair of vectors from each set and divides them by the norm of the two.

Parameters:
  • new_vector_set_1 (numpy.ndarray) – first set of vectors.
  • new_vector_set_2 (numpy.ndarray) – second set of vectors.
  • ord (optional) – basically the same arguments as numpy.linalg.norm.
Returns:

an array with the normalized

distances between each pair of vectors.

Return type:

(numpy.ndarray)

Examples

>>> (dot_product_normalized(numpy.eye(2), numpy.eye(2), 2) == numpy.eye(2)).all()
True
>>> (dot_product_normalized(numpy.eye(10), numpy.eye(10), 2) == numpy.eye(10)).all()
True
>>> dot_product_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  0]]),
...     2
... )
array([[ 1.]])
>>> dot_product_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 0,  1]]),
...     2
... )
array([[ 0.]])
>>> dot_product_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[-1,  0]]),
...     2
... )
array([[-1.]])
>>> dot_product_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 0, -1]]),
...     2
... )
array([[ 0.]])
>>> dot_product_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  1]]),
...     2
... )
array([[ 0.70710678]])
>>> dot_product_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  1]]),
...     1
... )
array([[ 0.5]])
>>> dot_product_normalized(
...     numpy.array([[ True,  False]]),
...     numpy.array([[ True,   True]]),
...     2
... )
array([[ 0.70710678]])
>>> dot_product_normalized(
...     numpy.array([[ True, False]]),
...     numpy.array([[ True,  True]]),
...     1
... )
array([[ 0.5]])
>>> dot_product_normalized(
...     numpy.arange(6).reshape((2,3)),
...     numpy.arange(5, 17).reshape((4,3)),
...     2
... )
array([[ 0.85280287,  0.82857143,  0.8157437 ,  0.80782729],
       [ 0.9978158 ,  0.99385869,  0.99111258,  0.98921809]])
nanshe.util.xnumpy.dot_product_partially_normalized(*args, **kwargs)[source]

Determines the dot product between the two pairs of vectors from each set and creates a tuple with the dot product divided by one norm or the other.

Parameters:
  • new_vector_set_1 (numpy.ndarray) – first set of vectors.
  • new_vector_set_2 (numpy.ndarray) – second set of vectors.
  • ord (optional) – basically the same arguments as numpy.linalg.norm
Returns:

an array with the normalized

distances between each pair of vectors.

Return type:

(numpy.ndarray)

Examples

>>> (numpy.array(dot_product_partially_normalized(numpy.eye(2), numpy.eye(2), 2)) == numpy.array((numpy.eye(2), numpy.eye(2),))).all()
True
>>> (numpy.array(dot_product_partially_normalized(numpy.eye(10), numpy.eye(10), 2)) == numpy.array((numpy.eye(10), numpy.eye(10),))).all()
True
>>> dot_product_partially_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  0]]),
...     2
... )
(array([[ 1.]]), array([[ 1.]]))
>>> dot_product_partially_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 0,  1]]),
...     2
... )
(array([[ 0.]]), array([[ 0.]]))
>>> dot_product_partially_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[-1,  0]]),
...     2
... )
(array([[-1.]]), array([[-1.]]))
>>> dot_product_partially_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 0, -1]]),
...     2
... )
(array([[ 0.]]), array([[ 0.]]))
>>> dot_product_partially_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  1]]),
...     2
... )
(array([[ 1.]]), array([[ 0.70710678]]))
>>> dot_product_partially_normalized(
...     numpy.array([[ 1,  0]]),
...     numpy.array([[ 1,  1]]),
...     1
... )
(array([[ 1.]]), array([[ 0.5]]))
>>> dot_product_partially_normalized(
...     numpy.array([[ True,  False]]),
...     numpy.array([[ True,  True]]),
...     2
... )
(array([[ 1.]]), array([[ 0.70710678]]))
>>> dot_product_partially_normalized(
...     numpy.array([[ True,  False]]),
...     numpy.array([[ True,  True]]),
...     1
... )
(array([[ 1.]]), array([[ 0.5]]))
>>> dot_product_partially_normalized(
...     numpy.arange(6).reshape((2,3)),
...     numpy.arange(5, 17).reshape((4,3)),
...     2
... )  #doctest: +NORMALIZE_WHITESPACE
(array([[  8.94427191,  12.96919427,  16.99411663,  21.01903899],
        [ 10.46518036,  15.55634919,  20.64751801,  25.73868684]]),
 array([[ 1.90692518,  1.85274204,  1.82405837,  1.80635674],
        [ 7.05562316,  7.02764221,  7.00822427,  6.99482822]]))
nanshe.util.xnumpy.enumerate_masks(new_masks, axis=0)[source]

Takes a mask stack and replaces them by an enumerated stack. In other words, each mask is replaced by a consecutive integer (starts with 1 and proceeds to the length of the given axis (0 by default)).

Note

The masks could be recreated by finding the values not equal to zero.

Parameters:
  • new_masks (numpy.ndarray) – masks to enumerate
  • axis (int) – axis to enumerate along (0 by default).
Returns:

an enumerated stack.

Return type:

(numpy.ndarray)

Examples

>>> enumerate_masks(numpy.ones((3,3,3), dtype=bool))
array([[[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]],
<BLANKLINE>
       [[2, 2, 2],
        [2, 2, 2],
        [2, 2, 2]],
<BLANKLINE>
       [[3, 3, 3],
        [3, 3, 3],
        [3, 3, 3]]], dtype=uint64)
>>> enumerate_masks(
...     numpy.array([[[ True, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False]],
...
...                  [[False, False, False, False],
...                   [False,  True, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False]],
...
...                  [[False, False, False, False],
...                   [False, False, False, False],
...                   [False, False,  True, False],
...                   [False, False, False, False]],
...
...                  [[False, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False,  True]]], dtype=bool)
... )
array([[[1, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]],
<BLANKLINE>
       [[0, 0, 0, 0],
        [0, 2, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]],
<BLANKLINE>
       [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 3, 0],
        [0, 0, 0, 0]],
<BLANKLINE>
       [[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 4]]], dtype=uint64)
nanshe.util.xnumpy.enumerate_masks_max(new_masks, axis=0)[source]

Takes a mask stack and replaces them by the max of an enumerated stack. In other words, each mask is replaced by a consecutive integer (starts with 1 and proceeds to the length of the given axis (0 by default)). Afterwards, the max is taken along the given axis. However, a singleton dimension is left on the original axis.

Note

The masks could be recreated by finding the values not equal to zero.

Parameters:
  • new_masks (numpy.ndarray) – masks to enumerate
  • axis (int) – axis to enumerate along (0 by default).
Returns:

an enumerated stack.

Return type:

(numpy.ndarray)

Examples

>>> enumerate_masks_max(numpy.ones((3,3,3), dtype=bool))
array([[[3, 3, 3],
        [3, 3, 3],
        [3, 3, 3]]], dtype=uint64)
>>> enumerate_masks_max(
...     numpy.array([[[ True, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False]],
...
...                  [[False, False, False, False],
...                   [False,  True, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False]],
...
...                  [[False, False, False, False],
...                   [False, False, False, False],
...                   [False, False,  True, False],
...                   [False, False, False, False]],
...
...                  [[False, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False, False],
...                   [False, False, False,  True]]], dtype=bool)
... )
array([[[1, 0, 0, 0],
        [0, 2, 0, 0],
        [0, 0, 3, 0],
        [0, 0, 0, 4]]], dtype=uint64)
nanshe.util.xnumpy.expand_arange(start, stop=None, step=1, dtype=<type 'numpy.int64'>, reps_before=(), reps_after=())[source]

Much like numpy.arange except that it applies expand_view afterwards to get a view of the same range in a larger hyperrectangle.

This is very useful for situations where broadcasting is desired.

Parameters:
  • start (int) – starting point (or stopping point if only one is specified).
  • stop (int) – stopping point (if the starting point is specified) (0 by default).
  • step (int) – size of steps to take between value (1 by default).
  • reps_after (tuple) – repetitions dimension size to add before (if int will turn into tuple).
  • reps_before (tuple) – repetitions dimension size to add after (if int will turn into tuple).
Returns:

a view of a numpy arange with

tiling in various dimension.

Return type:

(numpy.ndarray)

Examples

>>> expand_arange(3, reps_before=3)
array([[0, 1, 2],
       [0, 1, 2],
       [0, 1, 2]])
>>> expand_arange(3, reps_after=3)
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2]])
>>> expand_arange(4, reps_before=3)
array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]])
>>> expand_arange(4, reps_after=3)
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])
>>> expand_arange(4, reps_before=3, reps_after=2)
array([[[0, 0],
        [1, 1],
        [2, 2],
        [3, 3]],
<BLANKLINE>
       [[0, 0],
        [1, 1],
        [2, 2],
        [3, 3]],
<BLANKLINE>
       [[0, 0],
        [1, 1],
        [2, 2],
        [3, 3]]])
nanshe.util.xnumpy.expand_view(*args, **kwargs)[source]

Behaves like NumPy tile except that it always returns a view and not a copy. Though, it differs in that additional dimensions are added for repetition as opposed to repeating in the same one. Also, it allows repetitions to be specified before or after unlike tile. Though, will behave identical to tile if the keyword is not specified.

Uses strides to trick NumPy into providing a view.

Parameters:
  • new_array (numpy.ndarray) – array to tile.
  • reps_after (tuple) – repetitions dimension size to add before (if int will turn into tuple).
  • reps_before (tuple) – repetitions dimension size to add after (if int will turn into tuple).
Returns:

a view of a numpy array with

tiling in various dimension.

Return type:

(numpy.ndarray)

Examples

>>> numpy.arange(6).reshape(2,3)
array([[0, 1, 2],
       [3, 4, 5]])
>>> expand_view(numpy.arange(6).reshape(2,3))
array([[0, 1, 2],
       [3, 4, 5]])
>>> a = numpy.arange(6).reshape(2,3); a is expand_view(a)
False
>>> expand_view(numpy.arange(6).reshape(2,3), 1)
array([[[0],
        [1],
        [2]],
<BLANKLINE>
       [[3],
        [4],
        [5]]])
>>> expand_view(numpy.arange(6).reshape(2,3), (1,))
array([[[0],
        [1],
        [2]],
<BLANKLINE>
       [[3],
        [4],
        [5]]])
>>> expand_view(numpy.arange(6).reshape((2,3)), reps_after=1)
array([[[0],
        [1],
        [2]],
<BLANKLINE>
       [[3],
        [4],
        [5]]])
>>> expand_view(numpy.arange(6).reshape((2,3)), reps_after=(1,))
array([[[0],
        [1],
        [2]],
<BLANKLINE>
       [[3],
        [4],
        [5]]])
>>> expand_view(numpy.arange(6).reshape((2,3)), reps_before=1)
array([[[0, 1, 2],
        [3, 4, 5]]])
>>> expand_view(numpy.arange(6).reshape((2,3)), reps_before=(1,))
array([[[0, 1, 2],
        [3, 4, 5]]])
>>> expand_view(numpy.arange(6).reshape((2,3)), reps_before=(3,))
array([[[0, 1, 2],
        [3, 4, 5]],
<BLANKLINE>
       [[0, 1, 2],
        [3, 4, 5]],
<BLANKLINE>
       [[0, 1, 2],
        [3, 4, 5]]])
>>> expand_view(numpy.arange(6).reshape((2,3)), reps_after=(4,))
array([[[0, 0, 0, 0],
        [1, 1, 1, 1],
        [2, 2, 2, 2]],
<BLANKLINE>
       [[3, 3, 3, 3],
        [4, 4, 4, 4],
        [5, 5, 5, 5]]])
>>> expand_view(
...     numpy.arange(6).reshape((2,3)),
...     reps_before=(3,),
...     reps_after=(4,)
... )
array([[[[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2]],
<BLANKLINE>
        [[3, 3, 3, 3],
         [4, 4, 4, 4],
         [5, 5, 5, 5]]],
<BLANKLINE>
<BLANKLINE>
       [[[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2]],
<BLANKLINE>
        [[3, 3, 3, 3],
         [4, 4, 4, 4],
         [5, 5, 5, 5]]],
<BLANKLINE>
<BLANKLINE>
       [[[0, 0, 0, 0],
         [1, 1, 1, 1],
         [2, 2, 2, 2]],
<BLANKLINE>
        [[3, 3, 3, 3],
         [4, 4, 4, 4],
         [5, 5, 5, 5]]]])
>>> expand_view(numpy.arange(6).reshape((2,3)), reps_after = (4,3))
array([[[[0, 0, 0],
         [0, 0, 0],
         [0, 0, 0],
         [0, 0, 0]],
<BLANKLINE>
        [[1, 1, 1],
         [1, 1, 1],
         [1, 1, 1],
         [1, 1, 1]],
<BLANKLINE>
        [[2, 2, 2],
         [2, 2, 2],
         [2, 2, 2],
         [2, 2, 2]]],
<BLANKLINE>
<BLANKLINE>
       [[[3, 3, 3],
         [3, 3, 3],
         [3, 3, 3],
         [3, 3, 3]],
<BLANKLINE>
        [[4, 4, 4],
         [4, 4, 4],
         [4, 4, 4],
         [4, 4, 4]],
<BLANKLINE>
        [[5, 5, 5],
         [5, 5, 5],
         [5, 5, 5],
         [5, 5, 5]]]])
>>> expand_view(
...     numpy.arange(6).reshape((2,3)),
...     reps_before=(4,3),
... )
array([[[[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]]],
<BLANKLINE>
<BLANKLINE>
       [[[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]]],
<BLANKLINE>
<BLANKLINE>
       [[[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]]],
<BLANKLINE>
<BLANKLINE>
       [[[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]],
<BLANKLINE>
        [[0, 1, 2],
         [3, 4, 5]]]])
nanshe.util.xnumpy.find_relative_offsets(*args, **kwargs)[source]

Given a series of points, find the relative points from the mean.

Parameters:
  • points (numpy.ndarray) – a set of integer points (NxD) where N is the number of points and D is the dimensionality, in which they lay.
  • center (numpy.ndarray) – an integer point (D) where D is the dimensionality, in which they lay. Defaults to the mean of the points.
  • out (numpy.ndarray) – another set of points relative to their mean.
Returns:

the results returned.

Return type:

out(numpy.ndarray)

Examples

>>> find_relative_offsets(numpy.zeros((3,2), dtype=int))
array([[0, 0],
       [0, 0],
       [0, 0]])
>>> find_relative_offsets(numpy.ones((3,2), dtype=int))
array([[0, 0],
       [0, 0],
       [0, 0]])
>>> find_relative_offsets(
...     numpy.ones((3,2), dtype=int), -numpy.ones((2,), dtype=int)
... )
array([[2, 2],
       [2, 2],
       [2, 2]])
>>> find_relative_offsets(numpy.arange(6).reshape(2,3).T % 2)
array([[ 0,  0],
       [ 1, -1],
       [ 0,  0]])
>>> a = numpy.arange(6).reshape(2,3).T % 2; b = numpy.zeros_like(a)
>>> find_relative_offsets(a, out=b)
array([[ 0,  0],
       [ 1, -1],
       [ 0,  0]])
>>> b
array([[ 0,  0],
       [ 1, -1],
       [ 0,  0]])
>>> a = numpy.arange(6).reshape(2,3).T % 2
>>> find_relative_offsets(a, out=a)
array([[ 0,  0],
       [ 1, -1],
       [ 0,  0]])
>>> a
array([[ 0,  0],
       [ 1, -1],
       [ 0,  0]])
nanshe.util.xnumpy.find_shortest_wraparound(*args, **kwargs)[source]

Compute the smallest values for the points given periodic boundary conditions.

Parameters:
  • points (numpy.ndarray) – a set of integer points (NxD) where N is the number of points and D is the dimensionality, in which they lay.
  • shape (numpy.ndarray) – the shape to use for wrapping (D).
  • out (numpy.ndarray) – another set of points relative to their mean.
Returns:

the results returned.

Return type:

out(numpy.ndarray)

Examples

>>> find_shortest_wraparound(
...     numpy.zeros((3, 2), dtype=int),
...     (4, 8)
... )
array([[0, 0],
       [0, 0],
       [0, 0]])
>>> find_shortest_wraparound(
...     numpy.ones((3, 2), dtype=int),
...     (4, 8)
... )
array([[1, 1],
       [1, 1],
       [1, 1]])
>>> find_shortest_wraparound(
...     4 * numpy.ones((3, 2), dtype=int),
...     (4, 8)
... )
array([[0, 4],
       [0, 4],
       [0, 4]])
>>> find_shortest_wraparound(
...     8 * (numpy.arange(6).reshape(3, 2) % 2),
...     (4, 8)
... )
array([[0, 0],
       [0, 0],
       [0, 0]])
>>> find_shortest_wraparound(
...     7 * (numpy.arange(6).reshape(3, 2) % 2),
...     (4, 8)
... )
array([[ 0, -1],
       [ 0, -1],
       [ 0, -1]])
>>> find_shortest_wraparound(
...     -numpy.ones((3, 2), dtype=int),
...     (4, 8)
... )
array([[-1, -1],
       [-1, -1],
       [-1, -1]])
>>> find_shortest_wraparound(
...     -4 * numpy.ones((3, 2), dtype=int),
...     (4, 8)
... )
array([[ 0, -4],
       [ 0, -4],
       [ 0, -4]])
>>> find_shortest_wraparound(
...     -7 * (numpy.arange(6).reshape(3, 2) % 2),
...     (4, 8)
... )
array([[0, 1],
       [0, 1],
       [0, 1]])
>>> a = -7 * (numpy.arange(6).reshape(3, 2) % 2)
>>> b = numpy.zeros_like(a)
>>> find_shortest_wraparound(
...     a,
...     (4, 8),
...     out=b
... )
array([[0, 1],
       [0, 1],
       [0, 1]])
>>> b
array([[0, 1],
       [0, 1],
       [0, 1]])
>>> a = -7 * (numpy.arange(6).reshape(3, 2) % 2)
>>> find_shortest_wraparound(
...     a,
...     (4, 8),
...     out=a
... )
array([[0, 1],
       [0, 1],
       [0, 1]])
>>> a
array([[0, 1],
       [0, 1],
       [0, 1]])
nanshe.util.xnumpy.generate_contour(a_image, separation_distance=1.0, margin=1.0)[source]

Takes an image and extracts labeled contours from the mask using some minimum distance from the mask edge and some margin.

Parameters:
  • a_image (numpy.ndarray) – takes an image.
  • separation_distance (float) – a separation distance from the edge of the mask for the center of the contour.
  • margin (float) – the width of contour.
Returns:

an array with the labeled

contours.

Return type:

(numpy.ndarray)

Examples

>>> a = numpy.array([[ True,  True, False],
...                  [False, False, False],
...                  [ True,  True,  True]], dtype=bool)
>>> generate_contour(a)
array([[ True,  True, False],
       [False, False, False],
       [ True,  True,  True]], dtype=bool)
>>> generate_contour(numpy.eye(3))
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])
>>> a = numpy.array([
...     [False, False,  True, False, False, False,  True],
...     [ True, False, False, False,  True, False, False],
...     [ True,  True, False,  True,  True, False,  True],
...     [ True, False, False,  True,  True, False, False],
...     [ True, False, False, False, False, False, False],
...     [False,  True, False, False, False, False,  True],
...     [False,  True,  True, False, False, False, False]
... ], dtype=bool)
>>> generate_contour(a)
array([[False, False,  True, False, False, False,  True],
       [ True, False, False, False,  True, False, False],
       [ True,  True, False,  True,  True, False,  True],
       [ True, False, False,  True,  True, False, False],
       [ True, False, False, False, False, False, False],
       [False,  True, False, False, False, False,  True],
       [False,  True,  True, False, False, False, False]], dtype=bool)
nanshe.util.xnumpy.generate_labeled_contours(a_mask, separation_distance=1.0, margin=1.0)[source]

Takes a bool mask and extracts labeled contours from the mask using some minimum distance from the mask edge and some margin.

Parameters:
  • a_mask (numpy.ndarray) – takes a bool mask.
  • separation_distance (float) – a separation distance from the edge of the mask for the center of the contour.
  • margin (float) – the width of contour.
Returns:

an array with the labeled

contours.

Return type:

(numpy.ndarray)

Examples

>>> a = numpy.array([[ True,  True, False],
...                  [False, False, False],
...                  [ True,  True,  True]], dtype=bool)
>>> generate_labeled_contours(a)
array([[1, 1, 0],
       [0, 0, 0],
       [2, 2, 2]], dtype=int32)
>>> generate_labeled_contours(numpy.eye(3))
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]], dtype=int32)
>>> a = numpy.array([
...     [False, False,  True, False, False, False,  True],
...     [ True, False, False, False,  True, False, False],
...     [ True,  True, False,  True,  True, False,  True],
...     [ True, False, False,  True,  True, False, False],
...     [ True, False, False, False, False, False, False],
...     [False,  True, False, False, False, False,  True],
...     [False,  True,  True, False, False, False, False]
... ], dtype=bool)
>>> generate_labeled_contours(a)
array([[0, 0, 1, 0, 0, 0, 2],
       [3, 0, 0, 0, 4, 0, 0],
       [3, 3, 0, 4, 4, 0, 5],
       [3, 0, 0, 4, 4, 0, 0],
       [3, 0, 0, 0, 0, 0, 0],
       [0, 3, 0, 0, 0, 0, 6],
       [0, 3, 3, 0, 0, 0, 0]], dtype=int32)
nanshe.util.xnumpy.get_quantiles(probs)[source]

Determines the probabilites for quantiles for given data much like MATLAB’s function

Parameters:
  • data (numpy.ndarray) – to find the quantiles of.
  • probs (int or float or numpy.ndarray) – either some sort of integer for the number of quantiles or a single float specifying which quantile to get or an array of floats specifying the division for each quantile in the the range (0, 1).
  • axis (int or None) – the axis to perform the calculation on (if default (None) then all, otherwise only on a particular axis.
Returns:

an array with the

quantiles (the first dimension will be the same length as probs).

Return type:

(numpy.ma.MaskedArray)

Examples

>>> get_quantiles(0)
array([], dtype=float64)
>>> get_quantiles(1)
array([ 0.5])
>>> get_quantiles(3)
array([ 0.25,  0.5 ,  0.75])
>>> get_quantiles(0.5)
array([ 0.5])
>>> get_quantiles([0.25, 0.75])
array([ 0.25,  0.75])
>>> get_quantiles(numpy.array([0.25, 0.75]))
array([ 0.25,  0.75])
nanshe.util.xnumpy.index_array_to_bool_array(*args, **kwargs)[source]

Creates a bool array mask that has each value from the index array as True. All other values are False. Requires a shape be specified to create the bool array.

Parameters:
  • index_array (numpy.ndarray of ints) – The index array with the indices to use.
  • shape (tuple of ints) – The shape to give the bool array.
Returns:

The bool array with

selected indices as True and the rest are False.

Return type:

(numpy.ndarray)

Examples

>>> index_array_to_bool_array((numpy.arange(5),), (5,))
array([ True,  True,  True,  True,  True], dtype=bool)
>>> index_array_to_bool_array((numpy.arange(1, 4),), (5,))
array([False,  True,  True,  True, False], dtype=bool)
>>> index_array_to_bool_array(
...     (numpy.arange(3), numpy.arange(3)), (3, 3)
... )
array([[ True, False, False],
       [False,  True, False],
       [False, False,  True]], dtype=bool)
nanshe.util.xnumpy.index_axis_at_pos(*args, **kwargs)[source]

Indexes an arbitrary axis to the given position, which may be an index, a slice, or any other NumPy allowed indexing type. This will return a view.

Parameters:
  • new_array (numpy.ndarray) – array to add the singleton axis to.
  • axis (int) – position for the axis to be in the final array.
  • pos (int or slice) – how to index at the given axis.
Returns:

a numpy array view of the

original array.

Return type:

(numpy.ndarray)

Examples

>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> index_axis_at_pos(a, 0, 0).shape
(2, 3, 4)
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> index_axis_at_pos(a, 1, 0).shape
(1, 3, 4)
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> index_axis_at_pos(a, 2, 0).shape
(1, 2, 4)
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> index_axis_at_pos(a, 3, 0).shape
(1, 2, 3)
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> (index_axis_at_pos(a, 3, 0) == a[:,:,:,0]).all()
True
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> (index_axis_at_pos(a, -1, 0) == a[:,:,:,0]).all()
True
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> (index_axis_at_pos(a, -1, 2) == a[:,:,:,2]).all()
True
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> (index_axis_at_pos(a, 1, 1) == a[:,1,:,:]).all()
True
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> (index_axis_at_pos(a, 2, slice(None,None,2)) == a[:,:,::2,:]).all()
True
>>> a = numpy.arange(24).reshape((1,2,3,4))
>>> index_axis_at_pos(a, 2, 2)[0, 1, 3] = 19; a[0, 1, 2, 3] == 19
True
nanshe.util.xnumpy.line_filter(*args, **kwargs)[source]

Creates a boolean array mask for a line. This mask has size for the length of the line and number of empty lines beside it in any orthogonal direction. The mask has dimensions equal to ndims and the line is placed along dimension dim.

Parameters:
  • shape (tuple of ints) – the distance from the center of the filter to the nearest edge for each dimension.
  • dim (int) – the dimension to put the line along.
Returns:

a boolean array to use as the filter.

Return type:

(numpy.ndarray)

Examples

>>> line_filter((1,1))
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> line_filter((1,1), dim = -1)
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> line_filter((1,1), dim = 1)
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> line_filter((2,1))
array([[False, False, False],
       [False, False, False],
       [ True,  True,  True],
       [False, False, False],
       [False, False, False]], dtype=bool)
>>> line_filter((1, 1, 1))
array([[[False, False, False],
        [False, False, False],
        [False, False, False]],
<BLANKLINE>
       [[False, False, False],
        [ True,  True,  True],
        [False, False, False]],
<BLANKLINE>
       [[False, False, False],
        [False, False, False],
        [False, False, False]]], dtype=bool)
nanshe.util.xnumpy.masks_intersection(*args, **kwargs)[source]

Generates a mask that contains the points share by both masks.

Parameters:
  • a (numpy.ndarray) – first mask.
  • b (numpy.ndarray) – second mask.
Returns:

a mask that is only True where both masks

are.

Return type:

out(numpy.ndarray)

Examples

>>> masks_intersection(numpy.eye(2).astype(bool),
...                    numpy.ones((2,2), dtype=bool))
array([[1, 1],
       [1, 1]], dtype=uint64)
>>> masks_intersection(numpy.eye(2).astype(bool),
...                    numpy.ones((2,), dtype=bool))
array([[1],
       [1]], dtype=uint64)
>>> masks_intersection(numpy.ones((2,), dtype=bool),
...                    numpy.eye(2).astype(bool))
array([[1, 1]], dtype=uint64)
>>> masks_intersection(numpy.ones((2,), dtype=bool),
...                    numpy.ones((2,), dtype=bool))
array([[2]], dtype=uint64)
>>> masks_intersection(numpy.eye(2).astype(bool),
...                    numpy.zeros((2,2), dtype=bool))
array([[0, 0],
       [0, 0]], dtype=uint64)
>>> (numpy.arange(6).reshape(2,3) % 2).astype(bool)
array([[False,  True, False],
       [ True, False,  True]], dtype=bool)
>>> numpy.arange(6).reshape(2,3) == 4
array([[False, False, False],
       [False,  True, False]], dtype=bool)
>>> masks_intersection(
...     (numpy.arange(6).reshape(2,3) % 2).astype(bool),
...     (numpy.arange(6).reshape(2,3) == 4)
... )
array([[0, 1],
       [0, 0]], dtype=uint64)
nanshe.util.xnumpy.masks_overlap_normalized(*args, **kwargs)[source]

The area of intersection of the masks divided by the area of their union.

Parameters:
  • a (numpy.ndarray) – first mask.
  • b (numpy.ndarray) – second mask.
Returns:

ratio of the areas of the masks’

intersection and union.

Return type:

out(numpy.ndarray)

Examples

>>> masks_overlap_normalized(numpy.eye(2).astype(bool),
...                          numpy.ones((2,2), dtype=bool))
array([[ 0.5,  0.5],
       [ 0.5,  0.5]])
>>> masks_overlap_normalized(numpy.eye(2).astype(bool),
...                          numpy.ones((2,), dtype=bool))
array([[ 0.5],
       [ 0.5]])
>>> masks_overlap_normalized(numpy.ones((2,), dtype=bool),
...                          numpy.eye(2).astype(bool))
array([[ 0.5,  0.5]])
>>> masks_overlap_normalized(numpy.ones((2,), dtype=bool),
...                          numpy.ones((2,), dtype=bool))
array([[ 1.]])
>>> masks_overlap_normalized(numpy.eye(2).astype(bool),
...                          numpy.zeros((2,2), dtype=bool))
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> masks_overlap_normalized(numpy.zeros((2,2), dtype=bool),
...                          numpy.zeros((2,2), dtype=bool))
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> (numpy.arange(6).reshape(2,3) % 2).astype(bool)
array([[False,  True, False],
       [ True, False,  True]], dtype=bool)
>>> numpy.arange(6).reshape(2,3) == 4
array([[False, False, False],
       [False,  True, False]], dtype=bool)
>>> masks_overlap_normalized(
...     (numpy.arange(6).reshape(2,3) % 2).astype(bool),
...     (numpy.arange(6).reshape(2,3) == 4)
... )
array([[ 0.,  1.],
       [ 0.,  0.]])
nanshe.util.xnumpy.masks_union(*args, **kwargs)[source]

A mask that contains point contained in either mask.

Parameters:
  • a (numpy.ndarray) – first mask.
  • b (numpy.ndarray) – second mask.
Returns:

a mask that is True where either mask is.

Return type:

out(numpy.ndarray)

Examples

>>> masks_union(numpy.eye(2).astype(bool),
...             numpy.ones((2,2), dtype=bool))
array([[2, 2],
       [2, 2]], dtype=uint64)
>>> masks_union(numpy.eye(2).astype(bool),
...             numpy.ones((2,), dtype=bool))
array([[2],
       [2]], dtype=uint64)
>>> masks_union(numpy.ones((2,), dtype=bool),
...             numpy.eye(2).astype(bool))
array([[2, 2]], dtype=uint64)
>>> masks_union(numpy.ones((2,), dtype=bool),
...             numpy.ones((2,), dtype=bool))
array([[2]], dtype=uint64)
>>> masks_union(numpy.eye(2).astype(bool),
...             numpy.zeros((2,2), dtype=bool))
array([[1, 1],
       [1, 1]], dtype=uint64)
>>> (numpy.arange(6).reshape(2,3) % 2).astype(bool)
array([[False,  True, False],
       [ True, False,  True]], dtype=bool)
>>> numpy.arange(6).reshape(2,3) == 4
array([[False, False, False],
       [False,  True, False]], dtype=bool)
>>> masks_union((numpy.arange(6).reshape(2,3) % 2).astype(bool),
...             (numpy.arange(6).reshape(2,3) == 4))
array([[1, 1],
       [2, 3]], dtype=uint64)
nanshe.util.xnumpy.matrix_reduced_op(*args, **kwargs)[source]

Sort of like numpy.dot. However, it will use the first axis with both arrays. This means they may not need to be matrices. However, they must have the same number of dimensions. Generally, though not explicitly required, the operator will likely expect every dimension other than the first to be the same shape.

Parameters:
  • a (numpy.ndarray) – first array.
  • b (numpy.ndarray) – second array.
  • op (callable) – an operator that will take a[i] and b[j] as arguments and return a scalar.
Returns:

an array (matrix) with the shape (len(a),

len(b)) with each element out[i, j] the result of op(a[i], b[j]).

Return type:

out(numpy.ndarray)

Examples

>>> matrix_reduced_op(
...     numpy.arange(6).reshape(2,3),
...     numpy.arange(0,12,2).reshape(2,3),
...     op=numpy.dot
... )
array([[ 10,  28],
       [ 28, 100]])
>>> numpy.dot(
...     numpy.arange(6).reshape(2,3),
...     numpy.arange(0,12,2).reshape(2,3).T,
... )
array([[ 10,  28],
       [ 28, 100]])
>>> matrix_reduced_op(
...     numpy.arange(8).reshape(2,4),
...     numpy.arange(0,16,2).reshape(2,4),
...     op=numpy.dot
... )
array([[ 28,  76],
       [ 76, 252]])
>>> numpy.dot(
...     numpy.arange(8).reshape(2,4),
...     numpy.arange(0,16,2).reshape(2,4).T,
... )
array([[ 28,  76],
       [ 76, 252]])
>>> matrix_reduced_op(numpy.eye(2).astype(bool),
...                   numpy.ones((2,2), dtype=bool),
...                   op=lambda _a, _b: (_a & _b).sum())
array([[ True,  True],
       [ True,  True]], dtype=bool)
>>> matrix_reduced_op(numpy.eye(2).astype(bool),
...                   numpy.ones((2,), dtype=bool),
...                   op=lambda _a, _b: (_a & _b).sum())
array([[ True],
       [ True]], dtype=bool)
>>> matrix_reduced_op(numpy.ones((2,), dtype=bool),
...                   numpy.eye(2).astype(bool),
...                   op=lambda _a, _b: (_a & _b).sum())
array([[ True,  True]], dtype=bool)
>>> matrix_reduced_op(numpy.ones((2,), dtype=bool),
...                    numpy.ones((2,), dtype=bool),
...                    op=lambda _a, _b: (_a & _b).sum())
array([[ True]], dtype=bool)
>>> matrix_reduced_op(numpy.eye(2).astype(bool),
...                   numpy.zeros((2,2), dtype=bool),
...                   op=lambda _a, _b: (_a & _b).sum())
array([[False, False],
       [False, False]], dtype=bool)
>>> matrix_reduced_op(numpy.eye(2).astype(bool),
...                   numpy.zeros((2,2), dtype=bool),
...                   op=lambda _a, _b: (_a | _b).sum())
array([[ True,  True],
       [ True,  True]], dtype=bool)
nanshe.util.xnumpy.max_abs(*args, **kwargs)[source]

Takes the max of the given array subject to the absolute value (magnitude for complex numbers).

Parameters:
  • new_array (numpy.ndarray) – array to find the max (subject to the absolute value).
  • axis (int) – desired matches to find.
  • keepdims (bool) – ensure the number of dimensions is the same by inserting singleton dimensions at all the axes squished (excepting the last one).
  • return_indices (bool) – whether to return the indices of the maxes in addition to the maxes.
Returns:

an array or value that is the

largest (subject to the absolute value) or if return_indices the indices corresponding to the largest value(s), as well.

Return type:

(tuple of numpy.ndarray)

Examples

>>> max_abs(numpy.arange(10))
9
>>> max_abs(numpy.arange(10).reshape(2,5))
9
>>> max_abs(numpy.arange(10).reshape(2,5), axis=0)
array([5, 6, 7, 8, 9])
>>> max_abs(numpy.arange(10).reshape(2,5), axis=1)
array([4, 9])
>>> max_abs(numpy.arange(10).reshape(2,5), axis=-1)
array([4, 9])
>>> max_abs(numpy.arange(10).reshape(2,5), axis=-1, keepdims=True)
array([[4],
       [9]])
>>> max_abs(numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]))
(1+3j)
>>> max_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=0
... )  # doctest: +SKIP
array([ 1.+0.j,  1.+1.j,  1.+3.j])
>>> max_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=1
... )  # doctest: +SKIP
array([ 2.+1.j,  1.+3.j])
>>> max_abs(numpy.arange(24).reshape(2,3,4), axis=(1,2))
array([11, 23])
>>> max_abs(numpy.arange(24).reshape(2,3,4), axis=(0,2))
array([15, 19, 23])
>>> max_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(0,2),
...     keepdims=True
... )
array([[[15],
        [19],
        [23]]])
>>> max_abs(numpy.arange(24).reshape(2,3,4), axis=(2,0))
array([15, 19, 23])
>>> max_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(2,0),
...     return_indices=True
... )
(array([15, 19, 23]), (array([1, 1, 1]), array([0, 1, 2]), array([3, 3, 3])))
>>> max_abs(numpy.array([numpy.nan, -2, 3]))
nan
nanshe.util.xnumpy.min_abs(*args, **kwargs)[source]

Takes the min of the given array subject to the absolute value (magnitude for complex numbers).

Parameters:
  • new_array (numpy.ndarray) – array to find the min (subject to the absolute value).
  • axis (int) – desired matches to find.
  • keepdims (bool) – ensure the number of dimensions is the same by inserting singleton dimensions at all the axes squished (excepting the last one).
  • return_indices (bool) – whether to return the indices of the mins in addition to the mins.
Returns:

an array or value that is the

smallest (subject to the absolute value) or if return_indices the indices corresponding to the smallest value(s), as well.

Return type:

(tuple of numpy.ndarray)

Examples

>>> min_abs(numpy.arange(10))
0
>>> min_abs(numpy.arange(10).reshape(2,5))
0
>>> min_abs(numpy.arange(10).reshape(2,5), axis=0)
array([0, 1, 2, 3, 4])
>>> min_abs(numpy.arange(10).reshape(2,5), axis=1)
array([0, 5])
>>> min_abs(numpy.arange(10).reshape(2,5), axis=-1)
array([0, 5])
>>> min_abs(numpy.arange(10).reshape(2,5), axis=-1, keepdims=True)
array([[0],
       [5]])
>>> min_abs(numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]))
0j
>>> min_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=0
... )  # doctest: +SKIP
array([ 0.+0.j,  0.+1.j,  2.+1.j])
>>> min_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=1
... )  # doctest: +SKIP
array([ 1.+0.j,  0.+0.j])
>>> min_abs(numpy.arange(24).reshape(2,3,4), axis=(1,2))
array([ 0, 12])
>>> min_abs(numpy.arange(24).reshape(2,3,4), axis=(0,2))
array([0, 4, 8])
>>> min_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(0,2),
...     keepdims=True
... )
array([[[0],
        [4],
        [8]]])
>>> min_abs(numpy.arange(24).reshape(2,3,4), axis=(2,0))
array([0, 4, 8])
>>> min_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(2,0),
...     return_indices=True
... )
(array([0, 4, 8]), (array([0, 0, 0]), array([0, 1, 2]), array([0, 0, 0])))
>>> min_abs(numpy.array([numpy.nan, -2, 3]))
nan
nanshe.util.xnumpy.nanmax_abs(*args, **kwargs)[source]

Takes the max of the given array subject to the absolute value (magnitude for complex numbers).

Parameters:
  • new_array (numpy.ndarray) – array to find the max (subject to the absolute value).
  • axis (int) – desired matches to find.
  • keepdims (bool) – ensure the number of dimensions is the same by inserting singleton dimensions at all the axes squished (excepting the last one).
  • return_indices (bool) – whether to return the indices of the maxes in addition to the maxes.
Returns:

an array or value that is the

largest (subject to the absolute value) or if return_indices the indices corresponding to the largest value(s), as well.

Return type:

(tuple of numpy.ndarray)

Examples

>>> nanmax_abs(numpy.arange(10))
9
>>> nanmax_abs(numpy.arange(10).reshape(2,5))
9
>>> nanmax_abs(numpy.arange(10).reshape(2,5), axis=0)
array([5, 6, 7, 8, 9])
>>> nanmax_abs(numpy.arange(10).reshape(2,5), axis=1)
array([4, 9])
>>> nanmax_abs(numpy.arange(10).reshape(2,5), axis=-1)
array([4, 9])
>>> nanmax_abs(
...     numpy.arange(10).reshape(2,5),
...     axis=-1,
...     keepdims=True
... )
array([[4],
       [9]])
>>> nanmax_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]])
... )
(1+3j)
>>> nanmax_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=0,
... )  # doctest: +SKIP
array([ 1.+0.j,  1.+1.j,  1.+3.j])
>>> nanmax_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=1,
... )  # doctest: +SKIP
array([ 2.+1.j,  1.+3.j])
>>> nanmax_abs(numpy.arange(24).reshape(2,3,4), axis=(1,2))
array([11, 23])
>>> nanmax_abs(numpy.arange(24).reshape(2,3,4), axis=(0,2))
array([15, 19, 23])
>>> nanmax_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(0,2),
...     keepdims=True
... )
array([[[15],
        [19],
        [23]]])
>>> nanmax_abs(numpy.arange(24).reshape(2,3,4), axis=(2,0))
array([15, 19, 23])
>>> nanmax_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(2,0),
...     return_indices=True
... )
(array([15, 19, 23]), (array([1, 1, 1]), array([0, 1, 2]), array([3, 3, 3])))
>>> nanmax_abs(numpy.array([numpy.nan, -2, 3]))
3.0
nanshe.util.xnumpy.nanmin_abs(*args, **kwargs)[source]

Takes the min of the given array subject to the absolute value (magnitude for complex numbers).

Parameters:
  • new_array (numpy.ndarray) – array to find the min (subject to the absolute value).
  • axis (int) – desired matches to find.
  • keepdims (bool) – ensure the number of dimensions is the same by inserting singleton dimensions at all the axes squished (excepting the last one).
  • return_indices (bool) – whether to return the indices of the mins in addition to the mins.
Returns:

an array or value that is the

smallest (subject to the absolute value) or if return_indices the indices corresponding to the smallest value(s), as well.

Return type:

(tuple of numpy.ndarray)

Examples

>>> nanmin_abs(numpy.arange(10))
0
>>> nanmin_abs(numpy.arange(10).reshape(2,5))
0
>>> nanmin_abs(numpy.arange(10).reshape(2,5), axis=0)
array([0, 1, 2, 3, 4])
>>> nanmin_abs(numpy.arange(10).reshape(2,5), axis=1)
array([0, 5])
>>> nanmin_abs(numpy.arange(10).reshape(2,5), axis=-1)
array([0, 5])
>>> nanmin_abs(
...     numpy.arange(10).reshape(2,5),
...     axis=-1,
...     keepdims=True
... )
array([[0],
       [5]])
>>> nanmin_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
... )
0j
>>> nanmin_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=0
... )  # doctest: +SKIP
array([ 0.+0.j,  0.+1.j,  2.+1.j])
>>> nanmin_abs(
...     numpy.array([[1+0j, 0+1j, 2+1j], [0+0j, 1+1j, 1+3j]]),
...     axis=1
... )  # doctest: +SKIP
array([ 1.+0.j,  0.+0.j])
>>> nanmin_abs(numpy.arange(24).reshape(2,3,4), axis=(1,2))
array([ 0, 12])
>>> nanmin_abs(numpy.arange(24).reshape(2,3,4), axis=(0,2))
array([0, 4, 8])
>>> nanmin_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(0,2),
...     keepdims=True
... )
array([[[0],
        [4],
        [8]]])
>>> nanmin_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(2,0)
... )
array([0, 4, 8])
>>> nanmin_abs(
...     numpy.arange(24).reshape(2,3,4),
...     axis=(2,0),
...     return_indices=True
... )
(array([0, 4, 8]), (array([0, 0, 0]), array([0, 1, 2]), array([0, 0, 0])))
>>> nanmin_abs(numpy.array([numpy.nan, -2, 3]))
-2.0
nanshe.util.xnumpy.norm(*args, **kwargs)[source]

Determines the norm of a vector or a set of vectors.

Parameters:
  • new_vector_set (numpy.ndarray) – either a single vector or a set of vectors (matrix).
  • ord (optional) – basically the same arguments as numpy.linalg.norm (though some are redundant here).
Returns:

an array with the norms of

all vectors in the set.

Return type:

(numpy.ndarray)

Examples

>>> norm(numpy.array([ 1,  0]), 2).ndim
0
>>> norm(numpy.array([[ 1,  0]]), 2).ndim
1
>>> norm(numpy.array([ 1,  0]), 2)
array(1.0)
>>> norm(numpy.array([ 1,  0]), 1)
array(1.0)
>>> norm(numpy.array([[ 1,  0]]), 2)
array([ 1.])
>>> norm(numpy.array([[ 1,  1]]), 1)
array([ 2.])
>>> norm(numpy.array([[ 1,  1]]), 2)
array([ 1.41421356])
>>> norm(numpy.array([[ True,  False]]), 1)
array([ 1.])
>>> norm(numpy.array([[ True,  False]]), 2)
array([ 1.])
>>> norm(numpy.array([[ True,  True]]), 1)
array([ 2.])
>>> norm(numpy.array([[ True,  True]]), 2)
array([ 1.41421356])
>>> norm(numpy.array([[ 1,  1,  1], [ 1,  0,  1]]), 1)
array([ 3.,  2.])
>>> norm(numpy.array([[ 1,  1,  1], [ 1,  0,  1]]), 2)
array([ 1.73205081,  1.41421356])
>>> norm(numpy.array([ 0,  1,  2]))
array(2.23606797749979)
>>> norm(numpy.zeros((0, 2,)))
array([], shape=(0, 2), dtype=float64)
>>> norm(numpy.zeros((2, 0,)))
array([], shape=(2, 0), dtype=float64)
nanshe.util.xnumpy.numpy_structured_array_dtype_generator(*args, **kwargs)[source]

Takes a NumPy structured array and returns a generator that goes over each name in the structured array and yields the name, type, and shape (for the given name).

Parameters:new_array (numpy.ndarray) – the array to get the info dtype from.
Raises:(NotNumPyStructuredArrayType) – if it is a normal NumPy array.
Returns:An iterator yielding tuples.
Return type:(iterator)
nanshe.util.xnumpy.numpy_structured_array_dtype_list(*args, **kwargs)[source]

Takes any NumPy array and returns either a list for a NumPy structured array via numpy_structured_array_dtype_generator or if it is a normal NumPy array it returns the type used.

Parameters:new_array (numpy.ndarray) – the array to get the dtype info from.
Returns:
something that can be given to
numpy.dtype to obtain the new_array.dtype, but is more malleable than a numpy.dtype.
Return type:(list or type)
nanshe.util.xnumpy.pair_dot_product(*args, **kwargs)[source]

Determines the dot product between the vectors in the set.

Parameters:new_vector_set (numpy.ndarray) – set of vectors.
Returns:
an array with the distances
between each pair of vectors.
Return type:(numpy.ndarray)

Examples

>>> (pair_dot_product(numpy.eye(2)) == numpy.eye(2)).all()
True
>>> (pair_dot_product(numpy.eye(10)) == numpy.eye(10)).all()
True
>>> pair_dot_product(numpy.array([[ 1,  0]]))
array([[ 1.]])
>>> pair_dot_product(numpy.array([[ 1.,  0.]]))
array([[ 1.]])
>>> pair_dot_product(numpy.array([[-1,  0]]))
array([[ 1.]])
>>> pair_dot_product(numpy.array([[ 0,  1]]))
array([[ 1.]])
>>> pair_dot_product(numpy.array([[ 1,  1]]))
array([[ 2.]])
>>> pair_dot_product(numpy.array([[ True,  False]]))
array([[ 1.]])
nanshe.util.xnumpy.pair_dot_product_normalized(*args, **kwargs)[source]

Determines the dot product between a pair of vectors from each set and divides them by the norm of the two.

Parameters:
  • new_vector_set (numpy.ndarray) – set of vectors.
  • ord (optional) – basically the same arguments as numpy.linalg.norm.
  • float_type (type) – some form of float
Returns:

an array with the normalized

distances between each pair of vectors.

Return type:

(numpy.ndarray)

Examples

>>> (pair_dot_product_normalized(numpy.eye(2)) == numpy.eye(2)).all()
True
>>> (pair_dot_product_normalized(numpy.eye(10)) == numpy.eye(10)).all()
True
>>> pair_dot_product_normalized(
...     numpy.array([[ 1,  0]])
... )
array([[ 1.]])
>>> pair_dot_product_normalized(
...     numpy.array([[ 1.,  0.]])
... )
array([[ 1.]])
>>> pair_dot_product_normalized(
...     numpy.array([[-1,  0]]),
... )
array([[ 1.]])
>>> pair_dot_product_normalized(
...     numpy.array([[ 0,  1]]),
... )
array([[ 1.]])
>>> pair_dot_product_normalized(
...     numpy.array([[ 1,  1]]),
... )
array([[ 1.]])
>>> pair_dot_product_normalized(
...     numpy.array([[ True,  False]]),
... )
array([[ 1.]])
>>> pair_dot_product_normalized(
...     numpy.arange(6).reshape((2,3)),
...     2
... )
array([[ 1.        ,  0.88543774],
       [ 0.88543774,  1.        ]])
>>> pair_dot_product_normalized(
...     numpy.array([[ True, False, False],
...                  [ True, False,  True]]),
...     3
... )
array([[ 1.        ,  0.79370053],
       [ 0.79370053,  1.25992105]])
nanshe.util.xnumpy.pair_dot_product_partially_normalized(*args, **kwargs)[source]

Determines the dot product between the two pairs of vectors from each set and creates a tuple with the dot product divided by one norm or the other.

Parameters:
  • new_vector_set (numpy.ndarray) – set of vectors.
  • ord (optional) – basically the same argument as numpy.linalg.norm
  • float_type (type) – some form of float
Returns:

an array with the normalized

distances between each pair of vectors.

Return type:

(numpy.ndarray)

Examples

>>> (pair_dot_product_partially_normalized(numpy.eye(2), 2) == numpy.eye(2)).all()
True
>>> (pair_dot_product_partially_normalized(numpy.eye(10), 2) == numpy.eye(10)).all()
True
>>> pair_dot_product_partially_normalized(
...     numpy.array([[ 1,  0]]),
...     2
... )
array([[ 1.]])
>>> pair_dot_product_partially_normalized(
...     numpy.array([[-1,  0]]),
...     2
... )
array([[ 1.]])
>>> pair_dot_product_partially_normalized(
...     numpy.array([[ 1,  1]]),
...     2
... )
array([[ 1.41421356]])
>>> pair_dot_product_partially_normalized(
...     numpy.array([[ 1,  1]]),
...     1
... )
array([[ 1.]])
>>> pair_dot_product_partially_normalized(
...     numpy.array([[ True,  False]]),
...     2
... )
array([[ 1.]])
>>> pair_dot_product_partially_normalized(
...     numpy.array([[ True,  True]]),
...     1
... )
array([[ 1.]])
>>> pair_dot_product_partially_normalized(
...     numpy.arange(6).reshape((2,3)),
...     2
... )
array([[ 2.23606798,  6.26099034],
       [ 1.97989899,  7.07106781]])
>>> pair_dot_product_partially_normalized(
...     numpy.array([[ True, False, False],
...                  [ True, False,  True]]),
...     3
... )
array([[ 1.        ,  1.        ],
       [ 0.79370053,  1.58740105]])
nanshe.util.xnumpy.quantile(data, probs, axis=None)[source]

Determines the quantiles for given data much like MATLAB’s function.

Parameters:
  • data (numpy.ndarray) – to find the quantiles of.
  • probs (int or float or numpy.ndarray) – either some sort of integer for the number of quantiles or a single float specifying which quantile to get or an array of floats specifying the division for each quantile in the range (0, 1).
  • axis (int or None) – the axis to perform the calculation on (if default (None) then all, otherwise only on a particular axis.
Returns:

an array with the

quantiles (the first dimension will be the same length as probs).

Return type:

(numpy.ma.MaskedArray)

Examples

>>> quantile(numpy.array([ 1.,  2.,  3.]), 2)
masked_array(data = [ 1.5  2.5],
             mask = False,
       fill_value = nan)
<BLANKLINE>
>>> quantile(numpy.array([ 1.,  2.,  3.]), 3)
masked_array(data = [ 1.25  2.    2.75],
             mask = False,
       fill_value = nan)
<BLANKLINE>
>>> quantile(
...     numpy.array([ 1.,  2.,  3.]),
...     numpy.array([ 0.25,  0.5,  0.75])
... )
masked_array(data = [ 1.25  2.    2.75],
             mask = False,
       fill_value = nan)
<BLANKLINE>
>>> quantile(numpy.array([ 1.,  2.,  3.]), 0.5)
masked_array(data = [ 2.],
             mask = False,
       fill_value = nan)
<BLANKLINE>
>>> a = numpy.array([[-1.1176, -0.0679, -0.3031,  0.8261],
...                  [ 1.2607, -0.1952,  0.023 ,  1.527 ],
...                  [ 0.6601, -0.2176,  0.0513,  0.4669]])
>>> quantile(a, 2, axis = 0)
masked_array(data =
 [[-0.22875 -0.2064  -0.14005  0.6465 ]
 [ 0.9604  -0.13155  0.03715  1.17655]],
             mask =
 False,
       fill_value = nan)
<BLANKLINE>
nanshe.util.xnumpy.renumber_label_image(*args, **kwargs)[source]

Takes a label image with non-consecutive numbering and renumbers it to be consecutive. Returns the relabeled image, a mapping from the old labels (by index) to the new ones, and a mapping from the new labels back to the old labels.

Parameters:new_array (numpy.ndarray) – the label image.
Returns:
the
relabeled label image, the forward label mapping and the reverse label mapping
Return type:(numpy.ndarray, numpy.ndarray, numpy.ndarray)

Examples

>>> renumber_label_image(numpy.array([1, 2, 3]))
(array([1, 2, 3]), array([0, 1, 2, 3]), array([0, 1, 2, 3]))
>>> renumber_label_image(numpy.array([1, 2, 4]))
(array([1, 2, 3]), array([0, 1, 2, 0, 3]), array([0, 1, 2, 4]))
>>> renumber_label_image(numpy.array([0, 1, 2, 3]))
(array([0, 1, 2, 3]), array([0, 1, 2, 3]), array([0, 1, 2, 3]))
>>> renumber_label_image(numpy.array([0, 1, 2, 4]))
(array([0, 1, 2, 3]), array([0, 1, 2, 0, 3]), array([0, 1, 2, 4]))
nanshe.util.xnumpy.roll(*args, **kwargs)[source]

Like numpy.roll, but generalizes to include a roll for each axis of new_array.

Note

Right shift occurs with a positive and left occurs with a negative.

Parameters:
  • new_array (numpy.ndarray) – array to roll axes of.
  • shift (container of ints) – some sort of container (list, tuple, array) of ints specifying how much to roll each axis.
  • out (numpy.ndarray) – array to store the results in.
  • to_mask (bool) – Makes the result a masked array with the portion that rolled off masked.
Returns:

result of the roll.

Return type:

out(numpy.ndarray)

Examples

>>> roll(numpy.arange(20), numpy.array([0]))
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19])
>>> roll(numpy.arange(20), numpy.array([1]))
array([19,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
       16, 17, 18])
>>> roll(numpy.arange(20), numpy.array([2]))
array([18, 19,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
       15, 16, 17])
>>> roll(numpy.arange(20), numpy.array([3]))
array([17, 18, 19,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
       14, 15, 16])
>>> roll(numpy.arange(20), numpy.array([4]))
array([16, 17, 18, 19,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12,
       13, 14, 15])
>>> roll(numpy.arange(20), numpy.array([5]))
array([15, 16, 17, 18, 19,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11,
       12, 13, 14])
>>> roll(numpy.arange(20), numpy.array([6]))
array([14, 15, 16, 17, 18, 19,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
       11, 12, 13])
>>> roll(numpy.arange(20), numpy.array([8]))
array([12, 13, 14, 15, 16, 17, 18, 19,  0,  1,  2,  3,  4,  5,  6,  7,  8,
        9, 10, 11])
>>> roll(numpy.arange(20), numpy.array([-1]))
array([ 1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17,
       18, 19,  0])
>>> roll(
...     numpy.arange(10).reshape(2,5),
...     numpy.ones((2,), dtype=int)
... )
array([[9, 5, 6, 7, 8],
       [4, 0, 1, 2, 3]])
>>> roll(numpy.arange(10).reshape(2,5), numpy.arange(2))
array([[4, 0, 1, 2, 3],
       [9, 5, 6, 7, 8]])
>>> roll(numpy.arange(10).reshape(2,5), numpy.arange(1, 3))
array([[8, 9, 5, 6, 7],
       [3, 4, 0, 1, 2]])
>>> roll(numpy.arange(10).reshape(2,5), numpy.array([2, 5]))
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> roll(numpy.arange(10).reshape(2,5), numpy.array([5, 3]))
array([[7, 8, 9, 5, 6],
       [2, 3, 4, 0, 1]])
>>> roll(numpy.arange(10).reshape(2,5), numpy.array([-1, -1]))
array([[6, 7, 8, 9, 5],
       [1, 2, 3, 4, 0]])
>>> roll(
...     numpy.arange(10).reshape(2,5),
...     numpy.array([1, -1]),
...     to_mask=True
... )
masked_array(data =
 [[-- -- -- -- --]
 [1 2 3 4 --]],
             mask =
 [[ True  True  True  True  True]
 [False False False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> roll(
...     numpy.arange(10).reshape(2,5),
...     numpy.array([0, -1]),
...     to_mask=True
... )
masked_array(data =
 [[1 2 3 4 --]
 [6 7 8 9 --]],
             mask =
 [[False False False False  True]
 [False False False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> a = numpy.ma.arange(10).reshape(2,5).copy()
>>> roll(a, numpy.array([0, -1]), to_mask=True, out=a)
masked_array(data =
 [[1 2 3 4 --]
 [6 7 8 9 --]],
             mask =
 [[False False False False  True]
 [False False False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> a
masked_array(data =
 [[1 2 3 4 --]
 [6 7 8 9 --]],
             mask =
 [[False False False False  True]
 [False False False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> a = numpy.ma.arange(10).reshape(2,5).copy(); b = a[:, 1:-1]
>>> roll(b, numpy.array([0, -1]), to_mask=True, out=b)
masked_array(data =
 [[2 3 --]
 [7 8 --]],
             mask =
 [[False False  True]
 [False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> b
masked_array(data =
 [[2 3 --]
 [7 8 --]],
             mask =
 [[False False  True]
 [False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> a # this should work, but it doesn't. # doctest: +SKIP
masked_array(data =
 [[0 2 3 -- 4]
 [5 7 8 -- 9]],
             mask =
 [[False False False  True False]
 [False False False  True False]],
       fill_value = 999999)
<BLANKLINE>
>>> a = numpy.ma.arange(10).reshape(2,5).copy(); b = a[:, 1:-1]
>>> roll(b, numpy.array([0, -1]), to_mask=True, out=b)
masked_array(data =
 [[2 3 --]
 [7 8 --]],
             mask =
 [[False False  True]
 [False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> b
masked_array(data =
 [[2 3 --]
 [7 8 --]],
             mask =
 [[False False  True]
 [False False  True]],
       fill_value = 999999)
<BLANKLINE>
>>> a.mask = numpy.ma.getmaskarray(a); a[:, 1:-1] = b; a
masked_array(data =
 [[0 2 3 -- 4]
 [5 7 8 -- 9]],
             mask =
 [[False False False  True False]
 [False False False  True False]],
       fill_value = 999999)
<BLANKLINE>
>>> a = numpy.arange(10).reshape(2,5)
>>> b = a.copy()
>>> roll(a, numpy.arange(1, 3), b)
array([[8, 9, 5, 6, 7],
       [3, 4, 0, 1, 2]])
>>> a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> b
array([[8, 9, 5, 6, 7],
       [3, 4, 0, 1, 2]])
>>> a = numpy.arange(10).reshape(2,5); a
array([[0, 1, 2, 3, 4],
       [5, 6, 7, 8, 9]])
>>> roll(a, numpy.arange(1, 3), a)
array([[8, 9, 5, 6, 7],
       [3, 4, 0, 1, 2]])
>>> a
array([[8, 9, 5, 6, 7],
       [3, 4, 0, 1, 2]])
nanshe.util.xnumpy.squish(*args, **kwargs)[source]

Moves the given axes to the last dimensions of the array and then squishes them into one dimension.

Note

Returns a view if possible. However, if the axes provided are not consecutive integers when placed in the range [0, new_array.ndim), due to reshaping, the returned array will be a copy.

Parameters:
  • new_array (numpy.ndarray) – array to find the max (subject to the absolute value).
  • axis (int or collection of ints) – desired axes to squish.
  • keepdims (bool) – ensure the number of dimensions is the same plus one by inserting singleton dimensions at all the axes squished.
Returns:

an array with one dimension at

the end containing all the given axes.

Return type:

(numpy.ndarray)

Examples

>>> a = numpy.arange(24).reshape(2,3,4).copy(); a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.base is None
True
>>> b = squish(a); b
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> b.base is a
True
>>> b = squish(a, axis=0); b
array([[[ 0, 12],
        [ 1, 13],
        [ 2, 14],
        [ 3, 15]],
<BLANKLINE>
       [[ 4, 16],
        [ 5, 17],
        [ 6, 18],
        [ 7, 19]],
<BLANKLINE>
       [[ 8, 20],
        [ 9, 21],
        [10, 22],
        [11, 23]]])
>>> b.base is a
True
>>> b = squish(a, axis=2); b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
True
>>> b = squish(a, axis=-1); b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
True
>>> b = squish(a, axis=(0,1)); b
array([[ 0,  4,  8, 12, 16, 20],
       [ 1,  5,  9, 13, 17, 21],
       [ 2,  6, 10, 14, 18, 22],
       [ 3,  7, 11, 15, 19, 23]])
>>> b.base is a
True
>>> b = squish(a, axis=(1, 0)); b
array([[ 0, 12,  4, 16,  8, 20],
       [ 1, 13,  5, 17,  9, 21],
       [ 2, 14,  6, 18, 10, 22],
       [ 3, 15,  7, 19, 11, 23]])
>>> b.base is a
False
>>> b = squish(a, axis=(0, 2)); b
array([[ 0,  1,  2,  3, 12, 13, 14, 15],
       [ 4,  5,  6,  7, 16, 17, 18, 19],
       [ 8,  9, 10, 11, 20, 21, 22, 23]])
>>> b.base is a
False
>>> b = squish(a, axis=(0, 2), keepdims=True); b
array([[[[ 0,  1,  2,  3, 12, 13, 14, 15]],
<BLANKLINE>
        [[ 4,  5,  6,  7, 16, 17, 18, 19]],
<BLANKLINE>
        [[ 8,  9, 10, 11, 20, 21, 22, 23]]]])
>>> b = squish(a, axis=(1, 0), keepdims=True); b
array([[[[ 0, 12,  4, 16,  8, 20],
         [ 1, 13,  5, 17,  9, 21],
         [ 2, 14,  6, 18, 10, 22],
         [ 3, 15,  7, 19, 11, 23]]]])
nanshe.util.xnumpy.symmetric_line_filter(*args, **kwargs)[source]

Creates a boolean array mask for a line. This mask has size for the length of the line and number of empty lines beside it in any orthogonal direction. The mask has dimensions equal to ndims and the line is placed along dimension dim.

Parameters:
  • size (int) – the distance from the center of the filter to the nearest edge.
  • ndims (int) – the number of dimensions for the filter.
  • dim (int) – the dimension to put the line along.
Returns:

a boolean array to use as the filter.

Return type:

(numpy.ndarray)

Examples

>>> symmetric_line_filter(1)
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> symmetric_line_filter(1, ndims = 2, dim = -1)
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> symmetric_line_filter(1, ndims = 2, dim = 1)
array([[False, False, False],
       [ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> symmetric_line_filter(1, ndims = 3)
array([[[False, False, False],
        [False, False, False],
        [False, False, False]],
<BLANKLINE>
       [[False, False, False],
        [ True,  True,  True],
        [False, False, False]],
<BLANKLINE>
       [[False, False, False],
        [False, False, False],
        [False, False, False]]], dtype=bool)
nanshe.util.xnumpy.tagging_reorder_array(*args, **kwargs)[source]

Transforms one axis ordering to another giving a view of the array (unless otherwise specified).

Parameters:
  • new_array (numpy.ndarray) – the array to reorder
  • from_axis_order (str or list of str) – current labeled axis order.
  • to_axis_order (str or list of str) – desired labeled axis order
  • to_copy (bool) – whether to return a view or a copy
Returns:

an array with the axis

order specified (view).

Return type:

(numpy.ndarray)

Examples

>>> tagging_reorder_array(numpy.ones((1,2,3,4,5))).shape
(1, 2, 3, 4, 5)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     from_axis_order = "tzyxc"
... ).shape
(1, 2, 3, 4, 5)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     to_axis_order = "tzyxc"
... ).shape
(1, 2, 3, 4, 5)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     from_axis_order = "tzyxc",
...     to_axis_order = "tzyxc"
... ).shape
(1, 2, 3, 4, 5)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     from_axis_order = "txyzc"
... ).shape
(1, 4, 3, 2, 5)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     from_axis_order = "ctxyz"
... ).shape
(2, 5, 4, 3, 1)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     to_axis_order = "txyzc"
... ).shape
(1, 4, 3, 2, 5)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     from_axis_order = ["c","t","x","y","z"]
... ).shape
(2, 5, 4, 3, 1)
>>> tagging_reorder_array(
...     numpy.ones((1,2,3,4,5)),
...     to_axis_order = ["t","x","y","z","c"]
... ).shape
(1, 4, 3, 2, 5)
nanshe.util.xnumpy.threshold_array(*args, **kwargs)[source]

Given a threshold, this function compares the given array to it to see which entries match.

Parameters:
  • an_array (numpy.ndarray) – an array to threshold.
  • threshold (int or float or numpy.ndarray) – something to compare to.
  • include_below (bool) – whether values below the threshold count or ones above it.
  • is_closed (bool) – whether to include values equal to the threshold
Returns:

a mask of entries

reflecting the threshold.

Return type:

out(numpy.ndarray)

Examples

>>> threshold_array(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=True,
...     is_closed=False
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=True,
...     is_closed=True
... )
array([[ True, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=False,
...     is_closed=True
... )
array([[ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=False,
...     is_closed=False
... )
array([[False,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3),
...     5,
...     include_below=True,
...     is_closed=True
... )
array([[ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3),
...     6,
...     include_below=True,
...     is_closed=False
... )
array([[ True,  True,  True],
       [ True,  True,  True]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3),
...     2*numpy.ones((2,3)),
...     include_below=True,
...     is_closed=True
... )
array([[ True,  True,  True],
       [False, False, False]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3) % 2,
...     1,
...     include_below=False,
...     is_closed=True
... )
array([[False,  True, False],
       [ True, False,  True]], dtype=bool)
>>> threshold_array(
...     numpy.arange(6).reshape(2,3) % 2,
...     1,
...     include_below=True,
...     is_closed=False
... )
array([[ True, False,  True],
       [False,  True, False]], dtype=bool)
nanshe.util.xnumpy.threshold_metric(*args, **kwargs)[source]

Given a threshold, this function finds which entries uniquely match given the threshold.

Parameters:
  • a_metric (numpy.ndarray) – an array to threshold.
  • threshold (int or float or numpy.ndarray) – something to compare to.
  • include_below (bool) – whether values below the threshold count or ones above it.
  • is_closed (bool) – whether to include values equal to the threshold
Returns:

a mapping of

unique matches.

Return type:

out(numpy.ndarray)

Examples

>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=True,
...     is_closed=False
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=True,
...     is_closed=True
... )
array([[ True, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=False,
...     is_closed=True
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     0,
...     include_below=False,
...     is_closed=False
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     5,
...     include_below=True,
...     is_closed=True
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     6,
...     include_below=True,
...     is_closed=False
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     2*numpy.ones((2,3)),
...     include_below=True,
...     is_closed=True
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3),
...     numpy.arange(0, 12, 2).reshape(2,3),
...     include_below=True,
...     is_closed=True
... )
array([[False, False, False],
       [False, False, False]], dtype=bool)
>>> threshold_metric(
...     numpy.arange(6).reshape(2,3) % 2,
...     1,
...     include_below=True,
...     is_closed=False
... )
array([[False, False, False],
       [False,  True, False]], dtype=bool)
nanshe.util.xnumpy.truncate_masked_frames(*args, **kwargs)[source]

Takes frames that have been shifted and truncates out the portion, which is an intact rectangular shape.

Parameters:shifted_frames (numpy.ma.masked_array) – Image stack to register (time is the first dimension uses C-order tyx or tzyx).
Returns:
an array containing a
subsection of the stack that has no mask.
Return type:(numpy.ndarray)

Examples

>>> a = numpy.arange(60).reshape(3,5,4)
>>> a = numpy.ma.masked_array(
...     a, mask=numpy.zeros(a.shape, dtype=bool), shrink=False
... )
>>> a[0, :1, :] = numpy.ma.masked; a[0, :, -1:] = numpy.ma.masked
>>> a[1, :2, :] = numpy.ma.masked; a[1, :, :0] = numpy.ma.masked
>>> a[2, :0, :] = numpy.ma.masked; a[2, :, :1] = numpy.ma.masked
>>> a
masked_array(data =
 [[[-- -- -- --]
  [4 5 6 --]
  [8 9 10 --]
  [12 13 14 --]
  [16 17 18 --]]
<BLANKLINE>
 [[-- -- -- --]
  [-- -- -- --]
  [28 29 30 31]
  [32 33 34 35]
  [36 37 38 39]]
<BLANKLINE>
 [[-- 41 42 43]
  [-- 45 46 47]
  [-- 49 50 51]
  [-- 53 54 55]
  [-- 57 58 59]]],
             mask =
 [[[ True  True  True  True]
  [False False False  True]
  [False False False  True]
  [False False False  True]
  [False False False  True]]
<BLANKLINE>
 [[ True  True  True  True]
  [ True  True  True  True]
  [False False False False]
  [False False False False]
  [False False False False]]
<BLANKLINE>
 [[ True False False False]
  [ True False False False]
  [ True False False False]
  [ True False False False]
  [ True False False False]]],
       fill_value = 999999)
<BLANKLINE>
>>> truncate_masked_frames(a)
array([[[ 9, 10],
        [13, 14],
        [17, 18]],
<BLANKLINE>
       [[29, 30],
        [33, 34],
        [37, 38]],
<BLANKLINE>
       [[49, 50],
        [53, 54],
        [57, 58]]])
nanshe.util.xnumpy.unique_mapping(*args, **kwargs)[source]

Take a binary mapping between two sets and excludes portions of the mapping that are not one-to-one.

Parameters:
  • mapping (numpy.ndarray) – bool array mapping between to sets.
  • out (numpy.ndarray) – where to store the results.
Returns:

the results returned.

Return type:

out(numpy.ndarray)

Examples

>>> unique_mapping(numpy.zeros((2,2), dtype=bool))
array([[False, False],
       [False, False]], dtype=bool)
>>> unique_mapping(numpy.ones((2,2), dtype=bool))
array([[False, False],
       [False, False]], dtype=bool)
>>> unique_mapping(numpy.eye(2, dtype=bool))
array([[ True, False],
       [False,  True]], dtype=bool)
>>> unique_mapping(
...     numpy.array([[ True,  True],
...                  [ True, False]], dtype=bool)
... )
array([[False, False],
       [False, False]], dtype=bool)
>>> unique_mapping(
...     numpy.array([[ True, False],
...                  [False, False]], dtype=bool)
... )
array([[ True, False],
       [False, False]], dtype=bool)
>>> a = numpy.ones((2,2), dtype=bool); b = a.copy(); a
array([[ True,  True],
       [ True,  True]], dtype=bool)
>>> unique_mapping(a, out=b)
array([[False, False],
       [False, False]], dtype=bool)
>>> b
array([[False, False],
       [False, False]], dtype=bool)
>>> unique_mapping(a, out=a)
array([[False, False],
       [False, False]], dtype=bool)
>>> a
array([[False, False],
       [False, False]], dtype=bool)
nanshe.util.xnumpy.unsquish(*args, **kwargs)[source]

Inverts the squish operation given the shape and the axis/axes to extract from the last dimension.

Parameters:
  • new_array (numpy.ndarray) – array to find the max (subject to the absolute value).
  • shape (collection of ints) – should be the shape of the result array (or the array before squishing).
  • axis (int or collection of ints) – desired axes to remove from the last axis.
Returns:

an array with the shape

provided and the axes removed from the end.

Return type:

(numpy.ndarray)

Examples

>>> a = numpy.arange(24).reshape(2,3,4).copy(); a
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.base is None
True
>>> a.reshape(-1)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> b = unsquish(a.reshape(-1), (2,3,4)); b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
True
>>> a.transpose(1,2,0)
array([[[ 0, 12],
        [ 1, 13],
        [ 2, 14],
        [ 3, 15]],
<BLANKLINE>
       [[ 4, 16],
        [ 5, 17],
        [ 6, 18],
        [ 7, 19]],
<BLANKLINE>
       [[ 8, 20],
        [ 9, 21],
        [10, 22],
        [11, 23]]])
>>> b = unsquish(a.transpose(1,2,0), (2,3,4), axis=0); b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
True
>>> b = unsquish(a, (2,3,4), axis=2); b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
True
>>> b = unsquish(a, (2,3,4), axis=-1); b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
True
>>> a.transpose(2,0,1).reshape(a.shape[2], -1)
array([[ 0,  4,  8, 12, 16, 20],
       [ 1,  5,  9, 13, 17, 21],
       [ 2,  6, 10, 14, 18, 22],
       [ 3,  7, 11, 15, 19, 23]])
>>> b = unsquish(
...     a.transpose(2,0,1).reshape(a.shape[2], -1),
...     (2,3,4),
...     axis=(0,1)
... )
>>> b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
True
>>> a.transpose(2, 1, 0).reshape(a.shape[2], -1)
array([[ 0, 12,  4, 16,  8, 20],
       [ 1, 13,  5, 17,  9, 21],
       [ 2, 14,  6, 18, 10, 22],
       [ 3, 15,  7, 19, 11, 23]])
>>> b = unsquish(
...     a.transpose(2, 1, 0).reshape(a.shape[2], -1),
...     (2,3,4),
...     axis=(1,0)
... )
>>> b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
False
>>> a.transpose(1, 0, 2).reshape(a.shape[1], -1)
array([[ 0,  1,  2,  3, 12, 13, 14, 15],
       [ 4,  5,  6,  7, 16, 17, 18, 19],
       [ 8,  9, 10, 11, 20, 21, 22, 23]])
>>> b = unsquish(
...     a.transpose(1, 0, 2).reshape(a.shape[1], -1),
...     (2,3,4),
...     axis=(0,2)
... )
>>> b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> b.base is a
False
>>> a.transpose(1, 0, 2).reshape(a.shape[1], -1)[None]
array([[[ 0,  1,  2,  3, 12, 13, 14, 15],
        [ 4,  5,  6,  7, 16, 17, 18, 19],
        [ 8,  9, 10, 11, 20, 21, 22, 23]]])
>>> b = unsquish(
...     a.transpose(1, 0, 2).reshape(a.shape[1], -1)[None],
...     (2,3,4),
...     axis=(0,2)
... )
>>> b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a.transpose(2, 1, 0).reshape(a.shape[2], -1)[None, None]
array([[[[ 0, 12,  4, 16,  8, 20],
         [ 1, 13,  5, 17,  9, 21],
         [ 2, 14,  6, 18, 10, 22],
         [ 3, 15,  7, 19, 11, 23]]]])
>>> b = unsquish(
...     a.transpose(2, 1, 0).reshape(a.shape[2], -1)[None, None],
...     (2,3,4),
...     axis=(1,0)
... )
>>> b
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],
<BLANKLINE>
       [[12, 13, 14, 15],
        [16, 17, 18, 19],
        [20, 21, 22, 23]]])