<commit_before>from __future__ import (absolute_import, division, print_function)
from builtins import super
from future.utils import implements_iterator, string_types
from . import algorithms
from . import proxops
from itertools import count
from collections import namedtuple, defaultdict
from .utils import wrap, restruct, destruct
import numpy as np
import tableprint as tp
import sys
try:
    from time import perf_counter
except ImportError:
    from time import time as perf_counter


class Optimizer(object):
    def __init__(self, theta_init, display=sys.stdout):
        self.iteration = 0
        self.theta = theta_init
        self.runtimes = list()
        self.store = defaultdict(list)
        self.display = display

    def __next__(self):
        raise NotImplementedError

    def optional_print(self, message):
        if self.display:
            self.display.write(message + "\n")
            self.display.flush()

    def run(self, maxiter=None):
        maxiter = np.inf if maxiter is None else (maxiter + self.iteration)
        try:
            self.optional_print(tp.header(['Iteration', 'Objective', 'Runtime']))
            for k in count(start=self.iteration):

                self.iteration = k

                # get the next iteration, time how long it takes
                tstart = perf_counter()
                self.theta = next(self)
                self.runtimes.append(perf_counter() - tstart)

                # TODO: run callbacks
                self.store['objective'].append(self.objective(destruct(self.theta)))

                # Update display
                self.optional_print(tp.row([self.iteration,
                                    self.store['objective'][-1],
                                    tp.humantime(self.runtimes[-1])]))

                # TODO: check for convergence
                if k >= maxiter:
                    break

        except KeyboardInterrupt:
            pass

        # cleanup
        self.optional_print(tp.hr(3))
        self.optional_print(u'\u279b Final objective: {}'.format(self.store['objective'][-1]))
        self.optional_print(u'\u279b Total runtime: {}'.format(tp.humantime(sum(self.runtimes))))
        self.optional_print(u'\u279b Per iteration runtime: {} +/- {}'.format(
            tp.humantime(np.mean(self.runtimes)),
            tp.humantime(np.std(self.runtimes)),
        ))

    def restruct(self, x):
        return restruct(x, self.theta)


@implements_iterator<commit_msg>Updated to work with tableprint v0.3.0<commit_after>from __future__ import (absolute_import, division, print_function)
from builtins import super
from future.utils import implements_iterator, string_types
from . import algorithms
from . import proxops
from itertools import count
from collections import namedtuple, defaultdict
from .utils import wrap, restruct, destruct
import numpy as np
import tableprint as tp
import sys
try:
    from time import perf_counter
except ImportError:
    from time import time as perf_counter


class Optimizer(object):
    def __init__(self, theta_init, display=sys.stdout):
        self.iteration = 0
        self.theta = theta_init
        self.runtimes = list()
        self.store = defaultdict(list)
        self.display = display

    def __next__(self):
        raise NotImplementedError

    def optional_print(self, message):
        if self.display:
            self.display.write(message + "\n")
            self.display.flush()

    def run(self, maxiter=None):
        maxiter = np.inf if maxiter is None else (maxiter + self.iteration)
        try:
            self.optional_print(tp.header(['Iteration', 'Objective', 'Runtime']))
            for k in count(start=self.iteration):

                self.iteration = k

                # get the next iteration, time how long it takes
                tstart = perf_counter()
                self.theta = next(self)
                self.runtimes.append(perf_counter() - tstart)

                # TODO: run callbacks
                self.store['objective'].append(self.objective(destruct(self.theta)))

                # Update display
                self.optional_print(tp.row([self.iteration,
                                            self.store['objective'][-1],
                                            tp.humantime(self.runtimes[-1])]))

                # TODO: check for convergence
                if k >= maxiter:
                    break

        except KeyboardInterrupt:
            pass

        # cleanup
        self.optional_print(tp.bottom(3))
        self.optional_print(u'\u279b Final objective: {}'.format(self.store['objective'][-1]))
        self.optional_print(u'\u279b Total runtime: {}'.format(tp.humantime(sum(self.runtimes))))
        self.optional_print(u'\u279b Per iteration runtime: {} +/- {}'.format(
            tp.humantime(np.mean(self.runtimes)),
            tp.humantime(np.std(self.runtimes)),
        ))

    def restruct(self, x):
        return restruct(x, self.theta)


@implements_iterator
{'fraction_non_alphanumeric': 0.09958175662218681, 'fraction_numerical': 0.006174068910575582, 'mean_word_length': 2.158490566037736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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}}
{'dir': 'git-commits-cleaned', 'id': '178613', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1334, 'n_words': 340}