Ejemplo n.º 1
0
	def __init__(self, root=None, parent=None, concept_tree=None, filename=None, directory="."):
		"""
		The constructor.
		"""
		self.instances = []
		self.viz = Visualization(self, directory)
		self.measure = Measure(self)
		self.filename = filename
		self.old_count = 0
		# keep track of the root
		if root:
			self.root = root
		else:
			self.root = self
		self.parent = parent
		# check if the constructor is being used as a copy constructor
		if concept_tree:
			self.utility = Utility(self, concept_tree.utility)
			self.children = copy.deepcopy(concept_tree.children)
			for c in self.children:
				c.parent = self
			for inst in concept_tree.instances:
				self.instances.append(inst)
		else:
			self.utility = Utility(self)
			self.children = []
		# must initialize after utility has been set
		self.CBWB = COBWEB(self)
Ejemplo n.º 2
0
class Node:
	mergedNodes = []
	splitNodes = []
	splitMergeOrder = []
	util_values = {}

	def __init__(self, root=None, parent=None, concept_tree=None, filename=None, directory="."):
		"""
		The constructor.
		"""
		self.instances = []
		self.viz = Visualization(self, directory)
		self.measure = Measure(self)
		self.filename = filename
		self.old_count = 0
		# keep track of the root
		if root:
			self.root = root
		else:
			self.root = self
		self.parent = parent
		# check if the constructor is being used as a copy constructor
		if concept_tree:
			self.utility = Utility(self, concept_tree.utility)
			self.children = copy.deepcopy(concept_tree.children)
			for c in self.children:
				c.parent = self
			for inst in concept_tree.instances:
				self.instances.append(inst)
		else:
			self.utility = Utility(self)
			self.children = []
		# must initialize after utility has been set
		self.CBWB = COBWEB(self)
	
	def makeTree(self, root=None, parent=None, concept_tree=None):
		return Node(root, parent, concept_tree)
	
	def saveObject(self, filename, remove='tmp.cbwb'):
		with open(filename, 'wb') as output:
			pickle.dump(self, output, pickle.HIGHEST_PROTOCOL)
		if os.path.isfile(remove):
			os.remove(remove)
		print "Saved", filename
	
	def firstCobweb(self, instance, display=False):
		Node.mergedNodes = []
		Node.splitNodes = []
		Node.splitMergeOrder = []
		Node.util_values = {}
		self.CBWB.firstCobweb(instance, display)
		# Save the original order to better understand
		instance.save_merges_and_splits(Node.mergedNodes, Node.splitNodes, Node.splitMergeOrder)
		# Clean up merges and splits
		in_both = []
		for change_list in Node.mergedNodes:
			if change_list in Node.splitNodes:
				in_both.append(change_list)
		Node.splitNodes = [l for l in Node.splitNodes if not l in in_both]
		Node.mergedNodes = [l for l in Node.mergedNodes if not l in in_both]
		# Set instance information (to explain changes)
		instance.surprise_num = self.measure.delta_num(instance)
		instance.number = len(self.instances)
		self.toDot(str(instance.number)+".dot", False, instance)
	
	def cobweb(self, instance):
		self.CBWB.cobweb(instance)

	def pretty_print(self):
		c.viz.pretty_print()
	
	def toDot(self, fileName, withStrings=True, latest=None):
		self.viz.toDot(fileName, withStrings, latest)
	
	def __str__(self):
		"""
		Converts the categorization tree into a string.
		"""
		ret = str(self.utility.av_counts)
		for c in self.children:
			ret += "\n" + str(c)
		return ret