code: def head_to_adj(head, directed=True, self_loop=False):
      """
      Convert a sequence of head indexes to an (numpy) adjacency matrix.
      """
      seq_len = len(head)
      head = head[:seq_len]
      root = None
      nodes = [Tree() for _ in head]
  
      for i in range(seq_len):
          h = head[i]
          setattr(nodes[i], "idx", i)
          if h == 0:
              root = nodes[i]
          else:
              nodes[h - 1].add_child(nodes[i])
  
      assert root is not None
  
      ret = np.zeros((seq_len, seq_len), dtype=np.float32)
      queue = [root]
      idx = []
      while len(queue) > 0:
          t, queue = queue[0], queue[1:]
          idx += [t.idx]
          for c in t.children:
              ret[t.idx, c.idx] = 1
          queue += t.children
  
      if not directed:
          ret = ret + ret.T
  
      if self_loop:
          for i in idx:
              ret[i, i] = 1
  
      return ret
  
file_path: DeepKE/src/deepke/attribution_extraction/standard/module/GCN.py
imports: import logging
  import torch
  import numpy
  import torch.nn
  import torch.nn.functional
label_desc: Use image node_id 1 for calling a global function with example usage: head_to_adj(head, directed, _loop) and returns: ret