You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_graphkernels.py 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. """Tests of graph kernels.
  2. """
  3. import pytest
  4. import multiprocessing
  5. def chooseDataset(ds_name):
  6. """Choose dataset according to name.
  7. """
  8. from gklearn.utils.graphfiles import loadDataset
  9. # no node labels (and no edge labels).
  10. if ds_name == 'Alkane':
  11. ds_file = 'datasets/Alkane/dataset.ds'
  12. ds_y = 'datasets/Alkane/dataset_boiling_point_names.txt'
  13. Gn, y = loadDataset(ds_file, filename_y=ds_y)
  14. # node symbolic labels.
  15. elif ds_name == 'Acyclic':
  16. ds_file = 'datasets/acyclic/dataset_bps.ds'
  17. Gn, y = loadDataset(ds_file)
  18. # node non-symbolic labels.
  19. elif ds_name == 'Letter-med':
  20. ds_file = 'datasets/Letter-med/Letter-med_A.txt'
  21. Gn, y = loadDataset(ds_file)
  22. # node symbolic and non-symbolic labels (and edge symbolic labels).
  23. elif ds_name == 'AIDS':
  24. ds_file = 'datasets/AIDS/AIDS_A.txt'
  25. Gn, y = loadDataset(ds_file)
  26. # edge non-symbolic labels (and node non-symbolic labels).
  27. elif ds_name == 'Fingerprint':
  28. ds_file = 'datasets/Fingerprint/Fingerprint_A.txt'
  29. Gn, y = loadDataset(ds_file)
  30. Gn = Gn[0:10]
  31. y = y[0:10]
  32. return Gn, y
  33. @pytest.mark.parametrize('ds_name', ['Alkane', 'AIDS'])
  34. @pytest.mark.parametrize('weight,compute_method', [(0.01, 'geo'), (1, 'exp')])
  35. #@pytest.mark.parametrize('parallel', ['imap_unordered', None])
  36. def test_commonwalkkernel(ds_name, weight, compute_method):
  37. """Test common walk kernel.
  38. """
  39. from gklearn.kernels.commonWalkKernel import commonwalkkernel
  40. Gn, y = chooseDataset(ds_name)
  41. try:
  42. Kmatrix, run_time, idx = commonwalkkernel(Gn,
  43. node_label='atom',
  44. edge_label='bond_type',
  45. weight=weight,
  46. compute_method=compute_method,
  47. # parallel=parallel,
  48. n_jobs=multiprocessing.cpu_count(),
  49. verbose=True)
  50. except Exception as exception:
  51. assert False, exception
  52. @pytest.mark.parametrize('ds_name', ['Alkane', 'AIDS'])
  53. @pytest.mark.parametrize('remove_totters', [True, False])
  54. #@pytest.mark.parametrize('parallel', ['imap_unordered', None])
  55. def test_marginalizedkernel(ds_name, remove_totters):
  56. """Test marginalized kernel.
  57. """
  58. from gklearn.kernels.marginalizedKernel import marginalizedkernel
  59. Gn, y = chooseDataset(ds_name)
  60. try:
  61. Kmatrix, run_time = marginalizedkernel(Gn,
  62. node_label='atom',
  63. edge_label='bond_type',
  64. p_quit=0.5,
  65. n_iteration=2,
  66. remove_totters=remove_totters,
  67. # parallel=parallel,
  68. n_jobs=multiprocessing.cpu_count(),
  69. verbose=True)
  70. except Exception as exception:
  71. assert False, exception
  72. @pytest.mark.parametrize(
  73. 'compute_method,ds_name,sub_kernel',
  74. [
  75. # ('sylvester', 'Alkane', None),
  76. # ('conjugate', 'Alkane', None),
  77. # ('conjugate', 'AIDS', None),
  78. # ('fp', 'Alkane', None),
  79. # ('fp', 'AIDS', None),
  80. ('spectral', 'Alkane', 'exp'),
  81. ('spectral', 'Alkane', 'geo'),
  82. ]
  83. )
  84. #@pytest.mark.parametrize('parallel', ['imap_unordered', None])
  85. def test_randomwalkkernel(ds_name, compute_method, sub_kernel):
  86. """Test random walk kernel kernel.
  87. """
  88. from gklearn.kernels.randomWalkKernel import randomwalkkernel
  89. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  90. import functools
  91. Gn, y = chooseDataset(ds_name)
  92. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  93. sub_kernels = [{'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}]
  94. try:
  95. Kmatrix, run_time, idx = randomwalkkernel(Gn,
  96. compute_method=compute_method,
  97. weight=1e-3,
  98. p=None,
  99. q=None,
  100. edge_weight=None,
  101. node_kernels=sub_kernels,
  102. edge_kernels=sub_kernels,
  103. node_label='atom',
  104. edge_label='bond_type',
  105. sub_kernel=sub_kernel,
  106. # parallel=parallel,
  107. n_jobs=multiprocessing.cpu_count(),
  108. verbose=True)
  109. except Exception as exception:
  110. assert False, exception
  111. @pytest.mark.parametrize('ds_name', ['Alkane', 'Acyclic', 'Letter-med', 'AIDS', 'Fingerprint'])
  112. #@pytest.mark.parametrize('parallel', ['imap_unordered', None])
  113. @pytest.mark.parametrize('parallel', ['imap_unordered'])
  114. def test_spkernel(ds_name, parallel):
  115. """Test shortest path kernel.
  116. """
  117. from gklearn.kernels.spKernel import spkernel
  118. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  119. import functools
  120. Gn, y = chooseDataset(ds_name)
  121. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  122. sub_kernels = {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}
  123. try:
  124. Kmatrix, run_time, idx = spkernel(Gn, node_label='atom',
  125. node_kernels=sub_kernels,
  126. parallel=parallel, n_jobs=multiprocessing.cpu_count(), verbose=True)
  127. except Exception as exception:
  128. assert False, exception
  129. #@pytest.mark.parametrize('ds_name', ['Alkane', 'Acyclic', 'Letter-med', 'AIDS', 'Fingerprint'])
  130. @pytest.mark.parametrize('ds_name', ['Alkane', 'Acyclic', 'Letter-med', 'AIDS'])
  131. @pytest.mark.parametrize('parallel', ['imap_unordered', None])
  132. def test_structuralspkernel(ds_name, parallel):
  133. """Test structural shortest path kernel.
  134. """
  135. from gklearn.kernels.structuralspKernel import structuralspkernel
  136. from gklearn.utils.kernels import deltakernel, gaussiankernel, kernelproduct
  137. import functools
  138. Gn, y = chooseDataset(ds_name)
  139. mixkernel = functools.partial(kernelproduct, deltakernel, gaussiankernel)
  140. sub_kernels = {'symb': deltakernel, 'nsymb': gaussiankernel, 'mix': mixkernel}
  141. try:
  142. Kmatrix, run_time = structuralspkernel(Gn, node_label='atom',
  143. edge_label='bond_type', node_kernels=sub_kernels,
  144. edge_kernels=sub_kernels,
  145. parallel=parallel, n_jobs=multiprocessing.cpu_count(),
  146. verbose=True)
  147. except Exception as exception:
  148. assert False, exception
  149. @pytest.mark.parametrize('ds_name', ['Alkane', 'AIDS'])
  150. @pytest.mark.parametrize('parallel', ['imap_unordered', None])
  151. #@pytest.mark.parametrize('k_func', ['MinMax', 'tanimoto', None])
  152. @pytest.mark.parametrize('k_func', ['MinMax', 'tanimoto'])
  153. @pytest.mark.parametrize('compute_method', ['trie', 'naive'])
  154. def test_untilhpathkernel(ds_name, parallel, k_func, compute_method):
  155. """Test path kernel up to length $h$.
  156. """
  157. from gklearn.kernels.untilHPathKernel import untilhpathkernel
  158. Gn, y = chooseDataset(ds_name)
  159. try:
  160. Kmatrix, run_time = untilhpathkernel(Gn, node_label='atom',
  161. edge_label='bond_type',
  162. depth=2, k_func=k_func, compute_method=compute_method,
  163. parallel=parallel,
  164. n_jobs=multiprocessing.cpu_count(), verbose=True)
  165. except Exception as exception:
  166. assert False, exception
  167. @pytest.mark.parametrize('ds_name', ['Alkane', 'AIDS'])
  168. @pytest.mark.parametrize('parallel', ['imap_unordered', None])
  169. def test_treeletkernel(ds_name, parallel):
  170. """Test treelet kernel.
  171. """
  172. from gklearn.kernels.treeletKernel import treeletkernel
  173. from gklearn.utils.kernels import polynomialkernel
  174. import functools
  175. Gn, y = chooseDataset(ds_name)
  176. pkernel = functools.partial(polynomialkernel, d=2, c=1e5)
  177. try:
  178. Kmatrix, run_time = treeletkernel(Gn,
  179. sub_kernel=pkernel,
  180. node_label='atom',
  181. edge_label='bond_type',
  182. parallel=parallel,
  183. n_jobs=multiprocessing.cpu_count(),
  184. verbose=True)
  185. except Exception as exception:
  186. assert False, exception
  187. @pytest.mark.parametrize('ds_name', ['Acyclic'])
  188. #@pytest.mark.parametrize('base_kernel', ['subtree', 'sp', 'edge'])
  189. @pytest.mark.parametrize('base_kernel', ['subtree'])
  190. @pytest.mark.parametrize('parallel', ['imap_unordered', None])
  191. def test_weisfeilerlehmankernel(ds_name, parallel, base_kernel):
  192. """Test Weisfeiler-Lehman kernel.
  193. """
  194. from gklearn.kernels.weisfeilerLehmanKernel import weisfeilerlehmankernel
  195. Gn, y = chooseDataset(ds_name)
  196. try:
  197. Kmatrix, run_time = weisfeilerlehmankernel(Gn,
  198. node_label='atom',
  199. edge_label='bond_type',
  200. height=2,
  201. base_kernel=base_kernel,
  202. parallel=parallel,
  203. n_jobs=multiprocessing.cpu_count(),
  204. verbose=True)
  205. except Exception as exception:
  206. assert False, exception
  207. if __name__ == "__main__":
  208. test_spkernel()

A Python package for graph kernels, graph edit distances and graph pre-image problem.