<commit_before>import curses
import matplotlib.pyplot as plt
import numpy as np
import numpy.lib.stride_tricks as stride
import time


def window_views(grid, 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] - 2) // xstep, (grid.shape[1] - 2) // ystep,
              xsize, ysize)
    all_windows = stride.as_strided(grid, window, strides)
    return all_windows


def mpl_plot(grid):
    plt.figure()
    plt.pcolor(grid)
    plt.show()


def curse_plot(window, grid, fps=None):
    for ind, line in enumerate(grid):
        #characters = line > 0
        #for col, character in enumerate(characters):
        #    if character > 0:
        #        #pass
        #        window.addch(ind, col, 'X', curses.color_pair(2))
        #    else:
        #        window.addch(ind, col, 'X', curses.color_pair(1))
        pout = ''.join(line.astype('|S1')).replace('0', ' ')
        window.addstr(ind, 0, pout)
    window.refresh()

    if fps:
        time.sleep(1. / fps)


def main():
    size = (38, 143)
    if np.any([ss % 3 != 2 for ss in size]):
        msg = 'Grid size {} does not conform to size_x % 3 == 2'.format(size)
        raise ValueError(msg)

    # Make the cursor visible
    stdscr = curses.initscr()
    # No echo of input characters
    curses.noecho()
    curses.start_color()
    curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_BLACK)
    curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_WHITE)

    # Visibility of cursor
    curses.curs_set(0)
    stdscr.refresh()

    grid = np.random.randint(0, 2, size)
    grid.astype(np.uint8)
    grid[0, :] = grid[-1, :] = grid[:, 0] = grid[:, -1] = 0

    views = window_views(grid)

    mask = np.array([[True, True, True],
                     [True, False, True],
                     [True, True, True]])

    for i in xrange(1000):
        neighbours = views[..., mask].sum(2)
        views[..., 1, 1] = (views[..., 1, 1] & ~(neighbours > 3) &
                            ~(neighbours < 2) | neighbours == 3)
        curse_plot(stdscr, grid)

    curses.nocbreak()
    stdscr.keypad(0)
    curses.echo()
    curses.endwin()


if __name__ == '__main__':
    np.random.seed(0)
    main()<commit_msg>ENH: Adjust grid size automatically.<commit_after>import curses
import matplotlib.pyplot as plt
import numpy as np
import numpy.lib.stride_tricks as stride
import time


def window_views(grid, 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] - 2) // xstep, (grid.shape[1] - 2) // ystep,
              xsize, ysize)
    all_windows = stride.as_strided(grid, window, strides)
    return all_windows


def mpl_plot(grid):
    plt.figure()
    plt.pcolor(grid)
    plt.show()


class CursePlot(object):
    def __init__(self, grid, window=None, fps=None):
        self.fps = fps
        if window:
            self.window = window
        else:
            self.window = curses.initscr()
        self.grid = grid

        # Make the cursor visible
        stdscr = curses.initscr()
        # No echo of input characters
        curses.noecho()
        curses.start_color()
        curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_BLACK)
        curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_WHITE)

        # Visibility of cursor
        curses.curs_set(0)

    def __enter__(self):
        return self

    def update(self):
        for ind, line in enumerate(self.grid):
            pout = ''.join(line.astype('|S1')).replace('0', ' ')
            self.window.addstr(ind, 0, pout)
        self.window.refresh()
    
        if self.fps:
            time.sleep(1. / self.fps)

    def __exit__(self):
        curses.nocbreak()
        stdscr.keypad(0)
        curses.echo()
        curses.endwin()


def game_of_life(size=(40, 140), plot_tool=CursePlot):
    # Adgust dimensions to suitable value
    size = [dim + (2 - diff) for dim, diff in
            zip(size, [ss % 3 for ss in size])]    

    grid = np.random.randint(0, 2, size)
    grid.astype(np.uint8)
    grid[0, :] = grid[-1, :] = grid[:, 0] = grid[:, -1] = 0

    views = window_views(grid)

    mask = np.array([[True, True, True],
                     [True, False, True],
                     [True, True, True]])

    with plot_tool(grid) as plot:
        for i in xrange(1000):
            neighbours = views[..., mask].sum(2)
            views[..., 1, 1] = (views[..., 1, 1] & ~(neighbours > 3) &
                                ~(neighbours < 2) | neighbours == 3)
            plot.update()


if __name__ == '__main__':
    game_of_life(plot_tool=CursePlot)
{'fraction_non_alphanumeric': 0.13311235170969993, 'fraction_numerical': 0.019016050244242848, 'mean_word_length': 2.018957345971564, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3759810', 'n_tokens_mistral': 2019, 'n_tokens_neox': 1926, 'n_words': 492}