File

File Management.

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, add_info: dict, csv_name: str, save_path='./results', sort_head: bool = False) None[source]

Save the evaluation results to a local csv file.

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

  • add_info (dict) – Additional information, such as data set name, method name.

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

  • 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/',
)

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