<commit_before>################################################################################
#                       idea2birds                                             #
#                   Bird simulation in python                                  #
################################################################################
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.animation import FuncAnimation
from matplotlib.collections import PathCollection
import time

width, height, n = 640, 480, 500

def limit(target, upperbound=False, lowerbound=False):
    norm = np.sqrt((target*target).sum(axis=1)).reshape(n,1)
    if upperbound:
        np.multiply(target, upperbound/norm, out=target, where=norm > upperbound)
    if lowerbound:
        np.multiply(target, lowerbound/norm, out=target, where=norm < lowerbound)



class Flock:
    def __init__(self, angle_view=30, max_velocity=1., max_acceleration=0.03):
        angle_view, max_velocity, max_acceleration = float(angle_view)/180*np.pi, float(max_velocity), float(max_acceleration)
        min_velocity = 0.1

        angles = np.random.uniform(0,2*np.pi,n)
        self.velocity = (np.array([np.cos(angles),np.sin(angles)])*np.random.uniform(min_velocity,max_velocity,n)).astype(np.float32).T
        self.position = np.random.rand(n,2).astype(np.float32)*[width,height]
        #self.position = np.array([[width/2+15,height/2],[width/2-15,height/2]],dtype=np.float32)


        self.max_velocity = max_velocity
        self.min_velocity = min_velocity
        self.max_acceleration = max_acceleration
        self.angle_view = angle_view

        
        print('velocity:')
        print(self.velocity)
        print('velocity ende')
        

    def run(self):
        self.distance = self.calc_distance() 

        
        mask_0 = np.absolute(np.arctan2(self.dy, self.dx) - np.arctan2(self.velocity[:,1],self.velocity[:,0])) < self.angle_view/2
        mask_0 *= (self.distance > 0)
        mask_1 = (self.distance < 25)
        mask_2 = (self.distance < 50)
        mask_1 *= mask_0
        mask_2 *= mask_0
        mask_3 = mask_2

        mask_1_count = np.maximum(mask_1.sum(axis=1), 1).reshape(n,1)
        mask_2_count = np.maximum(mask_2.sum(axis=1), 1).reshape(n,1)
        mask_3_count = mask_2_count

        separation = self.calc_separation(mask_1,mask_1_count)
        alignment = self.calc_alignment(mask_2,mask_2_count)
        cohesion = self.calc_cohesion(mask_3,mask_3_count)


        acceleration = 1.5 * separation + alignment + cohesion
        #acceleration = 1.5 * separation
        #acceleration = alignment
        #acceleration = cohesion
        self.velocity += acceleration
        self.random_turn()
        limit(self.velocity, self.max_velocity, self.min_velocity)
        self.position += self.velocity
        self.position %= [width, height]


    def calc_distance(self):
        """
        Calculate distance between birds. Return n x n array with each row
        corresponding to each bird's distance to the others
        """
        # subtract each element of the position array from the others
        self.dx = np.absolute(np.subtract.outer(self.position[:, 0], self.position[:, 0]))
        self.dy = np.absolute(np.subtract.outer(self.position[:, 1], self.position[:, 1]))
        
        # wrap around
        np.subtract(width+1, self.dx, out=self.dx, where=self.dx > width/2)
        np.subtract(height+1, self.dy, out=self.dy, where=self.dy > height/2)

        # return hypotenuse of corresponding elements i.e. distance
        return np.hypot(self.dx, self.dy)

    def calc_alignment(self, mask, count):
        # Compute the average velocity of local neighbours
        target = np.dot(mask, self.velocity)/count

        # Compute steering
        norm = np.sqrt((target*target).sum(axis=1)).reshape(n, 1)
        target = self.max_velocity * np.divide(target, norm, out=target, where=norm != 0)
        target -= self.velocity

        limit(target, self.max_acceleration)
        return target


    def calc_cohesion(self, mask, count):
        # Compute the gravity center of local neighbours
        center = np.dot(mask, self.position)/count
        
        # Compute direction toward the center
        target = center - self.position
        
        # Normalize the result
        norm = np.sqrt((target*target).sum(axis=1)).reshape(n, 1)
        target = self.max_velocity * np.divide(target, norm, out=target, where=norm != 0)
        
        # Compute the resulting steering
        target -= self.velocity

        limit(target, self.max_acceleration)
        return target


    def calc_separation(self, mask, count):
        # Compute the repulsion force from local neighbours
        repulsion = np.dstack((self.dx, self.dy))
        
        # Force is inversely proportional to the distance
        repulsion = np.divide(repulsion, self.distance.reshape(n, n, 1)**2, out=repulsion, where=self.distance.reshape(n, n, 1) != 0)
        
        # Compute direction away from others
        target = (repulsion*mask.reshape(n, n, 1)).sum(axis=1)/count
        
        # Normalize the result
        norm = np.sqrt((target*target).sum(axis=1)).reshape(n, 1)
        target = self.max_velocity * np.divide(target, norm, out=target, where=norm != 0)
        
        # Separation at constant speed (max_velocity)
        target *= self.max_velocity
        
        # Compute the resulting steering
        target -= self.velocity

        limit(target, self.max_acceleration)
        return target

    def random_turn(self):
        angles = np.random.normal(0,self.angle_view*0.1,n)
        c = np.cos(angles)
        s = np.sin(angles)
        self.velocity[:,0] = self.velocity[:,0]*c - self.velocity[:,1]*s
        self.velocity[:,1] = self.velocity[:,1]*c + self.velocity[:,0]*s
        return
        


<commit_msg>bugfix<commit_after>################################################################################
#                       idea2birds                                             #
#                   Bird simulation in python                                  #
################################################################################
import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.path import Path
from matplotlib.animation import FuncAnimation
from matplotlib.collections import PathCollection
import time

width, height, n = 640, 480, 500

def limit(target, upperbound=False, lowerbound=False):
    norm = np.sqrt((target*target).sum(axis=1)).reshape(n,1)
    if upperbound:
        np.multiply(target, upperbound/norm, out=target, where=norm > upperbound)
    if lowerbound:
        np.multiply(target, lowerbound/norm, out=target, where=norm < lowerbound)



class Flock:
    def __init__(self, angle_view=30, max_velocity=1., max_acceleration=0.03):
        angle_view, max_velocity, max_acceleration = float(angle_view)/180*np.pi, float(max_velocity), float(max_acceleration)
        min_velocity = 0.1

        angles = np.random.uniform(0,2*np.pi,n)
        self.velocity = (np.array([np.cos(angles),np.sin(angles)])*np.random.uniform(min_velocity,max_velocity,n)).astype(np.float32).T
        self.position = np.random.rand(n,2).astype(np.float32)*[width,height]
        #self.position = np.array([[width/2+15,height/2],[width/2-15,height/2]],dtype=np.float32)


        self.max_velocity = max_velocity
        self.min_velocity = min_velocity
        self.max_acceleration = max_acceleration
        self.angle_view = angle_view

        
        print('velocity:')
        print(self.velocity)
        print('velocity ende')
        

    def run(self):
        self.distance = self.calc_distance() 

        
        #mask_view = np.absolute(np.arctan2(self.dy, self.dx) - np.arctan2(self.velocity[:,1],self.velocity[:,0])) < self.angle_view/2
        mask_0 = (self.distance > 0)
        mask_1 = (self.distance < 25)
        mask_2 = (self.distance < 50)
        mask_1 *= mask_0
        mask_2 *= mask_0#*mask_view
        mask_3 = mask_2

        mask_1_count = np.maximum(mask_1.sum(axis=1), 1).reshape(n,1)
        mask_2_count = np.maximum(mask_2.sum(axis=1), 1).reshape(n,1)
        mask_3_count = mask_2_count

        separation = self.calc_separation(mask_1,mask_1_count)
        alignment = self.calc_alignment(mask_2,mask_2_count)
        cohesion = self.calc_cohesion(mask_3,mask_3_count)


        acceleration = 1.5 * separation + alignment + cohesion
        #acceleration = 1.5 * separation
        #acceleration = alignment
        #acceleration = cohesion
        self.velocity += acceleration
        self.random_turn()
        limit(self.velocity, self.max_velocity, self.min_velocity)
        self.position += self.velocity
        self.position %= [width, height]


    def calc_distance(self):
        """
        Calculate distance between birds. Return n x n array with each row
        corresponding to each bird's distance to the others
        """
        # subtract each element of the position array from the others
        self.dx = np.subtract.outer(self.position[:, 0], self.position[:, 0])
        self.dy = np.subtract.outer(self.position[:, 1], self.position[:, 1])
        dx = np.absolute(self.dx)
        dy = np.absolute(self.dy)
        
        # wrap around
        np.subtract(width+1, dx, out=dx, where=dx > width/2)
        np.subtract(height+1, dy, out=dy, where=dy > height/2)

        # return hypotenuse of corresponding elements i.e. distance
        return np.hypot(dx, dy)

    def calc_alignment(self, mask, count):
        # Compute the average velocity of local neighbours
        target = np.dot(mask, self.velocity)/count

        # Compute steering
        norm = np.sqrt((target*target).sum(axis=1)).reshape(n, 1)
        target = self.max_velocity * np.divide(target, norm, out=target, where=norm != 0)
        target -= self.velocity

        limit(target, self.max_acceleration)
        return target


    def calc_cohesion(self, mask, count):
        # Compute the gravity center of local neighbours
        center = np.dot(mask, self.position)/count
        
        # Compute direction toward the center
        target = center - self.position
        
        # Normalize the result
        norm = np.sqrt((target*target).sum(axis=1)).reshape(n, 1)
        target = self.max_velocity * np.divide(target, norm, out=target, where=norm != 0)
        
        # Compute the resulting steering
        target -= self.velocity

        limit(target, self.max_acceleration)
        return target


    def calc_separation(self, mask, count):
        # Compute the repulsion force from local neighbours
        repulsion = np.dstack((self.dx, self.dy))
        
        # Force is inversely proportional to the distance
        np.divide(repulsion, self.distance.reshape(n, n, 1)**2, out=repulsion, where=self.distance.reshape(n, n, 1) != 0)
        
        # Compute direction away from others
        target = (repulsion*mask.reshape(n, n, 1)).sum(axis=1)/count
        
        # Normalize the result
        norm = np.sqrt((target*target).sum(axis=1)).reshape(n, 1)
        target = self.max_velocity * np.divide(target, norm, out=target, where=norm != 0)
        
        # Compute the resulting steering
        target -= self.velocity

        limit(target, self.max_acceleration)
        return target

    def random_turn(self):
        angles = np.random.normal(0,self.angle_view*0.1,n)
        c = np.cos(angles)
        s = np.sin(angles)
        self.velocity[:,0] = self.velocity[:,0]*c - self.velocity[:,1]*s
        self.velocity[:,1] = self.velocity[:,1]*c + self.velocity[:,0]*s
        return
        



{'fraction_non_alphanumeric': 0.14012901035477848, 'fraction_numerical': 0.019266677983364455, 'mean_word_length': 2.5512356841470765, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '707773', 'n_tokens_mistral': 3769, 'n_tokens_neox': 3469, 'n_words': 752}