dhg.utils

Logging

dhg.utils.default_log_formatter()[source]

Create a default formatter of log messages for logging.

dhg.utils.simple_stdout2file(file_path)[source]

This function simply wraps the sys.stdout stream, and outputs messages to the sys.stdout and a specified file, simultaneously.

Parameters

file_path (file_path: Union[str, Path]) – The path of the file to output the messages.

Download

dhg.utils.download_file(url, file_path)[source]

Download a file from a url.

Parameters
  • url (str) – the url of the file

  • file_path (str) – the path to the file

dhg.utils.check_file(file_path, md5)[source]

Check if a file is valid.

Parameters
  • file_path (Path) – The local path of the file.

  • md5 (str) – The md5 of the file.

Raises

FileNotFoundError – Not found the file.

dhg.utils.download_and_check(url, file_path, md5)[source]

Download a file from a url and check its integrity.

Parameters
  • url (str) – The url of the file.

  • file_path (Path) – The path to the file.

  • md5 (str) – The md5 of the file.

Structure Helpers

dhg.utils.remap_edge_list(e_list, bipartite_graph=False, ret_map=False)[source]

Remap the vertex markers to numbers of an ordered and continuous range.

Note

This function can support both low-order structures and high-order structures.

Parameters
  • e_list (List[tuple]) – Edge list of low-order structures or high-order structures.

  • bipartite_graph (bool) – Whether the structure is bipartite graph. Defaults to False.

  • ret_map (bool) – Whether to return the map dictionary of raw marker to new index. Defaults to False.

dhg.utils.remap_edge_lists(*e_lists, bipartite_graph=False, ret_map=False)[source]

Remap the vertex markers to numbers of an ordered and continuous range for given multiple edge lists.

Note

This function can support both low-order structures and high-order structures.

Parameters
  • e_lists (List[List[tuple]]) – The list of edge list of low-order structures or high-order structures.

  • bipartite_graph (bool) – Whether the structure is bipartite graph. Defaults to False.

  • ret_map (bool) – Whether to return the map dictionary of raw marker to new index. Defaults to False.

dhg.utils.remap_adj_list(adj_list, bipartite_graph=False, ret_map=False)[source]

Remap the vertex markers to numbers of an ordered and continuous range.

Note

This function can only support low-order structures like graph, directed graph, and bipartite graph.

Parameters
  • adj_list (List[List[int]]) – Adjacency list of low-order structures.

  • bipartite_graph (bool) – Whether the structure is bipartite graph. Defaults to False.

  • ret_map (bool) – Whether to return the map dictionary of raw marker to new index. Defaults to False.

dhg.utils.remap_adj_lists(*adj_lists, bipartite_graph=False, ret_map=False)[source]

Remap the vertex markers to numbers of an ordered and continuous range for given multiple adjacency lists.

Note

This function can only support low-order structures like graph, directed graph, and bipartite graph.

Parameters
  • adj_lists (List[List[List[int]]]) – The list of adjacency list of low-order structures.

  • bipartite_graph (bool) – Whether the structure is bipartite graph. Defaults to False.

  • ret_map (bool) – Whether to return the map dictionary of raw marker to new index. Defaults to False.

dhg.utils.edge_list_to_adj_list(e_list)[source]

Convert edge list to adjacency list for low-order structures.

Note

Adjacency list can only represent low-order structures like graph, directed graph, and bipartite graph.

Parameters

e_list (List[Tuple[int, int]]) – Edge list.

dhg.utils.edge_list_to_adj_dict(e_list)[source]

Convert edge list to adjacency dictionary for low-order structures.

Note

Adjacency list can only represent low-order structures like graph, directed graph, and bipartite graph.

Parameters

e_list (List[Tuple[int, int]]) – Edge list.

dhg.utils.adj_list_to_edge_list(adj_list)[source]

Convert adjacency list to edge list for low-order structures.

Note

Adjacency list can only represent low-order structures like graph, directed graph, and bipartite graph.

Parameters

adj_list (List[List[int]]) – Adjacency list.

Dataset Wrapers

class dhg.utils.UserItemDataset(*args, **kwargs)[source]

Bases: torch.utils.data.Dataset

The dataset class of user-item bipartite graph for recommendation task.

Parameters
  • num_users (int) – The number of users.

  • num_items (int) – The number of items.

  • user_item_list (List[Tuple[int, int]]) – The list of user-item pairs.

  • train_user_item_list (List[Tuple[int, int]], optional) – The list of user-item pairs for training. This is only needed for testing to mask those seen items in training. Defaults to None.

  • strict_link (bool) – Whether to iterate through all interactions in the dataset. If set to False, in training phase the dataset will keep randomly sampling interactions until meeting the same number of original interactions. Defaults to True.

  • phase (str) – The phase of the dataset can be either "train" or "test". Defaults to "train".

__getitem__(index)[source]

Return the item at the index. If the phase is "train", return the (User-PositiveItem-NegativeItem) triplet. If the phase is "test", return all true positive items for each user.

Parameters

index (int) – The index of the item.

__len__()[source]

Return the length of the dataset. If the phase is "train", return the number of interactions. If the phase is "test", return the number of users.

sample_neg_item(user)[source]

Sample a negative item for the sepcified user.

Parameters

user (int) – The index of the specified user.

sample_triplet()[source]

Sample a triple of user, positive item, and negtive item from all interactions.