<commit_before># TODO : Doc, Tests, PEP8 check

import numpy as np
from skimage.util.shape import view_as_blocks

def downsample(image, factors, method='sum'):

    # works only if image.shape is perfectly divisible by factors
    out = view_as_blocks(image, factors)
    block_shape = out.shape

    if method == 'sum':
        for i in range(len(block_shape)/2):
            out = out.sum(-1)
    else:<commit_msg>Padding ndarray with zeros to support downsampling by any integer factor<commit_after># TODO : Doc, Tests, PEP8 check

import numpy as np
from skimage.util.shape import view_as_blocks


def _pad_asymmetric_zeros(arr, pad_amt, axis=-1):
    """Pads `arr` by `pad_amt` along specified `axis`"""
    if axis == -1:
        axis = arr.ndim - 1

    zeroshape = tuple([x if i != axis else pad_amt
                       for (i, x) in enumerate(arr.shape)])

    return np.concatenate((arr, np.zeros(zeroshape, dtype=arr.dtype)),
                          axis=axis)


def downsample(image, factors, method='sum'):

    pad_size = []
    if len(factors) != image.ndim:
        raise ValueError("'factors' must have the same length "
                         "as 'image.shape'")
    else:
        for i in range(len(factors)):
            if image.shape[i] % factors[i] != 0:
                pad_size.append(factors[i] - (image.shape[i] % factors[i]))
            else:
                pad_size.append(0)

    for i in range(len(pad_size)):
        image = _pad_asymmetric_zeros(image, pad_size[i], i)

    out = view_as_blocks(image, factors)
    block_shape = out.shape

    if method == 'sum':
        for i in range(len(block_shape)/2):
            out = out.sum(-1)
    else:
{'fraction_non_alphanumeric': 0.11841326228537596, 'fraction_numerical': 0.006512729425695678, 'mean_word_length': 2.441955193482688, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '95672', 'n_tokens_mistral': 557, 'n_tokens_neox': 535, 'n_words': 153}