File

File Management.

the_utils.file.dataframe_to_latex_3line(df: DataFrame, file_path: str, caption: str | None = None, label: str | None = None) None[source]

Convert a DataFrame to a LaTeX 3-line tabular format with bolded model names and second-largest values underlined.

Parameters:
  • df (pd.DataFrame) – pd.DataFrame table.

  • file_path (str) – Path to save the latex table.

  • caption (str, optional) – Table caption. Defaults to None.

  • label (str, optional) – Table label. Defaults to None.

the_utils.file.csv_to_table(raw_path: str, save_path: str, row_key: str, col_key: str, val_key: str, fillna: float | str | None = None, row_order: List[str] | None = None, col_order: List[str] | None = None, average_rank: bool = True, bold_max: bool = True, save_latex: bool = False, caption: str = 'Comparison of Models Across Datasets', label: str = 'tab:model_comparison') DataFrame[source]

Transfer a csv into table.

Parameters:
  • raw_path (str) – raw csv path.

  • save_path (str) – path to save the table.

  • row_key (str) – key for row index.

  • col_key (str) – key for column index.

  • val_key (str) – key for value index.

  • fillna (Union[float, str], optional) – fill empty cell with the value given. Defaults to None.

  • row_order (List[str], optional) – sort the rows with given order list. Defaults to None.

  • col_order (List[str], optional) – sort the columns with given order list. Defaults to None.

  • average_rank (bool, optional) – whether to add a column with average ranks. Defaults to True.

  • bold_max (bool, optional) – whether to wrap the maximum value of each column with **value**. Defaults to True.

  • save_latex (bool, optional) – whether to save the latex table version. Defaults to True.

  • caption (str, optional) – Table caption. Defaults to “Comparison of Models Across Datasets”.

  • label (str, optional) – Table label. Defaults to “tab:model_comparison”.

Returns:

table.

Return type:

pd.DataFrame

the_utils.file.make_parent_dirs(target_path: PurePath) None[source]

make all the parent dirs of the target path.

Parameters:

target_path (PurePath) – target path.

the_utils.file.refresh_file(target_path: str | None = None) None[source]

clear target path

Parameters:

target_path (str) – file path

the_utils.file.csv2file(target_path: str, thead: List[str] | None = None, tbody: List[Any] | None = None, refresh: bool = False, is_dict_list: bool = False, sort_head: bool = False) None[source]

save data to target_path of a csv file.

Parameters:
  • target_path (str) – target path

  • thead (List[str], optional) – csv table header, only written into the file when it is not None and file is empty. Defaults to None.

  • tbody (List, optional) – csv table content. Defaults to None.

  • refresh (bool, optional) – whether to clean the file first. Defaults to False.

  • is_dict_list (bool, optional) – whether the tbody is in the format of a list of dicts. Defaults to False.

  • sort_head (bool, optional) – whether to sort the head with lowercase before writing. Defaults to False.

Example

from the_utils import csv2file
save_file = "./results/example.csv"
final_params = {
    "dataset": "cora",
    "acc": "99.1",
    "NMI": "89.0",
}
thead=[]
# list of values
csv2file(
    target_path=save_file,
    thead=list(final_params.keys()),
    tbody=list(final_params.values()),
    refresh=False,
    is_dict_list=False,
)
# list of dicts
csv2file(
    target_path=save_file,
    tbody=[
        {
            "a": 1,
            "b": 2
        },
        {
            "a": 2,
            "b": 1
        },
    ],
    is_dict_list=True,
)
the_utils.file.save_to_csv_files(results: dict, csv_name: str, insert_info: dict | None = None, append_info: dict | None = None, save_path='./results', sort_head: bool = False) None[source]

Save the evaluation results to a local csv file.

Parameters:
  • results (dict) – Evaluation results document.

  • csv_name (str) – csv file name to store.

  • insert_info (dict) – Insert information in front of the results. Defaults to None.

  • append_info (dict) – Append information after the results. Defaults to None.

  • save_path (str, optional) – Folder path to store. Defaults to ‘./results’.

  • sort_head (bool, optional) – whether to sort the head before writing. Defaults to False.

Example

from the_utils import evaluate_from_embed_file
from the_utils import save_to_csv_files

method_name='orderedgnn'
data_name='texas'

clustering_res, classification_res = evaluate_from_embed_file(
    f'{data_name}_{method_name}_embeds.pth',
    f'{data_name}_data.pth',
    save_path='./save/',
)

insert_info = {'data': data_name, 'method': method_name,}
save_to_csv_files(clustering_res, insert_info, 'clutering.csv')
save_to_csv_files(classification_res, insert_info, 'classification.csv')