# Path: keras_image_captioning/config.py
# def active_config(new_active_config=None):
#     if new_active_config:
#         global _active_config
#         _active_config = new_active_config
#     return _active_config
# 
# def write_to_file(config, yaml_path):
#     config_dict = dict(config._asdict())
#     time_limit = config_dict['time_limit']
#     if time_limit:
#         config_dict['time_limit'] = str(time_limit)
#     write_yaml_file(config_dict, yaml_path)
# 
# class Embed300FineRandomConfigBuilder(Embed300RandomConfigBuilder):
#     def __init__(self, fixed_config_keys):
#         super(Embed300FineRandomConfigBuilder, self).__init__(
#                                                             fixed_config_keys)
# 
#         self._reduce_lr_factor = lambda: uniform(0.1, 0.9)
#         self._reduce_lr_patience = lambda: 4
#         self._lemmatize_caption = lambda: False
# 
#         # Values below are from an analysis of hpsearch/16
#         self._word_vector_init = lambda: 'glove'
#         self._rnn_layers = lambda: randint(2, 3)
#         self._learning_rate = lambda: 10 ** uniform(-4, -2.7)
#         self._dropout_rate = lambda: uniform(0.2, 0.5)
# 
# Path: keras_image_captioning/common_utils.py
# def parse_timedelta(timedelta_str):
#     if isinstance(timedelta_str, timedelta):
#         return timedelta_str
#     if not timedelta_str or timedelta_str == 'null':
#         return None
# 
#     tokens = re.split(r' days?,? ', timedelta_str)
#     if len(tokens) == 1:
#         if tokens[0].find('day') == -1:
#             days = '0'
#             rest = tokens[0]
#         else:
#             days = tokens[0].split(' ')[0]
#             rest = '00:00:00'
#     else:
#         days, rest = tokens
# 
#     days = int(days)
#     hours, minutes, seconds = map(int, rest.split(':'))
#     return timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
# 
# Path: keras_image_captioning/datasets.py
# def get_dataset_instance(dataset_name=None, lemmatize_caption=None,
#                          single_caption=False):
#     """
#     If an arg is None, it will get its value from config.active_config.
#     """
#     dataset_name = dataset_name or active_config().dataset_name
#     lemmatize_caption = lemmatize_caption or active_config().lemmatize_caption
# 
#     for dataset_class in [Flickr8kDataset]:
#         if dataset_class.DATASET_NAME == dataset_name:
#             return dataset_class(lemmatize_caption=lemmatize_caption,
#                                  single_caption=single_caption)
# 
#     raise ValueError('Cannot find {} dataset!'.format(dataset_name))
# 
# Path: keras_image_captioning/io_utils.py
# def mkdir_p(path):
#     try:
#         os.makedirs(path)
#     except OSError as exc:
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else:
#             raise
# 
# def logging(*args, **kwargs):
#     utc_now = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
#     print(utc_now, '|', *args, **kwargs)
#     sys.stdout.flush()


import fire
import itertools
import os
import signal
import sh
import sys
import traceback
from concurrent.futures import ThreadPoolExecutor
from random import uniform
from threading import Lock, Semaphore
from time import sleep
from tempfile import gettempdir, NamedTemporaryFile
from .config import (active_config, write_to_file,
                     Embed300FineRandomConfigBuilder)
from .common_utils import parse_timedelta
from .datasets import get_dataset_instance
from .io_utils import mkdir_p, logging
    def gpu_index(self):
        return self._gpu_index

    @property
    def config_filepath(self):
        return self._config_filepath

    def execute(self):
        """Execute the training."""
        env = os.environ.copy()
        env['CUDA_VISIBLE_DEVICES'] = str(self.gpu_index)
        return self.COMMAND(training_label=self.training_label,
                            config_file=self.config_filepath,
                            _env=env,
                            _out=self._log_filepath,
                            _err_to_out=True,
                            _bg=self._background,
                            _done=self._done_callback)

    def _init_config_filepath(self):
        tmp_dir = os.path.join(gettempdir(), 'keras_image_captioning')
        mkdir_p(tmp_dir)
        config_file = NamedTemporaryFile(suffix='.yaml', dir=tmp_dir,
                                         delete=False)
        config_file.close()
        self._config_filepath = config_file.name
        write_to_file(self._config, self._config_filepath)

    def _init_log_filepath(self):
        LOG_FILENAME = 'training-log.txt'

# Path: keras_image_captioning/config.py
# def active_config(new_active_config=None):
#     if new_active_config:
#         global _active_config
#         _active_config = new_active_config
#     return _active_config
# 
# def write_to_file(config, yaml_path):
#     config_dict = dict(config._asdict())
#     time_limit = config_dict['time_limit']
#     if time_limit:
#         config_dict['time_limit'] = str(time_limit)
#     write_yaml_file(config_dict, yaml_path)
# 
# class Embed300FineRandomConfigBuilder(Embed300RandomConfigBuilder):
#     def __init__(self, fixed_config_keys):
#         super(Embed300FineRandomConfigBuilder, self).__init__(
#                                                             fixed_config_keys)
# 
#         self._reduce_lr_factor = lambda: uniform(0.1, 0.9)
#         self._reduce_lr_patience = lambda: 4
#         self._lemmatize_caption = lambda: False
# 
#         # Values below are from an analysis of hpsearch/16
#         self._word_vector_init = lambda: 'glove'
#         self._rnn_layers = lambda: randint(2, 3)
#         self._learning_rate = lambda: 10 ** uniform(-4, -2.7)
#         self._dropout_rate = lambda: uniform(0.2, 0.5)
# 
# Path: keras_image_captioning/common_utils.py
# def parse_timedelta(timedelta_str):
#     if isinstance(timedelta_str, timedelta):
#         return timedelta_str
#     if not timedelta_str or timedelta_str == 'null':
#         return None
# 
#     tokens = re.split(r' days?,? ', timedelta_str)
#     if len(tokens) == 1:
#         if tokens[0].find('day') == -1:
#             days = '0'
#             rest = tokens[0]
#         else:
#             days = tokens[0].split(' ')[0]
#             rest = '00:00:00'
#     else:
#         days, rest = tokens
# 
#     days = int(days)
#     hours, minutes, seconds = map(int, rest.split(':'))
#     return timedelta(days=days, hours=hours, minutes=minutes, seconds=seconds)
# 
# Path: keras_image_captioning/datasets.py
# def get_dataset_instance(dataset_name=None, lemmatize_caption=None,
#                          single_caption=False):
#     """
#     If an arg is None, it will get its value from config.active_config.
#     """
#     dataset_name = dataset_name or active_config().dataset_name
#     lemmatize_caption = lemmatize_caption or active_config().lemmatize_caption
# 
#     for dataset_class in [Flickr8kDataset]:
#         if dataset_class.DATASET_NAME == dataset_name:
#             return dataset_class(lemmatize_caption=lemmatize_caption,
#                                  single_caption=single_caption)
# 
#     raise ValueError('Cannot find {} dataset!'.format(dataset_name))
# 
# Path: keras_image_captioning/io_utils.py
# def mkdir_p(path):
#     try:
#         os.makedirs(path)
#     except OSError as exc:
#         if exc.errno == errno.EEXIST and os.path.isdir(path):
#             pass
#         else:
#             raise
# 
# def logging(*args, **kwargs):
#     utc_now = datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')
#     print(utc_now, '|', *args, **kwargs)
#     sys.stdout.flush()


# Path: keras_image_captioning/hyperparam_search.py
import fire
import itertools
import os
import signal
import sh
import sys
import traceback
from concurrent.futures import ThreadPoolExecutor
from random import uniform
from threading import Lock, Semaphore
from time import sleep
from tempfile import gettempdir, NamedTemporaryFile
from .config import (active_config, write_to_file,
                     Embed300FineRandomConfigBuilder)
from .common_utils import parse_timedelta
from .datasets import get_dataset_instance
from .io_utils import mkdir_p, logging

    def gpu_index(self):
        return self._gpu_index

    @property
    def config_filepath(self):
        return self._config_filepath

    def execute(self):
        """Execute the training."""
        env = os.environ.copy()
        env['CUDA_VISIBLE_DEVICES'] = str(self.gpu_index)
        return self.COMMAND(training_label=self.training_label,
                            config_file=self.config_filepath,
                            _env=env,
                            _out=self._log_filepath,
                            _err_to_out=True,
                            _bg=self._background,
                            _done=self._done_callback)

    def _init_config_filepath(self):
        tmp_dir = os.path.join(gettempdir(), 'keras_image_captioning')
        mkdir_p(tmp_dir)
        config_file = NamedTemporaryFile(suffix='.yaml', dir=tmp_dir,
                                         delete=False)
        config_file.close()
        self._config_filepath = config_file.name
        write_to_file(self._config, self._config_filepath)

    def _init_log_filepath(self):
        LOG_FILENAME = 'training-log.txt'

        dataset = get_dataset_instance(self._config.dataset_name,