aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/utils/MiscFeatures.py
blob: ba0b1b6687911a31a0276268dc4efe68f4f943f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python

import matplotlib.pyplot as mpyplot
import matplotlib.patches as mpatches

# Print iterations progress
def printProgressBar (iteration:int, total:int, prefix:str = '', suffix:str = '', decimals:int = 1, length:int = 100, fill:str = '█', printEnd:str = "\r"):
    """Print iterations progress.  
    Call in a loop to create terminal progress bar.  
    - current iteration  
    - total iterations  
    - prefix string  
    - suffix string  
    - positive number of decimals in percent complete  
    - character length of bar  
    - bar fill character  
    - end character (e.g. "\r", "\r\n")
    """
    percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
    filledLength = int(length * iteration // total)
    bar = fill * filledLength + '-' * (length - filledLength)
    print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)

    # Print New Line on Complete
    if iteration == total: 
        print()

# Plot timestamped dataframe as stem
def plotTimestampedDataframe(df, name: str, color='#000000'):

    markerline, stemlines, baseline = mpyplot.stem(df.index, df[name])
    mpyplot.setp(markerline, color=color, linewidth=1, markersize = 1)
    mpyplot.setp(stemlines, color=color, linewidth=1)
    mpyplot.setp(baseline, color=color, linewidth=1)

# Handle exit event
class ExitSignalHandler():

    def __init__(self):

        import signal
        import threading

        global __exit_event
        global __on_exit_signal

        __exit_event = threading.Event()

        def __on_exit_signal(signo, _frame):
            __exit_event.set()

        for sig in ('TERM', 'HUP', 'INT'):
            signal.signal(getattr(signal, 'SIG'+sig), __on_exit_signal)

    def status(self):
        return __exit_event.is_set()