topact.densetools

Tools for manipulating dense matrices.

Module Contents

Functions

_get_pad(shift)

_get_slice(shift)

first_nonzero_1d(vector)

Returns the inedx of the first nonzero element of a 1D array.

first_nonzero_2d(array, axis)

last_nonzero_2d(array, axis)

translate(matrix, x_shift, y_shift)

Translates the matrix by the given shift, filling in with 0s.

pool(matrix, radius)

Replaces each entry by the sum of all neighbouring entries.

density(matrix, radius)

Computes the density at each entry in the array at the given radius.

density_hull(matrix, radius, threshold)

topact.densetools._get_pad(shift)
Parameters

shift (int) –

Return type

tuple[int, int]

topact.densetools._get_slice(shift)
Parameters

shift (int) –

Return type

slice

topact.densetools.first_nonzero_1d(vector)

Returns the inedx of the first nonzero element of a 1D array.

Parameters

vector (numpy.typing.NDArray) – a 1-dimensional numpy array

Returns

The smallest i >= 0 such that vector[i] != 0. If no such i exists, then -1.

Return type

int

topact.densetools.first_nonzero_2d(array, axis)
Parameters
  • array (numpy.typing.NDArray) –

  • axis (int) –

Return type

numpy.typing.NDArray

topact.densetools.last_nonzero_2d(array, axis)
Parameters
  • array (numpy.typing.NDArray) –

  • axis (int) –

Return type

numpy.typing.NDArray

topact.densetools.translate(matrix, x_shift, y_shift)

Translates the matrix by the given shift, filling in with 0s.

If matrix’ is the output, it has the same shape (M,N) as the input matrix with entires matrix’[i,j] = matrix[i-y, j-x] whenever 0 <= i-y < M and 0 <= j-x < N, and 0 otherwise.

Parameters
  • matrix (numpy.typing.NDArray) – A 2D numpy array

  • x_shift (int) – The signed number of places the matrix is shifted horizontally

  • y_shift (int) – The signed number of places the matrix is shifted vertically

Returns

A matrix of the same shape as the input matrix, but with entries shifted by the given values in each direction. Missing values are set to 0.

Return type

numpy.typing.NDArray

topact.densetools.pool(matrix, radius)

Replaces each entry by the sum of all neighbouring entries.

Parameters
  • matrix (numpy.typing.NDArray) –

  • radius (int) –

Return type

numpy.typing.NDArray

topact.densetools.density(matrix, radius)

Computes the density at each entry in the array at the given radius.

Parameters
  • matrix (numpy.typing.NDArray) –

  • radius (int) –

Return type

numpy.typing.NDArray

topact.densetools.density_hull(matrix, radius, threshold)
Parameters
  • matrix (numpy.typing.NDArray) –

  • radius (int) –

  • threshold (float) –

Return type

numpy.typing.NDArray