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_spkernel.ipynb 38 kB

7 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 2,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import numpy"
  10. ]
  11. },
  12. {
  13. "cell_type": "code",
  14. "execution_count": 8,
  15. "metadata": {},
  16. "outputs": [],
  17. "source": [
  18. "# Author: Elisabetta Ghisu\n",
  19. "\n",
  20. "\"\"\"\n",
  21. "- Script containing functions for computing the shortest path kernel\n",
  22. "- The Floyd Warshall algorithm is first implemented\n",
  23. "- Then the SP is calculated\n",
  24. "\"\"\"\n",
  25. "\n",
  26. "\n",
  27. "#######################\n",
  28. "# - IMPORT PACKAGES - #\n",
  29. "#######################\n",
  30. "\n",
  31. "\n",
  32. "\n",
  33. "import numpy.matlib as matlib\n",
  34. "import numpy as np\n",
  35. "\n",
  36. "\"\"\"\n",
  37. "### FLOYD WARSHALL ALGORITHM\n",
  38. "Input:\n",
  39. "- Adjancency matrix A\n",
  40. "Output:\n",
  41. "- Shortest path matrix S\n",
  42. "\"\"\"\n",
  43. "\n",
  44. "def floyd_warshall(A):\n",
  45. "\n",
  46. "\t# nuber of nodes\n",
  47. "\tn = A.shape[0]\n",
  48. "\n",
  49. "\t# initialize shortes path matrix\n",
  50. "\tS = np.zeros(shape = (n,n))\n",
  51. "\n",
  52. "\tfor i in range(n):\n",
  53. "\t\tfor j in range(n):\n",
  54. "\t\t\tif A[i,j] == 0 and i!=j:\n",
  55. "\t\t\t\tS[i,j] = float(\"inf\")\n",
  56. "\t\t\telse:\n",
  57. "\t\t\t\tS[i,j] = A[i,j]\n",
  58. "\n",
  59. "\t# Compute the shortest path matrix\n",
  60. "\tfor k in range(n):\n",
  61. "\t\tfor i in range(n):\n",
  62. "\t\t\tfor j in range(n):\n",
  63. "\t\t\t\tif S[i,j] > S[i,k] + S[k,j]:\n",
  64. "\t\t\t\t\tS[i,j] = S[i,k] + S[k,j]\n",
  65. "\n",
  66. "\treturn S\t\t\t\t\t\t\t\t\n",
  67. "\n",
  68. "\n",
  69. "\n",
  70. "\"\"\"\n",
  71. "SHORTEST PATH KERNEL: This is a fast implementation of the shortest path\n",
  72. "kernel algorithm\n",
  73. "Inputs\n",
  74. "- Adjancency matrix\n",
  75. "- List of list of node labels for each graph\n",
  76. "- Total number of node labels \n",
  77. "Outputs\n",
  78. "- Kernel matrix\n",
  79. "- Feature matrix\n",
  80. "\"\"\"\n",
  81. "\n",
  82. "def sp_kernel_fast(adj_mat, labels, L):\n",
  83. "\n",
  84. "\t# Number of graphs\n",
  85. "\tn = len(adj_mat)\n",
  86. "\tL = int(L)\n",
  87. "\tS = []\n",
  88. "\n",
  89. "\t# shortest path matrices\n",
  90. "\tfor i in xrange(n):\n",
  91. "\t\tif i%1000 == 0 and i !=0:\n",
  92. " \t\t\tprint('haha') #( \"%d\" % i)\n",
  93. "\t\tS.append(floyd_warshall(adj_mat[i]))\n",
  94. "\t\n",
  95. "\t# maximum length of shortest paths in the dataset\n",
  96. "\tmax_path = 0\n",
  97. "\n",
  98. "\t# for each graph in dataset\n",
  99. "\tfor i in xrange(n):\n",
  100. "\n",
  101. "\t\tS_cur = np.copy(S[i])\n",
  102. "\t\tS_cur[S_cur == np.inf] = 0\n",
  103. "\t\tnew_max = np.max(S_cur)\n",
  104. "\t\t\n",
  105. "\t\tif new_max > max_path:\n",
  106. "\t\t\tmax_path = new_max # get max short path in all Ss\n",
  107. "\n",
  108. "\t# maximum length of shortest paths\n",
  109. "\tmax_path = int(max_path)\n",
  110. "\n",
  111. "\t# initialize feature matrix\n",
  112. "\tsp = np.zeros(((max_path + 1) * L * (L+1) /2,n))\n",
  113. "\n",
  114. "\t# compute feature map for shortest path\n",
  115. "\tfor i in xrange(n):\n",
  116. "\n",
  117. "\t\tif i % 1000 == 0:\n",
  118. "\t\t\tprint('haha') #\"Processed %d graphs\" %i\n",
  119. "\n",
  120. "\t\tS_graph = S[i]\n",
  121. "\t\tlabels_graph = np.asarray(labels[i].reshape((len(labels[i]),1)))\n",
  122. "\t\tlabels_graph = labels_graph + 1\n",
  123. "\t\t\n",
  124. "\t\tlabels_aux = matlib.repmat(labels_graph, 1, len(labels_graph))\n",
  125. "\t\t\n",
  126. "\t\tmin_lab = np.minimum(labels_aux, labels_aux.T)\n",
  127. "\t\t\n",
  128. "\t\tmax_lab = np.maximum(labels_aux, labels_aux.T)\n",
  129. "\t\tsub_path = np.triu(~(np.isinf(S_graph))).T\n",
  130. "\n",
  131. "\t\tmin_lab = min_lab[sub_path]\n",
  132. "\t\tmax_lab = max_lab[sub_path]\n",
  133. "\n",
  134. "\n",
  135. "\t\tind = S_graph[sub_path] * L * (L + 1) / 2 + (min_lab - 1) * (2*L + 2 - min_lab) / 2 + max_lab - min_lab\n",
  136. "\t\tind = ind.astype(int)\n",
  137. "\t\taccum = np.zeros((max_path + 1) * L * (L + 1) /2)\n",
  138. "\t\taccum[:ind.max() + 1] += np.bincount(ind.astype(int))\n",
  139. "\t\tsp[ind,i] = accum[ind]\n",
  140. "\t\n",
  141. "\tsum_cols = np.sum(sp, axis = 1)\n",
  142. "\tind_true = sum_cols != 0\n",
  143. "\tsp = sp[ind_true,:]\n",
  144. "\t\n",
  145. "\t# compute kernel matrix\n",
  146. "\tK = np.dot(sp.T,sp)\n",
  147. " \n",
  148. "\treturn K, sp"
  149. ]
  150. },
  151. {
  152. "cell_type": "code",
  153. "execution_count": 11,
  154. "metadata": {},
  155. "outputs": [
  156. {
  157. "ename": "ImportError",
  158. "evalue": "No module named 'igraph'",
  159. "output_type": "error",
  160. "traceback": [
  161. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  162. "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)",
  163. "\u001b[0;32m<ipython-input-11-effbaf3a1e10>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0;31m# iGraph imports to handle graphs and for graph I/O\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0migraph\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mGraph\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 18\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
  164. "\u001b[0;31mImportError\u001b[0m: No module named 'igraph'"
  165. ]
  166. }
  167. ],
  168. "source": [
  169. "#Authors: Elisabetta Ghisu, Felipe Llinares Lopez\n",
  170. "\n",
  171. "\"\"\"\n",
  172. "- This script includes a list of functions for analyzing \n",
  173. "parsing and formatting graphs\n",
  174. "- The graphs are given in graphml format\n",
  175. "- It also cntans functions for loading, processing the graphs\n",
  176. "and extract graph statistics\n",
  177. "\"\"\"\n",
  178. "\n",
  179. "\n",
  180. "import numpy as np\n",
  181. "from numpy import genfromtxt\n",
  182. "\n",
  183. "# iGraph imports to handle graphs and for graph I/O\n",
  184. "from igraph import Graph\n",
  185. "\n",
  186. "\n",
  187. "# ---------------------------------GRAPHML I/O FUNCTIONS------------------------------------ #\n",
  188. "\n",
  189. "# INPUT:\n",
  190. "# filenames_graphs: list of GraphML files, where each file contains one graph in the dataset\n",
  191. "# filename_labels: text file with labels corresponding to each graph in the dataset, in the same order as they are in\n",
  192. "# filename_graphs\n",
  193. "# OUTPUT:\n",
  194. "# G: A list containing one iGraph object for each graph in the dataset\n",
  195. "# Y: A Numpy array containing the labels corresponding to each graph, in the same order as G\n",
  196. "def load_graphml(filenames_graphs, filename_labels):\n",
  197. " G = []\n",
  198. " for fname in filenames_graphs:\n",
  199. " G.append(Graph.Read_GraphML(fname))\n",
  200. " Y = genfromtxt(filename_labels)\n",
  201. " return (G, Y)\n",
  202. "\n",
  203. "\n",
  204. "# Loads a list of paths to GraphML files from filename_list\n",
  205. "def load_file_list(filename_flist):\n",
  206. " f = open(filename_flist, 'r')\n",
  207. " f_graphs = []\n",
  208. " for line in f:\n",
  209. " f_graphs.append(line.strip())\n",
  210. " f.close()\n",
  211. " return f_graphs\n",
  212. "\n",
  213. "\n",
  214. "# --------------------------------COMPUTE STATISTICS---------------------------------------- #\n",
  215. "\n",
  216. "\n",
  217. "# Retrieve labels of all vertices belonging to any graph in the list of iGraph objects G and\n",
  218. "# returns the entire list, and a list with the alphabet of the vertex labels\n",
  219. "def get_all_vertex_labels(G, att_name='label'):\n",
  220. " v_l = []\n",
  221. " for g in G:\n",
  222. " v_l += g.vs[att_name]\n",
  223. " return (v_l, np.unique(v_l))\n",
  224. "\n",
  225. "\n",
  226. "# Retrieve labels of all edges belonging to any graph in the list of iGraph objects G and\n",
  227. "# returns the entire list, and a list with the alphabet of the edge labels\n",
  228. "def get_all_edge_labels(G, att_name='label'):\n",
  229. " e_l = []\n",
  230. " for g in G:\n",
  231. " e_l += g.es[att_name]\n",
  232. " return (e_l, np.unique(e_l))\n",
  233. "\n",
  234. "\n",
  235. "# Returns a list where each element is itself the adjacency list of the corresponding graph\n",
  236. "# The adjacency lit of a graph has the following format:\n",
  237. "# it is a list where each element is a list containing the id of adjacent nodes\n",
  238. "def get_adj_list(G):\n",
  239. " ad_l = []\n",
  240. " for g in G:\n",
  241. " ad_l.append(g.get_adjlist())\n",
  242. " return ad_l\n",
  243. "\n",
  244. "# Returns a list where each element is the adjacency matrix of the graph \n",
  245. "# The adjancency matrix is in iGraph format\n",
  246. "def get_adj_mat(G):\n",
  247. " ad_m = []\n",
  248. " for g in G:\n",
  249. " ad_m.append(g.get_adjacency())\n",
  250. " return ad_m\n",
  251. "\n",
  252. "# Returns a list where each element contains the nodes label for a graph\n",
  253. "def get_node_labels(G, att_name = 'label'):\n",
  254. " node_l = []\n",
  255. " for g in G:\n",
  256. " node_l.append(g.vs[att_name])\n",
  257. " return node_l\n",
  258. "\n",
  259. "\n",
  260. "\n",
  261. "# ----------------- LOAD AND PROCESS THE GRAPHS --------------- #\n",
  262. "\n",
  263. "\n",
  264. "\"\"\"\n",
  265. "Inputs:\n",
  266. "- list of graphs file\n",
  267. "- labels file\n",
  268. "- path to the data folder\n",
  269. "Outputs:\n",
  270. "- List of node labels\n",
  271. "- List of adjancency lists\n",
  272. "- List of graphs in graphml format\n",
  273. "- Targets\n",
  274. "- number of classes\n",
  275. "- sample size\n",
  276. "\"\"\"\n",
  277. "\n",
  278. "\n",
  279. "def load_and_process(filenames_graphs, filename_labels, path_to_dataset):\n",
  280. "\n",
  281. " # load a list of names to graphml files\n",
  282. " f_graphs = load_file_list(filenames_graphs)\n",
  283. " # sample size\n",
  284. " n = len(f_graphs)\n",
  285. "\n",
  286. " # create a list of paths to the files\n",
  287. " f_graphs_path =[]\n",
  288. "\n",
  289. " # for each graph in dataset\n",
  290. " for i in range(n):\n",
  291. "\n",
  292. " # index the graph\n",
  293. " graph_name = f_graphs[i]\n",
  294. "\n",
  295. " # path to the data folder\n",
  296. " path = \"%s/%s\" % (path_to_dataset, graph_name)\n",
  297. " f_graphs_path.append(path)\n",
  298. "\n",
  299. " # If the data is DD have to delete an element (corrupted file)\n",
  300. " if graph_name == \"DD\":\n",
  301. " del f_graphs_path[148]\n",
  302. " n = n-1\n",
  303. "\n",
  304. " # Load the graphs in graphml format\n",
  305. " # G is a llist of graphml graph\n",
  306. " # Y is an array of targets\n",
  307. " G,Y = load_graphml(f_graphs_path, filename_labels)\n",
  308. "\n",
  309. " # Delete corrupted file in DD\n",
  310. " if graph_name == \"DD\": \n",
  311. " Y = np.delete(Y, 148)\n",
  312. "\n",
  313. " # get adjacency list and matrix for all the graphs in G\n",
  314. " ad_list = get_adj_list(G)\n",
  315. " ad_mat = get_adj_mat(G)\n",
  316. "\n",
  317. " # get a list containing lists of node labels\n",
  318. " node_label = get_node_labels(G)\n",
  319. "\n",
  320. " return node_label, ad_list, G, Y\n",
  321. "\n",
  322. "\n",
  323. "\n",
  324. "\"\"\"\n",
  325. "RENAME NODES: function to rename nodes from 0,...,num_nodes\n",
  326. "Input\n",
  327. "- list of list of node labels in each graph\n",
  328. "Output\n",
  329. "- L: total number of different labels in the dataset\n",
  330. "- node_label: new renamed labels\n",
  331. "\"\"\"\n",
  332. "\n",
  333. "def rename_nodes(node_label): \n",
  334. " \n",
  335. " # number of graphs in the dataset\n",
  336. " n = len(node_label)\n",
  337. "\n",
  338. " # labels will store the new labels\n",
  339. " labels = [0] * n\n",
  340. "\n",
  341. " # disctionary containing the map from the old to the new labels\n",
  342. " label_lookup = {}\n",
  343. "\n",
  344. " # counter of unique labels\n",
  345. " label_counter = 0\n",
  346. "\n",
  347. " # for each graph in dataset\n",
  348. " for i in range(n):\n",
  349. "\n",
  350. "\n",
  351. " # number of nodes in graph[i]\n",
  352. " num_nodes = len(node_label[i]) \n",
  353. "\n",
  354. " # will be used to store the new labels\n",
  355. " labels[i] = np.zeros(num_nodes, dtype = np.uint64) # positive integers\n",
  356. "\n",
  357. " # for each node in the graph\n",
  358. " for j in range(num_nodes):\n",
  359. "\n",
  360. " # the node label to a string\n",
  361. " l_node_str = str(np.copy(node_label[i][j]))\n",
  362. " \n",
  363. " # if the string has not been observed yet\n",
  364. " # the corresponding node is assigned a new label\n",
  365. " # otherwise it will be named with the same label\n",
  366. " # already assigned to an identical string\n",
  367. "\n",
  368. " if not label_lookup.has_key(l_node_str):\n",
  369. " label_lookup[l_node_str] = label_counter\n",
  370. " labels[i][j] = label_counter \n",
  371. " label_counter += 1\n",
  372. " else:\n",
  373. " labels[i][j] = label_lookup[l_node_str]\n",
  374. "\n",
  375. " # total number of labels in the dataset\n",
  376. " L = label_counter\n",
  377. " print('haha') #'Number of original labels %d' % L \n",
  378. "\n",
  379. " return L, labels"
  380. ]
  381. },
  382. {
  383. "cell_type": "code",
  384. "execution_count": 13,
  385. "metadata": {},
  386. "outputs": [
  387. {
  388. "name": "stderr",
  389. "output_type": "stream",
  390. "text": [
  391. "usage: ipykernel_launcher.py [-h] --dataset DATASET\n",
  392. "ipykernel_launcher.py: error: the following arguments are required: --dataset\n"
  393. ]
  394. },
  395. {
  396. "ename": "SystemExit",
  397. "evalue": "2",
  398. "output_type": "error",
  399. "traceback": [
  400. "An exception has occurred, use %tb to see the full traceback.\n",
  401. "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 2\n"
  402. ]
  403. },
  404. {
  405. "name": "stderr",
  406. "output_type": "stream",
  407. "text": [
  408. "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py:2918: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
  409. " warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
  410. ]
  411. }
  412. ],
  413. "source": [
  414. "# Author: Elisabetta Ghisu\n",
  415. "\n",
  416. "\"\"\"\n",
  417. "- Script for computing the kernel matrix and features map \n",
  418. "using shortest path kernel\n",
  419. "\"\"\"\n",
  420. "\n",
  421. "###########################\n",
  422. "# --- IMPORT PACKAGES --- #\n",
  423. "###########################\n",
  424. "\n",
  425. "import numpy as np\n",
  426. "import argparse\n",
  427. "import os\n",
  428. "import pickle\n",
  429. "\n",
  430. "from numpy import genfromtxt\n",
  431. "\n",
  432. "# from sp_functions import *\n",
  433. "# from parse_graphs import *\n",
  434. "\n",
  435. "\n",
  436. "\n",
  437. "##############################\n",
  438. "### Command Line Arguments ###\n",
  439. "##############################\n",
  440. "\n",
  441. "parser = argparse.ArgumentParser(description = \"Compute kernel and features matrices via shortest path kernel\")\n",
  442. "parser.add_argument(\"--dataset\", required = True, help = \"Name of the dataset\")\n",
  443. "args = parser.parse_args()\n",
  444. "\n",
  445. "\n",
  446. "#####################\n",
  447. "### LOAD THE DATA ###\n",
  448. "#####################\n",
  449. "\n",
  450. "\"\"\"\n",
  451. "- Here we load the data input and targets\n",
  452. "- The data are assumed to be in graph formats\n",
  453. "- They should be in graphml format \n",
  454. "\"\"\"\n",
  455. "\n",
  456. "# path to the list of graphs and dataset\n",
  457. "filenames_graphs = \"data/%s.list\" % (args.dataset)\n",
  458. "path_to_dataset = \"data/%s\" % (args.dataset) \n",
  459. "\n",
  460. "# Load the targets\n",
  461. "filename_labels = \"data/%s_label.txt\" % (args.dataset)\n",
  462. "\n",
  463. "# load and process graphs\n",
  464. "node_label, ad_list, G, Y = load_and_process(filenames_graphs, filename_labels, path_to_dataset)\n",
  465. "\n",
  466. "# output directory\n",
  467. "out_path = \"kernel_matrices/%s/sp\" % args.dataset\n",
  468. "\n",
  469. "# If the output directory does not exist, then create it\n",
  470. "if not os.path.exists(out_path):\n",
  471. " os.makedirs(out_path)\n",
  472. "\n",
  473. "\n",
  474. "#########################\n",
  475. "# --- SHORTEST PATH --- #\n",
  476. "#########################\n",
  477. "\n",
  478. "\n",
  479. "# assign labels starting from zero to the nodes\n",
  480. "L, labels = rename_nodes(node_label)\n",
  481. "\n",
  482. "\n",
  483. "# Compute adjancency matrix \n",
  484. "adj_mat = get_adj_mat(G)\n",
  485. "\n",
  486. "# Compute kernel and feature maps using shortest path\n",
  487. "K, phi = sp_kernel_fast(adj_mat, labels, L)\n",
  488. "\n",
  489. "# save kernel matrix\n",
  490. "file_name = \"%s/%s_ker_mat\" % (out_path, args.dataset)\n",
  491. "np.save(file_name, K)\n",
  492. "\n",
  493. "# save feature map\n",
  494. "file_name = \"%s/%s_phi_map\" % (out_path, args.dataset)\n",
  495. "np.save(file_name, phi)"
  496. ]
  497. },
  498. {
  499. "cell_type": "code",
  500. "execution_count": 17,
  501. "metadata": {},
  502. "outputs": [
  503. {
  504. "name": "stdout",
  505. "output_type": "stream",
  506. "text": [
  507. "[[ 0. 2. 3. 1. 2.]]\n",
  508. "{0: {0: [0], 1: [0, 3, 1], 2: [0, 3, 4, 2], 3: [0, 3], 4: [0, 3, 4]}, 1: {0: [1, 3, 0], 1: [1], 2: [1, 3, 4, 2], 3: [1, 3], 4: [1, 3, 4]}, 2: {0: [2, 4, 3, 0], 1: [2, 4, 3, 1], 2: [2], 3: [2, 4, 3], 4: [2, 4]}, 3: {0: [3, 0], 1: [3, 1], 2: [3, 4, 2], 3: [3], 4: [3, 4]}, 4: {0: [4, 3, 0], 1: [4, 3, 1], 2: [4, 2], 3: [4, 3], 4: [4]}}\n",
  509. "[[ 0. 2. 3. 1. 2.]\n",
  510. " [ 2. 0. 3. 1. 2.]\n",
  511. " [ 3. 3. 0. 2. 1.]\n",
  512. " [ 1. 1. 2. 0. 1.]\n",
  513. " [ 2. 2. 1. 1. 0.]]\n"
  514. ]
  515. },
  516. {
  517. "ename": "NameError",
  518. "evalue": "name 'plt' is not defined",
  519. "output_type": "error",
  520. "traceback": [
  521. "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
  522. "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
  523. "\u001b[0;32m<ipython-input-17-c1e1e7524d30>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 11\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 12\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ml2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 13\u001b[0;31m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 15\u001b[0m \u001b[0mS\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetSPGraph\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mG1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
  524. "\u001b[0;31mNameError\u001b[0m: name 'plt' is not defined"
  525. ]
  526. }
  527. ],
  528. "source": [
  529. "dataset, y = loadDataset(\"/home/ljia/Documents/research-repo/datasets/acyclic/Acyclic/dataset_bps.ds\")\n",
  530. "G1 = dataset[12]\n",
  531. "\n",
  532. "nx.draw_networkx(G1)\n",
  533. "# print(list(dataset[12][4]))\n",
  534. "\n",
  535. "l = nx.shortest_path(G1)\n",
  536. "\n",
  537. "l2 = nx.floyd_warshall_numpy(G1)\n",
  538. "print(np.array(l2[0]))\n",
  539. "print(l)\n",
  540. "print(l2)\n",
  541. "plt.show()\n",
  542. "\n",
  543. "S = getSPGraph(G1)\n",
  544. "nx.draw_networkx(S)\n",
  545. "pos = nx.spring_layout(S)\n",
  546. "edge_labels = nx.get_edge_attributes(S,'cost')\n",
  547. "print(edge_labels)\n",
  548. "nx.draw_networkx_edge_labels(S, pos, edge_labels = edge_labels)\n",
  549. "plt.show()"
  550. ]
  551. },
  552. {
  553. "cell_type": "code",
  554. "execution_count": 18,
  555. "metadata": {},
  556. "outputs": [],
  557. "source": [
  558. "import networkx as nx\n",
  559. " \n",
  560. "def loadCT(filename):\n",
  561. " \"\"\"load data from .ct file.\n",
  562. " \n",
  563. " Notes\n",
  564. " ------ \n",
  565. " a typical example of data in .ct is like this:\n",
  566. " \n",
  567. " 3 2 <- number of nodes and edges\n",
  568. " 0.0000 0.0000 0.0000 C <- each line describes a node, the last parameter in which is the label of the node, representing a chemical element @Q what are the first 3 numbers?\n",
  569. " 0.0000 0.0000 0.0000 C\n",
  570. " 0.0000 0.0000 0.0000 O\n",
  571. " 1 3 1 1 <- each line describes an edge, the first two numbers represent two nodes of the edge, the last number represents the label. @Q what are the 3th numbers?\n",
  572. " 2 3 1 1\n",
  573. " \"\"\"\n",
  574. " content = open(filename).read().splitlines()\n",
  575. " G = nx.Graph(name=str(content[0])) # set name of the graph\n",
  576. " tmp = content[1].split(\" \")\n",
  577. " if tmp[0] == '':\n",
  578. " nb_nodes = int(tmp[1]) # number of the nodes\n",
  579. " nb_edges = int(tmp[2]) # number of the edges\n",
  580. " else:\n",
  581. " nb_nodes = int(tmp[0])\n",
  582. " nb_edges = int(tmp[1])\n",
  583. "\n",
  584. " for i in range(0, nb_nodes):\n",
  585. " tmp = content[i + 2].split(\" \")\n",
  586. " tmp = [x for x in tmp if x != '']\n",
  587. " G.add_node(i, label=tmp[3])\n",
  588. "\n",
  589. " for i in range(0, nb_edges):\n",
  590. " tmp = content[i + G.number_of_nodes() + 2].split(\" \")\n",
  591. " tmp = [x for x in tmp if x != '']\n",
  592. " G.add_edge(int(tmp[0]) - 1, int(tmp[1]) - 1, label=int(tmp[3]))\n",
  593. " return G\n",
  594. "\n",
  595. "\n",
  596. "def loadGXL(filename):\n",
  597. " import networkx as nx\n",
  598. " import xml.etree.ElementTree as ET\n",
  599. "\n",
  600. " tree = ET.parse(filename)\n",
  601. " root = tree.getroot()\n",
  602. " index = 0\n",
  603. " G = nx.Graph()\n",
  604. " dic={}\n",
  605. " for node in root.iter('node'):\n",
  606. " label = node.find('attr')[0].text\n",
  607. " dic[node.attrib['id']] = index\n",
  608. " G.add_node(index, id=node.attrib['id'], label=label)\n",
  609. " index += 1\n",
  610. " \n",
  611. " for edge in root.iter('edge'):\n",
  612. " label = edge.find('attr')[0].text\n",
  613. " G.add_edge(dic[edge.attrib['from']], dic[edge.attrib['to']], label=label)\n",
  614. " return G\n",
  615. " \n",
  616. "def loadDataset(filename):\n",
  617. " \"\"\"load file list of the dataset.\n",
  618. " \"\"\"\n",
  619. " from os.path import dirname, splitext\n",
  620. "\n",
  621. " dirname_dataset = dirname(filename)\n",
  622. " extension = splitext(filename)[1][1:]\n",
  623. " data = []\n",
  624. " y = []\n",
  625. " if(extension == \"ds\"):\n",
  626. " content = open(filename).read().splitlines()\n",
  627. " for i in range(0, len(content)):\n",
  628. " tmp = content[i].split(' ')\n",
  629. " data.append(loadCT(dirname_dataset + '/' + tmp[0].replace('#', '', 1))) # remove the '#'s in file names\n",
  630. " y.append(float(tmp[1]))\n",
  631. " elif(extension == \"cxl\"):\n",
  632. " import xml.etree.ElementTree as ET\n",
  633. "\n",
  634. " tree = ET.parse(filename)\n",
  635. " root = tree.getroot()\n",
  636. " data = []\n",
  637. " y = []\n",
  638. " for graph in root.iter('print'):\n",
  639. " mol_filename = graph.attrib['file']\n",
  640. " mol_class = graph.attrib['class']\n",
  641. " data.append(loadGXL(dirname_dataset + '/' + mol_filename))\n",
  642. " y.append(mol_class)\n",
  643. "\n",
  644. " return data, y"
  645. ]
  646. },
  647. {
  648. "cell_type": "code",
  649. "execution_count": 82,
  650. "metadata": {},
  651. "outputs": [
  652. {
  653. "ename": "SyntaxError",
  654. "evalue": "invalid syntax (<ipython-input-82-ac9ab22d42ef>, line 48)",
  655. "output_type": "error",
  656. "traceback": [
  657. "\u001b[0;36m File \u001b[0;32m\"<ipython-input-82-ac9ab22d42ef>\"\u001b[0;36m, line \u001b[0;32m48\u001b[0m\n\u001b[0;31m Kmatrix[j][i] += (i == j ? 0 : 1)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
  658. ]
  659. }
  660. ],
  661. "source": [
  662. "import sys\n",
  663. "import pathlib\n",
  664. "sys.path.insert(0, \"../\")\n",
  665. "\n",
  666. "\n",
  667. "import networkx as nx\n",
  668. "import numpy as np\n",
  669. "import time\n",
  670. "\n",
  671. "from utils.utils import getSPGraph\n",
  672. "\n",
  673. "\n",
  674. "def spkernel(Gn):\n",
  675. " \"\"\"Transform graph G to its corresponding shortest-paths graph using Floyd-transformation.\n",
  676. " \n",
  677. " Parameters\n",
  678. " ----------\n",
  679. " G : NetworkX graph\n",
  680. " The graph to be tramsformed.\n",
  681. " \n",
  682. " Return\n",
  683. " ------\n",
  684. " S : NetworkX graph\n",
  685. " The shortest-paths graph corresponding to G.\n",
  686. " \n",
  687. " References\n",
  688. " ----------\n",
  689. " [1] Borgwardt KM, Kriegel HP. Shortest-path kernels on graphs. InData Mining, Fifth IEEE International Conference on 2005 Nov 27 (pp. 8-pp). IEEE.\n",
  690. " \"\"\"\n",
  691. " Kmatrix = np.zeros((len(Gn), len(Gn)))\n",
  692. " \n",
  693. " Sn = [] # get shortest path graphs of Gn\n",
  694. " for i in range(0, len(Gn)):\n",
  695. " Sn.append(getSPGraph(Gn[i]))\n",
  696. " \n",
  697. "# print(S1.nodes(data = True))\n",
  698. "# print(S2.nodes(data = True))\n",
  699. "# print(S1.edges(data = True))\n",
  700. "# print(S2.edges(data = True))\n",
  701. " \n",
  702. " start_time = time.time()\n",
  703. " for i in range(0, len(Gn)):\n",
  704. " for j in range(i, len(Gn)):\n",
  705. " for e1 in Sn[i].edges(data = True):\n",
  706. " for e2 in Sn[j].edges(data = True): \n",
  707. " if e1[2]['cost'] != 0 and e1[2]['cost'] == e2[2]['cost'] and ((e1[0] == e2[0] and e1[1] == e2[1]) or (e1[0] == e2[1] and e1[1] == e2[0])):\n",
  708. " Kmatrix[i][j] += 1\n",
  709. " Kmatrix[j][i] += (i == j ? 0 : 1)\n",
  710. " \n",
  711. " print(\"--- %s seconds ---\" % (time.time() - start_time))\n",
  712. " \n",
  713. " return Kmatrix"
  714. ]
  715. },
  716. {
  717. "cell_type": "code",
  718. "execution_count": 83,
  719. "metadata": {},
  720. "outputs": [
  721. {
  722. "name": "stdout",
  723. "output_type": "stream",
  724. "text": [
  725. "0\n",
  726. "--- 0.05678129196166992 seconds ---\n",
  727. "1\n",
  728. "--- 0.15176129341125488 seconds ---\n",
  729. "2\n",
  730. "--- 0.20930719375610352 seconds ---\n",
  731. "3\n",
  732. "--- 0.3049781322479248 seconds ---\n",
  733. "4\n",
  734. "--- 0.4029049873352051 seconds ---\n",
  735. "5\n",
  736. "--- 0.5458371639251709 seconds ---\n",
  737. "6\n",
  738. "--- 0.6920650005340576 seconds ---\n",
  739. "7\n",
  740. "--- 0.7972092628479004 seconds ---\n",
  741. "8\n",
  742. "--- 0.947425365447998 seconds ---\n",
  743. "9\n",
  744. "--- 1.1016933917999268 seconds ---\n",
  745. "10\n",
  746. "--- 1.2554333209991455 seconds ---\n",
  747. "11\n",
  748. "--- 1.4140815734863281 seconds ---\n",
  749. "12\n",
  750. "--- 1.562861442565918 seconds ---\n",
  751. "13\n",
  752. "--- 1.7876057624816895 seconds ---\n",
  753. "14\n",
  754. "--- 1.9889881610870361 seconds ---\n",
  755. "15\n",
  756. "--- 2.2633984088897705 seconds ---\n",
  757. "16\n",
  758. "--- 2.480710983276367 seconds ---\n",
  759. "17\n",
  760. "--- 2.683915138244629 seconds ---\n",
  761. "18\n",
  762. "--- 2.8276052474975586 seconds ---\n",
  763. "19\n",
  764. "--- 2.972059488296509 seconds ---\n",
  765. "20\n",
  766. "--- 3.11892032623291 seconds ---\n",
  767. "21\n",
  768. "--- 3.330472469329834 seconds ---\n",
  769. "22\n",
  770. "--- 3.5461206436157227 seconds ---\n",
  771. "23\n",
  772. "--- 3.7521393299102783 seconds ---\n",
  773. "24\n",
  774. "--- 3.956348180770874 seconds ---\n",
  775. "25\n",
  776. "--- 4.162136793136597 seconds ---\n",
  777. "26\n",
  778. "--- 4.365236759185791 seconds ---\n",
  779. "27\n",
  780. "--- 4.572294473648071 seconds ---\n",
  781. "28\n",
  782. "--- 4.778241872787476 seconds ---\n",
  783. "29\n",
  784. "--- 4.981487035751343 seconds ---\n",
  785. "30\n",
  786. "--- 5.189010143280029 seconds ---\n",
  787. "31\n",
  788. "--- 5.466430902481079 seconds ---\n",
  789. "32\n",
  790. "--- 5.73804497718811 seconds ---\n",
  791. "33\n",
  792. "--- 6.0193397998809814 seconds ---\n",
  793. "34\n",
  794. "--- 6.293334245681763 seconds ---\n",
  795. "35\n",
  796. "--- 6.569210767745972 seconds ---\n",
  797. "36\n",
  798. "--- 6.783808708190918 seconds ---\n",
  799. "37\n",
  800. "--- 6.999167203903198 seconds ---\n",
  801. "38\n",
  802. "--- 7.209052085876465 seconds ---\n",
  803. "39\n",
  804. "--- 7.414280652999878 seconds ---\n",
  805. "40\n",
  806. "--- 7.620949983596802 seconds ---\n",
  807. "41\n",
  808. "--- 7.892791986465454 seconds ---\n",
  809. "42\n",
  810. "--- 8.166114330291748 seconds ---\n",
  811. "43\n",
  812. "--- 8.46480393409729 seconds ---\n",
  813. "44\n",
  814. "--- 8.75532841682434 seconds ---\n",
  815. "45\n",
  816. "--- 9.027160882949829 seconds ---\n",
  817. "46\n",
  818. "--- 9.303063869476318 seconds ---\n",
  819. "47\n",
  820. "--- 9.575549125671387 seconds ---\n",
  821. "48\n",
  822. "--- 9.867429733276367 seconds ---\n",
  823. "49\n",
  824. "--- 10.160123109817505 seconds ---\n",
  825. "50\n",
  826. "--- 10.437638759613037 seconds ---\n",
  827. "51\n",
  828. "--- 10.714671611785889 seconds ---\n",
  829. "52\n",
  830. "--- 10.987818479537964 seconds ---\n",
  831. "53\n",
  832. "--- 11.259410381317139 seconds ---\n",
  833. "54\n",
  834. "--- 11.535178184509277 seconds ---\n",
  835. "55\n",
  836. "--- 11.807695865631104 seconds ---\n",
  837. "56\n",
  838. "--- 12.158225774765015 seconds ---\n",
  839. "57\n",
  840. "--- 12.506253004074097 seconds ---\n",
  841. "58\n",
  842. "--- 12.856064319610596 seconds ---\n",
  843. "59\n",
  844. "--- 13.203948497772217 seconds ---\n",
  845. "60\n",
  846. "--- 13.552793741226196 seconds ---\n",
  847. "61\n",
  848. "--- 13.906684160232544 seconds ---\n",
  849. "62\n",
  850. "--- 14.256698369979858 seconds ---\n",
  851. "63\n",
  852. "--- 14.606950283050537 seconds ---\n",
  853. "64\n",
  854. "--- 14.876070022583008 seconds ---\n",
  855. "65\n",
  856. "--- 15.148754596710205 seconds ---\n",
  857. "66\n",
  858. "--- 15.43168306350708 seconds ---\n",
  859. "67\n",
  860. "--- 15.710469961166382 seconds ---\n",
  861. "68\n",
  862. "--- 15.98047399520874 seconds ---\n",
  863. "69\n",
  864. "--- 16.25121569633484 seconds ---\n",
  865. "70\n",
  866. "--- 16.52086853981018 seconds ---\n",
  867. "71\n",
  868. "--- 16.790047645568848 seconds ---\n",
  869. "72\n",
  870. "--- 17.06355619430542 seconds ---\n",
  871. "73\n",
  872. "--- 17.335728406906128 seconds ---\n",
  873. "74\n",
  874. "--- 17.607405424118042 seconds ---\n",
  875. "75\n",
  876. "--- 17.955402135849 seconds ---\n",
  877. "76\n",
  878. "--- 18.303555488586426 seconds ---\n",
  879. "77\n",
  880. "--- 18.654282808303833 seconds ---\n",
  881. "78\n",
  882. "--- 19.004570245742798 seconds ---\n",
  883. "79\n",
  884. "--- 19.35291624069214 seconds ---\n",
  885. "80\n",
  886. "--- 19.700473070144653 seconds ---\n",
  887. "81\n",
  888. "--- 20.04847502708435 seconds ---\n",
  889. "82\n",
  890. "--- 20.39787983894348 seconds ---\n",
  891. "83\n",
  892. "--- 20.74629044532776 seconds ---\n",
  893. "84\n",
  894. "--- 21.094562768936157 seconds ---\n",
  895. "85\n",
  896. "--- 21.445199489593506 seconds ---\n",
  897. "86\n",
  898. "--- 21.794403791427612 seconds ---\n",
  899. "87\n",
  900. "--- 22.143365383148193 seconds ---\n",
  901. "88\n",
  902. "--- 22.49206042289734 seconds ---\n",
  903. "89\n",
  904. "--- 22.840426445007324 seconds ---\n",
  905. "90\n",
  906. "--- 23.189460515975952 seconds ---\n",
  907. "91\n",
  908. "--- 23.539386749267578 seconds ---\n",
  909. "92\n",
  910. "--- 23.888701677322388 seconds ---\n",
  911. "93\n",
  912. "--- 24.23668909072876 seconds ---\n",
  913. "94\n",
  914. "--- 24.58505630493164 seconds ---\n",
  915. "95\n",
  916. "--- 25.019609451293945 seconds ---\n",
  917. "96\n",
  918. "--- 25.456527709960938 seconds ---\n",
  919. "97\n",
  920. "--- 25.891918182373047 seconds ---\n",
  921. "98\n",
  922. "--- 26.32820987701416 seconds ---\n",
  923. "99\n",
  924. "--- 26.76149344444275 seconds ---\n",
  925. "100\n",
  926. "--- 27.197012424468994 seconds ---\n",
  927. "101\n",
  928. "--- 27.63314127922058 seconds ---\n",
  929. "102\n",
  930. "--- 28.068315029144287 seconds ---\n",
  931. "103\n",
  932. "--- 28.50419807434082 seconds ---\n",
  933. "104\n",
  934. "--- 28.852453231811523 seconds ---\n",
  935. "105\n",
  936. "--- 29.205727338790894 seconds ---\n",
  937. "106\n",
  938. "--- 29.554840087890625 seconds ---\n",
  939. "107\n",
  940. "--- 29.90355086326599 seconds ---\n",
  941. "108\n",
  942. "--- 30.251071214675903 seconds ---\n",
  943. "109\n",
  944. "--- 30.599868059158325 seconds ---\n",
  945. "110\n",
  946. "--- 30.94942593574524 seconds ---\n",
  947. "111\n",
  948. "--- 31.298285245895386 seconds ---\n",
  949. "112\n",
  950. "--- 31.648550271987915 seconds ---\n",
  951. "113\n",
  952. "--- 32.0825355052948 seconds ---\n",
  953. "114\n",
  954. "--- 32.516993045806885 seconds ---\n",
  955. "115\n",
  956. "--- 32.950743198394775 seconds ---\n",
  957. "116\n",
  958. "--- 33.38488531112671 seconds ---\n",
  959. "117\n",
  960. "--- 33.81857705116272 seconds ---\n",
  961. "118\n",
  962. "--- 34.27995991706848 seconds ---\n",
  963. "119\n",
  964. "--- 34.728654623031616 seconds ---\n",
  965. "120\n",
  966. "--- 35.16262221336365 seconds ---\n",
  967. "121\n",
  968. "--- 35.5960898399353 seconds ---\n",
  969. "122\n",
  970. "--- 36.02964925765991 seconds ---\n",
  971. "123\n",
  972. "--- 36.46674466133118 seconds ---\n",
  973. "124\n",
  974. "--- 36.91917443275452 seconds ---\n",
  975. "125\n",
  976. "--- 37.381704330444336 seconds ---\n",
  977. "126\n",
  978. "--- 37.81864261627197 seconds ---\n",
  979. "127\n",
  980. "--- 38.3528311252594 seconds ---\n",
  981. "128\n",
  982. "--- 38.89131188392639 seconds ---\n",
  983. "129\n",
  984. "--- 39.42161011695862 seconds ---\n",
  985. "130\n",
  986. "--- 39.95006561279297 seconds ---\n",
  987. "131\n",
  988. "--- 40.476089000701904 seconds ---\n",
  989. "132\n",
  990. "--- 41.00121235847473 seconds ---\n",
  991. "133\n",
  992. "--- 41.4318163394928 seconds ---\n",
  993. "134\n",
  994. "--- 41.86459708213806 seconds ---\n",
  995. "135\n",
  996. "--- 42.29518222808838 seconds ---\n",
  997. "136\n",
  998. "--- 42.729474782943726 seconds ---\n",
  999. "137\n",
  1000. "--- 43.16999864578247 seconds ---\n",
  1001. "138\n",
  1002. "--- 43.606104135513306 seconds ---\n",
  1003. "139\n",
  1004. "--- 44.04209113121033 seconds ---\n",
  1005. "140\n",
  1006. "--- 44.4772834777832 seconds ---\n",
  1007. "141\n",
  1008. "--- 45.01142644882202 seconds ---\n",
  1009. "142\n",
  1010. "--- 45.543590784072876 seconds ---\n",
  1011. "143\n",
  1012. "--- 46.07910680770874 seconds ---\n",
  1013. "144\n",
  1014. "--- 46.612366914749146 seconds ---\n",
  1015. "145\n",
  1016. "--- 47.1452751159668 seconds ---\n",
  1017. "146\n",
  1018. "--- 47.67322564125061 seconds ---\n",
  1019. "147\n",
  1020. "--- 48.20156168937683 seconds ---\n",
  1021. "148\n",
  1022. "--- 48.73471546173096 seconds ---\n",
  1023. "149\n",
  1024. "--- 49.2733519077301 seconds ---\n",
  1025. "150\n",
  1026. "--- 49.806400537490845 seconds ---\n",
  1027. "151\n",
  1028. "--- 50.33490014076233 seconds ---\n",
  1029. "152\n",
  1030. "--- 50.86489534378052 seconds ---\n",
  1031. "153\n",
  1032. "--- 51.39602565765381 seconds ---\n",
  1033. "154\n",
  1034. "--- 51.93729043006897 seconds ---\n",
  1035. "155\n",
  1036. "--- 52.473469972610474 seconds ---\n",
  1037. "156\n",
  1038. "--- 53.01401090621948 seconds ---\n",
  1039. "157\n",
  1040. "--- 53.58053278923035 seconds ---\n",
  1041. "158\n",
  1042. "--- 54.22534370422363 seconds ---\n",
  1043. "159\n",
  1044. "--- 54.870089292526245 seconds ---\n",
  1045. "160\n",
  1046. "--- 55.50953507423401 seconds ---\n",
  1047. "161\n",
  1048. "--- 56.144059896469116 seconds ---\n",
  1049. "162\n",
  1050. "--- 56.779675245285034 seconds ---\n",
  1051. "163\n",
  1052. "--- 57.41550326347351 seconds ---\n",
  1053. "164\n",
  1054. "--- 58.04742622375488 seconds ---\n",
  1055. "165\n",
  1056. "--- 58.57527136802673 seconds ---\n",
  1057. "166\n",
  1058. "--- 59.10521101951599 seconds ---\n",
  1059. "167\n",
  1060. "--- 59.737877368927 seconds ---\n",
  1061. "168\n",
  1062. "--- 60.373518228530884 seconds ---\n",
  1063. "169\n",
  1064. "--- 61.00429916381836 seconds ---\n",
  1065. "170\n",
  1066. "--- 61.64198398590088 seconds ---\n",
  1067. "171\n",
  1068. "--- 62.27683877944946 seconds ---\n",
  1069. "172\n",
  1070. "--- 62.91516971588135 seconds ---\n",
  1071. "173\n",
  1072. "--- 63.26179265975952 seconds ---\n",
  1073. "174\n",
  1074. "--- 63.89728498458862 seconds ---\n",
  1075. "175\n",
  1076. "--- 64.53867197036743 seconds ---\n",
  1077. "176\n",
  1078. "--- 65.18402314186096 seconds ---\n",
  1079. "177\n",
  1080. "--- 65.82770419120789 seconds ---\n",
  1081. "178\n",
  1082. "--- 66.46093964576721 seconds ---\n",
  1083. "179\n",
  1084. "--- 67.09133172035217 seconds ---\n",
  1085. "180\n",
  1086. "--- 67.73470735549927 seconds ---\n",
  1087. "181\n",
  1088. "--- 68.4084141254425 seconds ---\n",
  1089. "182\n",
  1090. "--- 69.05118441581726 seconds ---\n",
  1091. "183\n",
  1092. "--- 69.68487596511841 seconds ---\n",
  1093. "184\n",
  1094. "--- 70.3204357624054 seconds ---\n",
  1095. "[[ 3. 1. 3. ..., 1. 1. 1.]\n",
  1096. " [ 1. 6. 1. ..., 0. 0. 3.]\n",
  1097. " [ 3. 1. 3. ..., 1. 1. 1.]\n",
  1098. " ..., \n",
  1099. " [ 1. 0. 1. ..., 55. 21. 7.]\n",
  1100. " [ 1. 0. 1. ..., 21. 55. 7.]\n",
  1101. " [ 1. 3. 1. ..., 7. 7. 55.]]\n"
  1102. ]
  1103. }
  1104. ],
  1105. "source": [
  1106. "dataset, y = loadDataset(\"/home/ljia/Documents/research-repo/datasets/acyclic/Acyclic/dataset_bps.ds\")\n",
  1107. "G1 = dataset[12]\n",
  1108. "G2 = dataset[20]\n",
  1109. "Kmatrix = spkernel(dataset)\n",
  1110. "\n",
  1111. "print(Kmatrix)"
  1112. ]
  1113. }
  1114. ],
  1115. "metadata": {
  1116. "kernelspec": {
  1117. "display_name": "Python 3",
  1118. "language": "python",
  1119. "name": "python3"
  1120. },
  1121. "language_info": {
  1122. "codemirror_mode": {
  1123. "name": "ipython",
  1124. "version": 3
  1125. },
  1126. "file_extension": ".py",
  1127. "mimetype": "text/x-python",
  1128. "name": "python",
  1129. "nbconvert_exporter": "python",
  1130. "pygments_lexer": "ipython3",
  1131. "version": "3.5.2"
  1132. }
  1133. },
  1134. "nbformat": 4,
  1135. "nbformat_minor": 2
  1136. }

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