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!
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #
  2. # Copyright (C) Stanislaw Adaszewski, 2020
  3. # License: GPLv3
  4. #
  5. from collections import defaultdict
  6. from dataclasses import dataclass
  7. import torch
  8. @dataclass
  9. class NodeType(object):
  10. name: str
  11. count: int
  12. @dataclass
  13. class RelationType(object):
  14. name: str
  15. node_type_row: int
  16. node_type_column: int
  17. adjacency_matrix: torch.Tensor
  18. is_autogenerated: bool = False
  19. class Data(object):
  20. def __init__(self) -> None:
  21. self.node_types = []
  22. self.relation_types = defaultdict(lambda: defaultdict(list))
  23. def add_node_type(self, name: str, count: int) -> None:
  24. name = str(name)
  25. count = int(count)
  26. if not name:
  27. raise ValueError('You must provide a non-empty node type name')
  28. if count <= 0:
  29. raise ValueError('You must provide a positive node count')
  30. self.node_types.append(NodeType(name, count))
  31. def add_relation_type(self, name: str, node_type_row: int, node_type_column: int,
  32. adjacency_matrix: torch.Tensor, adjacency_matrix_backward: torch.Tensor = None,
  33. two_way: bool = True) -> None:
  34. name = str(name)
  35. node_type_row = int(node_type_row)
  36. node_type_column = int(node_type_column)
  37. if node_type_row < 0 or node_type_row > len(self.node_types):
  38. raise ValueError('node_type_row outside of the valid range of node types')
  39. if node_type_column < 0 or node_type_column > len(self.node_types):
  40. raise ValueError('node_type_column outside of the valid range of node types')
  41. if not isinstance(adjacency_matrix, torch.Tensor):
  42. raise ValueError('adjacency_matrix must be a torch.Tensor')
  43. if adjacency_matrix_backward and not isinstance(adjacency_matrix_backward, torch.Tensor):
  44. raise ValueError('adjacency_matrix_backward must be a torch.Tensor')
  45. if adjacency_matrix.shape != (self.node_types[node_type_row].count,
  46. self.node_types[node_type_column].count):
  47. raise ValueError('adjacency_matrix shape must be (num_row_nodes, num_column_nodes)')
  48. if adjacency_matrix_backward is not None and \
  49. adjacency_matrix_backward.shape != (self.node_types[node_type_column].count,
  50. self.node_types[node_type_row].count):
  51. raise ValueError('adjacency_matrix shape must be (num_column_nodes, num_row_nodes)')
  52. two_way = bool(two_way)
  53. if node_type_row == node_type_column and \
  54. adjacency_matrix_backward is not None:
  55. raise ValueError('Relation between nodes of the same type must be expressed using a single matrix')
  56. self.relation_types[node_type_row][node_type_column].append(
  57. RelationType(name, node_type_row, node_type_column,
  58. adjacency_matrix, False))
  59. if node_type_row != node_type_column and two_way:
  60. if adjacency_matrix_backward is None:
  61. adjacency_matrix_backward = adjacency_matrix.transpose(0, 1)
  62. self.relation_types[node_type_column][node_type_row].append(
  63. RelationType(name, node_type_column, node_type_row,
  64. adjacency_matrix_backward, True))
  65. def __repr__(self):
  66. n = len(self.node_types)
  67. if n == 0:
  68. return 'Empty Icosagon Data'
  69. s = ''
  70. s += 'Icosagon Data with:\n'
  71. s += '- ' + str(n) + ' node type(s):\n'
  72. for nt in self.node_types:
  73. s += ' - ' + nt.name + '\n'
  74. if len(self.relation_types) == 0:
  75. s += '- No relation types\n'
  76. return s.strip()
  77. s_1 = ''
  78. count = 0
  79. for i in range(n):
  80. for j in range(n):
  81. if i not in self.relation_types or \
  82. j not in self.relation_types[i]:
  83. continue
  84. s_1 += ' - ' + self.node_types[i].name + ' -- ' + \
  85. self.node_types[j].name + ':\n'
  86. for r in self.relation_types[i][j]:
  87. if r.is_autogenerated:
  88. continue
  89. s_1 += ' - ' + r.name + '\n'
  90. count += 1
  91. s += '- %d relation type(s):\n' % count
  92. s += s_1
  93. return s.strip()
  94. # n = sum(map(len, self.relation_types))
  95. #
  96. # for i in range(n):
  97. # for j in range(n):
  98. # key = (i, j)
  99. # if key not in self.relation_types:
  100. # continue
  101. # rels = self.relation_types[key]
  102. #
  103. # for r in rels:
  104. #
  105. # return s.strip()