<commit_before>"""
Script for generating dataset example figures

Uses show_examples() to generate example figures of a dataset
and stores it temporary in examples/

Typical usage example:

  ds_name = 'beans'
  generate_visualization(ds_name)

"""

import os
import tensorflow_datasets as tfds
from PIL import Image
from absl import app
import matplotlib.pyplot

FIG_DIR = ('../docs/catalog/images/examples/')

def generate_visualization(ds_name):
  """
  Method used to generate examples figures of a dataset
  """

  try:
    ds, ds_info = tfds.load(name=ds_name, split='train', with_info=True)
    path_dir = tfds.core.get_tfds_path(FIG_DIR)
    print(path_dir)
    fig = tfds.show_examples(ds_info, ds, plot_scale=2)
    suffix = '.png'
    fpath = os.path.join(path_dir, ds_name + suffix)
    fig.savefig(fname=fpath)

    # Optimizing figures
    optimize_image(ds_name)
    os.remove(fpath)

  except RuntimeError:
    print('The selected dataset is not supported')

def optimize_image(ds_name):
  """<commit_msg>Minor fix: typo<commit_after>"""
Script for generating dataset example figures

Uses show_examples() to generate example figures of a dataset
and stores it temporary in examples/

Typical usage example:

  ds_name = 'beans'
  generate_visualization(ds_name)

"""

import os
import tensorflow_datasets as tfds
from PIL import Image
from absl import app
import matplotlib.pyplot

FIG_DIR = '../docs/catalog/images/examples/'

def generate_visualization(ds_name):
  """
  Method used to generate examples figures of a dataset
  """

  try:
    ds, ds_info = tfds.load(name=ds_name, split='train', with_info=True)
    path_dir = tfds.core.get_tfds_path(FIG_DIR)
    print(path_dir)
    fig = tfds.show_examples(ds_info, ds, plot_scale=2)
    suffix = '.png'
    fpath = os.path.join(path_dir, ds_name + suffix)
    fig.savefig(fname=fpath)

    # Optimizing figures
    optimize_image(ds_name)
    os.remove(fpath)

  except RuntimeError:
    print('The selected dataset is not supported')

def optimize_image(ds_name):
  """
{'fraction_non_alphanumeric': 0.09647404505386875, 'fraction_numerical': 0.0009794319294809011, 'mean_word_length': 4.27906976744186, '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': '1558881', 'n_tokens_mistral': 690, 'n_tokens_neox': 669, 'n_words': 184}