コード例 #1
0
	def test_inspection_of_both_sides_of_splitting_plane(self):
		'''
		While walking down the kdtree, find_closest chooses to inspect points
		in the left_child or right_child of a node based on whether the
		ref_location is on the "left" or "right" side of the splitting plane
		created by the node.

		If the ref_location is closer to the splitting plane than to the
		current closest, then the true closest neighbor might be on the other
		side of the splitting plane.

		If find_closest is doing *everything else* correctly *except*
		inspecting both sides of the splitting plane (when necessary),
		then find_closest will return "D" as the closest neighbor here,
		rather than "E".
		'''
		neighbors_coords = [
			('A', 1.00, 1.00),
			('B', 1.20, 2.00),
			('C', 2.50, 3.50),
			('D', 3.50, 2.80),
			('E', 3.70, 0.80),
			('F', 4.25, 1.80),
			('G', 6.00, 5.60)
		]
		neighbors_df = TestFindClosest.coords_to_df(neighbors_coords)
		ref_location = (3.36, 0.9)
		tree = build_kd_tree(neighbors_df)
		result = find_closest(ref_location, tree)
		self.assertEqual(result.name, "E")
		self.assertEqual(result.coordinates, (3.7, 0.8))
コード例 #2
0
	def test_1000_random_10D_points(self):
		total_runs = 5
		num_dimensions = 10
		for run_num in range(1, total_runs + 1):
			print(
				"Testing 1000 random 10D points, run" +
				"%d/%d" % (run_num, total_runs)
			)

			def rand_val():
				# Choose values between -1 and 1.
				r = random.randint(-1000, 1000)
				while r == 0:
					r = random.randint(-1000, 1000)
				return 1. / r

			# Make 10 random points, with 10 random names.
			neighbors_coords = [
				(uuid.uuid4().hex,) +
				tuple((rand_val() for _ in range(num_dimensions)))
				for _ in range(1000)
			]
			neighbors_df = TestFindClosest.coords_to_df(neighbors_coords)

			# Generate a random reference location.
			ref_location = tuple(rand_val() for _ in range(num_dimensions))
			tree = build_kd_tree(neighbors_df)
			result = find_closest(ref_location, tree)
			expected_name, expected_coords = find_closest_with_brute_force(
				neighbors_df, ref_location
			)
			self.assertEqual(result.name, expected_name)
			self.assertEqual(result.coordinates, expected_coords)
コード例 #3
0
	def test_basic(self):
		neighbors_coords = [
			('A', -6, 3),
			('B', 2, 2.2),
			('C', 3, -6),
		]
		neighbors_df = TestFindClosest.coords_to_df(neighbors_coords)
		ref_location = (-1, -2)
		tree = build_kd_tree(neighbors_df)
		result = find_closest(ref_location, tree)
		self.assertEqual(result.name, "B")
		self.assertEqual(result.coordinates, (2, 2.2))
コード例 #4
0
	def test_all_neighbors_equal(self):
		neighbors_coords = [
			('A', 1.1, 1.3),
			('B', 1.1, 1.3),
		]

		neighbors_df = TestFindClosest.coords_to_df(neighbors_coords)
		ref_location = (-1, -2)
		tree = build_kd_tree(neighbors_df)
		result = find_closest(ref_location, tree)

		# Not testing the name here. The algorithm does not
		# promise stable traversal of the tree.
		self.assertEqual(result.coordinates, (1.1, 1.3))