<commit_before>import numpy as nm
from bspline import BSpline

class SplineBox:
    """
    B-spline geometry parametrization. Geometry can be modified
    by moving spline control points.
    """
    @staticmethod
    def create_spb(bbox, coors, degree=3, nsg=None):
        bbox = nm.array(bbox)
        coors = nm.array(coors)
        dim = coors.shape[1]
        spbox = []

        if nsg is None:
            nsg = nm.ones((dim,), dtype=nm.int)
        else:
            nsg = nm.array(nsg)

        ncpoints = 1
        spbox = {'base': [None] * dim, 'uidx': [None] * dim,
                 'cp': [None] * dim, 'ncp': [None] * dim,
                 'cp_idx': [None] * dim}
        for idim in range(dim):
            ucoors, ucoors_idx = nm.unique(coors[:,idim], return_inverse=True)
            ncp = degree + nsg[idim]
            bspl = BSpline(degree, ncp=ncp)
            bspl.make_knot_vector(knot_range=(bbox[idim,0], bbox[idim,1]))
            knots = bspl.get_knot_vector()
            cp = nm.zeros((ncp,), dtype=nm.double)
            for j in range(cp.shape[0]):
                cp[j] = nm.sum(knots[(j + 1):(j + degree + 1)]) / degree

            spbox['base'][idim] = bspl.eval_basis(t=ucoors,
                                                  return_val=True).copy()
            spbox['uidx'][idim] = ucoors_idx[:]
            spbox['cp'][idim] = cp[:]
            spbox['ncp'][idim] = ncp
            ncpoints *= ncp

        cpoints = nm.zeros((ncpoints, dim), dtype=nm.double)
        ncp = spbox['ncp']
        cp = spbox['cp']
        if dim == 2:
            idxs = nm.mgrid[0:ncp[0],0:ncp[1]]

        elif dim == 3:
            idxs = nm.mgrid[0:ncp[0],0:ncp[1],0:ncp[2]]

        aux = [1]
        for ii in range(dim - 1):
            aux.append(aux[ii] * ncp[ii])
        spbox['mul_cp_idx'] = nm.array(aux)

        for ii in range(dim):
            spbox['cp_idx'][ii] = idxs[ii].reshape(ncpoints, order='F')
            cpoints[:,ii] = cp[ii][spbox['cp_idx'][ii]]

        return spbox, cpoints

    def __init__(self, bbox, coors,
                 name='spbox', **kwargs):
        """
        Create a SplineBox.

        Parameters
        ----------
        bbox : array
            Mesh bounding box.
        coors : array
            Coordinates of mesh nodes.
        name : str
            Object name.
        """
        self.spbox, self.control_points = self.create_spb(bbox, coors)
        self.ncoors, self.dim = coors.shape
        self.control_points0 = self.control_points.copy()

    def get_coors_shape(self):
        """
        Get the shape of the coordinates.
        """
        return (self.ncoors, self.dim)

    def get_control_points(self, init=False):
        """
        Get spline control points coordinates.

        Returns
        -------
        cpt_coors : array
            The coordinates of the spline control points.
        init : bool
            If True, return initial state.
        """
        if init:<commit_msg>update splinebox.py: number of segments as an input argument to __init__()<commit_after>import numpy as nm
from bspline import BSpline

class SplineBox:
    """
    B-spline geometry parametrization. Geometry can be modified
    by moving spline control points.
    """
    @staticmethod
    def create_spb(bbox, coors, degree=3, nsg=None):
        bbox = nm.array(bbox)
        coors = nm.array(coors)
        dim = coors.shape[1]
        spbox = []

        if nsg is None:
            nsg = nm.ones((dim,), dtype=nm.int)
        else:
            nsg = nm.array(nsg)

        ncpoints = 1
        spbox = {'base': [None] * dim, 'uidx': [None] * dim,
                 'cp': [None] * dim, 'ncp': [None] * dim,
                 'cp_idx': [None] * dim}
        for idim in range(dim):
            ucoors, ucoors_idx = nm.unique(coors[:,idim], return_inverse=True)
            ncp = degree + nsg[idim]
            bspl = BSpline(degree, ncp=ncp)
            bspl.make_knot_vector(knot_range=(bbox[idim,0], bbox[idim,1]))
            knots = bspl.get_knot_vector()
            cp = nm.zeros((ncp,), dtype=nm.double)
            for j in range(cp.shape[0]):
                cp[j] = nm.sum(knots[(j + 1):(j + degree + 1)]) / degree

            spbox['base'][idim] = bspl.eval_basis(t=ucoors,
                                                  return_val=True).copy()
            spbox['uidx'][idim] = ucoors_idx[:]
            spbox['cp'][idim] = cp[:]
            spbox['ncp'][idim] = ncp
            ncpoints *= ncp

        cpoints = nm.zeros((ncpoints, dim), dtype=nm.double)
        ncp = spbox['ncp']
        cp = spbox['cp']
        if dim == 2:
            idxs = nm.mgrid[0:ncp[0],0:ncp[1]]

        elif dim == 3:
            idxs = nm.mgrid[0:ncp[0],0:ncp[1],0:ncp[2]]

        aux = [1]
        for ii in range(dim - 1):
            aux.append(aux[ii] * ncp[ii])
        spbox['mul_cp_idx'] = nm.array(aux)

        for ii in range(dim):
            spbox['cp_idx'][ii] = idxs[ii].reshape(ncpoints, order='F')
            cpoints[:,ii] = cp[ii][spbox['cp_idx'][ii]]

        return spbox, cpoints

    def __init__(self, bbox, coors,
                 nsg=None, name='spbox', **kwargs):
        """
        Create a SplineBox.

        Parameters
        ----------
        bbox : array
            Mesh bounding box.
        coors : array
            Coordinates of mesh nodes.
        name : str
            Object name.
        nsg : array
            Number of segments.
        """
        self.spbox, self.control_points = self.create_spb(bbox, coors, nsg=nsg)
        self.ncoors, self.dim = coors.shape
        self.control_points0 = self.control_points.copy()

    def get_coors_shape(self):
        """
        Get the shape of the coordinates.
        """
        return (self.ncoors, self.dim)

    def get_control_points(self, init=False):
        """
        Get spline control points coordinates.

        Returns
        -------
        cpt_coors : array
            The coordinates of the spline control points.
        init : bool
            If True, return initial state.
        """
        if init:
{'fraction_non_alphanumeric': 0.13660130718954247, 'fraction_numerical': 0.007516339869281046, 'mean_word_length': 1.655531453362256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5289517', 'n_tokens_mistral': 2086, 'n_tokens_neox': 2007, 'n_words': 452}