def import_sampled_points(self, annotation_set, import_data):
        """ create annotation points with information from uploaded CSV and add to this annotation
            set """

        from projects.models import PointAnnotation

        images = annotation_set.images.all()
        points_to_bulk_save = []

        # iterate through the images and create points
        for image in images:
            for annotation in import_data[str(image.deployment.id)][image.image_name]:

                point_annotation = PointAnnotation()
                point_annotation.annotation_set = annotation_set
                point_annotation.image = image
                point_annotation.owner = annotation_set.owner
                point_annotation.x = annotation['Point in Image'].split(',')[0]
                point_annotation.y = annotation['Point in Image'].split(',')[1]

                point_annotation.annotation_caab_code = annotation['Annotation Code']
                point_annotation.qualifier_short_name = annotation['Qualifier Name']

                point_annotation.annotation_caab_code_secondary = annotation['Annotation Code 2']
                point_annotation.qualifier_short_name_secondary = annotation['Qualifier Name 2']

                #point_annotation.save()
                points_to_bulk_save.append(point_annotation)

        # do the bulk save - for performance
        PointAnnotation.objects.bulk_create(points_to_bulk_save)
    def apply_fixed_five_points(self, annotation_set):
        """ 5 points based on AIMS standard """

        from projects.models import PointAnnotation

        images = annotation_set.images.all()
        points_to_bulk_save = []

        # create the grid
        row_points = [0.25, 0.25, 0.5, 0.75, 0.75]
        column_points = [0.25, 0.75, 0.5, 0.75, 0.25]

        # apply the points to the images
        for image in images:
            for i in range(int(5)):
                    point_annotation = PointAnnotation()

                    point_annotation.annotation_set = annotation_set
                    point_annotation.image = image
                    point_annotation.owner = annotation_set.owner
                    point_annotation.x = row_points[i]
                    point_annotation.y = column_points[i]

                    point_annotation.annotation_caab_code = ""
                    point_annotation.qualifier_short_name = ""

                    point_annotation.annotation_caab_code_secondary = ""
                    point_annotation.qualifier_short_name_secondary = ""

                    #point_annotation.save()
                    points_to_bulk_save.append(point_annotation)

        # do the bulk save - for performance
        PointAnnotation.objects.bulk_create(points_to_bulk_save)
    def apply_random_sampled_points(self, annotation_set, sample_size):
        """ Randomly apply points to the images attached to this annotation
            set """

        from projects.models import PointAnnotation

        images = annotation_set.images.all()
        points_to_bulk_save = []

        # iterate through the images and create points
        for image in images:
            for i in range(int(sample_size)):

                point_annotation = PointAnnotation()

                point_annotation.annotation_set = annotation_set
                point_annotation.image = image
                point_annotation.owner = annotation_set.owner
                point_annotation.x = random.uniform(0.008, 0.992) #random.random()
                point_annotation.y = random.uniform(0.008, 0.992)

                point_annotation.annotation_caab_code = ""
                point_annotation.qualifier_short_name = ""

                point_annotation.annotation_caab_code_secondary = ""
                point_annotation.qualifier_short_name_secondary = ""

                #point_annotation.save()
                points_to_bulk_save.append(point_annotation)

        # do the bulk save - for performance
        PointAnnotation.objects.bulk_create(points_to_bulk_save)
    def apply_uniform_grid_points(self, annotation_set, sample_size):
        """ Apply a uniform grid of points to an image. """

        from projects.models import PointAnnotation

        images = annotation_set.images.all()
        points_to_bulk_save = []

        # take the square root of the sample size and round
        square = math.sqrt(int(sample_size))
        rows = columns = round(square)

        # +1 to the rows and cols
        rows += 1
        columns += 1

        # create the grid
        row_points = np.linspace(0.008, 0.992, num=rows, endpoint=False)
        column_points = np.linspace(0.008, 0.992, num=columns, endpoint=False)

        # pop the first item from the arrays - we do this so we get an even spacing excluding edges
        row_points = np.delete(row_points, 0)
        column_points = np.delete(column_points, 0)

        # apply the points to the images
        for image in images:
            for row in row_points:
                for column in column_points:

                    point_annotation = PointAnnotation()

                    point_annotation.annotation_set = annotation_set
                    point_annotation.image = image
                    point_annotation.owner = annotation_set.owner
                    point_annotation.x = row
                    point_annotation.y = column

                    point_annotation.annotation_caab_code = ""
                    point_annotation.qualifier_short_name = ""

                    point_annotation.annotation_caab_code_secondary = ""
                    point_annotation.qualifier_short_name_secondary = ""

                    #point_annotation.save()
                    points_to_bulk_save.append(point_annotation)

        # do the bulk save - for performance
        PointAnnotation.objects.bulk_create(points_to_bulk_save)