def __init__(self,
                 centroid,
                 points,
                 name,
                 number,
                 give_info=True,
                 do_ellipsoid=True,
                 do_cuboid=True):
        if give_info:
            logger.log_header("Created Cluster: " + str([name]) +
                              " Number: #" + str(number),
                              styles=[logger.LogHeaderStyle.SUB_HEADER])

        self.center = centroid
        self.points = points
        if do_cuboid:
            logger.log("Creating Cuboid in Cluster")
            self.cuboid = Cuboid(self.points)
        if do_ellipsoid:
            logger.log("Creating Ellipsoid in Cluster")
            self.ellipsoid = Ellipsoid(self.points, global_v.SEMI_AXIS_SCALE)
            if (global_v.CHAR_NUM == 3):
                self.rejected_x, self.rejected_y, self.rejected_z = self.ellipsoid.is_point_in_ellipsoid(
                    self.points[:])
            else:
                self.rejected_x, self.rejected_y = self.ellipsoid.is_point_in_ellipsoid(
                    self.points[:])
            if give_info:
                self.__info(name, number)
                logger.log('Points in ellipsoid: ' +
                           str((1 - len(self.rejected_x) / len(self.points)) *
                               100) + '%')
Example #2
0
class Cluster:
    """
    This class holds data about single cluster,
    its center and points around it
    """
    def __init__(self, centroid, points, name, number, give_info = True, do_ellipsoid = True, do_cuboid = True):
        self.center = centroid
        self.points = points
        if do_cuboid:
            self.cuboid = Cuboid(self.points)
        if do_ellipsoid:
            self.ellipsoid = Ellipsoid(self.points, global_v.SEMI_AXIS_SCALE)
            if(global_v.CHAR_NUM == 3):    
                self.rejected_x, self.rejected_y, self.rejected_z = self.ellipsoid.is_point_in_ellipsoid(self.points[:])
            else:
                self.rejected_x, self.rejected_y = self.ellipsoid.is_point_in_ellipsoid(self.points[:])
            if give_info:
                self.__info(name, number)
                print('        >> points in ellipsoid:',(1- len(self.rejected_x)/len(self.points)) * 100,'%','\n')

    '''
        Prints out info about created cluster.
    '''
    def __info(self, name, number):
        print('    SYMBOL:',[name],'CLUSTER #',number,'\n'
              '        >> ellipsoid radius reduced to:', global_v.SEMI_AXIS_SCALE * 100,'%\n',
              '       >> minimum volume enclosing ellipsoid error:',global_v.MVEE_ERR)
class Cluster:
    """
    This class holds data about single cluster,
    its center and points around it
    """
    def __init__(self,
                 centroid,
                 points,
                 name,
                 number,
                 give_info=True,
                 do_ellipsoid=True,
                 do_cuboid=True):
        if give_info:
            logger.log_header("Created Cluster: " + str([name]) +
                              " Number: #" + str(number),
                              styles=[logger.LogHeaderStyle.SUB_HEADER])

        self.center = centroid
        self.points = points
        if do_cuboid:
            logger.log("Creating Cuboid in Cluster")
            self.cuboid = Cuboid(self.points)
        if do_ellipsoid:
            logger.log("Creating Ellipsoid in Cluster")
            self.ellipsoid = Ellipsoid(self.points, global_v.SEMI_AXIS_SCALE)
            if (global_v.CHAR_NUM == 3):
                self.rejected_x, self.rejected_y, self.rejected_z = self.ellipsoid.is_point_in_ellipsoid(
                    self.points[:])
            else:
                self.rejected_x, self.rejected_y = self.ellipsoid.is_point_in_ellipsoid(
                    self.points[:])
            if give_info:
                self.__info(name, number)
                logger.log('Points in ellipsoid: ' +
                           str((1 - len(self.rejected_x) / len(self.points)) *
                               100) + '%')

    '''
        Prints out info about created cluster.
    '''

    def __info(self, name, number):
        logger.log("Name: " + str([name]) + " Number #" + str(number) + "\n" +
                   'ellipsoid radius reduced to:' +
                   str(global_v.SEMI_AXIS_SCALE * 100) + '%\n' +
                   'minimum volume enclosing ellipsoid error:' +
                   str(global_v.MVEE_ERR))
 def __generate_objects(self, symbolClasses, type, with_test):
     set_of_objects = []
     for cl in symbolClasses:
         # Gather up points from each distorted class
         temp_points = []
         test_tmp = []
         for el in cl.learning_set:
             temp_points.append(el.characteristicsValues[:])
         
         if with_test:
             for el in cl.test_set:
                 test_tmp.append(el.characteristicsValues[:])
             
         if(type == ObjectType.ELLIPSOID):
             ellipsoid = Ellipsoid(temp_points)
             # Check accuracy 
             pointsInEllipsoid = ellipsoid.is_point_in_ellipsoid(temp_points,False, True)
             print("           Points in Ellipsoid:                            ", 100 * len(pointsInEllipsoid)/len(temp_points),"%")
             # Test set
             if with_test:
                 testPoints = ellipsoid.is_point_in_ellipsoid(test_tmp,False, True)
                 f = open(os.path.join("..","log",global_v.DIR_NAME,"TEST_SET_ACCURACY.txt"), 'a')
                 f.write("class [" + str(cl.name)+ "] ")
                 f.write("ellipsoid " + str(100 * len(testPoints)/len(test_tmp))+"%\n")
                 f.close()
                 print("           Test set accuracy:                              ", 100 * len(testPoints)/len(test_tmp),"%")                
             # Save an ellipsoid
             set_of_objects.append(EllipsoidWrap(temp_points,ellipsoid,cl.name))
             
         if(type == ObjectType.CUBOID):
             cuboid = Cuboid(temp_points)
             # Check accuracy
             pointsInCuboid = cuboid.points_in_cuboid(temp_points)
             print("           Points in Cuboid:                               ", 100 * len(pointsInCuboid)/len(temp_points),"%")
             # Test set
             if with_test:
                 testPoints = cuboid.points_in_cuboid(test_tmp)
                 print("           Test set accuracy:                              ", 100 * len(testPoints)/len(test_tmp),"%")   
                 f = open(os.path.join("..","log",global_v.DIR_NAME,"TEST_SET_ACCURACY.txt"), 'a')
                 f.write("class [" + str(cl.name)+ "] ")
                 f.write("cuboid " + str(100 * len(testPoints)/len(test_tmp))+ "%\n")
                 f.close()
             # Save an ellipsoid
             set_of_objects.append(CuboidWrap(temp_points,cuboid, cl.name))
         
     return set_of_objects
def ambiguity_for_different_radiuses(symbolClasses,
                                     foreignClassesHomo=[],
                                     foreignClassesNonHomo=[]):
    global results_data
    results_data = init()

    print("LOLOLOL", results_data)

    for r in range(0, len(radiuses)):
        print("    RADIUS:", radiuses[r])

        #
        # Shrink each ellipsoid
        #
        path = os.path.join("..", "log", global_v.DIR_NAME,
                            "r" + str(radiuses[r]))
        os.makedirs(path, exist_ok=True)
        file = open(
            os.path.join(path, "r" + str(radiuses[r]) + "_shrinking.txt"), 'a')
        print("        SHRINKING ELLIPSOIDS IN EACH CLUSTER")
        for cl in symbolClasses:
            for cluster in cl.clusters:
                # Check if there is need to recalculate
                if (radiuses[r] != global_v.SEMI_AXIS_SCALE):
                    cluster.ellipsoid = Ellipsoid(cluster.points, radiuses[r])

                # list of point in current ellipsoid
                pointsInEllipsoid = cluster.ellipsoid.is_point_in_ellipsoid(
                    cluster.points, False, True)
                print("           Symbol:", [cl.name],
                      " Points in cluster:                ",
                      100 * len(pointsInEllipsoid) / len(cluster.points), "%")
                file.write("Symbol:" + str([cl.name]) +
                           " Points in cluster:                " +
                           str(100 * len(pointsInEllipsoid) /
                               len(cluster.points)) + "%\n")
        file.close()

        #
        # TESTS FOR DIFFERENT SETS
        #
        print("\n        MEMBERSHIP RESULTS [LEARN SET]")
        ambiguity_test(DataInfo.LEARN, r, symbolClasses)
        print("\n        MEMBERSHIP RESULTS [TEST SET]")
        ambiguity_test(DataInfo.TEST, r, symbolClasses)

        #
        # Check ambiguity for each foreign set
        #
        print("\n\n        MEMBERSHIP RESULTS [FOREIGN H**O]")
        foreign_ambiguity_test(DataInfo.H**O, r, foreignClassesHomo,
                               symbolClasses)
        results_data.batch(r).print_matrix(DataInfo.FOREIGN, DataInfo.H**O)
        print("\n\n        MEMBERSHIP RESULTS [FOREIGN NON-H**O]")
        foreign_ambiguity_test(DataInfo.NONHOMO, r, foreignClassesNonHomo,
                               symbolClasses)
        results_data.batch(r).print_matrix(DataInfo.FOREIGN, DataInfo.NONHOMO)
Example #6
0
 def __init__(self, centroid, points, name, number, give_info = True, do_ellipsoid = True, do_cuboid = True):
     self.center = centroid
     self.points = points
     if do_cuboid:
         self.cuboid = Cuboid(self.points)
     if do_ellipsoid:
         self.ellipsoid = Ellipsoid(self.points, global_v.SEMI_AXIS_SCALE)
         if(global_v.CHAR_NUM == 3):    
             self.rejected_x, self.rejected_y, self.rejected_z = self.ellipsoid.is_point_in_ellipsoid(self.points[:])
         else:
             self.rejected_x, self.rejected_y = self.ellipsoid.is_point_in_ellipsoid(self.points[:])
         if give_info:
             self.__info(name, number)
             print('        >> points in ellipsoid:',(1- len(self.rejected_x)/len(self.points)) * 100,'%','\n')
Example #7
0
 def recalculate(self):
     self.ellipsoid = Ellipsoid(self.points)
Example #8
0
    def __generate_objects(self, symbolClasses, type, with_test):
        set_of_objects = []
        for cl in symbolClasses:
            # Gather up points from each distorted class
            temp_points = []
            test_tmp = []
            for el in cl.learning_set:
                temp_points.append(el.characteristicsValues[:])

            if with_test:
                for el in cl.test_set:
                    test_tmp.append(el.characteristicsValues[:])

            if (type == ObjectType.ELLIPSOID):
                ellipsoid = Ellipsoid(temp_points)
                # Check accuracy
                pointsInEllipsoid = ellipsoid.is_point_in_ellipsoid(
                    temp_points, False, True)
                print(
                    "           Points in Ellipsoid:                            ",
                    100 * len(pointsInEllipsoid) / len(temp_points), "%")
                # Test set
                if with_test:
                    testPoints = ellipsoid.is_point_in_ellipsoid(
                        test_tmp, False, True)
                    f = open(
                        os.path.join("..", "log", global_v.DIR_NAME,
                                     "TEST_SET_ACCURACY.txt"), 'a')
                    f.write("class [" + str(cl.name) + "] ")
                    f.write("ellipsoid " +
                            str(100 * len(testPoints) / len(test_tmp)) + "%\n")
                    f.close()
                    print(
                        "           Test set accuracy:                              ",
                        100 * len(testPoints) / len(test_tmp), "%")
                # Save an ellipsoid
                set_of_objects.append(
                    EllipsoidWrap(temp_points, ellipsoid, cl.name))

            if (type == ObjectType.CUBOID):
                cuboid = Cuboid(temp_points)
                # Check accuracy
                pointsInCuboid = cuboid.points_in_cuboid(temp_points)
                print(
                    "           Points in Cuboid:                               ",
                    100 * len(pointsInCuboid) / len(temp_points), "%")
                # Test set
                if with_test:
                    testPoints = cuboid.points_in_cuboid(test_tmp)
                    print(
                        "           Test set accuracy:                              ",
                        100 * len(testPoints) / len(test_tmp), "%")
                    f = open(
                        os.path.join("..", "log", global_v.DIR_NAME,
                                     "TEST_SET_ACCURACY.txt"), 'a')
                    f.write("class [" + str(cl.name) + "] ")
                    f.write("cuboid " +
                            str(100 * len(testPoints) / len(test_tmp)) + "%\n")
                    f.close()
                # Save an ellipsoid
                set_of_objects.append(CuboidWrap(temp_points, cuboid, cl.name))

        return set_of_objects