def test_fit_point2_vline(self): reference_points = [[0, -2], [0, 4]] target_distances = [2, 4] expected = [0, 0] point, t = fit_point(reference_points, target_distances) self.assertTrue( math.isclose(distance(point, expected), 0, abs_tol=0.0001), point) self.assertLess(t, SMALL_MAX_T)
def test_fit_point1_d0(self): reference_point = [5, 3] target_distance = 0 point, t = fit_point([reference_point], [target_distance]) self.assertTrue( math.isclose(distance(point, reference_point), 0, abs_tol=.0001), point) self.assertLess(t, SMALL_MAX_T)
def test_fit_point3(self): reference_points = [[math.sqrt(8), math.sqrt(8)], [0, -4], [-4, 0]] target_distances = [4, 4, 4] expected = [0, 0] point, t = fit_point(reference_points, target_distances, visualize=True) self.assertTrue( math.isclose(distance(point, expected), 0, abs_tol=0.0001), point) self.assertLess(t, SMALL_MAX_T)
def test_fit_point2_hline(self): reference_points = [[-1, 0], [5, 0]] target_distances = [2, 4] expected = [1, 0] point, t = fit_point(reference_points, target_distances, visualize=True) self.assertTrue( math.isclose(distance(point, expected), 0, abs_tol=0.0001), point) self.assertLess(t, SMALL_MAX_T)
def test_fit_point(self): reference_points_1 = [[0, -2, 3], [1, 0, -6], [2, 4, 9], [3, 6, -12]] #reference_points_1 = [[0, -2, 3], [1, 0, -6], [0, 4, 9], [3, 6, -12]] reference_points_2 = [[3, -2, 3], [2, 0, -6], [1, 4, 9], [0, 6, -12]] target_distances_1 = [5, 4, 3, 2] target_distances_2 = [1, 2, 3, 4] #target_distances_2 = [1, 1, 4, 3] point_11, t_11 = fit_point(reference_points_1, target_distances_1, visualize=True) self.assertTrue(all([not math.isnan(p) for p in point_11]), point_11) self.assertLess(t_11, BIG_MAX_T) point_12, t_12 = fit_point(reference_points_1, target_distances_2, visualize=True) self.assertTrue(all([not math.isnan(p) for p in point_12]), point_12) self.assertLess(t_12, BIG_MAX_T) point_21, t_21 = fit_point(reference_points_2, target_distances_1, visualize=True) self.assertTrue(all([not math.isnan(p) for p in point_21]), point_21) self.assertLess(t_21, BIG_MAX_T) point_22, t_22 = fit_point(reference_points_2, target_distances_2, visualize=True) self.assertTrue(all([not math.isnan(p) for p in point_22]), point_22) self.assertLess(t_22, BIG_MAX_T) self.assertGreater(distance(point_11, point_12), 1) self.assertGreater(distance(point_11, point_21), 1) self.assertGreater(distance(point_11, point_22), 1) self.assertGreater(distance(point_12, point_21), 1) self.assertGreater(distance(point_12, point_22), .75) self.assertGreater(distance(point_21, point_22), 1)
def _fit_top_k(self, predictions): ## Fitting across top-k points. colour_probabilities = {} for output, probability in predictions.items(): self.mapped_output(output) colour = self.output_colour(output) if colour not in colour_probabilities: colour_probabilities[colour] = 0 colour_probabilities[colour] += probability colour, probability = max(colour_probabilities.items(), key=lambda item: item[1]) total = sum(colour_probabilities.values()) fit = geometry.fit_proportion((colour, [255, 255, 255]), (total - probability, probability)) return "rgb(%d, %d, %d)" % tuple([round(i) for i in fit]) maximum_distance = None for pair in itertools.combinations( [colour for colour in colour_probabilities.keys()], 2): distance = geometry.distance(pair[0], pair[1]) if maximum_distance is None or distance > maximum_distance: maximum_distance = distance inverted = [ item for item in mlbase.regmax( {c: 1.0 - p for c, p in colour_probabilities.items()}).items() ] fit, _ = geometry.fit_point( [item[0] for item in inverted], [item[1] * maximum_distance for item in inverted]) return "rgb(%d, %d, %d)" % tuple([round(i) for i in fit])
} #for word, probability in predictions[key].items(): # colour = self.colour_embeddings[word] # # if colour not in interpolation_points: # interpolation_points[colour] = (word, probability) # else: # if interpolation_points[colour][1] < probability: # interpolation_points[colour] = (word, probability) # if len(interpolation_points) == 1: colours[key] = "rgb(%d, %d, %d)" % next(iter(interpolation_points.keys())) else: maximum_distance = None for pair in itertools.combinations([colour for colour in interpolation_points.keys()], 2): distance = geometry.distance(pair[0], pair[1]) if maximum_distance is None or distance > maximum_distance: maximum_distance = distance lowest_probability = min([p for w, p in interpolation_points.values()]) highest_probability = max([p for w, p in interpolation_points.values()]) maximum_domain = highest_probability + lowest_probability prediction_distances = [(w, maximum_distance + (-p * maximum_distance / maximum_domain)) for w, p in interpolation_points.values()] fit, _ = geometry.fit_point([colour_embeddings[item[0]] for item in prediction_distances], [item[1] for item in prediction_distances], epsilon=0.1, visualize=False) print("rgb(%d, %d, %d)" % tuple([round(i) for i in fit]))