jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def conv2d_forward_batch(self, inputs, weights, bias, outputs,
                             padding, stride):
        num_filters = weights.shape[0]
        num_images, input_rows, input_cols, num_input_maps = inputs.shape
        kernel_shape = weights.shape[1:]
        num_output_pixels = outputs.shape[1] * outputs.shape[2]
        num_kernel_params = np.prod(kernel_shape)
        out_shape = (num_output_pixels, num_filters)

        for i in range(num_images):
            col = np.zeros((num_output_pixels, num_kernel_params),
                           dtype=self.dtype)
            brainstorm.handlers._cpuop.im2col(
                inputs[i].reshape(inputs[i].size),
                input_rows, input_cols, num_input_maps,
                kernel_shape[0], kernel_shape[1],
                padding, padding, padding, padding, stride[0],
                stride[1], col.reshape(col.size))

            reshaped_params = weights.reshape(num_filters, num_kernel_params)
            np.dot(col, reshaped_params.T, out=outputs[i].reshape(out_shape))

        outputs += bias.reshape((1, 1, 1, num_filters))
//  [python]
def process_shape(
    dim: Optional[int],
    shape: Union[None, int, Sequence[int]],
) -> Tuple[int, Sequence[int]]:
    """Make a shape pack."""
    if shape is None and dim is None:
        raise ValueError("Missing both, shape and embedding_dim")
    elif shape is not None and dim is not None:
        raise ValueError("Provided both, shape and embedding_dim")
    elif shape is None and dim is not None:
        shape = (dim,)
    elif isinstance(shape, int) and dim is None:
        dim = shape
        shape = (shape,)
    elif isinstance(shape, Sequence) and dim is None:
        shape = tuple(shape)
        dim = int(n.prod(shape))
    else:
        raise TypeError(f"Invalid type for shape: ({type(shape)}) {shape}")
    return dim, shape
//  [python]
def process_shape(
    dim: Optional[int],
    shape: Union[None, int, Sequence[int]],
) -> Tuple[int, Sequence[int]]:
    """Make a shape pack."""
    if shape is None and dim is None:
        raise ValueError("Missing both, shape and embedding_dim")
    elif shape is not None and dim is not None:
        raise ValueError("Provided both, shape and embedding_dim")
    elif shape is None and dim is not None:
        shape = (dim,)
    elif isinstance(shape, int) and dim is None:
        dim = shape
        shape = (shape,)
    elif isinstance(shape, Sequence) and dim is None:
        shape = tuple(shape)
        dim = int(np.prod(shape))
    else:
        raise TypeError(f"Invalid type for shape: ({type(shape)}) {shape}")
    return dim, shape
//  [python]
def conv2d_backward_batch(self, inputs, params, padding, stride,
                              in_deltas, out_deltas, dparams, dbias):
        num_filters = params.shape[0]
        num_images, input_rows, input_cols, num_input_maps = inputs.shape
        kernel_shape = params.shape[1:]
        num_output_pixels = out_deltas.shape[1] * out_deltas.shape[2]
        num_kernel_params = np.multiply.reduce(kernel_shape)

        dparams.fill(0.0)
        dbias.fill(0.0)
        col = np.zeros((num_output_pixels, num_kernel_params),
                       dtype=self.dtype)

        for i in range(num_images):
            brainstorm.handlers._cpuop.im2col(
                inputs[i].reshape(inputs[i].size),
                input_rows, input_cols, num_input_maps,
                kernel_shape[0], kernel_shape[1],
                padding, padding, padding, padding,
                stride[0], stride[1], col.reshape(col.size))

            # Compute gradients
            reshaped_dparams = dparams.reshape(num_filters, num_kernel_params)
            reshaped_out_deltas = out_deltas[i].reshape((num_output_pixels,
                                                         num_filters))
            self.dot_add_mm(reshaped_out_deltas, col, out=reshaped_dparams,
                            transa=True)
            dbias += np.sum(reshaped_out_deltas, axis=0)

            # Compute in_deltas
            reshaped_params = params.reshape((num_filters, num_kernel_params))
            np.dot(reshaped_out_deltas, reshaped_params, out=col)
            brainstorm.handlers._cpuop.col2im(
                col.reshape(col.size),
                input_rows, input_cols, num_input_maps,
                kernel_shape[0], kernel_shape[1],
                padding, padding, padding, padding,
                stride[0], stride[1], in_deltas[i].reshape(in_deltas[i].size))
//  [python]
def conv2d_backward_batch(self, inputs, params, padding, stride,
                              in_deltas, out_deltas, dparams, dbias):
        num_filters = params.shape[0]
        num_images, input_rows, input_cols, num_input_maps = inputs.shape
        kernel_shape = params.shape[1:]
        num_output_pixels = out_deltas.shape[1] * out_deltas.shape[2]
        num_kernel_params = np.prod(kernel_shape)

        dparams.fill(0.0)
        dbias.fill(0.0)
        col = np.zeros((num_output_pixels, num_kernel_params),
                       dtype=self.dtype)

        for i in range(num_images):
            brainstorm.handlers._cpuop.im2col(
                inputs[i].reshape(inputs[i].size),
                input_rows, input_cols, num_input_maps,
                kernel_shape[0], kernel_shape[1],
                padding, padding, padding, padding,
                stride[0], stride[1], col.reshape(col.size))

            # Compute gradients
            reshaped_dparams = dparams.reshape(num_filters, num_kernel_params)
            reshaped_out_deltas = out_deltas[i].reshape((num_output_pixels,
                                                         num_filters))
            self.dot_add_mm(reshaped_out_deltas, col, out=reshaped_dparams,
                            transa=True)
            dbias += np.sum(reshaped_out_deltas, axis=0)

            # Compute in_deltas
            reshaped_params = params.reshape((num_filters, num_kernel_params))
            np.dot(reshaped_out_deltas, reshaped_params, out=col)
            brainstorm.handlers._cpuop.col2im(
                col.reshape(col.size),
                input_rows, input_cols, num_input_maps,
                kernel_shape[0], kernel_shape[1],
                padding, padding, padding, padding,
                stride[0], stride[1], in_deltas[i].reshape(in_deltas[i].size))
//  [python]
def factor_sizes(cls, max_id: int, shape: Sequence[int], num_cores: int) -> Tuple[Sequence[int], Sequence[int]]:
        r"""Factor the representation shape into smaller shapes for the cores.

        :param max_id:
            the number of representations, "row count", $M$
        :param shape:
            the shape of an individual representation, "column count", $N$
        :param num_cores:
            the number of cores, $k$

        :return:
            a tuple (ms, ns) of positive integer sequences of length $k$ fulfilling

            .. math ::

                \prod \limits_{m_i \in ms} m_i \geq M

                \prod \limits_{n_i \in ns} n_i \geq N
        """
        m_k = int(math.ceil(max_id ** (1 / num_cores)))
        n_k = int(math.ceil(reduce(lambda x, y: x * y, shape) ** (1 / num_cores)))
        return [m_k] * num_cores, [n_k] * num_cores
//  [python]
def factor_sizes(cls, max_id: int, shape: Sequence[int], num_cores: int) -> Tuple[Sequence[int], Sequence[int]]:
        r"""Factor the representation shape into smaller shapes for the cores.

        :param max_id:
            the number of representations, "row count", $M$
        :param shape:
            the shape of an individual representation, "column count", $N$
        :param num_cores:
            the number of cores, $k$

        :return:
            a tuple (ms, ns) of positive integer sequences of length $k$ fulfilling

            .. math ::

                \prod \limits_{m_i \in ms} m_i \geq M

                \prod \limits_{n_i \in ns} n_i \geq N
        """
        m_k = int(math.ceil(max_id ** (1 / num_cores)))
        n_k = int(math.ceil(numpy.prod(shape) ** (1 / num_cores)))
        return [m_k] * num_cores, [n_k] * num_cores
//  [python]
def test_fromiter_comparison(self):
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
//  [python]
def test_fromiter_comparison(self):
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.all(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.all(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
//  [python]
def test_fromiter_bytes(self):
        # Ticket #1058
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
//  [python]
def test_fromiter_bytes(self):
        # Ticket #1058
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.all(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.all(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
//  [python]
def test_mask(self):
        d = detectors.detector_factory("Pilatus200k")
        dc = distortion.Distortion(d, empty=-1, method="csr")
        dc.reset(prepare=True)
        self.assertEqual(len(dc.lut[0]), n.prod(d.shape) - d.mask.sum(), "All empty bins have been removed")
        w = numpy.where(dc.lut[2][1:] == dc.lut[2][:-1])
        self.assertEqual(len(w[0]), d.mask.sum(), "masked pixels are all missing")
        a = numpy.random.randint(1, 100, size=d.shape)
        b = dc.correct_ng(a)
        self.assertGreater(a.min(), 0)  # 1 is the lowset
        self.assertLess(b.min(), 0)  # -1 have appeared
        self.assertLess(b.mean(), a.mean())
        from ..opencl import ocl, pyopencl
        if ocl is not None:
            ctx = ocl.create_context()
            odevice = ctx.devices[0]
            oplat = odevice.platform
            device_id = oplat.get_devices().index(odevice)
            platform_id = pyopencl.get_platforms().index(oplat)
            target = (platform_id, device_id)
            dc.reset("csr", target, prepare=True)
            w = numpy.where(dc.lut[2][1:] == dc.lut[2][:-1])
            self.assertEqual(len(w[0]), d.mask.sum(), "masked pixels are all missing, opencl")
            b = dc.correct_ng(a)
            self.assertGreater(a.min(), 0)  # 1 is the lowset
            self.assertLess(b.min(), 0)  # -1 have appeared
            self.assertLess(b.mean(), a.mean())
//  [python]
def test_mask(self):
        d = detectors.detector_factory("Pilatus200k")
        dc = distortion.Distortion(d, empty=-1, method="csr")
        dc.reset(prepare=True)
        self.assertEqual(len(dc.lut[0]), numpy.prod(d.shape) - d.mask.sum(), "All empty bins have been removed")
        w = numpy.where(dc.lut[2][1:] == dc.lut[2][:-1])
        self.assertEqual(len(w[0]), d.mask.sum(), "masked pixels are all missing")
        a = numpy.random.randint(1, 100, size=d.shape)
        b = dc.correct_ng(a)
        self.assertGreater(a.min(), 0)  # 1 is the lowset
        self.assertLess(b.min(), 0)  # -1 have appeared
        self.assertLess(b.mean(), a.mean())
        from ..opencl import ocl, pyopencl
        if ocl is not None:
            ctx = ocl.create_context()
            odevice = ctx.devices[0]
            oplat = odevice.platform
            device_id = oplat.get_devices().index(odevice)
            platform_id = pyopencl.get_platforms().index(oplat)
            target = (platform_id, device_id)
            dc.reset("csr", target, prepare=True)
            w = numpy.where(dc.lut[2][1:] == dc.lut[2][:-1])
            self.assertEqual(len(w[0]), d.mask.sum(), "masked pixels are all missing, opencl")
            b = dc.correct_ng(a)
            self.assertGreater(a.min(), 0)  # 1 is the lowset
            self.assertLess(b.min(), 0)  # -1 have appeared
            self.assertLess(b.mean(), a.mean())
//  [python]
def test_alltrue(self):
        self.assert_deprecated(lambda: np.alltrue(np.array([True, False])))
//  [python]
def test_alltrue(self):
        self.assert_deprecated(lambda: np.all(np.array([True, False])))
//  [python]
def test_nd(self):
        y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]]
        assert_(not np.all(y1))
        assert_array_equal(np.alltrue(y1, axis=0), [0, 0, 1])
        assert_array_equal(np.alltrue(y1, axis=1), [0, 0, 1])
//  [python]
def test_nd(self):
        y1 = [[0, 0, 1], [0, 1, 1], [1, 1, 1]]
        assert_(not np.all(y1))
        assert_array_equal(np.all(y1, axis=0), [0, 0, 1])
        assert_array_equal(np.all(y1, axis=1), [0, 0, 1])
//  [python]
def derivative(self, x, *args):
        if self.jac is not None and numpy.alltrue(x == self.x):
            return self.jac
        else:
            self(x, *args)
            return self.jac
//  [python]
def derivative(self, x, *args):
        if self.jac is not None and numpy.all(x == self.x):
            return self.jac
        else:
            self(x, *args)
            return self.jac
//  [python]
def check_factors(ms: Sequence[int], ns: Sequence[int], max_id: int, shape: Tuple[int, ...], num_cores: int):
        r"""
        Check whether the factors match the other parts.

        Verifies that

        .. math ::
            \prod \limits_{m_i \in ms} m_i \geq M
            \prod \limits_{n_i \in ns} n_i \geq N

        :param ms: length: num_cores
            the $M$ factors $m_i$
        :param ns: length: num_cores
            the $N$ factors $n_i$
        :param max_id:
            the maximum id, $M$
        :param shape:
            the shape, $N=prod(shape)$
        :param num_cores:
            the number of cores

        :raises ValueError:
            if any of the conditions is violated
        """
        if len(ms) != num_cores or len(ns) != num_cores:
            raise ValueError(f"Invalid length: len(ms)={len(ms)}, len(ns)={len(ns)} vs. num_cores={num_cores}")

        m_prod = n.prod(ms).item()
        if m_prod < max_id:
            raise ValueError(f"prod(ms)={m_prod} < max_id={max_id}")

        n_prod = n.prod(ns).item()
        s_prod = n.prod(shape).item()
        if n_prod < s_prod:
            raise ValueError(f"prod(ns)={n_prod} < prod(shape)={s_prod}")
//  [python]
def check_factors(ms: Sequence[int], ns: Sequence[int], max_id: int, shape: Tuple[int, ...], num_cores: int):
        r"""
        Check whether the factors match the other parts.

        Verifies that

        .. math ::
            \prod \limits_{m_i \in ms} m_i \geq M
            \prod \limits_{n_i \in ns} n_i \geq N

        :param ms: length: num_cores
            the $M$ factors $m_i$
        :param ns: length: num_cores
            the $N$ factors $n_i$
        :param max_id:
            the maximum id, $M$
        :param shape:
            the shape, $N=prod(shape)$
        :param num_cores:
            the number of cores

        :raises ValueError:
            if any of the conditions is violated
        """
        if len(ms) != num_cores or len(ns) != num_cores:
            raise ValueError(f"Invalid length: len(ms)={len(ms)}, len(ns)={len(ns)} vs. num_cores={num_cores}")

        m_prod = numpy.prod(ms).item()
        if m_prod < max_id:
            raise ValueError(f"prod(ms)={m_prod} < max_id={max_id}")

        n_prod = numpy.prod(ns).item()
        s_prod = numpy.prod(shape).item()
        if n_prod < s_prod:
            raise ValueError(f"prod(ns)={n_prod} < prod(shape)={s_prod}")
//  [python]
def test_values(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        assert_(np.alltrue(a == expected, axis=0))
        assert_(np.alltrue(a20 == expected[:20], axis=0))
//  [python]
def test_values(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        assert_(np.all(a == expected, axis=0))
        assert_(np.all(a20 == expected[:20], axis=0))
//  [python]
def read_where(self, condition, condvars=None, field=None,
                   start=None, stop=None, step=None):
        """Read table data fulfilling the given *condition*.

        This method is similar to :meth:`Table.read`, having their common
        arguments and return values the same meanings. However, only the rows
        fulfilling the *condition* are included in the result.

        The meaning of the other arguments is the same as in the
        :meth:`Table.where` method.

        """

        self._g_check_open()
        coords = [p.nrow for p in
                  self._where(condition, condvars, start, stop, step)]
        self._where_condition = None  # reset the conditions
        if len(coords) > 1:
            cstart, cstop = coords[0], coords[-1] + 1
            if cstop - cstart == len(coords):
                # Chances for monotonically increasing row values. Refine.
                inc_seq = numpy.alltrue(
                    numpy.arange(cstart, cstop) == numpy.array(coords))
                if inc_seq:
                    return self.read(cstart, cstop, field=field)
        return self.read_coordinates(coords, field)
//  [python]
def read_where(self, condition, condvars=None, field=None,
                   start=None, stop=None, step=None):
        """Read table data fulfilling the given *condition*.

        This method is similar to :meth:`Table.read`, having their common
        arguments and return values the same meanings. However, only the rows
        fulfilling the *condition* are included in the result.

        The meaning of the other arguments is the same as in the
        :meth:`Table.where` method.

        """

        self._g_check_open()
        coords = [p.nrow for p in
                  self._where(condition, condvars, start, stop, step)]
        self._where_condition = None  # reset the conditions
        if len(coords) > 1:
            cstart, cstop = coords[0], coords[-1] + 1
            if cstop - cstart == len(coords):
                # Chances for monotonically increasing row values. Refine.
                inc_seq = numpy.all(
                    numpy.arange(cstart, cstop) == numpy.array(coords))
                if inc_seq:
                    return self.read(cstart, cstop, field=field)
        return self.read_coordinates(coords, field)
//  [python]
def rank(X, cond=1.0e-12):
    """
    Return the rank of a matrix X based on its generalized inverse,
    not the SVD.
    """
    X = np.asarray(X)
    if len(X.shape) == 2:
        import scipy.linalg as SL
        D = SL.svdvals(X)
        result = np.add.reduce(np.greater(D / D.max(), cond))
        return int(result.astype(np.int32))
    else:
        return int(not np.alltrue(np.equal(X, 0.)))
//  [python]
def rank(X, cond=1.0e-12):
    """
    Return the rank of a matrix X based on its generalized inverse,
    not the SVD.
    """
    X = np.asarray(X)
    if len(X.shape) == 2:
        import scipy.linalg as SL
        D = SL.svdvals(X)
        result = np.add.reduce(np.greater(D / D.max(), cond))
        return int(result.astype(np.int32))
    else:
        return int(not np.all(np.equal(X, 0.)))
//  [python]
def test_values(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        self.assertTrue(np.alltrue(a == expected, axis=0))
        self.assertTrue(np.alltrue(a20 == expected[:20], axis=0))
//  [python]
def test_values(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        self.assertTrue(np.all(a == expected, axis=0))
        self.assertTrue(np.all(a20 == expected[:20], axis=0))
//  [python]
def validDir(self, dirstring):
        d = numpy.fromstring(dirstring, dtype=float, sep=",")
        if len(d) == 3:
            return numpy.alltrue(numpy.isfinite(d))
        return False
//  [python]
def validDir(self, dirstring):
        d = numpy.fromstring(dirstring, dtype=float, sep=",")
        if len(d) == 3:
            return numpy.all(numpy.isfinite(d))
        return False
//  [python]
def zero_pad(image, shape, position='corner'):
    """
    Extends image to a certain size with zeros
    Parameters
    ----------
    image: real 2d `numpy.ndarray`
        Input image
    shape: tuple of int
        Desired output shape of the image
    position : str, optional
        The position of the input image in the output one:
            * 'corner'
                top-left corner (default)
            * 'center'
                centered
    Returns
    -------
    padded_img: real `numpy.ndarray`
        The zero-padded image
    """
    shape = np.asarray(shape, dtype=int)
    imshape = np.asarray(image.shape, dtype=int)
    if np.alltrue(imshape == shape):
        return image
    if np.any(shape <= 0):
        raise ValueError("ZERO_PAD: null or negative shape given")
    dshape = shape - imshape
    if np.any(dshape < 0):
        raise ValueError("ZERO_PAD: target size smaller than source one")
    pad_img = np.zeros(shape, dtype=image.dtype)
    idx, idy = np.indices(imshape)
    if position == 'center':
        if np.any(dshape % 2 != 0):
            raise ValueError("ZERO_PAD: source and target shapes "
                             "have different parity.")
        offx, offy = dshape // 2
    else:
        offx, offy = (0, 0)
    pad_img[idx + offx, idy + offy] = image
    return pad_img
//  [python]
def zero_pad(image, shape, position='corner'):
    """
    Extends image to a certain size with zeros
    Parameters
    ----------
    image: real 2d `numpy.ndarray`
        Input image
    shape: tuple of int
        Desired output shape of the image
    position : str, optional
        The position of the input image in the output one:
            * 'corner'
                top-left corner (default)
            * 'center'
                centered
    Returns
    -------
    padded_img: real `numpy.ndarray`
        The zero-padded image
    """
    shape = np.asarray(shape, dtype=int)
    imshape = np.asarray(image.shape, dtype=int)
    if np.all(imshape == shape):
        return image
    if np.any(shape <= 0):
        raise ValueError("ZERO_PAD: null or negative shape given")
    dshape = shape - imshape
    if np.any(dshape < 0):
        raise ValueError("ZERO_PAD: target size smaller than source one")
    pad_img = np.zeros(shape, dtype=image.dtype)
    idx, idy = np.indices(imshape)
    if position == 'center':
        if np.any(dshape % 2 != 0):
            raise ValueError("ZERO_PAD: source and target shapes "
                             "have different parity.")
        offx, offy = dshape // 2
    else:
        offx, offy = (0, 0)
    pad_img[idx + offx, idy + offy] = image
    return pad_img
//  [python]
def alltrue(x, axis=0):
    return np.alltrue(x, axis)
//  [python]
def alltrue(x, axis=0):
    return np.all(x, axis)
//  [python]
def test_1d_default_roundtrip(self):
        """ roundtrip test call with default values """
        a = Fr.frgetvect1d("./test.dat","Adc1")
        Fr.frputvect('writetest.gwf', [{'name':'Adc1', 'data':a[0],
            'start':a[1], 'dx':a[3], 'kind':'ADC', 'x_unit':a[4],
            'y_unit':a[5]}])
        b = Fr.frgetvect1d("writetest.gwf", "Adc1")
        self.assert_(numpy.alltrue(a[0] == b[0]))
        self.assert_(numpy.alltrue(a[1:] == b[1:]))
        os.remove("writetest.gwf")
//  [python]
def test_1d_default_roundtrip(self):
        """ roundtrip test call with default values """
        a = Fr.frgetvect1d("./test.dat","Adc1")
        Fr.frputvect('writetest.gwf', [{'name':'Adc1', 'data':a[0],
            'start':a[1], 'dx':a[3], 'kind':'ADC', 'x_unit':a[4],
            'y_unit':a[5]}])
        b = Fr.frgetvect1d("writetest.gwf", "Adc1")
        self.assert_(numpy.all(a[0] == b[0]))
        self.assert_(numpy.all(a[1:] == b[1:]))
        os.remove("writetest.gwf")
//  [python]
def _min_or_max_filter(input, size, footprint, structure, output, mode,
                       cval, origin, minimum):
    if structure is None:
        if footprint is None:
            if size is None:
                raise RuntimeError("no footprint provided")
            separable = True
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
            if numpy.alltrue(numpy.ravel(footprint), axis=0):
                size = footprint.shape
                footprint = None
                separable = True
            else:
                separable = False
    else:
        structure = numpy.asarray(structure, dtype=numpy.float64)
        separable = False
        if footprint is None:
            footprint = numpy.ones(structure.shape, bool)
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input)
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if separable:
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        axes = list(range(input.ndim))
        axes = [(axes[ii], sizes[ii], origins[ii])
                               for ii in range(len(axes)) if sizes[ii] > 1]
        if minimum:
            filter_ = minimum_filter1d
        else:
            filter_ = maximum_filter1d
        if len(axes) > 0:
            for axis, size, origin in axes:
                filter_(input, int(size), axis, output, mode, cval, origin)
                input = output
        else:
            output[...] = input[...]
    else:
        fshape = [ii for ii in footprint.shape if ii > 0]
        if len(fshape) != input.ndim:
            raise RuntimeError('footprint array has incorrect shape.')
        for origin, lenf in zip(origins, fshape):
            if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
                raise ValueError('invalid origin')
        if not footprint.flags.contiguous:
            footprint = footprint.copy()
        if structure is not None:
            if len(structure.shape) != input.ndim:
                raise RuntimeError('structure array has incorrect shape')
            if not structure.flags.contiguous:
                structure = structure.copy()
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.min_or_max_filter(input, footprint, structure, output,
                                    mode, cval, origins, minimum)
    return return_value
//  [python]
def _min_or_max_filter(input, size, footprint, structure, output, mode,
                       cval, origin, minimum):
    if structure is None:
        if footprint is None:
            if size is None:
                raise RuntimeError("no footprint provided")
            separable = True
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
            if numpy.all(numpy.ravel(footprint), axis=0):
                size = footprint.shape
                footprint = None
                separable = True
            else:
                separable = False
    else:
        structure = numpy.asarray(structure, dtype=numpy.float64)
        separable = False
        if footprint is None:
            footprint = numpy.ones(structure.shape, bool)
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input)
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if separable:
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        axes = list(range(input.ndim))
        axes = [(axes[ii], sizes[ii], origins[ii])
                               for ii in range(len(axes)) if sizes[ii] > 1]
        if minimum:
            filter_ = minimum_filter1d
        else:
            filter_ = maximum_filter1d
        if len(axes) > 0:
            for axis, size, origin in axes:
                filter_(input, int(size), axis, output, mode, cval, origin)
                input = output
        else:
            output[...] = input[...]
    else:
        fshape = [ii for ii in footprint.shape if ii > 0]
        if len(fshape) != input.ndim:
            raise RuntimeError('footprint array has incorrect shape.')
        for origin, lenf in zip(origins, fshape):
            if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
                raise ValueError('invalid origin')
        if not footprint.flags.contiguous:
            footprint = footprint.copy()
        if structure is not None:
            if len(structure.shape) != input.ndim:
                raise RuntimeError('structure array has incorrect shape')
            if not structure.flags.contiguous:
                structure = structure.copy()
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.min_or_max_filter(input, footprint, structure, output,
                                    mode, cval, origins, minimum)
    return return_value
//  [python]
def test_1d_keywords_roundtrip(self):
        """ roundtrip test call with keyword arguments """
        a = Fr.frgetvect1d("./test.dat", "Adc1", span=1)
        Fr.frputvect('writetest.gwf', [{'name':'Adc1', 'data':a[0],
        'start':a[1], 'dx':a[3], 'kind':'ADC', 'x_unit':a[4],
        'y_unit':a[5]}])
        b = Fr.frgetvect1d("writetest.gwf", "Adc1")
        self.assert_(numpy.alltrue(a[0] == b[0]))
        self.assert_(numpy.alltrue(a[1:] == b[1:]))
        os.remove("writetest.gwf")
//  [python]
def test_1d_keywords_roundtrip(self):
        """ roundtrip test call with keyword arguments """
        a = Fr.frgetvect1d("./test.dat", "Adc1", span=1)
        Fr.frputvect('writetest.gwf', [{'name':'Adc1', 'data':a[0],
        'start':a[1], 'dx':a[3], 'kind':'ADC', 'x_unit':a[4],
        'y_unit':a[5]}])
        b = Fr.frgetvect1d("writetest.gwf", "Adc1")
        self.assert_(numpy.all(a[0] == b[0]))
        self.assert_(numpy.all(a[1:] == b[1:]))
        os.remove("writetest.gwf")
//  [python]
def test_pixfun_real_c():

    filename = 'data/pixfun_real_c.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/cint_sar.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.alltrue(data == refdata.real)
//  [python]
def test_pixfun_real_c():

    filename = 'data/pixfun_real_c.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/cint_sar.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.all(data == refdata.real)
//  [python]
def project_simplex(v, s=1):
    """
    Taken from https://gist.github.com/daien/1272551/edd95a6154106f8e28209a1c7964623ef8397246.

    Compute the Euclidean projection on a positive simplex
    Solves the optimisation problem (using the algorithm from [1]):
        min_w 0.5 * || w - v ||_2^2 , s.t. sum_i w_i = s, w_i >= 0
    Parameters
    ----------
    v: (n,) numpy array,
       n-dimensional vector to project
    s: int, optional, default: 1,
       radius of the simplex
    Returns
    -------
    w: (n,) numpy array,
       Euclidean projection of v on the simplex
    Notes
    -----
    The complexity of this algorithm is in O(n log(n)) as it involves sorting v.
    Better alternatives exist for high-dimensional sparse vectors (cf. [1])
    However, this implementation still easily scales to millions of dimensions.
    References
    ----------
    [1] Efficient Projections onto the .1-Ball for Learning in High Dimensions
        John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra.
        International Conference on Machine Learning (ICML 2008)
        http://www.cs.berkeley.edu/~jduchi/projects/DuchiSiShCh08.pdf
    """

    assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s

    n, = v.shape  # will raise ValueError if v is not 1-D
    # check if we are already on the simplex
    if v.sum() == s and numpy.alltrue(v >= 0):
        # best projection: itself!
        return v
    # get the array of cumulative sums of a sorted (decreasing) copy of v
    u = numpy.sort(v)[::-1]
    cssv = numpy.cumsum(u)
    # get the number of > 0 components of the optimal solution
    rho = numpy.nonzero(u * numpy.arange(1, n+1) > (cssv - s))[0][-1]
    # compute the Lagrange multiplier associated to the simplex constraint
    theta = float(cssv[rho] - s) / rho
    # compute the projection by thresholding v using theta
    w = (v - theta).clip(min=0)
    return w
//  [python]
def project_simplex(v, s=1):
    """
    Taken from https://gist.github.com/daien/1272551/edd95a6154106f8e28209a1c7964623ef8397246.

    Compute the Euclidean projection on a positive simplex
    Solves the optimisation problem (using the algorithm from [1]):
        min_w 0.5 * || w - v ||_2^2 , s.t. sum_i w_i = s, w_i >= 0
    Parameters
    ----------
    v: (n,) numpy array,
       n-dimensional vector to project
    s: int, optional, default: 1,
       radius of the simplex
    Returns
    -------
    w: (n,) numpy array,
       Euclidean projection of v on the simplex
    Notes
    -----
    The complexity of this algorithm is in O(n log(n)) as it involves sorting v.
    Better alternatives exist for high-dimensional sparse vectors (cf. [1])
    However, this implementation still easily scales to millions of dimensions.
    References
    ----------
    [1] Efficient Projections onto the .1-Ball for Learning in High Dimensions
        John Duchi, Shai Shalev-Shwartz, Yoram Singer, and Tushar Chandra.
        International Conference on Machine Learning (ICML 2008)
        http://www.cs.berkeley.edu/~jduchi/projects/DuchiSiShCh08.pdf
    """

    assert s > 0, "Radius s must be strictly positive (%d <= 0)" % s

    n, = v.shape  # will raise ValueError if v is not 1-D
    # check if we are already on the simplex
    if v.sum() == s and numpy.all(v >= 0):
        # best projection: itself!
        return v
    # get the array of cumulative sums of a sorted (decreasing) copy of v
    u = numpy.sort(v)[::-1]
    cssv = numpy.cumsum(u)
    # get the number of > 0 components of the optimal solution
    rho = numpy.nonzero(u * numpy.arange(1, n+1) > (cssv - s))[0][-1]
    # compute the Lagrange multiplier associated to the simplex constraint
    theta = float(cssv[rho] - s) / rho
    # compute the projection by thresholding v using theta
    w = (v - theta).clip(min=0)
    return w
//  [python]
def test_1d_two_channels_roundtrip(self):
        """ roundtrip test call with two channels in a frame """
        a = Fr.frgetvect1d("./test.dat","Adc1")
        Fr.frputvect('writetest.gwf', [{'name':'Adc1', 'data':a[0],
        'start':a[1], 'dx':a[3], 'kind':'ADC', 'x_unit':a[4],
        'y_unit':a[5]},{'name':'reverse', 'data':a[0][::-1], 'start':a[1],
        'dx':a[3], 'kind':'ADC', 'x_unit':a[4], 'y_unit': a[5]}])
        b = Fr.frgetvect1d("writetest.gwf", "Adc1")
        self.assert_(numpy.alltrue(a[0] == b[0]))
        self.assert_(numpy.alltrue(a[1:] == b[1:]))

        c = Fr.frgetvect1d("writetest.gwf", "reverse")
        self.assert_(numpy.alltrue(a[0][::-1] == c[0]))
        self.assert_(numpy.alltrue(a[1:] == c[1:]))
        os.remove("writetest.gwf")
//  [python]
def test_1d_two_channels_roundtrip(self):
        """ roundtrip test call with two channels in a frame """
        a = Fr.frgetvect1d("./test.dat","Adc1")
        Fr.frputvect('writetest.gwf', [{'name':'Adc1', 'data':a[0],
        'start':a[1], 'dx':a[3], 'kind':'ADC', 'x_unit':a[4],
        'y_unit':a[5]},{'name':'reverse', 'data':a[0][::-1], 'start':a[1],
        'dx':a[3], 'kind':'ADC', 'x_unit':a[4], 'y_unit': a[5]}])
        b = Fr.frgetvect1d("writetest.gwf", "Adc1")
        self.assert_(numpy.all(a[0] == b[0]))
        self.assert_(numpy.all(a[1:] == b[1:]))

        c = Fr.frgetvect1d("writetest.gwf", "reverse")
        self.assert_(numpy.all(a[0][::-1] == c[0]))
        self.assert_(numpy.all(a[1:] == c[1:]))
        os.remove("writetest.gwf")
//  [python]
def _min_or_max_filter(input, size, footprint, structure, output, mode,
                       cval, origin, minimum):
    if structure is None:
        if footprint is None:
            if size is None:
                raise RuntimeError("no footprint provided")
            separable = True
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
            if not footprint.any():
                raise ValueError("All-zero footprint is not supported.")
            if numpy.alltrue(numpy.ravel(footprint), axis=0):
                size = footprint.shape
                footprint = None
                separable = True
            else:
                separable = False
    else:
        structure = numpy.asarray(structure, dtype=numpy.float64)
        separable = False
        if footprint is None:
            footprint = numpy.ones(structure.shape, bool)
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input)
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if separable:
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        modes = _ni_support._normalize_sequence(mode, input.ndim)
        axes = list(range(input.ndim))
        axes = [(axes[ii], sizes[ii], origins[ii], modes[ii])
                               for ii in range(len(axes)) if sizes[ii] > 1]
        if minimum:
            filter_ = minimum_filter1d
        else:
            filter_ = maximum_filter1d
        if len(axes) > 0:
            for axis, size, origin, mode in axes:
                filter_(input, int(size), axis, output, mode, cval, origin)
                input = output
        else:
            output[...] = input[...]
    else:
        fshape = [ii for ii in footprint.shape if ii > 0]
        if len(fshape) != input.ndim:
            raise RuntimeError('footprint array has incorrect shape.')
        for origin, lenf in zip(origins, fshape):
            if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
                raise ValueError('invalid origin')
        if not footprint.flags.contiguous:
            footprint = footprint.copy()
        if structure is not None:
            if len(structure.shape) != input.ndim:
                raise RuntimeError('structure array has incorrect shape')
            if not structure.flags.contiguous:
                structure = structure.copy()
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.min_or_max_filter(input, footprint, structure, output,
                                    mode, cval, origins, minimum)
    return return_value
//  [python]
def _min_or_max_filter(input, size, footprint, structure, output, mode,
                       cval, origin, minimum):
    if structure is None:
        if footprint is None:
            if size is None:
                raise RuntimeError("no footprint provided")
            separable = True
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
            if not footprint.any():
                raise ValueError("All-zero footprint is not supported.")
            if numpy.all(numpy.ravel(footprint), axis=0):
                size = footprint.shape
                footprint = None
                separable = True
            else:
                separable = False
    else:
        structure = numpy.asarray(structure, dtype=numpy.float64)
        separable = False
        if footprint is None:
            footprint = numpy.ones(structure.shape, bool)
        else:
            footprint = numpy.asarray(footprint)
            footprint = footprint.astype(bool)
    input = numpy.asarray(input)
    if numpy.iscomplexobj(input):
        raise TypeError('Complex type not supported')
    output, return_value = _ni_support._get_output(output, input)
    origins = _ni_support._normalize_sequence(origin, input.ndim)
    if separable:
        sizes = _ni_support._normalize_sequence(size, input.ndim)
        modes = _ni_support._normalize_sequence(mode, input.ndim)
        axes = list(range(input.ndim))
        axes = [(axes[ii], sizes[ii], origins[ii], modes[ii])
                               for ii in range(len(axes)) if sizes[ii] > 1]
        if minimum:
            filter_ = minimum_filter1d
        else:
            filter_ = maximum_filter1d
        if len(axes) > 0:
            for axis, size, origin, mode in axes:
                filter_(input, int(size), axis, output, mode, cval, origin)
                input = output
        else:
            output[...] = input[...]
    else:
        fshape = [ii for ii in footprint.shape if ii > 0]
        if len(fshape) != input.ndim:
            raise RuntimeError('footprint array has incorrect shape.')
        for origin, lenf in zip(origins, fshape):
            if (lenf // 2 + origin < 0) or (lenf // 2 + origin >= lenf):
                raise ValueError('invalid origin')
        if not footprint.flags.contiguous:
            footprint = footprint.copy()
        if structure is not None:
            if len(structure.shape) != input.ndim:
                raise RuntimeError('structure array has incorrect shape')
            if not structure.flags.contiguous:
                structure = structure.copy()
        mode = _ni_support._extend_mode_to_code(mode)
        _nd_image.min_or_max_filter(input, footprint, structure, output,
                                    mode, cval, origins, minimum)
    return return_value
//  [python]
def test_pixfun_imag_r():

    filename = 'data/pixfun_imag_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    assert numpy.alltrue(data == 0)
//  [python]
def test_pixfun_imag_r():

    filename = 'data/pixfun_imag_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    assert numpy.all(data == 0)
//  [python]
def test_pixfun_real_r():

    filename = 'data/pixfun_real_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/int32.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.alltrue(data == refdata.real)
//  [python]
def test_pixfun_real_r():

    filename = 'data/pixfun_real_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/int32.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.all(data == refdata.real)
//  [python]
def test_pixfun_imag_c():

    filename = 'data/pixfun_imag_c.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/cint_sar.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.alltrue(data == refdata.imag)

    # Test bugfix of #6599
    copied_ds = gdal.Translate('', filename, format='MEM')
    data_ds = copied_ds.GetRasterBand(1).ReadAsArray()
    copied_ds = None

    assert numpy.alltrue(data == data_ds)
//  [python]
def test_pixfun_imag_c():

    filename = 'data/pixfun_imag_c.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/cint_sar.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.all(data == refdata.imag)

    # Test bugfix of #6599
    copied_ds = gdal.Translate('', filename, format='MEM')
    data_ds = copied_ds.GetRasterBand(1).ReadAsArray()
    copied_ds = None

    assert numpy.all(data == data_ds)
//  [python]
def test_pixfun_phase_r():

    filename = 'data/pixfun_phase_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/pixfun_imag_c.vrt'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.alltrue(data == numpy.arctan2(0, refdata))
//  [python]
def test_pixfun_phase_r():

    filename = 'data/pixfun_phase_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/pixfun_imag_c.vrt'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.all(data == numpy.arctan2(0, refdata))
//  [python]
def test_pixfun_mod_r():

    filename = 'data/pixfun_mod_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/int32.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    res = numpy.alltrue(data == numpy.abs(refdata))
    if gdaltest.is_travis_branch('sanitize') and not res:
        print(data)
        print(numpy.abs(refdata))
        pytest.xfail()

    assert res
//  [python]
def test_pixfun_mod_r():

    filename = 'data/pixfun_mod_r.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/int32.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    res = numpy.all(data == numpy.abs(refdata))
    if gdaltest.is_travis_branch('sanitize') and not res:
        print(data)
        print(numpy.abs(refdata))
        pytest.xfail()

    assert res
//  [python]
def test_pixfun_conj_c():

    filename = 'data/pixfun_conj_c.vrt'
    ds = gdal.OpenShared(filename, gdal.GA_ReadOnly)
    assert ds is not None, ('Unable to open "%s" dataset.' % filename)
    data = ds.GetRasterBand(1).ReadAsArray()

    reffilename = 'data/cint_sar.tif'
    refds = gdal.Open(reffilename)
    assert refds is not None, ('Unable to open "%s" dataset.' % reffilename)
    refdata = refds.GetRasterBand(1).ReadAsArray()

    assert numpy.alltrue(data == numpy.conj(refdata))