<commit_before>import numpy as np
import numpy.lib.stride_tricks as stride
import mpl_toolkits.mplot3d
import matplotlib.pyplot as plt


def window_views(xsize=3, ysize=3, xstep=1, ystep=1):
    """
    Generate view ndarray of the grid.

    Kwargs:

    * xsize (int):
        Window dim-0 size.
    * ysize (int):
        Window dim-1 size.
    * xstep (int):
        Window dim-0 step, corresponding to window center.
    * ystep(int):
        Window dim-1 step, corresponding to window center.

    Returns:
        Numpy ndarray which represents a view of the grid.

    .. note::

    This function is not currently in use.

    """
    strides = (grid.strides[0] * xstep,
               grid.strides[1] * ystep,
               grid.strides[0],
               grid.strides[1])
    window = (((grid.shape[0] - 1) / (xsize - 1)),
              ((grid.shape[1] - 1) / (ysize - 1)),
              xsize,
              ysize)
    all_windows = stride.as_strided(grid, window, strides)
    return all_windows


def subdivide_grid(iteration, size):
    """
    Determine step size from subdivision number.

    Args:

    * iteration (int):
        Subdivision iteration number.  The relationship between dimension and
        iteration is as follows:
        dim_size_x, dim_size_y = (2**n + 1), (2**n + 1)

    Returns:
        Dictionary with size of window and the step sizes toward their center
        points information.

    """
    xsize = (size[0] - 1) / (2 ** iteration)
    ysize = (size[1] - 1) / (2 ** iteration)
    xstep = (xsize) / 2
    ystep = (ysize) / 2

    # Double checking to ensure compliance with algorithm.
    if xstep != ystep:
        raise RuntimeError('step sizes do not match')
    if xsize != ysize:
        raise RuntimeError('window dimensions not equal')

    return xsize, ysize, xstep, ystep


def perturbation(constant):
    """
    Provides a random number in the range [-constant, +constant].

    Args:

    * constant (float)

    Returns:
    Random number in the range [-constant, +constant].

    """
    return np.random.rand() * 2 * constant - constant


def square_det(xstep, ystep, step_x, step_y, size, wrap=True):
    """
    Return index of all edge midpoints of surrounding point.

    With 'O' representing the current location in the grid, 'o' represents
    point in the immediate neighbourhood that are not used, while those marked
    with 'X' have their location returned:

    o X o
    X O X
    o X o

    Args:

    * xstep (int):
        The number of indexes between points considered in 'x' (corresponds
        to an iteratively subdivided grid).
    * ystep (int):
        The number of indexes between points considered in 'y' (corresponds
        to an iteratively subdivided grid).
    * step_x (int):
        x Location of our point with respect to the entire grid.
    * step_y (int):
        y Location of our point with respect to the entire grid.

    Kwargs:
    
    * wrap (bool):
        Performs wrap-around for neighbours on the grid edge if True.  Default
        is to wrap-around.

    Returns:
        x index ndarray and y index ndarray representing the location of each
        surrounding point in the grid.

    """
    # Performs wraparound by default.
    center_x = np.array([step_x - xstep,
                         step_x,
                         step_x + xstep,
                         step_x])
    center_y = np.array([step_y,
                         step_y + ystep,
                         step_y,
                         step_y - ystep])
    if wrap:
        # Sort our wrapped indexes.
        center_x[center_x < 0] -= 1
        center_y[center_y < 0] -= 1
        center_x[center_x > size[0] - 1] = (
            center_x[center_x > size[0] - 1] * (- 1) + (size[0] - 2))
        center_y[center_y > size[1] - 1] = (
            center_y[center_y > size[1] - 1] * (- 1) + (size[1] - 2))
    else:
        # Remove elements being wrapped.
        mask = ((center_x >= 0) & (center_x < size[0]) &
                (center_y >= 0) & (center_y < size[1]))
        center_x = center_x[mask]
        center_y = center_y[mask]

    return center_y, center_x


def midpoint_sd(size, scale=0.5, random_corner=True):
    """
    Midpoint displacement algorithm.

    The dimaond-square version is implemented.

    Args:

    * size (tuple):
        (x, y) representing the resulting grid size.  This also indirectly
        determines the number of subdivision iterations for the algorithm.
        This is determined by the following relation:
        size_0, size_1 = (2**s + 1), (2**s + 1) where 's' is the number of
        iterations.  Rearranging for s = log_2(size_x - 1).  The dimension of
        the grid must be equal and must abide by, 2**n + 1 where n in Z.

    Kwargs:

    * scale (float):
        Determines the scale of the resulting terrain.  This value has no
        impact on the behaviour of the algorithm.
    * random_corder (bool):
        Specifies whether grid corner points are initialised at 0 (False) or
        populated with random values (True).  By default grid corner points are
        assigned random values (rand() * scale).

    Returns:
        ndarray grid representing a heightmap.

    """
    if np.log2(size[0] - 1) % 2:
        raise ValueError('Grid size {} not allowed, 2**n + 1 accepted where '
            'n in Z'.format(size[0]))
    if size[0] != size[1]:
        raise ValueError(
            'Grid dimensions specified: {} are not equal'.format(size))

    grid = np.ma.zeros(size, dtype=np.float)

    if random_corner:
        corner_x = np.array([0, 0, -1, -1])
        corner_y = np.array([0, -1, -1, 0])
        grid[corner_x, corner_y] = np.random.rand(4) * scale

    iterations = (
        np.log2(size[0] - 1).astype(int), np.log2(size[1] - 1).astype(int))

    for iteration in xrange(iterations[0]):
        xsize, ysize, xstep, ystep = subdivide_grid(iteration, size)

        # Diamond step.
        for step_x in xrange(xstep, size[0], (xstep) * 2):
            for step_y in xrange(ystep, size[1], (ystep) * 2):
                center_x = np.array([step_x - xstep,
                                     step_x + xstep,
                                     step_x + xstep,
                                     step_x - xstep])
                center_y = np.array([step_y + ystep,
                                     step_y + ystep,
                                     step_y - ystep,
                                     step_y - ystep])
<commit_msg>BUG: Size check fixed.<commit_after>import numpy as np
import numpy.lib.stride_tricks as stride
import mpl_toolkits.mplot3d
import matplotlib.pyplot as plt


def window_views(xsize=3, ysize=3, xstep=1, ystep=1):
    """
    Generate view ndarray of the grid.

    Kwargs:

    * xsize (int):
        Window dim-0 size.
    * ysize (int):
        Window dim-1 size.
    * xstep (int):
        Window dim-0 step, corresponding to window center.
    * ystep(int):
        Window dim-1 step, corresponding to window center.

    Returns:
        Numpy ndarray which represents a view of the grid.

    .. note::

    This function is not currently in use.

    """
    strides = (grid.strides[0] * xstep,
               grid.strides[1] * ystep,
               grid.strides[0],
               grid.strides[1])
    window = (((grid.shape[0] - 1) / (xsize - 1)),
              ((grid.shape[1] - 1) / (ysize - 1)),
              xsize,
              ysize)
    all_windows = stride.as_strided(grid, window, strides)
    return all_windows


def subdivide_grid(iteration, size):
    """
    Determine step size from subdivision number.

    Args:

    * iteration (int):
        Subdivision iteration number.  The relationship between dimension and
        iteration is as follows:
        dim_size_x, dim_size_y = (2**n + 1), (2**n + 1)

    Returns:
        Dictionary with size of window and the step sizes toward their center
        points information.

    """
    xsize = (size[0] - 1) / (2 ** iteration)
    ysize = (size[1] - 1) / (2 ** iteration)
    xstep = (xsize) / 2
    ystep = (ysize) / 2

    # Double checking to ensure compliance with algorithm.
    if xstep != ystep:
        raise RuntimeError('step sizes do not match')
    if xsize != ysize:
        raise RuntimeError('window dimensions not equal')

    return xsize, ysize, xstep, ystep


def perturbation(constant):
    """
    Provides a random number in the range [-constant, +constant].

    Args:

    * constant (float)

    Returns:
    Random number in the range [-constant, +constant].

    """
    return np.random.rand() * 2 * constant - constant


def square_det(xstep, ystep, step_x, step_y, size, wrap=True):
    """
    Return index of all edge midpoints of surrounding point.

    With 'O' representing the current location in the grid, 'o' represents
    point in the immediate neighbourhood that are not used, while those marked
    with 'X' have their location returned:

    o X o
    X O X
    o X o

    Args:

    * xstep (int):
        The number of indexes between points considered in 'x' (corresponds
        to an iteratively subdivided grid).
    * ystep (int):
        The number of indexes between points considered in 'y' (corresponds
        to an iteratively subdivided grid).
    * step_x (int):
        x Location of our point with respect to the entire grid.
    * step_y (int):
        y Location of our point with respect to the entire grid.

    Kwargs:
    
    * wrap (bool):
        Performs wrap-around for neighbours on the grid edge if True.  Default
        is to wrap-around.

    Returns:
        x index ndarray and y index ndarray representing the location of each
        surrounding point in the grid.

    """
    # Performs wraparound by default.
    center_x = np.array([step_x - xstep,
                         step_x,
                         step_x + xstep,
                         step_x])
    center_y = np.array([step_y,
                         step_y + ystep,
                         step_y,
                         step_y - ystep])
    if wrap:
        # Sort our wrapped indexes.
        center_x[center_x < 0] -= 1
        center_y[center_y < 0] -= 1
        center_x[center_x > size[0] - 1] = (
            center_x[center_x > size[0] - 1] * (- 1) + (size[0] - 2))
        center_y[center_y > size[1] - 1] = (
            center_y[center_y > size[1] - 1] * (- 1) + (size[1] - 2))
    else:
        # Remove elements being wrapped.
        mask = ((center_x >= 0) & (center_x < size[0]) &
                (center_y >= 0) & (center_y < size[1]))
        center_x = center_x[mask]
        center_y = center_y[mask]

    return center_y, center_x


def midpoint_sd(size, scale=0.5, random_corner=True):
    """
    Midpoint displacement algorithm.

    The dimaond-square version is implemented.

    Args:

    * size (tuple):
        (x, y) representing the resulting grid size.  This also indirectly
        determines the number of subdivision iterations for the algorithm.
        This is determined by the following relation:
        size_0, size_1 = (2**s + 1), (2**s + 1) where 's' is the number of
        iterations.  Rearranging for s = log_2(size_x - 1).  The dimension of
        the grid must be equal and must abide by, 2**n + 1 where n in Z.

    Kwargs:

    * scale (float):
        Determines the scale of the resulting terrain.  This value has no
        impact on the behaviour of the algorithm.
    * random_corder (bool):
        Specifies whether grid corner points are initialised at 0 (False) or
        populated with random values (True).  By default grid corner points are
        assigned random values (rand() * scale).

    Returns:
        ndarray grid representing a heightmap.

    """
    if not np.log2(size[0] - 1).is_integer():
        raise ValueError('Grid size {} not allowed, 2**n + 1 accepted where '
            'n in Z'.format(size[0]))
    if size[0] != size[1]:
        raise ValueError(
            'Grid dimensions specified: {} are not equal'.format(size))

    grid = np.ma.zeros(size, dtype=np.float)

    if random_corner:
        corner_x = np.array([0, 0, -1, -1])
        corner_y = np.array([0, -1, -1, 0])
        grid[corner_x, corner_y] = np.random.rand(4) * scale

    iterations = (
        np.log2(size[0] - 1).astype(int), np.log2(size[1] - 1).astype(int))

    for iteration in xrange(iterations[0]):
        xsize, ysize, xstep, ystep = subdivide_grid(iteration, size)

        # Diamond step.
        for step_x in xrange(xstep, size[0], (xstep) * 2):
            for step_y in xrange(ystep, size[1], (ystep) * 2):
                center_x = np.array([step_x - xstep,
                                     step_x + xstep,
                                     step_x + xstep,
                                     step_x - xstep])
                center_y = np.array([step_y + ystep,
                                     step_y + ystep,
                                     step_y - ystep,
                                     step_y - ystep])

{'fraction_non_alphanumeric': 0.10090338386158322, 'fraction_numerical': 0.014622569284948705, 'mean_word_length': 1.88940499889405, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3452341', 'n_tokens_mistral': 4104, 'n_tokens_neox': 3872, 'n_words': 1321}