File size: 12,848 Bytes
631641c |
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
###################################################################################
# ALEX'S UTILITIES
#
# alternatively
# url = "https://alx.gd/util"
# with httpimport.remote_repo(url):
# import util
#
# poetry add pandas tabulate rich
import json
import os
import shlex
import struct
import platform
import subprocess
import tabulate
from IPython.display import clear_output
import rich
import datetime
import time
import random
import string
import pandas as pd
import logging
import importlib
from uuid import uuid4
from IPython import embed
import numpy as np
from pathlib import Path
import inspect
import sys
from rich import console
console = console.Console()
def find_root_from_readme():
# Attempt to find README.md in the current directory and up two levels
max_levels_up = 2
current_dir = os.path.abspath(os.curdir)
for _ in range(max_levels_up + 1):
# Construct the path to where README.md might be
readme_path = os.path.join(current_dir, "README.md")
# Check if README.md exists at this path
if os.path.isfile(readme_path):
# Return the absolute path if found
return os.path.dirname(os.path.abspath(readme_path))
# Move up one directory level
current_dir = os.path.dirname(current_dir)
# Return None if README.md was not found
return None
ROOT_DIR = find_root_from_readme()
PAPER_DIR = os.path.join(ROOT_DIR, 'manuscript')
FIG_DIR = os.path.join(PAPER_DIR, 'figures')
AUDIO_DIR = os.path.join(ROOT_DIR, 'audio_files')
pd.set_option('display.max_colwidth', 70)
def configure_logging():
filename = f"{ROOT_DIR}/logs/log.txt"
if not os.path.exists(filename):
os.makedirs(os.path.dirname(filename), exist_ok=True)
logging.basicConfig(filename=filename,
filemode='w',
format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M',
level=logging.DEBUG)
return filename
def get_root_dir():
# assumes in the root/utilities folder
return os.path.dirname(os.path.abspath("../README.md"))
def get_fig_dir():
return get_root_dir() + "/manuscript/figures"
def reload():
importlib.reload(util)
def generate_hash():
return str(uuid4())
def generate_random_string():
return ''.join(random.choice(string.ascii_lowercase) for _ in range(2)) + ''.join(
random.choice(string.digits) for _ in range(2)) + ''.join(
random.choice(string.ascii_lowercase) for _ in range(2)) + ''.join(
random.choice(string.digits) for _ in range(2)) + ''.join(
random.choice(string.ascii_lowercase) for _ in range(2))
def wait_rand():
wait_time = random.randint(1, 3)
time.sleep(wait_time)
def log_and_print(text):
logging.info(text)
print(text)
def log(text):
logging.info(text)
def get_timestamp():
timestamp = '{:%Y-%m-%d-T-%H-%M-%S}'.format(datetime.datetime.now())
return timestamp
def printl(text):
print(text, end="")
def cprint(text):
clear_output(wait=True)
print(text, flush=True)
def log_and_rich_print(text):
logging.info(text)
rich.print(text, flush=True)
def rprint(text):
rich.print(text, flush=True)
def lp(text):
log_and_rich_print(text)
def start_log_task(text):
rich.print(f"[yellow] β {text} [/yellow]", flush=True, end="...")
logging.info(text)
def log_task(text):
rich.print(f"[yellow] β¦Ώ {text} [/yellow]", flush=True)
logging.info(text)
def end_log_task():
rich.print(f"[yellow bold] Done [/yellow bold]", flush=True)
logging.info("Done")
def log_mini_task(text, text2=None):
if text2:
text = text + "..." + str(text2)
console.log(f" βββ {text} ", style="italic")
logging.info(text)
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def tab_cols(df, cns):
for cn in cns:
print("\n\n{}".format(titlecase(cn)))
print(tabulate.tabulate(pd.DataFrame(df[cn].value_counts()), tablefmt="pipe", headers=['Name', 'Count']))
def tab(df, tbformat="heavy_grid"):
print(tabulate.tabulate(df, headers='keys', tablefmt=tbformat, showindex=False))
def header(m):
length = get_terminal_size()[0]
print(colored(m, 'yellow'))
print(colored('β' * length, 'white'))
def alert(m, error_code):
text_color = ['green', 'yellow', 'red', 'white'][error_code]
length = get_terminal_size()[0]
print(colored('\n > ' + m, text_color))
def hr():
length = get_terminal_size()[0]
print(colored('-' * length, 'white'))
def logo():
logo = '''
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββ ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββ [bold]SYNTHETIC[/bold] ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββ [yellow bold]PATIENT[/yellow bold] ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββ [yellow]PROJECT[/yellow] ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββ ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββ βββββββββββββββββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββ ββββ βββ ββββββββββββββββββββββββββββ β
β ββββββββββββββββββββ β β ββ β βββ βββββββββββββββββββββ β
β βββββββββββββββββββββ β ββ ββββ βββββββββ ββββββββββββββββββββ β
β ββββββββββββββββββββββ βββββββ ββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββ βββββββ ββββββββββββββ βββββββββββββββββββββββ β
β ββββββββββββββββββββββ ββ ββββ ββββββββββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββ βββββββ β β βββββββββββββββββββββββββββββββ β
β βββββββββββββββββββββββββββββ βββ ββββββββββββββββββββββββββββββ β
β ββββββββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββββββββ β
β ββββββββββββββββββββββββ ββββββββββββββββββ ββββββββββββββββββββββββ β
β ββββββββββββββββββββββ βββββββββββββββ ββββββββββββββββββββββ β
β βββββββββββββββββββ βββββ βββββββββββββββββββ β
β ββββββββββββββ ββ β βββββββ β βββββββββββββ β
β βββββββββ ββββββββββββββββ βββββββββ β
β βββββββ ββββββββ βββ βββββββ β
β ββββββ ββ ββββββ βββ β ββββββ β
β βββββ β β β βββββ β
β ββββ βββββββββββββββββββ βββββ ββββββββββββββββββββ ββββ β
β ββββ βββββ βββ βββββ ββββ β
β ββββββββ βββ βββ ββββββββ β
β βββ βββββ βββ βββ β
β ββ ββ β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
β [italic white]Ahmed Al-Farsi, Synthetic Patient #1[/italic white] β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
[blue]Β© [bold]GRANOLA AI[/bold][/blue]
'''
rich.print(logo)
def initialize():
logging_file_path = configure_logging()
clear()
logo()
start_log_task("Loading utilities")
end_log_task()
log_mini_task("Logging configured at: " + logging_file_path)
log_mini_task("Utilities loaded")
if __name__ == "__main__":
print("hello world")
|