Пример #1
0
    def test_display_cluster(self):
        ''' Test errors for display_cluster method '''
        # Create a cluster
        cluster_test = Cluster()
        point = ThreeDimensionalPoint(1, 2, 3)
        cluster_test.append(point)
        # Create a correct arg1
        color = 'black'
        # Create a correct arg2
        delay = 1

        # 1. Test response to wrong type
        # 1.1  arg1 of correct type, arg2 of wrong type
        self.assertRaises(TypeError, cluster_test.display_cluster, color, 'a')
        # 1.2 arg1 of wrong type, arg2 of correct type
        self.assertRaises(TypeError, cluster_test.display_cluster, 5, delay)

        # 2. Test for special circumstances
        # 2.1 delay not 0 or 1
        delay = 15
        self.assertRaises(ValueError, cluster_test.display_cluster, color,
                          delay)

        # 3. Test delay 0
        delay = 0
        self.assertIsNone(cluster_test.display_cluster(color, delay))
Пример #2
0
    def test_create_array(self):
        ''' Test errors for create_xyz_array '''
        # Create a cluster
        cluster = Cluster()
        point = ThreeDimensionalPoint(1, 2, 3)
        for _ in range(15):
            cluster.append(point)
        # Create an empty cluster
        empty_cluster = Cluster()
        # Create wrong cluster
        wrong_cluster = Cluster()
        wrong_cluster.append(1)

        # Create a correct key
        key = 1
        # Create a wrong valued key
        wrong_key = 15
        # Create a wrong type of key
        string_key = 'a'

        # 1. Test for different clusters
        # 1.1 Test response to correct cluster
        self.assertIsNotNone(cluster.create_xyz_array, key)
        # 1.2. Test for wrong cluster
        self.assertRaises(TypeError, wrong_cluster.create_xyz_array, key)
        # 1.3 Test response to empty cluster
        self.assertRaises(ValueError, empty_cluster.create_xyz_array, key)

        # 2. Test for different keys
        # 2.1 Test response to wrong valued key
        self.assertRaises(ValueError, cluster.create_xyz_array, wrong_key)
        # 2.2 Test response to wrong type of key
        self.assertRaises(TypeError, cluster.create_xyz_array, string_key)
def randomize_3d_points(dimensions, data_size):
    '''
    Randomizes points
    dimensions    -- the desired dimensions that the points will have
    data_sze      -- the desired number of points
    Returns an array of points
    '''
    if not isinstance(dimensions, int) or not isinstance(data_size, int):
        raise TypeError(
            "The dimension and number of datas must be integers!(random_3d_points)")
    object_array = Cluster()
    for _ in range(data_size):
        temporary_x = random.randint(0, dimensions)
        temporary_y = random.randint(0, dimensions)
        temporary_z = random.randint(0, dimensions)
        temporary_object = ThreeDimensionalPoint(
            temporary_x, temporary_y, temporary_z)
        object_array.append(temporary_object)
    return object_array
Пример #4
0
    def test_inputs(self):
        ''' Check if type errors and value errors are raised correctly '''

        # Create a correct arg2
        cluster_array = []
        # Create a correct arg1
        data = Cluster()
        point = ThreeDimensionalPoint(1, 2, 3)
        for _ in range(50):
            data.append(point)
        # Create a correct arg3
        distance = 15

        # 1. Check if type errors is raised if arguments are of wrong type
        # 1.1 arg1 of wrong, arg2 and arg3 of correct type
        self.assertRaises(TypeError, create_double_clusters, 'a',
                          cluster_array, distance)
        # 1.2 arg2 of wrong type, arg1 and arg3 of correct type
        self.assertRaises(TypeError, create_double_clusters, data, 15,
                          distance)
        # 1.3 arg3 of wrong type, arg1 and arg2 of correct type
        self.assertRaises(TypeError, create_double_clusters, data,
                          cluster_array, 'b')

        # 2. Check for special circumstances
        # 2.1 arg1 empty cluster
        empty_cluster = Cluster()
        self.assertRaises(ValueError, create_double_clusters, empty_cluster,
                          cluster_array, distance)
        # 2.2 arg2 list with some elements
        arg2_list = [1, 2, 3]
        self.assertRaises(ValueError, create_double_clusters, data, arg2_list,
                          distance)
        # 2.3 corrupt data array
        data_array = Cluster()
        data_array.append(1)
        data_array.append(3)
        self.assertRaises(TypeError, create_double_clusters, data_array,
                          cluster_array, distance)

        # 3. Check response to valid input
        create_double_clusters(data, cluster_array, 100)
        for item in cluster_array:
            self.assertIsInstance(item, Cluster)
def create_double_clusters(data_array, cluster_array, max_clustering_distance):
    '''
    Creates clusters with size two
    data_array     -- The raw data that will be clustered
    cluster_array  -- The clusters will be stored here
    max_clustering_distance -- The maximum distance between two points that will be clustered
    '''
    # Check data_array
    if isinstance(data_array, Cluster):
        if len(data_array) <= 1:
            raise ValueError(
                "There are less than 2 points!(create_double_clusters)")
        for point in data_array:
            if not isinstance(point, ThreeDimensionalPoint):
                raise TypeError("data_array must only contain points!(create_double_clusters)")
    else:
        raise TypeError("data_array must be a cluster!(create_double_clusters)")
    # Check cluster array
    if isinstance(cluster_array, list):
        if len(cluster_array) >= 1:
            raise ValueError("Cluster array must be empty!(create_double_clusters)")
    else:
        raise TypeError("Cluster array must be a list!(create_double_clusters)")
    # Check max_clustering_distance
    if not isinstance(max_clustering_distance, int):
        raise TypeError("max_clustering distance must be int(create_double_clusters)")


    index_array = []
    for counter_one in range(len(data_array)):
        # If counter_one is already in a cluster, continue
        if counter_one in index_array:
            continue
        # Initialize the minimum distance to compare
        min_distance = max_clustering_distance
        # first_point
        point_one = data_array[counter_one]
        for counter_two in range(counter_one + 1, len(data_array)):
            # If t is already in a cluster, continue
            if counter_two in index_array:
                continue
            # second_point
            point_two = data_array[counter_two]
            # Calculate distance between first_point and second_point
            distance = calculate_distance(point_one, point_two)
            # Check to see if it is the shortest possible distance
            if distance < min_distance:
                # If it is save t in order to use later on, and update the shortest distance
                index_second_point = counter_two
                min_distance = distance
        # If minimum distance is within the accepted range
        if min_distance < max_clustering_distance:
            # 1:Take second point. 2: make first_point and second_point into a cluster.
            # 3: add this cluster to the cluster array
            point_two = data_array[index_second_point]
            temporary_cluster = Cluster()
            temporary_cluster.append(point_one)
            temporary_cluster.append(point_two)
            cluster_array.append(temporary_cluster)
            # Save the indexes of both points in order to delete them once everything is finished
            index_array.append(counter_one)
            index_array.append(index_second_point)
    # Delete indexes from raw data if there are indexes to be deleted
    if index_array:
        data_array = delete_indexes(data_array, index_array)
def merge_clusters(cluster_array, max_merging_distance):
    '''
    Merges clusters until they can't be merged anymore
    cluster_array -- The clusters that will be merged
    max_merging_distance   -- The maximum distance in which the clusters will be merged
    '''


    # Check cluster_array
    if isinstance(cluster_array, list):
        if len(cluster_array) <= 1:
            raise ValueError("Cluster array is empty or has 1 item!(merge_clusters)")
        for cluster in cluster_array:
            if not isinstance(cluster, Cluster):
                raise TypeError("Cluster array must contain clusters!(merge_clusters)")
            for point in cluster:
                if not isinstance(point, ThreeDimensionalPoint):
                    raise TypeError("Corrupt cluster!(merge_clusters)")
    else:
        raise TypeError("cluster_array must be a list!(merge_clusters)")
    # Check max_merging_distance
    if not isinstance(max_merging_distance, int):
        raise TypeError("max_merging_distance must be int!(merge_clusters)")

    # This code will run until there are no changes made to the cluster array
    while True:
        cluster_array_temporary = []
        index_array = []
        # Take cluster1
        for counter_one in range(len(cluster_array)):
            #Take first cluster
            cluster_one = cluster_array[counter_one]
            # Take first_point
            for first_point in cluster_one:
                # Start from next index to avoid comparing with itself Take cluster2
                for counter_two in range(counter_one + 1, len(cluster_array)):
                    cluster_two = cluster_array[counter_two]
                    # Take second_point
                    for second_point in cluster_two:
                        # Calculate the distance
                        distance = calculate_distance(
                            first_point, second_point)
                        # If distance is ok merge two clusters and add them to the array
                        if distance <= max_merging_distance:
                            temporary_cluster = Cluster()
                            temporary_cluster.extend(cluster_one)
                            temporary_cluster.extend(cluster_two)
                            cluster_array_temporary.append(temporary_cluster)
                            index_array.append(counter_one)
                            index_array.append(counter_two)
                            break
                    else:
                        continue
                    break
                else:
                    continue
                break
            else:
                continue
            break
        if index_array:
            cluster_array = delete_indexes(cluster_array, index_array)
            cluster_array.extend(cluster_array_temporary)
        else:
            break
Пример #7
0
 def test_inputs(self):
     ''' Check if type errors and value errors are raised correctly '''
     # 1. Check response to wrong type
     self.assertRaises(TypeError, print_clusters, 15)
     # 2. Check response to an empty list
     data = []
     self.assertRaises(ValueError, print_clusters, data)
     # 3. Check response to a cluster
     data = Cluster()
     point = ThreeDimensionalPoint(1, 2, 3)
     data.append(point)
     data.append(point)
     self.assertRaises(TypeError, print_clusters, data)
     # 4. Check response to a random list
     data = [0, 1, 2, 3, 4, 5]
     self.assertRaises(TypeError, print_clusters, data)
     # 5. Check response to valid input
     point = ThreeDimensionalPoint(1, 2, 3)
     cluster_array = []
     cluster_one = Cluster()
     cluster_one.append(point)
     cluster_one.append(point)
     cluster_two = Cluster()
     cluster_two.append(point)
     cluster_two.append(point)
     cluster_array.append(cluster_one)
     cluster_array.append(cluster_two)
     self.assertIsNone(print_clusters(cluster_array))
     # 6. data contains corrupt cluster
     point = ThreeDimensionalPoint(1, 2, 3)
     cluster_array = []
     cluster_one = Cluster()
     cluster_one.append(point)
     cluster_one.append(point)
     cluster_two = Cluster()
     cluster_two.append(point)
     cluster_two.append(point)
     corrupt_cluster = Cluster()
     corrupt_cluster.append(1)
     cluster_array.append(cluster_one)
     cluster_array.append(cluster_two)
     cluster_array.append(corrupt_cluster)
     self.assertRaises(TypeError, print_clusters, cluster_array)
Пример #8
0
    def test_inputs(self):
        ''' Check if type errors and value errors are raised correctly '''
        point = ThreeDimensionalPoint(1, 2, 3)
        # Create a correct arg1
        data = Cluster()
        data.append(point)
        data.append(point)
        # Create a correct arg2
        cluster_array = []
        cluster_one = Cluster()
        cluster_one.append(point)
        cluster_one.append(point)
        cluster_two = Cluster()
        cluster_two.append(point)
        cluster_two.append(point)
        cluster_array.append(cluster_one)
        cluster_array.append(cluster_two)
        # Create a correct arg3
        size = 5

        # 1. Check if type errors is raised if arguments are of wrong type
        # 1.1 arg1 of wrong, arg2 and arg3 of correct type
        self.assertRaises(TypeError, clear_small_clusters, 'a', cluster_array,
                          size)
        # 1.2 arg2 of wrong type, arg1 and arg3 of correct type
        self.assertRaises(TypeError, clear_small_clusters, data, 15, size)
        # 1.3 arg3 of wrong type, arg1 and arg2 of correct type
        self.assertRaises(TypeError, clear_small_clusters, data, cluster_array,
                          'b')

        # 2. Check for special circumstances
        # 2.1 arg2 empty list
        empty_list = []
        self.assertRaises(ValueError, clear_small_clusters, data, empty_list,
                          size)
        # 2.2 data array cluster but contains non point member
        wrong_data = Cluster()
        wrong_data.append(1)
        self.assertRaises(TypeError, clear_small_clusters, wrong_data,
                          cluster_array, size)
        # 2.3 Cluster array contains non cluster item
        wrong_cluster_array = [1, 2]
        self.assertRaises(TypeError, clear_small_clusters, data,
                          wrong_cluster_array, size)
        # 2.4 Cluster array contains a cluster with non point items
        wrong_cluster_array = []
        wrong_cluster_array.append(wrong_data)
        self.assertRaises(TypeError, clear_small_clusters, data,
                          wrong_cluster_array, size)

        # 3. Check response to correct input
        clear_small_clusters(data, cluster_array, 5)
        for item in data:
            self.assertIsInstance(item, ThreeDimensionalPoint)
Пример #9
0
 def test_inputs(self):
     ''' Check if type errors and value errors are raised correctly '''
     # 1. Test response to wrong type
     point = ThreeDimensionalPoint(1, 2, 3)
     cluster_array = []
     cluster_one = Cluster()
     cluster_one.append(point)
     cluster_one.append(point)
     cluster_two = Cluster()
     cluster_two.append(point)
     cluster_two.append(point)
     cluster_array.append(cluster_one)
     cluster_array.append(cluster_two)
     # 0. Test for correct types
     # 0.1 Test for a cluster
     self.assertIsNone(display_image(cluster_one, 'blue'))
     # 0.2 Test for a list of clusters
     self.assertIsNone(display_image(cluster_array, 'blue'))
     # 1.1  arg1 of correct type, arg2 of wrong type
     self.assertRaises(TypeError, display_image, cluster_one, 5)
     # 1.2 arg1 of wrong type, arg2 of correct type
     self.assertRaises(TypeError, display_image, 5, 'blue')
     # 2. arg1 must be a cluster, a list of clusters or a point
     # 2.1 Test empty cluster
     cluster = Cluster()
     self.assertRaises(ValueError, display_image, cluster, 'blue')
     # 2.2 Test empty list
     data = []
     self.assertRaises(ValueError, display_image, data, 'blue')
     # 2.3 Test random list
     data = [1, 2, 3, 4, 5]
     self.assertRaises(TypeError, display_image, data, 'blue')
     # 2.4 Test corrupt cluster
     cluster = Cluster()
     cluster.append(1)
     self.assertRaises(TypeError, display_image, cluster, 'blue')
Пример #10
0
    def test_inputs(self):
        ''' Check if type errors and value errors are raised correctly '''
        # Create correct arg1
        cluster_array = []
        point = ThreeDimensionalPoint(1, 2, 3)
        cluster_one = Cluster()
        cluster_one.append(point)
        cluster_one.append(point)
        cluster_two = Cluster()
        cluster_two.append(point)
        cluster_two.append(point)
        cluster_array.append(cluster_one)
        cluster_array.append(cluster_two)
        # Create correct arg2
        distance = 15

        # 1. Check if type errors is raised if arguments are of wrong type
        # 1.1 arg1 of wrong, arg2 of correct type
        self.assertRaises(TypeError, merge_clusters, cluster_array, 'a')
        # 1.2 arg2 of wrong type, arg1 of correct type
        self.assertRaises(TypeError, merge_clusters, 'b', distance)

        # 2. Check for special circumstances
        # 2.1 arg1 empty list
        empty_list = []
        self.assertRaises(ValueError, merge_clusters, empty_list, distance)
        # 2.2 Cluster array contains non cluster item
        wrong_cluster_array = [1, 2]
        self.assertRaises(TypeError, merge_clusters, wrong_cluster_array,
                          distance)
        # 2.3 Cluster array contains corrupt cluster
        wrong_cluster = Cluster()
        wrong_cluster.append(1)
        wrong_cluster_array = []
        wrong_cluster_array.append(wrong_cluster)
        wrong_cluster_array.append(wrong_cluster)
        self.assertRaises(TypeError, merge_clusters, wrong_cluster_array,
                          distance)

        # 3. Check response to correct input
        merge_clusters(cluster_array, distance)
        for item in cluster_array:
            self.assertIsInstance(item, Cluster)
Пример #11
0
    def test_inputs(self):
        ''' Check if type errors and value errors are raised correctly '''
        # Create a correct arg1
        data = Cluster()
        point = ThreeDimensionalPoint(1, 2, 3)
        data.append(point)
        data.append(point)
        # Create a correct arg2
        cluster_array = []
        cluster_one = Cluster()
        cluster_one.append(point)
        cluster_one.append(point)
        cluster_two = Cluster()
        cluster_two.append(point)
        cluster_two.append(point)
        cluster_array.append(cluster_one)
        cluster_array.append(cluster_two)
        # Create a correct arg3
        distance = 15

        # 1. Check if type errors is raised if arguments are of wrong type
        # 1.1 arg1 of wrong, arg2 and arg3 of correct type
        self.assertRaises(TypeError, add_to_clusters, 'a', cluster_array,
                          distance)
        # 1.2 arg2 of wrong type, arg1 and arg3 of correct type
        self.assertRaises(TypeError, add_to_clusters, data, 15, distance)
        # 1.3 arg3 of wrong type, arg1 and arg2 of correct type
        self.assertRaises(TypeError, add_to_clusters, data, cluster_array, 'b')

        # 2. Check for special circumstances
        # 2.1 arg1 empty cluster
        empty_cluster = Cluster()
        self.assertRaises(ValueError, add_to_clusters, empty_cluster,
                          cluster_array, distance)
        # 2.2 arg2 empty list
        empty_list = []
        self.assertRaises(ValueError, add_to_clusters, data, empty_list,
                          distance)
        # 2.3 arg2 list with some elements
        arg2_list = [1, 2, 3]
        self.assertRaises(TypeError, add_to_clusters, data, arg2_list,
                          distance)
        # 2.4 arg1 list with some elements
        arg1_list = [1, 2, 3]
        self.assertRaises(TypeError, add_to_clusters, arg1_list, cluster_array,
                          distance)
        # 2.5 data array contains non point member
        wrong_data_array = Cluster()
        wrong_data_array.append(1)
        self.assertRaises(TypeError, add_to_clusters, wrong_data_array,
                          cluster_array, distance)
        # 2.6 cluster array contains corrupt cluster
        corrupt_cluster = Cluster()
        corrupt_cluster.append(1)
        wrong_cluster_array = []
        wrong_cluster_array.append(corrupt_cluster)
        self.assertRaises(TypeError, add_to_clusters, data,
                          wrong_cluster_array, distance)

        # 3. Check for valid inputs
        add_to_clusters(data, cluster_array, 50)
        for item in cluster_array:
            self.assertIsInstance(item, Cluster)