[docs]defget_config(key):'''Get config value from settings.ini. Parameters ---------- key : str Key in settings.ini. Returns ------- value : str Value in settings.ini. '''has_config=os.path.isfile('settings.ini')ifhas_config:importconfigparserconfig=configparser.ConfigParser()config_path=os.path.abspath('settings.ini')print("[I] Found settings.ini at",config_path)config.read(config_path)path=config["PATHS"][key]else:print("[W] No settings.ini found.")path=Nonereturnpath
[docs]deflog2html(df,log_name,open_browser=True,log_path=None,browser_path=None):'''Display and save a dataframe in HTML, and open it in browser if needed. Please create settings.ini or set ``file_path`` and ``browser_path`` manually before calling. Parameters ---------- df : pandas.DataFrame Dataframe to be displayed in HTML. log_name : str Name of the log file. open_browser : bool Whether to open in browser. log_path : str Path to save the log file. browser_path : str Path of the browser. '''log_path=get_config(key="saved_logs")iflog_pathisNoneelselog_pathbrowser_path=get_config(key="browser")+r' %s'ifbrowser_pathisNoneelsebrowser_pathhtml_head='''<!DOCTYPE html><html><head> <style> table, th, td { border: 1px solid black; border-collapse: collapse; font-family: math; } th, td { border-style: dotted; border-color: black; } table.dataframe td:nth-child(odd) { background-color: #cecece; } table.dataframe tr:nth-child(even) { background-color: #e8e8e8; } </style></head><body>'''html_tail='''</body>'''html=df.to_html()html=html_head+html+html_tailfull_path=_make_html(log_path,log_name,html)ifopen_browser:_open_html(full_path,browser_path)
[docs]deflog2latex(df,log_name,open_browser=True,log_path=None,browser_path=None):'''Display a dataframe in TeX on overleaf.com. This tool automatically highlights the maximum values in each column. Parameters ---------- df : pandas.DataFrame Dataframe to be displayed in TeX. log_name : str Name of the log file. open_browser : bool Whether to open in browser. log_path : str Path to save the log file. browser_path : str Path of the browser. '''log_path=get_config(key="saved_logs")iflog_pathisNoneelselog_pathbrowser_path=get_config(key="browser")+r' %s'ifbrowser_pathisNoneelsebrowser_pathwidth=int(df.columns.size*0.8)height=int(len(df)*0.2)+1.0geometry=f"[left=20px,right=10px,top=10px,bottom=20px,paperwidth={width}in,paperheight={height}in]"latex_head=r'''\documentclass{article}\usepackage{graphicx}\usepackage{booktabs}\usepackage{lscape}\usepackage[table]{xcolor}\usepackage'''+geometry+r'''{geometry}\title{'''+log_name+r'''}\begin{document}'''latex_tail=r'''\end{document}'''# TODO: enable highlight_max# latex = df.style.highlight_max(props='cellcolor:[HTML]{FFFF00}; color:{red};')# latex = latex.to_latex(hrules=False, clines="skip-last;data", multicol_align='c')latex=df.to_latex()latex=latex_head+latex+latex_tailhtml_head='''<body onload="document.forms['open_overleaf'].submit()"> <form action="https://www.overleaf.com/docs" method="post" name="open_overleaf"> <input type="text" name="snip_uri" value="data:application/x-tex;base64,'''html_tail='''"><br> </form></body>'''latex_bytes=latex.encode("ascii")latex_b64code=base64.b64encode(latex_bytes)latex_b64str=latex_b64code.decode("ascii")html=html_head+latex_b64str+html_tailfull_path=_make_html(log_path,log_name+" overleaf",html)ifopen_browser:_open_html(full_path,browser_path)
[docs]def_make_name(model=None,model_name=None,format="%Y-%m-%d %H-%M-%S-%f "):'''Make a file name for an instance of a model. Milliseconds are added to the end of the name to make it unique. Parameters ---------- model : object Model object. model_name : str Name of the model. format : str Format of the timestamp. Returns ------- model_name : str Name of the model. '''ifmodelisNoneandmodel_nameisNone:print("[E] In _make_name(), model and model_name cannot be both None.")ifmodel_nameisNone:model_name=str(type(model))model_name=re.split(r'[`\-=~!@#$%^&*()_+\[\]{};\'\\:"|<,./<>?]',model_name)[-3]model_name=pd.Timestamp.now().strftime(format)+model_namereturnmodel_name
[docs]def_make_html(file_path,file_name,html):'''Make a html file. Parameters ---------- file_path : str Path to save the html file. file_name : str Name of the html file. html : str HTML code. Returns ------- full_path : str Full path of the html file. '''full_path=os.path.join(os.path.abspath(file_path),file_name+".html")withopen(full_path,"w")asf:f.write(html)print("[I] HTML saved as: "+os.path.abspath(full_path))returnfull_path
[docs]def_open_html(full_path,browser_path):'''Open a html file in browser. Parameters ---------- full_path : str Full path of the html file. browser_path : str Path of the browser. '''print("[I] Opening HTML in browser: "+browser_path)webbrowser.get(using=browser_path).open('file:///'+os.path.abspath(full_path),new=2)