IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an email to s dot adaszewski at gmail dot com. User accounts are meant only to report issues and/or generate pull requests. This is a purpose-specific Git hosting for ADARED projects. Thank you for your understanding!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
4.1KB

  1. #
  2. # Copyright (C) Stanislaw Adaszewski, 2020
  3. # License: GPLv3
  4. #
  5. from collections import defaultdict
  6. from .decode import BilinearDecoder
  7. from .weights import init_glorot
  8. class NodeType(object):
  9. def __init__(self, name, count):
  10. self.name = name
  11. self.count = count
  12. class RelationType(object):
  13. def __init__(self, name, node_type_row, node_type_column,
  14. adjacency_matrix):
  15. self.name = name
  16. self.node_type_row = node_type_row
  17. self.node_type_column = node_type_column
  18. self.adjacency_matrix = adjacency_matrix
  19. def get_adjacency_matrix(node_type_row, node_type_column):
  20. if self.node_type_row == node_type_row and \
  21. self.node_type_column == node_type_column:
  22. return self.adjacency_matrix
  23. elif self.node_type_row == node_type_column and \
  24. self.node_type_column == node_type_row:
  25. return self.adjacency_matrix.transpose(0, 1)
  26. else:
  27. raise ValueError('Specified row/column types do not correspond to this relation')
  28. class Data(object):
  29. def __init__(self):
  30. self.node_types = []
  31. self.relation_types = defaultdict(list)
  32. # self.decoder_types = defaultdict(lambda: BilinearDecoder)
  33. # self.latent_node = []
  34. def add_node_type(self, name, count): # , latent_length):
  35. self.node_types.append(NodeType(name, count))
  36. # self.latent_node.append(init_glorot(count, latent_length))
  37. def add_relation_type(self, name, node_type_row, node_type_column, adjacency_matrix):
  38. n = len(self.node_types)
  39. if node_type_row >= n or node_type_column >= n:
  40. raise ValueError('Node type index out of bounds, add node type first')
  41. key = (node_type_row, node_type_column)
  42. if adjacency_matrix is not None and not adjacency_matrix.is_sparse:
  43. adjacency_matrix = adjacency_matrix.to_sparse()
  44. self.relation_types[key].append(RelationType(name, node_type_row, node_type_column, adjacency_matrix))
  45. # _ = self.decoder_types[(node_type_row, node_type_column)]
  46. #def set_decoder_type(self, node_type_row, node_type_column, decoder_class):
  47. # if (node_type_row, node_type_column) not in self.decoder_types:
  48. # raise ValueError('Relation type not found, add relation first')
  49. # self.decoder_types[(node_type_row, node_type_column)] = decoder_class
  50. def get_adjacency_matrices(self, node_type_row, node_type_column):
  51. # rels = list(filter(lambda a: a[0] == node_type_row and a[1] == node_type_column), self.relation_types)
  52. key = (node_type_row, node_type_column)
  53. if key not in self.relation_types:
  54. raise ValueError('Relation type not found')
  55. rels = self.relation_types[key]
  56. rels = list(map(lambda a: a.adjacency_matrix, rels))
  57. return rels
  58. def __repr__(self):
  59. n = len(self.node_types)
  60. if n == 0:
  61. return 'Empty GNN Data'
  62. s = ''
  63. s += 'GNN Data with:\n'
  64. s += '- ' + str(n) + ' node type(s):\n'
  65. for nt in self.node_types:
  66. s += ' - ' + nt.name + '\n'
  67. if len(self.relation_types) == 0:
  68. s += '- No relation types\n'
  69. return s.strip()
  70. n = sum(map(len, self.relation_types))
  71. s += '- ' + str(n) + ' relation type(s):\n'
  72. for i in range(n):
  73. for j in range(n):
  74. key = (i, j)
  75. if key not in self.relation_types:
  76. continue
  77. rels = self.relation_types[key]
  78. # rels = list(filter(lambda a: a[0] == i and a[1] == j, self.relation_types))
  79. #if len(rels) == 0:
  80. # continue
  81. # dir = '<->' if i == j else '->'
  82. dir = '--'
  83. s += ' - ' + self.node_types[i].name + ' ' + dir + ' ' + self.node_types[j].name + ':\n'
  84. #' (' + self.decoder_types[(i, j)].__name__ + '):\n'
  85. for r in rels:
  86. s += ' - ' + r.name + '\n'
  87. return s.strip()