<commit_before>import numpy
import numpy.linalg


def perpedndicular1(v):
    """calculate the perpendicular unit vector"""
    return numpy.array((-v[1], v[0])) / numpy.linalg.norm((-v[1], v[0]))

def normalize(v):
    return v/numpy.linalg.norm(v)


class Branch(object):
    """
    Represent a dendrite branch, or part of a dendrite branch.
    Provide functionality for splitting, drawing a polygon and iterating
    over segments.
    """

    def __init__(self,data):
        if len(data) < 2:
            raise ValueError("Data length is to small, need at least a quadrilateral!")
        self.__data = data

    def __getitem__(self,key):
        return self.__data[key]

    @property
    def corners(self):
        """
        Nx2x2 array, where N is the number of corners.
        The second dimension is for left and right corner.
        The last dimension holds x,y values.
        """

        if "corners" in self.__data.dtype.fields:
            return self['corners']

        # the corners of the polygons for each element of the segment
        corners = numpy.empty(shape = (len(self.__data),2,2),dtype = float)

        # handle first and last element
        c0 = numpy.array((self['x'][0],self['y'][0]))
        c1 = numpy.array((self['x'][1],self['y'][1]))

        corners[0,0] = c0 + perpedndicular1(c1-c0)*self['radius'][0]
        corners[0,1] = c0 - perpedndicular1(c1-c0)*self['radius'][0]

        c0 = numpy.array((self['x'][-1],self['y'][-1]))
        c1 = numpy.array((self['x'][-2],self['y'][-2]))

        corners[-1,0] = c0 + perpedndicular1(-c1+c0)*self['radius'][-1]
        corners[-1,1] = c0 - perpedndicular1(-c1+c0)*self['radius'][-1]

        # handle the intermediate segments
        for i,(s0,s1,s2) in enumerate(zip(self[0:-2],self[1:-1],self[2:])):
            c0 = numpy.array((s0['x'],s0['y']))
            c1 = numpy.array((s1['x'],s1['y']))
            c2 = numpy.array((s2['x'],s2['y']))

            # perpendicular unit vectors
            pv01 = perpedndicular1(c1-c0)
            pv12 = perpedndicular1(c2-c1)

            corners[i+1,0] = c1+s1['radius']*normalize((pv01+pv12)/2.)
            corners[i+1,1] = c1-s1['radius']*normalize((pv01+pv12)/2.)
        return corners

    @property
    def outline(self):
        """
        Return the corners of the branch in such order that they encode a polygon.
        """
        return numpy.row_stack((self.corners[:,0,:],self.corners[::-1,1,:]))

    @property
    def length(self):
        dx = self['x'][1:] - self['x'][:-1]
        dy = self['y'][1:] - self['y'][:-1]
        return numpy.sqrt(dx**2 + dy**2).sum()

    def split(self, nsegments = 2, length = None, k = 1, s = 0):
        """Split the branch into n subbranches"""
        if length is None:
            sublength = self.length/nsegments # the target length of the segments
        else:
            sublength = float(length)

        # get the data that requires interpolation
        x,y,z = self['x'],self['y'],self['z'] #,self['radius']
        lcx = self.corners[:,0,0]
        lcy = self.corners[:,0,1]
        rcx = self.corners[:,1,0]
        rcy = self.corners[:,1,1]

        # the cumulative length
        t = numpy.r_[0,numpy.cumsum(((x[:-1]-x[1:])**2 + (y[:-1]-y[1:])**2)**.5)]

        # create interpolation coefficients
        import scipy.interpolate
        splinecoeffs, u = scipy.interpolate.splprep([x,y,z,lcx,lcy,rcx,rcy], u = t, k = k, s = s)


        # create new parametrization array. It is supposed to consist of the old lengths
        # plus points that cut the length into the proper segment size
        tnew    = t.tolist()
        # the indices where to split the resulting tnew
        indices = [0]
        import bisect
        for n in range(1,int(self.length/sublength)+1):
            index = bisect.bisect_left(tnew,n*sublength)
            indices.append(index)
            bisect.insort_left(tnew,n*sublength)

        # append the end index for the last segment
        indices.append(len(tnew))
<commit_msg>fixed brach, to only create last segment on split if length larger eps<commit_after>import numpy
import numpy.linalg


def perpedndicular1(v):
    """calculate the perpendicular unit vector"""
    return numpy.array((-v[1], v[0])) / numpy.linalg.norm((-v[1], v[0]))

def normalize(v):
    return v/numpy.linalg.norm(v)


class Branch(object):
    """
    Represent a dendrite branch, or part of a dendrite branch.
    Provide functionality for splitting, drawing a polygon and iterating
    over segments.
    """

    def __init__(self,data):
        if len(data) < 2:
            raise ValueError("Data length is to small, need at least a quadrilateral!")
        self.__data = data

    def __getitem__(self,key):
        return self.__data[key]

    @property
    def corners(self):
        """
        Nx2x2 array, where N is the number of corners.
        The second dimension is for left and right corner.
        The last dimension holds x,y values.
        """

        if "corners" in self.__data.dtype.fields:
            return self['corners']

        # the corners of the polygons for each element of the segment
        corners = numpy.empty(shape = (len(self.__data),2,2),dtype = float)

        # handle first and last element
        c0 = numpy.array((self['x'][0],self['y'][0]))
        c1 = numpy.array((self['x'][1],self['y'][1]))

        corners[0,0] = c0 + perpedndicular1(c1-c0)*self['radius'][0]
        corners[0,1] = c0 - perpedndicular1(c1-c0)*self['radius'][0]

        c0 = numpy.array((self['x'][-1],self['y'][-1]))
        c1 = numpy.array((self['x'][-2],self['y'][-2]))

        corners[-1,0] = c0 + perpedndicular1(-c1+c0)*self['radius'][-1]
        corners[-1,1] = c0 - perpedndicular1(-c1+c0)*self['radius'][-1]

        # handle the intermediate segments
        for i,(s0,s1,s2) in enumerate(zip(self[0:-2],self[1:-1],self[2:])):
            c0 = numpy.array((s0['x'],s0['y']))
            c1 = numpy.array((s1['x'],s1['y']))
            c2 = numpy.array((s2['x'],s2['y']))

            # perpendicular unit vectors
            pv01 = perpedndicular1(c1-c0)
            pv12 = perpedndicular1(c2-c1)

            corners[i+1,0] = c1+s1['radius']*normalize((pv01+pv12)/2.)
            corners[i+1,1] = c1-s1['radius']*normalize((pv01+pv12)/2.)
        return corners

    @property
    def outline(self):
        """
        Return the corners of the branch in such order that they encode a polygon.
        """
        return numpy.row_stack((self.corners[:,0,:],self.corners[::-1,1,:]))

    @property
    def length(self):
        dx = self['x'][1:] - self['x'][:-1]
        dy = self['y'][1:] - self['y'][:-1]
        return numpy.sqrt(dx**2 + dy**2).sum()

    def split(self, nsegments = 2, length = None, k = 1, s = 0):
        """Split the branch into n subbranches"""
        if length is None:
            sublength = self.length/nsegments # the target length of the segments
        else:
            sublength = float(length)

        # get the data that requires interpolation
        x,y,z = self['x'],self['y'],self['z'] #,self['radius']
        lcx = self.corners[:,0,0]
        lcy = self.corners[:,0,1]
        rcx = self.corners[:,1,0]
        rcy = self.corners[:,1,1]

        # the cumulative length
        t = numpy.r_[0,numpy.cumsum(((x[:-1]-x[1:])**2 + (y[:-1]-y[1:])**2)**.5)]

        # create interpolation coefficients
        import scipy.interpolate
        splinecoeffs, u = scipy.interpolate.splprep([x,y,z,lcx,lcy,rcx,rcy], u = t, k = k, s = s)


        # create new parametrization array. It is supposed to consist of the old lengths
        # plus points that cut the length into the proper segment size
        tnew    = t.tolist()
        # the indices where to split the resulting tnew
        indices = [0]
        import bisect
        for n in range(1,int(self.length/sublength)+1):
            index = bisect.bisect_left(tnew,n*sublength)
            indices.append(index)
            bisect.insort_left(tnew,n*sublength)

        # append the end index for the last segment if last segment size is larger than eps
        if tnew[-1] - tnew[-2] > 0.01:
            indices.append(len(tnew))

{'fraction_non_alphanumeric': 0.15797807551766138, 'fraction_numerical': 0.03130328867235079, 'mean_word_length': 2.4312578353531134, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1710520', 'n_tokens_mistral': 2834, 'n_tokens_neox': 2755, 'n_words': 662}