def test_discretize_in_multiple_points1(self): def three_points_almost_equals(theroical_three_points, actual_three_points): def almost_equals(a, b): return la.norm( np.array(a.coordinates) - np.array(b.coordinates)) < 1e-10 res = True for i in range(3): res = res and almost_equals(theroical_three_points[i], actual_three_points[i]) return res theroical_three_points1 = list(map((lambda x: Point(self.discretisator3.change_base_u_to_e(x))),\ self.theorical_three_point_in_u31)) actual_three_points1 = self.discretisator3.discretize_in_multiple_points(Point(\ self.discretisator3.change_base_u_to_e(self.central_point_coord_in_base_u31))) assert three_points_almost_equals( theroical_three_points1, actual_three_points1), str(actual_three_points1) theroical_three_points2 = list(map((lambda x: Point(self.discretisator3.change_base_u_to_e(x))), \ self.theorical_three_point_in_u32)) actual_three_points2 = self.discretisator3.discretize_in_multiple_points(Point( \ self.discretisator3.change_base_u_to_e(self.central_point_coord_in_base_u32))) assert three_points_almost_equals( theroical_three_points2, actual_three_points2), str(actual_three_points2)
def setUp(self): """Initialize tests.""" self.bf_tester = BloomFilterTester(10, 80) self.bf_tester.feed([Point([0.5, 0.5])], discretisator=RectangleDiscretisator(1)) self.discretized_points = [ Point([0.0, 0.0]), Point([1.0, 0.0]), Point([0.0, 1.0]), Point([1.0, 1.0]) ]
def setUp(self): """Initialize tests.""" self.bf_tester = BloomFilterTester( 10, 8000, 2, discretisator=CircleDiscretisator2D(5)) self.bf_tester.feed([Point([5, 5])]) self.discretized_points = [ Point([5, 5]), Point([5, 6]), Point([5, 4]), Point([6, 5]), Point([4, 5]), Point([4, 7]), Point([101, 90]) ] # Second test dim = 2 delta = 5 feed_data = RandomDataGenerator(dim, size_of_data_set=200, domain=1000000) feed_data.genarate() self.similar_data = RandomDataGenerator(dim, size_of_data_set=200, domain=1000000) self.similar_data.genarate_similar(delta, feed_data.get_points()) Cdiscretizor = CircleDiscretisator2D(10) self.bf2 = BloomFilterTester(200, 3200, 2, discretisator=Cdiscretizor) self.bf2.feed(feed_data.get_points())
def get_points(self): # We test if the file exist path_file = Path(self.file_path) self.logger.info('We get the points from' + str(path_file)) if not path_file.is_file(): self.logger.error('The file does not exist') raise Exception("The file does not exist" + str(path_file)) data_file = pd.read_csv(str(path_file)) line_counter = -1 try: for i, row in enumerate(data_file.values): line_counter += 1 coodinates = [ Decimal(int(elem)) for index, elem in enumerate(row) if index != 0 ] # We get a dimension problem. if len(coodinates) != self.dimension: self.logger.error( 'The dimension is not correct, expected : ' + str(len(coodinates)) + str(self.dimension)) raise Exception("The dimension of the point at line " + str(line_counter) + " is not correct") self.point_list.append(Point(coodinates)) except Exception as e: self.logger.error("Probleme during reading line " + str(line_counter)) raise e return self.point_list
class PointTest(unittest.TestCase): """Test case used for tests my Point class.""" def setUp(self): """Initialize tests.""" self.point_a = Point([1,2]) self.point_b = Point([4,6]) def test_distance(self): """Test of function distance.""" dist = self.point_a.distance(self.point_b) self.assertEqual(5, dist) def test_zero_distance(self): """Test of function distance.""" dist = self.point_a.distance(self.point_a) self.assertEqual(0, dist)
def setUp(self): """Initialize tests.""" self.discretisator = RectangleDiscretisator(5) """"For minimize""" self.point_a = Point([7]) self.point_b = Point([5]) self.point_c = Point([10]) self.point_d = Point([1, 32]) self.point_e = Point([5, 10]) """"For discrette""" self.point_f = Point([7]) self.point_g = Point([5]) self.point_h = Point([10]) self.point_i = Point([1, 32]) self.point_j = Point([5, 10])
def setUp(self): """Initialize tests.""" # For test_change_base_e_to_u self.discretisator1 = CircleDiscretisator2D(10) # For test_four_points_around self.discretisator2 = CircleDiscretisator2D(10) self.point_coord_in_base_u = [0.5, 0.5] # For test_get_three_closer_points & test_discretize_in_multiple_points1 self.discretisator3 = CircleDiscretisator2D(10) self.central_point_coord_in_base_u31 = [0.1, 0.1] self.theorical_three_point_in_u31 = ([0.0, 0.0], [1.0, 0.0], [0.0, 1.0]) self.central_point_coord_in_base_u32 = [0.9, 0.9] self.theorical_three_point_in_u32 = ([1.0, 0.0], [0.0, 1.0], [1.0, 1.0]) # For test_get_three_descretized_points_around2 & test_discretize_in_one_point self.discretisator4 = CircleDiscretisator2D(10) self.central_point41 = Point( self.discretisator4.change_base_u_to_e([0.1, 0.1])) self.central_point42 = Point( self.discretisator4.change_base_u_to_e([0.9, 0.9])) self.central_point43 = Point( self.discretisator4.change_base_u_to_e([1.1, 0.1])) self.central_point44 = Point( self.discretisator4.change_base_u_to_e([0.1, 1.1])) # Only for test_discretize_in_one_point self.central_point45 = Point( self.discretisator4.change_base_u_to_e([0.9, 0.1])) self.central_point46 = Point( self.discretisator4.change_base_u_to_e([0.1, 0.9]))
def discretise_point(self, point): """ Apply a combinatory ceil and floor to each coordinate of the given point in parameter, according to the lambda_error. Return a list of points that enclose the given point in parameter, in function of the dimension of the point and the lambda_error Args : :param point: coordinates which have to be maximised """ #deepcopy the point given in parmeter to prevent instruction to modify it and be able able to use it again without modification point_c = deepcopy(point) results = [] results.append(Point(self.minimisePoint(point_c.coordinates))) self.discretise_recursive(point_c.coordinates, point.coordinates, 0, results) return results
def four_points_around(self, point_coord_in_base_u): """ :param point_coord_in_base_u: :return: tuple of for point that are around the point represented by the coordonate : point_coord_in_base_u """ floored_x_coord = math.floor(point_coord_in_base_u[0]) floored_y_coord = math.floor(point_coord_in_base_u[1]) down_left = Point( list( np.transpose( np.dot(self.transition_matrix_u_to_e, np.array([[floored_x_coord], [floored_y_coord]])))[0])) down_right = Point( list( np.transpose( np.dot( self.transition_matrix_u_to_e, np.array([[floored_x_coord + 1], [floored_y_coord]])))[0])) up_left = Point( list( np.transpose( np.dot( self.transition_matrix_u_to_e, np.array([[floored_x_coord], [floored_y_coord + 1]])))[0])) up_right = Point( list( np.transpose( np.dot( self.transition_matrix_u_to_e, np.array([[floored_x_coord + 1], [floored_y_coord + 1]])))[0])) return (down_left, down_right, up_left, up_right)
def genarate_similar(self, delta, data_set, save_file_name=None): """ This method genrate the data and store it into the object attribute point_list. Each point generated in list_point is at least at <delta> from any point in <data_set>. :param delta: (float) the minimum distance between the point <point> to the <data_set> :param data_set: The data set in form of list of object Point :param save_file_name: name of the file in which we will store the generate data for next tests. if this parameter if not registered the data will be not save. :return: Nothing """ number_of_vector = 0 number_of_test = 0 array_vector = [] while number_of_vector < self.size_of_data_set: index = randint(0, len(data_set) - 1) vct = list(data_set[index].coordinates) for i in range(len(vct)): vct[i] += uniform(0, 1) * delta point = Point(vct) if point.distance(data_set[index]) < delta: self.point_list.append(point) array_vector.append(vct) number_of_vector += 1 number_of_test += 1 if number_of_test >= 1000 * len(data_set): logger.error("infinit loop to construct similar") assert (False) if save_file_name: try: os.remove( os.path.join(os.getcwd(), DATA_FOLDER, save_file_name)) except OSError: pass pd.DataFrame(array_vector).to_csv(os.path.join( os.getcwd(), DATA_FOLDER, save_file_name), encoding='utf-8')
def discretise_recursive(self, point, original_points, starting_index, results): """ shouldn't be called directly, call for discretise """ for i in range(starting_index, len(point)): if point[i] != original_points[i]: #the original value of point should be the same for each passage in the loop so deepcopy point_c = point[:] if self.method_type == Constants.DIS_DOUBLE: point_c[i] += self.lambda_error else: point_c[i] += 2 * self.lambda_error results.append(Point(point_c)) self.discretise_recursive(point_c, original_points, i + 1, results)
def test_discretize_in_one_point(self): def almost_equals(a, b): return la.norm(np.array(a.coordinates) - np.array(b.coordinates)) < 1e-10 closest_dis_point1 = self.discretisator4.discretize_in_one_point( self.central_point41) closest_dis_point2 = self.discretisator4.discretize_in_one_point( self.central_point42) closest_dis_point3 = self.discretisator4.discretize_in_one_point( self.central_point45) closest_dis_point4 = self.discretisator4.discretize_in_one_point( self.central_point46) assert almost_equals(closest_dis_point1, Point( [0, 0])), str(closest_dis_point1) + " " + str(Point([0, 0])) assert almost_equals(closest_dis_point2, Point(self.discretisator4.change_base_u_to_e([1, 1]))),\ str(closest_dis_point2) + " " + str(Point([1, 1])) assert almost_equals(closest_dis_point3, Point(self.discretisator4.change_base_u_to_e([1, 0]))), \ str(closest_dis_point3) + " " + str(Point([1, 0])) assert almost_equals(closest_dis_point4, Point(self.discretisator4.change_base_u_to_e([0, 1]))), \ str(closest_dis_point4) + " " + str(Point([0, 1]))
def genarate(self, save_file_name=None): """ This method genrate the data and store it into the object attribute point_list. :param save_file_name: name of the file in which we will store the generate data for next tests. if this parameter if not registered the data will be not save. :return: Nothing. """ array_vector = np.random.randint(self.domain, size=(self.size_of_data_set, self.dimension)) for vct in array_vector: self.point_list.append(Point(list(vct))) if save_file_name: try: os.remove( os.path.join(os.getcwd(), DATA_FOLDER, save_file_name)) except OSError: pass vector_data_frame_ = pd.DataFrame(array_vector) vector_data_frame_.to_csv(os.path.join(os.getcwd(), DATA_FOLDER, save_file_name), encoding='utf-8')
def genarate_falses(self, delta, data_set, save_file_name=None): """ This method genrate the data and store it into the object attribute point_list. Each point generated in list_point is at least at <delta> from any point in <data_set>. :param delta: (float) the minimum distance between the point <point> to the <data_set> :param data_set: The data set in form of list of object Point :param save_file_name: name of the file in which we will store the generate data for next tests. if this parameter if not registered the data will be not save. :return: Nothing """ number_of_vector = 0 number_of_test = 0 array_vector = [] while number_of_vector < self.size_of_data_set: vct = np.random.randint(self.domain, size=(self.size_of_data_set, self.dimension)).tolist()[0] point = Point(vct) if self.distant_enough(point, delta, data_set): self.point_list.append(point) array_vector.append(vct) number_of_vector += 1 number_of_test += 1 if number_of_test >= 1000 * len(data_set): logger.error("infinite loop to construct different") assert (False) if save_file_name: try: os.remove( os.path.join(os.getcwd(), DATA_FOLDER, save_file_name)) except OSError: pass pd.DataFrame(array_vector).to_csv(os.path.join( os.getcwd(), DATA_FOLDER, save_file_name), encoding='utf-8') return array_vector
def test_four_points_around(self): def almost_equals(a, b): return la.norm(np.array(a.coordinates) - np.array(b.coordinates)) < 1e-10 assert almost_equals(self.discretisator2.four_points_around(self.point_coord_in_base_u)[0], Point([0.0, 0.0])),\ self.discretisator2.four_points_around(self.point_coord_in_base_u)[0].to_string() assert almost_equals(self.discretisator2.four_points_around(self.point_coord_in_base_u)[1], Point(list(\ self.discretisator2.u1))), self.discretisator2.four_points_around(self.point_coord_in_base_u)[1].\ to_string() assert almost_equals(self.discretisator2.four_points_around(self.point_coord_in_base_u)[2], Point(list(\ self.discretisator2.u2))), self.discretisator2.four_points_around(self.point_coord_in_base_u)[2]. \ to_string() assert almost_equals(self.discretisator2.four_points_around(self.point_coord_in_base_u)[3], Point(list( \ self.discretisator2.u2+self.discretisator2.u1))), self.discretisator2.four_points_around(self.\ point_coord_in_base_u)[3].to_string()
def setUp(self): """Initialize tests.""" self.point_a = Point([1,2]) self.point_b = Point([4,6])