<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
poisson_art.py - create city skyline art using a modified Poisson-process
"""

import numpy as np
from itertools import accumulate
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib

def simulate_process(beta, steps):
    """
    :beta: Mean holding time.
    :steps: Number of steps to take _after_ t = 0.
    """
    holding_times = np.random.exponential(beta, steps)
    jump_times = [0] + list(accumulate(holding_times))
    states = [0] + [np.random.poisson(delta_t) for delta_t in holding_times]
    return np.array(jump_times), np.array(states)

betas = [1, 1, 1]
steps = [50, 50, 50]
alpha = [.3, .6, .9]
lws = [2, 3, 8]

def draw_picture(betas, steps, alphas, lws):
    """Draw a picture of the results from the given paramters.

    :betas: TODO
    :steps: TODO
    :alphas: TODO
    :lws: TODO
    :returns: TODO

    """
    results = [simulate_process(beta, steps) for beta, steps in
                    zip(betas, steps)]

    plt.style.use("ggplot")
    plt.figure(figsize=(18, 10))
    #plt.axis("off")
    # Turn off ticks for both axes (but don't turn off the axes, because we
    # want to do background colors).
    plt.tick_params(
        axis='both',
        which='both',
        bottom='off',
        left="off",
        labelleft="off",
        top='off',
        labelbottom='off')

    for result, alpha, lw in zip(results, alphas, lws):
        ts, ys = result
        plt.step(ts, ys, alpha=alpha, lw=lw)

        plt.gca().set_axis_bgcolor((.95, .95, .95))
    plt.xlim(0, np.min([ts.max() for ts, ys in results]))

# Animation stuff.

betas = [1, 1, 1]
steps = [50, 50, 50]
alphas = [.3, .6, .9]
lws = [2, 3, 5]

def animations(betas, steps, alphas, lws):

    results = [simulate_process(beta, steps) for beta, steps in
                    zip(betas, steps)]

    fig = plt.figure(figsize=(18, 10))
    max_t = np.min([ts.max() for ts, ys in results])
    max_y = np.max([ys.max() for ts, ys in results])
    max_y *= 1.1

    ax = plt.axes(xlim=(0, max_t), ylim=(0, max_y))
    lines = [ax.plot([0], [0], lw=lw, alpha=alpha)[0] for lw, alpha in zip(lws, alphas)]

    plt.tick_params(
        axis='both',
        which='both',
        bottom='off',
        left="off",
        labelleft="off",
        top='off',
        labelbottom='off')

    def init():
        for line in lines:
            line.set_data([0], [0])
        return lines

    def animate(i):
        global results
        if i == 0:
            # Create new results to plot.
            results = [simulate_process(beta, steps) for beta, steps in
                            zip(betas, steps)]
            max_t = np.min([ts.max() for ts, ys in results])
            max_y = np.max([ys.max() for ts, ys in results])
            max_y *= 1.1
            plt.xlim(0, max_t)
            plt.ylim(0, max_y)

        for result, line in zip(results, lines):
            ts, ys = result
            # There are two line segments per point.
            point_to = i // (2*frames_per_segment)

            t_start, y_start = ts[point_to], ys[point_to]
            t_end, y_end = ts[point_to + 1], ys[point_to + 1]

            plotted_ts, plotted_ys = line.get_data()
            current_t, current_y = plotted_ts[-1], plotted_ys[-1]

            # First: Move to the new t-coordinate.
            # Second: Move to the new y-coordinate.
            # This will create a bar-plot like thing.

            if not np.isclose(current_t, t_end):
                # Move to the new t-coordinate.
                dt = (t_end - t_start) / frames_per_segment
                new_t = current_t + dt
                new_y = current_y
            else:
                # Move to the new y-coordinate.
                dy = (y_end - y_start) / frames_per_segment
                new_y = current_y + dy
                new_t = current_t

            # Add new point to the list.
            new_ts = np.append(plotted_ts, new_t)
            new_ys = np.append(plotted_ys, new_y)

            line.set_data(new_ts, new_ys)

        return lines

    plt.style.use("ggplot")

    # TODO: Change this to "frames_per_unit" and then use segment length. This
    # will create "smoother" drawings.
    # This is difficult, because different drawings will produce different numbers
    # of frames, and matplotlib only allows a fixed number.
    frames_per_segment = 5

    n_points = len(results[0][0])
    n_segments = n_points
<commit_msg>Centralize figure setup<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
poisson_art.py - create city skyline art using a modified Poisson-process
"""

import numpy as np
from itertools import accumulate
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import matplotlib

def simulate_process(beta, steps):
    """
    :beta: Mean holding time.
    :steps: Number of steps to take _after_ t = 0.
    """
    holding_times = np.random.exponential(beta, steps)
    jump_times = [0] + list(accumulate(holding_times))
    states = [0] + [np.random.poisson(delta_t) for delta_t in holding_times]
    return np.array(jump_times), np.array(states)

betas = [1, 1, 1]
steps = [50, 50, 50]
alpha = [.3, .6, .9]
lws = [2, 3, 8]

def drawing_init(figsize=None):
    """TODO: Docstring for drawing_init.
    :returns: TODO

    """
    plt.style.use("ggplot")
    fig = plt.figure(figsize=figsize) if figsize else plt.figure()

    # Turn off ticks for both axes (but don't turn off the axes, because we
    # want to do background colors).
    plt.tick_params(
        axis='both',
        which='both',
        bottom='off',
        left="off",
        labelleft="off",
        top='off',
        labelbottom='off')
    return fig

def draw_picture(betas, steps, alphas, lws):
    """Draw a picture of the results from the given paramters.

    :betas: TODO
    :steps: TODO
    :alphas: TODO
    :lws: TODO
    :returns: TODO

    """
    results = [simulate_process(beta, steps) for beta, steps in
                    zip(betas, steps)]

    drawing_init((45, 20))
    for result, alpha, lw in zip(results, alphas, lws):
        ts, ys = result
        plt.step(ts, ys, alpha=alpha, lw=lw)

        plt.gca().set_axis_bgcolor((.95, .95, .95))
    plt.xlim(0, np.min([ts.max() for ts, ys in results]))

# Animation stuff.

betas = [1, 1, 1]
steps = [50, 50, 50]
alphas = [.3, .6, .9]
lws = [2, 3, 5]

def animations(betas, steps, alphas, lws):

    results = [simulate_process(beta, steps) for beta, steps in
                    zip(betas, steps)]

    fig = drawing_init()
    max_t = np.min([ts.max() for ts, ys in results])
    max_y = np.max([ys.max() for ts, ys in results])
    max_y *= 1.1

    ax = plt.axes(xlim=(0, max_t), ylim=(0, max_y))
    lines = [ax.plot([0], [0], lw=lw, alpha=alpha)[0] for lw, alpha in zip(lws, alphas)]

    def init():
        for line in lines:
            line.set_data([0], [0])
        return lines

    def animate(i):
        global results
        if i == 0:
            # Create new results to plot.
            results = [simulate_process(beta, steps) for beta, steps in
                            zip(betas, steps)]
            max_t = np.min([ts.max() for ts, ys in results])
            max_y = np.max([ys.max() for ts, ys in results])
            max_y *= 1.1
            plt.xlim(0, max_t)
            plt.ylim(0, max_y)

        for result, line in zip(results, lines):
            ts, ys = result
            # There are two line segments per point.
            point_to = i // (2*frames_per_segment)

            t_start, y_start = ts[point_to], ys[point_to]
            t_end, y_end = ts[point_to + 1], ys[point_to + 1]

            plotted_ts, plotted_ys = line.get_data()
            current_t, current_y = plotted_ts[-1], plotted_ys[-1]

            # First: Move to the new t-coordinate.
            # Second: Move to the new y-coordinate.
            # This will create a bar-plot like thing.

            if not np.isclose(current_t, t_end):
                # Move to the new t-coordinate.
                dt = (t_end - t_start) / frames_per_segment
                new_t = current_t + dt
                new_y = current_y
            else:
                # Move to the new y-coordinate.
                dy = (y_end - y_start) / frames_per_segment
                new_y = current_y + dy
                new_t = current_t

            # Add new point to the list.
            new_ts = np.append(plotted_ts, new_t)
            new_ys = np.append(plotted_ys, new_y)

            line.set_data(new_ts, new_ys)

        return lines

    # TODO: Change this to "frames_per_unit" and then use segment length. This
    # will create "smoother" drawings.
    # This is difficult, because different drawings will produce different numbers
    # of frames, and matplotlib only allows a fixed number.
    frames_per_segment = 5

    n_points = len(results[0][0])
    n_segments = n_points

{'fraction_non_alphanumeric': 0.12080462324961103, 'fraction_numerical': 0.01555901311402534, 'mean_word_length': 2.091377533493645, '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': '1723029', 'n_tokens_mistral': 3130, 'n_tokens_neox': 2973, 'n_words': 887}