<commit_before>"""
A simple progress bar to monitor MCMC sampling progress.
Modified from original code by <NAME> (2010)
"""

from __future__ import print_function

import sys, time
import uuid
try:
    from IPython.core.display import HTML, Javascript, display
except ImportError:
    pass

__all__ = ['progress_bar']

class ProgressBar(object): 
    def __init__(self, iterations, animation_interval = .5):
        self.iterations = iterations
        self.start = time.time()
        self.last = 0
        self.animation_interval = animation_interval

    def percentage(self, i): 
        return 100*i/float(self.iterations)

    def update(self, i):
        elapsed = time.time() - self.start
        i = i + 1
        
        if elapsed - self.last > self.animation_interval or i == self.iterations:
             self.animate(i+1, elapsed)
             self.last = elapsed
        
class TextProgressBar(ProgressBar):
    def __init__(self, iterations, printer):
        self.fill_char = '-'
        self.width = 40
        self.printer = printer

        ProgressBar.__init__(self, iterations)
        self.update(0)

    def animate(self, i, elapsed):
        self.printer(self.progbar(i, elapsed))


    def progbar(self,i, elapsed): 
        bar = self.bar(self.percentage(i))
        return "[%s] %i of %i complete in %.1f sec" % (bar, i, self.iterations, round(elapsed,1))
 
    def bar(self, percent):
        all_full = self.width - 2
        num_hashes = int(percent/100 * all_full)
        
        bar = self.fill_char * num_hashes + ' ' * (all_full - num_hashes)
        
        info = '%d%%' % percent
        loc = (  len(bar) - len(info)  )/2
        return replace_at(bar, info, loc, loc + len(info))


def replace_at(str, new, start, stop):
    return str[:start] + new + str[stop:]

def consoleprint(s):
    if sys.platform.lower().startswith('win'):
        print(s, '\r', end='')
    else:
        print(s)
    
def ipythonprint(s):
    print('\r', s, end='')
    sys.stdout.flush()

class IPythonNotebookPB(ProgressBar):
    def __init__(self, iterations):
        self.divid = str(uuid.uuid4())
        self.sec_id = str(uuid.uuid4())

        pb = HTML(
            """
            <div style="float: left; border: 1px solid black; width:500px">
              <div id="%s" style="background-color:blue; width:0%%">&nbsp;</div>
            </div> 
            <label id="%s" style="padding-left: 10px;" text = ""/>
            """ % (self.divid,self.sec_id))
        display(pb)

        ProgressBar.__init__(self, iterations)

    def animate(self, i, elapsed):
        percentage = int(self.fraction(i))

        display(Javascript("$('div#%s').width('%i%%')" % (self.divid, percentage)))
        display(Javascript("$('label#%s').text('%i%% in %.1f sec')" % (self.sec_id, fraction, round(elapsed, 1))))

def run_from_ipython():
        try:
            __IPYTHON__
            return True
        except NameError:
            return False

def progress_bar(iters):
    if run_from_ipython(): 
        if in_notebook():
            return NotebookProgressBar(iters)
        else: 
            return IPythonConsolePB(iters, ipythonprint)
    else:
        return TextProgressBar(iters, consoleprint)<commit_msg>use text progress bar until we figure out how to detect notebook<commit_after>"""
A simple progress bar to monitor MCMC sampling progress.
Modified from original code by <NAME> (2010)
"""

from __future__ import print_function

import sys, time
import uuid
try:
    from IPython.core.display import HTML, Javascript, display
except ImportError:
    pass

__all__ = ['progress_bar']

class ProgressBar(object): 
    def __init__(self, iterations, animation_interval = .5):
        self.iterations = iterations
        self.start = time.time()
        self.last = 0
        self.animation_interval = animation_interval

    def percentage(self, i): 
        return 100*i/float(self.iterations)

    def update(self, i):
        elapsed = time.time() - self.start
        i = i + 1
        
        if elapsed - self.last > self.animation_interval or i == self.iterations:
             self.animate(i+1, elapsed)
             self.last = elapsed
        
class TextProgressBar(ProgressBar):
    def __init__(self, iterations, printer):
        self.fill_char = '-'
        self.width = 40
        self.printer = printer

        ProgressBar.__init__(self, iterations)
        self.update(0)

    def animate(self, i, elapsed):
        self.printer(self.progbar(i, elapsed))


    def progbar(self,i, elapsed): 
        bar = self.bar(self.percentage(i))
        return "[%s] %i of %i complete in %.1f sec" % (bar, i, self.iterations, round(elapsed,1))
 
    def bar(self, percent):
        all_full = self.width - 2
        num_hashes = int(percent/100 * all_full)
        
        bar = self.fill_char * num_hashes + ' ' * (all_full - num_hashes)
        
        info = '%d%%' % percent
        loc = (  len(bar) - len(info)  )/2
        return replace_at(bar, info, loc, loc + len(info))


def replace_at(str, new, start, stop):
    return str[:start] + new + str[stop:]

def consoleprint(s):
    if sys.platform.lower().startswith('win'):
        print(s, '\r', end='')
    else:
        print(s)
    
def ipythonprint(s):
    print('\r', s, end='')
    sys.stdout.flush()

class IPythonNotebookPB(ProgressBar):
    def __init__(self, iterations):
        self.divid = str(uuid.uuid4())
        self.sec_id = str(uuid.uuid4())

        pb = HTML(
            """
            <div style="float: left; border: 1px solid black; width:500px">
              <div id="%s" style="background-color:blue; width:0%%">&nbsp;</div>
            </div> 
            <label id="%s" style="padding-left: 10px;" text = ""/>
            """ % (self.divid,self.sec_id))
        display(pb)

        ProgressBar.__init__(self, iterations)

    def animate(self, i, elapsed):
        percentage = int(self.fraction(i))

        display(Javascript("$('div#%s').width('%i%%')" % (self.divid, percentage)))
        display(Javascript("$('label#%s').text('%i%% in %.1f sec')" % (self.sec_id, fraction, round(elapsed, 1))))

def run_from_ipython():
        try:
            __IPYTHON__
            return True
        except NameError:
            return False

def progress_bar(iters):
    if run_from_ipython(): 
        if None:
            return NotebookProgressBar(iters)
        else: 
            return TextProgressBar(iters, ipythonprint)
    else:
        return TextProgressBar(iters, consoleprint)
{'fraction_non_alphanumeric': 0.1335579721243682, 'fraction_numerical': 0.009802419972430693, 'mean_word_length': 2.393970893970894, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '5789343', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2023, 'n_words': 496}