|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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(): |
|
|
|
max_levels_up = 2 |
|
current_dir = os.path.abspath(os.curdir) |
|
|
|
for _ in range(max_levels_up + 1): |
|
|
|
readme_path = os.path.join(current_dir, "README.md") |
|
|
|
|
|
if os.path.isfile(readme_path): |
|
|
|
return os.path.dirname(os.path.abspath(readme_path)) |
|
|
|
|
|
current_dir = os.path.dirname(current_dir) |
|
|
|
|
|
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(): |
|
|
|
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") |
|
|