jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def to_physical(self):
        """Transform to physical space.

        This method returns a :class:`PhysicalArray` containing the
        Fourier transform of the data.  For details of the algorithm,
        see :ref:`Computing Three-Dimensional FFTs with Distributed
        Arrays`.
        """
        N = self.grid.sdims
        M = self.grid.pdims
        i0 = numpy.array([*range(0, N[0]//2+1), *range(-((N[0]-1)//2), 0)])
        s = numpy.zeros(
            self.shape[:-3]
            + (self.grid.pdims[0], 
               self.grid._local_ky_slice.stop
               - self.grid._local_ky_slice.start, 
               self.grid._local_kz_slice.stop
               - self.grid._local_kz_slice.start),
            dtype=complex
            )
        s[..., i0, :, :] = self
        if self.grid._aliasing_strategy == 'mpi4py':
            if M[0] > N[0] and N[0] % 2 == 0:
                s[..., N[0]//2, :, :] *= 0.5
                s[..., -(N[0]//2), :, :] = s[..., N[0]//2, :, :]
        t1 = numpy.ascontiguousarray(numpy.fft.ifft(s, axis=-3))
        t2 = numpy.zeros(
            self.shape[:-3]
            + (self.grid._local_x_slice.stop
               - self.grid._local_x_slice.start,
               self.grid.pdims[1],
               self.grid._local_kz_slice.stop
               - self.grid._local_kz_slice.start),
            dtype=complex
            )
        count = numpy.multiply.reduce(self.shape[:-3], dtype=int)
        counts = [count] * self.grid.comm_yx.size
        displs = [0] * self.grid.comm_yx.size
        self.grid.comm_yx.Alltoallw(
            [t1, counts, displs, self.grid._x_pencils],
            [t2, counts, displs, self.grid._ky_pencils],
            )
        if self.grid._aliasing_strategy == 'mpi4py':
            if M[1] > N[1] and N[1] % 2 == 0:
                t2[..., :, N[1]//2, :] *= 0.5
                t2[..., :, -(N[1]//2), :] = t2[..., :, N[1]//2, :]
        t3 = numpy.ascontiguousarray(numpy.fft.ifft(
            t2,
            axis=-2
            ))
        t4 = numpy.zeros(
            self.shape[:-3]
            + (self.grid._local_x_slice.stop
               - self.grid._local_x_slice.start,
               self.grid._local_y_slice.stop
               - self.grid._local_y_slice.start,
               self.grid.pdims[2] // 2 + 1),
            dtype=complex
            )
        count = numpy.multiply.reduce(self.shape[:-3], dtype=int)
        counts = [count] * self.grid.comm_zy.size
        displs = [0] * self.grid.comm_zy.size
        self.grid.comm_zy.Alltoallw(
            [t3, counts, displs, self.grid._y_pencils],
            [t4, counts, displs, self.grid._kz_pencils],
            )
        t5 = numpy.fft.irfft(
            t4,
            n=self.grid.x.shape[3],
            axis=-1
            )*numpy.multiply.reduce(self.grid.pdims)
        return PhysicalArray(self.grid, t5)
//  [python]
def to_physical(self):
        """Transform to physical space.

        This method returns a :class:`PhysicalArray` containing the
        Fourier transform of the data.  For details of the algorithm,
        see :ref:`Computing Three-Dimensional FFTs with Distributed
        Arrays`.
        """
        N = self.grid.sdims
        M = self.grid.pdims
        i0 = numpy.array([*range(0, N[0]//2+1), *range(-((N[0]-1)//2), 0)])
        s = numpy.zeros(
            self.shape[:-3]
            + (self.grid.pdims[0], 
               self.grid._local_ky_slice.stop
               - self.grid._local_ky_slice.start, 
               self.grid._local_kz_slice.stop
               - self.grid._local_kz_slice.start),
            dtype=complex
            )
        s[..., i0, :, :] = self
        if self.grid._aliasing_strategy == 'mpi4py':
            if M[0] > N[0] and N[0] % 2 == 0:
                s[..., N[0]//2, :, :] *= 0.5
                s[..., -(N[0]//2), :, :] = s[..., N[0]//2, :, :]
        t1 = numpy.ascontiguousarray(numpy.fft.ifft(s, axis=-3))
        t2 = numpy.zeros(
            self.shape[:-3]
            + (self.grid._local_x_slice.stop
               - self.grid._local_x_slice.start,
               self.grid.pdims[1],
               self.grid._local_kz_slice.stop
               - self.grid._local_kz_slice.start),
            dtype=complex
            )
        count = numpy.prod(self.shape[:-3], dtype=int)
        counts = [count] * self.grid.comm_yx.size
        displs = [0] * self.grid.comm_yx.size
        self.grid.comm_yx.Alltoallw(
            [t1, counts, displs, self.grid._x_pencils],
            [t2, counts, displs, self.grid._ky_pencils],
            )
        if self.grid._aliasing_strategy == 'mpi4py':
            if M[1] > N[1] and N[1] % 2 == 0:
                t2[..., :, N[1]//2, :] *= 0.5
                t2[..., :, -(N[1]//2), :] = t2[..., :, N[1]//2, :]
        t3 = numpy.ascontiguousarray(numpy.fft.ifft(
            t2,
            axis=-2
            ))
        t4 = numpy.zeros(
            self.shape[:-3]
            + (self.grid._local_x_slice.stop
               - self.grid._local_x_slice.start,
               self.grid._local_y_slice.stop
               - self.grid._local_y_slice.start,
               self.grid.pdims[2] // 2 + 1),
            dtype=complex
            )
        count = numpy.prod(self.shape[:-3], dtype=int)
        counts = [count] * self.grid.comm_zy.size
        displs = [0] * self.grid.comm_zy.size
        self.grid.comm_zy.Alltoallw(
            [t3, counts, displs, self.grid._y_pencils],
            [t4, counts, displs, self.grid._kz_pencils],
            )
        t5 = numpy.fft.irfft(
            t4,
            n=self.grid.x.shape[3],
            axis=-1
            )*numpy.prod(self.grid.pdims)
        return PhysicalArray(self.grid, t5)
//  [python]
def Prod(a, axis, keep_dims):
    """
    Prod reduction op.
    """
    return n.prod(a, axis=axis if not isinstance(axis, np.ndarray) else tuple(axis),
                   keepdims=keep_dims),
//  [python]
def Prod(a, axis, keep_dims):
    """
    Prod reduction op.
    """
    return np.prod(a, axis=axis if not isinstance(axis, np.ndarray) else tuple(axis),
                   keepdims=keep_dims),
//  [python]
def check_bilinear_upsampling_with_shape(data_shape, weight_shape, scale, root_scale, num_filter):
    def _init_bilinear(arr, f):
        weight = np.zeros(np.prod(arr.shape), dtype='float32')
        shape = arr.shape
        c = (2 * f - 1 - f % 2) / (2. * f)
        for i in range(np.prod(shape)):
            x = i % shape[3]
            y = (i // shape[3]) % shape[2]
            weight[i] = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
        arr[:] = weight.reshape(shape)
        return arr

    up = mx.sym.UpSampling(mx.sym.Variable("data"),
        mx.sym.Variable('weight'), sample_type='bilinear', scale=root_scale,
        num_filter=num_filter, num_args=2)
    arg_shapes, out_shapes, _ = up.infer_shape(data=data_shape)
    arr = {'data': mx.random.uniform(-5, 5, data_shape, ctx=mx.cpu()).copyto(default_context()),
        'weight':  mx.nd.array(_init_bilinear(mx.ndarray.empty(arg_shapes[1]).asnumpy(), root_scale))}

    arr_grad = [mx.nd.empty(s) for s in arg_shapes]
    exe = up.bind(default_context(), args=arr, args_grad=arr_grad)
    exe.forward(is_train=True)
    out = exe.outputs[0].asnumpy()
    exe.backward(exe.outputs)
    target_shape = (data_shape[2] * root_scale, data_shape[3] * root_scale)
    assert out.shape == data_shape[:2] + target_shape
//  [python]
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(1, 1), stride=(1,1)):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                              filter height,filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                             image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows,#cols)
        """

        assert image_shape[1] == filter_shape[1]
        self.input = input

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = n.prod(filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = (filter_shape[0] * n.prod(filter_shape[2:]) /
                   n.prod(poolsize))
        # initialize weights with random weights
        W_bound = numpy.sqrt(6. / (fan_in + fan_out))
        self.W = theano.shared(numpy.asarray(
            rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
            dtype=theano.config.floatX),
                               borrow=True)

        # the bias is a 1D tensor -- one bias per output feature map
        b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
        self.b = theano.shared(value=b_values, borrow=True)

        # convolve input feature maps with filters
        conv_out = conv.conv2d(input=input, filters=self.W,
                filter_shape=filter_shape, image_shape=image_shape)

        # downsample each feature map individually, using maxpooling
        pooled_out = downsample.max_pool_2d(input=conv_out,
                                            ds=poolsize, ignore_border=True, st=stride)

        # add the bias term. Since the bias is a vector (1D array), we first
        # reshape it to a tensor of shape (1,n_filters,1,1). Each bias will
        # thus be broadcasted across mini-batches and feature map
        # width & height
        self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))

        # store parameters of this layer
        self.params = [self.W, self.b]
//  [python]
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(1, 1), stride=(1,1)):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.

        :type rng: numpy.random.RandomState
        :param rng: a random number generator used to initialize weights

        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type filter_shape: tuple or list of length 4
        :param filter_shape: (number of filters, num input feature maps,
                              filter height,filter width)

        :type image_shape: tuple or list of length 4
        :param image_shape: (batch size, num input feature maps,
                             image height, image width)

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows,#cols)
        """

        assert image_shape[1] == filter_shape[1]
        self.input = input

        # there are "num input feature maps * filter height * filter width"
        # inputs to each hidden unit
        fan_in = numpy.prod(filter_shape[1:])
        # each unit in the lower layer receives a gradient from:
        # "num output feature maps * filter height * filter width" /
        #   pooling size
        fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) /
                   numpy.prod(poolsize))
        # initialize weights with random weights
        W_bound = numpy.sqrt(6. / (fan_in + fan_out))
        self.W = theano.shared(numpy.asarray(
            rng.uniform(low=-W_bound, high=W_bound, size=filter_shape),
            dtype=theano.config.floatX),
                               borrow=True)

        # the bias is a 1D tensor -- one bias per output feature map
        b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX)
        self.b = theano.shared(value=b_values, borrow=True)

        # convolve input feature maps with filters
        conv_out = conv.conv2d(input=input, filters=self.W,
                filter_shape=filter_shape, image_shape=image_shape)

        # downsample each feature map individually, using maxpooling
        pooled_out = downsample.max_pool_2d(input=conv_out,
                                            ds=poolsize, ignore_border=True, st=stride)

        # add the bias term. Since the bias is a vector (1D array), we first
        # reshape it to a tensor of shape (1,n_filters,1,1). Each bias will
        # thus be broadcasted across mini-batches and feature map
        # width & height
        self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))

        # store parameters of this layer
        self.params = [self.W, self.b]
//  [python]
def full(self, device=None):
        if device is None:
            device = self.orig_device
        full_numel = n.prod(self.full_size())
        flat_tensor = torch.zeros([full_numel], dtype=self.local_data.dtype, device=device)
        partition_tensors = []
        for part_id in range(self.num_parts):
            part_size = self.partition[part_id + 1] - self.partition[part_id]
            buf = flat_tensor.narrow(0, start=self.partition[part_id], length=part_size)
            if part_id == self.rank:
                buf.copy_(self.local_data)
            partition_tensors.append(buf)
        dist.all_gather(partition_tensors, partition_tensors[self.rank], group=self.group)
        for i in range(len(partition_tensors)):
            partition_tensors[i].data = torch.zeros(1)
            partition_tensors[i] = None
        return flat_tensor.view(self.full_size()).clone().detach()
//  [python]
def full(self, device=None):
        if device is None:
            device = self.orig_device
        full_numel = prod(self.full_size())
        flat_tensor = torch.zeros([full_numel], dtype=self.local_data.dtype, device=device)
        partition_tensors = []
        for part_id in range(self.num_parts):
            part_size = self.partition[part_id + 1] - self.partition[part_id]
            buf = flat_tensor.narrow(0, start=self.partition[part_id], length=part_size)
            if part_id == self.rank:
                buf.copy_(self.local_data)
            partition_tensors.append(buf)
        dist.all_gather(partition_tensors, partition_tensors[self.rank], group=self.group)
        for i in range(len(partition_tensors)):
            partition_tensors[i].data = torch.zeros(1)
            partition_tensors[i] = None
        return flat_tensor.view(self.full_size()).clone().detach()
//  [python]
def get_size(self, shape_info):
        """
        Number of bytes taken by the object represented by shape_info.

        Parameters
        ----------
        shape_info
            The output of the call to get_shape_info().

        Returns
        -------
        int
            The number of bytes taken by the object described by ``shape_info``.

        """
        if shape_info:
            return reduce(operator.mul, shape_info) * numpy.dtype(self.dtype).itemsize
        else:  # a scalar
            return numpy.dtype(self.dtype).itemsize
//  [python]
def get_size(self, shape_info):
        """
        Number of bytes taken by the object represented by shape_info.

        Parameters
        ----------
        shape_info
            The output of the call to get_shape_info().

        Returns
        -------
        int
            The number of bytes taken by the object described by ``shape_info``.

        """
        if shape_info:
            return numpy.prod(shape_info) * numpy.dtype(self.dtype).itemsize
        else:  # a scalar
            return numpy.dtype(self.dtype).itemsize
//  [python]
def init_parameters(self):
        # just explore the network, find every weight and bias matrix and fill it
        for m in self.modules():
            if isinstance(m, (nn.Conv2d, nn.ConvTranspose2d, nn.Linear)):
                if hasattr(m, "weight") and m.weight is not None and m.weight.requires_grad:
                    #init as original implementation
                    scale = 1.0/numpy.sqrt(numpy.prod(m.weight.shape[1:]))
                    scale /=numpy.sqrt(3)
                    #nn.init.xavier_normal(m.weight,1)
                    #nn.init.constant(m.weight,0.005)
                    nn.init.uniform(m.weight,-scale,scale)
                if hasattr(m, "bias") and m.bias is not None and m.bias.requires_grad:
                    nn.init.constant(m.bias, 0.0)
//  [python]
def _get_nan_decs(self):
        """Set all variables to nan for specializations of fused types for
        which don't have signatures.

        """
        # Set non fused-type variables to nan
        tab = " "*4
        fused_types, lines = [], [tab + "else:"]
        seen = set()
        for outvar, outtype, code in zip(self.outvars, self.outtypes, self.outcodes):
            if len(code) == 1:
                line = "{}[0] = {}".format(outvar, NAN_VALUE[code])
                lines.append(2*tab + line)
            else:
                fused_type = outtype
                name, _ = fused_type
                if name not in seen:
                    fused_types.append(fused_type)
                    seen.add(name)
        if not fused_types:
            return lines

        # Set fused-type variables to nan
        all_codes = tuple([codes for _unused, codes in fused_types])

        codelens = list(map(lambda x: len(x), all_codes))
        last = reduce(lambda x, y: x*y, codelens) - 1
        for m, codes in enumerate(itertools.product(*all_codes)):
            fused_codes, decs = [], []
            for n, fused_type in enumerate(fused_types):
                code = codes[n]
                fused_codes.append(underscore(CY_TYPES[code]))
                for nn, outvar in enumerate(self.outvars):
                    if self.outtypes[nn] == fused_type:
                        line = "{}[0] = {}".format(outvar, NAN_VALUE[code])
                        decs.append(line)
            if m == 0:
                adverb = "if"
            elif m == last:
                adverb = "else"
            else:
                adverb = "elif"
            cond = self._get_conditional(fused_types, codes, adverb)
            lines.append(2*tab + cond)
            lines.extend(map(lambda x: 3*tab + x, decs))
        return lines
//  [python]
def _get_nan_decs(self):
        """Set all variables to nan for specializations of fused types for
        which don't have signatures.

        """
        # Set non fused-type variables to nan
        tab = " "*4
        fused_types, lines = [], [tab + "else:"]
        seen = set()
        for outvar, outtype, code in zip(self.outvars, self.outtypes, self.outcodes):
            if len(code) == 1:
                line = "{}[0] = {}".format(outvar, NAN_VALUE[code])
                lines.append(2*tab + line)
            else:
                fused_type = outtype
                name, _ = fused_type
                if name not in seen:
                    fused_types.append(fused_type)
                    seen.add(name)
        if not fused_types:
            return lines

        # Set fused-type variables to nan
        all_codes = tuple([codes for _unused, codes in fused_types])

        codelens = list(map(lambda x: len(x), all_codes))
        last = numpy.prod(codelens) - 1
        for m, codes in enumerate(itertools.product(*all_codes)):
            fused_codes, decs = [], []
            for n, fused_type in enumerate(fused_types):
                code = codes[n]
                fused_codes.append(underscore(CY_TYPES[code]))
                for nn, outvar in enumerate(self.outvars):
                    if self.outtypes[nn] == fused_type:
                        line = "{}[0] = {}".format(outvar, NAN_VALUE[code])
                        decs.append(line)
            if m == 0:
                adverb = "if"
            elif m == last:
                adverb = "else"
            else:
                adverb = "elif"
            cond = self._get_conditional(fused_types, codes, adverb)
            lines.append(2*tab + cond)
            lines.extend(map(lambda x: 3*tab + x, decs))
        return lines
//  [python]
def testShapesValues(self):
    for dtype in [dtypes.float32, dtypes.float64]:
      for shape in [(10, 10), (10, 9, 8), (100, 5, 5), (50, 40), (40, 50)]:
        init = init_ops.orthogonal_initializer(dtype=dtype)
        tol = 1e-5 if dtype == dtypes.float32 else 1e-12
        with self.session(graph=ops.Graph(), use_gpu=True):
          # Check the shape
          t = init(shape).eval()
          self.assertAllEqual(shape, t.shape)
          # Check orthogonality by computing the inner product
          t = t.reshape((np.prod(t.shape[:-1]), t.shape[-1]))
          if t.shape[0] > t.shape[1]:
            self.assertAllClose(
                np.dot(t.T, t), np.eye(t.shape[1]), rtol=tol, atol=tol)
          else:
            self.assertAllClose(
                np.dot(t, t.T), np.eye(t.shape[0]), rtol=tol, atol=tol)
//  [python]
def count_params(x):
  """Returns the static number of elements in a variable or tensor.

  Args:
      x: Variable or tensor.

  Returns:
      Integer, the number of scalars in `x`.

  Example:

  >>> kvar = tf.keras.backend.zeros((2,3))
  >>> tf.keras.backend.count_params(kvar)
  6
  >>> tf.keras.backend.eval(kvar)
  array([[0.,  0.,  0.],
         [0.,  0.,  0.]], dtype=float32)

  """
  return n.prod(x.shape.as_list())
//  [python]
def count_params(x):
  """Returns the static number of elements in a variable or tensor.

  Args:
      x: Variable or tensor.

  Returns:
      Integer, the number of scalars in `x`.

  Example:

  >>> kvar = tf.keras.backend.zeros((2,3))
  >>> tf.keras.backend.count_params(kvar)
  6
  >>> tf.keras.backend.eval(kvar)
  array([[0.,  0.,  0.],
         [0.,  0.,  0.]], dtype=float32)

  """
  return np.prod(x.shape.as_list())
//  [python]
def __init__(self, v, n, region_type=True, scheme_fcn=qd.c2.get_good_scheme):
        from quadpy.cn._helpers import transform, get_detJ

        if region_type:
            from quadpy.c2 import rectangle_points

            self.v = rectangle_points(*v)
        else:
            self.v = v
        super().__init__(
            scheme=scheme_fcn(n),
            trans=lambda x: transform(x, self.v),
            jac=lambda x: np.abs(get_detJ(x, self.v))
            * 2 ** n.prod(len(self.v.shape) - 1),
        )
//  [python]
def __init__(self, v, n, region_type=True, scheme_fcn=qd.c2.get_good_scheme):
        from quadpy.cn._helpers import transform, get_detJ

        if region_type:
            from quadpy.c2 import rectangle_points

            self.v = rectangle_points(*v)
        else:
            self.v = v
        super().__init__(
            scheme=scheme_fcn(n),
            trans=lambda x: transform(x, self.v),
            jac=lambda x: np.abs(get_detJ(x, self.v))
            * 2 ** np.prod(len(self.v.shape) - 1),
        )
//  [python]
def _resolve_dims(cls, old_dim, new_dims, pad_to_multiples=False):
        """
        :param int old_dim:
        :param tuple[int]|list[int] new_dims:
        :param bool|None pad_to_multiples:
        :return: new_dims with -1 resolved
        :rtype: tuple[int]
        """
        import numpy

        if all([d > 0 for d in new_dims]):
            new_dims_resolved = new_dims
        else:
            assert all([d != 0 and (d == -1 or d > 0) for d in new_dims])
            assert len([d for d in new_dims if d == -1]) == 1
            new_pos_dims = [d for d in new_dims if d > 0]
            n = 1
            for i in new_pos_dims:
                n *= i
            if pad_to_multiples:
                old_dim += (-old_dim) % n
            assert old_dim % n == 0
            rem = old_dim // n
            new_dims_resolved = [(d if (d > 0) else rem) for d in new_dims]
        assert n * numpy.prod([x for x in new_dims_resolved if x != rem]) == old_dim
        return tuple(new_dims_resolved)
//  [python]
def _resolve_dims(cls, old_dim, new_dims, pad_to_multiples=False):
        """
        :param int old_dim:
        :param tuple[int]|list[int] new_dims:
        :param bool|None pad_to_multiples:
        :return: new_dims with -1 resolved
        :rtype: tuple[int]
        """
        import numpy

        if all([d > 0 for d in new_dims]):
            new_dims_resolved = new_dims
        else:
            assert all([d != 0 and (d == -1 or d > 0) for d in new_dims])
            assert len([d for d in new_dims if d == -1]) == 1
            new_pos_dims = [d for d in new_dims if d > 0]
            n = int(numpy.prod(new_pos_dims))
            if pad_to_multiples:
                old_dim += (-old_dim) % n
            assert old_dim % n == 0
            rem = old_dim // n
            new_dims_resolved = [(d if (d > 0) else rem) for d in new_dims]
        assert numpy.prod(new_dims_resolved) == old_dim
        return tuple(new_dims_resolved)
//  [python]
def __init__(self, v, n, region_type=True, scheme_fcn=qd.c3.get_good_scheme):
        from quadpy.cn._helpers import transform, get_detJ

        assert (
            n <= 11
        ), "The degree of the cubature is not more than 11. Otherwise use nD cube scheme!"
        if region_type:
            from quadpy.c3 import cube_points

            self.v = cube_points(*v)
        else:
            self.v = v
        super().__init__(
            scheme=scheme_fcn(n),
            trans=lambda x: transform(x, self.v),
            jac=lambda x: np.abs(get_detJ(x, self.v))
            * 2 ** n.prod(len(self.v.shape) - 1),
        )
//  [python]
def __init__(self, v, n, region_type=True, scheme_fcn=qd.c3.get_good_scheme):
        from quadpy.cn._helpers import transform, get_detJ

        assert (
            n <= 11
        ), "The degree of the cubature is not more than 11. Otherwise use nD cube scheme!"
        if region_type:
            from quadpy.c3 import cube_points

            self.v = cube_points(*v)
        else:
            self.v = v
        super().__init__(
            scheme=scheme_fcn(n),
            trans=lambda x: transform(x, self.v),
            jac=lambda x: np.abs(get_detJ(x, self.v))
            * 2 ** np.prod(len(self.v.shape) - 1),
        )
//  [python]
def __init__(self, v, dim, region_type=True, scheme_fcn=qd.cn.stroud_cn_3_3):
        from quadpy.cn._helpers import transform, get_detJ

        self.dim = dim
        if region_type:
            from quadpy.cn._helpers import ncube_points

            self.v = ncube_points(*v)
        else:
            self.v = v
        super().__init__(
            scheme=scheme_fcn(self.dim),
            trans=lambda x: transform(x, self.v),
            jac=lambda x: 2 ** n.prod(len(self.v.shape) - 1)
            * np.abs(get_detJ(x, self.v)),
        )
//  [python]
def __init__(self, v, dim, region_type=True, scheme_fcn=qd.cn.stroud_cn_3_3):
        from quadpy.cn._helpers import transform, get_detJ

        self.dim = dim
        if region_type:
            from quadpy.cn._helpers import ncube_points

            self.v = ncube_points(*v)
        else:
            self.v = v
        super().__init__(
            scheme=scheme_fcn(self.dim),
            trans=lambda x: transform(x, self.v),
            jac=lambda x: 2 ** np.prod(len(self.v.shape) - 1)
            * np.abs(get_detJ(x, self.v)),
        )
//  [python]
def getaxes(pos, mass, radius=numpy.inf):
    '''
    Measure the principal axes of the moment of inertia tensor,
    using only particles within the elliptical radius smaller than the provided
    value (iteratively adjusting the axes of the bounding ellipse to match the
    axes of the moment of inertia).
    Arguments:
      pos: Nx3 array of particle positions;
      mass: array of particle masses of length N;
      radius: maximum elliptical radius of particles included in the computation,
        defined to be the geometric average of the three principal axes of the
        ellipse (default: infinity).
    Return:
      a tuple of three arrays:
      - three principal axes of the moment of inertia tensor;
      - a boolean array of length N indicating which particles were used in
        computing the moment of inertia;
      - the rotation matrix defined so that the positions and velocities in the
        rotated reference frame aligned with the principal axes are given by
        pos_aligned = pos.dot(matrix)
        vel_aligned = vel.dot(matrix)
    '''
    evec = numpy.eye(3)   # initial guess for axes orientation
    axes = numpy.ones(3)  # and axis ratios; these are updated at each iteration
    while True:
        # use particles within the elliptical radius less than the provided value
        ellpos  = pos.dot(evec) / axes
        filter  = numpy.sum(ellpos**2, axis=1) < radius**2
        inertia = pos[filter].T.dot(pos[filter] * mass[filter,None])
        val,vec = numpy.linalg.eigh(inertia)
        order   = numpy.argsort(-val)  # sort axes in decreasing order
        evec    = vec[:,order]         # updated axis directions
        axesnew = (val[order] / n.prod(val)**(1./3))**0.5  # updated axis ratios, normalized so that ax*ay*az=1
        if sum(abs(axesnew-axes))<0.01: break
        axes    = axesnew
    # evec is almost equivalent to the rotation matrix, just need to ensure that it is right-handed
    # and preferrably has positive values on the diagonal
    if numpy.linalg.det(evec)<0: evec *= -1
    if evec[2,2]<0: evec[:,1:3] *= -1
    if evec[1,1]<0: evec[:,0:2] *= -1
    return axes, filter, evec
//  [python]
def getaxes(pos, mass, radius=numpy.inf):
    '''
    Measure the principal axes of the moment of inertia tensor,
    using only particles within the elliptical radius smaller than the provided
    value (iteratively adjusting the axes of the bounding ellipse to match the
    axes of the moment of inertia).
    Arguments:
      pos: Nx3 array of particle positions;
      mass: array of particle masses of length N;
      radius: maximum elliptical radius of particles included in the computation,
        defined to be the geometric average of the three principal axes of the
        ellipse (default: infinity).
    Return:
      a tuple of three arrays:
      - three principal axes of the moment of inertia tensor;
      - a boolean array of length N indicating which particles were used in
        computing the moment of inertia;
      - the rotation matrix defined so that the positions and velocities in the
        rotated reference frame aligned with the principal axes are given by
        pos_aligned = pos.dot(matrix)
        vel_aligned = vel.dot(matrix)
    '''
    evec = numpy.eye(3)   # initial guess for axes orientation
    axes = numpy.ones(3)  # and axis ratios; these are updated at each iteration
    while True:
        # use particles within the elliptical radius less than the provided value
        ellpos  = pos.dot(evec) / axes
        filter  = numpy.sum(ellpos**2, axis=1) < radius**2
        inertia = pos[filter].T.dot(pos[filter] * mass[filter,None])
        val,vec = numpy.linalg.eigh(inertia)
        order   = numpy.argsort(-val)  # sort axes in decreasing order
        evec    = vec[:,order]         # updated axis directions
        axesnew = (val[order] / numpy.prod(val)**(1./3))**0.5  # updated axis ratios, normalized so that ax*ay*az=1
        if sum(abs(axesnew-axes))<0.01: break
        axes    = axesnew
    # evec is almost equivalent to the rotation matrix, just need to ensure that it is right-handed
    # and preferrably has positive values on the diagonal
    if numpy.linalg.det(evec)<0: evec *= -1
    if evec[2,2]<0: evec[:,1:3] *= -1
    if evec[1,1]<0: evec[:,0:2] *= -1
    return axes, filter, evec
//  [python]
def save_into_ilp(self, project_name, labels, guidename):
        import h5py
        import wx

        with h5py.File(project_name) as f:
            g = f["DataSets"]
            for k in g:
                data_item = g[k]
                if data_item.attrs.get("fileName") == guidename:
                    break
            else:
                wx.MessageBox(
                    "Sorry, could not find the file, %s, in the project, %s"
                    % (guidename, project_name)
                )
            project_labels = data_item["labels"]["data"]
            mask = numpy.ones(project_labels.shape[2:4], project_labels.dtype)
            for label in labels:
                mask[label != 0] = 2
            #
            # "only" use the first 100,000 points in the image
            #
            subsample = 100000
            npts = numpy.multiply.reduce(mask.shape)
            if npts > subsample:
                r = numpy.random.RandomState()
                r.seed(numpy.sum(mask) % (2 ** 16))
                i, j = numpy.mgrid[0 : mask.shape[0], 0 : mask.shape[1]]
                i0 = i[mask == 1]
                j0 = j[mask == 1]
                i1 = i[mask == 2]
                j1 = j[mask == 2]
                if len(i1) < subsample / 2:
                    p0 = r.permutation(len(i0))[: (subsample - len(i1))]
                    p1 = numpy.arange(len(i1))
                elif len(i0) < subsample / 2:
                    p0 = numpy.arange(len(i0))
                    p1 = r.permutation(len(i1))[: (subsample - len(i0))]
                else:
                    p0 = r.permutation(len(i0))[: (subsample / 2)]
                    p1 = r.permutation(len(i1))[: (subsample / 2)]
                mask_copy = numpy.zeros(mask.shape, mask.dtype)
                mask_copy[i0[p0], j0[p0]] = 1
                mask_copy[i1[p1], j1[p1]] = 2
                if "prediction" in data_item:
                    prediction = data_item["prediction"]
                    if numpy.max(prediction[0, 0, :, :, 0]) > 0.5:
                        # Only do if prediction was done (otherwise all == 0)
                        for n in range(2):
                            p = prediction[0, 0, :, :, n]
                            bad = (p < 0.5) & (mask == n + 1)
                            mask_copy[i[bad], j[bad]] = n + 1
                mask = mask_copy
            project_labels[0, 0, :, :, 0] = mask
//  [python]
def save_into_ilp(self, project_name, labels, guidename):
        import h5py
        import wx

        with h5py.File(project_name) as f:
            g = f["DataSets"]
            for k in g:
                data_item = g[k]
                if data_item.attrs.get("fileName") == guidename:
                    break
            else:
                wx.MessageBox(
                    "Sorry, could not find the file, %s, in the project, %s"
                    % (guidename, project_name)
                )
            project_labels = data_item["labels"]["data"]
            mask = numpy.ones(project_labels.shape[2:4], project_labels.dtype)
            for label in labels:
                mask[label != 0] = 2
            #
            # "only" use the first 100,000 points in the image
            #
            subsample = 100000
            npts = numpy.prod(mask.shape)
            if npts > subsample:
                r = numpy.random.RandomState()
                r.seed(numpy.sum(mask) % (2 ** 16))
                i, j = numpy.mgrid[0 : mask.shape[0], 0 : mask.shape[1]]
                i0 = i[mask == 1]
                j0 = j[mask == 1]
                i1 = i[mask == 2]
                j1 = j[mask == 2]
                if len(i1) < subsample / 2:
                    p0 = r.permutation(len(i0))[: (subsample - len(i1))]
                    p1 = numpy.arange(len(i1))
                elif len(i0) < subsample / 2:
                    p0 = numpy.arange(len(i0))
                    p1 = r.permutation(len(i1))[: (subsample - len(i0))]
                else:
                    p0 = r.permutation(len(i0))[: (subsample / 2)]
                    p1 = r.permutation(len(i1))[: (subsample / 2)]
                mask_copy = numpy.zeros(mask.shape, mask.dtype)
                mask_copy[i0[p0], j0[p0]] = 1
                mask_copy[i1[p1], j1[p1]] = 2
                if "prediction" in data_item:
                    prediction = data_item["prediction"]
                    if numpy.max(prediction[0, 0, :, :, 0]) > 0.5:
                        # Only do if prediction was done (otherwise all == 0)
                        for n in range(2):
                            p = prediction[0, 0, :, :, n]
                            bad = (p < 0.5) & (mask == n + 1)
                            mask_copy[i[bad], j[bad]] = n + 1
                mask = mask_copy
            project_labels[0, 0, :, :, 0] = mask
//  [python]
def reduce_prod(input_tensor, axis=None, keepdims=None):
    if not SWITCH_ON or has_tensor(input_tensor):
        return znp.prod(input_tensor, axis, keepdims=keepdims)
    elif keepdims is None:
        return n.prod(input_tensor, axis)
    return n.prod(input_tensor, axis, keepdims=keepdims)
//  [python]
def reduce_prod(input_tensor, axis=None, keepdims=None):
    if not SWITCH_ON or has_tensor(input_tensor):
        return znp.prod(input_tensor, axis, keepdims=keepdims)
    elif keepdims is None:
        return np.prod(input_tensor, axis)
    return np.prod(input_tensor, axis, keepdims=keepdims)
//  [python]
def backward_var(self, grad, index, **kwargs):
        (a,) = self.variables
        n = a.data.size if self.axis is None else np.prod([a.shape[i] for i in self.axis])
        return super().backward_var(grad / n, index, **kwargs)
//  [python]
def backward_var(self, grad, index, **kwargs):
        (a,) = self.variables
        n = (
            a.data.size
            if self.axis is None
            else np.prod([a.shape[i] for i in self.axis])
        )
        return super().backward_var(grad / n, index, **kwargs)
//  [python]
def forward(self, x, c):
        #print('loss_forward')
        (a,tmp) = torch.max(c,1)
        tmp = tmp.unsqueeze(1).float()
        inputSize = np.array([x.size()[0],x.size()[1],x.size()[2],x.size()[3]])
        numPixelsPerImage = np.prod(inputSize[2:4])
        numPixels = numPixelsPerImage * inputSize[0]
        imageVolume = numPixelsPerImage * inputSize[1]
        n = np.array(range(numPixels))
        n = np.reshape(n, (tmp.size()[0],tmp.size()[1],tmp.size()[2],tmp.size()[3]))
        offset = np.mod(n,numPixelsPerImage) + imageVolume * np.fix(n / numPixelsPerImage)
        ci = torch.from_numpy(offset).float().cuda() + numPixelsPerImage * torch.max(tmp,torch.zeros(tmp.size()).cuda())
        (Xmax,b) = torch.max(x,1)
        Xmax = Xmax.unsqueeze(1).float()
        ex = torch.exp(x - Xmax)
        x_line = x.reshape(x.size()[0]*x.size()[1])
        x_ci = torch.zeros(ci.size()).cuda()
        for i in range(x.size()[0]):
            x_ci[i, 0,0,0] = x_line[ci[i, 0, 0, 0].long()]
        t = Xmax + torch.log(torch.sum(ex,1).unsqueeze(1)) - x_ci
        t = torch.sum(t)
        self.save_for_backward(x, tmp)
        return t
//  [python]
def backward(self, grad_output):
        #print('loss_backward')
        x,tmp = self.saved_tensors
        x_grad = tmp_grad = None
        inputSize = np.array([x.size()[0],x.size()[1],x.size()[2],x.size()[3]])
        numPixelsPerImage = np.multiply.reduce(inputSize[2:4]) ;
        numPixels = numPixelsPerImage * inputSize[0] ;
        imageVolume = numPixelsPerImage * inputSize[1];

        n = np.array(range(numPixels))
        n = np.reshape(n, (tmp.size()[0],tmp.size()[1],tmp.size()[2],tmp.size()[3]))
        offset = np.mod(n,numPixelsPerImage) + imageVolume * np.fix(n / numPixelsPerImage)
        ci = torch.from_numpy(offset).float().cuda() + numPixelsPerImage * torch.max(tmp,torch.zeros(tmp.size()).cuda())
        (Xmax,b) = torch.max(x,1)
        Xmax = Xmax.unsqueeze(1).float()
        ex  = torch.exp(x - Xmax)
        x_grad = ex.div(torch.sum(ex,1).unsqueeze(1))

        x_grad_line = x_grad.reshape(x_grad.size()[0]*x_grad.size()[1])
        for i in range(ci.size()[0]):
            index = ci[i,0,0,0].long()
            x_grad_line[index] = x_grad_line[index] - 1
        x_grad = x_grad_line.reshape(x_grad.size()[0],x_grad.size()[1],x_grad.size()[2],x_grad.size()[3])
        x_grad = grad_output.mul(x_grad)
        x_grad = x_grad * x.size()[1]
        #print('x_grad',x_grad[0,:,:,:])
        return x_grad , tmp_grad
//  [python]
def backward(self, grad_output):
        #print('loss_backward')
        x,tmp = self.saved_tensors
        x_grad = tmp_grad = None
        inputSize = np.array([x.size()[0],x.size()[1],x.size()[2],x.size()[3]])
        numPixelsPerImage = np.prod(inputSize[2:4]) ;
        numPixels = numPixelsPerImage * inputSize[0] ;
        imageVolume = numPixelsPerImage * inputSize[1];

        n = np.array(range(numPixels))
        n = np.reshape(n, (tmp.size()[0],tmp.size()[1],tmp.size()[2],tmp.size()[3]))
        offset = np.mod(n,numPixelsPerImage) + imageVolume * np.fix(n / numPixelsPerImage)
        ci = torch.from_numpy(offset).float().cuda() + numPixelsPerImage * torch.max(tmp,torch.zeros(tmp.size()).cuda())
        (Xmax,b) = torch.max(x,1)
        Xmax = Xmax.unsqueeze(1).float()
        ex  = torch.exp(x - Xmax)
        x_grad = ex.div(torch.sum(ex,1).unsqueeze(1))

        x_grad_line = x_grad.reshape(x_grad.size()[0]*x_grad.size()[1])
        for i in range(ci.size()[0]):
            index = ci[i,0,0,0].long()
            x_grad_line[index] = x_grad_line[index] - 1
        x_grad = x_grad_line.reshape(x_grad.size()[0],x_grad.size()[1],x_grad.size()[2],x_grad.size()[3])
        x_grad = grad_output.mul(x_grad)
        x_grad = x_grad * x.size()[1]
        #print('x_grad',x_grad[0,:,:,:])
        return x_grad , tmp_grad
//  [python]
def backward_var(self, grad, index, **kwargs):
        (a,) = self.variables
        axis: Optional[Tuple[int, ...]] = self.axis
        if isinstance(axis, Sequence) and len(axis) == 0:
            return np.zeros(a.shape, dtype=float)

        N = a.size if axis is None else np.multiply.reduce([a.shape[i] for i in axis])
        N -= self.ddof

        grad = self._grad_preprocess(grad)
        if grad.ndim == 0:
            # keepdims=False and axis=None (or all axes)
            grad = np.full(a.shape, grad, dtype=float)
        else:
            axis: Tuple[int, ...]
            if not self.keepdims:
                index = [slice(None)] * a.ndim
                for i in axis:
                    index[i] = np.newaxis
                grad = grad[tuple(index)]
        back = (2.0 / N) * (a.data - a.data.mean(axis=axis, keepdims=True))
        return back * grad
//  [python]
def backward_var(self, grad, index, **kwargs):
        (a,) = self.variables
        axis: Optional[Tuple[int, ...]] = self.axis
        if isinstance(axis, Sequence) and len(axis) == 0:
            return np.zeros(a.shape, dtype=float)

        N = a.size if axis is None else np.prod([a.shape[i] for i in axis])
        N -= self.ddof

        grad = self._grad_preprocess(grad)
        if grad.ndim == 0:
            # keepdims=False and axis=None (or all axes)
            grad = np.full(a.shape, grad, dtype=float)
        else:
            axis: Tuple[int, ...]
            if not self.keepdims:
                index = [slice(None)] * a.ndim
                for i in axis:
                    index[i] = np.newaxis
                grad = grad[tuple(index)]
        back = (2.0 / N) * (a.data - a.data.mean(axis=axis, keepdims=True))
        return back * grad
//  [python]
def n_elements(self) -> int:
        """Number of spatial elements/points"""
        n_elem = int(np.prod(self.shape))
        if self.n_timesteps > 1:
            n_elem = int(n_elem / self.n_timesteps)
        return n_elem
//  [python]
def bbox_iou(bbox_a, bbox_b, offset=0):
    """Calculate Intersection-Over-Union(IOU) of two bounding boxes.

    Parameters
    ----------
    bbox_a : numpy.ndarray
        An ndarray with shape :math:`(N, 4)`.
    bbox_b : numpy.ndarray
        An ndarray with shape :math:`(M, 4)`.
    offset : float or int, default is 0
        The ``offset`` is used to control the whether the width(or height) is computed as
        (right - left + ``offset``).
        Note that the offset must be 0 for normalized bboxes, whose ranges are in ``[0, 1]``.

    Returns
    -------
    numpy.ndarray
        An ndarray with shape :math:`(N, M)` indicates IOU between each pairs of
        bounding boxes in `bbox_a` and `bbox_b`.

    """
    if bbox_a.shape[1] < 4 or bbox_b.shape[1] < 4:
        raise IndexError("Bounding boxes axis 1 must have at least length 4")

    tl = np.maximum(bbox_a[:, None, :2], bbox_b[:, :2])
    br = np.minimum(bbox_a[:, None, 2:4], bbox_b[:, 2:4])

    area_i = np.multiply.reduce(br - tl + offset, axis=2) * (tl < br).all(axis=2)
    area_a = np.multiply.reduce(bbox_a[:, 2:4] - bbox_a[:, :2] + offset, axis=1)
    area_b = np.multiply.reduce(bbox_b[:, 2:4] - bbox_b[:, :2] + offset, axis=1)
    return area_i / (area_a[:, None] + area_b - area_i)
//  [python]
def bbox_iou(bbox_a, bbox_b, offset=0):
    """Calculate Intersection-Over-Union(IOU) of two bounding boxes.

    Parameters
    ----------
    bbox_a : numpy.ndarray
        An ndarray with shape :math:`(N, 4)`.
    bbox_b : numpy.ndarray
        An ndarray with shape :math:`(M, 4)`.
    offset : float or int, default is 0
        The ``offset`` is used to control the whether the width(or height) is computed as
        (right - left + ``offset``).
        Note that the offset must be 0 for normalized bboxes, whose ranges are in ``[0, 1]``.

    Returns
    -------
    numpy.ndarray
        An ndarray with shape :math:`(N, M)` indicates IOU between each pairs of
        bounding boxes in `bbox_a` and `bbox_b`.

    """
    if bbox_a.shape[1] < 4 or bbox_b.shape[1] < 4:
        raise IndexError("Bounding boxes axis 1 must have at least length 4")

    tl = np.maximum(bbox_a[:, None, :2], bbox_b[:, :2])
    br = np.minimum(bbox_a[:, None, 2:4], bbox_b[:, 2:4])

    area_i = np.prod(br - tl + offset, axis=2) * (tl < br).all(axis=2)
    area_a = np.prod(bbox_a[:, 2:4] - bbox_a[:, :2] + offset, axis=1)
    area_b = np.prod(bbox_b[:, 2:4] - bbox_b[:, :2] + offset, axis=1)
    return area_i / (area_a[:, None] + area_b - area_i)
//  [python]
def _compare_onnx_pytorch_outputs_in_np(
    onnx_outs: _OutputsType,
    pt_outs: _OutputsType,
    options: VerificationOptions,
):
    assert len(onnx_outs) == len(
        pt_outs
    ), f"Number of outputs differ ONNX runtime: ({len(onnx_outs)}) PyTorch: ({len(pt_outs)})"
    acceptable_error_percentage = options.acceptable_error_percentage
    if acceptable_error_percentage and (
        acceptable_error_percentage > 1.0 or acceptable_error_percentage < 0.0
    ):
        raise ValueError(
            "If set, acceptable_error_percentage should be between 0.0 and 1.0"
        )

    for ort_out, pt_out in zip(onnx_outs, pt_outs):
        try:
            # TODO: Remove `check_shape` option once every shape inconsistent issue is addressed.
            if not options.check_shape:
                # Allow different but broadcastable output shapes.
                ort_out, pt_out = np.broadcast_arrays(ort_out, pt_out)
            torch.testing.assert_close(
                ort_out,
                pt_out,
                rtol=options.rtol,
                atol=options.atol,
                check_dtype=options.check_dtype,
                equal_nan=True,
            )
        except AssertionError as e:
            if acceptable_error_percentage:
                error_percentage = 1 - np.sum(
                    np.isclose(ort_out, pt_out, rtol=options.rtol, atol=options.atol)
                ) / np.multiply.reduce(ort_out.shape)
                if error_percentage <= acceptable_error_percentage:
                    warnings.warn(
                        f"Suppressed AssertionError:\n{e}.\n"
                        f"Error percentage {error_percentage} "
                        f"within acceptable range {acceptable_error_percentage}."
                    )
                    continue
            if ort_out.dtype == np.uint8 or ort_out.dtype == np.int8:
                warnings.warn("ONNX output is quantized")
            if pt_out.dtype == np.uint8 or pt_out.dtype == np.int8:
                warnings.warn("PyTorch output is quantized")
            raise