Skip to content

Instantly share code, notes, and snippets.

@maharjun
Created May 4, 2023 04:27
Show Gist options
  • Save maharjun/1d9b9f01d78b3dc0f2fb066abea1eeff to your computer and use it in GitHub Desktop.
Save maharjun/1d9b9f01d78b3dc0f2fb066abea1eeff to your computer and use it in GitHub Desktop.
Numpy utils
# BSD 3-Clause License
#
# Copyright (c) 2023, maharjun
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
from numbers import Integral
import numpy as np
def almost_equal_split(ndarray, n_splits, axis=0):
"""
Takes an ndarray and splits it along the specified axis into n_splits
sub-arrays where the length of the largest sub-array is at most one more
than the length of the smallest sub-array
:param ndarray: a numpy ndarray
:param n_splits: an integer representing the number of pieces to split array into
:param axis: an integer representing the axis along which to split
"""
assert isinstance(ndarray, np.ndarray), "ndarray must be a numpy ndarray"
assert isinstance(n_splits, Integral) and n_splits > 0, "'n_splits' must be a positive integer"
assert isinstance(axis, Integral) and 0 <= axis < ndarray.ndim, "The axis must be an integer from 0 to ndarray.ndim - 1"
axis_len = ndarray.shape[axis]
elems_per_split_lower = axis_len // n_splits
split_lengths = elems_per_split_lower * np.ones(n_splits, dtype=np.int64)
split_lengths[:axis_len % n_splits] += 1
assert np.sum(split_lengths) == axis_len
split_indices = np.cumsum(split_lengths)[:-1]
return np.split(ndarray, split_indices, axis=axis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment