Exemple #1
0
    def println(self, message, with_tag=False):
        """
        Print `message` via `print_function`.

        Args:
            message (str): Message to be printed.
            with_tag (bool): Whether or not to add the epoch & step tag?
                (default :obj:`False`)
        """
        if with_tag:

            def format_tag(v, max_v, name):
                if max_v is not None:
                    return '{} {}/{}'.format(name, v, max_v)
                else:
                    return '{} {}'.format(name, v)

            if not self._within_step and not self._within_epoch:
                self._require_context()
            tags = []
            if self._max_epoch != 1:
                tags.append(format_tag(self.epoch, self._max_epoch, 'Epoch'))
            tags.append(format_tag(self.step, self._max_step, 'Step'))
            if self._show_eta:
                eta = self.get_eta()
                if eta is not None:
                    tags.append('ETA {}'.format(humanize_duration(eta)))
            message = '[{}] {}'.format(', '.join(tags), message)
        self._print_func(message)
Exemple #2
0
    def test_humanize_duration(self):
        cases = [
            (0.0, '0 sec'),
            (1e-8, '1e-08 sec'),
            (0.1, '0.1 sec'),
            (1.0, '1 sec'),
            (1, '1 sec'),
            (1.1, '1.1 secs'),
            (59, '59 secs'),
            (59.9, '59.9 secs'),
            (60, '1 min'),
            (61, '1 min 1 sec'),
            (62, '1 min 2 secs'),
            (119, '1 min 59 secs'),
            (120, '2 mins'),
            (121, '2 mins 1 sec'),
            (122, '2 mins 2 secs'),
            (3599, '59 mins 59 secs'),
            (3600, '1 hr'),
            (3601, '1 hr 1 sec'),
            (3661, '1 hr 1 min 1 sec'),
            (86399, '23 hrs 59 mins 59 secs'),
            (86400, '1 day'),
            (86401, '1 day 1 sec'),
            (172799, '1 day 23 hrs 59 mins 59 secs'),
            (259199, '2 days 23 hrs 59 mins 59 secs'),
        ]
        for seconds, answer in cases:
            result = humanize_duration(seconds)
            self.assertEqual(
                result, answer,
                msg='humanize_duraion(%r) is expected to be %r, but got %r.' %
                    (seconds, answer, result)
            )

        for seconds, answer in cases[1:]:
            seconds = -seconds
            answer = answer + ' ago'
            result = humanize_duration(seconds)
            self.assertEqual(
                result, answer,
                msg='humanize_duraion(%r) is expected to be %r, but got %r.' %
                    (seconds, answer, result)
            )
Exemple #3
0
    def format_metric_value(self, name, value):
        """Format the value of specified metric.

        Parameters
        ----------
        name : str
            Name of the metric.

        value : any
            Value of the metric.

        Returns
        -------
        str
            Human readable string representation of the metric value.
        """
        if name.endswith('time') or name.endswith('timer'):
            return humanize_duration(value)
        else:
            return '%.6g' % (value,)
Exemple #4
0
    def test_examples_can_run_one_step(self):
        timer = -time.time()

        # discover all example scripts
        def walk(pa, dst):
            for fn in os.listdir(pa):
                fp = os.path.join(pa, fn)
                if os.path.isdir(fp):
                    walk(fp, dst)
                elif fp.endswith('.py'):
                    with codecs.open(fp, 'rb', 'utf-8') as f:
                        cnt = f.read()
                    if re.search(r'''if\s+__name__\s*==\s+(['"])__main__\1:''',
                                 cnt):
                        if 'max_step=config.max_step' not in cnt:
                            raise RuntimeError(
                                'Example script does not have '
                                'max_step configuration: {}'.format(fp))
                        dst.append(fp)
            return dst

        examples_dir = os.path.join(
            os.path.split(os.path.abspath(__file__))[0],
            '../../tfsnippet/examples')
        examples_scripts = walk(examples_dir, [])

        # run all examples scripts for just max_step
        env_dict = copy.copy(os.environ)

        for example_script in examples_scripts:
            print('Run {} ...'.format(example_script))

            with TemporaryDirectory() as tempdir:
                args = [sys.executable, '-u', example_script, '--max_step=1']
                subprocess.check_call(args, cwd=tempdir, env=env_dict)
                print('')

        # report finished tests
        print('Finished to run {} example scripts in {}.'.format(
            len(examples_scripts), humanize_duration(time.time() + timer)))
    def println(self, message, with_tag=False, with_log=True):
        """
        打印

        Args:
            message (str): 内容
            with_tag (bool): 是否要加 epoch & step 标签?
                (default :obj:`False`)
        """
        self._require_entered()
        suffix = ""
        if with_tag:

            def format_tag(v, max_v, name):
                if max_v is not None:
                    return '{} {}/{}'.format(name, v, max_v)
                else:
                    return '{} {}'.format(name, v)

            if not self._within_step and not self._within_epoch:
                self._require_context()
            tags = []
            if self._max_epoch != 1:
                tags.append(format_tag(self._epoch, self._max_epoch, 'Epoch'))
            tags.append(format_tag(self._step, self._max_step, 'Step'))
            if self._show_eta:
                progress = self.get_progress()
                if progress is not None:
                    eta = self._eta.get_eta(progress)
                    if eta is not None:
                        tags.append('ETA {}'.format(humanize_duration(eta)))
            suffix = '[{}]'.format(', '.join(tags))
        if with_log:
            self._print_func(suffix)
            self._print_func(message)
        return suffix, message
Exemple #6
0
 def format_metric(self, name, value):
     if name.endswith('time') or name.endswith('timer'):
         return humanize_duration(float(value))
     else:
         return '{:.6g}'.format(float(value))