opengsl.utils

opengsl.utils.accuracy(labels, logits)[source]

Compute the accuracy score given true labels and predicted labels.

Parameters
  • labels (np.array) – Ground truth labels.

  • logits (np.array) – Predicted labels.

Returns

accuracy – The Accuracy score.

Return type

np.float

opengsl.utils.get_adjusted_homophily(_label, adj)[source]

Calculate adjusted homophily of a graph.

Parameters
  • _label (torch.tensor) – The ground truth labels.

  • adj (torch.tensor) – The adjacency matrix in dense form.

Returns

homophily – The adjusted homophily of a graph.

Return type

np.float

opengsl.utils.get_edge_homophily(label, adj)[source]

Calculate the node homophily of a graph.

Parameters
  • label (torch.tensor) – The ground truth labels.

  • adj (torch.tensor) – The adjacency matrix in dense form.

Returns

homophily – The edge homophily of the graph.

Return type

torch.float

opengsl.utils.get_homophily(label, adj, type='node', fill=None)[source]

Calculate node or edge homophily of a graph.

Parameters
  • label (torch.tensor) – The ground truth labels.

  • adj (torch.tensor) – The adjacency matrix in dense form.

  • type (str) – This decides whether to calculate node homo or edge homo.

  • fill (str) – The value to fill in the diagonal of adj. If set to None, the operation won’t be done.

Returns

homophily – The node or edge homophily of a graph.

Return type

np.float

opengsl.utils.get_label_informativeness(_label, adj)[source]

Calculate label informativeness of a graph.

Parameters
  • _label (torch.tensor) – The ground truth labels.

  • adj (torch.tensor) – The adjacency matrix in dense form.

Returns

label_informativeness – The label informativeness of a graph.

Return type

np.float

opengsl.utils.get_node_homophily(label, adj)[source]

Calculate the node homophily of a graph.

Parameters
  • label (torch.tensor) – The ground truth labels.

  • adj (torch.tensor) – The adjacency matrix in dense form.

Returns

homophily – The node homophily of the graph.

Return type

torch.float

opengsl.utils.scipy_sparse_to_sparse_tensor(sparse_mx)[source]

Convert a scipy sparse matrix to a torch sparse tensor.

Parameters

sparse_mx (scipy.sparse_matrix) – Sparse matrix to convert.

Returns

sparse_tensor – A tensor stored in sparse form.

Return type

torch.Tensor in sparse form

opengsl.utils.set_seed(seed)[source]

Set seed to make sure the results can be repetitive.

Parameters

seed (int) – Random seed to set.

opengsl.utils.sparse_tensor_to_scipy_sparse(sparse_tensor)[source]

Convert a torch sparse tensor to a scipy sparse matrix.

Parameters

sparse_tensor (torch.Tensor in sparse form) – A tensor stored in sparse form to convert.

Returns

sparse_mx – Sparse matrix.

Return type

scipy.sparse_matrix