# Path: moonlight/pipeline/pipeline_flags.py
# FLAGS = flags.FLAGS
# def create_pipeline(**kwargs):
# 
# Path: moonlight/training/generation/generation.py
# POSITIVE_EXAMPLES_PER_IMAGE = 3
# def _normalize_path(filename):
#   def __init__(self, num_pages_per_batch, vexflow_generator_command,
#                svg_to_png_command):
#   def process(self, batch_num):
#   def get_pages_for_batch(self, batch_num, num_pages_per_batch):
#   def get_pages(self, seeds):
#   def _svg_to_png(self, svg):
#   def __init__(self,
#                negative_example_distance,
#                patch_width,
#                negative_to_positive_example_ratio,
#                noise_fn=lambda x: x):
#   def start_bundle(self):
#   def process(self, item):
#   def _create_example(self, staffline, x, label):
# class PageGenerationDoFn(beam.DoFn):
# class PatchExampleDoFn(beam.DoFn):
# 
# Path: moonlight/training/generation/image_noise.py
# def placeholder_image():
# def random_rotation(image, angle=math.pi / 180):
# def gaussian_noise(image, stddev=5):


from absl import app
from absl import flags
from moonlight.pipeline import pipeline_flags
from moonlight.training.generation import generation
from moonlight.training.generation import image_noise
import apache_beam as beam
import tensorflow as tf
flags.DEFINE_multi_string(
    'svg_to_png_command', [
        '/usr/bin/env',
        'convert',
        'svg:-',
        'png:-',
    ], 'Command line to convert a SVG (stdin) to PNG (stdout).')
flags.DEFINE_integer('patch_width', 15, 'Width of a staffline patch.')
flags.DEFINE_integer(
    'negative_example_distance', 3,
    'The minimum distance of a negative example from any glyph.')


def main(_):
  with pipeline_flags.create_pipeline() as pipeline:
    num_pages = (FLAGS.num_positive_examples +
                 generation.POSITIVE_EXAMPLES_PER_IMAGE -
                 1) // generation.POSITIVE_EXAMPLES_PER_IMAGE
    num_batches = (num_pages + FLAGS.num_pages_per_batch -
                   1) // FLAGS.num_pages_per_batch
    batch_nums = pipeline | beam.transforms.Create(list(range(num_batches)))
    pages = batch_nums | beam.ParDo(
        generation.PageGenerationDoFn(
            num_pages_per_batch=FLAGS.num_pages_per_batch,
            vexflow_generator_command=FLAGS.vexflow_generator_command,
            svg_to_png_command=FLAGS.svg_to_png_command))

    def noise_fn(image):
      # TODO(ringw): Add better noise, maybe using generative adversarial
      # networks trained on real scores from IMSLP.

# Path: moonlight/pipeline/pipeline_flags.py
# FLAGS = flags.FLAGS
# def create_pipeline(**kwargs):
# 
# Path: moonlight/training/generation/generation.py
# POSITIVE_EXAMPLES_PER_IMAGE = 3
# def _normalize_path(filename):
#   def __init__(self, num_pages_per_batch, vexflow_generator_command,
#                svg_to_png_command):
#   def process(self, batch_num):
#   def get_pages_for_batch(self, batch_num, num_pages_per_batch):
#   def get_pages(self, seeds):
#   def _svg_to_png(self, svg):
#   def __init__(self,
#                negative_example_distance,
#                patch_width,
#                negative_to_positive_example_ratio,
#                noise_fn=lambda x: x):
#   def start_bundle(self):
#   def process(self, item):
#   def _create_example(self, staffline, x, label):
# class PageGenerationDoFn(beam.DoFn):
# class PatchExampleDoFn(beam.DoFn):
# 
# Path: moonlight/training/generation/image_noise.py
# def placeholder_image():
# def random_rotation(image, angle=math.pi / 180):
# def gaussian_noise(image, stddev=5):


# Path: moonlight/training/generation/vexflow_generator_pipeline.py
from absl import app
from absl import flags
from moonlight.pipeline import pipeline_flags
from moonlight.training.generation import generation
from moonlight.training.generation import image_noise
import apache_beam as beam
import tensorflow as tf

flags.DEFINE_multi_string(
    'svg_to_png_command', [
        '/usr/bin/env',
        'convert',
        'svg:-',
        'png:-',
    ], 'Command line to convert a SVG (stdin) to PNG (stdout).')
flags.DEFINE_integer('patch_width', 15, 'Width of a staffline patch.')
flags.DEFINE_integer(
    'negative_example_distance', 3,
    'The minimum distance of a negative example from any glyph.')


def main(_):
  with pipeline_flags.create_pipeline() as pipeline:
    num_pages = (FLAGS.num_positive_examples +
                 generation.POSITIVE_EXAMPLES_PER_IMAGE -
                 1) // generation.POSITIVE_EXAMPLES_PER_IMAGE
    num_batches = (num_pages + FLAGS.num_pages_per_batch -
                   1) // FLAGS.num_pages_per_batch
    batch_nums = pipeline | beam.transforms.Create(list(range(num_batches)))
    pages = batch_nums | beam.ParDo(
        generation.PageGenerationDoFn(
            num_pages_per_batch=FLAGS.num_pages_per_batch,
            vexflow_generator_command=FLAGS.vexflow_generator_command,
            svg_to_png_command=FLAGS.svg_to_png_command))

    def noise_fn(image):
      # TODO(ringw): Add better noise, maybe using generative adversarial
      # networks trained on real scores from IMSLP.

      return image_noise.gaussian_noise(image_noise.random_rotation(image))