<jupyter_start><jupyter_text>Geometric object generator  Required modules Load the `autoreload` iPython extension to ensure module reloads during development.<jupyter_code>%load_ext autoreload
%autoreload 2<jupyter_output><empty_output><jupyter_text>Load the required modules<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline
import random<jupyter_output><empty_output><jupyter_text>Load the module that implements the object generation for demonstration purposes.  It can also be run directory from the command line.<jupyter_code>import figures<jupyter_output><empty_output><jupyter_text>Generators Currently, three generators are defined for circles, squares and equilateral triangles.  Set the width and the height of the images, and the maximum size of the geometric objects, expressed in points.<jupyter_code>width, height = 100, 100
max_size = 25<jupyter_output><empty_output><jupyter_text>The generator for circles, and an example.<jupyter_code>circle_gen = figures.CircleGenerator(width, height, max_size)
fig = circle_gen.create()
plt.imshow(fig.data, cmap='gray');<jupyter_output><empty_output><jupyter_text>The generator for squares, and an example.<jupyter_code>square_gen = figures.SquareGenerator(width, height, max_size)
fig = square_gen.create()
plt.imshow(fig.data, cmap='gray');<jupyter_output><empty_output><jupyter_text>The generator for equilateral triangles, and an example.<jupyter_code>triangle_gen = figures.TriangleGenerator(width, height, max_size)
fig = triangle_gen.create()
plt.imshow(fig.data, cmap='gray');<jupyter_output><empty_output><jupyter_text>Transformations A transformation can be scaling the geometric object (down), rotating it, shifting it and performing a Gaussian blur.<jupyter_code>transformer = figures.FigureTransformer(width=width, height=height,
                                               min_size=10, max_size=max_size,
                                               center_margin=0.3,
                                               blur_factor=3.0)<jupyter_output><empty_output><jupyter_text>Execture some random scalings on a square.<jupyter_code>nr_plots = 6
figure, axes = plt.subplots(ncols=nr_plots, nrows=1, figsize=(12, 4))
for i in range(nr_plots):
    fig = square_gen.create()
    transformer.scale(fig)
    axes[i].get_xaxis().set_visible(False)
    axes[i].get_yaxis().set_visible(False)
    axes[i].imshow(fig.data, cmap='gray')<jupyter_output><empty_output><jupyter_text>Execute some random rotations on a square.<jupyter_code>nr_plots = 6
figure, axes = plt.subplots(ncols=nr_plots, nrows=1, figsize=(12, 4))
for i in range(nr_plots):
    fig = square_gen.create()
    transformer.rotate(fig)
    axes[i].get_xaxis().set_visible(False)
    axes[i].get_yaxis().set_visible(False)
    axes[i].imshow(fig.data, cmap='gray')<jupyter_output><empty_output><jupyter_text>Execute some random shifts on a square.<jupyter_code>nr_plots = 6
figure, axes = plt.subplots(ncols=nr_plots, nrows=1, figsize=(12, 4))
for i in range(nr_plots):
    fig = square_gen.create()
    transformer.shift(fig)
    axes[i].get_xaxis().set_visible(False)
    axes[i].get_yaxis().set_visible(False)
    axes[i].imshow(fig.data, cmap='gray')<jupyter_output><empty_output><jupyter_text>Execute some random blurs on a triangle.<jupyter_code>nr_plots = 6
figure, axes = plt.subplots(ncols=nr_plots, nrows=1, figsize=(12, 4))
for i in range(nr_plots):
    fig = triangle_gen.create()
    transformer.blur(fig)
    axes[i].get_xaxis().set_visible(False)
    axes[i].get_yaxis().set_visible(False)
    axes[i].imshow(fig.data, cmap='gray')<jupyter_output><empty_output><jupyter_text>Generate random geometric objects, with all transformations applied.<jupyter_code>nrows, ncols = 5, 10
generators = [triangle_gen, square_gen, circle_gen]
figure, axes = plt.subplots(ncols=ncols, nrows=nrows, figsize=(12, 6))
for i in range(nrows):
    for j in range(ncols):
        generator = random.choice(generators)
        fig = generator.create()
        transformer.transform(fig)
        axes[i, j].get_xaxis().set_visible(False)
        axes[i, j].get_yaxis().set_visible(False)
        axes[i, j].imshow(fig.data, cmap='gray')<jupyter_output><empty_output><jupyter_text>HDF5 files The script part of `figures` will write an HDF5 file with the data. The input dataset is called `x_values`, the output dataset `y_values`.<jupyter_code>import h5py
from pathlib import Path
import subprocess
nrows, ncols = 4, 5
h5_path = Path('data.h5')
if not h5_path.exists():
    print(f'generating data file {h5_path.as_posix()}')
    subprocess.call(['./generate_images.py', '--n', str(nrows*ncols),
                     '--verbose', h5_path.as_posix()])

object_types = ['circle', 'square', 'triangle']
figure, axes = plt.subplots(ncols=ncols, nrows=nrows, figsize=(12, 6))
with h5py.File(str(h5_path), 'r') as h5_file:
    for i in range(nrows):
        for j in range(ncols):
            index = i*ncols + j
            fig = h5_file['x_values'][index]
            axes[i, j].get_xaxis().set_visible(False)
            axes[i, j].get_yaxis().set_visible(False)
            object_id = np.argmax(h5_file['y_values'][index])
            axes[i, j].set_title(object_types[object_id])
            axes[i, j].imshow(fig, cmap='gray')
figure.tight_layout()<jupyter_output>generating data file data.h5<jupyter_text>Multiple objects per image<jupyter_code>nrows, ncols = 4, 5
h5_path = Path('multiple_objects_data.h5')
if not h5_path.exists():
    subprocess.call(['./generate_images.py', '--n', str(nrows*ncols),
                     '--min_size', '5', '--max_size', '10', '--center_margin', '0.2',
                     '--max_objects', '5', h5_path.as_posix()])

def generate_title(counts):
    names = ['c', 's', 't']
    strs = map(lambda x: f'{x[0]}: {x[1]:d}', zip(names, counts))
    return ', '.join(strs)

figure, axes = plt.subplots(ncols=ncols, nrows=nrows, figsize=(12, 6))
with h5py.File(h5_path.as_posix(), 'r') as h5_file:
    for i in range(nrows):
        for j in range(ncols):
            index = i*ncols + j
            fig = h5_file['x_values'][index]
            axes[i, j].get_xaxis().set_visible(False)
            axes[i, j].get_yaxis().set_visible(False)
            title = generate_title(h5_file['y_values'][index])
            axes[i, j].set_title(title)
            axes[i, j].imshow(fig, cmap='gray')
figure.tight_layout()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1325113157484002, 'fraction_numerical': 0.012798501638832527, 'mean_word_length': 4.412162162162162, 'pattern_counts': {'":': 0, '<': 60, '<?xml version=': 0, '>': 60, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '307611', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2302, 'n_tokens_neox': 2204, 'n_words': 452, 'path': 'source-code/keras/Flatland/data_generation.ipynb', 'repo_name': 'Gjacquenot/Python-for-machine-learning', 'size': '6407'}