Looking for inactive functions – Part 3

Adding networkx and mapplotlib.pyplot allows us to build a knowledge graph and visualise it.

I created a basic networkx object and added all the filenames, classes and functions as nodes. Each node has a data attribute class “entity_type”. I also setup a color map array to show what type of node is being shown and added a title.

import os
import re
import networkx as nx
import matplotlib.pyplot as plt


class Map(object):
	def __init__(self):
		self.use_directories = []
		self.ignore_filenames = []
		self.file_extensions_required = []
		self.map_files = []
		self.classes_found = []
		self.functions_found = []
		

	def mapper(self):
		
		self.get_all_files()
		
	def get_all_files(self):

		# read all the required files in the directories into map_files array

		for directory in self.use_directories:
			
			files = [ f for f in os.listdir(directory) if os.path.isfile(os.path.join(directory, f)) ]
			
			for f in files:
				if self.is_file_required(f): 
					full_path = "/".join([directory, f])
					self.map_files.append( { 'path' : full_path, 'name' : f } )
	
		# if we have any files at all
		if self.map_files:
			self.find_functions_and_classes()
	

	def is_file_required(self, filename):
		
		# Ignoring : file has no extension
		if "." not in filename:
			return False
		
		# Ignoring : file extension not required
		file_parts = filename.split(".")
		if file_parts[-1] not in self.file_extensions_required:
			return False
		
		# Ignoring : Filename is excluded
		if filename in self.ignore_filenames:
			return False
			
		return True

	def find_functions_and_classes(self):
			
		for mf in self.map_files:

			with open(mf['path'], "r") as f:
				current_class = ""
				
				for l in f:
				
					if re.search("^class", l):
						current_class = l.replace("\n", "").strip()
						self.classes_found.append( { 'file' : mf['name'], 'classname' : current_class })
						
						
					if re.search("def\s(.*):$", l):
						function_name = l.replace("\n", "").strip()
						self.functions_found.append( { 'file' : mf['name'], 'classname' : current_class, 'functionname' : function_name } )
					

# create class
mapper = Map()

# set requirements for
mapper.use_directories.append("/home/phill/Desktop/projects/zombizied_gui")
mapper.use_directories.append("/home/phill/Desktop/projects/zombizied_gui/classes")
mapper.file_extensions_required.append("py")
mapper.ignore_filenames.append("zombizied_ai_preprocessor.py")
mapper.ignore_filenames.append("zombizied_mapbuilder.py")

mapper.mapper()

# ADD filenames, classes and functions AS Nodes to Knowledge Graph

G = nx.Graph()

for m in mapper.map_files:
	G.add_node(m['name'], entity_type="file")

for c in mapper.classes_found:
	G.add_node(c['classname'], entity_type="class")

for f in mapper.functions_found:
	G.add_node(f['functionname'], entity_type="function")

# Check Nodes are created	

for g in G:
	print("Node:", g)

# Check Nodes with Entity_Types

for g, data in G.nodes(data=True):
	print("Node:", g, data['entity_type'])


print("Create Basic Knowledge Graph Plot")


# add color map to color Nodes according to their entity type

color_map = {
	'file' : 'skyblue',
	'class' : 'lightgreen',
	'function' : 'lightcoral'
}

node_colors = [color_map[G.nodes[node]['entity_type']] for node in G.nodes()]

pos = nx.spring_layout(G)

nx.draw(G,
		pos,
		with_labels=True,
		node_color=node_colors,
		node_size=500,
		font_size=8
)

plt.show()

Creating a basic Knowledge Graph for the nx Object G

pos = nx.spring_layout(G)
nx.draw(G,pos)
plt.show()
A knowledge graph rendered with just nodes (no edges ~ connections)

Adding titles and colors to the knowledge graph

color_map = {
	'file' : 'skyblue',
	'class' : 'lightgreen',
	'function' : 'lightcoral'
}

node_colors = [color_map[G.nodes[node]['entity_type']] for node in G.nodes()]

pos = nx.spring_layout(G)

nx.draw(G,
		pos,
		with_labels=True,
		node_color=node_colors,
		node_size=500,
		font_size=8
)

plt.show()
A lot going on and no edges have been defined yet.
When zooming in, the colors and titles are easier to read.

Similar Posts