Guest User

Untitled

a guest
Dec 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4.  
  5. YAVUZ ÇETİN ÖLÜMSÜZDÜR
  6.  
  7. @author: Mehmet
  8. """
  9.  
  10. import networkx as nx
  11. import pandas as pd
  12. from networkx.drawing.nx_agraph import graphviz_layout, to_agraph
  13. """
  14. Model class for the Markov Chains object
  15. """
  16. class Chain:
  17.  
  18. """
  19. Takes the sample name and a list containing the names of the instructions
  20.  
  21. Creates a graph G, which is a Directed MultiGraph networkx object and M,
  22. which is initially zero but after calling adjacency_matrix_to_pd_dataframe()
  23. it contains the adjacency matrix
  24.  
  25. Notice that instruction_names should contain unique names
  26. """
  27. def __init__(self, name, instruction_names):
  28. self.name = name
  29. self.instruction_names = instruction_names
  30. self.num_of_instructions = len(instruction_names)
  31. self.G = nx.MultiDiGraph()
  32. self.M = None
  33.  
  34. for i in range(self.num_of_instructions):
  35. self.G.add_node(instruction_names[i])
  36.  
  37.  
  38. """
  39. Adds a single instance to the graph
  40. """
  41. def add(self, source_instr, target_instr, weight):
  42. self.G.add_edges_from([(source_instr, target_instr, {'weight': weight})])
  43.  
  44.  
  45. """
  46. Takes a list object that contains tuples in the format (source_instr, source_target, weight)
  47. """
  48. def add_multiple(self, _list):
  49. to_add = [(w[0], w[1], {'weight': w[2]}) for w in _list]
  50. self.G.add_edges_from(to_add)
  51.  
  52.  
  53. """
  54. Returns the adjacency matrix
  55. """
  56. def adjacency_matrix_to_pd_dataframe(self):
  57. self.M = nx.to_pandas_adjacency(self.G)
  58. return self.M
  59.  
  60. """
  61. Check if the adjacency matrix satisfied the condition where
  62. probabilities of all events originating from a single node sums to 1
  63. """
  64. def check_validity(self):
  65. if self.M is None:
  66. raise Exception('Adjacency matrix is not calculated yet!')
  67. else:
  68. sum_vector = self.M.sum(axis=1)
  69. unique = list(set(sum_vector))
  70. is_valid = True
  71.  
  72. for i in range(len(unique)):
  73. if unique[i] not in (0, 1):
  74. is_valid = False
  75.  
  76. return is_valid
  77.  
  78. """
  79. !!!!!!!! BIG TODO !!!!!!!!!
  80. Change the pickle serialization into h5py
  81. """
  82. """
  83. Save the model (which is, in fact, the adjacency matrix)
  84. """
  85. def save_model(self, path='chain_model.pkl'):
  86. self.M.to_pickle(path)
  87.  
  88.  
  89. """
  90. Load the model from a pickle file
  91. """
  92. def load_model(self, path):
  93. self.M = pd.read_pickle(path)
  94.  
  95.  
  96. """
  97. Draws the graph into a specified path
  98. """
  99. def draw(self, draw_path='graph.png'):
  100. # draw the graph with graphviz
  101. # get weights
  102. for u,v,d in self.G.edges(data=True):
  103. d['label'] = d.get('weight','')
  104. # set defaults
  105. A = to_agraph(self.G)
  106. A.layout('dot')
  107. A.draw('graph.png')
  108.  
  109.  
  110. ###### testing
  111. test_instruction_data = ['pop', 'push', 'move', 'add', 'sub', 'add', 'or']
  112.  
  113. chain = Chain('zeus', test_instruction_data)
  114. chain.add_multiple([('pop', 'move', 0.4),
  115. ('pop', 'add', 0.4),
  116. ('pop', 'pop', 0.2)])
  117.  
  118. chain.add('or', 'pop', 0.4)
  119. chain.add('or', 'move', 0.6)
  120.  
  121.  
  122. M = chain.adjacency_matrix_to_pd_dataframe()
  123. chain.draw()
Add Comment
Please, Sign In to add comment