示例#1
0
def plot_single_cluster(clusters_file_path, cluster_number, average=False):
	clusters_file = open(clusters_file_path, "r")	

	# Create temporary file for plot data.

	plot_data_tmpfile_path = tempfile.mkstemp()[1]
	plot_data_tmpfile = open(plot_data_tmpfile_path, "w")

	# Start searching for cluster we want to plot in file
	# with clusters.

	found_cluster = False

	line = clusters_file.readline().strip()
	while line != "":

		if line.isdigit() == True and int(line) == cluster_number:

			found_cluster = True

			if average:
				# We will now average price vectors from all
				# companies in this group.
				company_name = clusters_file.readline().strip()
				vec = utils.make_prices_vec_by_company(data, company_name)
				company_number = 1
				company_name = clusters_file.readline().strip()
				while not company_name.isdigit() and company_name != "":
					vec2 = utils.make_prices_vec_by_company(data, company_name)
					company_number += 1
					vec = [x+y for x,y in zip(vec,vec2)]
					company_name = clusters_file.readline().strip()
				vec = [ (x/company_number) for x in vec ]
				map(lambda p: plot_data_tmpfile.write(str(p) + "\n"), vec)
				plot_data_tmpfile.write("\n\n")

			else:
				# Ok, we've found our cluster. Now we iterate companies in
				# this group and write their prices vectors seperated by two
				# blank lines to one file (used as input for gnuplot).
				company_name = clusters_file.readline().strip()
				while not company_name.isdigit() and company_name != "":

					vec = utils.make_prices_vec_by_company(data, company_name)
					map(lambda p: plot_data_tmpfile.write(str(p) + "\n"), vec)
					plot_data_tmpfile.write("\n\n")
					company_name = clusters_file.readline().strip()

			# Great, we're done, we wanted data for only one cluster,
			# so break here.
			break

		line = clusters_file.readline().strip()
	
	# Close this file here, we'll need file buffers to be flushed for this
	# file before running gnuplot.

	plot_data_tmpfile.close()
	clusters_file.close()
	return found_cluster, plot_data_tmpfile_path
示例#2
0
	def testMakePricesVecByCompany(self):
		"""Obtaining prices vector by company name."""

		ambra_prices_vec = utils.make_prices_vec_by_company(self.data1, "AMBRA")		
		self.assertEqual(ambra_prices_vec, [9.5, 9, 10.5, 20.5])

		tvn_prices_vec = utils.make_prices_vec_by_company(self.data1, "TVN")		
		self.assertEqual(tvn_prices_vec, [9.5, 16.5, 17.5, 18.5])