jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _fit(self, X, handle_unknown="error"):
        X_list, n_samples, n_features = self._check_X(X)

        if self.categories != "auto":
            if len(self.categories) != n_features:
                raise ValueError("Shape mismatch: if categories is an array," " it has to be of shape (n_features,).")

        if self.max_levels is not None:
            if not isinstance(self.max_levels, Integral) or self.max_levels <= 0:
                raise ValueError("max_levels must be None or a strictly " "positive int, got {}.".format(self.max_levels))

        self.categories_ = []
        self.infrequent_indices_ = []

        for i in range(n_features):
            Xi = X_list[i]
            if self.categories == "auto":
                cats = _encode(Xi)
            else:
                cats = np.array(self.categories[i], dtype=Xi.dtype)
                if Xi.dtype != object:
                    if not np.all(np.sort(cats) == cats):
                        raise ValueError("Unsorted categories are not " "supported for numerical categories")
                if handle_unknown == "error":
                    diff = _encode_check_unknown(Xi, cats)
                    if diff:
                        msg = "Found unknown categories {0} in column {1}" " during fit".format(diff, i)
                        raise ValueError(msg)
            self.categories_.append(cats)

            if self.max_levels is not None:
                infrequent_indices = self._find_infrequent_category_indices(Xi)
            else:
                infrequent_indices = np.array([])
            self.infrequent_indices_.append(infrequent_indices)
//  [python]
def last_index(X):
    timestamps_infinite = n.all(~n.isfinite(X), axis=1)  # Are there NaNs padded after the TS?
    if n.alltrue(~timestamps_infinite):
        idx = X.shape[0]
    else:  # Yes? then remove them
        idx = n.nonzero(timestamps_infinite)[0][0]
    return idx
//  [python]
def last_index(X):
    timestamps_infinite = numpy.all(~numpy.isfinite(X), axis=1)  # Are there NaNs padded after the TS?
    if numpy.alltrue(~timestamps_infinite):
        idx = X.shape[0]
    else:  # Yes? then remove them
        idx = numpy.nonzero(timestamps_infinite)[0][0]
    return idx
//  [python]
def test_large_ci(self):
        res = fci.addons.large_ci(ci0, norb, nelec, tol=.1)
        refstr =[('0b111'  , '0b111'  ),
                 ('0b111'  , '0b1011' ),
                 ('0b1011' , '0b111'  ),
                 ('0b1011' , '0b1011' ),
                 ('0b10101', '0b10101')]
        refci = [0.86848550920009038, 0.15130668599599939, 0.15130668599597297,
                 0.36620088911284837, 0.10306162063159749]
        self.assertTrue(numpy.allclose([abs(x[0]) for x in res], refci))
        self.assertEqual([x[1:] for x in res], refstr)

        res = fci.addons.large_ci(ci0, norb, nelec, tol=.1, return_strs=False)
        refa = numpy.array(((0,1,2), (0,1,2), (0,1,3), (0,1,3), (0,2,4)))
        refb = numpy.array(((0,1,2), (0,1,3), (0,1,2), (0,1,3), (0,2,4)))
        self.assertTrue(numpy.alltrue([x[1] for x in res] == refa))
        self.assertTrue(numpy.alltrue([x[2] for x in res] == refb))

        na = fci.cistring.num_strings(6, 3)
        numpy.random.seed(9)
        ci1 = numpy.random.random((na,na))
        ci1 /= numpy.linalg.norm(ci1)
        res = fci.addons.large_ci(ci1, 6, (3,3), tol=.2)
        self.assertEqual(res[0][1:], ('0b110100', '0b1101'))
//  [python]
def test_large_ci(self):
        res = fci.addons.large_ci(ci0, norb, nelec, tol=.1)
        refstr =[('0b111'  , '0b111'  ),
                 ('0b111'  , '0b1011' ),
                 ('0b1011' , '0b111'  ),
                 ('0b1011' , '0b1011' ),
                 ('0b10101', '0b10101')]
        refci = [0.86848550920009038, 0.15130668599599939, 0.15130668599597297,
                 0.36620088911284837, 0.10306162063159749]
        self.assertTrue(numpy.allclose([abs(x[0]) for x in res], refci))
        self.assertEqual([x[1:] for x in res], refstr)

        res = fci.addons.large_ci(ci0, norb, nelec, tol=.1, return_strs=False)
        refa = numpy.array(((0,1,2), (0,1,2), (0,1,3), (0,1,3), (0,2,4)))
        refb = numpy.array(((0,1,2), (0,1,3), (0,1,2), (0,1,3), (0,2,4)))
        self.assertTrue(numpy.all([x[1] for x in res] == refa))
        self.assertTrue(numpy.all([x[2] for x in res] == refb))

        na = fci.cistring.num_strings(6, 3)
        numpy.random.seed(9)
        ci1 = numpy.random.random((na,na))
        ci1 /= numpy.linalg.norm(ci1)
        res = fci.addons.large_ci(ci1, 6, (3,3), tol=.2)
        self.assertEqual(res[0][1:], ('0b110100', '0b1101'))
//  [python]
def _filterNegativeValues(
        data: numpy.ndarray | numbers.Number | None,
    ) -> numpy.ndarray | numbers.Number | None:
        """Returns data with negative values to 0"""
        if data is None:
            return None

        # Convert data to array to avoid specific case for complex scalar
        if numpy.all(numpy.asarray(data) >= 0):
            return data

        return numpy.clip(data, 0, None)
//  [python]
def _function(self, data, *args, **kwargs):
        out = np.ndarray((data.size, 4))
        coord = self.coord ** (1 / self._gamma) # gamma from LinearSegmentedColormap
        assert self.ncoord in (1,4), "require consisient set of gamma"
        if self.ncoord == 1:
            # use np.piecwise instead?
            color0 = self.color[0,:]
            coord0 = coord[0,0]
            for i in xrange(1, self.n):
                if self.model[i-1] != self.model[i]:
                    color0[0:3] = self.model[i].inverse()(self.model[i-1](color0[0:3]))
                color1 = self.color[i,:]
                coord1 = coord[i,0]
                if coord0 < coord1: # allow discontinuous maps
                    ind = np.logical_and(data >= coord[i-1], data <= coord[i]) 
                    if np.count_nonzero(ind):
                        dcolor = color1 - color0
                        dcoord = coord1 - coord0
                        colcoord = (data[ind] - coord0) / dcoord
                        for j in xrange(4):
                            out[ind,j] = color0[j] + self.gamma[i,j](colcoord)*dcolor[j]
                        if self.model[i] != _color_models['RGB']:
                            out[ind,0:3] = self.model[i](out[ind,0:3])
                color0 = color1
                coord0 = coord1
        else:
            assert np.alltrue(self.model[0] == self.model[:]),'All color models need to be equal if using independent coordinates'
            for j in xrange(4):
                coord0 = coord[0, j]
                color0 = self.color[0,j]
                for i in xrange(1, self.n):
                    color1 = self.color[i,j]
                    coord1 = coord[i, j]
                    if coord0 < coord1: # allow discontinuous maps
                        ind = np.logical_and(data >= coord0, data <= coord1) 
                        if np.count_nonzero(ind):
                            dcolor = color1 - color0
                            dcoord = coord1 - coord0                            
                            colcoord = (data[ind] - coord0) / dcoord
                            out[ind, j] = color0 + self.gamma[i,j](colcoord)*dcolor
                    color0 = color1
                    coord0 = coord1
            if self.model[0] != _color_models['RGB']:
                 # transform only valid data
                 ind = np.logical_and(data >= 0, data <= 1)
                 out[ind,0:3] = self.model[i](out[ind,0:3])        
        return out
//  [python]
def _function(self, data, *args, **kwargs):
        out = np.ndarray((data.size, 4))
        coord = self.coord ** (1 / self._gamma) # gamma from LinearSegmentedColormap
        assert self.ncoord in (1,4), "require consisient set of gamma"
        if self.ncoord == 1:
            # use np.piecwise instead?
            color0 = self.color[0,:]
            coord0 = coord[0,0]
            for i in xrange(1, self.n):
                if self.model[i-1] != self.model[i]:
                    color0[0:3] = self.model[i].inverse()(self.model[i-1](color0[0:3]))
                color1 = self.color[i,:]
                coord1 = coord[i,0]
                if coord0 < coord1: # allow discontinuous maps
                    ind = np.logical_and(data >= coord[i-1], data <= coord[i]) 
                    if np.count_nonzero(ind):
                        dcolor = color1 - color0
                        dcoord = coord1 - coord0
                        colcoord = (data[ind] - coord0) / dcoord
                        for j in xrange(4):
                            out[ind,j] = color0[j] + self.gamma[i,j](colcoord)*dcolor[j]
                        if self.model[i] != _color_models['RGB']:
                            out[ind,0:3] = self.model[i](out[ind,0:3])
                color0 = color1
                coord0 = coord1
        else:
            assert np.all(self.model[0] == self.model[:]),'All color models need to be equal if using independent coordinates'
            for j in xrange(4):
                coord0 = coord[0, j]
                color0 = self.color[0,j]
                for i in xrange(1, self.n):
                    color1 = self.color[i,j]
                    coord1 = coord[i, j]
                    if coord0 < coord1: # allow discontinuous maps
                        ind = np.logical_and(data >= coord0, data <= coord1) 
                        if np.count_nonzero(ind):
                            dcolor = color1 - color0
                            dcoord = coord1 - coord0                            
                            colcoord = (data[ind] - coord0) / dcoord
                            out[ind, j] = color0 + self.gamma[i,j](colcoord)*dcolor
                    color0 = color1
                    coord0 = coord1
            if self.model[0] != _color_models['RGB']:
                 # transform only valid data
                 ind = np.logical_and(data >= 0, data <= 1)
                 out[ind,0:3] = self.model[i](out[ind,0:3])        
        return out
//  [python]
def smart_where(condition, x_fn, y_fn):
  """As tf.where, but only calls x_fn/y_fn when condition not statically known.

  IMPORTANT: Since this avoids executing the inoperative branch when possible,
  it will not necessarily broadcast `x_fn()` with `y_fn()`, so it is imperative
  that they return `Tensor`s which broadcast with `condition` to the same final
  shape.

  Args:
    condition: A `bool` Tensor.
    x_fn: A callable returning a `Tensor`, for locations where `condition` is
      `True`.
    y_fn: A callable returning a `Tensor`, for locations where `condition` is
      `False`.

  Returns:
    A `Tensor` equivalent to `tf.where(condition, x_fn(), y_fn())`.
  """
  cond_static = _get_static_value(condition)
  if cond_static is not None:
    if np.size(cond_static) == 1 and cond_static in (0, 1, False, True):
      return x_fn() if bool(cond_static) else y_fn()
    elif isinstance(cond_static, (np.ndarray, np.generic)):
      if np.all(cond_static):
        x = x_fn()
        return tf.broadcast_to(
            x, tf.broadcast_dynamic_shape(tf.shape(x), tf.shape(condition)))
      elif not np.any(cond_static):
        y = y_fn()
        return tf.broadcast_to(
            y, tf.broadcast_dynamic_shape(tf.shape(y), tf.shape(condition)))
  return tf.where(condition, x_fn(), y_fn())
//  [python]
def __init__(self, endog, exog, weights=1.0, check_endog=False,
                 check_weights=False):
        self.endog = endog
        self.exog = exog
        self.weights = weights
        w_half = np.sqrt(weights)
        if check_weights:
            if not np.all(np.isfinite(w_half)):
                raise ValueError(self.msg.format('weights'))

        if check_endog:
            if not np.all(np.isfinite(endog)):
                raise ValueError(self.msg.format('endog'))

        self.wendog = w_half * endog
        if np.isscalar(weights):
            self.wexog = w_half * exog
        else:
            self.wexog = np.asarray(w_half)[:, None] * exog
//  [python]
def _finalize(self):
        citype = self.__class__.__name__
        if n.all(self.converged):
            logger.info(self, '%s converged', citype)
        else:
            logger.info(self, '%s not converged', citype)
        if self.nroots > 1:
            for i,e in enumerate(self.e_tot):
                logger.note(self, '%s root %d  E = %.16g', citype, i, e)
        else:
            logger.note(self, 'E(%s) = %.16g  E_corr = %.16g',
                        citype, self.e_tot, self.e_corr)
        return self
//  [python]
def _finalize(self):
        citype = self.__class__.__name__
        if numpy.all(self.converged):
            logger.info(self, '%s converged', citype)
        else:
            logger.info(self, '%s not converged', citype)
        if self.nroots > 1:
            for i,e in enumerate(self.e_tot):
                logger.note(self, '%s root %d  E = %.16g', citype, i, e)
        else:
            logger.note(self, 'E(%s) = %.16g  E_corr = %.16g',
                        citype, self.e_tot, self.e_corr)
        return self
//  [python]
def take(
        self, indices: Sequence_[int], allow_fill: bool = False, fill_value: bool = None
    ) -> "PandasArrayExtensionArray":
        indices: np.ndarray = np.asarray(indices, dtype=int)
        if allow_fill:
            fill_value = (
                self.dtype.na_value if fill_value is None else np.asarray(fill_value, dtype=self.dtype.value_type)
            )
            mask = indices == -1
            if (indices < -1).any():
                raise ValueError("Invalid value in `indices`, must be all >= -1 for `allow_fill` is True")
            elif len(self) > 0:
                pass
            elif not np.alltrue(mask):
                raise IndexError("Invalid take for empty PandasArrayExtensionArray, must be all -1.")
            else:
                data = np.array([fill_value] * len(indices), dtype=self.dtype.value_type)
                return PandasArrayExtensionArray(data, copy=False)
        took = self._data.take(indices, axis=0)
        if allow_fill and mask.any():
            took[mask] = [fill_value] * np.sum(mask)
        return PandasArrayExtensionArray(took, copy=False)
//  [python]
def take(
        self, indices: Sequence_[int], allow_fill: bool = False, fill_value: bool = None
    ) -> "PandasArrayExtensionArray":
        indices: np.ndarray = np.asarray(indices, dtype=int)
        if allow_fill:
            fill_value = (
                self.dtype.na_value if fill_value is None else np.asarray(fill_value, dtype=self.dtype.value_type)
            )
            mask = indices == -1
            if (indices < -1).any():
                raise ValueError("Invalid value in `indices`, must be all >= -1 for `allow_fill` is True")
            elif len(self) > 0:
                pass
            elif not np.all(mask):
                raise IndexError("Invalid take for empty PandasArrayExtensionArray, must be all -1.")
            else:
                data = np.array([fill_value] * len(indices), dtype=self.dtype.value_type)
                return PandasArrayExtensionArray(data, copy=False)
        took = self._data.take(indices, axis=0)
        if allow_fill and mask.any():
            took[mask] = [fill_value] * np.sum(mask)
        return PandasArrayExtensionArray(took, copy=False)
//  [python]
def test4d(self):
        g = Graph()
        oper = OpThresholdOneLevel(graph=g)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.LowThreshold.setValue(0.5)
        oper.InputImage.setValue(self.data)

        output = oper.Output[:].wait()
        assert n.all(output.shape == self.data.shape)

        clusters = self.generateData((self.nz, self.ny, self.nx))

        cluster1 = numpy.logical_and(output, clusters[0])
        assert n.any(cluster1 != 0)

        oper.MinSize.setValue(5)
        output = oper.Output[:].wait()
        cluster1 = numpy.logical_and(output, clusters[0])
        assert n.all(cluster1 == 0)

        cluster4 = numpy.logical_and(output.squeeze(), clusters[3])
        assert n.all(cluster4 == 0)

        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert n.all(cluster5 == 0)
        oper.LowThreshold.setValue(0.2)
        output = oper.Output[:].wait()
        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert n.any(cluster5 != 0)
//  [python]
def test4d(self):
        g = Graph()
        oper = OpThresholdOneLevel(graph=g)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.LowThreshold.setValue(0.5)
        oper.InputImage.setValue(self.data)

        output = oper.Output[:].wait()
        assert numpy.all(output.shape == self.data.shape)

        clusters = self.generateData((self.nz, self.ny, self.nx))

        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.any(cluster1 != 0)

        oper.MinSize.setValue(5)
        output = oper.Output[:].wait()
        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.all(cluster1 == 0)

        cluster4 = numpy.logical_and(output.squeeze(), clusters[3])
        assert numpy.all(cluster4 == 0)

        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.all(cluster5 == 0)
        oper.LowThreshold.setValue(0.2)
        output = oper.Output[:].wait()
        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.any(cluster5 != 0)
//  [python]
def testNoOp(self):
        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(self.data5d[0:1, ..., 0:1])
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(self.data5d.size)
        oper5d.HighThreshold.setValue(-0.01)
        oper5d.LowThreshold.setValue(-0.01)

        out5d = oper5d.Output[:].wait()
        assert numpy.all(out5d > 0), "{}% elements <= 0".format((out5d <= 0).sum() / float(out5d.size) * 100)
//  [python]
def checkResult(self, result):
        result = result.withAxes(*"zyxc")
        clusters = self.generateData((self.nz, self.ny, self.nx))

        failed = 0
        msg = []

        # the cluster 2 and 5 must pass, others mustn't
        for i in (1,):
            cluster = result[clusters[i] != 0]
            if not n.all(cluster != 0):
                failed += 1
                msg.append("Cluster {} did not pass.".format(i + 1))
        for i in (0, 2, 3, 4):
            cluster = result[clusters[i] != 0]
            if not n.all(cluster == 0):
                failed += 1
                msg.append("Cluster {} passed.".format(i + 1))

        assert failed == 0, "\n".join(msg)
//  [python]
def checkResult(self, result):
        result = result.withAxes(*"zyxc")
        clusters = self.generateData((self.nz, self.ny, self.nx))

        failed = 0
        msg = []

        # the cluster 2 and 5 must pass, others mustn't
        for i in (1,):
            cluster = result[clusters[i] != 0]
            if not numpy.all(cluster != 0):
                failed += 1
                msg.append("Cluster {} did not pass.".format(i + 1))
        for i in (0, 2, 3, 4):
            cluster = result[clusters[i] != 0]
            if not numpy.all(cluster == 0):
                failed += 1
                msg.append("Cluster {} passed.".format(i + 1))

        assert failed == 0, "\n".join(msg)
//  [python]
def testNoOp(self):
        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(self.data5d)
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(self.data5d.size)
        oper5d.LowThreshold.setValue(-0.01)
        oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        oper5d.CurOperator.setValue(self.curOperator)

        out5d = oper5d.Output[:].wait()
        numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape)

        # the image should be white, because of negative threshold
        assert n.all(out5d > 0), "{}% elements <= 0".format((out5d <= 0).sum() / float(out5d.size) * 100)

        oper5d.LowThreshold.setValue(1.01)
        out5d = oper5d.Output[:].wait()

        # the image should be black, because of threshold larger than 1
        assert n.all(out5d == 0), "{}% elements > 0".format((out5d > 0).sum() / float(out5d.size) * 100)
//  [python]
def testNoOp(self):
        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(self.data5d)
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(self.data5d.size)
        oper5d.LowThreshold.setValue(-0.01)
        oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        oper5d.CurOperator.setValue(self.curOperator)

        out5d = oper5d.Output[:].wait()
        numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape)

        # the image should be white, because of negative threshold
        assert numpy.all(out5d > 0), "{}% elements <= 0".format((out5d <= 0).sum() / float(out5d.size) * 100)

        oper5d.LowThreshold.setValue(1.01)
        out5d = oper5d.Output[:].wait()

        # the image should be black, because of threshold larger than 1
        assert numpy.all(out5d == 0), "{}% elements > 0".format((out5d > 0).sum() / float(out5d.size) * 100)
//  [python]
def test5d(self):
        g = Graph()
        oper = OpThresholdTwoLevels(graph=g)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper.LowThreshold.setValue(0.5)
        oper.InputImage.setValue(self.data5d)

        output = oper.Output[:].wait()
        assert n.all(output.shape == self.data5d.shape)

        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")

        clusters = self.generateData((self.nz, self.ny, self.nx))

        cluster1 = numpy.logical_and(output, clusters[0])
        assert n.any(cluster1 != 0)

        oper.MinSize.setValue(5)
        output = oper.Output[:].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster1 = numpy.logical_and(output, clusters[0])
        assert n.all(cluster1 == 0)

        cluster4 = numpy.logical_and(output.squeeze(), clusters[3])
        assert n.all(cluster4 == 0)

        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert n.all(cluster5 == 0)
        oper.LowThreshold.setValue(0.2)
        output = oper.Output[:].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert n.any(cluster5 != 0)
//  [python]
def test5d(self):
        g = Graph()
        oper = OpThresholdTwoLevels(graph=g)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper.LowThreshold.setValue(0.5)
        oper.InputImage.setValue(self.data5d)

        output = oper.Output[:].wait()
        assert numpy.all(output.shape == self.data5d.shape)

        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")

        clusters = self.generateData((self.nz, self.ny, self.nx))

        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.any(cluster1 != 0)

        oper.MinSize.setValue(5)
        output = oper.Output[:].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.all(cluster1 == 0)

        cluster4 = numpy.logical_and(output.squeeze(), clusters[3])
        assert numpy.all(cluster4 == 0)

        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.all(cluster5 == 0)
        oper.LowThreshold.setValue(0.2)
        output = oper.Output[:].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.any(cluster5 != 0)
//  [python]
def testFunnyAxes(self):
        vol = self.data.withAxes(*"ztxcy")
        g = Graph()
        oper = OpThresholdTwoLevels(graph=g)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.LowThreshold.setValue(0.5)
        oper.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper.InputImage.setValue(vol)

        output = oper.Output[:, 0, :, 0, :].wait()
        assert n.all(output.shape == vol.shape)

        clusters = self.generateData((self.nz, self.ny, self.nx))
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")

        cluster1 = numpy.logical_and(output, clusters[0])
        assert n.any(cluster1 != 0)

        oper.MinSize.setValue(5)
        output = oper.Output[:, 0, :, 0, :].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster1 = numpy.logical_and(output, clusters[0])
        assert n.all(cluster1 == 0)

        cluster4 = numpy.logical_and(output.squeeze(), clusters[3])
        assert n.all(cluster4 == 0)

        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert n.all(cluster5 == 0)
        oper.LowThreshold.setValue(0.2)
        output = oper.Output[:, 0, :, 0, :].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert n.any(cluster5 != 0)
//  [python]
def testFunnyAxes(self):
        vol = self.data.withAxes(*"ztxcy")
        g = Graph()
        oper = OpThresholdTwoLevels(graph=g)
        oper.MinSize.setValue(self.minSize)
        oper.MaxSize.setValue(self.maxSize)
        oper.LowThreshold.setValue(0.5)
        oper.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper.InputImage.setValue(vol)

        output = oper.Output[:, 0, :, 0, :].wait()
        assert numpy.all(output.shape == vol.shape)

        clusters = self.generateData((self.nz, self.ny, self.nx))
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")

        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.any(cluster1 != 0)

        oper.MinSize.setValue(5)
        output = oper.Output[:, 0, :, 0, :].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster1 = numpy.logical_and(output, clusters[0])
        assert numpy.all(cluster1 == 0)

        cluster4 = numpy.logical_and(output.squeeze(), clusters[3])
        assert numpy.all(cluster4 == 0)

        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.all(cluster5 == 0)
        oper.LowThreshold.setValue(0.2)
        output = oper.Output[:, 0, :, 0, :].wait()
        output = vigra.taggedView(output, axistags=oper.Output.meta.axistags)
        output = output.withAxes(*"zyx")
        cluster5 = numpy.logical_and(output.squeeze(), clusters[2])
        assert numpy.any(cluster5 != 0)
//  [python]
def testNoOp(self):
        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(self.data5d)
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(self.data5d.size)
        oper5d.HighThreshold.setValue(-0.01)
        oper5d.LowThreshold.setValue(-0.01)
        oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        oper5d.CurOperator.setValue(1)

        out5d = oper5d.Output[:].wait()
        big = vigra.taggedView(oper5d.BigRegions[:].wait(), axistags=oper5d.BigRegions.meta.axistags)
        assert n.all(big > 0)
        small = vigra.taggedView(oper5d.SmallRegions[:].wait(), axistags=oper5d.SmallRegions.meta.axistags)
        assert n.all(small > 0)
        numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape)
        assert n.all(out5d > 0)
//  [python]
def testNoOp(self):
        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(self.data5d)
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(self.data5d.size)
        oper5d.HighThreshold.setValue(-0.01)
        oper5d.LowThreshold.setValue(-0.01)
        oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        oper5d.CurOperator.setValue(1)

        out5d = oper5d.Output[:].wait()
        big = vigra.taggedView(oper5d.BigRegions[:].wait(), axistags=oper5d.BigRegions.meta.axistags)
        assert numpy.all(big > 0)
        small = vigra.taggedView(oper5d.SmallRegions[:].wait(), axistags=oper5d.SmallRegions.meta.axistags)
        assert numpy.all(small > 0)
        numpy.testing.assert_array_equal(out5d.shape, self.data5d.shape)
        assert numpy.all(out5d > 0)
//  [python]
def testChannel(self):
        shape = (200, 100, 1)
        tags = "xyc"
        zeros = numpy.zeros(shape)
        ones = numpy.ones(shape)
        vol = numpy.concatenate((zeros, ones), axis=2)
        vol = vigra.taggedView(vol, axistags=tags)
        assert vol.shape[vol.axistags.index("c")] == 2

        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(vol)
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(zeros.size)
        oper5d.HighThreshold.setValue(0.5)
        oper5d.LowThreshold.setValue(0.5)
        oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        oper5d.CurOperator.setValue(1)

        # channel 0
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        out5d = oper5d.Output[:].wait()
        assert numpy.all(out5d == 0), str(numpy.histogram(out5d))

        # channel 0
        oper5d.Channel.setValue(1)
        oper5d.CoreChannel.setValue(1)
        out5d = oper5d.Output[:].wait()
        assert numpy.all(out5d > 0), str(numpy.histogram(out5d))
//  [python]
def test_rand(self):
        result = iarandom.RNG(0).rand(10, 20, 3)
        assert result.dtype.name == "float32"
        assert result.shape == (10, 20, 3)
        assert n.all(result >= 0.0)
        assert n.all(result <= 1.0)
        assert n.any(result > 0.0)
        assert n.any(result < 1.0)
//  [python]
def test_rand(self):
        result = iarandom.RNG(0).rand(10, 20, 3)
        assert result.dtype.name == "float32"
        assert result.shape == (10, 20, 3)
        assert np.all(result >= 0.0)
        assert np.all(result <= 1.0)
        assert np.any(result > 0.0)
        assert np.any(result < 1.0)
//  [python]
def test_randint(self):
        result = iarandom.RNG(0).randint(10, 100, size=(10, 20, 3))
        assert result.dtype.name == "int32"
        assert result.shape == (10, 20, 3)
        assert n.all(result >= 10)
        assert n.all(result <= 99)
        assert n.any(result > 10)
        assert n.any(result < 99)
//  [python]
def test_randint(self):
        result = iarandom.RNG(0).randint(10, 100, size=(10, 20, 3))
        assert result.dtype.name == "int32"
        assert result.shape == (10, 20, 3)
        assert np.all(result >= 10)
        assert np.all(result <= 99)
        assert np.any(result > 10)
        assert np.any(result < 99)
//  [python]
def test_random_integers__no_high(self):
        result = iarandom.RNG(0).random_integers(100, size=(10, 20, 3))
        assert result.dtype.name == "int32"
        assert result.shape == (10, 20, 3)
        assert n.all(result >= 1)
        assert n.all(result <= 100)
        assert n.any(result > 1)
        assert n.any(result < 100)
//  [python]
def test_random_integers__no_high(self):
        result = iarandom.RNG(0).random_integers(100, size=(10, 20, 3))
        assert result.dtype.name == "int32"
        assert result.shape == (10, 20, 3)
        assert np.all(result >= 1)
        assert np.all(result <= 100)
        assert np.any(result > 1)
        assert np.any(result < 100)
//  [python]
def test_random_integers(self):
        result = iarandom.RNG(0).random_integers(10, 100, size=(10, 20, 3))
        assert result.dtype.name == "int32"
        assert result.shape == (10, 20, 3)
        assert n.all(result >= 10)
        assert n.all(result <= 100)
        assert n.any(result > 10)
        assert n.any(result < 100)
//  [python]
def test_random_integers(self):
        result = iarandom.RNG(0).random_integers(10, 100, size=(10, 20, 3))
        assert result.dtype.name == "int32"
        assert result.shape == (10, 20, 3)
        assert np.all(result >= 10)
        assert np.all(result <= 100)
        assert np.any(result > 10)
        assert np.any(result < 100)
//  [python]
def testSingletonx(self):
        shape = (200, 100, 1)
        tags = "xyc"
        zeros = numpy.zeros(shape)
        ones = numpy.ones(shape)
        vol = numpy.concatenate((zeros, ones), axis=2)
        vol = vigra.taggedView(vol, axistags=tags)
        assert vol.shape[vol.axistags.index("c")] == 2

        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(vol)
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(zeros.size)
        oper5d.HighThreshold.setValue(0.5)
        oper5d.LowThreshold.setValue(0.5)
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        oper5d.CurOperator.setValue(1)

        # no smoothing
        oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        out5d = oper5d.Output[:].wait()
        assert n.all(out5d == 0), str(numpy.histogram(out5d))

        # smoothing
        oper5d.SmootherSigma.setValue({"z": 1.0, "y": 1.0, "x": 1.0})
        out5d = oper5d.Output[:].wait()
        assert n.all(out5d == 0), str(numpy.histogram(out5d))
//  [python]
def testSingletonx(self):
        shape = (200, 100, 1)
        tags = "xyc"
        zeros = numpy.zeros(shape)
        ones = numpy.ones(shape)
        vol = numpy.concatenate((zeros, ones), axis=2)
        vol = vigra.taggedView(vol, axistags=tags)
        assert vol.shape[vol.axistags.index("c")] == 2

        oper5d = OpThresholdTwoLevels(graph=Graph())
        oper5d.InputImage.setValue(vol)
        oper5d.MinSize.setValue(1)
        oper5d.MaxSize.setValue(zeros.size)
        oper5d.HighThreshold.setValue(0.5)
        oper5d.LowThreshold.setValue(0.5)
        oper5d.Channel.setValue(0)
        oper5d.CoreChannel.setValue(0)
        oper5d.CurOperator.setValue(1)

        # no smoothing
        oper5d.SmootherSigma.setValue({"z": 0.0, "y": 0.0, "x": 0.0})
        out5d = oper5d.Output[:].wait()
        assert numpy.all(out5d == 0), str(numpy.histogram(out5d))

        # smoothing
        oper5d.SmootherSigma.setValue({"z": 1.0, "y": 1.0, "x": 1.0})
        out5d = oper5d.Output[:].wait()
        assert numpy.all(out5d == 0), str(numpy.histogram(out5d))
//  [python]
def test_random_sample(self):
        result = iarandom.RNG(0).random_sample((10, 20, 3))
        assert result.dtype.name == "float64"
        assert result.shape == (10, 20, 3)
        assert n.all(result >= 0.0)
        assert n.all(result <= 1.0)
        assert n.any(result > 0.0)
        assert n.any(result < 1.0)
//  [python]
def test_random_sample(self):
        result = iarandom.RNG(0).random_sample((10, 20, 3))
        assert result.dtype.name == "float64"
        assert result.shape == (10, 20, 3)
        assert np.all(result >= 0.0)
        assert np.all(result <= 1.0)
        assert np.any(result > 0.0)
        assert np.any(result < 1.0)
//  [python]
def test_tomaxint(self):
        result = iarandom.RNG(0).tomaxint((10, 200, 3))
        assert result.dtype.name == "int32"
        assert result.shape == (10, 200, 3)
        assert np.all(result >= 0)
        assert np.any(result > 10000)
//  [python]
def test_sampled_values_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        result = iarandom.polyfill_integers(gen, 1, 10, size=(1000,),
                                            endpoint=False)

        assert result.dtype.name == "int32"
        assert np.alltrue(result >= 1)
        assert np.alltrue(result < 10)
//  [python]
def test_sampled_values_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        result = iarandom.polyfill_integers(gen, 1, 10, size=(1000,),
                                            endpoint=False)

        assert result.dtype.name == "int32"
        assert np.all(result >= 1)
        assert np.all(result < 10)
//  [python]
def test_call_np117(self):
        gen = iarandom.convert_seed_to_generator(1)
        gen_copy = copylib.deepcopy(gen)

        result = iarandom.derive_generators_(gen, 2)

        assert len(result) == 2
        assert all([isinstance(gen, np.random.Generator)
                       for gen in result])
        assert not iarandom.is_generator_equal_to(result[0], gen_copy)
        assert not iarandom.is_generator_equal_to(result[1], gen_copy)
        assert not iarandom.is_generator_equal_to(result[0], result[1])
        # derive should advance state
        assert not iarandom.is_generator_equal_to(gen, gen_copy)
//  [python]
def test_call_np117(self):
        gen = iarandom.convert_seed_to_generator(1)
        gen_copy = copylib.deepcopy(gen)

        result = iarandom.derive_generators_(gen, 2)

        assert len(result) == 2
        assert np.all([isinstance(gen, np.random.Generator)
                       for gen in result])
        assert not iarandom.is_generator_equal_to(result[0], gen_copy)
        assert not iarandom.is_generator_equal_to(result[1], gen_copy)
        assert not iarandom.is_generator_equal_to(result[0], result[1])
        # derive should advance state
        assert not iarandom.is_generator_equal_to(gen, gen_copy)
//  [python]
def test_sampled_values_with_endpoint_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        result = iarandom.polyfill_integers(gen, 1, 10, size=(1000,),
                                            endpoint=True)

        assert result.dtype.name == "int32"
        assert n.all(result >= 1)
        assert n.all(result <= 10)
//  [python]
def test_sampled_values_with_endpoint_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        result = iarandom.polyfill_integers(gen, 1, 10, size=(1000,),
                                            endpoint=True)

        assert result.dtype.name == "int32"
        assert np.all(result >= 1)
        assert np.all(result <= 10)
//  [python]
def test_sampled_values_np116(self):
        gen = np.random.RandomState(1)

        result = iarandom.polyfill_integers(gen, 1, 10, size=(1000,),
                                            endpoint=False)

        assert result.dtype.name == "int32"
        assert np.all(result >= 1)
        assert np.all(result < 10)
//  [python]
def test_call_np116(self):
        gen = np.random.RandomState(1)
        gen_copy = copylib.deepcopy(gen)

        result = iarandom.derive_generators_(gen, 2)

        assert len(result) == 2
        assert n.all([isinstance(gen, np.random.RandomState)
                       for gen in result])
        assert not iarandom.is_generator_equal_to(result[0], gen_copy)
        assert not iarandom.is_generator_equal_to(result[1], gen_copy)
        assert not iarandom.is_generator_equal_to(result[0], result[1])
        # derive should advance state
        assert not iarandom.is_generator_equal_to(gen, gen_copy)
//  [python]
def test_call_np116(self):
        gen = np.random.RandomState(1)
        gen_copy = copylib.deepcopy(gen)

        result = iarandom.derive_generators_(gen, 2)

        assert len(result) == 2
        assert np.all([isinstance(gen, np.random.RandomState)
                       for gen in result])
        assert not iarandom.is_generator_equal_to(result[0], gen_copy)
        assert not iarandom.is_generator_equal_to(result[1], gen_copy)
        assert not iarandom.is_generator_equal_to(result[0], result[1])
        # derive should advance state
        assert not iarandom.is_generator_equal_to(gen, gen_copy)
//  [python]
def test_sampled_values_np116(self):
        gen = np.random.RandomState(1)

        result = iarandom.polyfill_random(gen, size=(1000,))

        assert result.dtype.name == "float32"
        assert n.all(result >= 0)
        assert n.all(result < 1.0)
//  [python]
def test_sampled_values_np116(self):
        gen = np.random.RandomState(1)

        result = iarandom.polyfill_random(gen, size=(1000,))

        assert result.dtype.name == "float32"
        assert np.all(result >= 0)
        assert np.all(result < 1.0)
//  [python]
def test_sampled_values_with_endpoint_np116(self):
        gen = np.random.RandomState(1)

        result = iarandom.polyfill_integers(gen, 1, 10, size=(1000,),
                                            endpoint=True)

        assert result.dtype.name == "int32"
        assert np.all(result >= 1)
        assert np.all(result <= 10)
//  [python]
def test_sampled_values_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        result = iarandom.polyfill_random(gen, size=(1000,))

        assert result.dtype.name == "float32"
        assert np.alltrue(result >= 0)
        assert np.alltrue(result < 1.0)
//  [python]
def test_sampled_values_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        result = iarandom.polyfill_random(gen, size=(1000,))

        assert result.dtype.name == "float32"
        assert np.all(result >= 0)
        assert np.all(result < 1.0)
//  [python]
def test_unary_PyUFunc_O_O_method(self, foo=foo):
        x = np.full(10, foo(), dtype=object)
        assert_(n.all(np.conjugate(x) == True))
//  [python]
def test_unary_PyUFunc_O_O_method(self, foo=foo):
        x = np.full(10, foo(), dtype=object)
        assert_(np.all(np.conjugate(x) == True))
//  [python]
def test_sampled_values_with_out_arg_np116(self):
        gen = np.random.RandomState(1)

        out = np.zeros((1000,), dtype="float32")
        result = iarandom.polyfill_random(gen, size=(1000,), out=out)

        assert result.dtype.name == "float32"
        assert n.all(result >= 0)
        assert n.all(result < 1.0)

        assert n.any(out > 0.9)
        assert n.all(out >= 0)
        assert n.all(out < 1.0)
//  [python]
def test_sampled_values_with_out_arg_np116(self):
        gen = np.random.RandomState(1)

        out = np.zeros((1000,), dtype="float32")
        result = iarandom.polyfill_random(gen, size=(1000,), out=out)

        assert result.dtype.name == "float32"
        assert np.all(result >= 0)
        assert np.all(result < 1.0)

        assert np.any(out > 0.9)
        assert np.all(out >= 0)
        assert np.all(out < 1.0)
//  [python]
def fit(self, X, y, sample_weight=None):
        """
        Fit the metric 

        :param X: data, numpy.array with size [n_samples, n_features] 
        :param y: target, numpy.array with size [n_samples]
        :param sample_weight: sample weights, numpy.array with size [n_samples]
        :return: self 
        """
        assert n.all(n.in1d(y, [0, 1]))
        self.y_signed = 2 * y - 1
        self.sample_weight = sample_weight
        return self
//  [python]
def fit(self, X, y, sample_weight=None):
        """
        Fit the metric 

        :param X: data, numpy.array with size [n_samples, n_features] 
        :param y: target, numpy.array with size [n_samples]
        :param sample_weight: sample weights, numpy.array with size [n_samples]
        :return: self 
        """
        assert numpy.all(numpy.in1d(y, [0, 1]))
        self.y_signed = 2 * y - 1
        self.sample_weight = sample_weight
        return self
//  [python]
def test_sampled_values_with_out_arg_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        out = np.zeros((1000,), dtype="float32")
        result = iarandom.polyfill_random(gen, size=(1000,), out=out)

        assert result.dtype.name == "float32"
        assert n.all(result >= 0)
        assert n.all(result < 1.0)

        assert n.any(out > 0.9)
        assert n.all(out >= 0)
        assert n.all(out < 1.0)
//  [python]
def test_sampled_values_with_out_arg_np117(self):
        gen = np.random.Generator(iarandom.BIT_GENERATOR(1))

        out = np.zeros((1000,), dtype="float32")
        result = iarandom.polyfill_random(gen, size=(1000,), out=out)

        assert result.dtype.name == "float32"
        assert np.all(result >= 0)
        assert np.all(result < 1.0)

        assert np.any(out > 0.9)
        assert np.all(out >= 0)
        assert np.all(out < 1.0)
//  [python]
def get_buckets(dist, num_buckets):
    dist = dist.cpu().data.numpy()

    min_dist = np.min(dist)
    max_dist = np.max(dist)
    bucket_size = (max_dist - min_dist) / num_buckets
    thresholds = bucket_size * np.arange(num_buckets)

    bucket_idx = []
    for i in range(num_buckets):
        if i < num_buckets - 1:
            idx = np.where(np.logical_and(dist > thresholds[i],
                                             dist <= thresholds[i + 1]))[0]
        else:
            idx = np.where(dist > thresholds[i])[0]
        bucket_idx.append(idx)

    return bucket_idx, thresholds
//  [python]
def get_buckets(dist, num_buckets):
    dist = dist.cpu().data.numpy()

    min_dist = np.min(dist)
    max_dist = np.max(dist)
    bucket_size = (max_dist - min_dist) / num_buckets
    thresholds = bucket_size * np.arange(num_buckets)

    bucket_idx = []
    for i in range(num_buckets):
        if i < num_buckets - 1:
            idx = np.where(np.all(np.vstack((dist > thresholds[i],
                                             dist <= thresholds[i + 1])), 0))[0]
        else:
            idx = np.where(dist > thresholds[i])[0]
        bucket_idx.append(idx)

    return bucket_idx, thresholds
//  [python]
def __init__(self, queries, relevances, maximal=100000):
        self.queries = queries
        self.relevances = relevances
        assert n.all(n.diff(queries) >= 0)
        _, self.qid_index, self.queries = n.unique(
            self.queries, return_index=True, return_inverse=True)
        prev_q = None
        ranks = n.zeros(len(queries), dtype='int')
        # define available ranks for each query
        for i, q in enumerate(self.queries):
            if q != prev_q:
                ranks[i] = 0
                prev_q = q
            else:
                ranks[i] = ranks[i - 1] + 1
        gains = 2 ** relevances - 1
        # normalizing for empty query
        query_gains = n.bincount(self.queries, weights=gains)
        gains[query_gains[self.queries] == 0] = 1.
        # compute available discounts
        discounts = 1. / n.log2(2 + ranks) * (ranks < maximal)
        # sort documents by relevance separately in each query
        ideal_orders = n.argsort(self.queries * 100 - relevances)
        assert n.allclose(self.queries, self.queries[ideal_orders])
        dcg_ideal_gains = discounts * gains[ideal_orders]
        # ideal DCG for query
        query_gains = n.bincount(self.queries, weights=dcg_ideal_gains)
        query_gains = n.maximum(query_gains, 1e-6)
        self.normalized_discounts = discounts
        self.gains = gains / query_gains[self.queries]