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.

random_walk.py 2.8 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Wed Aug 19 16:55:17 2020
  5. @author: ljia
  6. @references:
  7. [1] S Vichy N Vishwanathan, Nicol N Schraudolph, Risi Kondor, and Karsten M Borgwardt. Graph kernels. Journal of Machine Learning Research, 11(Apr):1201–1242, 2010.
  8. """
  9. import sys
  10. from tqdm import tqdm
  11. import numpy as np
  12. import networkx as nx
  13. from gklearn.utils import SpecialLabel
  14. from gklearn.utils.parallel import parallel_gm, parallel_me
  15. from gklearn.utils.utils import direct_product_graph
  16. from gklearn.kernels import GraphKernel
  17. class RandomWalk(GraphKernel):
  18. def __init__(self, **kwargs):
  19. GraphKernel.__init__(self)
  20. self._compute_method = kwargs.get('compute_method', None)
  21. self._weight = kwargs.get('weight', 1)
  22. self._p = kwargs.get('p', None)
  23. self._q = kwargs.get('q', None)
  24. self._edge_weight = kwargs.get('edge_weight', None)
  25. self._ds_infos = kwargs.get('ds_infos', {})
  26. self._compute_method = self.__compute_method.lower()
  27. def _compute_gm_series(self):
  28. pass
  29. def _compute_gm_imap_unordered(self):
  30. pass
  31. def _compute_kernel_list_series(self, g1, g_list):
  32. pass
  33. def _compute_kernel_list_imap_unordered(self, g1, g_list):
  34. pass
  35. def _compute_single_kernel_series(self, g1, g2):
  36. pass
  37. def _check_graphs(self, Gn):
  38. # remove graphs with no edges, as no walk can be found in their structures,
  39. # so the weight matrix between such a graph and itself might be zero.
  40. for g in Gn:
  41. if nx.number_of_edges(g) == 0:
  42. raise Exception('Graphs must contain edges to construct weight matrices.')
  43. def _check_edge_weight(self, G0, verbose):
  44. eweight = None
  45. if self._edge_weight == None:
  46. if verbose >= 2:
  47. print('\n None edge weight is specified. Set all weight to 1.\n')
  48. else:
  49. try:
  50. some_weight = list(nx.get_edge_attributes(G0, self._edge_weight).values())[0]
  51. if isinstance(some_weight, float) or isinstance(some_weight, int):
  52. eweight = self._edge_weight
  53. else:
  54. if verbose >= 2:
  55. print('\n Edge weight with name %s is not float or integer. Set all weight to 1.\n' % self._edge_weight)
  56. except:
  57. if verbose >= 2:
  58. print('\n Edge weight with name "%s" is not found in the edge attributes. Set all weight to 1.\n' % self._edge_weight)
  59. self._edge_weight = eweight
  60. def _add_dummy_labels(self, Gn):
  61. if len(self.__node_labels) == 0 or (len(self.__node_labels) == 1 and self.__node_labels[0] == SpecialLabel.DUMMY):
  62. for i in range(len(Gn)):
  63. nx.set_node_attributes(Gn[i], '0', SpecialLabel.DUMMY)
  64. self.__node_labels = [SpecialLabel.DUMMY]
  65. if len(self.__edge_labels) == 0 or (len(self.__edge_labels) == 1 and self.__edge_labels[0] == SpecialLabel.DUMMY):
  66. for i in range(len(Gn)):
  67. nx.set_edge_attributes(Gn[i], '0', SpecialLabel.DUMMY)
  68. self.__edge_labels = [SpecialLabel.DUMMY]

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